digest.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Copyright 1995-2018 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 "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/engine.h>
  14. #include "internal/evp_int.h"
  15. #include "internal/provider.h"
  16. #include "evp_locl.h"
  17. /* This call frees resources associated with the context */
  18. int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
  19. {
  20. if (ctx == NULL)
  21. return 1;
  22. if (ctx->digest == NULL || ctx->digest->prov == NULL)
  23. goto legacy;
  24. if (ctx->provctx != NULL) {
  25. if (ctx->digest->freectx != NULL)
  26. ctx->digest->freectx(ctx->provctx);
  27. ctx->provctx = NULL;
  28. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  29. }
  30. if (ctx->pctx != NULL)
  31. goto legacy;
  32. return 1;
  33. /* TODO(3.0): Remove legacy code below */
  34. legacy:
  35. /*
  36. * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
  37. * sometimes only copies of the context are ever finalised.
  38. */
  39. if (ctx->digest && ctx->digest->cleanup
  40. && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
  41. ctx->digest->cleanup(ctx);
  42. if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
  43. && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
  44. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  45. }
  46. /*
  47. * pctx should be freed by the user of EVP_MD_CTX
  48. * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
  49. */
  50. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
  51. EVP_PKEY_CTX_free(ctx->pctx);
  52. #ifndef OPENSSL_NO_ENGINE
  53. ENGINE_finish(ctx->engine);
  54. #endif
  55. OPENSSL_cleanse(ctx, sizeof(*ctx));
  56. return 1;
  57. }
  58. EVP_MD_CTX *EVP_MD_CTX_new(void)
  59. {
  60. return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  61. }
  62. void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  63. {
  64. if (ctx == NULL)
  65. return;
  66. if (ctx->digest == NULL || ctx->digest->prov == NULL)
  67. goto legacy;
  68. EVP_MD_CTX_reset(ctx);
  69. EVP_MD_meth_free(ctx->fetched_digest);
  70. ctx->fetched_digest = NULL;
  71. ctx->digest = NULL;
  72. ctx->reqdigest = NULL;
  73. OPENSSL_free(ctx);
  74. return;
  75. /* TODO(3.0): Remove legacy code below */
  76. legacy:
  77. EVP_MD_CTX_reset(ctx);
  78. OPENSSL_free(ctx);
  79. }
  80. int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
  81. {
  82. EVP_MD_CTX_reset(ctx);
  83. return EVP_DigestInit_ex(ctx, type, NULL);
  84. }
  85. int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
  86. {
  87. EVP_MD *provmd;
  88. ENGINE *tmpimpl = NULL;
  89. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  90. if (type != NULL)
  91. ctx->reqdigest = type;
  92. /* TODO(3.0): Legacy work around code below. Remove this */
  93. #ifndef OPENSSL_NO_ENGINE
  94. /*
  95. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  96. * this context may already have an ENGINE! Try to avoid releasing the
  97. * previous handle, re-querying for an ENGINE, and having a
  98. * reinitialisation, when it may all be unnecessary.
  99. */
  100. if (ctx->engine && ctx->digest &&
  101. (type == NULL || (type->type == ctx->digest->type)))
  102. goto skip_to_init;
  103. if (type != NULL && impl == NULL)
  104. tmpimpl = ENGINE_get_digest_engine(type->type);
  105. #endif
  106. /*
  107. * If there are engines involved or if we're being used as part of
  108. * EVP_DigestSignInit then we should use legacy handling for now.
  109. */
  110. if (ctx->engine != NULL
  111. || impl != NULL
  112. || tmpimpl != NULL
  113. || ctx->pctx != NULL
  114. || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
  115. if (ctx->digest == ctx->fetched_digest)
  116. ctx->digest = NULL;
  117. EVP_MD_meth_free(ctx->fetched_digest);
  118. ctx->fetched_digest = NULL;
  119. goto legacy;
  120. }
  121. if (type->prov == NULL) {
  122. switch(type->type) {
  123. case NID_sha256:
  124. case NID_md2:
  125. break;
  126. default:
  127. goto legacy;
  128. }
  129. }
  130. if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
  131. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  132. ctx->md_data = NULL;
  133. }
  134. /* TODO(3.0): Start of non-legacy code below */
  135. if (type->prov == NULL) {
  136. provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
  137. if (provmd == NULL) {
  138. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  139. return 0;
  140. }
  141. type = provmd;
  142. EVP_MD_meth_free(ctx->fetched_digest);
  143. ctx->fetched_digest = provmd;
  144. }
  145. ctx->digest = type;
  146. if (ctx->provctx == NULL) {
  147. ctx->provctx = ctx->digest->newctx();
  148. if (ctx->provctx == NULL) {
  149. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  150. return 0;
  151. }
  152. }
  153. if (ctx->digest->dinit == NULL) {
  154. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  155. return 0;
  156. }
  157. return ctx->digest->dinit(ctx->provctx);
  158. /* TODO(3.0): Remove legacy code below */
  159. legacy:
  160. #ifndef OPENSSL_NO_ENGINE
  161. if (type) {
  162. /*
  163. * Ensure an ENGINE left lying around from last time is cleared (the
  164. * previous check attempted to avoid this if the same ENGINE and
  165. * EVP_MD could be used).
  166. */
  167. ENGINE_finish(ctx->engine);
  168. if (impl != NULL) {
  169. if (!ENGINE_init(impl)) {
  170. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  171. return 0;
  172. }
  173. } else {
  174. /* Ask if an ENGINE is reserved for this job */
  175. impl = tmpimpl;
  176. }
  177. if (impl != NULL) {
  178. /* There's an ENGINE for this job ... (apparently) */
  179. const EVP_MD *d = ENGINE_get_digest(impl, type->type);
  180. if (d == NULL) {
  181. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
  182. ENGINE_finish(impl);
  183. return 0;
  184. }
  185. /* We'll use the ENGINE's private digest definition */
  186. type = d;
  187. /*
  188. * Store the ENGINE functional reference so we know 'type' came
  189. * from an ENGINE and we need to release it when done.
  190. */
  191. ctx->engine = impl;
  192. } else
  193. ctx->engine = NULL;
  194. } else {
  195. if (!ctx->digest) {
  196. EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
  197. return 0;
  198. }
  199. type = ctx->digest;
  200. }
  201. #endif
  202. if (ctx->digest != type) {
  203. if (ctx->digest && ctx->digest->ctx_size) {
  204. OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  205. ctx->md_data = NULL;
  206. }
  207. ctx->digest = type;
  208. if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
  209. ctx->update = type->update;
  210. ctx->md_data = OPENSSL_zalloc(type->ctx_size);
  211. if (ctx->md_data == NULL) {
  212. EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
  213. return 0;
  214. }
  215. }
  216. }
  217. #ifndef OPENSSL_NO_ENGINE
  218. skip_to_init:
  219. #endif
  220. if (ctx->pctx) {
  221. int r;
  222. r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
  223. EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
  224. if (r <= 0 && (r != -2))
  225. return 0;
  226. }
  227. if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
  228. return 1;
  229. return ctx->digest->init(ctx);
  230. }
  231. int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
  232. {
  233. if (count == 0)
  234. return 1;
  235. if (ctx->digest == NULL || ctx->digest->prov == NULL)
  236. goto legacy;
  237. if (ctx->digest->dupdate == NULL) {
  238. EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
  239. return 0;
  240. }
  241. return ctx->digest->dupdate(ctx->provctx, data, count);
  242. /* TODO(3.0): Remove legacy code below */
  243. legacy:
  244. return ctx->update(ctx, data, count);
  245. }
  246. /* The caller can assume that this removes any secret data from the context */
  247. int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
  248. {
  249. int ret;
  250. ret = EVP_DigestFinal_ex(ctx, md, size);
  251. EVP_MD_CTX_reset(ctx);
  252. return ret;
  253. }
  254. /* The caller can assume that this removes any secret data from the context */
  255. int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
  256. {
  257. int ret;
  258. size_t size = 0;
  259. if (ctx->digest == NULL || ctx->digest->prov == NULL)
  260. goto legacy;
  261. if (ctx->digest->dfinal == NULL) {
  262. EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
  263. return 0;
  264. }
  265. ret = ctx->digest->dfinal(ctx->provctx, md, &size);
  266. if (isize != NULL) {
  267. if (size <= UINT_MAX) {
  268. *isize = (int)size;
  269. } else {
  270. EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
  271. ret = 0;
  272. }
  273. }
  274. EVP_MD_CTX_reset(ctx);
  275. return ret;
  276. /* TODO(3.0): Remove legacy code below */
  277. legacy:
  278. OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
  279. ret = ctx->digest->final(ctx, md);
  280. if (isize != NULL)
  281. *isize = ctx->digest->md_size;
  282. if (ctx->digest->cleanup) {
  283. ctx->digest->cleanup(ctx);
  284. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  285. }
  286. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  287. return ret;
  288. }
  289. int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
  290. {
  291. int ret = 0;
  292. if (ctx->digest->flags & EVP_MD_FLAG_XOF
  293. && size <= INT_MAX
  294. && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
  295. ret = ctx->digest->final(ctx, md);
  296. if (ctx->digest->cleanup != NULL) {
  297. ctx->digest->cleanup(ctx);
  298. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
  299. }
  300. OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
  301. } else {
  302. EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
  303. }
  304. return ret;
  305. }
  306. int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  307. {
  308. EVP_MD_CTX_reset(out);
  309. return EVP_MD_CTX_copy_ex(out, in);
  310. }
  311. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
  312. {
  313. unsigned char *tmp_buf;
  314. if (in == NULL || in->digest == NULL) {
  315. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
  316. return 0;
  317. }
  318. if (in->digest->prov == NULL)
  319. goto legacy;
  320. if (in->digest->dupctx == NULL) {
  321. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
  322. return 0;
  323. }
  324. EVP_MD_CTX_reset(out);
  325. if (out->fetched_digest != NULL)
  326. EVP_MD_meth_free(out->fetched_digest);
  327. *out = *in;
  328. /* NULL out pointers in case of error */
  329. out->pctx = NULL;
  330. out->provctx = NULL;
  331. if (in->fetched_digest != NULL)
  332. EVP_MD_upref(in->fetched_digest);
  333. out->provctx = in->digest->dupctx(in->provctx);
  334. if (out->provctx == NULL) {
  335. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
  336. return 0;
  337. }
  338. /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
  339. EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  340. if (in->pctx != NULL) {
  341. out->pctx = EVP_PKEY_CTX_dup(in->pctx);
  342. if (out->pctx == NULL) {
  343. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
  344. EVP_MD_CTX_reset(out);
  345. return 0;
  346. }
  347. }
  348. return 1;
  349. /* TODO(3.0): Remove legacy code below */
  350. legacy:
  351. #ifndef OPENSSL_NO_ENGINE
  352. /* Make sure it's safe to copy a digest context using an ENGINE */
  353. if (in->engine && !ENGINE_init(in->engine)) {
  354. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
  355. return 0;
  356. }
  357. #endif
  358. if (out->digest == in->digest) {
  359. tmp_buf = out->md_data;
  360. EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
  361. } else
  362. tmp_buf = NULL;
  363. EVP_MD_CTX_reset(out);
  364. memcpy(out, in, sizeof(*out));
  365. /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
  366. EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  367. /* Null these variables, since they are getting fixed up
  368. * properly below. Anything else may cause a memleak and/or
  369. * double free if any of the memory allocations below fail
  370. */
  371. out->md_data = NULL;
  372. out->pctx = NULL;
  373. if (in->md_data && out->digest->ctx_size) {
  374. if (tmp_buf)
  375. out->md_data = tmp_buf;
  376. else {
  377. out->md_data = OPENSSL_malloc(out->digest->ctx_size);
  378. if (out->md_data == NULL) {
  379. EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
  380. return 0;
  381. }
  382. }
  383. memcpy(out->md_data, in->md_data, out->digest->ctx_size);
  384. }
  385. out->update = in->update;
  386. if (in->pctx) {
  387. out->pctx = EVP_PKEY_CTX_dup(in->pctx);
  388. if (!out->pctx) {
  389. EVP_MD_CTX_reset(out);
  390. return 0;
  391. }
  392. }
  393. if (out->digest->copy)
  394. return out->digest->copy(out, in);
  395. return 1;
  396. }
  397. int EVP_Digest(const void *data, size_t count,
  398. unsigned char *md, unsigned int *size, const EVP_MD *type,
  399. ENGINE *impl)
  400. {
  401. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  402. int ret;
  403. if (ctx == NULL)
  404. return 0;
  405. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
  406. ret = EVP_DigestInit_ex(ctx, type, impl)
  407. && EVP_DigestUpdate(ctx, data, count)
  408. && EVP_DigestFinal_ex(ctx, md, size);
  409. EVP_MD_CTX_free(ctx);
  410. return ret;
  411. }
  412. int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
  413. {
  414. if (ctx->digest && ctx->digest->md_ctrl) {
  415. int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
  416. if (ret <= 0)
  417. return 0;
  418. return 1;
  419. }
  420. return 0;
  421. }
  422. static void *evp_md_from_dispatch(int mdtype, const OSSL_DISPATCH *fns,
  423. OSSL_PROVIDER *prov)
  424. {
  425. EVP_MD *md = NULL;
  426. int fncnt = 0;
  427. if ((md = EVP_MD_meth_new(mdtype, NID_undef)) == NULL)
  428. return NULL;
  429. for (; fns->function_id != 0; fns++) {
  430. switch (fns->function_id) {
  431. case OSSL_FUNC_DIGEST_NEWCTX:
  432. if (md->newctx != NULL)
  433. break;
  434. md->newctx = OSSL_get_OP_digest_newctx(fns);
  435. fncnt++;
  436. break;
  437. case OSSL_FUNC_DIGEST_INIT:
  438. if (md->dinit != NULL)
  439. break;
  440. md->dinit = OSSL_get_OP_digest_init(fns);
  441. fncnt++;
  442. break;
  443. case OSSL_FUNC_DIGEST_UPDDATE:
  444. if (md->dupdate != NULL)
  445. break;
  446. md->dupdate = OSSL_get_OP_digest_update(fns);
  447. fncnt++;
  448. break;
  449. case OSSL_FUNC_DIGEST_FINAL:
  450. if (md->dfinal != NULL)
  451. break;
  452. md->dfinal = OSSL_get_OP_digest_final(fns);
  453. fncnt++;
  454. break;
  455. case OSSL_FUNC_DIGEST_DIGEST:
  456. if (md->digest != NULL)
  457. break;
  458. md->digest = OSSL_get_OP_digest_digest(fns);
  459. /* We don't increment fnct for this as it is stand alone */
  460. break;
  461. case OSSL_FUNC_DIGEST_FREECTX:
  462. if (md->freectx != NULL)
  463. break;
  464. md->freectx = OSSL_get_OP_digest_freectx(fns);
  465. fncnt++;
  466. break;
  467. case OSSL_FUNC_DIGEST_DUPCTX:
  468. if (md->dupctx != NULL)
  469. break;
  470. md->dupctx = OSSL_get_OP_digest_dupctx(fns);
  471. break;
  472. case OSSL_FUNC_DIGEST_SIZE:
  473. if (md->size != NULL)
  474. break;
  475. md->size = OSSL_get_OP_digest_size(fns);
  476. break;
  477. case OSSL_FUNC_DIGEST_BLOCK_SIZE:
  478. if (md->dblock_size != NULL)
  479. break;
  480. md->dblock_size = OSSL_get_OP_digest_block_size(fns);
  481. break;
  482. }
  483. }
  484. if ((fncnt != 0 && fncnt != 5)
  485. || (fncnt == 0 && md->digest == NULL)
  486. || md->size == NULL) {
  487. /*
  488. * In order to be a consistent set of functions we either need the
  489. * whole set of init/update/final etc functions or none of them.
  490. * The "digest" function can standalone. We at least need one way to
  491. * generate digests.
  492. */
  493. EVP_MD_meth_free(md);
  494. return NULL;
  495. }
  496. md->prov = prov;
  497. if (prov != NULL)
  498. ossl_provider_upref(prov);
  499. return md;
  500. }
  501. static int evp_md_upref(void *md)
  502. {
  503. return EVP_MD_upref(md);
  504. }
  505. static void evp_md_free(void *md)
  506. {
  507. EVP_MD_meth_free(md);
  508. }
  509. static int evp_md_nid(void *vmd)
  510. {
  511. EVP_MD *md = vmd;
  512. return md->type;
  513. }
  514. EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
  515. const char *properties)
  516. {
  517. return evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
  518. evp_md_from_dispatch, evp_md_upref,
  519. evp_md_free, evp_md_nid);
  520. }