m_sha1.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 1995-2016 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 "internal/evp_int.h"
  16. static int init(EVP_MD_CTX *ctx)
  17. {
  18. return SHA1_Init(EVP_MD_CTX_md_data(ctx));
  19. }
  20. static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
  21. {
  22. return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
  23. }
  24. static int final(EVP_MD_CTX *ctx, unsigned char *md)
  25. {
  26. return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
  27. }
  28. static int ctrl(EVP_MD_CTX *ctx, int cmd, int mslen, void *ms)
  29. {
  30. unsigned char padtmp[40];
  31. unsigned char sha1tmp[SHA_DIGEST_LENGTH];
  32. SHA_CTX *sha1;
  33. if (cmd != EVP_CTRL_SSL3_MASTER_SECRET)
  34. return -2;
  35. if (ctx == NULL)
  36. return 0;
  37. sha1 = EVP_MD_CTX_md_data(ctx);
  38. /* SSLv3 client auth handling: see RFC-6101 5.6.8 */
  39. if (mslen != 48)
  40. return 0;
  41. /* At this point hash contains all handshake messages, update
  42. * with master secret and pad_1.
  43. */
  44. if (SHA1_Update(sha1, ms, mslen) <= 0)
  45. return 0;
  46. /* Set padtmp to pad_1 value */
  47. memset(padtmp, 0x36, sizeof(padtmp));
  48. if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
  49. return 0;
  50. if (!SHA1_Final(sha1tmp, sha1))
  51. return 0;
  52. /* Reinitialise context */
  53. if (!SHA1_Init(sha1))
  54. return 0;
  55. if (SHA1_Update(sha1, ms, mslen) <= 0)
  56. return 0;
  57. /* Set padtmp to pad_2 value */
  58. memset(padtmp, 0x5c, sizeof(padtmp));
  59. if (!SHA1_Update(sha1, padtmp, sizeof(padtmp)))
  60. return 0;
  61. if (!SHA1_Update(sha1, sha1tmp, sizeof(sha1tmp)))
  62. return 0;
  63. /* Now when ctx is finalised it will return the SSL v3 hash value */
  64. OPENSSL_cleanse(sha1tmp, sizeof(sha1tmp));
  65. return 1;
  66. }
  67. static const EVP_MD sha1_md = {
  68. NID_sha1,
  69. NID_sha1WithRSAEncryption,
  70. SHA_DIGEST_LENGTH,
  71. EVP_MD_FLAG_DIGALGID_ABSENT,
  72. init,
  73. update,
  74. final,
  75. NULL,
  76. NULL,
  77. SHA_CBLOCK,
  78. sizeof(EVP_MD *) + sizeof(SHA_CTX),
  79. ctrl
  80. };
  81. const EVP_MD *EVP_sha1(void)
  82. {
  83. return (&sha1_md);
  84. }
  85. static int init224(EVP_MD_CTX *ctx)
  86. {
  87. return SHA224_Init(EVP_MD_CTX_md_data(ctx));
  88. }
  89. static int init256(EVP_MD_CTX *ctx)
  90. {
  91. return SHA256_Init(EVP_MD_CTX_md_data(ctx));
  92. }
  93. /*
  94. * Even though there're separate SHA224_[Update|Final], we call
  95. * SHA256 functions even in SHA224 context. This is what happens
  96. * there anyway, so we can spare few CPU cycles:-)
  97. */
  98. static int update256(EVP_MD_CTX *ctx, const void *data, size_t count)
  99. {
  100. return SHA256_Update(EVP_MD_CTX_md_data(ctx), data, count);
  101. }
  102. static int final256(EVP_MD_CTX *ctx, unsigned char *md)
  103. {
  104. return SHA256_Final(md, EVP_MD_CTX_md_data(ctx));
  105. }
  106. static const EVP_MD sha224_md = {
  107. NID_sha224,
  108. NID_sha224WithRSAEncryption,
  109. SHA224_DIGEST_LENGTH,
  110. EVP_MD_FLAG_DIGALGID_ABSENT,
  111. init224,
  112. update256,
  113. final256,
  114. NULL,
  115. NULL,
  116. SHA256_CBLOCK,
  117. sizeof(EVP_MD *) + sizeof(SHA256_CTX),
  118. };
  119. const EVP_MD *EVP_sha224(void)
  120. {
  121. return (&sha224_md);
  122. }
  123. static const EVP_MD sha256_md = {
  124. NID_sha256,
  125. NID_sha256WithRSAEncryption,
  126. SHA256_DIGEST_LENGTH,
  127. EVP_MD_FLAG_DIGALGID_ABSENT,
  128. init256,
  129. update256,
  130. final256,
  131. NULL,
  132. NULL,
  133. SHA256_CBLOCK,
  134. sizeof(EVP_MD *) + sizeof(SHA256_CTX),
  135. };
  136. const EVP_MD *EVP_sha256(void)
  137. {
  138. return (&sha256_md);
  139. }
  140. static int init384(EVP_MD_CTX *ctx)
  141. {
  142. return SHA384_Init(EVP_MD_CTX_md_data(ctx));
  143. }
  144. static int init512(EVP_MD_CTX *ctx)
  145. {
  146. return SHA512_Init(EVP_MD_CTX_md_data(ctx));
  147. }
  148. /* See comment in SHA224/256 section */
  149. static int update512(EVP_MD_CTX *ctx, const void *data, size_t count)
  150. {
  151. return SHA512_Update(EVP_MD_CTX_md_data(ctx), data, count);
  152. }
  153. static int final512(EVP_MD_CTX *ctx, unsigned char *md)
  154. {
  155. return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
  156. }
  157. static const EVP_MD sha384_md = {
  158. NID_sha384,
  159. NID_sha384WithRSAEncryption,
  160. SHA384_DIGEST_LENGTH,
  161. EVP_MD_FLAG_DIGALGID_ABSENT,
  162. init384,
  163. update512,
  164. final512,
  165. NULL,
  166. NULL,
  167. SHA512_CBLOCK,
  168. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  169. };
  170. const EVP_MD *EVP_sha384(void)
  171. {
  172. return (&sha384_md);
  173. }
  174. static const EVP_MD sha512_md = {
  175. NID_sha512,
  176. NID_sha512WithRSAEncryption,
  177. SHA512_DIGEST_LENGTH,
  178. EVP_MD_FLAG_DIGALGID_ABSENT,
  179. init512,
  180. update512,
  181. final512,
  182. NULL,
  183. NULL,
  184. SHA512_CBLOCK,
  185. sizeof(EVP_MD *) + sizeof(SHA512_CTX),
  186. };
  187. const EVP_MD *EVP_sha512(void)
  188. {
  189. return (&sha512_md);
  190. }