digest.c 8.9 KB

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