evp_enc.c 51 KB

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