2
0

legacy_sha.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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, sha3, '\x06')
  58. IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(shake, sha3, '\x1f')
  59. static int sha1_int_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  60. {
  61. return sha1_ctrl(ctx != NULL ? EVP_MD_CTX_md_data(ctx) : NULL, cmd, p1, p2);
  62. }
  63. static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
  64. {
  65. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  66. switch (cmd) {
  67. case EVP_MD_CTRL_XOF_LEN:
  68. ctx->md_size = p1;
  69. return 1;
  70. default:
  71. return 0;
  72. }
  73. }
  74. static const EVP_MD sha1_md = {
  75. NID_sha1,
  76. NID_sha1WithRSAEncryption,
  77. SHA_DIGEST_LENGTH,
  78. EVP_MD_FLAG_DIGALGID_ABSENT,
  79. LEGACY_EVP_MD_METH_TABLE(sha1_init, sha1_update, sha1_final, sha1_int_ctrl,
  80. SHA_CBLOCK),
  81. };
  82. const EVP_MD *EVP_sha1(void)
  83. {
  84. return &sha1_md;
  85. }
  86. static const EVP_MD sha224_md = {
  87. NID_sha224,
  88. NID_sha224WithRSAEncryption,
  89. SHA224_DIGEST_LENGTH,
  90. EVP_MD_FLAG_DIGALGID_ABSENT,
  91. LEGACY_EVP_MD_METH_TABLE(sha224_init, sha224_update, sha224_final, NULL,
  92. SHA256_CBLOCK),
  93. };
  94. const EVP_MD *EVP_sha224(void)
  95. {
  96. return &sha224_md;
  97. }
  98. static const EVP_MD sha256_md = {
  99. NID_sha256,
  100. NID_sha256WithRSAEncryption,
  101. SHA256_DIGEST_LENGTH,
  102. EVP_MD_FLAG_DIGALGID_ABSENT,
  103. LEGACY_EVP_MD_METH_TABLE(sha256_init, sha256_update, sha256_final, NULL,
  104. SHA256_CBLOCK),
  105. };
  106. const EVP_MD *EVP_sha256(void)
  107. {
  108. return &sha256_md;
  109. }
  110. static const EVP_MD sha512_224_md = {
  111. NID_sha512_224,
  112. NID_sha512_224WithRSAEncryption,
  113. SHA224_DIGEST_LENGTH,
  114. EVP_MD_FLAG_DIGALGID_ABSENT,
  115. LEGACY_EVP_MD_METH_TABLE(sha512_224_int_init, sha512_224_int_update,
  116. sha512_224_int_final, NULL, SHA512_CBLOCK),
  117. };
  118. const EVP_MD *EVP_sha512_224(void)
  119. {
  120. return &sha512_224_md;
  121. }
  122. static const EVP_MD sha512_256_md = {
  123. NID_sha512_256,
  124. NID_sha512_256WithRSAEncryption,
  125. SHA256_DIGEST_LENGTH,
  126. EVP_MD_FLAG_DIGALGID_ABSENT,
  127. LEGACY_EVP_MD_METH_TABLE(sha512_256_int_init, sha512_256_int_update,
  128. sha512_256_int_final, NULL, SHA512_CBLOCK),
  129. };
  130. const EVP_MD *EVP_sha512_256(void)
  131. {
  132. return &sha512_256_md;
  133. }
  134. static const EVP_MD sha384_md = {
  135. NID_sha384,
  136. NID_sha384WithRSAEncryption,
  137. SHA384_DIGEST_LENGTH,
  138. EVP_MD_FLAG_DIGALGID_ABSENT,
  139. LEGACY_EVP_MD_METH_TABLE(sha384_init, sha384_update, sha384_final, NULL,
  140. SHA512_CBLOCK),
  141. };
  142. const EVP_MD *EVP_sha384(void)
  143. {
  144. return &sha384_md;
  145. }
  146. static const EVP_MD sha512_md = {
  147. NID_sha512,
  148. NID_sha512WithRSAEncryption,
  149. SHA512_DIGEST_LENGTH,
  150. EVP_MD_FLAG_DIGALGID_ABSENT,
  151. LEGACY_EVP_MD_METH_TABLE(sha512_init, sha512_update, sha512_final, NULL,
  152. SHA512_CBLOCK),
  153. };
  154. const EVP_MD *EVP_sha512(void)
  155. {
  156. return &sha512_md;
  157. }
  158. #define EVP_MD_SHA3(bitlen) \
  159. const EVP_MD *EVP_sha3_##bitlen(void) \
  160. { \
  161. static const EVP_MD sha3_##bitlen##_md = { \
  162. NID_sha3_##bitlen, \
  163. NID_RSA_SHA3_##bitlen, \
  164. bitlen / 8, \
  165. EVP_MD_FLAG_DIGALGID_ABSENT, \
  166. LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update, \
  167. sha3_int_final, NULL, \
  168. (KECCAK1600_WIDTH - bitlen * 2) / 8), \
  169. }; \
  170. return &sha3_##bitlen##_md; \
  171. }
  172. #define EVP_MD_SHAKE(bitlen) \
  173. const EVP_MD *EVP_shake##bitlen(void) \
  174. { \
  175. static const EVP_MD shake##bitlen##_md = { \
  176. NID_shake##bitlen, \
  177. 0, \
  178. bitlen / 8, \
  179. EVP_MD_FLAG_XOF, \
  180. LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final, \
  181. shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8), \
  182. }; \
  183. return &shake##bitlen##_md; \
  184. }
  185. EVP_MD_SHA3(224)
  186. EVP_MD_SHA3(256)
  187. EVP_MD_SHA3(384)
  188. EVP_MD_SHA3(512)
  189. EVP_MD_SHAKE(128)
  190. EVP_MD_SHAKE(256)