bdb.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Copyright 2015 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. * Boot descriptor block firmware functions
  6. */
  7. #ifndef VBOOT_REFERENCE_BDB_H_
  8. #define VBOOT_REFERENCE_BDB_H_
  9. #include <stdlib.h>
  10. #include <stddef.h>
  11. #include "bdb_struct.h"
  12. /*****************************************************************************/
  13. /*
  14. Expected calling sequence:
  15. Load and check just the header
  16. bdb_check_header(buf, size);
  17. Load and verify the entire BDB
  18. bdb_verify(buf, size, bdb_key_hash, dev_mode_flag);
  19. Check RW datakey version. If normal boot from primary BDB, roll forward
  20. Check data version. If normal boot from primary BDB, roll forward
  21. */
  22. /*****************************************************************************/
  23. /* Codes for functions returning numeric error codes */
  24. enum bdb_return_code {
  25. /* Success */
  26. BDB_SUCCESS = 0,
  27. /* BDB key did not match hash, but other than that the BDB was
  28. * fully verified. */
  29. BDB_GOOD_OTHER_THAN_KEY = 1,
  30. /* Function is not implemented, thus supposed to be not called */
  31. BDB_ERROR_NOT_IMPLEMENTED,
  32. /* Other errors */
  33. BDB_ERROR_UNKNOWN = 100,
  34. /* Buffer size too small or wraps around */
  35. BDB_ERROR_BUF_SIZE,
  36. /* Bad fields in structures */
  37. BDB_ERROR_STRUCT_MAGIC,
  38. BDB_ERROR_STRUCT_VERSION,
  39. BDB_ERROR_STRUCT_SIZE,
  40. BDB_ERROR_SIGNED_SIZE,
  41. BDB_ERROR_BDB_SIZE,
  42. BDB_ERROR_OEM_AREA_SIZE,
  43. BDB_ERROR_HASH_ENTRY_SIZE,
  44. BDB_ERROR_HASH_ALG,
  45. BDB_ERROR_SIG_ALG,
  46. BDB_ERROR_DESCRIPTION,
  47. /* Bad components of BDB in bdb_verify() */
  48. BDB_ERROR_HEADER,
  49. BDB_ERROR_BDBKEY,
  50. BDB_ERROR_OEM_AREA_0,
  51. BDB_ERROR_DATAKEY,
  52. BDB_ERROR_BDB_SIGNED_SIZE,
  53. BDB_ERROR_HEADER_SIG,
  54. BDB_ERROR_DATA,
  55. BDB_ERROR_DATA_SIG,
  56. BDB_ERROR_DATA_CHECK_SIG,
  57. BDB_ERROR_DATA_SIGNED_SIZE,
  58. /* Other errors in bdb_verify() */
  59. BDB_ERROR_DIGEST, /* Error calculating digest */
  60. BDB_ERROR_VERIFY_SIG, /* Error verifying signature */
  61. /* Errors in vba_bdb_init */
  62. BDB_ERROR_TRY_OTHER_SLOT,
  63. BDB_ERROR_RECOVERY_REQUEST,
  64. BDB_ERROR_NVM_INIT,
  65. BDB_ERROR_NVM_WRITE,
  66. BDB_ERROR_NVM_RW_HMAC,
  67. BDB_ERROR_NVM_RW_INVALID_HMAC,
  68. BDB_ERROR_NVM_INVALID_PARAMETER,
  69. BDB_ERROR_NVM_INVALID_SECRET,
  70. BDB_ERROR_NVM_RW_MAGIC,
  71. BDB_ERROR_NVM_STRUCT_SIZE,
  72. BDB_ERROR_NVM_WRITE_VERIFY,
  73. BDB_ERROR_NVM_STRUCT_VERSION,
  74. BDB_ERROR_NVM_VBE_READ,
  75. BDB_ERROR_NVM_RW_BUFFER_SMALL,
  76. BDB_ERROR_DECRYPT_BUC,
  77. BDB_ERROR_ENCRYPT_BUC,
  78. BDB_ERROR_WRITE_BUC,
  79. BDB_ERROR_SECRET_TYPE,
  80. BDB_ERROR_SECRET_BUC,
  81. BDB_ERROR_SECRET_BOOT_VERIFIED,
  82. BDB_ERROR_SECRET_BOOT_PATH,
  83. BDB_ERROR_SECRET_BDB,
  84. };
  85. /*****************************************************************************/
  86. /* Functions */
  87. /**
  88. * Sanity-check BDB structures.
  89. *
  90. * This checks for known version numbers, magic numbers, algorithms, etc. and
  91. * ensures the sizes are consistent with those parameters.
  92. *
  93. * @param p Pointer to structure to check
  94. * @param size Size of structure buffer
  95. * @return 0 if success, non-zero error code if error.
  96. */
  97. int bdb_check_header(const struct bdb_header *p, size_t size);
  98. int bdb_check_key(const struct bdb_key *p, size_t size);
  99. int bdb_check_sig(const struct bdb_sig *p, size_t size);
  100. int bdb_check_data(const struct bdb_data *p, size_t size);
  101. /**
  102. * Verify the entire BDB
  103. *
  104. * @param buf Data to hash
  105. * @param size Size of data in bytes
  106. * @param bdb_key_digest Pointer to expected digest for BDB key.
  107. * Must be BDB_SHA256_DIGEST_SIZE bytes long.
  108. * If it's NULL, digest match will be skipped
  109. * (and it'll be treated as 'mismatch').
  110. *
  111. * @return 0 if success, non-zero error code if error. Note that error code
  112. * BDB_GOOD_OTHER_THAN_KEY may still indicate an acceptable BDB if the Boot
  113. * Verified fuse has not been set, or in developer mode.
  114. */
  115. int bdb_verify(const void *buf, size_t size, const uint8_t *bdb_key_digest);
  116. /**
  117. * Functions to extract things from a verified BDB buffer.
  118. *
  119. * Do not call these externally until after bdb_verify()! These methods
  120. * assume data structures have already been verified.
  121. *
  122. * @param buf Pointer to BDB buffer
  123. * @param type Data type, for bdb_get_hash()
  124. * @return A pointer to the requested data, or NULL if error / not present.
  125. */
  126. const struct bdb_header *bdb_get_header(const void *buf);
  127. const struct bdb_key *bdb_get_bdbkey(const void *buf);
  128. const void *bdb_get_oem_area_0(const void *buf);
  129. const struct bdb_key *bdb_get_datakey(const void *buf);
  130. const struct bdb_sig *bdb_get_header_sig(const void *buf);
  131. const struct bdb_data *bdb_get_data(const void *buf);
  132. const void *bdb_get_oem_area_1(const void *buf);
  133. const struct bdb_hash *bdb_get_hash_by_type(const void *buf,
  134. enum bdb_data_type type);
  135. const struct bdb_hash *bdb_get_hash_by_index(const void *buf, int index);
  136. const struct bdb_sig *bdb_get_data_sig(const void *buf);
  137. /**
  138. * Functions to calculate size of BDB components
  139. *
  140. * @param buf Pointer to BDB buffer
  141. * @return Size of the component
  142. */
  143. uint32_t bdb_size_of(const void *buf);
  144. /**
  145. * Functions to calculate offset of BDB components
  146. *
  147. * @param buf Pointer to BDB buffer
  148. * @return Offset of the component
  149. */
  150. ptrdiff_t bdb_offset_of_datakey(const void *buf);
  151. ptrdiff_t bdb_offset_of_header_sig(const void *buf);
  152. ptrdiff_t bdb_offset_of_data(const void *buf);
  153. /*****************************************************************************/
  154. /* Functions probably provided by the caller */
  155. /**
  156. * Calculate a SHA-256 digest of a buffer.
  157. *
  158. * @param digest Pointer to the digest buffer. Must be
  159. * BDB_SHA256_DIGEST_SIZE bytes long.
  160. * @param buf Data to hash
  161. * @param size Size of data in bytes
  162. * @return 0 if success, non-zero error code if error.
  163. */
  164. int bdb_sha256(void *digest, const void *buf, size_t size);
  165. /**
  166. * Verify a RSA-4096 signed digest
  167. *
  168. * @param key_data Key data to use (BDB_RSA4096_KEY_DATA_SIZE bytes)
  169. * @param sig_data Signature to verify (BDB_RSA4096_SIG_SIZE bytes)
  170. * @param digest Digest of signed data (BDB_SHA256_DIGEST bytes)
  171. * @return 0 if success, non-zero error code if error.
  172. */
  173. int bdb_rsa4096_verify(const uint8_t *key_data,
  174. const uint8_t *sig,
  175. const uint8_t *digest);
  176. /**
  177. * Verify a RSA-3072B signed digest
  178. *
  179. * @param key_data Key data to use (BDB_RSA3072B_KEY_DATA_SIZE bytes)
  180. * @param sig_data Signature to verify (BDB_RSA3072B_SIG_SIZE bytes)
  181. * @param digest Digest of signed data (BDB_SHA256_DIGEST bytes)
  182. * @return 0 if success, non-zero error code if error.
  183. */
  184. int bdb_rsa3072b_verify(const uint8_t *key_data,
  185. const uint8_t *sig,
  186. const uint8_t *digest);
  187. /**
  188. * Verify a ECDSA-521 signed digest
  189. *
  190. * @param key_data Key data to use (BDB_ECDSA521_KEY_DATA_SIZE bytes)
  191. * @param sig_data Signature to verify (BDB_ECDSA521_SIG_SIZE bytes)
  192. * @param digest Digest of signed data (BDB_SHA256_DIGEST bytes)
  193. * @return 0 if success, non-zero error code if error.
  194. */
  195. int bdb_ecdsa521_verify(const uint8_t *key_data,
  196. const uint8_t *sig,
  197. const uint8_t *digest);
  198. /*****************************************************************************/
  199. #endif /* VBOOT_REFERENCE_BDB_H_ */