2
0

digest.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
  164. {
  165. int ret = 0;
  166. if (ctx->digest->flags & EVP_MD_FLAG_XOF
  167. && size <= INT_MAX
  168. && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
  169. ret = ctx->digest->final(ctx, md);
  170. if (ctx->digest->cleanup != NULL) {
  171. ctx->digest->cleanup(ctx);
  172. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  173. }
  174. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  175. } else {
  176. EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
  177. }
  178. return ret;
  179. }
  180. int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  181. {
  182. EVP_MD_CTX_reset(out);
  183. return EVP_MD_CTX_copy_ex(out, in);
  184. }
  185. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  186. {
  187. unsigned char *tmp_buf;
  188. if ((in == NULL) || (in->digest == NULL)) {
  189. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
  190. return 0;
  191. }
  192. #ifndef OPENSSL_NO_ENGINE
  193. /* Make sure it's safe to copy a digest context using an ENGINE */
  194. if (in->engine && !ENGINE_init(in->engine)) {
  195. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
  196. return 0;
  197. }
  198. #endif
  199. if (out->digest == in->digest) {
  200. tmp_buf = out->md_data;
  201. EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
  202. } else
  203. tmp_buf = NULL;
  204. EVP_MD_CTX_reset(out);
  205. memcpy(out, in, sizeof(*out));
  206. /* Null these variables, since they are getting fixed up
  207. * properly below. Anything else may cause a memleak and/or
  208. * double free if any of the memory allocations below fail
  209. */
  210. out->md_data = NULL;
  211. out->pctx = NULL;
  212. if (in->md_data && out->digest->ctx_size) {
  213. if (tmp_buf)
  214. out->md_data = tmp_buf;
  215. else {
  216. out->md_data = OPENSSL_malloc(out->digest->ctx_size);
  217. if (out->md_data == NULL) {
  218. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
  219. return 0;
  220. }
  221. }
  222. memcpy(out->md_data, in->md_data, out->digest->ctx_size);
  223. }
  224. out->update = in->update;
  225. if (in->pctx) {
  226. out->pctx = EVP_PKEY_CTX_dup(in->pctx);
  227. if (!out->pctx) {
  228. EVP_MD_CTX_reset(out);
  229. return 0;
  230. }
  231. }
  232. if (out->digest->copy)
  233. return out->digest->copy(out, in);
  234. return 1;
  235. }
  236. int EVP_Digest(const void *data, size_t count,
  237. unsigned char *md, unsigned int *size, const EVP_MD *type,
  238. ENGINE *impl)
  239. {
  240. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  241. int ret;
  242. if (ctx == NULL)
  243. return 0;
  244. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
  245. ret = EVP_DigestInit_ex(ctx, type, impl)
  246. && EVP_DigestUpdate(ctx, data, count)
  247. && EVP_DigestFinal_ex(ctx, md, size);
  248. EVP_MD_CTX_free(ctx);
  249. return ret;
  250. }
  251. int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  252. {
  253. if (ctx->digest && ctx->digest->md_ctrl) {
  254. int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
  255. if (ret <= 0)
  256. return 0;
  257. return 1;
  258. }
  259. return 0;
  260. }