digest.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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 "internal/provider.h"
  20. #include "internal/core.h"
  21. #include "crypto/evp.h"
  22. #include "evp_local.h"
  23. void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force)
  24. {
  25. if (ctx->algctx != NULL) {
  26. if (ctx->digest != NULL && ctx->digest->freectx != NULL)
  27. ctx->digest->freectx(ctx->algctx);
  28. ctx->algctx = NULL;
  29. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  30. }
  31. /* Code below to be removed when legacy support is dropped. */
  32. /*
  33. * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
  34. * sometimes only copies of the context are ever finalised.
  35. */
  36. if (ctx->digest && ctx->digest->cleanup
  37. && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
  38. ctx->digest->cleanup(ctx);
  39. if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
  40. && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE) || force))
  41. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  42. if (force)
  43. ctx->digest = NULL;
  44. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  45. ENGINE_finish(ctx->engine);
  46. ctx->engine = NULL;
  47. #endif
  48. /* Non legacy code, this has to be later than the ctx->digest cleaning */
  49. EVP_MD_free(ctx->fetched_digest);
  50. ctx->fetched_digest = NULL;
  51. ctx->reqdigest = NULL;
  52. }
  53. /* This call frees resources associated with the context */
  54. int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
  55. {
  56. if (ctx == NULL)
  57. return 1;
  58. #ifndef FIPS_MODULE
  59. /*
  60. * pctx should be freed by the user of EVP_MD_CTX
  61. * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
  62. */
  63. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
  64. EVP_PKEY_CTX_free(ctx->pctx);
  65. ctx->pctx = NULL;
  66. }
  67. #endif
  68. evp_md_ctx_clear_digest(ctx, 0);
  69. OPENSSL_cleanse(ctx, sizeof(*ctx));
  70. return 1;
  71. }
  72. #ifndef FIPS_MODULE
  73. EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
  74. OSSL_LIB_CTX *libctx, const char *propq)
  75. {
  76. EVP_MD_CTX *ctx;
  77. EVP_PKEY_CTX *pctx = NULL;
  78. if ((ctx = EVP_MD_CTX_new()) == NULL
  79. || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) {
  80. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  81. goto err;
  82. }
  83. if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0)
  84. goto err;
  85. EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
  86. return ctx;
  87. err:
  88. EVP_PKEY_CTX_free(pctx);
  89. EVP_MD_CTX_free(ctx);
  90. return NULL;
  91. }
  92. #endif
  93. EVP_MD_CTX *EVP_MD_CTX_new(void)
  94. {
  95. return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  96. }
  97. void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  98. {
  99. if (ctx == NULL)
  100. return;
  101. EVP_MD_CTX_reset(ctx);
  102. OPENSSL_free(ctx);
  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.algctx != 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->algctx != 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->algctx);
  136. ctx->algctx = 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->algctx != NULL && ctx->digest != NULL && ctx->digest != type) {
  208. if (ctx->digest->freectx != NULL)
  209. ctx->digest->freectx(ctx->algctx);
  210. ctx->algctx = 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->algctx == NULL) {
  222. ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
  223. if (ctx->algctx == 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->algctx, 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.algctx != 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->algctx, 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_get_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->algctx, 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->algctx, 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->algctx = NULL;
  457. if (in->fetched_digest != NULL)
  458. EVP_MD_up_ref(in->fetched_digest);
  459. if (in->algctx != NULL) {
  460. out->algctx = in->digest->dupctx(in->algctx);
  461. if (out->algctx == 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_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
  545. const void *data, size_t count,
  546. unsigned char *md, unsigned int *size)
  547. {
  548. EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
  549. int ret = 0;
  550. if (digest != NULL) {
  551. ret = EVP_Digest(data, count, md, size, digest, NULL);
  552. EVP_MD_free(digest);
  553. }
  554. return ret;
  555. }
  556. int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
  557. {
  558. if (digest != NULL && digest->get_params != NULL)
  559. return digest->get_params(params);
  560. return 0;
  561. }
  562. const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
  563. {
  564. if (digest != NULL && digest->gettable_params != NULL)
  565. return digest->gettable_params(
  566. ossl_provider_ctx(EVP_MD_get0_provider(digest)));
  567. return NULL;
  568. }
  569. int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
  570. {
  571. EVP_PKEY_CTX *pctx = ctx->pctx;
  572. /* If we have a pctx then we should try that first */
  573. if (pctx != NULL
  574. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  575. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  576. && pctx->op.sig.algctx != NULL
  577. && pctx->op.sig.signature->set_ctx_md_params != NULL)
  578. return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx,
  579. params);
  580. if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
  581. return ctx->digest->set_ctx_params(ctx->algctx, params);
  582. return 0;
  583. }
  584. const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
  585. {
  586. void *provctx;
  587. if (md != NULL && md->settable_ctx_params != NULL) {
  588. provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
  589. return md->settable_ctx_params(NULL, provctx);
  590. }
  591. return NULL;
  592. }
  593. const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
  594. {
  595. EVP_PKEY_CTX *pctx;
  596. void *alg;
  597. if (ctx == NULL)
  598. return NULL;
  599. /* If we have a pctx then we should try that first */
  600. pctx = ctx->pctx;
  601. if (pctx != NULL
  602. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  603. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  604. && pctx->op.sig.algctx != NULL
  605. && pctx->op.sig.signature->settable_ctx_md_params != NULL)
  606. return pctx->op.sig.signature->settable_ctx_md_params(
  607. pctx->op.sig.algctx);
  608. if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) {
  609. alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
  610. return ctx->digest->settable_ctx_params(ctx->algctx, alg);
  611. }
  612. return NULL;
  613. }
  614. int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
  615. {
  616. EVP_PKEY_CTX *pctx = ctx->pctx;
  617. /* If we have a pctx then we should try that first */
  618. if (pctx != NULL
  619. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  620. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  621. && pctx->op.sig.algctx != NULL
  622. && pctx->op.sig.signature->get_ctx_md_params != NULL)
  623. return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx,
  624. params);
  625. if (ctx->digest != NULL && ctx->digest->get_params != NULL)
  626. return ctx->digest->get_ctx_params(ctx->algctx, params);
  627. return 0;
  628. }
  629. const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
  630. {
  631. void *provctx;
  632. if (md != NULL && md->gettable_ctx_params != NULL) {
  633. provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
  634. return md->gettable_ctx_params(NULL, provctx);
  635. }
  636. return NULL;
  637. }
  638. const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
  639. {
  640. EVP_PKEY_CTX *pctx;
  641. void *provctx;
  642. if (ctx == NULL)
  643. return NULL;
  644. /* If we have a pctx then we should try that first */
  645. pctx = ctx->pctx;
  646. if (pctx != NULL
  647. && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
  648. || pctx->operation == EVP_PKEY_OP_SIGNCTX)
  649. && pctx->op.sig.algctx != NULL
  650. && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
  651. return pctx->op.sig.signature->gettable_ctx_md_params(
  652. pctx->op.sig.algctx);
  653. if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) {
  654. provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
  655. return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
  656. }
  657. return NULL;
  658. }
  659. int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  660. {
  661. int ret = EVP_CTRL_RET_UNSUPPORTED;
  662. int set_params = 1;
  663. size_t sz;
  664. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  665. if (ctx == NULL) {
  666. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  667. return 0;
  668. }
  669. if (ctx->digest != NULL && ctx->digest->prov == NULL)
  670. goto legacy;
  671. switch (cmd) {
  672. case EVP_MD_CTRL_XOF_LEN:
  673. sz = (size_t)p1;
  674. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
  675. break;
  676. case EVP_MD_CTRL_MICALG:
  677. set_params = 0;
  678. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
  679. p2, p1 ? p1 : 9999);
  680. break;
  681. case EVP_CTRL_SSL3_MASTER_SECRET:
  682. params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  683. p2, p1);
  684. break;
  685. default:
  686. goto conclude;
  687. }
  688. if (set_params)
  689. ret = EVP_MD_CTX_set_params(ctx, params);
  690. else
  691. ret = EVP_MD_CTX_get_params(ctx, params);
  692. goto conclude;
  693. /* Code below to be removed when legacy support is dropped. */
  694. legacy:
  695. if (ctx->digest->md_ctrl == NULL) {
  696. ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
  697. return 0;
  698. }
  699. ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
  700. conclude:
  701. if (ret <= 0)
  702. return 0;
  703. return ret;
  704. }
  705. EVP_MD *evp_md_new(void)
  706. {
  707. EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
  708. if (md != NULL) {
  709. md->lock = CRYPTO_THREAD_lock_new();
  710. if (md->lock == NULL) {
  711. OPENSSL_free(md);
  712. return NULL;
  713. }
  714. md->refcnt = 1;
  715. }
  716. return md;
  717. }
  718. /*
  719. * FIPS module note: since internal fetches will be entirely
  720. * provider based, we know that none of its code depends on legacy
  721. * NIDs or any functionality that use them.
  722. */
  723. #ifndef FIPS_MODULE
  724. static void set_legacy_nid(const char *name, void *vlegacy_nid)
  725. {
  726. int nid;
  727. int *legacy_nid = vlegacy_nid;
  728. /*
  729. * We use lowest level function to get the associated method, because
  730. * higher level functions such as EVP_get_digestbyname() have changed
  731. * to look at providers too.
  732. */
  733. const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  734. if (*legacy_nid == -1) /* We found a clash already */
  735. return;
  736. if (legacy_method == NULL)
  737. return;
  738. nid = EVP_MD_nid(legacy_method);
  739. if (*legacy_nid != NID_undef && *legacy_nid != nid) {
  740. *legacy_nid = -1;
  741. return;
  742. }
  743. *legacy_nid = nid;
  744. }
  745. #endif
  746. static int evp_md_cache_constants(EVP_MD *md)
  747. {
  748. int ok, xof = 0, algid_absent = 0;
  749. size_t blksz = 0;
  750. size_t mdsize = 0;
  751. OSSL_PARAM params[5];
  752. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
  753. params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
  754. params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
  755. params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
  756. &algid_absent);
  757. params[4] = OSSL_PARAM_construct_end();
  758. ok = evp_do_md_getparams(md, params) > 0;
  759. if (mdsize > INT_MAX || blksz > INT_MAX)
  760. ok = 0;
  761. if (ok) {
  762. md->block_size = (int)blksz;
  763. md->md_size = (int)mdsize;
  764. if (xof)
  765. md->flags |= EVP_MD_FLAG_XOF;
  766. if (algid_absent)
  767. md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
  768. }
  769. return ok;
  770. }
  771. static void *evp_md_from_algorithm(int name_id,
  772. const OSSL_ALGORITHM *algodef,
  773. OSSL_PROVIDER *prov)
  774. {
  775. const OSSL_DISPATCH *fns = algodef->implementation;
  776. EVP_MD *md = NULL;
  777. int fncnt = 0;
  778. /* EVP_MD_fetch() will set the legacy NID if available */
  779. if ((md = evp_md_new()) == NULL) {
  780. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  781. return NULL;
  782. }
  783. #ifndef FIPS_MODULE
  784. md->type = NID_undef;
  785. if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
  786. || md->type == -1) {
  787. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  788. EVP_MD_free(md);
  789. return NULL;
  790. }
  791. #endif
  792. md->name_id = name_id;
  793. if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  794. EVP_MD_free(md);
  795. return NULL;
  796. }
  797. md->description = algodef->algorithm_description;
  798. for (; fns->function_id != 0; fns++) {
  799. switch (fns->function_id) {
  800. case OSSL_FUNC_DIGEST_NEWCTX:
  801. if (md->newctx == NULL) {
  802. md->newctx = OSSL_FUNC_digest_newctx(fns);
  803. fncnt++;
  804. }
  805. break;
  806. case OSSL_FUNC_DIGEST_INIT:
  807. if (md->dinit == NULL) {
  808. md->dinit = OSSL_FUNC_digest_init(fns);
  809. fncnt++;
  810. }
  811. break;
  812. case OSSL_FUNC_DIGEST_UPDATE:
  813. if (md->dupdate == NULL) {
  814. md->dupdate = OSSL_FUNC_digest_update(fns);
  815. fncnt++;
  816. }
  817. break;
  818. case OSSL_FUNC_DIGEST_FINAL:
  819. if (md->dfinal == NULL) {
  820. md->dfinal = OSSL_FUNC_digest_final(fns);
  821. fncnt++;
  822. }
  823. break;
  824. case OSSL_FUNC_DIGEST_DIGEST:
  825. if (md->digest == NULL)
  826. md->digest = OSSL_FUNC_digest_digest(fns);
  827. /* We don't increment fnct for this as it is stand alone */
  828. break;
  829. case OSSL_FUNC_DIGEST_FREECTX:
  830. if (md->freectx == NULL) {
  831. md->freectx = OSSL_FUNC_digest_freectx(fns);
  832. fncnt++;
  833. }
  834. break;
  835. case OSSL_FUNC_DIGEST_DUPCTX:
  836. if (md->dupctx == NULL)
  837. md->dupctx = OSSL_FUNC_digest_dupctx(fns);
  838. break;
  839. case OSSL_FUNC_DIGEST_GET_PARAMS:
  840. if (md->get_params == NULL)
  841. md->get_params = OSSL_FUNC_digest_get_params(fns);
  842. break;
  843. case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
  844. if (md->set_ctx_params == NULL)
  845. md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
  846. break;
  847. case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
  848. if (md->get_ctx_params == NULL)
  849. md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
  850. break;
  851. case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
  852. if (md->gettable_params == NULL)
  853. md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
  854. break;
  855. case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
  856. if (md->settable_ctx_params == NULL)
  857. md->settable_ctx_params =
  858. OSSL_FUNC_digest_settable_ctx_params(fns);
  859. break;
  860. case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
  861. if (md->gettable_ctx_params == NULL)
  862. md->gettable_ctx_params =
  863. OSSL_FUNC_digest_gettable_ctx_params(fns);
  864. break;
  865. }
  866. }
  867. if ((fncnt != 0 && fncnt != 5)
  868. || (fncnt == 0 && md->digest == NULL)) {
  869. /*
  870. * In order to be a consistent set of functions we either need the
  871. * whole set of init/update/final etc functions or none of them.
  872. * The "digest" function can standalone. We at least need one way to
  873. * generate digests.
  874. */
  875. EVP_MD_free(md);
  876. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  877. return NULL;
  878. }
  879. md->prov = prov;
  880. if (prov != NULL)
  881. ossl_provider_up_ref(prov);
  882. if (!evp_md_cache_constants(md)) {
  883. EVP_MD_free(md);
  884. ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
  885. md = NULL;
  886. }
  887. return md;
  888. }
  889. static int evp_md_up_ref(void *md)
  890. {
  891. return EVP_MD_up_ref(md);
  892. }
  893. static void evp_md_free(void *md)
  894. {
  895. EVP_MD_free(md);
  896. }
  897. EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  898. const char *properties)
  899. {
  900. EVP_MD *md =
  901. evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
  902. evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
  903. return md;
  904. }
  905. int EVP_MD_up_ref(EVP_MD *md)
  906. {
  907. int ref = 0;
  908. if (md->origin == EVP_ORIG_DYNAMIC)
  909. CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
  910. return 1;
  911. }
  912. void EVP_MD_free(EVP_MD *md)
  913. {
  914. int i;
  915. if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
  916. return;
  917. CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
  918. if (i > 0)
  919. return;
  920. evp_md_free_int(md);
  921. }
  922. void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
  923. void (*fn)(EVP_MD *mac, void *arg),
  924. void *arg)
  925. {
  926. evp_generic_do_all(libctx, OSSL_OP_DIGEST,
  927. (void (*)(void *, void *))fn, arg,
  928. evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
  929. }