digest.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/ec.h>
  15. #include <openssl/engine.h>
  16. #include <openssl/params.h>
  17. #include <openssl/core_names.h>
  18. #include "internal/cryptlib.h"
  19. #include "crypto/evp.h"
  20. #include "internal/provider.h"
  21. #include "evp_local.h"
  22. void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force)
  23. {
  24. EVP_MD_free(ctx->fetched_digest);
  25. ctx->fetched_digest = NULL;
  26. ctx->reqdigest = NULL;
  27. if (ctx->provctx != NULL) {
  28. if (ctx->digest->freectx != NULL)
  29. ctx->digest->freectx(ctx->provctx);
  30. ctx->provctx = NULL;
  31. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  32. }
  33. /* TODO(3.0): Remove legacy code below */
  34. /*
  35. * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
  36. * sometimes only copies of the context are ever finalised.
  37. */
  38. if (ctx->digest && ctx->digest->cleanup
  39. && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
  40. ctx->digest->cleanup(ctx);
  41. if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
  42. && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE) || force))
  43. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  44. if (force)
  45. ctx->digest = NULL;
  46. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  47. ENGINE_finish(ctx->engine);
  48. ctx->engine = NULL;
  49. #endif
  50. }
  51. /* This call frees resources associated with the context */
  52. int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
  53. {
  54. if (ctx == NULL)
  55. return 1;
  56. #ifndef FIPS_MODULE
  57. /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
  58. /*
  59. * pctx should be freed by the user of EVP_MD_CTX
  60. * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
  61. */
  62. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
  63. EVP_PKEY_CTX_free(ctx->pctx);
  64. ctx->pctx = NULL;
  65. }
  66. #endif
  67. evp_md_ctx_clear_digest(ctx, 0);
  68. OPENSSL_cleanse(ctx, sizeof(*ctx));
  69. return 1;
  70. }
  71. #ifndef FIPS_MODULE
  72. EVP_MD_CTX *evp_md_ctx_new_with_libctx(EVP_PKEY *pkey,
  73. const ASN1_OCTET_STRING *id,
  74. OPENSSL_CTX *libctx, const char *propq)
  75. {
  76. EVP_MD_CTX *ctx;
  77. EVP_PKEY_CTX *pctx = NULL;
  78. if ((ctx = EVP_MD_CTX_new()) == NULL
  79. || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) {
  80. ASN1err(0, ERR_R_MALLOC_FAILURE);
  81. goto err;
  82. }
  83. # ifndef OPENSSL_NO_EC
  84. if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0) {
  85. ASN1err(0, ERR_R_MALLOC_FAILURE);
  86. goto err;
  87. }
  88. # endif
  89. EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
  90. return ctx;
  91. err:
  92. EVP_PKEY_CTX_free(pctx);
  93. EVP_MD_CTX_free(ctx);
  94. return NULL;
  95. }
  96. #endif
  97. EVP_MD_CTX *EVP_MD_CTX_new(void)
  98. {
  99. return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  100. }
  101. void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  102. {
  103. if (ctx == NULL)
  104. return;
  105. EVP_MD_CTX_reset(ctx);
  106. OPENSSL_free(ctx);
  107. return;
  108. }
  109. int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
  110. {
  111. EVP_MD_CTX_reset(ctx);
  112. return EVP_DigestInit_ex(ctx, type, NULL);
  113. }
  114. int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
  115. {
  116. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  117. ENGINE *tmpimpl = NULL;
  118. #endif
  119. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  120. if (ctx->provctx != NULL) {
  121. if (!ossl_assert(ctx->digest != NULL)) {
  122. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  123. return 0;
  124. }
  125. if (ctx->digest->freectx != NULL)
  126. ctx->digest->freectx(ctx->provctx);
  127. ctx->provctx = NULL;
  128. }
  129. if (type != NULL)
  130. ctx->reqdigest = type;
  131. /* TODO(3.0): Legacy work around code below. Remove this */
  132. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  133. /*
  134. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  135. * this context may already have an ENGINE! Try to avoid releasing the
  136. * previous handle, re-querying for an ENGINE, and having a
  137. * reinitialisation, when it may all be unnecessary.
  138. */
  139. if (ctx->engine && ctx->digest &&
  140. (type == NULL || (type->type == ctx->digest->type)))
  141. goto skip_to_init;
  142. if (type != NULL) {
  143. /*
  144. * Ensure an ENGINE left lying around from last time is cleared (the
  145. * previous check attempted to avoid this if the same ENGINE and
  146. * EVP_MD could be used).
  147. */
  148. ENGINE_finish(ctx->engine);
  149. ctx->engine = NULL;
  150. }
  151. if (type != NULL && impl == NULL)
  152. tmpimpl = ENGINE_get_digest_engine(type->type);
  153. #endif
  154. /*
  155. * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
  156. * should use legacy handling for now.
  157. */
  158. if (ctx->engine != NULL
  159. || impl != NULL
  160. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  161. || tmpimpl != NULL
  162. #endif
  163. || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
  164. if (ctx->digest == ctx->fetched_digest)
  165. ctx->digest = NULL;
  166. EVP_MD_free(ctx->fetched_digest);
  167. ctx->fetched_digest = NULL;
  168. goto legacy;
  169. }
  170. if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
  171. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  172. ctx->md_data = NULL;
  173. }
  174. /* TODO(3.0): Start of non-legacy code below */
  175. if (type->prov == NULL) {
  176. #ifdef FIPS_MODULE
  177. /* We only do explicit fetches inside the FIPS module */
  178. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  179. return 0;
  180. #else
  181. EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
  182. if (provmd == NULL)
  183. return 0;
  184. type = provmd;
  185. EVP_MD_free(ctx->fetched_digest);
  186. ctx->fetched_digest = provmd;
  187. #endif
  188. }
  189. if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
  190. if (ctx->digest->freectx != NULL)
  191. ctx->digest->freectx(ctx->provctx);
  192. ctx->provctx = NULL;
  193. }
  194. ctx->digest = type;
  195. if (ctx->provctx == NULL) {
  196. ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
  197. if (ctx->provctx == NULL) {
  198. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  199. return 0;
  200. }
  201. }
  202. if (ctx->digest->dinit == NULL) {
  203. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  204. return 0;
  205. }
  206. return ctx->digest->dinit(ctx->provctx);
  207. /* TODO(3.0): Remove legacy code below */
  208. legacy:
  209. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  210. if (type) {
  211. if (impl != NULL) {
  212. if (!ENGINE_init(impl)) {
  213. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  214. return 0;
  215. }
  216. } else {
  217. /* Ask if an ENGINE is reserved for this job */
  218. impl = tmpimpl;
  219. }
  220. if (impl != NULL) {
  221. /* There's an ENGINE for this job ... (apparently) */
  222. const EVP_MD *d = ENGINE_get_digest(impl, type->type);
  223. if (d == NULL) {
  224. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  225. ENGINE_finish(impl);
  226. return 0;
  227. }
  228. /* We'll use the ENGINE's private digest definition */
  229. type = d;
  230. /*
  231. * Store the ENGINE functional reference so we know 'type' came
  232. * from an ENGINE and we need to release it when done.
  233. */
  234. ctx->engine = impl;
  235. } else
  236. ctx->engine = NULL;
  237. } else {
  238. if (!ctx->digest) {
  239. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
  240. return 0;
  241. }
  242. type = ctx->digest;
  243. }
  244. #endif
  245. if (ctx->digest != type) {
  246. if (ctx->digest && ctx->digest->ctx_size) {
  247. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  248. ctx->md_data = NULL;
  249. }
  250. ctx->digest = type;
  251. if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
  252. ctx->update = type->update;
  253. ctx->md_data = OPENSSL_zalloc(type->ctx_size);
  254. if (ctx->md_data == NULL) {
  255. EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
  256. return 0;
  257. }
  258. }
  259. }
  260. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  261. skip_to_init:
  262. #endif
  263. #ifndef FIPS_MODULE
  264. /*
  265. * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
  266. * or when using providers.
  267. */
  268. if (ctx->pctx != NULL
  269. && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
  270. || ctx->pctx->op.sig.signature == NULL)) {
  271. int r;
  272. r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
  273. EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
  274. if (r <= 0 && (r != -2))
  275. return 0;
  276. }
  277. #endif
  278. if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
  279. return 1;
  280. return ctx->digest->init(ctx);
  281. }
  282. int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
  283. {
  284. if (count == 0)
  285. return 1;
  286. if (ctx->pctx != NULL
  287. && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
  288. && ctx->pctx->op.sig.sigprovctx != NULL) {
  289. /*
  290. * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
  291. * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
  292. * Some code calls EVP_DigestUpdate() directly even when initialised
  293. * with EVP_DigestSignInit_with_libctx() or
  294. * EVP_DigestVerifyInit_with_libctx(), so we detect that and redirect to
  295. * the correct EVP_Digest*Update() function
  296. */
  297. if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
  298. return EVP_DigestSignUpdate(ctx, data, count);
  299. if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
  300. return EVP_DigestVerifyUpdate(ctx, data, count);
  301. EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
  302. return 0;
  303. }
  304. if (ctx->digest == NULL
  305. || ctx->digest->prov == NULL
  306. || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
  307. goto legacy;
  308. if (ctx->digest->dupdate == NULL) {
  309. EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
  310. return 0;
  311. }
  312. return ctx->digest->dupdate(ctx->provctx, data, count);
  313. /* TODO(3.0): Remove legacy code below */
  314. legacy:
  315. return ctx->update(ctx, data, count);
  316. }
  317. /* The caller can assume that this removes any secret data from the context */
  318. int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
  319. {
  320. int ret;
  321. ret = EVP_DigestFinal_ex(ctx, md, size);
  322. EVP_MD_CTX_reset(ctx);
  323. return ret;
  324. }
  325. /* The caller can assume that this removes any secret data from the context */
  326. int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
  327. {
  328. int ret, sz;
  329. size_t size = 0;
  330. size_t mdsize = 0;
  331. if (ctx->digest == NULL)
  332. return 0;
  333. sz = EVP_MD_size(ctx->digest);
  334. if (sz < 0)
  335. return 0;
  336. mdsize = sz;
  337. if (ctx->digest->prov == NULL)
  338. goto legacy;
  339. if (ctx->digest->dfinal == NULL) {
  340. EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
  341. return 0;
  342. }
  343. ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
  344. if (isize != NULL) {
  345. if (size <= UINT_MAX) {
  346. *isize = (int)size;
  347. } else {
  348. EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
  349. ret = 0;
  350. }
  351. }
  352. return ret;
  353. /* TODO(3.0): Remove legacy code below */
  354. legacy:
  355. OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
  356. ret = ctx->digest->final(ctx, md);
  357. if (isize != NULL)
  358. *isize = mdsize;
  359. if (ctx->digest->cleanup) {
  360. ctx->digest->cleanup(ctx);
  361. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  362. }
  363. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  364. return ret;
  365. }
  366. int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
  367. {
  368. int ret = 0;
  369. OSSL_PARAM params[2];
  370. size_t i = 0;
  371. if (ctx->digest == NULL || ctx->digest->prov == NULL)
  372. goto legacy;
  373. if (ctx->digest->dfinal == NULL) {
  374. EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
  375. return 0;
  376. }
  377. params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
  378. params[i++] = OSSL_PARAM_construct_end();
  379. if (EVP_MD_CTX_set_params(ctx, params) > 0)
  380. ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
  381. EVP_MD_CTX_reset(ctx);
  382. return ret;
  383. legacy:
  384. if (ctx->digest->flags & EVP_MD_FLAG_XOF
  385. && size <= INT_MAX
  386. && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
  387. ret = ctx->digest->final(ctx, md);
  388. if (ctx->digest->cleanup != NULL) {
  389. ctx->digest->cleanup(ctx);
  390. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  391. }
  392. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  393. } else {
  394. EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
  395. }
  396. return ret;
  397. }
  398. int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  399. {
  400. EVP_MD_CTX_reset(out);
  401. return EVP_MD_CTX_copy_ex(out, in);
  402. }
  403. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  404. {
  405. unsigned char *tmp_buf;
  406. if (in == NULL || in->digest == NULL) {
  407. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
  408. return 0;
  409. }
  410. if (in->digest->prov == NULL
  411. || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
  412. goto legacy;
  413. if (in->digest->dupctx == NULL) {
  414. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
  415. return 0;
  416. }
  417. EVP_MD_CTX_reset(out);
  418. if (out->fetched_digest != NULL)
  419. EVP_MD_free(out->fetched_digest);
  420. *out = *in;
  421. /* NULL out pointers in case of error */
  422. out->pctx = NULL;
  423. out->provctx = NULL;
  424. if (in->fetched_digest != NULL)
  425. EVP_MD_up_ref(in->fetched_digest);
  426. if (in->provctx != NULL) {
  427. out->provctx = in->digest->dupctx(in->provctx);
  428. if (out->provctx == NULL) {
  429. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
  430. return 0;
  431. }
  432. }
  433. /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
  434. EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  435. #ifndef FIPS_MODULE
  436. /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
  437. if (in->pctx != NULL) {
  438. out->pctx = EVP_PKEY_CTX_dup(in->pctx);
  439. if (out->pctx == NULL) {
  440. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
  441. EVP_MD_CTX_reset(out);
  442. return 0;
  443. }
  444. }
  445. #endif
  446. return 1;
  447. /* TODO(3.0): Remove legacy code below */
  448. legacy:
  449. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  450. /* Make sure it's safe to copy a digest context using an ENGINE */
  451. if (in->engine && !ENGINE_init(in->engine)) {
  452. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
  453. return 0;
  454. }
  455. #endif
  456. if (out->digest == in->digest) {
  457. tmp_buf = out->md_data;
  458. EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
  459. } else
  460. tmp_buf = NULL;
  461. EVP_MD_CTX_reset(out);
  462. memcpy(out, in, sizeof(*out));
  463. /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
  464. EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  465. /* Null these variables, since they are getting fixed up
  466. * properly below. Anything else may cause a memleak and/or
  467. * double free if any of the memory allocations below fail
  468. */
  469. out->md_data = NULL;
  470. out->pctx = NULL;
  471. if (in->md_data && out->digest->ctx_size) {
  472. if (tmp_buf)
  473. out->md_data = tmp_buf;
  474. else {
  475. out->md_data = OPENSSL_malloc(out->digest->ctx_size);
  476. if (out->md_data == NULL) {
  477. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
  478. return 0;
  479. }
  480. }
  481. memcpy(out->md_data, in->md_data, out->digest->ctx_size);
  482. }
  483. out->update = in->update;
  484. #ifndef FIPS_MODULE
  485. /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
  486. if (in->pctx) {
  487. out->pctx = EVP_PKEY_CTX_dup(in->pctx);
  488. if (!out->pctx) {
  489. EVP_MD_CTX_reset(out);
  490. return 0;
  491. }
  492. }
  493. #endif
  494. if (out->digest->copy)
  495. return out->digest->copy(out, in);
  496. return 1;
  497. }
  498. int EVP_Digest(const void *data, size_t count,
  499. unsigned char *md, unsigned int *size, const EVP_MD *type,
  500. ENGINE *impl)
  501. {
  502. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  503. int ret;
  504. if (ctx == NULL)
  505. return 0;
  506. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
  507. ret = EVP_DigestInit_ex(ctx, type, impl)
  508. && EVP_DigestUpdate(ctx, data, count)
  509. && EVP_DigestFinal_ex(ctx, md, size);
  510. EVP_MD_CTX_free(ctx);
  511. return ret;
  512. }
  513. int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
  514. {
  515. if (digest != NULL && digest->get_params != NULL)
  516. return digest->get_params(params);
  517. return 0;
  518. }
  519. const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
  520. {
  521. if (digest != NULL && digest->gettable_params != NULL)
  522. return digest->gettable_params(
  523. ossl_provider_ctx(EVP_MD_provider(digest)));
  524. return NULL;
  525. }
  526. int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
  527. {
  528. EVP_PKEY_CTX *pctx = ctx->pctx;
  529. /* If we have a pctx then we should try that first */
  530. if (pctx != NULL
  531. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  532. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  533. && pctx->op.sig.sigprovctx != NULL
  534. && pctx->op.sig.signature->set_ctx_md_params != NULL)
  535. return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.sigprovctx,
  536. params);
  537. if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
  538. return ctx->digest->set_ctx_params(ctx->provctx, params);
  539. return 0;
  540. }
  541. const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
  542. {
  543. if (md != NULL && md->settable_ctx_params != NULL)
  544. return md->settable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md)));
  545. return NULL;
  546. }
  547. const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
  548. {
  549. EVP_PKEY_CTX *pctx;
  550. if (ctx == NULL)
  551. return NULL;
  552. /* If we have a pctx then we should try that first */
  553. pctx = ctx->pctx;
  554. if (pctx != NULL
  555. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  556. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  557. && pctx->op.sig.sigprovctx != NULL
  558. && pctx->op.sig.signature->settable_ctx_md_params != NULL)
  559. return pctx->op.sig.signature->settable_ctx_md_params(
  560. pctx->op.sig.sigprovctx);
  561. if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL)
  562. return ctx->digest->settable_ctx_params(
  563. ossl_provider_ctx(EVP_MD_provider(ctx->digest)));
  564. return NULL;
  565. }
  566. int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
  567. {
  568. EVP_PKEY_CTX *pctx = ctx->pctx;
  569. /* If we have a pctx then we should try that first */
  570. if (pctx != NULL
  571. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  572. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  573. && pctx->op.sig.sigprovctx != NULL
  574. && pctx->op.sig.signature->get_ctx_md_params != NULL)
  575. return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.sigprovctx,
  576. params);
  577. if (ctx->digest != NULL && ctx->digest->get_params != NULL)
  578. return ctx->digest->get_ctx_params(ctx->provctx, params);
  579. return 0;
  580. }
  581. const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
  582. {
  583. if (md != NULL && md->gettable_ctx_params != NULL)
  584. return md->gettable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md)));
  585. return NULL;
  586. }
  587. const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
  588. {
  589. EVP_PKEY_CTX *pctx;
  590. if (ctx == NULL)
  591. return NULL;
  592. /* If we have a pctx then we should try that first */
  593. pctx = ctx->pctx;
  594. if (pctx != NULL
  595. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  596. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  597. && pctx->op.sig.sigprovctx != NULL
  598. && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
  599. return pctx->op.sig.signature->gettable_ctx_md_params(
  600. pctx->op.sig.sigprovctx);
  601. if (ctx->digest != NULL
  602. && ctx->digest->gettable_ctx_params != NULL)
  603. return ctx->digest->gettable_ctx_params(
  604. ossl_provider_ctx(EVP_MD_provider(ctx->digest)));
  605. return NULL;
  606. }
  607. /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
  608. int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  609. {
  610. int ret = EVP_CTRL_RET_UNSUPPORTED;
  611. int set_params = 1;
  612. size_t sz;
  613. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  614. if (ctx == NULL) {
  615. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  616. return 0;
  617. }
  618. if (ctx->digest != NULL && ctx->digest->prov == NULL)
  619. goto legacy;
  620. switch (cmd) {
  621. case EVP_MD_CTRL_XOF_LEN:
  622. sz = (size_t)p1;
  623. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
  624. break;
  625. case EVP_MD_CTRL_MICALG:
  626. set_params = 0;
  627. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
  628. p2, p1 ? p1 : 9999);
  629. break;
  630. case EVP_CTRL_SSL3_MASTER_SECRET:
  631. params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  632. p2, p1);
  633. break;
  634. default:
  635. goto conclude;
  636. }
  637. if (set_params)
  638. ret = EVP_MD_CTX_set_params(ctx, params);
  639. else
  640. ret = EVP_MD_CTX_get_params(ctx, params);
  641. goto conclude;
  642. /* TODO(3.0): Remove legacy code below */
  643. legacy:
  644. if (ctx->digest->md_ctrl == NULL) {
  645. ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
  646. return 0;
  647. }
  648. ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
  649. conclude:
  650. if (ret <= 0)
  651. return 0;
  652. return ret;
  653. }
  654. EVP_MD *evp_md_new(void)
  655. {
  656. EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
  657. if (md != NULL) {
  658. md->lock = CRYPTO_THREAD_lock_new();
  659. if (md->lock == NULL) {
  660. OPENSSL_free(md);
  661. return NULL;
  662. }
  663. md->refcnt = 1;
  664. }
  665. return md;
  666. }
  667. /*
  668. * FIPS module note: since internal fetches will be entirely
  669. * provider based, we know that none of its code depends on legacy
  670. * NIDs or any functionality that use them.
  671. */
  672. #ifndef FIPS_MODULE
  673. /* TODO(3.x) get rid of the need for legacy NIDs */
  674. static void set_legacy_nid(const char *name, void *vlegacy_nid)
  675. {
  676. int nid;
  677. int *legacy_nid = vlegacy_nid;
  678. /*
  679. * We use lowest level function to get the associated method, because
  680. * higher level functions such as EVP_get_digestbyname() have changed
  681. * to look at providers too.
  682. */
  683. const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  684. if (*legacy_nid == -1) /* We found a clash already */
  685. return;
  686. if (legacy_method == NULL)
  687. return;
  688. nid = EVP_MD_nid(legacy_method);
  689. if (*legacy_nid != NID_undef && *legacy_nid != nid) {
  690. *legacy_nid = -1;
  691. return;
  692. }
  693. *legacy_nid = nid;
  694. }
  695. #endif
  696. static void *evp_md_from_dispatch(int name_id,
  697. const OSSL_DISPATCH *fns,
  698. OSSL_PROVIDER *prov)
  699. {
  700. EVP_MD *md = NULL;
  701. int fncnt = 0;
  702. /* EVP_MD_fetch() will set the legacy NID if available */
  703. if ((md = evp_md_new()) == NULL) {
  704. EVPerr(0, ERR_R_MALLOC_FAILURE);
  705. return NULL;
  706. }
  707. #ifndef FIPS_MODULE
  708. /* TODO(3.x) get rid of the need for legacy NIDs */
  709. md->type = NID_undef;
  710. evp_names_do_all(prov, name_id, set_legacy_nid, &md->type);
  711. if (md->type == -1) {
  712. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  713. EVP_MD_free(md);
  714. return NULL;
  715. }
  716. #endif
  717. md->name_id = name_id;
  718. for (; fns->function_id != 0; fns++) {
  719. switch (fns->function_id) {
  720. case OSSL_FUNC_DIGEST_NEWCTX:
  721. if (md->newctx == NULL) {
  722. md->newctx = OSSL_FUNC_digest_newctx(fns);
  723. fncnt++;
  724. }
  725. break;
  726. case OSSL_FUNC_DIGEST_INIT:
  727. if (md->dinit == NULL) {
  728. md->dinit = OSSL_FUNC_digest_init(fns);
  729. fncnt++;
  730. }
  731. break;
  732. case OSSL_FUNC_DIGEST_UPDATE:
  733. if (md->dupdate == NULL) {
  734. md->dupdate = OSSL_FUNC_digest_update(fns);
  735. fncnt++;
  736. }
  737. break;
  738. case OSSL_FUNC_DIGEST_FINAL:
  739. if (md->dfinal == NULL) {
  740. md->dfinal = OSSL_FUNC_digest_final(fns);
  741. fncnt++;
  742. }
  743. break;
  744. case OSSL_FUNC_DIGEST_DIGEST:
  745. if (md->digest == NULL)
  746. md->digest = OSSL_FUNC_digest_digest(fns);
  747. /* We don't increment fnct for this as it is stand alone */
  748. break;
  749. case OSSL_FUNC_DIGEST_FREECTX:
  750. if (md->freectx == NULL) {
  751. md->freectx = OSSL_FUNC_digest_freectx(fns);
  752. fncnt++;
  753. }
  754. break;
  755. case OSSL_FUNC_DIGEST_DUPCTX:
  756. if (md->dupctx == NULL)
  757. md->dupctx = OSSL_FUNC_digest_dupctx(fns);
  758. break;
  759. case OSSL_FUNC_DIGEST_GET_PARAMS:
  760. if (md->get_params == NULL)
  761. md->get_params = OSSL_FUNC_digest_get_params(fns);
  762. break;
  763. case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
  764. if (md->set_ctx_params == NULL)
  765. md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
  766. break;
  767. case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
  768. if (md->get_ctx_params == NULL)
  769. md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
  770. break;
  771. case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
  772. if (md->gettable_params == NULL)
  773. md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
  774. break;
  775. case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
  776. if (md->settable_ctx_params == NULL)
  777. md->settable_ctx_params =
  778. OSSL_FUNC_digest_settable_ctx_params(fns);
  779. break;
  780. case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
  781. if (md->gettable_ctx_params == NULL)
  782. md->gettable_ctx_params =
  783. OSSL_FUNC_digest_gettable_ctx_params(fns);
  784. break;
  785. }
  786. }
  787. if ((fncnt != 0 && fncnt != 5)
  788. || (fncnt == 0 && md->digest == NULL)) {
  789. /*
  790. * In order to be a consistent set of functions we either need the
  791. * whole set of init/update/final etc functions or none of them.
  792. * The "digest" function can standalone. We at least need one way to
  793. * generate digests.
  794. */
  795. EVP_MD_free(md);
  796. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  797. return NULL;
  798. }
  799. md->prov = prov;
  800. if (prov != NULL)
  801. ossl_provider_up_ref(prov);
  802. return md;
  803. }
  804. static int evp_md_up_ref(void *md)
  805. {
  806. return EVP_MD_up_ref(md);
  807. }
  808. static void evp_md_free(void *md)
  809. {
  810. EVP_MD_free(md);
  811. }
  812. EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
  813. const char *properties)
  814. {
  815. EVP_MD *md =
  816. evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
  817. evp_md_from_dispatch, evp_md_up_ref, evp_md_free);
  818. return md;
  819. }
  820. int EVP_MD_up_ref(EVP_MD *md)
  821. {
  822. int ref = 0;
  823. CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
  824. return 1;
  825. }
  826. void EVP_MD_free(EVP_MD *md)
  827. {
  828. int i;
  829. if (md == NULL)
  830. return;
  831. CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
  832. if (i > 0)
  833. return;
  834. ossl_provider_free(md->prov);
  835. CRYPTO_THREAD_lock_free(md->lock);
  836. OPENSSL_free(md);
  837. }
  838. void EVP_MD_do_all_provided(OPENSSL_CTX *libctx,
  839. void (*fn)(EVP_MD *mac, void *arg),
  840. void *arg)
  841. {
  842. evp_generic_do_all(libctx, OSSL_OP_DIGEST,
  843. (void (*)(void *, void *))fn, arg,
  844. evp_md_from_dispatch, evp_md_free);
  845. }