digest.c 31 KB

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