digest.c 33 KB

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