bdb.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (c) 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 "bdb_struct.h"
  11. /*****************************************************************************/
  12. /*
  13. Expected calling sequence:
  14. Load and check just the header
  15. bdb_check_header(buf, size);
  16. Load and verify the entire BDB
  17. bdb_verify(buf, size, bdb_key_hash, dev_mode_flag);
  18. Check RW subkey version. If normal boot from primary BDB, roll forward
  19. Check data version. If normal boot from primary BDB, roll forward
  20. */
  21. /*****************************************************************************/
  22. /* Codes for functions returning numeric error codes */
  23. enum bdb_return_code {
  24. /* Success */
  25. BDB_SUCCESS = 0,
  26. /* BDB key did not match hash, but other than that the BDB was
  27. * fully verified. */
  28. BDB_GOOD_OTHER_THAN_KEY = 1,
  29. /* Other errors */
  30. BDB_ERROR_UNKNOWN = 100,
  31. /* Buffer size too small or wraps around */
  32. BDB_ERROR_BUF_SIZE,
  33. /* Bad fields in structures */
  34. BDB_ERROR_STRUCT_MAGIC,
  35. BDB_ERROR_STRUCT_VERSION,
  36. BDB_ERROR_STRUCT_SIZE,
  37. BDB_ERROR_SIGNED_SIZE,
  38. BDB_ERROR_BDB_SIZE,
  39. BDB_ERROR_OEM_AREA_SIZE,
  40. BDB_ERROR_HASH_ENTRY_SIZE,
  41. BDB_ERROR_HASH_ALG,
  42. BDB_ERROR_SIG_ALG,
  43. BDB_ERROR_DESCRIPTION,
  44. /* Bad components of BDB in bdb_verify() */
  45. BDB_ERROR_HEADER,
  46. BDB_ERROR_BDBKEY,
  47. BDB_ERROR_OEM_AREA_0,
  48. BDB_ERROR_SUBKEY,
  49. BDB_ERROR_BDB_SIGNED_SIZE,
  50. BDB_ERROR_HEADER_SIG,
  51. BDB_ERROR_DATA,
  52. BDB_ERROR_DATA_SIG,
  53. /* Other errors in bdb_verify() */
  54. BDB_ERROR_DIGEST, /* Error calculating digest */
  55. BDB_ERROR_VERIFY_SIG, /* Error verifying signature */
  56. };
  57. /*****************************************************************************/
  58. /* Functions */
  59. /**
  60. * Sanity-check BDB structures.
  61. *
  62. * This checks for known version numbers, magic numbers, algorithms, etc. and
  63. * ensures the sizes are consistent with those parameters.
  64. *
  65. * @param p Pointer to structure to check
  66. * @param size Size of structure buffer
  67. * @return 0 if success, non-zero error code if error.
  68. */
  69. int bdb_check_header(const struct bdb_header *p, size_t size);
  70. int bdb_check_key(const struct bdb_key *p, size_t size);
  71. int bdb_check_sig(const struct bdb_sig *p, size_t size);
  72. int bdb_check_data(const struct bdb_data *p, size_t size);
  73. /**
  74. * Verify the entire BDB
  75. *
  76. * @param buf Data to hash
  77. * @param size Size of data in bytes
  78. * @param bdb_key_digest Pointer to expected digest for BDB key.
  79. * Must be BDB_SHA256_DIGEST_SIZE bytes long.
  80. *
  81. * @return 0 if success, non-zero error code if error. Note that error code
  82. * BDB_GOOD_OTHER_THAN_KEY may still indicate an acceptable BDB if the Boot
  83. * Verified fuse has not been set, or in developer mode.
  84. */
  85. int bdb_verify(const void *buf, size_t size, const uint8_t *bdb_key_digest);
  86. /**
  87. * Functions to extract things from a verified BDB buffer.
  88. *
  89. * Do not call these externally until after bdb_verify()! These methods
  90. * assume data structures have already been verified.
  91. *
  92. * @param buf Pointer to BDB buffer
  93. * @param type Data type, for bdb_get_hash()
  94. * @return A pointer to the requested data, or NULL if error / not present.
  95. */
  96. const struct bdb_header *bdb_get_header(const void *buf);
  97. const struct bdb_key *bdb_get_bdbkey(const void *buf);
  98. const void *bdb_get_oem_area_0(const void *buf);
  99. const struct bdb_key *bdb_get_subkey(const void *buf);
  100. const struct bdb_sig *bdb_get_header_sig(const void *buf);
  101. const struct bdb_data *bdb_get_data(const void *buf);
  102. const void *bdb_get_oem_area_1(const void *buf);
  103. const struct bdb_hash *bdb_get_hash(const void *buf, enum bdb_data_type type);
  104. const struct bdb_sig *bdb_get_data_sig(const void *buf);
  105. /*****************************************************************************/
  106. /* Functions probably provided by the caller */
  107. /**
  108. * Calculate a SHA-256 digest of a buffer.
  109. *
  110. * @param digest Pointer to the digest buffer. Must be
  111. * BDB_SHA256_DIGEST_SIZE bytes long.
  112. * @param buf Data to hash
  113. * @param size Size of data in bytes
  114. * @return 0 if success, non-zero error code if error.
  115. */
  116. __attribute__((weak))
  117. int bdb_sha256(void *digest, const void *buf, size_t size);
  118. /**
  119. * Verify a RSA-4096 signed digest
  120. *
  121. * @param key_data Key data to use (BDB_RSA4096_KEY_DATA_SIZE bytes)
  122. * @param sig_data Signature to verify (BDB_RSA4096_SIG_SIZE bytes)
  123. * @param digest Digest of signed data (BDB_SHA256_DIGEST bytes)
  124. * @return 0 if success, non-zero error code if error.
  125. */
  126. __attribute__((weak))
  127. int bdb_rsa4096_verify(const uint8_t *key_data,
  128. const uint8_t *sig,
  129. const uint8_t *digest);
  130. /**
  131. * Verify a RSA-3072B signed digest
  132. *
  133. * @param key_data Key data to use (BDB_RSA3072B_KEY_DATA_SIZE bytes)
  134. * @param sig_data Signature to verify (BDB_RSA3072B_SIG_SIZE bytes)
  135. * @param digest Digest of signed data (BDB_SHA256_DIGEST bytes)
  136. * @return 0 if success, non-zero error code if error.
  137. */
  138. __attribute__((weak))
  139. int bdb_rsa3072b_verify(const uint8_t *key_data,
  140. const uint8_t *sig,
  141. const uint8_t *digest);
  142. /**
  143. * Verify a ECDSA-521 signed digest
  144. *
  145. * @param key_data Key data to use (BDB_ECDSA521_KEY_DATA_SIZE bytes)
  146. * @param sig_data Signature to verify (BDB_ECDSA521_SIG_SIZE bytes)
  147. * @param digest Digest of signed data (BDB_SHA256_DIGEST bytes)
  148. * @return 0 if success, non-zero error code if error.
  149. */
  150. __attribute__((weak))
  151. int bdb_ecdsa521_verify(const uint8_t *key_data,
  152. const uint8_t *sig,
  153. const uint8_t *digest);
  154. /*****************************************************************************/
  155. #endif /* VBOOT_REFERENCE_BDB_H_ */