legacy_sha.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2019-2020 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_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_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_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_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_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. LEGACY_EVP_MD_METH_TABLE(sha1_init, sha1_update, sha1_final, sha1_int_ctrl,
  81. SHA_CBLOCK),
  82. };
  83. const EVP_MD *EVP_sha1(void)
  84. {
  85. return &sha1_md;
  86. }
  87. static const EVP_MD sha224_md = {
  88. NID_sha224,
  89. NID_sha224WithRSAEncryption,
  90. SHA224_DIGEST_LENGTH,
  91. EVP_MD_FLAG_DIGALGID_ABSENT,
  92. LEGACY_EVP_MD_METH_TABLE(sha224_init, sha224_update, sha224_final, NULL,
  93. SHA256_CBLOCK),
  94. };
  95. const EVP_MD *EVP_sha224(void)
  96. {
  97. return &sha224_md;
  98. }
  99. static const EVP_MD sha256_md = {
  100. NID_sha256,
  101. NID_sha256WithRSAEncryption,
  102. SHA256_DIGEST_LENGTH,
  103. EVP_MD_FLAG_DIGALGID_ABSENT,
  104. LEGACY_EVP_MD_METH_TABLE(sha256_init, sha256_update, sha256_final, NULL,
  105. SHA256_CBLOCK),
  106. };
  107. const EVP_MD *EVP_sha256(void)
  108. {
  109. return &sha256_md;
  110. }
  111. static const EVP_MD sha512_224_md = {
  112. NID_sha512_224,
  113. NID_sha512_224WithRSAEncryption,
  114. SHA224_DIGEST_LENGTH,
  115. EVP_MD_FLAG_DIGALGID_ABSENT,
  116. LEGACY_EVP_MD_METH_TABLE(sha512_224_int_init, sha512_224_int_update,
  117. sha512_224_int_final, NULL, SHA512_CBLOCK),
  118. };
  119. const EVP_MD *EVP_sha512_224(void)
  120. {
  121. return &sha512_224_md;
  122. }
  123. static const EVP_MD sha512_256_md = {
  124. NID_sha512_256,
  125. NID_sha512_256WithRSAEncryption,
  126. SHA256_DIGEST_LENGTH,
  127. EVP_MD_FLAG_DIGALGID_ABSENT,
  128. LEGACY_EVP_MD_METH_TABLE(sha512_256_int_init, sha512_256_int_update,
  129. sha512_256_int_final, NULL, SHA512_CBLOCK),
  130. };
  131. const EVP_MD *EVP_sha512_256(void)
  132. {
  133. return &sha512_256_md;
  134. }
  135. static const EVP_MD sha384_md = {
  136. NID_sha384,
  137. NID_sha384WithRSAEncryption,
  138. SHA384_DIGEST_LENGTH,
  139. EVP_MD_FLAG_DIGALGID_ABSENT,
  140. LEGACY_EVP_MD_METH_TABLE(sha384_init, sha384_update, sha384_final, NULL,
  141. SHA512_CBLOCK),
  142. };
  143. const EVP_MD *EVP_sha384(void)
  144. {
  145. return &sha384_md;
  146. }
  147. static const EVP_MD sha512_md = {
  148. NID_sha512,
  149. NID_sha512WithRSAEncryption,
  150. SHA512_DIGEST_LENGTH,
  151. EVP_MD_FLAG_DIGALGID_ABSENT,
  152. LEGACY_EVP_MD_METH_TABLE(sha512_init, sha512_update, sha512_final, NULL,
  153. SHA512_CBLOCK),
  154. };
  155. const EVP_MD *EVP_sha512(void)
  156. {
  157. return &sha512_md;
  158. }
  159. #define EVP_MD_SHA3(bitlen) \
  160. const EVP_MD *EVP_sha3_##bitlen(void) \
  161. { \
  162. static const EVP_MD sha3_##bitlen##_md = { \
  163. NID_sha3_##bitlen, \
  164. NID_RSA_SHA3_##bitlen, \
  165. bitlen / 8, \
  166. EVP_MD_FLAG_DIGALGID_ABSENT, \
  167. LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update, \
  168. sha3_int_final, NULL, \
  169. (KECCAK1600_WIDTH - bitlen * 2) / 8), \
  170. }; \
  171. return &sha3_##bitlen##_md; \
  172. }
  173. #define EVP_MD_SHAKE(bitlen) \
  174. const EVP_MD *EVP_shake##bitlen(void) \
  175. { \
  176. static const EVP_MD shake##bitlen##_md = { \
  177. NID_shake##bitlen, \
  178. 0, \
  179. bitlen / 8, \
  180. EVP_MD_FLAG_XOF, \
  181. LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final, \
  182. shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8), \
  183. }; \
  184. return &shake##bitlen##_md; \
  185. }
  186. EVP_MD_SHA3(224)
  187. EVP_MD_SHA3(256)
  188. EVP_MD_SHA3(384)
  189. EVP_MD_SHA3(512)
  190. EVP_MD_SHAKE(128)
  191. EVP_MD_SHAKE(256)