m_sha1.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 1995-2018 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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/evp.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/sha.h>
  14. #include <openssl/rsa.h>
  15. #include "internal/evp_int.h"
  16. #include "internal/sha.h"
  17. static int init(EVP_MD_CTX *ctx)
  18. {
  19. return SHA1_Init(EVP_MD_CTX_md_data(ctx));
  20. }
  21. static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
  22. {
  23. return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
  24. }
  25. static int final(EVP_MD_CTX *ctx, unsigned char *md)
  26. {
  27. return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
  28. }
  29. static int ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  30. {
  31. return sha1_ctrl(ctx != NULL ? EVP_MD_CTX_md_data(ctx) : NULL, cmd, p1, p2);
  32. }
  33. static const EVP_MD sha1_md = {
  34. NID_sha1,
  35. NID_sha1WithRSAEncryption,
  36. SHA_DIGEST_LENGTH,
  37. EVP_MD_FLAG_DIGALGID_ABSENT,
  38. init,
  39. update,
  40. final,
  41. NULL,
  42. NULL,
  43. SHA_CBLOCK,
  44. sizeof(EVP_MD *) + sizeof(SHA_CTX),
  45. ctrl
  46. };
  47. const EVP_MD *EVP_sha1(void)
  48. {
  49. return &sha1_md;
  50. }
  51. static int init224(EVP_MD_CTX *ctx)
  52. {
  53. return SHA224_Init(EVP_MD_CTX_md_data(ctx));
  54. }
  55. static int update224(EVP_MD_CTX *ctx, const void *data, size_t count)
  56. {
  57. return SHA224_Update(EVP_MD_CTX_md_data(ctx), data, count);
  58. }
  59. static int final224(EVP_MD_CTX *ctx, unsigned char *md)
  60. {
  61. return SHA224_Final(md, EVP_MD_CTX_md_data(ctx));
  62. }
  63. static int init256(EVP_MD_CTX *ctx)
  64. {
  65. return SHA256_Init(EVP_MD_CTX_md_data(ctx));
  66. }
  67. static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
  68. {
  69. return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
  70. }
  71. static int final256(EVP_MD_CTX *ctx, unsigned char *md)
  72. {
  73. return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
  74. }
  75. static const EVP_MD sha224_md = {
  76. NID_sha224,
  77. NID_sha224WithRSAEncryption,
  78. SHA224_DIGEST_LENGTH,
  79. EVP_MD_FLAG_DIGALGID_ABSENT,
  80. init224,
  81. update224,
  82. final224,
  83. NULL,
  84. NULL,
  85. SHA256_CBLOCK,
  86. sizeof(EVP_MD *) + sizeof(SHA256_CTX),
  87. };
  88. const EVP_MD *EVP_sha224(void)
  89. {
  90. return &sha224_md;
  91. }
  92. static const EVP_MD sha256_md = {
  93. NID_sha256,
  94. NID_sha256WithRSAEncryption,
  95. SHA256_DIGEST_LENGTH,
  96. EVP_MD_FLAG_DIGALGID_ABSENT,
  97. init256,
  98. update256,
  99. final256,
  100. NULL,
  101. NULL,
  102. SHA256_CBLOCK,
  103. sizeof(EVP_MD *) + sizeof(SHA256_CTX),
  104. };
  105. const EVP_MD *EVP_sha256(void)
  106. {
  107. return &sha256_md;
  108. }
  109. static int init512_224(EVP_MD_CTX *ctx)
  110. {
  111. return sha512_224_init(EVP_MD_CTX_md_data(ctx));
  112. }
  113. static int init512_256(EVP_MD_CTX *ctx)
  114. {
  115. return sha512_256_init(EVP_MD_CTX_md_data(ctx));
  116. }
  117. static int init384(EVP_MD_CTX *ctx)
  118. {
  119. return SHA384_Init(EVP_MD_CTX_md_data(ctx));
  120. }
  121. static int update384(EVP_MD_CTX *ctx, const void *data, size_t count)
  122. {
  123. return SHA384_Update(EVP_MD_CTX_md_data(ctx), data, count);
  124. }
  125. static int final384(EVP_MD_CTX *ctx, unsigned char *md)
  126. {
  127. return SHA384_Final(md, EVP_MD_CTX_md_data(ctx));
  128. }
  129. static int init512(EVP_MD_CTX *ctx)
  130. {
  131. return SHA512_Init(EVP_MD_CTX_md_data(ctx));
  132. }
  133. /* See comment in SHA224/256 section */
  134. static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
  135. {
  136. return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
  137. }
  138. static int final512(EVP_MD_CTX *ctx, unsigned char *md)
  139. {
  140. return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
  141. }
  142. static const EVP_MD sha512_224_md = {
  143. NID_sha512_224,
  144. NID_sha512_224WithRSAEncryption,
  145. SHA224_DIGEST_LENGTH,
  146. EVP_MD_FLAG_DIGALGID_ABSENT,
  147. init512_224,
  148. update512,
  149. final512,
  150. NULL,
  151. NULL,
  152. SHA512_CBLOCK,
  153. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  154. };
  155. const EVP_MD *EVP_sha512_224(void)
  156. {
  157. return &sha512_224_md;
  158. }
  159. static const EVP_MD sha512_256_md = {
  160. NID_sha512_256,
  161. NID_sha512_256WithRSAEncryption,
  162. SHA256_DIGEST_LENGTH,
  163. EVP_MD_FLAG_DIGALGID_ABSENT,
  164. init512_256,
  165. update512,
  166. final512,
  167. NULL,
  168. NULL,
  169. SHA512_CBLOCK,
  170. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  171. };
  172. const EVP_MD *EVP_sha512_256(void)
  173. {
  174. return &sha512_256_md;
  175. }
  176. static const EVP_MD sha384_md = {
  177. NID_sha384,
  178. NID_sha384WithRSAEncryption,
  179. SHA384_DIGEST_LENGTH,
  180. EVP_MD_FLAG_DIGALGID_ABSENT,
  181. init384,
  182. update384,
  183. final384,
  184. NULL,
  185. NULL,
  186. SHA512_CBLOCK,
  187. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  188. };
  189. const EVP_MD *EVP_sha384(void)
  190. {
  191. return &sha384_md;
  192. }
  193. static const EVP_MD sha512_md = {
  194. NID_sha512,
  195. NID_sha512WithRSAEncryption,
  196. SHA512_DIGEST_LENGTH,
  197. EVP_MD_FLAG_DIGALGID_ABSENT,
  198. init512,
  199. update512,
  200. final512,
  201. NULL,
  202. NULL,
  203. SHA512_CBLOCK,
  204. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  205. };
  206. const EVP_MD *EVP_sha512(void)
  207. {
  208. return &sha512_md;
  209. }