digests.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/digest.h>
  57. #include <assert.h>
  58. #include <string.h>
  59. #include <openssl/md4.h>
  60. #include <openssl/md5.h>
  61. #include <openssl/nid.h>
  62. #include <openssl/sha.h>
  63. #include "internal.h"
  64. #include "../delocate.h"
  65. #include "../../internal.h"
  66. #if defined(NDEBUG)
  67. #define CHECK(x) (void) (x)
  68. #else
  69. #define CHECK(x) assert(x)
  70. #endif
  71. static void md4_init(EVP_MD_CTX *ctx) {
  72. CHECK(MD4_Init(ctx->md_data));
  73. }
  74. static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
  75. CHECK(MD4_Update(ctx->md_data, data, count));
  76. }
  77. static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) {
  78. CHECK(MD4_Final(out, ctx->md_data));
  79. }
  80. DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md4) {
  81. out->type = NID_md4;
  82. out->md_size = MD4_DIGEST_LENGTH;
  83. out->flags = 0;
  84. out->init = md4_init;
  85. out->update = md4_update;
  86. out->final = md4_final;
  87. out->block_size = 64;
  88. out->ctx_size = sizeof(MD4_CTX);
  89. }
  90. static void md5_init(EVP_MD_CTX *ctx) {
  91. CHECK(MD5_Init(ctx->md_data));
  92. }
  93. static void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
  94. CHECK(MD5_Update(ctx->md_data, data, count));
  95. }
  96. static void md5_final(EVP_MD_CTX *ctx, uint8_t *out) {
  97. CHECK(MD5_Final(out, ctx->md_data));
  98. }
  99. DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md5) {
  100. out->type = NID_md5;
  101. out->md_size = MD5_DIGEST_LENGTH;
  102. out->flags = 0;
  103. out->init = md5_init;
  104. out->update = md5_update;
  105. out->final = md5_final;
  106. out->block_size = 64;
  107. out->ctx_size = sizeof(MD5_CTX);
  108. }
  109. static void sha1_init(EVP_MD_CTX *ctx) {
  110. CHECK(SHA1_Init(ctx->md_data));
  111. }
  112. static void sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
  113. CHECK(SHA1_Update(ctx->md_data, data, count));
  114. }
  115. static void sha1_final(EVP_MD_CTX *ctx, uint8_t *md) {
  116. CHECK(SHA1_Final(md, ctx->md_data));
  117. }
  118. DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha1) {
  119. out->type = NID_sha1;
  120. out->md_size = SHA_DIGEST_LENGTH;
  121. out->flags = 0;
  122. out->init = sha1_init;
  123. out->update = sha1_update;
  124. out->final = sha1_final;
  125. out->block_size = 64;
  126. out->ctx_size = sizeof(SHA_CTX);
  127. }
  128. static void sha224_init(EVP_MD_CTX *ctx) {
  129. CHECK(SHA224_Init(ctx->md_data));
  130. }
  131. static void sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
  132. CHECK(SHA224_Update(ctx->md_data, data, count));
  133. }
  134. static void sha224_final(EVP_MD_CTX *ctx, uint8_t *md) {
  135. CHECK(SHA224_Final(md, ctx->md_data));
  136. }
  137. DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha224) {
  138. out->type = NID_sha224;
  139. out->md_size = SHA224_DIGEST_LENGTH;
  140. out->flags = 0;
  141. out->init = sha224_init;
  142. out->update = sha224_update;
  143. out->final = sha224_final;
  144. out->block_size = 64;
  145. out->ctx_size = sizeof(SHA256_CTX);
  146. }
  147. static void sha256_init(EVP_MD_CTX *ctx) {
  148. CHECK(SHA256_Init(ctx->md_data));
  149. }
  150. static void sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
  151. CHECK(SHA256_Update(ctx->md_data, data, count));
  152. }
  153. static void sha256_final(EVP_MD_CTX *ctx, uint8_t *md) {
  154. CHECK(SHA256_Final(md, ctx->md_data));
  155. }
  156. DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha256) {
  157. out->type = NID_sha256;
  158. out->md_size = SHA256_DIGEST_LENGTH;
  159. out->flags = 0;
  160. out->init = sha256_init;
  161. out->update = sha256_update;
  162. out->final = sha256_final;
  163. out->block_size = 64;
  164. out->ctx_size = sizeof(SHA256_CTX);
  165. }
  166. static void sha384_init(EVP_MD_CTX *ctx) {
  167. CHECK(SHA384_Init(ctx->md_data));
  168. }
  169. static void sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
  170. CHECK(SHA384_Update(ctx->md_data, data, count));
  171. }
  172. static void sha384_final(EVP_MD_CTX *ctx, uint8_t *md) {
  173. CHECK(SHA384_Final(md, ctx->md_data));
  174. }
  175. DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha384) {
  176. out->type = NID_sha384;
  177. out->md_size = SHA384_DIGEST_LENGTH;
  178. out->flags = 0;
  179. out->init = sha384_init;
  180. out->update = sha384_update;
  181. out->final = sha384_final;
  182. out->block_size = 128;
  183. out->ctx_size = sizeof(SHA512_CTX);
  184. }
  185. static void sha512_init(EVP_MD_CTX *ctx) {
  186. CHECK(SHA512_Init(ctx->md_data));
  187. }
  188. static void sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
  189. CHECK(SHA512_Update(ctx->md_data, data, count));
  190. }
  191. static void sha512_final(EVP_MD_CTX *ctx, uint8_t *md) {
  192. CHECK(SHA512_Final(md, ctx->md_data));
  193. }
  194. DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512) {
  195. out->type = NID_sha512;
  196. out->md_size = SHA512_DIGEST_LENGTH;
  197. out->flags = 0;
  198. out->init = sha512_init;
  199. out->update = sha512_update;
  200. out->final = sha512_final;
  201. out->block_size = 128;
  202. out->ctx_size = sizeof(SHA512_CTX);
  203. }
  204. typedef struct {
  205. MD5_CTX md5;
  206. SHA_CTX sha1;
  207. } MD5_SHA1_CTX;
  208. static void md5_sha1_init(EVP_MD_CTX *md_ctx) {
  209. MD5_SHA1_CTX *ctx = md_ctx->md_data;
  210. CHECK(MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1));
  211. }
  212. static void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data,
  213. size_t count) {
  214. MD5_SHA1_CTX *ctx = md_ctx->md_data;
  215. CHECK(MD5_Update(&ctx->md5, data, count) &&
  216. SHA1_Update(&ctx->sha1, data, count));
  217. }
  218. static void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) {
  219. MD5_SHA1_CTX *ctx = md_ctx->md_data;
  220. CHECK(MD5_Final(out, &ctx->md5) &&
  221. SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1));
  222. }
  223. DEFINE_METHOD_FUNCTION(EVP_MD, EVP_md5_sha1) {
  224. out->type = NID_md5_sha1;
  225. out->md_size = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
  226. out->flags = 0;
  227. out->init = md5_sha1_init;
  228. out->update = md5_sha1_update;
  229. out->final = md5_sha1_final;
  230. out->block_size = 64;
  231. out->ctx_size = sizeof(MD5_SHA1_CTX);
  232. }
  233. #undef CHECK