digest.c 33 KB

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