vb2_common.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * Common functions between firmware and kernel verified boot.
  6. */
  7. #ifndef VBOOT_REFERENCE_VB2_COMMON_H_
  8. #define VBOOT_REFERENCE_VB2_COMMON_H_
  9. #include "2api.h"
  10. #include "2common.h"
  11. #include "2return_codes.h"
  12. #include "2sha.h"
  13. #include "2struct.h"
  14. #include "vb2_struct.h"
  15. /*
  16. * Helper functions to get data pointed to by a public key or signature.
  17. */
  18. const uint8_t *vb2_packed_key_data(const struct vb2_packed_key *key);
  19. uint8_t *vb2_signature_data(struct vb2_signature *sig);
  20. /**
  21. * Verify the data pointed to by a subfield is inside the parent data.
  22. *
  23. * The subfield has a header pointed to by member, and a separate data
  24. * field at an offset relative to the header. That is:
  25. *
  26. * struct parent {
  27. * (possibly other parent fields)
  28. * struct member {
  29. * (member header fields)
  30. * };
  31. * (possibly other parent fields)
  32. * };
  33. * (possibly some other parent data)
  34. * (member data)
  35. * (possibly some other parent data)
  36. *
  37. * @param parent Parent data
  38. * @param parent_size Parent size in bytes
  39. * @param member Subfield header
  40. * @param member_size Size of subfield header in bytes
  41. * @param member_data_offset Offset of member data from start of member
  42. * @param member_data_size Size of member data in bytes
  43. * @return VB2_SUCCESS, or non-zero if error.
  44. */
  45. int vb2_verify_member_inside(const void *parent, size_t parent_size,
  46. const void *member, size_t member_size,
  47. ptrdiff_t member_data_offset,
  48. size_t member_data_size);
  49. /**
  50. * Verify a signature is fully contained in its parent data
  51. *
  52. * @param parent Parent data
  53. * @param parent_size Parent size in bytes
  54. * @param sig Signature pointer
  55. * @return VB2_SUCCESS, or non-zero if error.
  56. */
  57. int vb2_verify_signature_inside(const void *parent,
  58. uint32_t parent_size,
  59. const struct vb2_signature *sig);
  60. /**
  61. * Verify a packed key is fully contained in its parent data
  62. *
  63. * @param parent Parent data
  64. * @param parent_size Parent size in bytes
  65. * @param key Packed key pointer
  66. * @return VB2_SUCCESS, or non-zero if error.
  67. */
  68. int vb2_verify_packed_key_inside(const void *parent,
  69. uint32_t parent_size,
  70. const struct vb2_packed_key *key);
  71. /**
  72. * Unpack a vboot1-format key buffer for use in verification
  73. *
  74. * The elements of the unpacked key will point into the source buffer, so don't
  75. * free the source buffer until you're done with the key.
  76. *
  77. * @param key Destintion for unpacked key
  78. * @param buf Source buffer containing packed key
  79. * @param size Size of buffer in bytes
  80. * @return VB2_SUCCESS, or non-zero error code if error.
  81. */
  82. int vb2_unpack_key_buffer(struct vb2_public_key *key,
  83. const uint8_t *buf,
  84. uint32_t size);
  85. /**
  86. * Unpack a vboot1-format key for use in verification
  87. *
  88. * The elements of the unpacked key will point into the source packed key, so
  89. * don't free the source until you're done with the public key.
  90. *
  91. * @param key Destintion for unpacked key
  92. * @param packed_key Source packed key
  93. * @param size Size of buffer in bytes
  94. * @return VB2_SUCCESS, or non-zero error code if error.
  95. */
  96. int vb2_unpack_key(struct vb2_public_key *key,
  97. const struct vb2_packed_key *packed_key);
  98. /**
  99. * Verify a signature against an expected hash digest.
  100. *
  101. * @param key Key to use in signature verification
  102. * @param sig Signature to verify (may be destroyed in process)
  103. * @param digest Digest of signed data
  104. * @param wb Work buffer
  105. * @return VB2_SUCCESS, or non-zero if error.
  106. */
  107. int vb2_verify_digest(const struct vb2_public_key *key,
  108. struct vb2_signature *sig,
  109. const uint8_t *digest,
  110. const struct vb2_workbuf *wb);
  111. /**
  112. * Verify data matches signature.
  113. *
  114. * @param data Data to verify
  115. * @param size Size of data buffer. Note that amount of data to
  116. * actually validate is contained in sig->data_size.
  117. * @param sig Signature of data (destroyed in process)
  118. * @param key Key to use to validate signature
  119. * @param wb Work buffer
  120. * @return VB2_SUCCESS, or non-zero error code if error.
  121. */
  122. int vb2_verify_data(const uint8_t *data,
  123. uint32_t size,
  124. struct vb2_signature *sig,
  125. const struct vb2_public_key *key,
  126. const struct vb2_workbuf *wb);
  127. /**
  128. * Check the sanity of a key block structure.
  129. *
  130. * Verifies all the header fields. Does not verify key index or key block
  131. * flags. Should be called before verifying the key block data itself using
  132. * the key. (This function does not itself verify the signature - just that
  133. * the right amount of data is claimed to be signed.)
  134. *
  135. * @param block Key block to verify
  136. * @param size Size of key block buffer
  137. * @param sig Which signature inside the keyblock to use
  138. */
  139. int vb2_check_keyblock(const struct vb2_keyblock *block,
  140. uint32_t size,
  141. const struct vb2_signature *sig);
  142. /**
  143. * Verify a key block using a public key.
  144. *
  145. * Header fields are also checked for sanity. Does not verify key index or key
  146. * block flags. Signature inside block is destroyed during check.
  147. *
  148. * @param block Key block to verify
  149. * @param size Size of key block buffer
  150. * @param key Key to use to verify block
  151. * @param wb Work buffer
  152. * @return VB2_SUCCESS, or non-zero error code if error.
  153. */
  154. int vb2_verify_keyblock(struct vb2_keyblock *block,
  155. uint32_t size,
  156. const struct vb2_public_key *key,
  157. const struct vb2_workbuf *wb);
  158. /**
  159. * Verify a key block using its hash.
  160. *
  161. * Header fields are also checked for sanity. Does not verify key index or key
  162. * block flags. Use this for self-signed keyblocks in developer mode.
  163. *
  164. * @param block Key block to verify
  165. * @param size Size of key block buffer
  166. * @param key Key to use to verify block
  167. * @param wb Work buffer
  168. * @return VB2_SUCCESS, or non-zero error code if error.
  169. */
  170. int vb2_verify_keyblock_hash(const struct vb2_keyblock *block,
  171. uint32_t size,
  172. const struct vb2_workbuf *wb);
  173. /**
  174. * Check the sanity of a firmware preamble using a public key.
  175. *
  176. * The signature in the preamble is destroyed during the check.
  177. *
  178. * @param preamble Preamble to verify
  179. * @param size Size of preamble buffer
  180. * @param key Key to use to verify preamble
  181. * @param wb Work buffer
  182. * @return VB2_SUCCESS, or non-zero error code if error.
  183. */
  184. int vb2_verify_fw_preamble(struct vb2_fw_preamble *preamble,
  185. uint32_t size,
  186. const struct vb2_public_key *key,
  187. const struct vb2_workbuf *wb);
  188. /**
  189. * Check the sanity of a kernel preamble using a public key.
  190. *
  191. * The signature in the preamble is destroyed during the check.
  192. *
  193. * @param preamble Preamble to verify
  194. * @param size Size of preamble buffer
  195. * @param key Key to use to verify preamble
  196. * @param wb Work buffer
  197. * @return VB2_SUCCESS, or non-zero error code if error.
  198. */
  199. int vb2_verify_kernel_preamble(struct vb2_kernel_preamble *preamble,
  200. uint32_t size,
  201. const struct vb2_public_key *key,
  202. const struct vb2_workbuf *wb);
  203. /**
  204. * Retrieve the 16-bit vmlinuz header address and size from the preamble.
  205. *
  206. * Size 0 means there is no 16-bit vmlinuz header present. Old preamble
  207. * versions (<2.1) return 0 for both fields.
  208. *
  209. * @param preamble Preamble to check
  210. * @param vmlinuz_header_address Destination for header address
  211. * @param vmlinuz_header_size Destination for header size
  212. */
  213. void vb2_kernel_get_vmlinuz_header(const struct vb2_kernel_preamble *preamble,
  214. uint64_t *vmlinuz_header_address,
  215. uint32_t *vmlinuz_header_size);
  216. /**
  217. * Get the flags for the kernel preamble.
  218. *
  219. * @param preamble Preamble to check
  220. * @return Flags for the preamble. Old preamble versions (<2.2) return 0.
  221. */
  222. uint32_t vb2_kernel_get_flags(const struct vb2_kernel_preamble *preamble);
  223. #endif /* VBOOT_REFERENCE_VB2_COMMON_H_ */