2sha.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * These APIs may be called by external firmware as well as vboot. External
  6. * firmware must NOT include this header file directly; instead, define
  7. * NEED_VB2_SHA_LIBRARY and include vb2api.h. This is permissible because the
  8. * SHA library routines below don't interact with the rest of vboot.
  9. */
  10. #ifndef VBOOT_REFERENCE_2SHA_H_
  11. #define VBOOT_REFERENCE_2SHA_H_
  12. #include "2crypto.h"
  13. /* Hash algorithms may be disabled individually to save code space */
  14. #ifndef VB2_SUPPORT_SHA1
  15. #define VB2_SUPPORT_SHA1 1
  16. #endif
  17. #ifndef VB2_SUPPORT_SHA256
  18. #define VB2_SUPPORT_SHA256 1
  19. #endif
  20. #ifndef VB2_SUPPORT_SHA512
  21. #define VB2_SUPPORT_SHA512 1
  22. #endif
  23. /* These are set to the biggest values among the supported hash algorithms.
  24. * They have to be updated as we add new hash algorithms */
  25. #define VB2_MAX_DIGEST_SIZE VB2_SHA512_DIGEST_SIZE
  26. #define VB2_MAX_BLOCK_SIZE VB2_SHA512_BLOCK_SIZE
  27. #define VB2_INVALID_ALG_NAME "INVALID"
  28. #define VB2_SHA1_DIGEST_SIZE 20
  29. #define VB2_SHA1_BLOCK_SIZE 64
  30. #define VB2_SHA1_ALG_NAME "SHA1"
  31. /* Context structs for hash algorithms */
  32. struct vb2_sha1_context {
  33. uint32_t count;
  34. uint32_t state[5];
  35. #if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
  36. union {
  37. uint8_t b[VB2_SHA1_BLOCK_SIZE];
  38. uint32_t w[VB2_SHA1_BLOCK_SIZE / sizeof(uint32_t)];
  39. } buf;
  40. #else
  41. uint8_t buf[VB2_SHA1_BLOCK_SIZE];
  42. #endif
  43. };
  44. #define VB2_SHA256_DIGEST_SIZE 32
  45. #define VB2_SHA256_BLOCK_SIZE 64
  46. #define VB2_SHA256_ALG_NAME "SHA256"
  47. struct vb2_sha256_context {
  48. uint32_t h[8];
  49. uint32_t total_size;
  50. uint32_t size;
  51. uint8_t block[2 * VB2_SHA256_BLOCK_SIZE];
  52. };
  53. #define VB2_SHA512_DIGEST_SIZE 64
  54. #define VB2_SHA512_BLOCK_SIZE 128
  55. #define VB2_SHA512_ALG_NAME "SHA512"
  56. struct vb2_sha512_context {
  57. uint64_t h[8];
  58. uint32_t total_size;
  59. uint32_t size;
  60. uint8_t block[2 * VB2_SHA512_BLOCK_SIZE];
  61. };
  62. /* Hash algorithm independent digest context; includes all of the above. */
  63. struct vb2_digest_context {
  64. /* Context union for all algorithms */
  65. union {
  66. #if VB2_SUPPORT_SHA1
  67. struct vb2_sha1_context sha1;
  68. #endif
  69. #if VB2_SUPPORT_SHA256
  70. struct vb2_sha256_context sha256;
  71. #endif
  72. #if VB2_SUPPORT_SHA512
  73. struct vb2_sha512_context sha512;
  74. #endif
  75. };
  76. /* Current hash algorithm */
  77. enum vb2_hash_algorithm hash_alg;
  78. /* 1 if digest is computed with vb2ex_hwcrypto routines, else 0 */
  79. int using_hwcrypto;
  80. };
  81. /**
  82. * Initialize a hash context.
  83. *
  84. * @param ctx Hash context
  85. */
  86. void vb2_sha1_init(struct vb2_sha1_context *ctx);
  87. void vb2_sha256_init(struct vb2_sha256_context *ctx);
  88. void vb2_sha512_init(struct vb2_sha512_context *ctx);
  89. /**
  90. * Update (extend) a hash.
  91. *
  92. * @param ctx Hash context
  93. * @param data Data to hash
  94. * @param size Length of data in bytes
  95. */
  96. void vb2_sha1_update(struct vb2_sha1_context *ctx,
  97. const uint8_t *data,
  98. uint32_t size);
  99. void vb2_sha256_update(struct vb2_sha256_context *ctx,
  100. const uint8_t *data,
  101. uint32_t size);
  102. void vb2_sha512_update(struct vb2_sha512_context *ctx,
  103. const uint8_t *data,
  104. uint32_t size);
  105. /**
  106. * Finalize a hash digest.
  107. *
  108. * @param ctx Hash context
  109. * @param digest Destination for hash; must be VB_SHA*_DIGEST_SIZE bytes
  110. */
  111. void vb2_sha1_finalize(struct vb2_sha1_context *ctx, uint8_t *digest);
  112. void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest);
  113. void vb2_sha512_finalize(struct vb2_sha512_context *ctx, uint8_t *digest);
  114. /**
  115. * Hash-extend data
  116. *
  117. * @param from Hash to be extended. It has to be the hash size.
  118. * @param by Block to be extended by. It has to be the hash block size.
  119. * @param to Destination for extended data
  120. */
  121. void vb2_sha256_extend(const uint8_t *from, const uint8_t *by, uint8_t *to);
  122. /**
  123. * Convert vb2_crypto_algorithm to vb2_hash_algorithm.
  124. *
  125. * @param algorithm Crypto algorithm (vb2_crypto_algorithm)
  126. *
  127. * @return The hash algorithm for that crypto algorithm, or VB2_HASH_INVALID if
  128. * the crypto algorithm or its corresponding hash algorithm is invalid or not
  129. * supported.
  130. */
  131. enum vb2_hash_algorithm vb2_crypto_to_hash(uint32_t algorithm);
  132. /**
  133. * Return the size of the digest for a hash algorithm.
  134. *
  135. * @param hash_alg Hash algorithm
  136. * @return The size of the digest, or 0 if error.
  137. */
  138. int vb2_digest_size(enum vb2_hash_algorithm hash_alg);
  139. /**
  140. * Return the block size of a hash algorithm.
  141. *
  142. * @param hash_alg Hash algorithm
  143. * @return The block size of the algorithm, or 0 if error.
  144. */
  145. int vb2_hash_block_size(enum vb2_hash_algorithm alg);
  146. /**
  147. * Return the name of a hash algorithm
  148. *
  149. * @param alg Hash algorithm ID
  150. * @return String containing a hash name or VB2_INVALID_ALG_NAME
  151. * if <alg> is invalid.
  152. */
  153. const char *vb2_get_hash_algorithm_name(enum vb2_hash_algorithm alg);
  154. /**
  155. * Initialize a digest context for doing block-style digesting.
  156. *
  157. * @param dc Digest context
  158. * @param hash_alg Hash algorithm
  159. * @return VB2_SUCCESS, or non-zero on error.
  160. */
  161. int vb2_digest_init(struct vb2_digest_context *dc,
  162. enum vb2_hash_algorithm hash_alg);
  163. /**
  164. * Extend a digest's hash with another block of data.
  165. *
  166. * @param dc Digest context
  167. * @param buf Data to hash
  168. * @param size Length of data in bytes
  169. * @return VB2_SUCCESS, or non-zero on error.
  170. */
  171. int vb2_digest_extend(struct vb2_digest_context *dc,
  172. const uint8_t *buf,
  173. uint32_t size);
  174. /**
  175. * Finalize a digest and store the result.
  176. *
  177. * The destination digest should be at least vb2_digest_size(algorithm).
  178. *
  179. * @param dc Digest context
  180. * @param digest Destination for digest
  181. * @param digest_size Length of digest buffer in bytes.
  182. * @return VB2_SUCCESS, or non-zero on error.
  183. */
  184. int vb2_digest_finalize(struct vb2_digest_context *dc,
  185. uint8_t *digest,
  186. uint32_t digest_size);
  187. /**
  188. * Calculate the digest of a buffer and store the result.
  189. *
  190. * @param buf Data to hash
  191. * @param size Length of data in bytes
  192. * @param hash_alg Hash algorithm
  193. * @param digest Destination for digest
  194. * @param digest_size Length of digest buffer in bytes.
  195. * @return VB2_SUCCESS, or non-zero on error.
  196. */
  197. int vb2_digest_buffer(const uint8_t *buf,
  198. uint32_t size,
  199. enum vb2_hash_algorithm hash_alg,
  200. uint8_t *digest,
  201. uint32_t digest_size);
  202. #endif /* VBOOT_REFERENCE_2SHA_H_ */