digest.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. /*
  2. * Copyright 1995-2023 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 (sz == 0) /* Assuming a xoflen must have been set. */
  403. mdsize = SIZE_MAX;
  404. if (ctx->digest->gettable_ctx_params != NULL) {
  405. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  406. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE,
  407. &mdsize);
  408. if (!EVP_MD_CTX_get_params(ctx, params))
  409. return 0;
  410. }
  411. if (ctx->digest->dfinal == NULL) {
  412. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  413. return 0;
  414. }
  415. if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
  416. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  417. return 0;
  418. }
  419. ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
  420. ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
  421. if (isize != NULL) {
  422. if (size <= UINT_MAX) {
  423. *isize = (unsigned int)size;
  424. } else {
  425. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  426. ret = 0;
  427. }
  428. }
  429. return ret;
  430. /* Code below to be removed when legacy support is dropped. */
  431. legacy:
  432. OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
  433. ret = ctx->digest->final(ctx, md);
  434. if (isize != NULL)
  435. *isize = mdsize;
  436. if (ctx->digest->cleanup) {
  437. ctx->digest->cleanup(ctx);
  438. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  439. }
  440. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  441. return ret;
  442. }
  443. /* This is a one shot operation */
  444. int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
  445. {
  446. int ret = 0;
  447. OSSL_PARAM params[2];
  448. size_t i = 0;
  449. if (ctx->digest == NULL) {
  450. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
  451. return 0;
  452. }
  453. if (ctx->digest->prov == NULL)
  454. goto legacy;
  455. if (ctx->digest->dfinal == NULL) {
  456. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  457. return 0;
  458. }
  459. if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
  460. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  461. return 0;
  462. }
  463. /*
  464. * For backward compatibility we pass the XOFLEN via a param here so that
  465. * older providers can use the supplied value. Ideally we should have just
  466. * used the size passed into ctx->digest->dfinal().
  467. */
  468. params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
  469. params[i++] = OSSL_PARAM_construct_end();
  470. if (EVP_MD_CTX_set_params(ctx, params) >= 0)
  471. ret = ctx->digest->dfinal(ctx->algctx, md, &size, size);
  472. ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
  473. return ret;
  474. legacy:
  475. if (ctx->digest->flags & EVP_MD_FLAG_XOF
  476. && size <= INT_MAX
  477. && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
  478. ret = ctx->digest->final(ctx, md);
  479. if (ctx->digest->cleanup != NULL) {
  480. ctx->digest->cleanup(ctx);
  481. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  482. }
  483. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  484. } else {
  485. ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
  486. }
  487. return ret;
  488. }
  489. /* EVP_DigestSqueeze() can be called multiple times */
  490. int EVP_DigestSqueeze(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
  491. {
  492. if (ctx->digest == NULL) {
  493. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
  494. return 0;
  495. }
  496. if (ctx->digest->prov == NULL) {
  497. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  498. return 0;
  499. }
  500. if (ctx->digest->dsqueeze == NULL) {
  501. ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
  502. return 0;
  503. }
  504. return ctx->digest->dsqueeze(ctx->algctx, md, &size, size);
  505. }
  506. EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in)
  507. {
  508. EVP_MD_CTX *out = EVP_MD_CTX_new();
  509. if (out != NULL && !EVP_MD_CTX_copy_ex(out, in)) {
  510. EVP_MD_CTX_free(out);
  511. out = NULL;
  512. }
  513. return out;
  514. }
  515. int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  516. {
  517. EVP_MD_CTX_reset(out);
  518. return EVP_MD_CTX_copy_ex(out, in);
  519. }
  520. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  521. {
  522. int digest_change = 0;
  523. unsigned char *tmp_buf;
  524. if (in == NULL) {
  525. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  526. return 0;
  527. }
  528. if (in->digest == NULL) {
  529. /* copying uninitialized digest context */
  530. EVP_MD_CTX_reset(out);
  531. if (out->fetched_digest != NULL)
  532. EVP_MD_free(out->fetched_digest);
  533. *out = *in;
  534. goto clone_pkey;
  535. }
  536. if (in->digest->prov == NULL
  537. || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
  538. goto legacy;
  539. if (in->digest->dupctx == NULL) {
  540. ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
  541. return 0;
  542. }
  543. evp_md_ctx_reset_ex(out, 1);
  544. digest_change = (out->fetched_digest != in->fetched_digest);
  545. if (digest_change && out->fetched_digest != NULL)
  546. EVP_MD_free(out->fetched_digest);
  547. *out = *in;
  548. /* NULL out pointers in case of error */
  549. out->pctx = NULL;
  550. out->algctx = NULL;
  551. if (digest_change && in->fetched_digest != NULL)
  552. EVP_MD_up_ref(in->fetched_digest);
  553. if (in->algctx != NULL) {
  554. out->algctx = in->digest->dupctx(in->algctx);
  555. if (out->algctx == NULL) {
  556. ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
  557. return 0;
  558. }
  559. }
  560. clone_pkey:
  561. /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
  562. EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  563. #ifndef FIPS_MODULE
  564. if (in->pctx != NULL) {
  565. out->pctx = EVP_PKEY_CTX_dup(in->pctx);
  566. if (out->pctx == NULL) {
  567. ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
  568. EVP_MD_CTX_reset(out);
  569. return 0;
  570. }
  571. }
  572. #endif
  573. return 1;
  574. /* Code below to be removed when legacy support is dropped. */
  575. legacy:
  576. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  577. /* Make sure it's safe to copy a digest context using an ENGINE */
  578. if (in->engine && !ENGINE_init(in->engine)) {
  579. ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
  580. return 0;
  581. }
  582. #endif
  583. if (out->digest == in->digest) {
  584. tmp_buf = out->md_data;
  585. EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
  586. } else
  587. tmp_buf = NULL;
  588. EVP_MD_CTX_reset(out);
  589. memcpy(out, in, sizeof(*out));
  590. /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
  591. EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  592. /* Null these variables, since they are getting fixed up
  593. * properly below. Anything else may cause a memleak and/or
  594. * double free if any of the memory allocations below fail
  595. */
  596. out->md_data = NULL;
  597. out->pctx = NULL;
  598. if (in->md_data && out->digest->ctx_size) {
  599. if (tmp_buf)
  600. out->md_data = tmp_buf;
  601. else {
  602. out->md_data = OPENSSL_malloc(out->digest->ctx_size);
  603. if (out->md_data == NULL)
  604. return 0;
  605. }
  606. memcpy(out->md_data, in->md_data, out->digest->ctx_size);
  607. }
  608. out->update = in->update;
  609. #ifndef FIPS_MODULE
  610. if (in->pctx) {
  611. out->pctx = EVP_PKEY_CTX_dup(in->pctx);
  612. if (!out->pctx) {
  613. EVP_MD_CTX_reset(out);
  614. return 0;
  615. }
  616. }
  617. #endif
  618. if (out->digest->copy)
  619. return out->digest->copy(out, in);
  620. return 1;
  621. }
  622. int EVP_Digest(const void *data, size_t count,
  623. unsigned char *md, unsigned int *size, const EVP_MD *type,
  624. ENGINE *impl)
  625. {
  626. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  627. int ret;
  628. if (ctx == NULL)
  629. return 0;
  630. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
  631. ret = EVP_DigestInit_ex(ctx, type, impl)
  632. && EVP_DigestUpdate(ctx, data, count)
  633. && EVP_DigestFinal_ex(ctx, md, size);
  634. EVP_MD_CTX_free(ctx);
  635. return ret;
  636. }
  637. int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
  638. const void *data, size_t datalen,
  639. unsigned char *md, size_t *mdlen)
  640. {
  641. EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
  642. unsigned int temp = 0;
  643. int ret = 0;
  644. if (digest != NULL) {
  645. ret = EVP_Digest(data, datalen, md, &temp, digest, NULL);
  646. EVP_MD_free(digest);
  647. }
  648. if (mdlen != NULL)
  649. *mdlen = temp;
  650. return ret;
  651. }
  652. int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
  653. {
  654. if (digest != NULL && digest->get_params != NULL)
  655. return digest->get_params(params);
  656. return 0;
  657. }
  658. const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
  659. {
  660. if (digest != NULL && digest->gettable_params != NULL)
  661. return digest->gettable_params(
  662. ossl_provider_ctx(EVP_MD_get0_provider(digest)));
  663. return NULL;
  664. }
  665. int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
  666. {
  667. EVP_PKEY_CTX *pctx = ctx->pctx;
  668. /* If we have a pctx then we should try that first */
  669. if (pctx != NULL
  670. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  671. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  672. && pctx->op.sig.algctx != NULL
  673. && pctx->op.sig.signature->set_ctx_md_params != NULL)
  674. return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx,
  675. params);
  676. if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
  677. return ctx->digest->set_ctx_params(ctx->algctx, params);
  678. return 0;
  679. }
  680. const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
  681. {
  682. void *provctx;
  683. if (md != NULL && md->settable_ctx_params != NULL) {
  684. provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
  685. return md->settable_ctx_params(NULL, provctx);
  686. }
  687. return NULL;
  688. }
  689. const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
  690. {
  691. EVP_PKEY_CTX *pctx;
  692. void *alg;
  693. if (ctx == NULL)
  694. return NULL;
  695. /* If we have a pctx then we should try that first */
  696. pctx = ctx->pctx;
  697. if (pctx != NULL
  698. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  699. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  700. && pctx->op.sig.algctx != NULL
  701. && pctx->op.sig.signature->settable_ctx_md_params != NULL)
  702. return pctx->op.sig.signature->settable_ctx_md_params(
  703. pctx->op.sig.algctx);
  704. if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) {
  705. alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
  706. return ctx->digest->settable_ctx_params(ctx->algctx, alg);
  707. }
  708. return NULL;
  709. }
  710. int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
  711. {
  712. EVP_PKEY_CTX *pctx = ctx->pctx;
  713. /* If we have a pctx then we should try that first */
  714. if (pctx != NULL
  715. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  716. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  717. && pctx->op.sig.algctx != NULL
  718. && pctx->op.sig.signature->get_ctx_md_params != NULL)
  719. return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx,
  720. params);
  721. if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL)
  722. return ctx->digest->get_ctx_params(ctx->algctx, params);
  723. return 0;
  724. }
  725. const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
  726. {
  727. void *provctx;
  728. if (md != NULL && md->gettable_ctx_params != NULL) {
  729. provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
  730. return md->gettable_ctx_params(NULL, provctx);
  731. }
  732. return NULL;
  733. }
  734. const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
  735. {
  736. EVP_PKEY_CTX *pctx;
  737. void *provctx;
  738. if (ctx == NULL)
  739. return NULL;
  740. /* If we have a pctx then we should try that first */
  741. pctx = ctx->pctx;
  742. if (pctx != NULL
  743. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  744. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  745. && pctx->op.sig.algctx != NULL
  746. && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
  747. return pctx->op.sig.signature->gettable_ctx_md_params(
  748. pctx->op.sig.algctx);
  749. if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) {
  750. provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
  751. return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
  752. }
  753. return NULL;
  754. }
  755. int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  756. {
  757. int ret = EVP_CTRL_RET_UNSUPPORTED;
  758. int set_params = 1;
  759. size_t sz;
  760. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  761. if (ctx == NULL) {
  762. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  763. return 0;
  764. }
  765. if (ctx->digest != NULL && ctx->digest->prov == NULL)
  766. goto legacy;
  767. switch (cmd) {
  768. case EVP_MD_CTRL_XOF_LEN:
  769. sz = (size_t)p1;
  770. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
  771. break;
  772. case EVP_MD_CTRL_MICALG:
  773. set_params = 0;
  774. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
  775. p2, p1 ? p1 : 9999);
  776. break;
  777. case EVP_CTRL_SSL3_MASTER_SECRET:
  778. params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  779. p2, p1);
  780. break;
  781. default:
  782. goto conclude;
  783. }
  784. if (set_params)
  785. ret = EVP_MD_CTX_set_params(ctx, params);
  786. else
  787. ret = EVP_MD_CTX_get_params(ctx, params);
  788. goto conclude;
  789. /* Code below to be removed when legacy support is dropped. */
  790. legacy:
  791. if (ctx->digest->md_ctrl == NULL) {
  792. ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
  793. return 0;
  794. }
  795. ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
  796. conclude:
  797. if (ret <= 0)
  798. return 0;
  799. return ret;
  800. }
  801. EVP_MD *evp_md_new(void)
  802. {
  803. EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
  804. if (md != NULL && !CRYPTO_NEW_REF(&md->refcnt, 1)) {
  805. OPENSSL_free(md);
  806. return NULL;
  807. }
  808. return md;
  809. }
  810. /*
  811. * FIPS module note: since internal fetches will be entirely
  812. * provider based, we know that none of its code depends on legacy
  813. * NIDs or any functionality that use them.
  814. */
  815. #ifndef FIPS_MODULE
  816. static void set_legacy_nid(const char *name, void *vlegacy_nid)
  817. {
  818. int nid;
  819. int *legacy_nid = vlegacy_nid;
  820. /*
  821. * We use lowest level function to get the associated method, because
  822. * higher level functions such as EVP_get_digestbyname() have changed
  823. * to look at providers too.
  824. */
  825. const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  826. if (*legacy_nid == -1) /* We found a clash already */
  827. return;
  828. if (legacy_method == NULL)
  829. return;
  830. nid = EVP_MD_nid(legacy_method);
  831. if (*legacy_nid != NID_undef && *legacy_nid != nid) {
  832. *legacy_nid = -1;
  833. return;
  834. }
  835. *legacy_nid = nid;
  836. }
  837. #endif
  838. static int evp_md_cache_constants(EVP_MD *md)
  839. {
  840. int ok, xof = 0, algid_absent = 0;
  841. size_t blksz = 0;
  842. size_t mdsize = 0;
  843. OSSL_PARAM params[5];
  844. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
  845. params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
  846. params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
  847. params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
  848. &algid_absent);
  849. params[4] = OSSL_PARAM_construct_end();
  850. ok = evp_do_md_getparams(md, params) > 0;
  851. if (mdsize > INT_MAX || blksz > INT_MAX)
  852. ok = 0;
  853. if (ok) {
  854. md->block_size = (int)blksz;
  855. md->md_size = (int)mdsize;
  856. if (xof)
  857. md->flags |= EVP_MD_FLAG_XOF;
  858. if (algid_absent)
  859. md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
  860. }
  861. return ok;
  862. }
  863. static void *evp_md_from_algorithm(int name_id,
  864. const OSSL_ALGORITHM *algodef,
  865. OSSL_PROVIDER *prov)
  866. {
  867. const OSSL_DISPATCH *fns = algodef->implementation;
  868. EVP_MD *md = NULL;
  869. int fncnt = 0;
  870. /* EVP_MD_fetch() will set the legacy NID if available */
  871. if ((md = evp_md_new()) == NULL) {
  872. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  873. return NULL;
  874. }
  875. #ifndef FIPS_MODULE
  876. md->type = NID_undef;
  877. if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
  878. || md->type == -1) {
  879. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  880. EVP_MD_free(md);
  881. return NULL;
  882. }
  883. #endif
  884. md->name_id = name_id;
  885. if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  886. EVP_MD_free(md);
  887. return NULL;
  888. }
  889. md->description = algodef->algorithm_description;
  890. for (; fns->function_id != 0; fns++) {
  891. switch (fns->function_id) {
  892. case OSSL_FUNC_DIGEST_NEWCTX:
  893. if (md->newctx == NULL) {
  894. md->newctx = OSSL_FUNC_digest_newctx(fns);
  895. fncnt++;
  896. }
  897. break;
  898. case OSSL_FUNC_DIGEST_INIT:
  899. if (md->dinit == NULL) {
  900. md->dinit = OSSL_FUNC_digest_init(fns);
  901. fncnt++;
  902. }
  903. break;
  904. case OSSL_FUNC_DIGEST_UPDATE:
  905. if (md->dupdate == NULL) {
  906. md->dupdate = OSSL_FUNC_digest_update(fns);
  907. fncnt++;
  908. }
  909. break;
  910. case OSSL_FUNC_DIGEST_FINAL:
  911. if (md->dfinal == NULL) {
  912. md->dfinal = OSSL_FUNC_digest_final(fns);
  913. fncnt++;
  914. }
  915. break;
  916. case OSSL_FUNC_DIGEST_SQUEEZE:
  917. if (md->dsqueeze == NULL) {
  918. md->dsqueeze = OSSL_FUNC_digest_squeeze(fns);
  919. fncnt++;
  920. }
  921. break;
  922. case OSSL_FUNC_DIGEST_DIGEST:
  923. if (md->digest == NULL)
  924. md->digest = OSSL_FUNC_digest_digest(fns);
  925. /* We don't increment fnct for this as it is stand alone */
  926. break;
  927. case OSSL_FUNC_DIGEST_FREECTX:
  928. if (md->freectx == NULL) {
  929. md->freectx = OSSL_FUNC_digest_freectx(fns);
  930. fncnt++;
  931. }
  932. break;
  933. case OSSL_FUNC_DIGEST_DUPCTX:
  934. if (md->dupctx == NULL)
  935. md->dupctx = OSSL_FUNC_digest_dupctx(fns);
  936. break;
  937. case OSSL_FUNC_DIGEST_GET_PARAMS:
  938. if (md->get_params == NULL)
  939. md->get_params = OSSL_FUNC_digest_get_params(fns);
  940. break;
  941. case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
  942. if (md->set_ctx_params == NULL)
  943. md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
  944. break;
  945. case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
  946. if (md->get_ctx_params == NULL)
  947. md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
  948. break;
  949. case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
  950. if (md->gettable_params == NULL)
  951. md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
  952. break;
  953. case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
  954. if (md->settable_ctx_params == NULL)
  955. md->settable_ctx_params =
  956. OSSL_FUNC_digest_settable_ctx_params(fns);
  957. break;
  958. case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
  959. if (md->gettable_ctx_params == NULL)
  960. md->gettable_ctx_params =
  961. OSSL_FUNC_digest_gettable_ctx_params(fns);
  962. break;
  963. }
  964. }
  965. if ((fncnt != 0 && fncnt != 5 && fncnt != 6)
  966. || (fncnt == 0 && md->digest == NULL)) {
  967. /*
  968. * In order to be a consistent set of functions we either need the
  969. * whole set of init/update/final etc functions or none of them.
  970. * The "digest" function can standalone. We at least need one way to
  971. * generate digests.
  972. */
  973. EVP_MD_free(md);
  974. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  975. return NULL;
  976. }
  977. md->prov = prov;
  978. if (prov != NULL)
  979. ossl_provider_up_ref(prov);
  980. if (!evp_md_cache_constants(md)) {
  981. EVP_MD_free(md);
  982. ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
  983. md = NULL;
  984. }
  985. return md;
  986. }
  987. static int evp_md_up_ref(void *md)
  988. {
  989. return EVP_MD_up_ref(md);
  990. }
  991. static void evp_md_free(void *md)
  992. {
  993. EVP_MD_free(md);
  994. }
  995. EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  996. const char *properties)
  997. {
  998. EVP_MD *md =
  999. evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
  1000. evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
  1001. return md;
  1002. }
  1003. int EVP_MD_up_ref(EVP_MD *md)
  1004. {
  1005. int ref = 0;
  1006. if (md->origin == EVP_ORIG_DYNAMIC)
  1007. CRYPTO_UP_REF(&md->refcnt, &ref);
  1008. return 1;
  1009. }
  1010. void EVP_MD_free(EVP_MD *md)
  1011. {
  1012. int i;
  1013. if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
  1014. return;
  1015. CRYPTO_DOWN_REF(&md->refcnt, &i);
  1016. if (i > 0)
  1017. return;
  1018. evp_md_free_int(md);
  1019. }
  1020. void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
  1021. void (*fn)(EVP_MD *mac, void *arg),
  1022. void *arg)
  1023. {
  1024. evp_generic_do_all(libctx, OSSL_OP_DIGEST,
  1025. (void (*)(void *, void *))fn, arg,
  1026. evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
  1027. }