2common.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_VBOOT_2COMMON_H_
  8. #define VBOOT_REFERENCE_VBOOT_2COMMON_H_
  9. #include "2api.h"
  10. #include "2return_codes.h"
  11. #include "2sha.h"
  12. #include "2struct.h"
  13. struct vb2_public_key;
  14. /*
  15. * Return the greater of A and B. This is used in macros which calculate the
  16. * required buffer size, so can't be turned into a static inline function.
  17. */
  18. #ifndef VB2_MAX
  19. #define VB2_MAX(A, B) ((A) > (B) ? (A) : (B))
  20. #endif
  21. /* Return the number of elements in an array */
  22. #ifndef ARRAY_SIZE
  23. #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
  24. #endif
  25. /* Debug output printf() for tests. Otherwise, it's platform-dependent. */
  26. #if defined(VBOOT_DEBUG)
  27. # if defined(FOR_TEST)
  28. # include <stdio.h>
  29. # define VB2_DEBUG(format, args...) do { \
  30. printf("%s", __func__); \
  31. printf(format, ## args); \
  32. } while(0)
  33. # define VB2_DEBUG_RAW(format, args...) printf(format, ## args)
  34. # else
  35. # define VB2_DEBUG(format, args...) vb2ex_printf(__func__, format, ## args)
  36. # define VB2_DEBUG_RAW(format, args...) vb2ex_printf(NULL, format, ## args)
  37. # endif
  38. #else
  39. # define VB2_DEBUG(format, args...)
  40. # define VB2_DEBUG_RAW(format, args...)
  41. #endif
  42. /*
  43. * Alignment for work buffer pointers/allocations should be useful for any
  44. * data type. When declaring workbuf buffers on the stack, the caller should
  45. * use explicit alignment to avoid run-time errors. For example:
  46. *
  47. * int foo(void)
  48. * {
  49. * struct vb2_workbuf wb;
  50. * uint8_t buf[NUM] __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  51. * wb.buf = buf;
  52. * wb.size = sizeof(buf);
  53. */
  54. /* We might get away with using __alignof__(void *), but since GCC defines a
  55. * macro for us we'll be safe and use that. */
  56. #define VB2_WORKBUF_ALIGN __BIGGEST_ALIGNMENT__
  57. /* Work buffer */
  58. struct vb2_workbuf {
  59. uint8_t *buf;
  60. uint32_t size;
  61. };
  62. /**
  63. * Initialize a work buffer.
  64. *
  65. * @param wb Work buffer to init
  66. * @param buf Pointer to work buffer data
  67. * @param size Size of work buffer data in bytes
  68. */
  69. void vb2_workbuf_init(struct vb2_workbuf *wb, uint8_t *buf, uint32_t size);
  70. /**
  71. * Allocate space in a work buffer.
  72. *
  73. * Note that the returned buffer will always be aligned to VB2_WORKBUF_ALIGN.
  74. *
  75. * The work buffer acts like a stack, and detailed tracking of allocs and frees
  76. * is not done. The caller must track the size of each allocation and free via
  77. * vb2_workbuf_free() in the reverse order they were allocated.
  78. *
  79. * An acceptable alternate workflow inside a function is to pass in a const
  80. * work buffer, then make a local copy. Allocations done to the local copy
  81. * then don't change the passed-in work buffer, and will effectively be freed
  82. * when the local copy goes out of scope.
  83. *
  84. * @param wb Work buffer
  85. * @param size Requested size in bytes
  86. * @return A pointer to the allocated space, or NULL if error.
  87. */
  88. void *vb2_workbuf_alloc(struct vb2_workbuf *wb, uint32_t size);
  89. /**
  90. * Reallocate space in a work buffer.
  91. *
  92. * Note that the returned buffer will always be aligned to VB2_WORKBUF_ALIGN.
  93. * The work buffer acts like a stack, so this must only be done to the most
  94. * recently allocated buffer.
  95. *
  96. * @param wb Work buffer
  97. * @param oldsize Old allocation size in bytes
  98. * @param newsize Requested size in bytes
  99. * @return A pointer to the allocated space, or NULL if error.
  100. */
  101. void *vb2_workbuf_realloc(struct vb2_workbuf *wb,
  102. uint32_t oldsize,
  103. uint32_t newsize);
  104. /**
  105. * Free the preceding allocation.
  106. *
  107. * Note that the work buffer acts like a stack, and detailed tracking of
  108. * allocs and frees is not done. The caller must track the size of each
  109. * allocation and free them in reverse order.
  110. *
  111. * @param wb Work buffer
  112. * @param size Size of data to free
  113. */
  114. void vb2_workbuf_free(struct vb2_workbuf *wb, uint32_t size);
  115. /* Check if a pointer is aligned on an align-byte boundary */
  116. #define vb2_aligned(ptr, align) (!(((uintptr_t)(ptr)) & ((align) - 1)))
  117. /**
  118. * Safer memcmp() for use in crypto.
  119. *
  120. * Compares the buffers to see if they are equal. Time taken to perform
  121. * the comparison is dependent only on the size, not the relationship of
  122. * the match between the buffers. Note that unlike memcmp(), this only
  123. * indicates inequality, not which buffer is lesser.
  124. *
  125. * @param s1 First buffer
  126. * @param s2 Second buffer
  127. * @param size Number of bytes to compare
  128. * @return 0 if match or size=0, non-zero if at least one byte mismatched.
  129. */
  130. int vb2_safe_memcmp(const void *s1, const void *s2, size_t size);
  131. /**
  132. * Align a buffer and check its size.
  133. *
  134. * @param **ptr Pointer to pointer to align
  135. * @param *size Points to size of buffer pointed to by *ptr
  136. * @param align Required alignment (must be power of 2)
  137. * @param want_size Required size
  138. * @return VB2_SUCCESS, or non-zero if error.
  139. */
  140. int vb2_align(uint8_t **ptr,
  141. uint32_t *size,
  142. uint32_t align,
  143. uint32_t want_size);
  144. /**
  145. * Return offset of ptr from base.
  146. *
  147. * @param base Base pointer
  148. * @param ptr Pointer at some offset from base
  149. * @return The offset of ptr from base.
  150. */
  151. ptrdiff_t vb2_offset_of(const void *base, const void *ptr);
  152. /**
  153. * Return expected signature size for a signature/hash algorithm pair
  154. *
  155. * @param sig_alg Signature algorithm
  156. * @param hash_alg Hash algorithm
  157. * @return The signature size, or zero if error / unsupported algorithm.
  158. */
  159. uint32_t vb2_sig_size(enum vb2_signature_algorithm sig_alg,
  160. enum vb2_hash_algorithm hash_alg);
  161. /**
  162. * Return a key ID for an unsigned hash algorithm.
  163. *
  164. * @param hash_alg Hash algorithm to return key for
  165. * @return A pointer to the key ID for that hash algorithm with
  166. * sig_alg=VB2_SIG_NONE, or NULL if error.
  167. */
  168. const struct vb2_id *vb2_hash_id(enum vb2_hash_algorithm hash_alg);
  169. /* Size of work buffer sufficient for vb2_verify_digest() worst case. */
  170. #define VB2_VERIFY_DIGEST_WORKBUF_BYTES VB2_VERIFY_RSA_DIGEST_WORKBUF_BYTES
  171. /* Size of work buffer sufficient for vb2_verify_data() worst case. */
  172. #define VB2_VERIFY_DATA_WORKBUF_BYTES \
  173. (VB2_SHA512_DIGEST_SIZE + \
  174. VB2_MAX(VB2_VERIFY_DIGEST_WORKBUF_BYTES, \
  175. sizeof(struct vb2_digest_context)))
  176. /* Size of work buffer sufficient for vb2_verify_keyblock() worst case. */
  177. #define VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES VB2_VERIFY_DATA_WORKBUF_BYTES
  178. /* Size of work buffer sufficient for vb2_verify_fw_preamble() worst case. */
  179. #define VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES VB2_VERIFY_DATA_WORKBUF_BYTES
  180. /*
  181. * Size of work buffer sufficient for vb2_verify_kernel_preamble() worst
  182. * case.
  183. */
  184. #define VB2_VERIFY_KERNEL_PREAMBLE_WORKBUF_BYTES VB2_VERIFY_DATA_WORKBUF_BYTES
  185. #endif /* VBOOT_REFERENCE_VBOOT_2COMMON_H_ */