2
0

digest.c 8.8 KB

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