evp_enc.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /*
  2. * Copyright 1995-2023 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 <limits.h>
  13. #include <assert.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/err.h>
  16. #include <openssl/rand.h>
  17. #ifndef FIPS_MODULE
  18. # include <openssl/engine.h>
  19. #endif
  20. #include <openssl/params.h>
  21. #include <openssl/core_names.h>
  22. #include "internal/cryptlib.h"
  23. #include "internal/provider.h"
  24. #include "internal/core.h"
  25. #include "internal/safe_math.h"
  26. #include "crypto/evp.h"
  27. #include "evp_local.h"
  28. OSSL_SAFE_MATH_SIGNED(int, int)
  29. int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
  30. {
  31. if (ctx == NULL)
  32. return 1;
  33. if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
  34. goto legacy;
  35. if (ctx->algctx != NULL) {
  36. if (ctx->cipher->freectx != NULL)
  37. ctx->cipher->freectx(ctx->algctx);
  38. ctx->algctx = NULL;
  39. }
  40. if (ctx->fetched_cipher != NULL)
  41. EVP_CIPHER_free(ctx->fetched_cipher);
  42. memset(ctx, 0, sizeof(*ctx));
  43. ctx->iv_len = -1;
  44. return 1;
  45. /* Remove legacy code below when legacy support is removed. */
  46. legacy:
  47. if (ctx->cipher != NULL) {
  48. if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
  49. return 0;
  50. /* Cleanse cipher context data */
  51. if (ctx->cipher_data && ctx->cipher->ctx_size)
  52. OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
  53. }
  54. OPENSSL_free(ctx->cipher_data);
  55. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  56. ENGINE_finish(ctx->engine);
  57. #endif
  58. memset(ctx, 0, sizeof(*ctx));
  59. ctx->iv_len = -1;
  60. return 1;
  61. }
  62. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
  63. {
  64. EVP_CIPHER_CTX *ctx;
  65. ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
  66. if (ctx == NULL)
  67. return NULL;
  68. ctx->iv_len = -1;
  69. return ctx;
  70. }
  71. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  72. {
  73. if (ctx == NULL)
  74. return;
  75. EVP_CIPHER_CTX_reset(ctx);
  76. OPENSSL_free(ctx);
  77. }
  78. static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
  79. const EVP_CIPHER *cipher,
  80. ENGINE *impl, const unsigned char *key,
  81. const unsigned char *iv, int enc,
  82. const OSSL_PARAM params[])
  83. {
  84. int n;
  85. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  86. ENGINE *tmpimpl = NULL;
  87. #endif
  88. /*
  89. * enc == 1 means we are encrypting.
  90. * enc == 0 means we are decrypting.
  91. * enc == -1 means, use the previously initialised value for encrypt/decrypt
  92. */
  93. if (enc == -1) {
  94. enc = ctx->encrypt;
  95. } else {
  96. if (enc)
  97. enc = 1;
  98. ctx->encrypt = enc;
  99. }
  100. if (cipher == NULL && ctx->cipher == NULL) {
  101. ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
  102. return 0;
  103. }
  104. /* Code below to be removed when legacy support is dropped. */
  105. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  106. /*
  107. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  108. * this context may already have an ENGINE! Try to avoid releasing the
  109. * previous handle, re-querying for an ENGINE, and having a
  110. * reinitialisation, when it may all be unnecessary.
  111. */
  112. if (ctx->engine && ctx->cipher
  113. && (cipher == NULL || cipher->nid == ctx->cipher->nid))
  114. goto skip_to_init;
  115. if (cipher != NULL && impl == NULL) {
  116. /* Ask if an ENGINE is reserved for this job */
  117. tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
  118. }
  119. #endif
  120. /*
  121. * If there are engines involved then we should use legacy handling for now.
  122. */
  123. if (ctx->engine != NULL
  124. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  125. || tmpimpl != NULL
  126. #endif
  127. || impl != NULL
  128. || (cipher != NULL && cipher->origin == EVP_ORIG_METH)
  129. || (cipher == NULL && ctx->cipher != NULL
  130. && ctx->cipher->origin == EVP_ORIG_METH)) {
  131. if (ctx->cipher == ctx->fetched_cipher)
  132. ctx->cipher = NULL;
  133. EVP_CIPHER_free(ctx->fetched_cipher);
  134. ctx->fetched_cipher = NULL;
  135. goto legacy;
  136. }
  137. /*
  138. * Ensure a context left lying around from last time is cleared
  139. * (legacy code)
  140. */
  141. if (cipher != NULL && ctx->cipher != NULL) {
  142. if (ctx->cipher->cleanup != NULL && !ctx->cipher->cleanup(ctx))
  143. return 0;
  144. OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
  145. ctx->cipher_data = NULL;
  146. }
  147. /* Start of non-legacy code below */
  148. /* Ensure a context left lying around from last time is cleared */
  149. if (cipher != NULL && ctx->cipher != NULL) {
  150. unsigned long flags = ctx->flags;
  151. EVP_CIPHER_CTX_reset(ctx);
  152. /* Restore encrypt and flags */
  153. ctx->encrypt = enc;
  154. ctx->flags = flags;
  155. }
  156. if (cipher == NULL)
  157. cipher = ctx->cipher;
  158. if (cipher->prov == NULL) {
  159. #ifdef FIPS_MODULE
  160. /* We only do explicit fetches inside the FIPS module */
  161. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  162. return 0;
  163. #else
  164. EVP_CIPHER *provciph =
  165. EVP_CIPHER_fetch(NULL,
  166. cipher->nid == NID_undef ? "NULL"
  167. : OBJ_nid2sn(cipher->nid),
  168. "");
  169. if (provciph == NULL)
  170. return 0;
  171. cipher = provciph;
  172. EVP_CIPHER_free(ctx->fetched_cipher);
  173. ctx->fetched_cipher = provciph;
  174. #endif
  175. }
  176. if (!ossl_assert(cipher->prov != NULL)) {
  177. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  178. return 0;
  179. }
  180. if (cipher != ctx->fetched_cipher) {
  181. if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
  182. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  183. return 0;
  184. }
  185. EVP_CIPHER_free(ctx->fetched_cipher);
  186. /* Coverity false positive, the reference counting is confusing it */
  187. /* coverity[use_after_free] */
  188. ctx->fetched_cipher = (EVP_CIPHER *)cipher;
  189. }
  190. ctx->cipher = cipher;
  191. if (ctx->algctx == NULL) {
  192. ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
  193. if (ctx->algctx == NULL) {
  194. ERR_raise(ERR_LIB_EVP, 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. #ifndef FIPS_MODULE
  207. /*
  208. * Fix for CVE-2023-5363
  209. * Passing in a size as part of the init call takes effect late
  210. * so, force such to occur before the initialisation.
  211. *
  212. * The FIPS provider's internal library context is used in a manner
  213. * such that this is not an issue.
  214. */
  215. if (params != NULL) {
  216. OSSL_PARAM param_lens[3] = { OSSL_PARAM_END, OSSL_PARAM_END,
  217. OSSL_PARAM_END };
  218. OSSL_PARAM *q = param_lens;
  219. const OSSL_PARAM *p;
  220. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  221. if (p != NULL)
  222. memcpy(q++, p, sizeof(*q));
  223. /*
  224. * Note that OSSL_CIPHER_PARAM_AEAD_IVLEN is a synonym for
  225. * OSSL_CIPHER_PARAM_IVLEN so both are covered here.
  226. */
  227. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
  228. if (p != NULL)
  229. memcpy(q++, p, sizeof(*q));
  230. if (q != param_lens) {
  231. if (!EVP_CIPHER_CTX_set_params(ctx, param_lens)) {
  232. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
  233. return 0;
  234. }
  235. }
  236. }
  237. #endif
  238. if (enc) {
  239. if (ctx->cipher->einit == NULL) {
  240. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  241. return 0;
  242. }
  243. return ctx->cipher->einit(ctx->algctx,
  244. key,
  245. key == NULL ? 0
  246. : EVP_CIPHER_CTX_get_key_length(ctx),
  247. iv,
  248. iv == NULL ? 0
  249. : EVP_CIPHER_CTX_get_iv_length(ctx),
  250. params);
  251. }
  252. if (ctx->cipher->dinit == NULL) {
  253. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  254. return 0;
  255. }
  256. return ctx->cipher->dinit(ctx->algctx,
  257. key,
  258. key == NULL ? 0
  259. : EVP_CIPHER_CTX_get_key_length(ctx),
  260. iv,
  261. iv == NULL ? 0
  262. : EVP_CIPHER_CTX_get_iv_length(ctx),
  263. params);
  264. /* Code below to be removed when legacy support is dropped. */
  265. legacy:
  266. if (cipher != NULL) {
  267. /*
  268. * Ensure a context left lying around from last time is cleared (we
  269. * previously attempted to avoid this if the same ENGINE and
  270. * EVP_CIPHER could be used).
  271. */
  272. if (ctx->cipher) {
  273. unsigned long flags = ctx->flags;
  274. EVP_CIPHER_CTX_reset(ctx);
  275. /* Restore encrypt and flags */
  276. ctx->encrypt = enc;
  277. ctx->flags = flags;
  278. }
  279. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  280. if (impl != NULL) {
  281. if (!ENGINE_init(impl)) {
  282. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  283. return 0;
  284. }
  285. } else {
  286. impl = tmpimpl;
  287. }
  288. if (impl != NULL) {
  289. /* There's an ENGINE for this job ... (apparently) */
  290. const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
  291. if (c == NULL) {
  292. /*
  293. * One positive side-effect of US's export control history,
  294. * is that we should at least be able to avoid using US
  295. * misspellings of "initialisation"?
  296. */
  297. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  298. return 0;
  299. }
  300. /* We'll use the ENGINE's private cipher definition */
  301. cipher = c;
  302. /*
  303. * Store the ENGINE functional reference so we know 'cipher' came
  304. * from an ENGINE and we need to release it when done.
  305. */
  306. ctx->engine = impl;
  307. } else {
  308. ctx->engine = NULL;
  309. }
  310. #endif
  311. ctx->cipher = cipher;
  312. if (ctx->cipher->ctx_size) {
  313. ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
  314. if (ctx->cipher_data == NULL) {
  315. ctx->cipher = NULL;
  316. return 0;
  317. }
  318. } else {
  319. ctx->cipher_data = NULL;
  320. }
  321. ctx->key_len = cipher->key_len;
  322. /* Preserve wrap enable flag, zero everything else */
  323. ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
  324. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  325. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL) <= 0) {
  326. ctx->cipher = NULL;
  327. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  328. return 0;
  329. }
  330. }
  331. }
  332. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  333. skip_to_init:
  334. #endif
  335. if (ctx->cipher == NULL)
  336. return 0;
  337. /* we assume block size is a power of 2 in *cryptUpdate */
  338. OPENSSL_assert(ctx->cipher->block_size == 1
  339. || ctx->cipher->block_size == 8
  340. || ctx->cipher->block_size == 16);
  341. if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
  342. && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
  343. ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
  344. return 0;
  345. }
  346. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
  347. & EVP_CIPH_CUSTOM_IV) == 0) {
  348. switch (EVP_CIPHER_CTX_get_mode(ctx)) {
  349. case EVP_CIPH_STREAM_CIPHER:
  350. case EVP_CIPH_ECB_MODE:
  351. break;
  352. case EVP_CIPH_CFB_MODE:
  353. case EVP_CIPH_OFB_MODE:
  354. ctx->num = 0;
  355. /* fall-through */
  356. case EVP_CIPH_CBC_MODE:
  357. n = EVP_CIPHER_CTX_get_iv_length(ctx);
  358. if (n < 0 || n > (int)sizeof(ctx->iv)) {
  359. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
  360. return 0;
  361. }
  362. if (iv != NULL)
  363. memcpy(ctx->oiv, iv, n);
  364. memcpy(ctx->iv, ctx->oiv, n);
  365. break;
  366. case EVP_CIPH_CTR_MODE:
  367. ctx->num = 0;
  368. /* Don't reuse IV for CTR mode */
  369. if (iv != NULL) {
  370. n = EVP_CIPHER_CTX_get_iv_length(ctx);
  371. if (n <= 0 || n > (int)sizeof(ctx->iv)) {
  372. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
  373. return 0;
  374. }
  375. memcpy(ctx->iv, iv, n);
  376. }
  377. break;
  378. default:
  379. return 0;
  380. }
  381. }
  382. if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  383. if (!ctx->cipher->init(ctx, key, iv, enc))
  384. return 0;
  385. }
  386. ctx->buf_len = 0;
  387. ctx->final_used = 0;
  388. ctx->block_mask = ctx->cipher->block_size - 1;
  389. return 1;
  390. }
  391. int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  392. const unsigned char *key, const unsigned char *iv,
  393. int enc, const OSSL_PARAM params[])
  394. {
  395. return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
  396. }
  397. int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  398. const unsigned char *key, const unsigned char *iv, int enc)
  399. {
  400. if (cipher != NULL)
  401. EVP_CIPHER_CTX_reset(ctx);
  402. return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
  403. }
  404. int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  405. ENGINE *impl, const unsigned char *key,
  406. const unsigned char *iv, int enc)
  407. {
  408. return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
  409. }
  410. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  411. const unsigned char *in, int inl)
  412. {
  413. if (ctx->encrypt)
  414. return EVP_EncryptUpdate(ctx, out, outl, in, inl);
  415. else
  416. return EVP_DecryptUpdate(ctx, out, outl, in, inl);
  417. }
  418. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  419. {
  420. if (ctx->encrypt)
  421. return EVP_EncryptFinal_ex(ctx, out, outl);
  422. else
  423. return EVP_DecryptFinal_ex(ctx, out, outl);
  424. }
  425. int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  426. {
  427. if (ctx->encrypt)
  428. return EVP_EncryptFinal(ctx, out, outl);
  429. else
  430. return EVP_DecryptFinal(ctx, out, outl);
  431. }
  432. int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  433. const unsigned char *key, const unsigned char *iv)
  434. {
  435. return EVP_CipherInit(ctx, cipher, key, iv, 1);
  436. }
  437. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  438. ENGINE *impl, const unsigned char *key,
  439. const unsigned char *iv)
  440. {
  441. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  442. }
  443. int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  444. const unsigned char *key, const unsigned char *iv,
  445. const OSSL_PARAM params[])
  446. {
  447. return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
  448. }
  449. int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  450. const unsigned char *key, const unsigned char *iv)
  451. {
  452. return EVP_CipherInit(ctx, cipher, key, iv, 0);
  453. }
  454. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  455. ENGINE *impl, const unsigned char *key,
  456. const unsigned char *iv)
  457. {
  458. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  459. }
  460. int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  461. const unsigned char *key, const unsigned char *iv,
  462. const OSSL_PARAM params[])
  463. {
  464. return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
  465. }
  466. /*
  467. * According to the letter of standard difference between pointers
  468. * is specified to be valid only within same object. This makes
  469. * it formally challenging to determine if input and output buffers
  470. * are not partially overlapping with standard pointer arithmetic.
  471. */
  472. #ifdef PTRDIFF_T
  473. # undef PTRDIFF_T
  474. #endif
  475. #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
  476. /*
  477. * Then we have VMS that distinguishes itself by adhering to
  478. * sizeof(size_t)==4 even in 64-bit builds, which means that
  479. * difference between two pointers might be truncated to 32 bits.
  480. * In the context one can even wonder how comparison for
  481. * equality is implemented. To be on the safe side we adhere to
  482. * PTRDIFF_T even for comparison for equality.
  483. */
  484. # define PTRDIFF_T uint64_t
  485. #else
  486. # define PTRDIFF_T size_t
  487. #endif
  488. int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
  489. {
  490. PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
  491. /*
  492. * Check for partially overlapping buffers. [Binary logical
  493. * operations are used instead of boolean to minimize number
  494. * of conditional branches.]
  495. */
  496. int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
  497. (diff > (0 - (PTRDIFF_T)len)));
  498. return overlapped;
  499. }
  500. static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
  501. unsigned char *out, int *outl,
  502. const unsigned char *in, int inl)
  503. {
  504. int i, j, bl, cmpl = inl;
  505. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  506. cmpl = safe_div_round_up_int(cmpl, 8, NULL);
  507. bl = ctx->cipher->block_size;
  508. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  509. /* If block size > 1 then the cipher will have to do this check */
  510. if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
  511. ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
  512. return 0;
  513. }
  514. i = ctx->cipher->do_cipher(ctx, out, in, inl);
  515. if (i < 0)
  516. return 0;
  517. else
  518. *outl = i;
  519. return 1;
  520. }
  521. if (inl <= 0) {
  522. *outl = 0;
  523. return inl == 0;
  524. }
  525. if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
  526. ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
  527. return 0;
  528. }
  529. if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
  530. if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
  531. *outl = inl;
  532. return 1;
  533. } else {
  534. *outl = 0;
  535. return 0;
  536. }
  537. }
  538. i = ctx->buf_len;
  539. OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
  540. if (i != 0) {
  541. if (bl - i > inl) {
  542. memcpy(&(ctx->buf[i]), in, inl);
  543. ctx->buf_len += inl;
  544. *outl = 0;
  545. return 1;
  546. } else {
  547. j = bl - i;
  548. /*
  549. * Once we've processed the first j bytes from in, the amount of
  550. * data left that is a multiple of the block length is:
  551. * (inl - j) & ~(bl - 1)
  552. * We must ensure that this amount of data, plus the one block that
  553. * we process from ctx->buf does not exceed INT_MAX
  554. */
  555. if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
  556. ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
  557. return 0;
  558. }
  559. memcpy(&(ctx->buf[i]), in, j);
  560. inl -= j;
  561. in += j;
  562. if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
  563. return 0;
  564. out += bl;
  565. *outl = bl;
  566. }
  567. } else
  568. *outl = 0;
  569. i = inl & (bl - 1);
  570. inl -= i;
  571. if (inl > 0) {
  572. if (!ctx->cipher->do_cipher(ctx, out, in, inl))
  573. return 0;
  574. *outl += inl;
  575. }
  576. if (i != 0)
  577. memcpy(ctx->buf, &(in[inl]), i);
  578. ctx->buf_len = i;
  579. return 1;
  580. }
  581. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  582. const unsigned char *in, int inl)
  583. {
  584. int ret;
  585. size_t soutl, inl_ = (size_t)inl;
  586. int blocksize;
  587. if (ossl_likely(outl != NULL)) {
  588. *outl = 0;
  589. } else {
  590. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  591. return 0;
  592. }
  593. /* Prevent accidental use of decryption context when encrypting */
  594. if (ossl_unlikely(!ctx->encrypt)) {
  595. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  596. return 0;
  597. }
  598. if (ossl_unlikely(ctx->cipher == NULL)) {
  599. ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
  600. return 0;
  601. }
  602. if (ossl_unlikely(ctx->cipher->prov == NULL))
  603. goto legacy;
  604. blocksize = ctx->cipher->block_size;
  605. if (ossl_unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) {
  606. ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
  607. return 0;
  608. }
  609. ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
  610. inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
  611. in, inl_);
  612. if (ossl_likely(ret)) {
  613. if (soutl > INT_MAX) {
  614. ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
  615. return 0;
  616. }
  617. *outl = soutl;
  618. }
  619. return ret;
  620. /* Code below to be removed when legacy support is dropped. */
  621. legacy:
  622. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  623. }
  624. int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  625. {
  626. int ret;
  627. ret = EVP_EncryptFinal_ex(ctx, out, outl);
  628. return ret;
  629. }
  630. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  631. {
  632. int n, ret;
  633. unsigned int i, b, bl;
  634. size_t soutl;
  635. int blocksize;
  636. if (outl != NULL) {
  637. *outl = 0;
  638. } else {
  639. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  640. return 0;
  641. }
  642. /* Prevent accidental use of decryption context when encrypting */
  643. if (!ctx->encrypt) {
  644. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  645. return 0;
  646. }
  647. if (ctx->cipher == NULL) {
  648. ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
  649. return 0;
  650. }
  651. if (ctx->cipher->prov == NULL)
  652. goto legacy;
  653. blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
  654. if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
  655. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  656. return 0;
  657. }
  658. ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
  659. blocksize == 1 ? 0 : blocksize);
  660. if (ret) {
  661. if (soutl > INT_MAX) {
  662. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  663. return 0;
  664. }
  665. *outl = soutl;
  666. }
  667. return ret;
  668. /* Code below to be removed when legacy support is dropped. */
  669. legacy:
  670. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  671. ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  672. if (ret < 0)
  673. return 0;
  674. else
  675. *outl = ret;
  676. return 1;
  677. }
  678. b = ctx->cipher->block_size;
  679. OPENSSL_assert(b <= sizeof(ctx->buf));
  680. if (b == 1) {
  681. *outl = 0;
  682. return 1;
  683. }
  684. bl = ctx->buf_len;
  685. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  686. if (bl) {
  687. ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  688. return 0;
  689. }
  690. *outl = 0;
  691. return 1;
  692. }
  693. n = b - bl;
  694. for (i = bl; i < b; i++)
  695. ctx->buf[i] = n;
  696. ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
  697. if (ret)
  698. *outl = b;
  699. return ret;
  700. }
  701. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  702. const unsigned char *in, int inl)
  703. {
  704. int fix_len, cmpl = inl, ret;
  705. unsigned int b;
  706. size_t soutl, inl_ = (size_t)inl;
  707. int blocksize;
  708. if (ossl_likely(outl != NULL)) {
  709. *outl = 0;
  710. } else {
  711. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  712. return 0;
  713. }
  714. /* Prevent accidental use of encryption context when decrypting */
  715. if (ossl_unlikely(ctx->encrypt)) {
  716. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  717. return 0;
  718. }
  719. if (ossl_unlikely(ctx->cipher == NULL)) {
  720. ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
  721. return 0;
  722. }
  723. if (ossl_unlikely(ctx->cipher->prov == NULL))
  724. goto legacy;
  725. blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
  726. if (ossl_unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) {
  727. ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
  728. return 0;
  729. }
  730. ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
  731. inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
  732. in, inl_);
  733. if (ossl_likely(ret)) {
  734. if (soutl > INT_MAX) {
  735. ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
  736. return 0;
  737. }
  738. *outl = soutl;
  739. }
  740. return ret;
  741. /* Code below to be removed when legacy support is dropped. */
  742. legacy:
  743. b = ctx->cipher->block_size;
  744. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  745. cmpl = safe_div_round_up_int(cmpl, 8, NULL);
  746. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  747. if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
  748. ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
  749. return 0;
  750. }
  751. fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
  752. if (fix_len < 0) {
  753. *outl = 0;
  754. return 0;
  755. } else
  756. *outl = fix_len;
  757. return 1;
  758. }
  759. if (inl <= 0) {
  760. *outl = 0;
  761. return inl == 0;
  762. }
  763. if (ctx->flags & EVP_CIPH_NO_PADDING)
  764. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  765. OPENSSL_assert(b <= sizeof(ctx->final));
  766. if (ctx->final_used) {
  767. /* see comment about PTRDIFF_T comparison above */
  768. if (((PTRDIFF_T)out == (PTRDIFF_T)in)
  769. || ossl_is_partially_overlapping(out, in, b)) {
  770. ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
  771. return 0;
  772. }
  773. /*
  774. * final_used is only ever set if buf_len is 0. Therefore the maximum
  775. * length output we will ever see from evp_EncryptDecryptUpdate is
  776. * the maximum multiple of the block length that is <= inl, or just:
  777. * inl & ~(b - 1)
  778. * Since final_used has been set then the final output length is:
  779. * (inl & ~(b - 1)) + b
  780. * This must never exceed INT_MAX
  781. */
  782. if ((inl & ~(b - 1)) > INT_MAX - b) {
  783. ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
  784. return 0;
  785. }
  786. memcpy(out, ctx->final, b);
  787. out += b;
  788. fix_len = 1;
  789. } else
  790. fix_len = 0;
  791. if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
  792. return 0;
  793. /*
  794. * if we have 'decrypted' a multiple of block size, make sure we have a
  795. * copy of this last block
  796. */
  797. if (b > 1 && !ctx->buf_len) {
  798. *outl -= b;
  799. ctx->final_used = 1;
  800. memcpy(ctx->final, &out[*outl], b);
  801. } else
  802. ctx->final_used = 0;
  803. if (fix_len)
  804. *outl += b;
  805. return 1;
  806. }
  807. int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  808. {
  809. int ret;
  810. ret = EVP_DecryptFinal_ex(ctx, out, outl);
  811. return ret;
  812. }
  813. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  814. {
  815. int i, n;
  816. unsigned int b;
  817. size_t soutl;
  818. int ret;
  819. int blocksize;
  820. if (outl != NULL) {
  821. *outl = 0;
  822. } else {
  823. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  824. return 0;
  825. }
  826. /* Prevent accidental use of encryption context when decrypting */
  827. if (ctx->encrypt) {
  828. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  829. return 0;
  830. }
  831. if (ctx->cipher == NULL) {
  832. ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
  833. return 0;
  834. }
  835. if (ctx->cipher->prov == NULL)
  836. goto legacy;
  837. blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
  838. if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
  839. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  840. return 0;
  841. }
  842. ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
  843. blocksize == 1 ? 0 : blocksize);
  844. if (ret) {
  845. if (soutl > INT_MAX) {
  846. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  847. return 0;
  848. }
  849. *outl = soutl;
  850. }
  851. return ret;
  852. /* Code below to be removed when legacy support is dropped. */
  853. legacy:
  854. *outl = 0;
  855. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  856. i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  857. if (i < 0)
  858. return 0;
  859. else
  860. *outl = i;
  861. return 1;
  862. }
  863. b = ctx->cipher->block_size;
  864. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  865. if (ctx->buf_len) {
  866. ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  867. return 0;
  868. }
  869. *outl = 0;
  870. return 1;
  871. }
  872. if (b > 1) {
  873. if (ctx->buf_len || !ctx->final_used) {
  874. ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
  875. return 0;
  876. }
  877. OPENSSL_assert(b <= sizeof(ctx->final));
  878. /*
  879. * The following assumes that the ciphertext has been authenticated.
  880. * Otherwise it provides a padding oracle.
  881. */
  882. n = ctx->final[b - 1];
  883. if (n == 0 || n > (int)b) {
  884. ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
  885. return 0;
  886. }
  887. for (i = 0; i < n; i++) {
  888. if (ctx->final[--b] != n) {
  889. ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
  890. return 0;
  891. }
  892. }
  893. n = ctx->cipher->block_size - n;
  894. for (i = 0; i < n; i++)
  895. out[i] = ctx->final[i];
  896. *outl = n;
  897. } else
  898. *outl = 0;
  899. return 1;
  900. }
  901. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
  902. {
  903. if (c->cipher->prov != NULL) {
  904. int ok;
  905. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  906. size_t len;
  907. if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
  908. return 1;
  909. /* Check the cipher actually understands this parameter */
  910. if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
  911. OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
  912. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
  913. return 0;
  914. }
  915. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
  916. if (!OSSL_PARAM_set_int(params, keylen))
  917. return 0;
  918. ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
  919. if (ok <= 0)
  920. return 0;
  921. c->key_len = keylen;
  922. return 1;
  923. }
  924. /* Code below to be removed when legacy support is dropped. */
  925. /*
  926. * Note there have never been any built-in ciphers that define this flag
  927. * since it was first introduced.
  928. */
  929. if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
  930. return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
  931. if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
  932. return 1;
  933. if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  934. c->key_len = keylen;
  935. return 1;
  936. }
  937. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
  938. return 0;
  939. }
  940. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
  941. {
  942. int ok;
  943. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  944. unsigned int pd = pad;
  945. if (pad)
  946. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  947. else
  948. ctx->flags |= EVP_CIPH_NO_PADDING;
  949. if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
  950. return 1;
  951. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
  952. ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  953. return ok != 0;
  954. }
  955. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  956. {
  957. int ret = EVP_CTRL_RET_UNSUPPORTED;
  958. int set_params = 1;
  959. size_t sz = arg;
  960. unsigned int i;
  961. OSSL_PARAM params[4] = {
  962. OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
  963. };
  964. if (ctx == NULL || ctx->cipher == NULL) {
  965. ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
  966. return 0;
  967. }
  968. if (ctx->cipher->prov == NULL)
  969. goto legacy;
  970. switch (type) {
  971. case EVP_CTRL_SET_KEY_LENGTH:
  972. if (arg < 0)
  973. return 0;
  974. if (ctx->key_len == arg)
  975. /* Skip calling into provider if unchanged. */
  976. return 1;
  977. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
  978. ctx->key_len = -1;
  979. break;
  980. case EVP_CTRL_RAND_KEY: /* Used by DES */
  981. set_params = 0;
  982. params[0] =
  983. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
  984. ptr, sz);
  985. break;
  986. case EVP_CTRL_INIT:
  987. /*
  988. * EVP_CTRL_INIT is purely legacy, no provider counterpart.
  989. * As a matter of fact, this should be dead code, but some caller
  990. * might still do a direct control call with this command, so...
  991. * Legacy methods return 1 except for exceptional circumstances, so
  992. * we do the same here to not be disruptive.
  993. */
  994. return 1;
  995. case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
  996. default:
  997. goto end;
  998. case EVP_CTRL_AEAD_SET_IVLEN:
  999. if (arg < 0)
  1000. return 0;
  1001. if (ctx->iv_len == arg)
  1002. /* Skip calling into provider if unchanged. */
  1003. return 1;
  1004. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
  1005. ctx->iv_len = -1;
  1006. break;
  1007. case EVP_CTRL_CCM_SET_L:
  1008. if (arg < 2 || arg > 8)
  1009. return 0;
  1010. sz = 15 - arg;
  1011. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
  1012. ctx->iv_len = -1;
  1013. break;
  1014. case EVP_CTRL_AEAD_SET_IV_FIXED:
  1015. params[0] = OSSL_PARAM_construct_octet_string(
  1016. OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
  1017. break;
  1018. case EVP_CTRL_GCM_IV_GEN:
  1019. set_params = 0;
  1020. if (arg < 0)
  1021. sz = 0; /* special case that uses the iv length */
  1022. params[0] = OSSL_PARAM_construct_octet_string(
  1023. OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
  1024. break;
  1025. case EVP_CTRL_GCM_SET_IV_INV:
  1026. if (arg < 0)
  1027. return 0;
  1028. params[0] = OSSL_PARAM_construct_octet_string(
  1029. OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
  1030. break;
  1031. case EVP_CTRL_GET_RC5_ROUNDS:
  1032. set_params = 0; /* Fall thru */
  1033. case EVP_CTRL_SET_RC5_ROUNDS:
  1034. if (arg < 0)
  1035. return 0;
  1036. i = (unsigned int)arg;
  1037. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
  1038. break;
  1039. case EVP_CTRL_SET_SPEED:
  1040. if (arg < 0)
  1041. return 0;
  1042. i = (unsigned int)arg;
  1043. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
  1044. break;
  1045. case EVP_CTRL_AEAD_GET_TAG:
  1046. set_params = 0; /* Fall thru */
  1047. case EVP_CTRL_AEAD_SET_TAG:
  1048. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  1049. ptr, sz);
  1050. break;
  1051. case EVP_CTRL_AEAD_TLS1_AAD:
  1052. /* This one does a set and a get - since it returns a size */
  1053. params[0] =
  1054. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
  1055. ptr, sz);
  1056. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1057. if (ret <= 0)
  1058. goto end;
  1059. params[0] =
  1060. OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
  1061. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1062. if (ret <= 0)
  1063. goto end;
  1064. return sz;
  1065. #ifndef OPENSSL_NO_RC2
  1066. case EVP_CTRL_GET_RC2_KEY_BITS:
  1067. set_params = 0; /* Fall thru */
  1068. case EVP_CTRL_SET_RC2_KEY_BITS:
  1069. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
  1070. break;
  1071. #endif /* OPENSSL_NO_RC2 */
  1072. #if !defined(OPENSSL_NO_MULTIBLOCK)
  1073. case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
  1074. params[0] = OSSL_PARAM_construct_size_t(
  1075. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
  1076. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1077. if (ret <= 0)
  1078. return 0;
  1079. params[0] = OSSL_PARAM_construct_size_t(
  1080. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
  1081. params[1] = OSSL_PARAM_construct_end();
  1082. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1083. if (ret <= 0)
  1084. return 0;
  1085. return sz;
  1086. case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
  1087. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
  1088. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
  1089. if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
  1090. return 0;
  1091. params[0] = OSSL_PARAM_construct_octet_string(
  1092. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
  1093. params[1] = OSSL_PARAM_construct_uint(
  1094. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
  1095. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1096. if (ret <= 0)
  1097. return ret;
  1098. /* Retrieve the return values changed by the set */
  1099. params[0] = OSSL_PARAM_construct_size_t(
  1100. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
  1101. params[1] = OSSL_PARAM_construct_uint(
  1102. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
  1103. params[2] = OSSL_PARAM_construct_end();
  1104. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1105. if (ret <= 0)
  1106. return 0;
  1107. return sz;
  1108. }
  1109. case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
  1110. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
  1111. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
  1112. params[0] = OSSL_PARAM_construct_octet_string(
  1113. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
  1114. params[1] = OSSL_PARAM_construct_octet_string(
  1115. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
  1116. p->len);
  1117. params[2] = OSSL_PARAM_construct_uint(
  1118. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
  1119. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1120. if (ret <= 0)
  1121. return ret;
  1122. params[0] = OSSL_PARAM_construct_size_t(
  1123. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
  1124. params[1] = OSSL_PARAM_construct_end();
  1125. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1126. if (ret <= 0)
  1127. return 0;
  1128. return sz;
  1129. }
  1130. #endif /* OPENSSL_NO_MULTIBLOCK */
  1131. case EVP_CTRL_AEAD_SET_MAC_KEY:
  1132. if (arg < 0)
  1133. return -1;
  1134. params[0] = OSSL_PARAM_construct_octet_string(
  1135. OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
  1136. break;
  1137. }
  1138. if (set_params)
  1139. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1140. else
  1141. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1142. goto end;
  1143. /* Code below to be removed when legacy support is dropped. */
  1144. legacy:
  1145. if (ctx->cipher->ctrl == NULL) {
  1146. ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
  1147. return 0;
  1148. }
  1149. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  1150. end:
  1151. if (ret == EVP_CTRL_RET_UNSUPPORTED) {
  1152. ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  1153. return 0;
  1154. }
  1155. return ret;
  1156. }
  1157. int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
  1158. {
  1159. if (cipher != NULL && cipher->get_params != NULL)
  1160. return cipher->get_params(params);
  1161. return 0;
  1162. }
  1163. int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
  1164. {
  1165. int r = 0;
  1166. const OSSL_PARAM *p;
  1167. if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
  1168. r = ctx->cipher->set_ctx_params(ctx->algctx, params);
  1169. if (r > 0) {
  1170. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  1171. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
  1172. r = 0;
  1173. ctx->key_len = -1;
  1174. }
  1175. }
  1176. if (r > 0) {
  1177. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
  1178. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
  1179. r = 0;
  1180. ctx->iv_len = -1;
  1181. }
  1182. }
  1183. }
  1184. return r;
  1185. }
  1186. int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
  1187. {
  1188. if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
  1189. return ctx->cipher->get_ctx_params(ctx->algctx, params);
  1190. return 0;
  1191. }
  1192. const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
  1193. {
  1194. if (cipher != NULL && cipher->gettable_params != NULL)
  1195. return cipher->gettable_params(
  1196. ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
  1197. return NULL;
  1198. }
  1199. const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
  1200. {
  1201. void *provctx;
  1202. if (cipher != NULL && cipher->settable_ctx_params != NULL) {
  1203. provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
  1204. return cipher->settable_ctx_params(NULL, provctx);
  1205. }
  1206. return NULL;
  1207. }
  1208. const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
  1209. {
  1210. void *provctx;
  1211. if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
  1212. provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
  1213. return cipher->gettable_ctx_params(NULL, provctx);
  1214. }
  1215. return NULL;
  1216. }
  1217. const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
  1218. {
  1219. void *alg;
  1220. if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
  1221. alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
  1222. return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
  1223. }
  1224. return NULL;
  1225. }
  1226. const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
  1227. {
  1228. void *provctx;
  1229. if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
  1230. provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
  1231. return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
  1232. }
  1233. return NULL;
  1234. }
  1235. #ifndef FIPS_MODULE
  1236. static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
  1237. {
  1238. const EVP_CIPHER *cipher = ctx->cipher;
  1239. const OSSL_PROVIDER *prov;
  1240. if (cipher == NULL)
  1241. return NULL;
  1242. prov = EVP_CIPHER_get0_provider(cipher);
  1243. return ossl_provider_libctx(prov);
  1244. }
  1245. #endif
  1246. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  1247. {
  1248. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  1249. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  1250. #ifdef FIPS_MODULE
  1251. return 0;
  1252. #else
  1253. {
  1254. int kl;
  1255. OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
  1256. kl = EVP_CIPHER_CTX_get_key_length(ctx);
  1257. if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
  1258. return 0;
  1259. return 1;
  1260. }
  1261. #endif /* FIPS_MODULE */
  1262. }
  1263. EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
  1264. {
  1265. EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
  1266. if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
  1267. EVP_CIPHER_CTX_free(out);
  1268. out = NULL;
  1269. }
  1270. return out;
  1271. }
  1272. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  1273. {
  1274. if ((in == NULL) || (in->cipher == NULL)) {
  1275. ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
  1276. return 0;
  1277. }
  1278. if (in->cipher->prov == NULL)
  1279. goto legacy;
  1280. if (in->cipher->dupctx == NULL) {
  1281. ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
  1282. return 0;
  1283. }
  1284. EVP_CIPHER_CTX_reset(out);
  1285. *out = *in;
  1286. out->algctx = NULL;
  1287. if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
  1288. out->fetched_cipher = NULL;
  1289. return 0;
  1290. }
  1291. out->algctx = in->cipher->dupctx(in->algctx);
  1292. if (out->algctx == NULL) {
  1293. ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
  1294. return 0;
  1295. }
  1296. return 1;
  1297. /* Code below to be removed when legacy support is dropped. */
  1298. legacy:
  1299. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  1300. /* Make sure it's safe to copy a cipher context using an ENGINE */
  1301. if (in->engine && !ENGINE_init(in->engine)) {
  1302. ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
  1303. return 0;
  1304. }
  1305. #endif
  1306. EVP_CIPHER_CTX_reset(out);
  1307. memcpy(out, in, sizeof(*out));
  1308. if (in->cipher_data && in->cipher->ctx_size) {
  1309. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  1310. if (out->cipher_data == NULL) {
  1311. out->cipher = NULL;
  1312. return 0;
  1313. }
  1314. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  1315. }
  1316. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  1317. if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
  1318. out->cipher = NULL;
  1319. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  1320. return 0;
  1321. }
  1322. return 1;
  1323. }
  1324. EVP_CIPHER *evp_cipher_new(void)
  1325. {
  1326. EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
  1327. if (cipher != NULL && !CRYPTO_NEW_REF(&cipher->refcnt, 1)) {
  1328. OPENSSL_free(cipher);
  1329. return NULL;
  1330. }
  1331. return cipher;
  1332. }
  1333. /*
  1334. * FIPS module note: since internal fetches will be entirely
  1335. * provider based, we know that none of its code depends on legacy
  1336. * NIDs or any functionality that use them.
  1337. */
  1338. #ifndef FIPS_MODULE
  1339. /* After removal of legacy support get rid of the need for legacy NIDs */
  1340. static void set_legacy_nid(const char *name, void *vlegacy_nid)
  1341. {
  1342. int nid;
  1343. int *legacy_nid = vlegacy_nid;
  1344. /*
  1345. * We use lowest level function to get the associated method, because
  1346. * higher level functions such as EVP_get_cipherbyname() have changed
  1347. * to look at providers too.
  1348. */
  1349. const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
  1350. if (*legacy_nid == -1) /* We found a clash already */
  1351. return;
  1352. if (legacy_method == NULL)
  1353. return;
  1354. nid = EVP_CIPHER_get_nid(legacy_method);
  1355. if (*legacy_nid != NID_undef && *legacy_nid != nid) {
  1356. *legacy_nid = -1;
  1357. return;
  1358. }
  1359. *legacy_nid = nid;
  1360. }
  1361. #endif
  1362. static void *evp_cipher_from_algorithm(const int name_id,
  1363. const OSSL_ALGORITHM *algodef,
  1364. OSSL_PROVIDER *prov)
  1365. {
  1366. const OSSL_DISPATCH *fns = algodef->implementation;
  1367. EVP_CIPHER *cipher = NULL;
  1368. int fnciphcnt = 0, fnctxcnt = 0;
  1369. if ((cipher = evp_cipher_new()) == NULL) {
  1370. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  1371. return NULL;
  1372. }
  1373. #ifndef FIPS_MODULE
  1374. cipher->nid = NID_undef;
  1375. if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
  1376. || cipher->nid == -1) {
  1377. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1378. EVP_CIPHER_free(cipher);
  1379. return NULL;
  1380. }
  1381. #endif
  1382. cipher->name_id = name_id;
  1383. if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  1384. EVP_CIPHER_free(cipher);
  1385. return NULL;
  1386. }
  1387. cipher->description = algodef->algorithm_description;
  1388. for (; fns->function_id != 0; fns++) {
  1389. switch (fns->function_id) {
  1390. case OSSL_FUNC_CIPHER_NEWCTX:
  1391. if (cipher->newctx != NULL)
  1392. break;
  1393. cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
  1394. fnctxcnt++;
  1395. break;
  1396. case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
  1397. if (cipher->einit != NULL)
  1398. break;
  1399. cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
  1400. fnciphcnt++;
  1401. break;
  1402. case OSSL_FUNC_CIPHER_DECRYPT_INIT:
  1403. if (cipher->dinit != NULL)
  1404. break;
  1405. cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
  1406. fnciphcnt++;
  1407. break;
  1408. case OSSL_FUNC_CIPHER_UPDATE:
  1409. if (cipher->cupdate != NULL)
  1410. break;
  1411. cipher->cupdate = OSSL_FUNC_cipher_update(fns);
  1412. fnciphcnt++;
  1413. break;
  1414. case OSSL_FUNC_CIPHER_FINAL:
  1415. if (cipher->cfinal != NULL)
  1416. break;
  1417. cipher->cfinal = OSSL_FUNC_cipher_final(fns);
  1418. fnciphcnt++;
  1419. break;
  1420. case OSSL_FUNC_CIPHER_CIPHER:
  1421. if (cipher->ccipher != NULL)
  1422. break;
  1423. cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
  1424. break;
  1425. case OSSL_FUNC_CIPHER_FREECTX:
  1426. if (cipher->freectx != NULL)
  1427. break;
  1428. cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
  1429. fnctxcnt++;
  1430. break;
  1431. case OSSL_FUNC_CIPHER_DUPCTX:
  1432. if (cipher->dupctx != NULL)
  1433. break;
  1434. cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
  1435. break;
  1436. case OSSL_FUNC_CIPHER_GET_PARAMS:
  1437. if (cipher->get_params != NULL)
  1438. break;
  1439. cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
  1440. break;
  1441. case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
  1442. if (cipher->get_ctx_params != NULL)
  1443. break;
  1444. cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
  1445. break;
  1446. case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
  1447. if (cipher->set_ctx_params != NULL)
  1448. break;
  1449. cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
  1450. break;
  1451. case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
  1452. if (cipher->gettable_params != NULL)
  1453. break;
  1454. cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
  1455. break;
  1456. case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
  1457. if (cipher->gettable_ctx_params != NULL)
  1458. break;
  1459. cipher->gettable_ctx_params =
  1460. OSSL_FUNC_cipher_gettable_ctx_params(fns);
  1461. break;
  1462. case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
  1463. if (cipher->settable_ctx_params != NULL)
  1464. break;
  1465. cipher->settable_ctx_params =
  1466. OSSL_FUNC_cipher_settable_ctx_params(fns);
  1467. break;
  1468. }
  1469. }
  1470. if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
  1471. || (fnciphcnt == 0 && cipher->ccipher == NULL)
  1472. || fnctxcnt != 2) {
  1473. /*
  1474. * In order to be a consistent set of functions we must have at least
  1475. * a complete set of "encrypt" functions, or a complete set of "decrypt"
  1476. * functions, or a single "cipher" function. In all cases we need both
  1477. * the "newctx" and "freectx" functions.
  1478. */
  1479. EVP_CIPHER_free(cipher);
  1480. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  1481. return NULL;
  1482. }
  1483. cipher->prov = prov;
  1484. if (prov != NULL)
  1485. ossl_provider_up_ref(prov);
  1486. if (!evp_cipher_cache_constants(cipher)) {
  1487. EVP_CIPHER_free(cipher);
  1488. ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
  1489. cipher = NULL;
  1490. }
  1491. return cipher;
  1492. }
  1493. static int evp_cipher_up_ref(void *cipher)
  1494. {
  1495. return EVP_CIPHER_up_ref(cipher);
  1496. }
  1497. static void evp_cipher_free(void *cipher)
  1498. {
  1499. EVP_CIPHER_free(cipher);
  1500. }
  1501. EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  1502. const char *properties)
  1503. {
  1504. EVP_CIPHER *cipher =
  1505. evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
  1506. evp_cipher_from_algorithm, evp_cipher_up_ref,
  1507. evp_cipher_free);
  1508. return cipher;
  1509. }
  1510. int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
  1511. {
  1512. int ref = 0;
  1513. if (cipher->origin == EVP_ORIG_DYNAMIC)
  1514. CRYPTO_UP_REF(&cipher->refcnt, &ref);
  1515. return 1;
  1516. }
  1517. void evp_cipher_free_int(EVP_CIPHER *cipher)
  1518. {
  1519. OPENSSL_free(cipher->type_name);
  1520. ossl_provider_free(cipher->prov);
  1521. CRYPTO_FREE_REF(&cipher->refcnt);
  1522. OPENSSL_free(cipher);
  1523. }
  1524. void EVP_CIPHER_free(EVP_CIPHER *cipher)
  1525. {
  1526. int i;
  1527. if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
  1528. return;
  1529. CRYPTO_DOWN_REF(&cipher->refcnt, &i);
  1530. if (i > 0)
  1531. return;
  1532. evp_cipher_free_int(cipher);
  1533. }
  1534. void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
  1535. void (*fn)(EVP_CIPHER *mac, void *arg),
  1536. void *arg)
  1537. {
  1538. evp_generic_do_all(libctx, OSSL_OP_CIPHER,
  1539. (void (*)(void *, void *))fn, arg,
  1540. evp_cipher_from_algorithm, evp_cipher_up_ref,
  1541. evp_cipher_free);
  1542. }