2sha_utility.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. * Utility functions for message digest functions.
  6. */
  7. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2sha.h"
  10. #if VB2_SUPPORT_SHA1
  11. #define CTH_SHA1 VB2_HASH_SHA1
  12. #else
  13. #define CTH_SHA1 VB2_HASH_INVALID
  14. #endif
  15. #if VB2_SUPPORT_SHA256
  16. #define CTH_SHA256 VB2_HASH_SHA256
  17. #else
  18. #define CTH_SHA256 VB2_HASH_INVALID
  19. #endif
  20. #if VB2_SUPPORT_SHA512
  21. #define CTH_SHA512 VB2_HASH_SHA512
  22. #else
  23. #define CTH_SHA512 VB2_HASH_INVALID
  24. #endif
  25. static const uint8_t crypto_to_hash[] = {
  26. CTH_SHA1,
  27. CTH_SHA256,
  28. CTH_SHA512,
  29. CTH_SHA1,
  30. CTH_SHA256,
  31. CTH_SHA512,
  32. CTH_SHA1,
  33. CTH_SHA256,
  34. CTH_SHA512,
  35. CTH_SHA1,
  36. CTH_SHA256,
  37. CTH_SHA512,
  38. };
  39. enum vb2_hash_algorithm vb2_crypto_to_hash(uint32_t algorithm)
  40. {
  41. if (algorithm < ARRAY_SIZE(crypto_to_hash))
  42. return crypto_to_hash[algorithm];
  43. else
  44. return VB2_HASH_INVALID;
  45. }
  46. int vb2_digest_size(enum vb2_hash_algorithm hash_alg)
  47. {
  48. switch (hash_alg) {
  49. #if VB2_SUPPORT_SHA1
  50. case VB2_HASH_SHA1:
  51. return VB2_SHA1_DIGEST_SIZE;
  52. #endif
  53. #if VB2_SUPPORT_SHA256
  54. case VB2_HASH_SHA256:
  55. return VB2_SHA256_DIGEST_SIZE;
  56. #endif
  57. #if VB2_SUPPORT_SHA512
  58. case VB2_HASH_SHA512:
  59. return VB2_SHA512_DIGEST_SIZE;
  60. #endif
  61. default:
  62. return 0;
  63. }
  64. }
  65. int vb2_hash_block_size(enum vb2_hash_algorithm alg)
  66. {
  67. switch (alg) {
  68. #if VB2_SUPPORT_SHA1
  69. case VB2_HASH_SHA1:
  70. return VB2_SHA1_BLOCK_SIZE;
  71. #endif
  72. #if VB2_SUPPORT_SHA256
  73. case VB2_HASH_SHA256:
  74. return VB2_SHA256_BLOCK_SIZE;
  75. #endif
  76. #if VB2_SUPPORT_SHA512
  77. case VB2_HASH_SHA512:
  78. return VB2_SHA512_BLOCK_SIZE;
  79. #endif
  80. default:
  81. return 0;
  82. }
  83. }
  84. const char *vb2_get_hash_algorithm_name(enum vb2_hash_algorithm alg)
  85. {
  86. switch (alg) {
  87. #if VB2_SUPPORT_SHA1
  88. case VB2_HASH_SHA1:
  89. return VB2_SHA1_ALG_NAME;
  90. #endif
  91. #if VB2_SUPPORT_SHA256
  92. case VB2_HASH_SHA256:
  93. return VB2_SHA256_ALG_NAME;
  94. #endif
  95. #if VB2_SUPPORT_SHA512
  96. case VB2_HASH_SHA512:
  97. return VB2_SHA512_ALG_NAME;
  98. #endif
  99. default:
  100. return VB2_INVALID_ALG_NAME;
  101. }
  102. }
  103. int vb2_digest_init(struct vb2_digest_context *dc,
  104. enum vb2_hash_algorithm hash_alg)
  105. {
  106. dc->hash_alg = hash_alg;
  107. dc->using_hwcrypto = 0;
  108. switch (dc->hash_alg) {
  109. #if VB2_SUPPORT_SHA1
  110. case VB2_HASH_SHA1:
  111. vb2_sha1_init(&dc->sha1);
  112. return VB2_SUCCESS;
  113. #endif
  114. #if VB2_SUPPORT_SHA256
  115. case VB2_HASH_SHA256:
  116. vb2_sha256_init(&dc->sha256);
  117. return VB2_SUCCESS;
  118. #endif
  119. #if VB2_SUPPORT_SHA512
  120. case VB2_HASH_SHA512:
  121. vb2_sha512_init(&dc->sha512);
  122. return VB2_SUCCESS;
  123. #endif
  124. default:
  125. return VB2_ERROR_SHA_INIT_ALGORITHM;
  126. }
  127. }
  128. int vb2_digest_extend(struct vb2_digest_context *dc,
  129. const uint8_t *buf,
  130. uint32_t size)
  131. {
  132. switch (dc->hash_alg) {
  133. #if VB2_SUPPORT_SHA1
  134. case VB2_HASH_SHA1:
  135. vb2_sha1_update(&dc->sha1, buf, size);
  136. return VB2_SUCCESS;
  137. #endif
  138. #if VB2_SUPPORT_SHA256
  139. case VB2_HASH_SHA256:
  140. vb2_sha256_update(&dc->sha256, buf, size);
  141. return VB2_SUCCESS;
  142. #endif
  143. #if VB2_SUPPORT_SHA512
  144. case VB2_HASH_SHA512:
  145. vb2_sha512_update(&dc->sha512, buf, size);
  146. return VB2_SUCCESS;
  147. #endif
  148. default:
  149. return VB2_ERROR_SHA_EXTEND_ALGORITHM;
  150. }
  151. }
  152. int vb2_digest_finalize(struct vb2_digest_context *dc,
  153. uint8_t *digest,
  154. uint32_t digest_size)
  155. {
  156. if (digest_size < vb2_digest_size(dc->hash_alg))
  157. return VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE;
  158. switch (dc->hash_alg) {
  159. #if VB2_SUPPORT_SHA1
  160. case VB2_HASH_SHA1:
  161. vb2_sha1_finalize(&dc->sha1, digest);
  162. return VB2_SUCCESS;
  163. #endif
  164. #if VB2_SUPPORT_SHA256
  165. case VB2_HASH_SHA256:
  166. vb2_sha256_finalize(&dc->sha256, digest);
  167. return VB2_SUCCESS;
  168. #endif
  169. #if VB2_SUPPORT_SHA512
  170. case VB2_HASH_SHA512:
  171. vb2_sha512_finalize(&dc->sha512, digest);
  172. return VB2_SUCCESS;
  173. #endif
  174. default:
  175. return VB2_ERROR_SHA_FINALIZE_ALGORITHM;
  176. }
  177. }
  178. int vb2_digest_buffer(const uint8_t *buf,
  179. uint32_t size,
  180. enum vb2_hash_algorithm hash_alg,
  181. uint8_t *digest,
  182. uint32_t digest_size)
  183. {
  184. struct vb2_digest_context dc;
  185. int rv;
  186. rv = vb2_digest_init(&dc, hash_alg);
  187. if (rv)
  188. return rv;
  189. rv = vb2_digest_extend(&dc, buf, size);
  190. if (rv)
  191. return rv;
  192. return vb2_digest_finalize(&dc, digest, digest_size);
  193. }