2
0

legacy_sha.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * All SHA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/sha.h> /* diverse SHA macros */
  15. #include "internal/sha3.h" /* KECCAK1600_WIDTH */
  16. #include "crypto/evp.h"
  17. /* Used by legacy methods */
  18. #include "crypto/sha.h"
  19. #include "legacy_meth.h"
  20. #include "evp_local.h"
  21. /*-
  22. * LEGACY methods for SHA.
  23. * These only remain to support engines that can get these methods.
  24. * Hardware support for SHA3 has been removed from these legacy cases.
  25. */
  26. #define IMPLEMENT_LEGACY_EVP_MD_METH_SHA3(nm, fn, tag) \
  27. static int nm##_init(EVP_MD_CTX *ctx) \
  28. { \
  29. return fn##_init(EVP_MD_CTX_get0_md_data(ctx), tag, ctx->digest->md_size * 8); \
  30. } \
  31. static int nm##_update(EVP_MD_CTX *ctx, const void *data, size_t count) \
  32. { \
  33. return fn##_update(EVP_MD_CTX_get0_md_data(ctx), data, count); \
  34. } \
  35. static int nm##_final(EVP_MD_CTX *ctx, unsigned char *md) \
  36. { \
  37. return fn##_final(md, EVP_MD_CTX_get0_md_data(ctx)); \
  38. }
  39. #define IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(nm, fn, tag) \
  40. static int nm##_init(EVP_MD_CTX *ctx) \
  41. { \
  42. return fn##_init(EVP_MD_CTX_get0_md_data(ctx), tag, ctx->digest->md_size * 8); \
  43. } \
  44. #define sha512_224_Init sha512_224_init
  45. #define sha512_256_Init sha512_256_init
  46. #define sha512_224_Update SHA512_Update
  47. #define sha512_224_Final SHA512_Final
  48. #define sha512_256_Update SHA512_Update
  49. #define sha512_256_Final SHA512_Final
  50. IMPLEMENT_LEGACY_EVP_MD_METH(sha1, SHA1)
  51. IMPLEMENT_LEGACY_EVP_MD_METH(sha224, SHA224)
  52. IMPLEMENT_LEGACY_EVP_MD_METH(sha256, SHA256)
  53. IMPLEMENT_LEGACY_EVP_MD_METH(sha384, SHA384)
  54. IMPLEMENT_LEGACY_EVP_MD_METH(sha512, SHA512)
  55. IMPLEMENT_LEGACY_EVP_MD_METH(sha512_224_int, sha512_224)
  56. IMPLEMENT_LEGACY_EVP_MD_METH(sha512_256_int, sha512_256)
  57. IMPLEMENT_LEGACY_EVP_MD_METH_SHA3(sha3_int, ossl_sha3, '\x06')
  58. IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(shake, ossl_sha3, '\x1f')
  59. static int sha1_int_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  60. {
  61. return ossl_sha1_ctrl(ctx != NULL ? EVP_MD_CTX_get0_md_data(ctx) : NULL,
  62. cmd, p1, p2);
  63. }
  64. static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
  65. {
  66. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  67. switch (cmd) {
  68. case EVP_MD_CTRL_XOF_LEN:
  69. ctx->md_size = p1;
  70. return 1;
  71. default:
  72. return 0;
  73. }
  74. }
  75. static const EVP_MD sha1_md = {
  76. NID_sha1,
  77. NID_sha1WithRSAEncryption,
  78. SHA_DIGEST_LENGTH,
  79. EVP_MD_FLAG_DIGALGID_ABSENT,
  80. EVP_ORIG_GLOBAL,
  81. LEGACY_EVP_MD_METH_TABLE(sha1_init, sha1_update, sha1_final, sha1_int_ctrl,
  82. SHA_CBLOCK),
  83. };
  84. const EVP_MD *EVP_sha1(void)
  85. {
  86. return &sha1_md;
  87. }
  88. static const EVP_MD sha224_md = {
  89. NID_sha224,
  90. NID_sha224WithRSAEncryption,
  91. SHA224_DIGEST_LENGTH,
  92. EVP_MD_FLAG_DIGALGID_ABSENT,
  93. EVP_ORIG_GLOBAL,
  94. LEGACY_EVP_MD_METH_TABLE(sha224_init, sha224_update, sha224_final, NULL,
  95. SHA256_CBLOCK),
  96. };
  97. const EVP_MD *EVP_sha224(void)
  98. {
  99. return &sha224_md;
  100. }
  101. static const EVP_MD sha256_md = {
  102. NID_sha256,
  103. NID_sha256WithRSAEncryption,
  104. SHA256_DIGEST_LENGTH,
  105. EVP_MD_FLAG_DIGALGID_ABSENT,
  106. EVP_ORIG_GLOBAL,
  107. LEGACY_EVP_MD_METH_TABLE(sha256_init, sha256_update, sha256_final, NULL,
  108. SHA256_CBLOCK),
  109. };
  110. const EVP_MD *EVP_sha256(void)
  111. {
  112. return &sha256_md;
  113. }
  114. static const EVP_MD sha512_224_md = {
  115. NID_sha512_224,
  116. NID_sha512_224WithRSAEncryption,
  117. SHA224_DIGEST_LENGTH,
  118. EVP_MD_FLAG_DIGALGID_ABSENT,
  119. EVP_ORIG_GLOBAL,
  120. LEGACY_EVP_MD_METH_TABLE(sha512_224_int_init, sha512_224_int_update,
  121. sha512_224_int_final, NULL, SHA512_CBLOCK),
  122. };
  123. const EVP_MD *EVP_sha512_224(void)
  124. {
  125. return &sha512_224_md;
  126. }
  127. static const EVP_MD sha512_256_md = {
  128. NID_sha512_256,
  129. NID_sha512_256WithRSAEncryption,
  130. SHA256_DIGEST_LENGTH,
  131. EVP_MD_FLAG_DIGALGID_ABSENT,
  132. EVP_ORIG_GLOBAL,
  133. LEGACY_EVP_MD_METH_TABLE(sha512_256_int_init, sha512_256_int_update,
  134. sha512_256_int_final, NULL, SHA512_CBLOCK),
  135. };
  136. const EVP_MD *EVP_sha512_256(void)
  137. {
  138. return &sha512_256_md;
  139. }
  140. static const EVP_MD sha384_md = {
  141. NID_sha384,
  142. NID_sha384WithRSAEncryption,
  143. SHA384_DIGEST_LENGTH,
  144. EVP_MD_FLAG_DIGALGID_ABSENT,
  145. EVP_ORIG_GLOBAL,
  146. LEGACY_EVP_MD_METH_TABLE(sha384_init, sha384_update, sha384_final, NULL,
  147. SHA512_CBLOCK),
  148. };
  149. const EVP_MD *EVP_sha384(void)
  150. {
  151. return &sha384_md;
  152. }
  153. static const EVP_MD sha512_md = {
  154. NID_sha512,
  155. NID_sha512WithRSAEncryption,
  156. SHA512_DIGEST_LENGTH,
  157. EVP_MD_FLAG_DIGALGID_ABSENT,
  158. EVP_ORIG_GLOBAL,
  159. LEGACY_EVP_MD_METH_TABLE(sha512_init, sha512_update, sha512_final, NULL,
  160. SHA512_CBLOCK),
  161. };
  162. const EVP_MD *EVP_sha512(void)
  163. {
  164. return &sha512_md;
  165. }
  166. #define EVP_MD_SHA3(bitlen) \
  167. const EVP_MD *EVP_sha3_##bitlen(void) \
  168. { \
  169. static const EVP_MD sha3_##bitlen##_md = { \
  170. NID_sha3_##bitlen, \
  171. NID_RSA_SHA3_##bitlen, \
  172. bitlen / 8, \
  173. EVP_MD_FLAG_DIGALGID_ABSENT, \
  174. EVP_ORIG_GLOBAL, \
  175. LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update, \
  176. sha3_int_final, NULL, \
  177. (KECCAK1600_WIDTH - bitlen * 2) / 8), \
  178. }; \
  179. return &sha3_##bitlen##_md; \
  180. }
  181. #define EVP_MD_SHAKE(bitlen) \
  182. const EVP_MD *EVP_shake##bitlen(void) \
  183. { \
  184. static const EVP_MD shake##bitlen##_md = { \
  185. NID_shake##bitlen, \
  186. 0, \
  187. bitlen / 8, \
  188. EVP_MD_FLAG_XOF, \
  189. EVP_ORIG_GLOBAL, \
  190. LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final, \
  191. shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8), \
  192. }; \
  193. return &shake##bitlen##_md; \
  194. }
  195. EVP_MD_SHA3(224)
  196. EVP_MD_SHA3(256)
  197. EVP_MD_SHA3(384)
  198. EVP_MD_SHA3(512)
  199. EVP_MD_SHAKE(128)
  200. EVP_MD_SHAKE(256)