evp_enc.c 51 KB

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