evp_enc.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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 <assert.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/evp.h>
  13. #include <openssl/err.h>
  14. #include <openssl/rand.h>
  15. #include <openssl/rand_drbg.h>
  16. #include <openssl/engine.h>
  17. #include <openssl/params.h>
  18. #include <openssl/core_names.h>
  19. #include "internal/evp_int.h"
  20. #include "internal/provider.h"
  21. #include "evp_locl.h"
  22. int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
  23. {
  24. if (ctx == NULL)
  25. return 1;
  26. if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
  27. goto legacy;
  28. if (ctx->provctx != NULL) {
  29. if (ctx->cipher->freectx != NULL)
  30. ctx->cipher->freectx(ctx->provctx);
  31. ctx->provctx = NULL;
  32. }
  33. if (ctx->fetched_cipher != NULL)
  34. EVP_CIPHER_meth_free(ctx->fetched_cipher);
  35. memset(ctx, 0, sizeof(*ctx));
  36. return 1;
  37. /* TODO(3.0): Remove legacy code below */
  38. legacy:
  39. if (ctx->cipher != NULL) {
  40. if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
  41. return 0;
  42. /* Cleanse cipher context data */
  43. if (ctx->cipher_data && ctx->cipher->ctx_size)
  44. OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
  45. }
  46. OPENSSL_free(ctx->cipher_data);
  47. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  48. ENGINE_finish(ctx->engine);
  49. #endif
  50. memset(ctx, 0, sizeof(*ctx));
  51. return 1;
  52. }
  53. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
  54. {
  55. return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
  56. }
  57. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  58. {
  59. EVP_CIPHER_CTX_reset(ctx);
  60. OPENSSL_free(ctx);
  61. }
  62. int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  63. const unsigned char *key, const unsigned char *iv, int enc)
  64. {
  65. if (cipher != NULL)
  66. EVP_CIPHER_CTX_reset(ctx);
  67. return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
  68. }
  69. int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  70. ENGINE *impl, const unsigned char *key,
  71. const unsigned char *iv, int enc)
  72. {
  73. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  74. ENGINE *tmpimpl = NULL;
  75. #endif
  76. const EVP_CIPHER *tmpcipher;
  77. /*
  78. * enc == 1 means we are encrypting.
  79. * enc == 0 means we are decrypting.
  80. * enc == -1 means, use the previously initialised value for encrypt/decrypt
  81. */
  82. if (enc == -1) {
  83. enc = ctx->encrypt;
  84. } else {
  85. if (enc)
  86. enc = 1;
  87. ctx->encrypt = enc;
  88. }
  89. if (cipher == NULL && ctx->cipher == NULL) {
  90. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
  91. return 0;
  92. }
  93. /* TODO(3.0): Legacy work around code below. Remove this */
  94. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  95. /*
  96. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  97. * this context may already have an ENGINE! Try to avoid releasing the
  98. * previous handle, re-querying for an ENGINE, and having a
  99. * reinitialisation, when it may all be unnecessary.
  100. */
  101. if (ctx->engine && ctx->cipher
  102. && (cipher == NULL || cipher->nid == ctx->cipher->nid))
  103. goto skip_to_init;
  104. if (cipher != NULL && impl == NULL) {
  105. /* Ask if an ENGINE is reserved for this job */
  106. tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
  107. }
  108. #endif
  109. /*
  110. * If there are engines involved then we should use legacy handling for now.
  111. */
  112. if (ctx->engine != NULL
  113. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  114. || tmpimpl != NULL
  115. #endif
  116. || impl != NULL) {
  117. if (ctx->cipher == ctx->fetched_cipher)
  118. ctx->cipher = NULL;
  119. EVP_CIPHER_meth_free(ctx->fetched_cipher);
  120. ctx->fetched_cipher = NULL;
  121. goto legacy;
  122. }
  123. tmpcipher = (cipher == NULL) ? ctx->cipher : cipher;
  124. if (tmpcipher->prov == NULL) {
  125. switch(tmpcipher->nid) {
  126. case NID_aes_256_ecb:
  127. case NID_aes_192_ecb:
  128. case NID_aes_128_ecb:
  129. case NID_aes_256_cbc:
  130. case NID_aes_192_cbc:
  131. case NID_aes_128_cbc:
  132. case NID_aes_256_ofb128:
  133. case NID_aes_192_ofb128:
  134. case NID_aes_128_ofb128:
  135. case NID_aes_256_cfb128:
  136. case NID_aes_192_cfb128:
  137. case NID_aes_128_cfb128:
  138. case NID_aes_256_cfb1:
  139. case NID_aes_192_cfb1:
  140. case NID_aes_128_cfb1:
  141. case NID_aes_256_cfb8:
  142. case NID_aes_192_cfb8:
  143. case NID_aes_128_cfb8:
  144. case NID_aes_256_ctr:
  145. case NID_aes_192_ctr:
  146. case NID_aes_128_ctr:
  147. break;
  148. default:
  149. goto legacy;
  150. }
  151. }
  152. /*
  153. * Ensure a context left lying around from last time is cleared
  154. * (legacy code)
  155. */
  156. if (cipher != NULL && ctx->cipher != NULL) {
  157. OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
  158. ctx->cipher_data = NULL;
  159. }
  160. /* TODO(3.0): Start of non-legacy code below */
  161. /* Ensure a context left lying around from last time is cleared */
  162. if (cipher != NULL && ctx->cipher != NULL) {
  163. unsigned long flags = ctx->flags;
  164. EVP_CIPHER_CTX_reset(ctx);
  165. /* Restore encrypt and flags */
  166. ctx->encrypt = enc;
  167. ctx->flags = flags;
  168. }
  169. if (cipher != NULL)
  170. ctx->cipher = cipher;
  171. else
  172. cipher = ctx->cipher;
  173. if (cipher->prov == NULL) {
  174. #ifdef FIPS_MODE
  175. /* We only do explict fetches inside the FIPS module */
  176. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  177. return 0;
  178. #else
  179. EVP_CIPHER *provciph =
  180. EVP_CIPHER_fetch(NULL, OBJ_nid2sn(cipher->nid), "");
  181. if (provciph == NULL) {
  182. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  183. return 0;
  184. }
  185. cipher = provciph;
  186. EVP_CIPHER_meth_free(ctx->fetched_cipher);
  187. ctx->fetched_cipher = provciph;
  188. #endif
  189. }
  190. ctx->cipher = cipher;
  191. if (ctx->provctx == NULL) {
  192. ctx->provctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
  193. if (ctx->provctx == NULL) {
  194. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  195. return 0;
  196. }
  197. }
  198. if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
  199. /*
  200. * If this ctx was already set up for no padding then we need to tell
  201. * the new cipher about it.
  202. */
  203. if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
  204. return 0;
  205. }
  206. switch (EVP_CIPHER_mode(ctx->cipher)) {
  207. case EVP_CIPH_CFB_MODE:
  208. case EVP_CIPH_OFB_MODE:
  209. case EVP_CIPH_CBC_MODE:
  210. /* For these modes we remember the original IV for later use */
  211. if (!ossl_assert(EVP_CIPHER_CTX_iv_length(ctx) <= (int)sizeof(ctx->oiv))) {
  212. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  213. return 0;
  214. }
  215. if (iv != NULL)
  216. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  217. }
  218. if (enc) {
  219. if (ctx->cipher->einit == NULL) {
  220. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  221. return 0;
  222. }
  223. return ctx->cipher->einit(ctx->provctx,
  224. key,
  225. key == NULL ? 0
  226. : EVP_CIPHER_CTX_key_length(ctx),
  227. iv,
  228. iv == NULL ? 0
  229. : EVP_CIPHER_CTX_iv_length(ctx));
  230. }
  231. if (ctx->cipher->dinit == NULL) {
  232. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  233. return 0;
  234. }
  235. return ctx->cipher->dinit(ctx->provctx,
  236. key,
  237. key == NULL ? 0
  238. : EVP_CIPHER_CTX_key_length(ctx),
  239. iv,
  240. iv == NULL ? 0
  241. : EVP_CIPHER_CTX_iv_length(ctx));
  242. /* TODO(3.0): Remove legacy code below */
  243. legacy:
  244. if (cipher != NULL) {
  245. /*
  246. * Ensure a context left lying around from last time is cleared (we
  247. * previously attempted to avoid this if the same ENGINE and
  248. * EVP_CIPHER could be used).
  249. */
  250. if (ctx->cipher) {
  251. unsigned long flags = ctx->flags;
  252. EVP_CIPHER_CTX_reset(ctx);
  253. /* Restore encrypt and flags */
  254. ctx->encrypt = enc;
  255. ctx->flags = flags;
  256. }
  257. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  258. if (impl != NULL) {
  259. if (!ENGINE_init(impl)) {
  260. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  261. return 0;
  262. }
  263. } else {
  264. impl = tmpimpl;
  265. }
  266. if (impl != NULL) {
  267. /* There's an ENGINE for this job ... (apparently) */
  268. const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
  269. if (c == NULL) {
  270. /*
  271. * One positive side-effect of US's export control history,
  272. * is that we should at least be able to avoid using US
  273. * misspellings of "initialisation"?
  274. */
  275. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  276. return 0;
  277. }
  278. /* We'll use the ENGINE's private cipher definition */
  279. cipher = c;
  280. /*
  281. * Store the ENGINE functional reference so we know 'cipher' came
  282. * from an ENGINE and we need to release it when done.
  283. */
  284. ctx->engine = impl;
  285. } else {
  286. ctx->engine = NULL;
  287. }
  288. #endif
  289. ctx->cipher = cipher;
  290. if (ctx->cipher->ctx_size) {
  291. ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
  292. if (ctx->cipher_data == NULL) {
  293. ctx->cipher = NULL;
  294. EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
  295. return 0;
  296. }
  297. } else {
  298. ctx->cipher_data = NULL;
  299. }
  300. ctx->key_len = cipher->key_len;
  301. /* Preserve wrap enable flag, zero everything else */
  302. ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
  303. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  304. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  305. ctx->cipher = NULL;
  306. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  307. return 0;
  308. }
  309. }
  310. }
  311. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  312. skip_to_init:
  313. #endif
  314. if (ctx->cipher == NULL)
  315. return 0;
  316. /* we assume block size is a power of 2 in *cryptUpdate */
  317. OPENSSL_assert(ctx->cipher->block_size == 1
  318. || ctx->cipher->block_size == 8
  319. || ctx->cipher->block_size == 16);
  320. if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
  321. && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
  322. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
  323. return 0;
  324. }
  325. if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
  326. switch (EVP_CIPHER_CTX_mode(ctx)) {
  327. case EVP_CIPH_STREAM_CIPHER:
  328. case EVP_CIPH_ECB_MODE:
  329. break;
  330. case EVP_CIPH_CFB_MODE:
  331. case EVP_CIPH_OFB_MODE:
  332. ctx->num = 0;
  333. /* fall-through */
  334. case EVP_CIPH_CBC_MODE:
  335. OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
  336. (int)sizeof(ctx->iv));
  337. if (iv)
  338. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  339. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  340. break;
  341. case EVP_CIPH_CTR_MODE:
  342. ctx->num = 0;
  343. /* Don't reuse IV for CTR mode */
  344. if (iv)
  345. memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  346. break;
  347. default:
  348. return 0;
  349. }
  350. }
  351. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  352. if (!ctx->cipher->init(ctx, key, iv, enc))
  353. return 0;
  354. }
  355. ctx->buf_len = 0;
  356. ctx->final_used = 0;
  357. ctx->block_mask = ctx->cipher->block_size - 1;
  358. return 1;
  359. }
  360. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  361. const unsigned char *in, int inl)
  362. {
  363. if (ctx->encrypt)
  364. return EVP_EncryptUpdate(ctx, out, outl, in, inl);
  365. else
  366. return EVP_DecryptUpdate(ctx, out, outl, in, inl);
  367. }
  368. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  369. {
  370. if (ctx->encrypt)
  371. return EVP_EncryptFinal_ex(ctx, out, outl);
  372. else
  373. return EVP_DecryptFinal_ex(ctx, out, outl);
  374. }
  375. int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  376. {
  377. if (ctx->encrypt)
  378. return EVP_EncryptFinal(ctx, out, outl);
  379. else
  380. return EVP_DecryptFinal(ctx, out, outl);
  381. }
  382. int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  383. const unsigned char *key, const unsigned char *iv)
  384. {
  385. return EVP_CipherInit(ctx, cipher, key, iv, 1);
  386. }
  387. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  388. ENGINE *impl, const unsigned char *key,
  389. const unsigned char *iv)
  390. {
  391. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  392. }
  393. int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  394. const unsigned char *key, const unsigned char *iv)
  395. {
  396. return EVP_CipherInit(ctx, cipher, key, iv, 0);
  397. }
  398. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  399. ENGINE *impl, const unsigned char *key,
  400. const unsigned char *iv)
  401. {
  402. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  403. }
  404. /*
  405. * According to the letter of standard difference between pointers
  406. * is specified to be valid only within same object. This makes
  407. * it formally challenging to determine if input and output buffers
  408. * are not partially overlapping with standard pointer arithmetic.
  409. */
  410. #ifdef PTRDIFF_T
  411. # undef PTRDIFF_T
  412. #endif
  413. #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
  414. /*
  415. * Then we have VMS that distinguishes itself by adhering to
  416. * sizeof(size_t)==4 even in 64-bit builds, which means that
  417. * difference between two pointers might be truncated to 32 bits.
  418. * In the context one can even wonder how comparison for
  419. * equality is implemented. To be on the safe side we adhere to
  420. * PTRDIFF_T even for comparison for equality.
  421. */
  422. # define PTRDIFF_T uint64_t
  423. #else
  424. # define PTRDIFF_T size_t
  425. #endif
  426. int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
  427. {
  428. PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
  429. /*
  430. * Check for partially overlapping buffers. [Binary logical
  431. * operations are used instead of boolean to minimize number
  432. * of conditional branches.]
  433. */
  434. int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
  435. (diff > (0 - (PTRDIFF_T)len)));
  436. return overlapped;
  437. }
  438. static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
  439. unsigned char *out, int *outl,
  440. const unsigned char *in, int inl)
  441. {
  442. int i, j, bl, cmpl = inl;
  443. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  444. cmpl = (cmpl + 7) / 8;
  445. bl = ctx->cipher->block_size;
  446. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  447. /* If block size > 1 then the cipher will have to do this check */
  448. if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
  449. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  450. return 0;
  451. }
  452. i = ctx->cipher->do_cipher(ctx, out, in, inl);
  453. if (i < 0)
  454. return 0;
  455. else
  456. *outl = i;
  457. return 1;
  458. }
  459. if (inl <= 0) {
  460. *outl = 0;
  461. return inl == 0;
  462. }
  463. if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
  464. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  465. return 0;
  466. }
  467. if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
  468. if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
  469. *outl = inl;
  470. return 1;
  471. } else {
  472. *outl = 0;
  473. return 0;
  474. }
  475. }
  476. i = ctx->buf_len;
  477. OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
  478. if (i != 0) {
  479. if (bl - i > inl) {
  480. memcpy(&(ctx->buf[i]), in, inl);
  481. ctx->buf_len += inl;
  482. *outl = 0;
  483. return 1;
  484. } else {
  485. j = bl - i;
  486. memcpy(&(ctx->buf[i]), in, j);
  487. inl -= j;
  488. in += j;
  489. if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
  490. return 0;
  491. out += bl;
  492. *outl = bl;
  493. }
  494. } else
  495. *outl = 0;
  496. i = inl & (bl - 1);
  497. inl -= i;
  498. if (inl > 0) {
  499. if (!ctx->cipher->do_cipher(ctx, out, in, inl))
  500. return 0;
  501. *outl += inl;
  502. }
  503. if (i != 0)
  504. memcpy(ctx->buf, &(in[inl]), i);
  505. ctx->buf_len = i;
  506. return 1;
  507. }
  508. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  509. const unsigned char *in, int inl)
  510. {
  511. int ret;
  512. size_t soutl;
  513. int blocksize;
  514. /* Prevent accidental use of decryption context when encrypting */
  515. if (!ctx->encrypt) {
  516. EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
  517. return 0;
  518. }
  519. if (ctx->cipher == NULL) {
  520. EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_NO_CIPHER_SET);
  521. return 0;
  522. }
  523. if (ctx->cipher->prov == NULL)
  524. goto legacy;
  525. blocksize = EVP_CIPHER_CTX_block_size(ctx);
  526. if (ctx->cipher->cupdate == NULL || blocksize < 1) {
  527. EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_UPDATE_ERROR);
  528. return 0;
  529. }
  530. ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
  531. inl + (blocksize == 1 ? 0 : blocksize), in,
  532. (size_t)inl);
  533. if (ret) {
  534. if (soutl > INT_MAX) {
  535. EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_UPDATE_ERROR);
  536. return 0;
  537. }
  538. *outl = soutl;
  539. }
  540. return ret;
  541. /* TODO(3.0): Remove legacy code below */
  542. legacy:
  543. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  544. }
  545. int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  546. {
  547. int ret;
  548. ret = EVP_EncryptFinal_ex(ctx, out, outl);
  549. return ret;
  550. }
  551. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  552. {
  553. int n, ret;
  554. unsigned int i, b, bl;
  555. size_t soutl;
  556. int blocksize;
  557. /* Prevent accidental use of decryption context when encrypting */
  558. if (!ctx->encrypt) {
  559. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  560. return 0;
  561. }
  562. if (ctx->cipher == NULL) {
  563. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_NO_CIPHER_SET);
  564. return 0;
  565. }
  566. if (ctx->cipher->prov == NULL)
  567. goto legacy;
  568. blocksize = EVP_CIPHER_CTX_block_size(ctx);
  569. if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
  570. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_FINAL_ERROR);
  571. return 0;
  572. }
  573. ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
  574. blocksize == 1 ? 0 : blocksize);
  575. if (ret) {
  576. if (soutl > INT_MAX) {
  577. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_FINAL_ERROR);
  578. return 0;
  579. }
  580. *outl = soutl;
  581. }
  582. return ret;
  583. /* TODO(3.0): Remove legacy code below */
  584. legacy:
  585. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  586. ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  587. if (ret < 0)
  588. return 0;
  589. else
  590. *outl = ret;
  591. return 1;
  592. }
  593. b = ctx->cipher->block_size;
  594. OPENSSL_assert(b <= sizeof(ctx->buf));
  595. if (b == 1) {
  596. *outl = 0;
  597. return 1;
  598. }
  599. bl = ctx->buf_len;
  600. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  601. if (bl) {
  602. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
  603. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  604. return 0;
  605. }
  606. *outl = 0;
  607. return 1;
  608. }
  609. n = b - bl;
  610. for (i = bl; i < b; i++)
  611. ctx->buf[i] = n;
  612. ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
  613. if (ret)
  614. *outl = b;
  615. return ret;
  616. }
  617. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  618. const unsigned char *in, int inl)
  619. {
  620. int fix_len, cmpl = inl, ret;
  621. unsigned int b;
  622. size_t soutl;
  623. int blocksize;
  624. /* Prevent accidental use of encryption context when decrypting */
  625. if (ctx->encrypt) {
  626. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
  627. return 0;
  628. }
  629. if (ctx->cipher == NULL) {
  630. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_NO_CIPHER_SET);
  631. return 0;
  632. }
  633. if (ctx->cipher->prov == NULL)
  634. goto legacy;
  635. blocksize = EVP_CIPHER_CTX_block_size(ctx);
  636. if (ctx->cipher->cupdate == NULL || blocksize < 1) {
  637. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);
  638. return 0;
  639. }
  640. ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
  641. inl + (blocksize == 1 ? 0 : blocksize), in,
  642. (size_t)inl);
  643. if (ret) {
  644. if (soutl > INT_MAX) {
  645. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);
  646. return 0;
  647. }
  648. *outl = soutl;
  649. }
  650. return ret;
  651. /* TODO(3.0): Remove legacy code below */
  652. legacy:
  653. b = ctx->cipher->block_size;
  654. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  655. cmpl = (cmpl + 7) / 8;
  656. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  657. if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
  658. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  659. return 0;
  660. }
  661. fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
  662. if (fix_len < 0) {
  663. *outl = 0;
  664. return 0;
  665. } else
  666. *outl = fix_len;
  667. return 1;
  668. }
  669. if (inl <= 0) {
  670. *outl = 0;
  671. return inl == 0;
  672. }
  673. if (ctx->flags & EVP_CIPH_NO_PADDING)
  674. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  675. OPENSSL_assert(b <= sizeof(ctx->final));
  676. if (ctx->final_used) {
  677. /* see comment about PTRDIFF_T comparison above */
  678. if (((PTRDIFF_T)out == (PTRDIFF_T)in)
  679. || is_partially_overlapping(out, in, b)) {
  680. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  681. return 0;
  682. }
  683. memcpy(out, ctx->final, b);
  684. out += b;
  685. fix_len = 1;
  686. } else
  687. fix_len = 0;
  688. if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
  689. return 0;
  690. /*
  691. * if we have 'decrypted' a multiple of block size, make sure we have a
  692. * copy of this last block
  693. */
  694. if (b > 1 && !ctx->buf_len) {
  695. *outl -= b;
  696. ctx->final_used = 1;
  697. memcpy(ctx->final, &out[*outl], b);
  698. } else
  699. ctx->final_used = 0;
  700. if (fix_len)
  701. *outl += b;
  702. return 1;
  703. }
  704. int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  705. {
  706. int ret;
  707. ret = EVP_DecryptFinal_ex(ctx, out, outl);
  708. return ret;
  709. }
  710. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  711. {
  712. int i, n;
  713. unsigned int b;
  714. size_t soutl;
  715. int ret;
  716. int blocksize;
  717. /* Prevent accidental use of encryption context when decrypting */
  718. if (ctx->encrypt) {
  719. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  720. return 0;
  721. }
  722. if (ctx->cipher == NULL) {
  723. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_NO_CIPHER_SET);
  724. return 0;
  725. }
  726. if (ctx->cipher->prov == NULL)
  727. goto legacy;
  728. blocksize = EVP_CIPHER_CTX_block_size(ctx);
  729. if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
  730. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_FINAL_ERROR);
  731. return 0;
  732. }
  733. ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
  734. blocksize == 1 ? 0 : blocksize);
  735. if (ret) {
  736. if (soutl > INT_MAX) {
  737. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_FINAL_ERROR);
  738. return 0;
  739. }
  740. *outl = soutl;
  741. }
  742. return ret;
  743. /* TODO(3.0): Remove legacy code below */
  744. legacy:
  745. *outl = 0;
  746. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  747. i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  748. if (i < 0)
  749. return 0;
  750. else
  751. *outl = i;
  752. return 1;
  753. }
  754. b = ctx->cipher->block_size;
  755. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  756. if (ctx->buf_len) {
  757. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
  758. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  759. return 0;
  760. }
  761. *outl = 0;
  762. return 1;
  763. }
  764. if (b > 1) {
  765. if (ctx->buf_len || !ctx->final_used) {
  766. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
  767. return 0;
  768. }
  769. OPENSSL_assert(b <= sizeof(ctx->final));
  770. /*
  771. * The following assumes that the ciphertext has been authenticated.
  772. * Otherwise it provides a padding oracle.
  773. */
  774. n = ctx->final[b - 1];
  775. if (n == 0 || n > (int)b) {
  776. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  777. return 0;
  778. }
  779. for (i = 0; i < n; i++) {
  780. if (ctx->final[--b] != n) {
  781. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  782. return 0;
  783. }
  784. }
  785. n = ctx->cipher->block_size - n;
  786. for (i = 0; i < n; i++)
  787. out[i] = ctx->final[i];
  788. *outl = n;
  789. } else
  790. *outl = 0;
  791. return 1;
  792. }
  793. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
  794. {
  795. if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
  796. return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
  797. if (EVP_CIPHER_CTX_key_length(c) == keylen)
  798. return 1;
  799. if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  800. c->key_len = keylen;
  801. return 1;
  802. }
  803. EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
  804. return 0;
  805. }
  806. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
  807. {
  808. if (pad)
  809. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  810. else
  811. ctx->flags |= EVP_CIPH_NO_PADDING;
  812. if (ctx->cipher != NULL && ctx->cipher->prov != NULL) {
  813. OSSL_PARAM params[] = {
  814. OSSL_PARAM_int(OSSL_CIPHER_PARAM_PADDING, NULL),
  815. OSSL_PARAM_END
  816. };
  817. params[0].data = &pad;
  818. if (ctx->cipher->ctx_set_params == NULL) {
  819. EVPerr(EVP_F_EVP_CIPHER_CTX_SET_PADDING, EVP_R_CTRL_NOT_IMPLEMENTED);
  820. return 0;
  821. }
  822. if (!ctx->cipher->ctx_set_params(ctx->provctx, params))
  823. return 0;
  824. }
  825. return 1;
  826. }
  827. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  828. {
  829. int ret;
  830. if (!ctx->cipher) {
  831. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  832. return 0;
  833. }
  834. if (!ctx->cipher->ctrl) {
  835. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  836. return 0;
  837. }
  838. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  839. if (ret == -1) {
  840. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
  841. EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  842. return 0;
  843. }
  844. return ret;
  845. }
  846. #if !defined(FIPS_MODE)
  847. /* TODO(3.0): No support for RAND yet in the FIPS module */
  848. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  849. {
  850. int kl;
  851. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  852. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  853. kl = EVP_CIPHER_CTX_key_length(ctx);
  854. if (kl <= 0 || RAND_priv_bytes(key, kl) <= 0)
  855. return 0;
  856. return 1;
  857. }
  858. #endif
  859. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  860. {
  861. if ((in == NULL) || (in->cipher == NULL)) {
  862. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
  863. return 0;
  864. }
  865. if (in->cipher->prov == NULL)
  866. goto legacy;
  867. if (in->cipher->dupctx == NULL) {
  868. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_NOT_ABLE_TO_COPY_CTX);
  869. return 0;
  870. }
  871. EVP_CIPHER_CTX_reset(out);
  872. *out = *in;
  873. out->provctx = NULL;
  874. if (in->fetched_cipher != NULL && !EVP_CIPHER_upref(in->fetched_cipher)) {
  875. out->fetched_cipher = NULL;
  876. return 0;
  877. }
  878. out->provctx = in->cipher->dupctx(in->provctx);
  879. if (out->provctx == NULL) {
  880. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_NOT_ABLE_TO_COPY_CTX);
  881. return 0;
  882. }
  883. return 1;
  884. /* TODO(3.0): Remove legacy code below */
  885. legacy:
  886. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
  887. /* Make sure it's safe to copy a cipher context using an ENGINE */
  888. if (in->engine && !ENGINE_init(in->engine)) {
  889. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
  890. return 0;
  891. }
  892. #endif
  893. EVP_CIPHER_CTX_reset(out);
  894. memcpy(out, in, sizeof(*out));
  895. if (in->cipher_data && in->cipher->ctx_size) {
  896. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  897. if (out->cipher_data == NULL) {
  898. out->cipher = NULL;
  899. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
  900. return 0;
  901. }
  902. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  903. }
  904. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  905. if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
  906. out->cipher = NULL;
  907. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
  908. return 0;
  909. }
  910. return 1;
  911. }
  912. static void *evp_cipher_from_dispatch(const OSSL_DISPATCH *fns,
  913. OSSL_PROVIDER *prov)
  914. {
  915. EVP_CIPHER *cipher = NULL;
  916. int fnciphcnt = 0, fnctxcnt = 0;
  917. /*
  918. * The legacy NID is set by EVP_CIPHER_fetch() if the name exists in
  919. * the object database.
  920. */
  921. if ((cipher = EVP_CIPHER_meth_new(0, 0, 0)) == NULL)
  922. return NULL;
  923. for (; fns->function_id != 0; fns++) {
  924. switch (fns->function_id) {
  925. case OSSL_FUNC_CIPHER_NEWCTX:
  926. if (cipher->newctx != NULL)
  927. break;
  928. cipher->newctx = OSSL_get_OP_cipher_newctx(fns);
  929. fnctxcnt++;
  930. break;
  931. case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
  932. if (cipher->einit != NULL)
  933. break;
  934. cipher->einit = OSSL_get_OP_cipher_encrypt_init(fns);
  935. fnciphcnt++;
  936. break;
  937. case OSSL_FUNC_CIPHER_DECRYPT_INIT:
  938. if (cipher->dinit != NULL)
  939. break;
  940. cipher->dinit = OSSL_get_OP_cipher_decrypt_init(fns);
  941. fnciphcnt++;
  942. break;
  943. case OSSL_FUNC_CIPHER_UPDATE:
  944. if (cipher->cupdate != NULL)
  945. break;
  946. cipher->cupdate = OSSL_get_OP_cipher_update(fns);
  947. fnciphcnt++;
  948. break;
  949. case OSSL_FUNC_CIPHER_FINAL:
  950. if (cipher->cfinal != NULL)
  951. break;
  952. cipher->cfinal = OSSL_get_OP_cipher_final(fns);
  953. fnciphcnt++;
  954. break;
  955. case OSSL_FUNC_CIPHER_CIPHER:
  956. if (cipher->ccipher != NULL)
  957. break;
  958. cipher->ccipher = OSSL_get_OP_cipher_cipher(fns);
  959. break;
  960. case OSSL_FUNC_CIPHER_FREECTX:
  961. if (cipher->freectx != NULL)
  962. break;
  963. cipher->freectx = OSSL_get_OP_cipher_freectx(fns);
  964. fnctxcnt++;
  965. break;
  966. case OSSL_FUNC_CIPHER_DUPCTX:
  967. if (cipher->dupctx != NULL)
  968. break;
  969. cipher->dupctx = OSSL_get_OP_cipher_dupctx(fns);
  970. break;
  971. case OSSL_FUNC_CIPHER_KEY_LENGTH:
  972. if (cipher->key_length != NULL)
  973. break;
  974. cipher->key_length = OSSL_get_OP_cipher_key_length(fns);
  975. break;
  976. case OSSL_FUNC_CIPHER_IV_LENGTH:
  977. if (cipher->iv_length != NULL)
  978. break;
  979. cipher->iv_length = OSSL_get_OP_cipher_iv_length(fns);
  980. break;
  981. case OSSL_FUNC_CIPHER_BLOCK_SIZE:
  982. if (cipher->blocksize != NULL)
  983. break;
  984. cipher->blocksize = OSSL_get_OP_cipher_block_size(fns);
  985. break;
  986. case OSSL_FUNC_CIPHER_GET_PARAMS:
  987. if (cipher->get_params != NULL)
  988. break;
  989. cipher->get_params = OSSL_get_OP_cipher_get_params(fns);
  990. break;
  991. case OSSL_FUNC_CIPHER_CTX_GET_PARAMS:
  992. if (cipher->ctx_get_params != NULL)
  993. break;
  994. cipher->ctx_get_params = OSSL_get_OP_cipher_ctx_get_params(fns);
  995. break;
  996. case OSSL_FUNC_CIPHER_CTX_SET_PARAMS:
  997. if (cipher->ctx_set_params != NULL)
  998. break;
  999. cipher->ctx_set_params = OSSL_get_OP_cipher_ctx_set_params(fns);
  1000. break;
  1001. }
  1002. }
  1003. if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
  1004. || (fnciphcnt == 0 && cipher->ccipher == NULL)
  1005. || fnctxcnt != 2
  1006. || cipher->blocksize == NULL
  1007. || cipher->iv_length == NULL
  1008. || cipher->key_length == NULL) {
  1009. /*
  1010. * In order to be a consistent set of functions we must have at least
  1011. * a complete set of "encrypt" functions, or a complete set of "decrypt"
  1012. * functions, or a single "cipher" function. In all cases we need a
  1013. * complete set of context management functions, as well as the
  1014. * blocksize, iv_length and key_length functions.
  1015. */
  1016. EVP_CIPHER_meth_free(cipher);
  1017. EVPerr(EVP_F_EVP_CIPHER_FROM_DISPATCH, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  1018. return NULL;
  1019. }
  1020. cipher->prov = prov;
  1021. if (prov != NULL)
  1022. ossl_provider_upref(prov);
  1023. return cipher;
  1024. }
  1025. static int evp_cipher_upref(void *cipher)
  1026. {
  1027. return EVP_CIPHER_upref(cipher);
  1028. }
  1029. static void evp_cipher_free(void *cipher)
  1030. {
  1031. EVP_CIPHER_meth_free(cipher);
  1032. }
  1033. EVP_CIPHER *EVP_CIPHER_fetch(OPENSSL_CTX *ctx, const char *algorithm,
  1034. const char *properties)
  1035. {
  1036. EVP_CIPHER *cipher =
  1037. evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
  1038. evp_cipher_from_dispatch, evp_cipher_upref,
  1039. evp_cipher_free);
  1040. #ifndef FIPS_MODE
  1041. /* TODO(3.x) get rid of the need for legacy NIDs */
  1042. if (cipher != NULL) {
  1043. /*
  1044. * FIPS module note: since internal fetches will be entirely
  1045. * provider based, we know that none of its code depends on legacy
  1046. * NIDs or any functionality that use them.
  1047. */
  1048. cipher->nid = OBJ_sn2nid(algorithm);
  1049. }
  1050. #endif
  1051. return cipher;
  1052. }