digest.c 27 KB

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