m_sha1.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "crypto/evp.h"
  16. #include "crypto/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 mslen, void *ms)
  30. {
  31. unsigned char padtmp[40];
  32. unsigned char sha1tmp[SHA_DIGEST_LENGTH];
  33. SHA_CTX *sha1;
  34. if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
  35. return -2;
  36. if (ctx == NULL)
  37. return 0;
  38. sha1 = EVP_MD_CTX_md_data(ctx);
  39. /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
  40. if (mslen != 48)
  41. return 0;
  42. /* At this point hash contains all handshake messages, update
  43. * with master secret and pad_1.
  44. */
  45. if (SHA1_Update(sha1, ms, mslen) <= 0)
  46. return 0;
  47. /* Set padtmp to pad_1 value */
  48. memset(padtmp, 0x36, sizeof(padtmp));
  49. if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
  50. return 0;
  51. if (!SHA1_Final(sha1tmp, sha1))
  52. return 0;
  53. /* Reinitialise context */
  54. if (!SHA1_Init(sha1))
  55. return 0;
  56. if (SHA1_Update(sha1, ms, mslen) <= 0)
  57. return 0;
  58. /* Set padtmp to pad_2 value */
  59. memset(padtmp, 0x5c, sizeof(padtmp));
  60. if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
  61. return 0;
  62. if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
  63. return 0;
  64. /* Now when ctx is finalised it will return the SSL v3 hash value */
  65. OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
  66. return 1;
  67. }
  68. static const EVP_MD sha1_md = {
  69. NID_sha1,
  70. NID_sha1WithRSAEncryption,
  71. SHA_DIGEST_LENGTH,
  72. EVP_MD_FLAG_DIGALGID_ABSENT,
  73. init,
  74. update,
  75. final,
  76. NULL,
  77. NULL,
  78. SHA_CBLOCK,
  79. sizeof(EVP_MD *) + sizeof(SHA_CTX),
  80. ctrl
  81. };
  82. const EVP_MD *EVP_sha1(void)
  83. {
  84. return &sha1_md;
  85. }
  86. static int init224(EVP_MD_CTX *ctx)
  87. {
  88. return SHA224_Init(EVP_MD_CTX_md_data(ctx));
  89. }
  90. static int update224(EVP_MD_CTX *ctx, const void *data, size_t count)
  91. {
  92. return SHA224_Update(EVP_MD_CTX_md_data(ctx), data, count);
  93. }
  94. static int final224(EVP_MD_CTX *ctx, unsigned char *md)
  95. {
  96. return SHA224_Final(md, EVP_MD_CTX_md_data(ctx));
  97. }
  98. static int init256(EVP_MD_CTX *ctx)
  99. {
  100. return SHA256_Init(EVP_MD_CTX_md_data(ctx));
  101. }
  102. static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
  103. {
  104. return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
  105. }
  106. static int final256(EVP_MD_CTX *ctx, unsigned char *md)
  107. {
  108. return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
  109. }
  110. static const EVP_MD sha224_md = {
  111. NID_sha224,
  112. NID_sha224WithRSAEncryption,
  113. SHA224_DIGEST_LENGTH,
  114. EVP_MD_FLAG_DIGALGID_ABSENT,
  115. init224,
  116. update224,
  117. final224,
  118. NULL,
  119. NULL,
  120. SHA256_CBLOCK,
  121. sizeof(EVP_MD *) + sizeof(SHA256_CTX),
  122. };
  123. const EVP_MD *EVP_sha224(void)
  124. {
  125. return &sha224_md;
  126. }
  127. static const EVP_MD sha256_md = {
  128. NID_sha256,
  129. NID_sha256WithRSAEncryption,
  130. SHA256_DIGEST_LENGTH,
  131. EVP_MD_FLAG_DIGALGID_ABSENT,
  132. init256,
  133. update256,
  134. final256,
  135. NULL,
  136. NULL,
  137. SHA256_CBLOCK,
  138. sizeof(EVP_MD *) + sizeof(SHA256_CTX),
  139. };
  140. const EVP_MD *EVP_sha256(void)
  141. {
  142. return &sha256_md;
  143. }
  144. static int init512_224(EVP_MD_CTX *ctx)
  145. {
  146. return sha512_224_init(EVP_MD_CTX_md_data(ctx));
  147. }
  148. static int init512_256(EVP_MD_CTX *ctx)
  149. {
  150. return sha512_256_init(EVP_MD_CTX_md_data(ctx));
  151. }
  152. static int init384(EVP_MD_CTX *ctx)
  153. {
  154. return SHA384_Init(EVP_MD_CTX_md_data(ctx));
  155. }
  156. static int update384(EVP_MD_CTX *ctx, const void *data, size_t count)
  157. {
  158. return SHA384_Update(EVP_MD_CTX_md_data(ctx), data, count);
  159. }
  160. static int final384(EVP_MD_CTX *ctx, unsigned char *md)
  161. {
  162. return SHA384_Final(md, EVP_MD_CTX_md_data(ctx));
  163. }
  164. static int init512(EVP_MD_CTX *ctx)
  165. {
  166. return SHA512_Init(EVP_MD_CTX_md_data(ctx));
  167. }
  168. /* See comment in SHA224/256 section */
  169. static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
  170. {
  171. return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
  172. }
  173. static int final512(EVP_MD_CTX *ctx, unsigned char *md)
  174. {
  175. return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
  176. }
  177. static const EVP_MD sha512_224_md = {
  178. NID_sha512_224,
  179. NID_sha512_224WithRSAEncryption,
  180. SHA224_DIGEST_LENGTH,
  181. EVP_MD_FLAG_DIGALGID_ABSENT,
  182. init512_224,
  183. update512,
  184. final512,
  185. NULL,
  186. NULL,
  187. SHA512_CBLOCK,
  188. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  189. };
  190. const EVP_MD *EVP_sha512_224(void)
  191. {
  192. return &sha512_224_md;
  193. }
  194. static const EVP_MD sha512_256_md = {
  195. NID_sha512_256,
  196. NID_sha512_256WithRSAEncryption,
  197. SHA256_DIGEST_LENGTH,
  198. EVP_MD_FLAG_DIGALGID_ABSENT,
  199. init512_256,
  200. update512,
  201. final512,
  202. NULL,
  203. NULL,
  204. SHA512_CBLOCK,
  205. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  206. };
  207. const EVP_MD *EVP_sha512_256(void)
  208. {
  209. return &sha512_256_md;
  210. }
  211. static const EVP_MD sha384_md = {
  212. NID_sha384,
  213. NID_sha384WithRSAEncryption,
  214. SHA384_DIGEST_LENGTH,
  215. EVP_MD_FLAG_DIGALGID_ABSENT,
  216. init384,
  217. update384,
  218. final384,
  219. NULL,
  220. NULL,
  221. SHA512_CBLOCK,
  222. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  223. };
  224. const EVP_MD *EVP_sha384(void)
  225. {
  226. return &sha384_md;
  227. }
  228. static const EVP_MD sha512_md = {
  229. NID_sha512,
  230. NID_sha512WithRSAEncryption,
  231. SHA512_DIGEST_LENGTH,
  232. EVP_MD_FLAG_DIGALGID_ABSENT,
  233. init512,
  234. update512,
  235. final512,
  236. NULL,
  237. NULL,
  238. SHA512_CBLOCK,
  239. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  240. };
  241. const EVP_MD *EVP_sha512(void)
  242. {
  243. return &sha512_md;
  244. }