evp_enc.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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. if (arg < 0)
  937. return 0;
  938. if (ctx->key_len == arg)
  939. /* Skip calling into provider if unchanged. */
  940. return 1;
  941. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
  942. ctx->key_len = -1;
  943. break;
  944. case EVP_CTRL_RAND_KEY: /* Used by DES */
  945. set_params = 0;
  946. params[0] =
  947. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
  948. ptr, sz);
  949. break;
  950. case EVP_CTRL_INIT:
  951. /*
  952. * EVP_CTRL_INIT is purely legacy, no provider counterpart.
  953. * As a matter of fact, this should be dead code, but some caller
  954. * might still do a direct control call with this command, so...
  955. * Legacy methods return 1 except for exceptional circumstances, so
  956. * we do the same here to not be disruptive.
  957. */
  958. return 1;
  959. case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
  960. default:
  961. goto end;
  962. case EVP_CTRL_AEAD_SET_IVLEN:
  963. if (arg < 0)
  964. return 0;
  965. if (ctx->iv_len == arg)
  966. /* Skip calling into provider if unchanged. */
  967. return 1;
  968. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
  969. ctx->iv_len = -1;
  970. break;
  971. case EVP_CTRL_CCM_SET_L:
  972. if (arg < 2 || arg > 8)
  973. return 0;
  974. sz = 15 - arg;
  975. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
  976. ctx->iv_len = -1;
  977. break;
  978. case EVP_CTRL_AEAD_SET_IV_FIXED:
  979. params[0] = OSSL_PARAM_construct_octet_string(
  980. OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
  981. break;
  982. case EVP_CTRL_GCM_IV_GEN:
  983. set_params = 0;
  984. if (arg < 0)
  985. sz = 0; /* special case that uses the iv length */
  986. params[0] = OSSL_PARAM_construct_octet_string(
  987. OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
  988. break;
  989. case EVP_CTRL_GCM_SET_IV_INV:
  990. if (arg < 0)
  991. return 0;
  992. params[0] = OSSL_PARAM_construct_octet_string(
  993. OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
  994. break;
  995. case EVP_CTRL_GET_RC5_ROUNDS:
  996. set_params = 0; /* Fall thru */
  997. case EVP_CTRL_SET_RC5_ROUNDS:
  998. if (arg < 0)
  999. return 0;
  1000. i = (unsigned int)arg;
  1001. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
  1002. break;
  1003. case EVP_CTRL_SET_SPEED:
  1004. if (arg < 0)
  1005. return 0;
  1006. i = (unsigned int)arg;
  1007. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
  1008. break;
  1009. case EVP_CTRL_AEAD_GET_TAG:
  1010. set_params = 0; /* Fall thru */
  1011. case EVP_CTRL_AEAD_SET_TAG:
  1012. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  1013. ptr, sz);
  1014. break;
  1015. case EVP_CTRL_AEAD_TLS1_AAD:
  1016. /* This one does a set and a get - since it returns a size */
  1017. params[0] =
  1018. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
  1019. ptr, sz);
  1020. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1021. if (ret <= 0)
  1022. goto end;
  1023. params[0] =
  1024. OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
  1025. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1026. if (ret <= 0)
  1027. goto end;
  1028. return sz;
  1029. #ifndef OPENSSL_NO_RC2
  1030. case EVP_CTRL_GET_RC2_KEY_BITS:
  1031. set_params = 0; /* Fall thru */
  1032. case EVP_CTRL_SET_RC2_KEY_BITS:
  1033. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
  1034. break;
  1035. #endif /* OPENSSL_NO_RC2 */
  1036. #if !defined(OPENSSL_NO_MULTIBLOCK)
  1037. case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
  1038. params[0] = OSSL_PARAM_construct_size_t(
  1039. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
  1040. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1041. if (ret <= 0)
  1042. return 0;
  1043. params[0] = OSSL_PARAM_construct_size_t(
  1044. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
  1045. params[1] = OSSL_PARAM_construct_end();
  1046. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1047. if (ret <= 0)
  1048. return 0;
  1049. return sz;
  1050. case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
  1051. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
  1052. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
  1053. if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
  1054. return 0;
  1055. params[0] = OSSL_PARAM_construct_octet_string(
  1056. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
  1057. params[1] = OSSL_PARAM_construct_uint(
  1058. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
  1059. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1060. if (ret <= 0)
  1061. return ret;
  1062. /* Retrieve the return values changed by the set */
  1063. params[0] = OSSL_PARAM_construct_size_t(
  1064. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
  1065. params[1] = OSSL_PARAM_construct_uint(
  1066. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
  1067. params[2] = OSSL_PARAM_construct_end();
  1068. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1069. if (ret <= 0)
  1070. return 0;
  1071. return sz;
  1072. }
  1073. case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
  1074. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
  1075. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
  1076. params[0] = OSSL_PARAM_construct_octet_string(
  1077. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
  1078. params[1] = OSSL_PARAM_construct_octet_string(
  1079. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
  1080. p->len);
  1081. params[2] = OSSL_PARAM_construct_uint(
  1082. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
  1083. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1084. if (ret <= 0)
  1085. return ret;
  1086. params[0] = OSSL_PARAM_construct_size_t(
  1087. OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
  1088. params[1] = OSSL_PARAM_construct_end();
  1089. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1090. if (ret <= 0)
  1091. return 0;
  1092. return sz;
  1093. }
  1094. #endif /* OPENSSL_NO_MULTIBLOCK */
  1095. case EVP_CTRL_AEAD_SET_MAC_KEY:
  1096. if (arg < 0)
  1097. return -1;
  1098. params[0] = OSSL_PARAM_construct_octet_string(
  1099. OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
  1100. break;
  1101. }
  1102. if (set_params)
  1103. ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  1104. else
  1105. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  1106. goto end;
  1107. /* Code below to be removed when legacy support is dropped. */
  1108. legacy:
  1109. if (ctx->cipher->ctrl == NULL) {
  1110. ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
  1111. return 0;
  1112. }
  1113. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  1114. end:
  1115. if (ret == EVP_CTRL_RET_UNSUPPORTED) {
  1116. ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  1117. return 0;
  1118. }
  1119. return ret;
  1120. }
  1121. int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
  1122. {
  1123. if (cipher != NULL && cipher->get_params != NULL)
  1124. return cipher->get_params(params);
  1125. return 0;
  1126. }
  1127. int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
  1128. {
  1129. int r = 0;
  1130. const OSSL_PARAM *p;
  1131. if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
  1132. r = ctx->cipher->set_ctx_params(ctx->algctx, params);
  1133. if (r > 0) {
  1134. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  1135. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
  1136. r = 0;
  1137. ctx->key_len = -1;
  1138. }
  1139. }
  1140. if (r > 0) {
  1141. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
  1142. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
  1143. r = 0;
  1144. ctx->iv_len = -1;
  1145. }
  1146. }
  1147. }
  1148. return r;
  1149. }
  1150. int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
  1151. {
  1152. if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
  1153. return ctx->cipher->get_ctx_params(ctx->algctx, params);
  1154. return 0;
  1155. }
  1156. const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
  1157. {
  1158. if (cipher != NULL && cipher->gettable_params != NULL)
  1159. return cipher->gettable_params(
  1160. ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
  1161. return NULL;
  1162. }
  1163. const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
  1164. {
  1165. void *provctx;
  1166. if (cipher != NULL && cipher->settable_ctx_params != NULL) {
  1167. provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
  1168. return cipher->settable_ctx_params(NULL, provctx);
  1169. }
  1170. return NULL;
  1171. }
  1172. const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
  1173. {
  1174. void *provctx;
  1175. if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
  1176. provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
  1177. return cipher->gettable_ctx_params(NULL, provctx);
  1178. }
  1179. return NULL;
  1180. }
  1181. const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
  1182. {
  1183. void *alg;
  1184. if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
  1185. alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
  1186. return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
  1187. }
  1188. return NULL;
  1189. }
  1190. const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
  1191. {
  1192. void *provctx;
  1193. if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
  1194. provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
  1195. return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
  1196. }
  1197. return NULL;
  1198. }
  1199. #ifndef FIPS_MODULE
  1200. static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
  1201. {
  1202. const EVP_CIPHER *cipher = ctx->cipher;
  1203. const OSSL_PROVIDER *prov;
  1204. if (cipher == NULL)
  1205. return NULL;
  1206. prov = EVP_CIPHER_get0_provider(cipher);
  1207. return ossl_provider_libctx(prov);
  1208. }
  1209. #endif
  1210. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  1211. {
  1212. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  1213. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  1214. #ifdef FIPS_MODULE
  1215. return 0;
  1216. #else
  1217. {
  1218. int kl;
  1219. OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
  1220. kl = EVP_CIPHER_CTX_get_key_length(ctx);
  1221. if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
  1222. return 0;
  1223. return 1;
  1224. }
  1225. #endif /* FIPS_MODULE */
  1226. }
  1227. EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
  1228. {
  1229. EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
  1230. if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
  1231. EVP_CIPHER_CTX_free(out);
  1232. out = NULL;
  1233. }
  1234. return out;
  1235. }
  1236. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  1237. {
  1238. if ((in == NULL) || (in->cipher == NULL)) {
  1239. ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
  1240. return 0;
  1241. }
  1242. if (in->cipher->prov == NULL)
  1243. goto legacy;
  1244. if (in->cipher->dupctx == NULL) {
  1245. ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
  1246. return 0;
  1247. }
  1248. EVP_CIPHER_CTX_reset(out);
  1249. *out = *in;
  1250. out->algctx = NULL;
  1251. if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
  1252. out->fetched_cipher = NULL;
  1253. return 0;
  1254. }
  1255. out->algctx = in->cipher->dupctx(in->algctx);
  1256. if (out->algctx == NULL) {
  1257. ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
  1258. return 0;
  1259. }
  1260. return 1;
  1261. /* Code below to be removed when legacy support is dropped. */
  1262. legacy:
  1263. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  1264. /* Make sure it's safe to copy a cipher context using an ENGINE */
  1265. if (in->engine && !ENGINE_init(in->engine)) {
  1266. ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
  1267. return 0;
  1268. }
  1269. #endif
  1270. EVP_CIPHER_CTX_reset(out);
  1271. memcpy(out, in, sizeof(*out));
  1272. if (in->cipher_data && in->cipher->ctx_size) {
  1273. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  1274. if (out->cipher_data == NULL) {
  1275. out->cipher = NULL;
  1276. return 0;
  1277. }
  1278. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  1279. }
  1280. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  1281. if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
  1282. out->cipher = NULL;
  1283. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  1284. return 0;
  1285. }
  1286. return 1;
  1287. }
  1288. EVP_CIPHER *evp_cipher_new(void)
  1289. {
  1290. EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
  1291. if (cipher != NULL) {
  1292. cipher->lock = CRYPTO_THREAD_lock_new();
  1293. if (cipher->lock == NULL) {
  1294. OPENSSL_free(cipher);
  1295. return NULL;
  1296. }
  1297. cipher->refcnt = 1;
  1298. }
  1299. return cipher;
  1300. }
  1301. /*
  1302. * FIPS module note: since internal fetches will be entirely
  1303. * provider based, we know that none of its code depends on legacy
  1304. * NIDs or any functionality that use them.
  1305. */
  1306. #ifndef FIPS_MODULE
  1307. /* After removal of legacy support get rid of the need for legacy NIDs */
  1308. static void set_legacy_nid(const char *name, void *vlegacy_nid)
  1309. {
  1310. int nid;
  1311. int *legacy_nid = vlegacy_nid;
  1312. /*
  1313. * We use lowest level function to get the associated method, because
  1314. * higher level functions such as EVP_get_cipherbyname() have changed
  1315. * to look at providers too.
  1316. */
  1317. const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
  1318. if (*legacy_nid == -1) /* We found a clash already */
  1319. return;
  1320. if (legacy_method == NULL)
  1321. return;
  1322. nid = EVP_CIPHER_get_nid(legacy_method);
  1323. if (*legacy_nid != NID_undef && *legacy_nid != nid) {
  1324. *legacy_nid = -1;
  1325. return;
  1326. }
  1327. *legacy_nid = nid;
  1328. }
  1329. #endif
  1330. static void *evp_cipher_from_algorithm(const int name_id,
  1331. const OSSL_ALGORITHM *algodef,
  1332. OSSL_PROVIDER *prov)
  1333. {
  1334. const OSSL_DISPATCH *fns = algodef->implementation;
  1335. EVP_CIPHER *cipher = NULL;
  1336. int fnciphcnt = 0, fnctxcnt = 0;
  1337. if ((cipher = evp_cipher_new()) == NULL) {
  1338. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  1339. return NULL;
  1340. }
  1341. #ifndef FIPS_MODULE
  1342. cipher->nid = NID_undef;
  1343. if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
  1344. || cipher->nid == -1) {
  1345. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1346. EVP_CIPHER_free(cipher);
  1347. return NULL;
  1348. }
  1349. #endif
  1350. cipher->name_id = name_id;
  1351. if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  1352. EVP_CIPHER_free(cipher);
  1353. return NULL;
  1354. }
  1355. cipher->description = algodef->algorithm_description;
  1356. for (; fns->function_id != 0; fns++) {
  1357. switch (fns->function_id) {
  1358. case OSSL_FUNC_CIPHER_NEWCTX:
  1359. if (cipher->newctx != NULL)
  1360. break;
  1361. cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
  1362. fnctxcnt++;
  1363. break;
  1364. case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
  1365. if (cipher->einit != NULL)
  1366. break;
  1367. cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
  1368. fnciphcnt++;
  1369. break;
  1370. case OSSL_FUNC_CIPHER_DECRYPT_INIT:
  1371. if (cipher->dinit != NULL)
  1372. break;
  1373. cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
  1374. fnciphcnt++;
  1375. break;
  1376. case OSSL_FUNC_CIPHER_UPDATE:
  1377. if (cipher->cupdate != NULL)
  1378. break;
  1379. cipher->cupdate = OSSL_FUNC_cipher_update(fns);
  1380. fnciphcnt++;
  1381. break;
  1382. case OSSL_FUNC_CIPHER_FINAL:
  1383. if (cipher->cfinal != NULL)
  1384. break;
  1385. cipher->cfinal = OSSL_FUNC_cipher_final(fns);
  1386. fnciphcnt++;
  1387. break;
  1388. case OSSL_FUNC_CIPHER_CIPHER:
  1389. if (cipher->ccipher != NULL)
  1390. break;
  1391. cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
  1392. break;
  1393. case OSSL_FUNC_CIPHER_FREECTX:
  1394. if (cipher->freectx != NULL)
  1395. break;
  1396. cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
  1397. fnctxcnt++;
  1398. break;
  1399. case OSSL_FUNC_CIPHER_DUPCTX:
  1400. if (cipher->dupctx != NULL)
  1401. break;
  1402. cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
  1403. break;
  1404. case OSSL_FUNC_CIPHER_GET_PARAMS:
  1405. if (cipher->get_params != NULL)
  1406. break;
  1407. cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
  1408. break;
  1409. case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
  1410. if (cipher->get_ctx_params != NULL)
  1411. break;
  1412. cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
  1413. break;
  1414. case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
  1415. if (cipher->set_ctx_params != NULL)
  1416. break;
  1417. cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
  1418. break;
  1419. case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
  1420. if (cipher->gettable_params != NULL)
  1421. break;
  1422. cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
  1423. break;
  1424. case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
  1425. if (cipher->gettable_ctx_params != NULL)
  1426. break;
  1427. cipher->gettable_ctx_params =
  1428. OSSL_FUNC_cipher_gettable_ctx_params(fns);
  1429. break;
  1430. case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
  1431. if (cipher->settable_ctx_params != NULL)
  1432. break;
  1433. cipher->settable_ctx_params =
  1434. OSSL_FUNC_cipher_settable_ctx_params(fns);
  1435. break;
  1436. }
  1437. }
  1438. if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
  1439. || (fnciphcnt == 0 && cipher->ccipher == NULL)
  1440. || fnctxcnt != 2) {
  1441. /*
  1442. * In order to be a consistent set of functions we must have at least
  1443. * a complete set of "encrypt" functions, or a complete set of "decrypt"
  1444. * functions, or a single "cipher" function. In all cases we need both
  1445. * the "newctx" and "freectx" functions.
  1446. */
  1447. EVP_CIPHER_free(cipher);
  1448. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  1449. return NULL;
  1450. }
  1451. cipher->prov = prov;
  1452. if (prov != NULL)
  1453. ossl_provider_up_ref(prov);
  1454. if (!evp_cipher_cache_constants(cipher)) {
  1455. EVP_CIPHER_free(cipher);
  1456. ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
  1457. cipher = NULL;
  1458. }
  1459. return cipher;
  1460. }
  1461. static int evp_cipher_up_ref(void *cipher)
  1462. {
  1463. return EVP_CIPHER_up_ref(cipher);
  1464. }
  1465. static void evp_cipher_free(void *cipher)
  1466. {
  1467. EVP_CIPHER_free(cipher);
  1468. }
  1469. EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  1470. const char *properties)
  1471. {
  1472. EVP_CIPHER *cipher =
  1473. evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
  1474. evp_cipher_from_algorithm, evp_cipher_up_ref,
  1475. evp_cipher_free);
  1476. return cipher;
  1477. }
  1478. int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
  1479. {
  1480. int ref = 0;
  1481. if (cipher->origin == EVP_ORIG_DYNAMIC)
  1482. CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
  1483. return 1;
  1484. }
  1485. void evp_cipher_free_int(EVP_CIPHER *cipher)
  1486. {
  1487. OPENSSL_free(cipher->type_name);
  1488. ossl_provider_free(cipher->prov);
  1489. CRYPTO_THREAD_lock_free(cipher->lock);
  1490. OPENSSL_free(cipher);
  1491. }
  1492. void EVP_CIPHER_free(EVP_CIPHER *cipher)
  1493. {
  1494. int i;
  1495. if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
  1496. return;
  1497. CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
  1498. if (i > 0)
  1499. return;
  1500. evp_cipher_free_int(cipher);
  1501. }
  1502. void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
  1503. void (*fn)(EVP_CIPHER *mac, void *arg),
  1504. void *arg)
  1505. {
  1506. evp_generic_do_all(libctx, OSSL_OP_CIPHER,
  1507. (void (*)(void *, void *))fn, arg,
  1508. evp_cipher_from_algorithm, evp_cipher_up_ref,
  1509. evp_cipher_free);
  1510. }