digest.c 8.8 KB

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