No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

digest.c 8.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/err.h>
  60. #include <openssl/obj.h>
  61. #include <openssl/mem.h>
  62. #include "internal.h"
  63. int EVP_MD_type(const EVP_MD *md) { return md->type; }
  64. uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
  65. size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
  66. size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; }
  67. void EVP_MD_CTX_init(EVP_MD_CTX *ctx) { memset(ctx, 0, sizeof(EVP_MD_CTX)); }
  68. EVP_MD_CTX *EVP_MD_CTX_create(void) {
  69. EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX));
  70. if (ctx) {
  71. EVP_MD_CTX_init(ctx);
  72. }
  73. return ctx;
  74. }
  75. int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
  76. if (ctx->digest && ctx->digest->ctx_size && ctx->md_data) {
  77. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  78. OPENSSL_free(ctx->md_data);
  79. }
  80. assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
  81. if (ctx->pctx_ops) {
  82. ctx->pctx_ops->free(ctx->pctx);
  83. }
  84. EVP_MD_CTX_init(ctx);
  85. return 1;
  86. }
  87. void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) {
  88. if (!ctx) {
  89. return;
  90. }
  91. EVP_MD_CTX_cleanup(ctx);
  92. OPENSSL_free(ctx);
  93. }
  94. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
  95. uint8_t *tmp_buf = NULL;
  96. if (in == NULL || in->digest == NULL) {
  97. OPENSSL_PUT_ERROR(DIGEST, EVP_MD_CTX_copy_ex,
  98. DIGEST_R_INPUT_NOT_INITIALIZED);
  99. return 0;
  100. }
  101. if (out->digest == in->digest) {
  102. /* |md_data| will be the correct size in this case so it's removed from
  103. * |out| at this point so that |EVP_MD_CTX_cleanup| doesn't free it and
  104. * then it's reused. */
  105. tmp_buf = out->md_data;
  106. out->md_data = NULL;
  107. }
  108. EVP_MD_CTX_cleanup(out);
  109. memcpy(out, in, sizeof(EVP_MD_CTX));
  110. if (in->md_data && in->digest->ctx_size) {
  111. if (tmp_buf) {
  112. out->md_data = tmp_buf;
  113. } else {
  114. out->md_data = OPENSSL_malloc(in->digest->ctx_size);
  115. if (!out->md_data) {
  116. OPENSSL_PUT_ERROR(DIGEST, EVP_MD_CTX_copy_ex, ERR_R_MALLOC_FAILURE);
  117. return 0;
  118. }
  119. }
  120. memcpy(out->md_data, in->md_data, in->digest->ctx_size);
  121. }
  122. assert(in->pctx == NULL || in->pctx_ops != NULL);
  123. if (in->pctx && in->pctx_ops) {
  124. out->pctx = in->pctx_ops->dup(in->pctx);
  125. if (!out->pctx) {
  126. EVP_MD_CTX_cleanup(out);
  127. return 0;
  128. }
  129. }
  130. return 1;
  131. }
  132. int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
  133. EVP_MD_CTX_init(out);
  134. return EVP_MD_CTX_copy_ex(out, in);
  135. }
  136. int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) {
  137. if (ctx->digest != type) {
  138. if (ctx->digest && ctx->digest->ctx_size) {
  139. OPENSSL_free(ctx->md_data);
  140. }
  141. ctx->digest = type;
  142. if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
  143. ctx->update = type->update;
  144. ctx->md_data = OPENSSL_malloc(type->ctx_size);
  145. if (ctx->md_data == NULL) {
  146. OPENSSL_PUT_ERROR(DIGEST, EVP_DigestInit_ex, ERR_R_MALLOC_FAILURE);
  147. return 0;
  148. }
  149. }
  150. }
  151. assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
  152. if (ctx->pctx_ops) {
  153. if (!ctx->pctx_ops->begin_digest(ctx)) {
  154. return 0;
  155. }
  156. }
  157. if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) {
  158. return 1;
  159. }
  160. return ctx->digest->init(ctx);
  161. }
  162. int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) {
  163. EVP_MD_CTX_init(ctx);
  164. return EVP_DigestInit_ex(ctx, type, NULL);
  165. }
  166. int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) {
  167. return ctx->update(ctx, data, len);
  168. }
  169. int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) {
  170. int ret;
  171. assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
  172. ret = ctx->digest->final(ctx, md_out);
  173. if (size != NULL) {
  174. *size = ctx->digest->md_size;
  175. }
  176. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  177. return ret;
  178. }
  179. int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) {
  180. int ret = EVP_DigestFinal_ex(ctx, md, size);
  181. EVP_MD_CTX_cleanup(ctx);
  182. return ret;
  183. }
  184. int EVP_Digest(const void *data, size_t count, uint8_t *out_md,
  185. unsigned int *out_size, const EVP_MD *type, ENGINE *impl) {
  186. EVP_MD_CTX ctx;
  187. int ret;
  188. EVP_MD_CTX_init(&ctx);
  189. ret = EVP_DigestInit_ex(&ctx, type, impl) &&
  190. EVP_DigestUpdate(&ctx, data, count) &&
  191. EVP_DigestFinal_ex(&ctx, out_md, out_size);
  192. EVP_MD_CTX_cleanup(&ctx);
  193. return ret;
  194. }
  195. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) {
  196. if (ctx == NULL) {
  197. return NULL;
  198. }
  199. return ctx->digest;
  200. }
  201. unsigned EVP_MD_CTX_size(const EVP_MD_CTX *ctx) {
  202. return EVP_MD_size(EVP_MD_CTX_md(ctx));
  203. }
  204. unsigned EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) {
  205. return EVP_MD_block_size(EVP_MD_CTX_md(ctx));
  206. }
  207. int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) {
  208. return EVP_MD_type(EVP_MD_CTX_md(ctx));
  209. }
  210. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, uint32_t flags) {
  211. ctx->flags |= flags;
  212. }
  213. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, uint32_t flags) {
  214. ctx->flags &= ~flags;
  215. }
  216. uint32_t EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, uint32_t flags) {
  217. return ctx->flags & flags;
  218. }
  219. int EVP_add_digest(const EVP_MD *digest) {
  220. return 1;
  221. }