vb21_struct.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Vboot 2.1 data structures
  6. *
  7. * Offsets should be padded to 32-bit boundaries, since some architectures
  8. * have trouble with accessing unaligned integers.
  9. */
  10. #ifndef VBOOT_REFERENCE_VB21_STRUCT_H_
  11. #define VBOOT_REFERENCE_VB21_STRUCT_H_
  12. #include <stdint.h>
  13. #include "2id.h"
  14. /*
  15. * Magic numbers used by vb21_struct_common.magic.
  16. *
  17. * All valid numbers should be listed here to avoid accidental overlap.
  18. * Numbers start at a large value, so that previous parsers (which stored
  19. * things like lengths and offsets at that field) will detect and reject new
  20. * structs as invalid.
  21. */
  22. enum vb21_struct_common_magic {
  23. /* "Vb2B" = vb21_keyblock.c.magic */
  24. VB21_MAGIC_KEYBLOCK = 0x42326256,
  25. /* "Vb2F" = vb21_fw_preamble.c.magic */
  26. VB21_MAGIC_FW_PREAMBLE = 0x46326256,
  27. /* "Vb2I" = vb21_packed_private_key.c.magic */
  28. VB21_MAGIC_PACKED_PRIVATE_KEY = 0x49326256,
  29. /* "Vb2K" = vb2_kernel_preamble.c.magic */
  30. VB21_MAGIC_KERNEL_PREAMBLE = 0x4b326256,
  31. /* "Vb2P" = vb21_packed_key.c.magic */
  32. VB21_MAGIC_PACKED_KEY = 0x50326256,
  33. /* "Vb2S" = vb21_signature.c.magic */
  34. VB21_MAGIC_SIGNATURE = 0x53326256,
  35. };
  36. /*
  37. * Generic struct header for all vboot2.1 structs. This makes it easy to
  38. * automatically parse and identify vboot structs (e.g., in futility). This
  39. * must be the first member of the parent vboot2.1 struct.
  40. */
  41. struct vb21_struct_common {
  42. /* Magic number; see vb21_struct_common_magic for expected values */
  43. uint32_t magic;
  44. /*
  45. * Parent struct version; see each struct for the expected value.
  46. *
  47. * How to handle struct version mismatches, if the parser is version
  48. * A.b and the data is version C.d:
  49. * 1) If A.b == C.d, we're good.
  50. * 2) If A != C, the data cannot be parsed at all.
  51. * 3) If b < d, C.d is a newer version of data which is backwards-
  52. * compatible to old parsers. We're good.
  53. * 4) If b > d, C.d is an older version of data. The parser should
  54. * use default values for fields added after version d. We're
  55. * good.
  56. *
  57. * Struct versions start at 3.0, since the highest version of the old
  58. * structures was 2.1. This way, there is no possibility of collision
  59. * for old code which depends on the version number.
  60. */
  61. uint16_t struct_version_major;
  62. uint16_t struct_version_minor;
  63. /*
  64. * Size of the parent structure and all its data, including the
  65. * description and any necessary padding. That is, all data must lie
  66. * in a contiguous region of <total_size> bytes starting at the first
  67. * byte of this header.
  68. */
  69. uint32_t total_size;
  70. /*
  71. * Size of the fixed portion of the parent structure. If a description
  72. * is present, it must start at this offset.
  73. */
  74. uint32_t fixed_size;
  75. /*
  76. * The object may contain an ASCII description following the fixed
  77. * portion of the structure. If it is present, it must be
  78. * null-terminated, and padded with 0 (null) bytes to a multiple of 32
  79. * bits.
  80. *
  81. * Size of ASCII description in bytes, counting null terminator and
  82. * padding (if any). Set 0 if no description is present. If non-zero,
  83. * there must be a null terminator (0) at offset (fixed_size +
  84. * desc_size - 1).
  85. */
  86. uint32_t desc_size;
  87. } __attribute__((packed));
  88. #define EXPECTED_VB21_STRUCT_COMMON_SIZE 20
  89. /* Current version of vb21_packed_key struct */
  90. #define VB21_PACKED_KEY_VERSION_MAJOR 3
  91. #define VB21_PACKED_KEY_VERSION_MINOR 0
  92. /*
  93. * Packed public key data
  94. *
  95. * The key data must be arranged like this:
  96. * 1) vb21_packed_key header struct h
  97. * 2) Key description (pointed to by h.c.fixed_size)
  98. * 3) Key data key (pointed to by h.key_offset)
  99. */
  100. struct vb21_packed_key {
  101. /* Common header fields */
  102. struct vb21_struct_common c;
  103. /* Offset of key data from start of this struct */
  104. uint32_t key_offset;
  105. /* Size of key data in bytes (NOT strength of key in bits) */
  106. uint32_t key_size;
  107. /* Signature algorithm used by the key (enum vb2_signature_algorithm) */
  108. uint16_t sig_alg;
  109. /*
  110. * Hash digest algorithm used with the key (enum vb2_hash_algorithm).
  111. * This is explicitly specified as part of the key to prevent use of a
  112. * strong key with a weak hash.
  113. */
  114. uint16_t hash_alg;
  115. /* Key version */
  116. uint32_t key_version;
  117. /* Key ID */
  118. struct vb2_id id;
  119. } __attribute__((packed));
  120. #define EXPECTED_VB21_PACKED_KEY_SIZE \
  121. (EXPECTED_VB21_STRUCT_COMMON_SIZE + 16 + EXPECTED_ID_SIZE)
  122. /* Current version of vb21_packed_private_key struct */
  123. #define VB21_PACKED_PRIVATE_KEY_VERSION_MAJOR 3
  124. #define VB21_PACKED_PRIVATE_KEY_VERSION_MINOR 0
  125. /*
  126. * Packed private key data
  127. *
  128. * The key data must be arranged like this:
  129. * 1) vb21_packed_private_key header struct h
  130. * 2) Key description (pointed to by h.c.fixed_size)
  131. * 3) Key data key (pointed to by h.key_offset)
  132. */
  133. struct vb21_packed_private_key {
  134. /* Common header fields */
  135. struct vb21_struct_common c;
  136. /* Offset of key data from start of this struct */
  137. uint32_t key_offset;
  138. /* Size of key data in bytes (NOT strength of key in bits) */
  139. uint32_t key_size;
  140. /* Signature algorithm used by the key (enum vb2_signature_algorithm) */
  141. uint16_t sig_alg;
  142. /*
  143. * Hash digest algorithm used with the key (enum vb2_hash_algorithm).
  144. * This is explicitly specified as part of the key to prevent use of a
  145. * strong key with a weak hash.
  146. */
  147. uint16_t hash_alg;
  148. /* Key ID */
  149. struct vb2_id id;
  150. } __attribute__((packed));
  151. #define EXPECTED_VB21_PACKED_PRIVATE_KEY_SIZE \
  152. (EXPECTED_VB21_STRUCT_COMMON_SIZE + 12 + EXPECTED_ID_SIZE)
  153. /* Current version of vb21_signature struct */
  154. #define VB21_SIGNATURE_VERSION_MAJOR 3
  155. #define VB21_SIGNATURE_VERSION_MINOR 0
  156. /*
  157. * Signature data
  158. *
  159. * The signature data must be arranged like this:
  160. * 1) vb21_signature header struct h
  161. * 2) Signature description (pointed to by h.c.fixed_size)
  162. * 3) Signature data (pointed to by h.sig_offset)
  163. */
  164. struct vb21_signature {
  165. /* Common header fields */
  166. struct vb21_struct_common c;
  167. /* Offset of signature data from start of this struct */
  168. uint32_t sig_offset;
  169. /* Size of signature data in bytes */
  170. uint32_t sig_size;
  171. /* Size of the data block which was signed in bytes */
  172. uint32_t data_size;
  173. /* Signature algorithm used (enum vb2_signature_algorithm) */
  174. uint16_t sig_alg;
  175. /* Hash digest algorithm used (enum vb2_hash_algorithm) */
  176. uint16_t hash_alg;
  177. /*
  178. * ID for the signature.
  179. *
  180. * If this is a keyblock signature entry, this is the ID of the key
  181. * used to generate this signature. This allows the firmware to
  182. * quickly determine which signature block (if any) goes with the key
  183. * being used by the firmware.
  184. *
  185. * If this is a preamble hash entry, this is the ID of the data type
  186. * being hashed. There is no key ID, because sig_alg=VB2_ALG_NONE.
  187. */
  188. struct vb2_id id;
  189. } __attribute__((packed));
  190. #define EXPECTED_VB21_SIGNATURE_SIZE \
  191. (EXPECTED_VB21_STRUCT_COMMON_SIZE + 16 + EXPECTED_ID_SIZE)
  192. /* Current version of vb21_keyblock struct */
  193. #define VB21_KEYBLOCK_VERSION_MAJOR 3
  194. #define VB21_KEYBLOCK_VERSION_MINOR 0
  195. /*
  196. * Key block. This contains a signed, versioned key for use in the next stage
  197. * of verified boot.
  198. *
  199. * The key block data must be arranged like this:
  200. * 1) vb21_keyblock header struct h
  201. * 2) Keyblock description (pointed to by h.c.fixed_size)
  202. * 3) Data key (pointed to by h.data_key_offset)
  203. * 4) Signatures (first signature pointed to by h.sig_offset)
  204. *
  205. * The signatures from 4) must cover all the data from 1), 2), 3). That is,
  206. * signatures must sign all data up to sig_offset.
  207. */
  208. struct vb21_keyblock {
  209. /* Common header fields */
  210. struct vb21_struct_common c;
  211. /* Flags (VB2_KEY_BLOCK_FLAG_*) */
  212. uint32_t flags;
  213. /*
  214. * Offset of key (struct vb21_packed_key) to use in next stage of
  215. * verification, from start of the keyblock.
  216. */
  217. uint32_t key_offset;
  218. /* Number of keyblock signatures which follow */
  219. uint32_t sig_count;
  220. /*
  221. * Offset of the first signature (struct vb21_signature) from the start
  222. * of the keyblock.
  223. *
  224. * Signatures sign the contents of this struct and the data pointed to
  225. * by data_key_offset, but not themselves or other signatures.
  226. *
  227. * For the firmware, there may be only one signature.
  228. *
  229. * Kernels often have at least two signatures - one using the kernel
  230. * subkey from the RW firmware (for signed kernels) and one which is
  231. * simply a SHA-512 hash (for unsigned developer kernels).
  232. *
  233. * The ID for each signature indicates which key was used to generate
  234. * the signature.
  235. */
  236. uint32_t sig_offset;
  237. } __attribute__((packed));
  238. #define EXPECTED_VB21_KEYBLOCK_SIZE (EXPECTED_VB21_STRUCT_COMMON_SIZE + 16)
  239. /* Current version of vb21_fw_preamble struct */
  240. #define VB21_FW_PREAMBLE_VERSION_MAJOR 3
  241. #define VB21_FW_PREAMBLE_VERSION_MINOR 0
  242. /* Flags for vb21_fw_preamble.flags */
  243. /* Reserved; do not use */
  244. #define VB21_FIRMWARE_PREAMBLE_RESERVED0 0x00000001
  245. /* Do not allow use of any hardware crypto accelerators. */
  246. #define VB21_FIRMWARE_PREAMBLE_DISALLOW_HWCRYPTO 0x00000002
  247. /*
  248. * Firmware preamble
  249. *
  250. * The preamble data must be arranged like this:
  251. * 1) vb21_fw_preamble header struct h
  252. * 2) Preamble description (pointed to by h.c.fixed_size)
  253. * 3) Hashes (pointed to by h.hash_offset)
  254. * 4) Signature (pointed to by h.sig_offset)
  255. *
  256. * The signature 4) must cover all the data from 1), 2), 3).
  257. */
  258. struct vb21_fw_preamble {
  259. /* Common header fields */
  260. struct vb21_struct_common c;
  261. /* Flags; see VB21_FIRMWARE_PREAMBLE_* */
  262. uint32_t flags;
  263. /* Firmware version */
  264. uint32_t fw_version;
  265. /* Offset of signature (struct vb21_signature) for this preamble */
  266. uint32_t sig_offset;
  267. /*
  268. * The preamble contains a list of hashes (struct vb21_signature) for
  269. * the various firmware components. These have sig_alg=VB2_SIG_NONE,
  270. * and the ID for each hash identifies the component being hashed.
  271. * The calling firmware is responsible for knowing where to find those
  272. * components, which may be on a different storage device than this
  273. * preamble.
  274. */
  275. /* Number of hash entries */
  276. uint32_t hash_count;
  277. /* Offset of first hash entry from start of preamble */
  278. uint32_t hash_offset;
  279. } __attribute__((packed));
  280. #define EXPECTED_VB21_FW_PREAMBLE_SIZE (EXPECTED_VB21_STRUCT_COMMON_SIZE + 20)
  281. #endif /* VBOOT_REFERENCE_VB21_STRUCT_H_ */