vb21_common.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_VB21_COMMON_H_
  8. #define VBOOT_REFERENCE_VB21_COMMON_H_
  9. #include "2common.h"
  10. #include "2return_codes.h"
  11. #include "2struct.h"
  12. #include "vb21_struct.h"
  13. /**
  14. * Return the description of an object starting with a vb21_struct_common
  15. * header.
  16. *
  17. * Does not sanity-check the buffer; merely returns the pointer.
  18. *
  19. * @param buf Pointer to common object
  20. * @return A pointer to description or an empty string if none.
  21. */
  22. const char *vb21_common_desc(const void *buf);
  23. /**
  24. * Verify the common struct header is fully contained in its parent data
  25. *
  26. * Also verifies the description is either zero-length or null-terminated.
  27. *
  28. * @param parent Parent data
  29. * @param parent_size Parent size in bytes
  30. * @return VB2_SUCCESS, or non-zero if error.
  31. */
  32. int vb21_verify_common_header(const void *parent, uint32_t parent_size);
  33. /**
  34. * Verify a member is within the data for a parent object
  35. *
  36. * @param parent Parent data (starts with struct vb21_struct_common)
  37. * @param min_offset Pointer to minimum offset where member can be located.
  38. * If this offset is 0 on input, uses the size of the
  39. * fixed header (and description, if any). This will be
  40. * updated on return to the end of the passed member. On
  41. * error, the value of min_offset is undefined.
  42. * @param member_offset Offset of member data from start of parent, in bytes
  43. * @param member_size Size of member data, in bytes
  44. * @return VB2_SUCCESS, or non-zero if error.
  45. */
  46. int vb21_verify_common_member(const void *parent,
  47. uint32_t *min_offset,
  48. uint32_t member_offset,
  49. uint32_t member_size);
  50. /**
  51. * Verify a member which starts with a common header is within the parent
  52. *
  53. * This does not verify the contents of the member or its header, only that the
  54. * member's claimed total size fits within the parent's claimed total size at
  55. * the specified offset.
  56. *
  57. * @param parent Parent data (starts with struct vb21_struct_common)
  58. * @param min_offset Pointer to minimum offset where member can be located.
  59. * If this offset is 0 on input, uses the size of the
  60. * fixed header (and description, if any). This will be
  61. * updated on return to the end of the passed member. On
  62. * error, the value of min_offset is undefined.
  63. * @param member_offset Offset of member data from start of parent, in bytes.
  64. * This should be the start of the common header of the
  65. * member.
  66. * @return VB2_SUCCESS, or non-zero if error.
  67. */
  68. int vb21_verify_common_subobject(const void *parent,
  69. uint32_t *min_offset,
  70. uint32_t member_offset);
  71. /**
  72. * Unpack a key 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 vb21_unpack_key(struct vb2_public_key *key,
  83. const uint8_t *buf,
  84. uint32_t size);
  85. /**
  86. * Unpack the RSA data fields for a public key
  87. *
  88. * This is called by vb21_unpack_key() to extract the arrays from a packed key.
  89. * These elements of *key will point inside the key_data buffer.
  90. *
  91. * @param key Destination key for RSA data fields
  92. * @param key_data Packed key data (from inside a packed key buffer)
  93. * @param key_size Size of packed key data in bytes
  94. */
  95. int vb2_unpack_key_data(struct vb2_public_key *key,
  96. const uint8_t *key_data,
  97. uint32_t key_size);
  98. /**
  99. * Verify the integrity of a signature struct
  100. * @param sig Signature struct
  101. * @param size Size of buffer containing signature struct
  102. * @return VB2_SUCCESS, or non-zero if error.
  103. */
  104. int vb21_verify_signature(const struct vb21_signature *sig,
  105. uint32_t size);
  106. /**
  107. * Verify a signature against an expected hash digest.
  108. *
  109. * @param key Key to use in signature verification
  110. * @param sig Signature to verify (may be destroyed in process)
  111. * @param digest Digest of signed data
  112. * @param wb Work buffer
  113. * @return VB2_SUCCESS, or non-zero if error.
  114. */
  115. int vb21_verify_digest(const struct vb2_public_key *key,
  116. struct vb21_signature *sig,
  117. const uint8_t *digest,
  118. const struct vb2_workbuf *wb);
  119. /**
  120. * Verify data matches signature.
  121. *
  122. * @param data Data to verify
  123. * @param size Size of data buffer. Note that amount of data to
  124. * actually validate is contained in sig->data_size.
  125. * @param sig Signature of data (destroyed in process)
  126. * @param key Key to use to validate signature
  127. * @param wb Work buffer
  128. * @return VB2_SUCCESS, or non-zero error code if error.
  129. */
  130. int vb21_verify_data(const void *data,
  131. uint32_t size,
  132. struct vb21_signature *sig,
  133. const struct vb2_public_key *key,
  134. const struct vb2_workbuf *wb);
  135. /**
  136. * Check the sanity of a key block using a public key.
  137. *
  138. * Header fields are also checked for sanity. Does not verify key index or key
  139. * block flags. Signature inside block is destroyed during check.
  140. *
  141. * @param block Key block to verify
  142. * @param size Size of key block buffer
  143. * @param key Key to use to verify block
  144. * @param wb Work buffer
  145. * @return VB2_SUCCESS, or non-zero error code if error.
  146. */
  147. int vb21_verify_keyblock(struct vb21_keyblock *block,
  148. uint32_t size,
  149. const struct vb2_public_key *key,
  150. const struct vb2_workbuf *wb);
  151. /**
  152. * Check the sanity of a firmware preamble using a public key.
  153. *
  154. * The signature in the preamble is destroyed during the check.
  155. *
  156. * @param preamble Preamble to verify
  157. * @param size Size of preamble buffer
  158. * @param key Key to use to verify preamble
  159. * @param wb Work buffer
  160. * @return VB2_SUCCESS, or non-zero error code if error.
  161. */
  162. int vb21_verify_fw_preamble(struct vb21_fw_preamble *preamble,
  163. uint32_t size,
  164. const struct vb2_public_key *key,
  165. const struct vb2_workbuf *wb);
  166. #endif /* VBOOT_REFERENCE_VB21_COMMON_H_ */