digest.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/objects.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/engine.h>
  14. #include "internal/evp_int.h"
  15. #include "evp_locl.h"
  16. /* This call frees resources associated with the context */
  17. int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
  18. {
  19. if (ctx == NULL)
  20. return 1;
  21. /*
  22. * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
  23. * sometimes only copies of the context are ever finalised.
  24. */
  25. if (ctx->digest && ctx->digest->cleanup
  26. && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
  27. ctx->digest->cleanup(ctx);
  28. if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
  29. && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
  30. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  31. }
  32. EVP_PKEY_CTX_free(ctx->pctx);
  33. #ifndef OPENSSL_NO_ENGINE
  34. ENGINE_finish(ctx->engine);
  35. #endif
  36. OPENSSL_cleanse(ctx, sizeof(*ctx));
  37. return 1;
  38. }
  39. EVP_MD_CTX *EVP_MD_CTX_new(void)
  40. {
  41. return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  42. }
  43. void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  44. {
  45. EVP_MD_CTX_reset(ctx);
  46. OPENSSL_free(ctx);
  47. }
  48. int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
  49. {
  50. EVP_MD_CTX_reset(ctx);
  51. return EVP_DigestInit_ex(ctx, type, NULL);
  52. }
  53. int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
  54. {
  55. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  56. #ifndef OPENSSL_NO_ENGINE
  57. /*
  58. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  59. * this context may already have an ENGINE! Try to avoid releasing the
  60. * previous handle, re-querying for an ENGINE, and having a
  61. * reinitialisation, when it may all be unnecessary.
  62. */
  63. if (ctx->engine && ctx->digest &&
  64. (type == NULL || (type->type == ctx->digest->type)))
  65. goto skip_to_init;
  66. if (type) {
  67. /*
  68. * Ensure an ENGINE left lying around from last time is cleared (the
  69. * previous check attempted to avoid this if the same ENGINE and
  70. * EVP_MD could be used).
  71. */
  72. ENGINE_finish(ctx->engine);
  73. if (impl != NULL) {
  74. if (!ENGINE_init(impl)) {
  75. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  76. return 0;
  77. }
  78. } else {
  79. /* Ask if an ENGINE is reserved for this job */
  80. impl = ENGINE_get_digest_engine(type->type);
  81. }
  82. if (impl != NULL) {
  83. /* There's an ENGINE for this job ... (apparently) */
  84. const EVP_MD *d = ENGINE_get_digest(impl, type->type);
  85. if (d == NULL) {
  86. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  87. ENGINE_finish(impl);
  88. return 0;
  89. }
  90. /* We'll use the ENGINE's private digest definition */
  91. type = d;
  92. /*
  93. * Store the ENGINE functional reference so we know 'type' came
  94. * from an ENGINE and we need to release it when done.
  95. */
  96. ctx->engine = impl;
  97. } else
  98. ctx->engine = NULL;
  99. } else {
  100. if (!ctx->digest) {
  101. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
  102. return 0;
  103. }
  104. type = ctx->digest;
  105. }
  106. #endif
  107. if (ctx->digest != type) {
  108. if (ctx->digest && ctx->digest->ctx_size) {
  109. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  110. ctx->md_data = NULL;
  111. }
  112. ctx->digest = type;
  113. if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
  114. ctx->update = type->update;
  115. ctx->md_data = OPENSSL_zalloc(type->ctx_size);
  116. if (ctx->md_data == NULL) {
  117. EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
  118. return 0;
  119. }
  120. }
  121. }
  122. #ifndef OPENSSL_NO_ENGINE
  123. skip_to_init:
  124. #endif
  125. if (ctx->pctx) {
  126. int r;
  127. r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
  128. EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
  129. if (r <= 0 && (r != -2))
  130. return 0;
  131. }
  132. if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
  133. return 1;
  134. return ctx->digest->init(ctx);
  135. }
  136. int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
  137. {
  138. return ctx->update(ctx, data, count);
  139. }
  140. /* The caller can assume that this removes any secret data from the context */
  141. int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
  142. {
  143. int ret;
  144. ret = EVP_DigestFinal_ex(ctx, md, size);
  145. EVP_MD_CTX_reset(ctx);
  146. return ret;
  147. }
  148. /* The caller can assume that this removes any secret data from the context */
  149. int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
  150. {
  151. int ret;
  152. OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
  153. ret = ctx->digest->final(ctx, md);
  154. if (size != NULL)
  155. *size = ctx->digest->md_size;
  156. if (ctx->digest->cleanup) {
  157. ctx->digest->cleanup(ctx);
  158. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  159. }
  160. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  161. return ret;
  162. }
  163. int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  164. {
  165. EVP_MD_CTX_reset(out);
  166. return EVP_MD_CTX_copy_ex(out, in);
  167. }
  168. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  169. {
  170. unsigned char *tmp_buf;
  171. if ((in == NULL) || (in->digest == NULL)) {
  172. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
  173. return 0;
  174. }
  175. #ifndef OPENSSL_NO_ENGINE
  176. /* Make sure it's safe to copy a digest context using an ENGINE */
  177. if (in->engine && !ENGINE_init(in->engine)) {
  178. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
  179. return 0;
  180. }
  181. #endif
  182. if (out->digest == in->digest) {
  183. tmp_buf = out->md_data;
  184. EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
  185. } else
  186. tmp_buf = NULL;
  187. EVP_MD_CTX_reset(out);
  188. memcpy(out, in, sizeof(*out));
  189. /* Null these variables, since they are getting fixed up
  190. * properly below. Anything else may cause a memleak and/or
  191. * double free if any of the memory allocations below fail
  192. */
  193. out->md_data = NULL;
  194. out->pctx = NULL;
  195. if (in->md_data && out->digest->ctx_size) {
  196. if (tmp_buf)
  197. out->md_data = tmp_buf;
  198. else {
  199. out->md_data = OPENSSL_malloc(out->digest->ctx_size);
  200. if (out->md_data == NULL) {
  201. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
  202. return 0;
  203. }
  204. }
  205. memcpy(out->md_data, in->md_data, out->digest->ctx_size);
  206. }
  207. out->update = in->update;
  208. if (in->pctx) {
  209. out->pctx = EVP_PKEY_CTX_dup(in->pctx);
  210. if (!out->pctx) {
  211. EVP_MD_CTX_reset(out);
  212. return 0;
  213. }
  214. }
  215. if (out->digest->copy)
  216. return out->digest->copy(out, in);
  217. return 1;
  218. }
  219. int EVP_Digest(const void *data, size_t count,
  220. unsigned char *md, unsigned int *size, const EVP_MD *type,
  221. ENGINE *impl)
  222. {
  223. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  224. int ret;
  225. if (ctx == NULL)
  226. return 0;
  227. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
  228. ret = EVP_DigestInit_ex(ctx, type, impl)
  229. && EVP_DigestUpdate(ctx, data, count)
  230. && EVP_DigestFinal_ex(ctx, md, size);
  231. EVP_MD_CTX_free(ctx);
  232. return ret;
  233. }
  234. int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  235. {
  236. if (ctx->digest && ctx->digest->md_ctrl) {
  237. int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
  238. if (ret <= 0)
  239. return 0;
  240. return 1;
  241. }
  242. return 0;
  243. }