legacy_sha.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. KECCAK1600_CTX *kctx = EVP_MD_CTX_get0_md_data(ctx); \
  38. return fn##_final(kctx, md, kctx->md_size); \
  39. }
  40. #define IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(nm, fn, tag) \
  41. static int nm##_init(EVP_MD_CTX *ctx) \
  42. { \
  43. return fn##_init(EVP_MD_CTX_get0_md_data(ctx), tag, ctx->digest->md_size * 8); \
  44. } \
  45. #define sha512_224_Init sha512_224_init
  46. #define sha512_256_Init sha512_256_init
  47. #define sha512_224_Update SHA512_Update
  48. #define sha512_224_Final SHA512_Final
  49. #define sha512_256_Update SHA512_Update
  50. #define sha512_256_Final SHA512_Final
  51. IMPLEMENT_LEGACY_EVP_MD_METH(sha1, SHA1)
  52. IMPLEMENT_LEGACY_EVP_MD_METH(sha224, SHA224)
  53. IMPLEMENT_LEGACY_EVP_MD_METH(sha256, SHA256)
  54. IMPLEMENT_LEGACY_EVP_MD_METH(sha384, SHA384)
  55. IMPLEMENT_LEGACY_EVP_MD_METH(sha512, SHA512)
  56. IMPLEMENT_LEGACY_EVP_MD_METH(sha512_224_int, sha512_224)
  57. IMPLEMENT_LEGACY_EVP_MD_METH(sha512_256_int, sha512_256)
  58. IMPLEMENT_LEGACY_EVP_MD_METH_SHA3(sha3_int, ossl_sha3, '\x06')
  59. IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(shake, ossl_sha3, '\x1f')
  60. static int sha1_int_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  61. {
  62. return ossl_sha1_ctrl(ctx != NULL ? EVP_MD_CTX_get0_md_data(ctx) : NULL,
  63. cmd, p1, p2);
  64. }
  65. static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
  66. {
  67. KECCAK1600_CTX *ctx;
  68. if (evp_ctx == NULL)
  69. return 0;
  70. ctx = evp_ctx->md_data;
  71. switch (cmd) {
  72. case EVP_MD_CTRL_XOF_LEN:
  73. ctx->md_size = p1;
  74. return 1;
  75. default:
  76. return 0;
  77. }
  78. }
  79. static const EVP_MD sha1_md = {
  80. NID_sha1,
  81. NID_sha1WithRSAEncryption,
  82. SHA_DIGEST_LENGTH,
  83. EVP_MD_FLAG_DIGALGID_ABSENT,
  84. EVP_ORIG_GLOBAL,
  85. LEGACY_EVP_MD_METH_TABLE(sha1_init, sha1_update, sha1_final, sha1_int_ctrl,
  86. SHA_CBLOCK),
  87. };
  88. const EVP_MD *EVP_sha1(void)
  89. {
  90. return &sha1_md;
  91. }
  92. static const EVP_MD sha224_md = {
  93. NID_sha224,
  94. NID_sha224WithRSAEncryption,
  95. SHA224_DIGEST_LENGTH,
  96. EVP_MD_FLAG_DIGALGID_ABSENT,
  97. EVP_ORIG_GLOBAL,
  98. LEGACY_EVP_MD_METH_TABLE(sha224_init, sha224_update, sha224_final, NULL,
  99. SHA256_CBLOCK),
  100. };
  101. const EVP_MD *EVP_sha224(void)
  102. {
  103. return &sha224_md;
  104. }
  105. static const EVP_MD sha256_md = {
  106. NID_sha256,
  107. NID_sha256WithRSAEncryption,
  108. SHA256_DIGEST_LENGTH,
  109. EVP_MD_FLAG_DIGALGID_ABSENT,
  110. EVP_ORIG_GLOBAL,
  111. LEGACY_EVP_MD_METH_TABLE(sha256_init, sha256_update, sha256_final, NULL,
  112. SHA256_CBLOCK),
  113. };
  114. const EVP_MD *EVP_sha256(void)
  115. {
  116. return &sha256_md;
  117. }
  118. static const EVP_MD sha512_224_md = {
  119. NID_sha512_224,
  120. NID_sha512_224WithRSAEncryption,
  121. SHA224_DIGEST_LENGTH,
  122. EVP_MD_FLAG_DIGALGID_ABSENT,
  123. EVP_ORIG_GLOBAL,
  124. LEGACY_EVP_MD_METH_TABLE(sha512_224_int_init, sha512_224_int_update,
  125. sha512_224_int_final, NULL, SHA512_CBLOCK),
  126. };
  127. const EVP_MD *EVP_sha512_224(void)
  128. {
  129. return &sha512_224_md;
  130. }
  131. static const EVP_MD sha512_256_md = {
  132. NID_sha512_256,
  133. NID_sha512_256WithRSAEncryption,
  134. SHA256_DIGEST_LENGTH,
  135. EVP_MD_FLAG_DIGALGID_ABSENT,
  136. EVP_ORIG_GLOBAL,
  137. LEGACY_EVP_MD_METH_TABLE(sha512_256_int_init, sha512_256_int_update,
  138. sha512_256_int_final, NULL, SHA512_CBLOCK),
  139. };
  140. const EVP_MD *EVP_sha512_256(void)
  141. {
  142. return &sha512_256_md;
  143. }
  144. static const EVP_MD sha384_md = {
  145. NID_sha384,
  146. NID_sha384WithRSAEncryption,
  147. SHA384_DIGEST_LENGTH,
  148. EVP_MD_FLAG_DIGALGID_ABSENT,
  149. EVP_ORIG_GLOBAL,
  150. LEGACY_EVP_MD_METH_TABLE(sha384_init, sha384_update, sha384_final, NULL,
  151. SHA512_CBLOCK),
  152. };
  153. const EVP_MD *EVP_sha384(void)
  154. {
  155. return &sha384_md;
  156. }
  157. static const EVP_MD sha512_md = {
  158. NID_sha512,
  159. NID_sha512WithRSAEncryption,
  160. SHA512_DIGEST_LENGTH,
  161. EVP_MD_FLAG_DIGALGID_ABSENT,
  162. EVP_ORIG_GLOBAL,
  163. LEGACY_EVP_MD_METH_TABLE(sha512_init, sha512_update, sha512_final, NULL,
  164. SHA512_CBLOCK),
  165. };
  166. const EVP_MD *EVP_sha512(void)
  167. {
  168. return &sha512_md;
  169. }
  170. #define EVP_MD_SHA3(bitlen) \
  171. const EVP_MD *EVP_sha3_##bitlen(void) \
  172. { \
  173. static const EVP_MD sha3_##bitlen##_md = { \
  174. NID_sha3_##bitlen, \
  175. NID_RSA_SHA3_##bitlen, \
  176. bitlen / 8, \
  177. EVP_MD_FLAG_DIGALGID_ABSENT, \
  178. EVP_ORIG_GLOBAL, \
  179. LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update, \
  180. sha3_int_final, NULL, \
  181. (KECCAK1600_WIDTH - bitlen * 2) / 8), \
  182. }; \
  183. return &sha3_##bitlen##_md; \
  184. }
  185. #define EVP_MD_SHAKE(bitlen) \
  186. const EVP_MD *EVP_shake##bitlen(void) \
  187. { \
  188. static const EVP_MD shake##bitlen##_md = { \
  189. NID_shake##bitlen, \
  190. 0, \
  191. bitlen / 8, \
  192. EVP_MD_FLAG_XOF, \
  193. EVP_ORIG_GLOBAL, \
  194. LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final, \
  195. shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8), \
  196. }; \
  197. return &shake##bitlen##_md; \
  198. }
  199. EVP_MD_SHA3(224)
  200. EVP_MD_SHA3(256)
  201. EVP_MD_SHA3(384)
  202. EVP_MD_SHA3(512)
  203. EVP_MD_SHAKE(128)
  204. EVP_MD_SHAKE(256)