evp_enc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <limits.h>
  11. #include <assert.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/evp.h>
  14. #include <openssl/err.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/rand_drbg.h>
  17. #include <openssl/engine.h>
  18. #include "crypto/evp.h"
  19. #include "evp_local.h"
  20. int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
  21. {
  22. if (c == NULL)
  23. return 1;
  24. if (c->cipher != NULL) {
  25. if (c->cipher->cleanup && !c->cipher->cleanup(c))
  26. return 0;
  27. /* Cleanse cipher context data */
  28. if (c->cipher_data && c->cipher->ctx_size)
  29. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  30. }
  31. OPENSSL_free(c->cipher_data);
  32. #ifndef OPENSSL_NO_ENGINE
  33. ENGINE_finish(c->engine);
  34. #endif
  35. memset(c, 0, sizeof(*c));
  36. return 1;
  37. }
  38. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
  39. {
  40. return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
  41. }
  42. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  43. {
  44. EVP_CIPHER_CTX_reset(ctx);
  45. OPENSSL_free(ctx);
  46. }
  47. int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  48. const unsigned char *key, const unsigned char *iv, int enc)
  49. {
  50. if (cipher != NULL)
  51. EVP_CIPHER_CTX_reset(ctx);
  52. return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
  53. }
  54. int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  55. ENGINE *impl, const unsigned char *key,
  56. const unsigned char *iv, int enc)
  57. {
  58. if (enc == -1)
  59. enc = ctx->encrypt;
  60. else {
  61. if (enc)
  62. enc = 1;
  63. ctx->encrypt = enc;
  64. }
  65. #ifndef OPENSSL_NO_ENGINE
  66. /*
  67. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  68. * this context may already have an ENGINE! Try to avoid releasing the
  69. * previous handle, re-querying for an ENGINE, and having a
  70. * reinitialisation, when it may all be unnecessary.
  71. */
  72. if (ctx->engine && ctx->cipher
  73. && (cipher == NULL || cipher->nid == ctx->cipher->nid))
  74. goto skip_to_init;
  75. #endif
  76. if (cipher) {
  77. /*
  78. * Ensure a context left lying around from last time is cleared (the
  79. * previous check attempted to avoid this if the same ENGINE and
  80. * EVP_CIPHER could be used).
  81. */
  82. if (ctx->cipher
  83. #ifndef OPENSSL_NO_ENGINE
  84. || ctx->engine
  85. #endif
  86. || ctx->cipher_data) {
  87. unsigned long flags = ctx->flags;
  88. EVP_CIPHER_CTX_reset(ctx);
  89. /* Restore encrypt and flags */
  90. ctx->encrypt = enc;
  91. ctx->flags = flags;
  92. }
  93. #ifndef OPENSSL_NO_ENGINE
  94. if (impl) {
  95. if (!ENGINE_init(impl)) {
  96. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  97. return 0;
  98. }
  99. } else
  100. /* Ask if an ENGINE is reserved for this job */
  101. impl = ENGINE_get_cipher_engine(cipher->nid);
  102. if (impl) {
  103. /* There's an ENGINE for this job ... (apparently) */
  104. const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
  105. if (!c) {
  106. ENGINE_finish(impl);
  107. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  108. return 0;
  109. }
  110. /* We'll use the ENGINE's private cipher definition */
  111. cipher = c;
  112. /*
  113. * Store the ENGINE functional reference so we know 'cipher' came
  114. * from an ENGINE and we need to release it when done.
  115. */
  116. ctx->engine = impl;
  117. } else
  118. ctx->engine = NULL;
  119. #endif
  120. ctx->cipher = cipher;
  121. if (ctx->cipher->ctx_size) {
  122. ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
  123. if (ctx->cipher_data == NULL) {
  124. ctx->cipher = NULL;
  125. EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
  126. return 0;
  127. }
  128. } else {
  129. ctx->cipher_data = NULL;
  130. }
  131. ctx->key_len = cipher->key_len;
  132. /* Preserve wrap enable flag, zero everything else */
  133. ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
  134. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  135. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  136. ctx->cipher = NULL;
  137. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  138. return 0;
  139. }
  140. }
  141. } else if (!ctx->cipher) {
  142. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
  143. return 0;
  144. }
  145. #ifndef OPENSSL_NO_ENGINE
  146. skip_to_init:
  147. #endif
  148. /* we assume block size is a power of 2 in *cryptUpdate */
  149. OPENSSL_assert(ctx->cipher->block_size == 1
  150. || ctx->cipher->block_size == 8
  151. || ctx->cipher->block_size == 16);
  152. if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
  153. && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
  154. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
  155. return 0;
  156. }
  157. if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
  158. switch (EVP_CIPHER_CTX_mode(ctx)) {
  159. case EVP_CIPH_STREAM_CIPHER:
  160. case EVP_CIPH_ECB_MODE:
  161. break;
  162. case EVP_CIPH_CFB_MODE:
  163. case EVP_CIPH_OFB_MODE:
  164. ctx->num = 0;
  165. /* fall-through */
  166. case EVP_CIPH_CBC_MODE:
  167. OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
  168. (int)sizeof(ctx->iv));
  169. if (iv)
  170. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  171. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  172. break;
  173. case EVP_CIPH_CTR_MODE:
  174. ctx->num = 0;
  175. /* Don't reuse IV for CTR mode */
  176. if (iv)
  177. memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  178. break;
  179. default:
  180. return 0;
  181. }
  182. }
  183. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  184. if (!ctx->cipher->init(ctx, key, iv, enc))
  185. return 0;
  186. }
  187. ctx->buf_len = 0;
  188. ctx->final_used = 0;
  189. ctx->block_mask = ctx->cipher->block_size - 1;
  190. return 1;
  191. }
  192. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  193. const unsigned char *in, int inl)
  194. {
  195. if (ctx->encrypt)
  196. return EVP_EncryptUpdate(ctx, out, outl, in, inl);
  197. else
  198. return EVP_DecryptUpdate(ctx, out, outl, in, inl);
  199. }
  200. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  201. {
  202. if (ctx->encrypt)
  203. return EVP_EncryptFinal_ex(ctx, out, outl);
  204. else
  205. return EVP_DecryptFinal_ex(ctx, out, outl);
  206. }
  207. int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  208. {
  209. if (ctx->encrypt)
  210. return EVP_EncryptFinal(ctx, out, outl);
  211. else
  212. return EVP_DecryptFinal(ctx, out, outl);
  213. }
  214. int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  215. const unsigned char *key, const unsigned char *iv)
  216. {
  217. return EVP_CipherInit(ctx, cipher, key, iv, 1);
  218. }
  219. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  220. ENGINE *impl, const unsigned char *key,
  221. const unsigned char *iv)
  222. {
  223. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  224. }
  225. int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  226. const unsigned char *key, const unsigned char *iv)
  227. {
  228. return EVP_CipherInit(ctx, cipher, key, iv, 0);
  229. }
  230. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  231. ENGINE *impl, const unsigned char *key,
  232. const unsigned char *iv)
  233. {
  234. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  235. }
  236. /*
  237. * According to the letter of standard difference between pointers
  238. * is specified to be valid only within same object. This makes
  239. * it formally challenging to determine if input and output buffers
  240. * are not partially overlapping with standard pointer arithmetic.
  241. */
  242. #ifdef PTRDIFF_T
  243. # undef PTRDIFF_T
  244. #endif
  245. #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
  246. /*
  247. * Then we have VMS that distinguishes itself by adhering to
  248. * sizeof(size_t)==4 even in 64-bit builds, which means that
  249. * difference between two pointers might be truncated to 32 bits.
  250. * In the context one can even wonder how comparison for
  251. * equality is implemented. To be on the safe side we adhere to
  252. * PTRDIFF_T even for comparison for equality.
  253. */
  254. # define PTRDIFF_T uint64_t
  255. #else
  256. # define PTRDIFF_T size_t
  257. #endif
  258. int is_partially_overlapping(const void *ptr1, const void *ptr2, size_t len)
  259. {
  260. PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
  261. /*
  262. * Check for partially overlapping buffers. [Binary logical
  263. * operations are used instead of boolean to minimize number
  264. * of conditional branches.]
  265. */
  266. int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
  267. (diff > (0 - (PTRDIFF_T)len)));
  268. return overlapped;
  269. }
  270. static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
  271. unsigned char *out, int *outl,
  272. const unsigned char *in, int inl)
  273. {
  274. int i, j, bl;
  275. size_t cmpl = (size_t)inl;
  276. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  277. cmpl = (cmpl + 7) / 8;
  278. bl = ctx->cipher->block_size;
  279. /*
  280. * CCM mode needs to know about the case where inl == 0 && in == NULL - it
  281. * means the plaintext/ciphertext length is 0
  282. */
  283. if (inl < 0
  284. || (inl == 0
  285. && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)) {
  286. *outl = 0;
  287. return inl == 0;
  288. }
  289. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  290. /* If block size > 1 then the cipher will have to do this check */
  291. if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
  292. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  293. return 0;
  294. }
  295. i = ctx->cipher->do_cipher(ctx, out, in, inl);
  296. if (i < 0)
  297. return 0;
  298. else
  299. *outl = i;
  300. return 1;
  301. }
  302. if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
  303. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  304. return 0;
  305. }
  306. if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
  307. if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
  308. *outl = inl;
  309. return 1;
  310. } else {
  311. *outl = 0;
  312. return 0;
  313. }
  314. }
  315. i = ctx->buf_len;
  316. OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
  317. if (i != 0) {
  318. if (bl - i > inl) {
  319. memcpy(&(ctx->buf[i]), in, inl);
  320. ctx->buf_len += inl;
  321. *outl = 0;
  322. return 1;
  323. } else {
  324. j = bl - i;
  325. /*
  326. * Once we've processed the first j bytes from in, the amount of
  327. * data left that is a multiple of the block length is:
  328. * (inl - j) & ~(bl - 1)
  329. * We must ensure that this amount of data, plus the one block that
  330. * we process from ctx->buf does not exceed INT_MAX
  331. */
  332. if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
  333. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE,
  334. EVP_R_OUTPUT_WOULD_OVERFLOW);
  335. return 0;
  336. }
  337. memcpy(&(ctx->buf[i]), in, j);
  338. inl -= j;
  339. in += j;
  340. if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
  341. return 0;
  342. out += bl;
  343. *outl = bl;
  344. }
  345. } else
  346. *outl = 0;
  347. i = inl & (bl - 1);
  348. inl -= i;
  349. if (inl > 0) {
  350. if (!ctx->cipher->do_cipher(ctx, out, in, inl))
  351. return 0;
  352. *outl += inl;
  353. }
  354. if (i != 0)
  355. memcpy(ctx->buf, &(in[inl]), i);
  356. ctx->buf_len = i;
  357. return 1;
  358. }
  359. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  360. const unsigned char *in, int inl)
  361. {
  362. /* Prevent accidental use of decryption context when encrypting */
  363. if (!ctx->encrypt) {
  364. EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
  365. return 0;
  366. }
  367. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  368. }
  369. int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  370. {
  371. int ret;
  372. ret = EVP_EncryptFinal_ex(ctx, out, outl);
  373. return ret;
  374. }
  375. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  376. {
  377. int n, ret;
  378. unsigned int i, b, bl;
  379. /* Prevent accidental use of decryption context when encrypting */
  380. if (!ctx->encrypt) {
  381. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  382. return 0;
  383. }
  384. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  385. ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  386. if (ret < 0)
  387. return 0;
  388. else
  389. *outl = ret;
  390. return 1;
  391. }
  392. b = ctx->cipher->block_size;
  393. OPENSSL_assert(b <= sizeof(ctx->buf));
  394. if (b == 1) {
  395. *outl = 0;
  396. return 1;
  397. }
  398. bl = ctx->buf_len;
  399. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  400. if (bl) {
  401. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
  402. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  403. return 0;
  404. }
  405. *outl = 0;
  406. return 1;
  407. }
  408. n = b - bl;
  409. for (i = bl; i < b; i++)
  410. ctx->buf[i] = n;
  411. ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
  412. if (ret)
  413. *outl = b;
  414. return ret;
  415. }
  416. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  417. const unsigned char *in, int inl)
  418. {
  419. int fix_len;
  420. unsigned int b;
  421. size_t cmpl = (size_t)inl;
  422. /* Prevent accidental use of encryption context when decrypting */
  423. if (ctx->encrypt) {
  424. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
  425. return 0;
  426. }
  427. b = ctx->cipher->block_size;
  428. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  429. cmpl = (cmpl + 7) / 8;
  430. /*
  431. * CCM mode needs to know about the case where inl == 0 - it means the
  432. * plaintext/ciphertext length is 0
  433. */
  434. if (inl < 0
  435. || (inl == 0
  436. && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)) {
  437. *outl = 0;
  438. return inl == 0;
  439. }
  440. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  441. if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
  442. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  443. return 0;
  444. }
  445. fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
  446. if (fix_len < 0) {
  447. *outl = 0;
  448. return 0;
  449. } else
  450. *outl = fix_len;
  451. return 1;
  452. }
  453. if (ctx->flags & EVP_CIPH_NO_PADDING)
  454. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  455. OPENSSL_assert(b <= sizeof(ctx->final));
  456. if (ctx->final_used) {
  457. /* see comment about PTRDIFF_T comparison above */
  458. if (((PTRDIFF_T)out == (PTRDIFF_T)in)
  459. || is_partially_overlapping(out, in, b)) {
  460. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  461. return 0;
  462. }
  463. /*
  464. * final_used is only ever set if buf_len is 0. Therefore the maximum
  465. * length output we will ever see from evp_EncryptDecryptUpdate is
  466. * the maximum multiple of the block length that is <= inl, or just:
  467. * inl & ~(b - 1)
  468. * Since final_used has been set then the final output length is:
  469. * (inl & ~(b - 1)) + b
  470. * This must never exceed INT_MAX
  471. */
  472. if ((inl & ~(b - 1)) > INT_MAX - b) {
  473. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_OUTPUT_WOULD_OVERFLOW);
  474. return 0;
  475. }
  476. memcpy(out, ctx->final, b);
  477. out += b;
  478. fix_len = 1;
  479. } else
  480. fix_len = 0;
  481. if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
  482. return 0;
  483. /*
  484. * if we have 'decrypted' a multiple of block size, make sure we have a
  485. * copy of this last block
  486. */
  487. if (b > 1 && !ctx->buf_len) {
  488. *outl -= b;
  489. ctx->final_used = 1;
  490. memcpy(ctx->final, &out[*outl], b);
  491. } else
  492. ctx->final_used = 0;
  493. if (fix_len)
  494. *outl += b;
  495. return 1;
  496. }
  497. int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  498. {
  499. int ret;
  500. ret = EVP_DecryptFinal_ex(ctx, out, outl);
  501. return ret;
  502. }
  503. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  504. {
  505. int i, n;
  506. unsigned int b;
  507. /* Prevent accidental use of encryption context when decrypting */
  508. if (ctx->encrypt) {
  509. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  510. return 0;
  511. }
  512. *outl = 0;
  513. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  514. i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  515. if (i < 0)
  516. return 0;
  517. else
  518. *outl = i;
  519. return 1;
  520. }
  521. b = ctx->cipher->block_size;
  522. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  523. if (ctx->buf_len) {
  524. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
  525. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  526. return 0;
  527. }
  528. *outl = 0;
  529. return 1;
  530. }
  531. if (b > 1) {
  532. if (ctx->buf_len || !ctx->final_used) {
  533. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
  534. return 0;
  535. }
  536. OPENSSL_assert(b <= sizeof(ctx->final));
  537. /*
  538. * The following assumes that the ciphertext has been authenticated.
  539. * Otherwise it provides a padding oracle.
  540. */
  541. n = ctx->final[b - 1];
  542. if (n == 0 || n > (int)b) {
  543. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  544. return 0;
  545. }
  546. for (i = 0; i < n; i++) {
  547. if (ctx->final[--b] != n) {
  548. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  549. return 0;
  550. }
  551. }
  552. n = ctx->cipher->block_size - n;
  553. for (i = 0; i < n; i++)
  554. out[i] = ctx->final[i];
  555. *outl = n;
  556. } else
  557. *outl = 0;
  558. return 1;
  559. }
  560. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
  561. {
  562. if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
  563. return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
  564. if (c->key_len == keylen)
  565. return 1;
  566. if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  567. c->key_len = keylen;
  568. return 1;
  569. }
  570. EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
  571. return 0;
  572. }
  573. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
  574. {
  575. if (pad)
  576. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  577. else
  578. ctx->flags |= EVP_CIPH_NO_PADDING;
  579. return 1;
  580. }
  581. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  582. {
  583. int ret;
  584. if (!ctx->cipher) {
  585. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  586. return 0;
  587. }
  588. if (!ctx->cipher->ctrl) {
  589. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  590. return 0;
  591. }
  592. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  593. if (ret == -1) {
  594. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
  595. EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  596. return 0;
  597. }
  598. return ret;
  599. }
  600. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  601. {
  602. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  603. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  604. if (RAND_priv_bytes(key, ctx->key_len) <= 0)
  605. return 0;
  606. return 1;
  607. }
  608. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  609. {
  610. if ((in == NULL) || (in->cipher == NULL)) {
  611. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
  612. return 0;
  613. }
  614. #ifndef OPENSSL_NO_ENGINE
  615. /* Make sure it's safe to copy a cipher context using an ENGINE */
  616. if (in->engine && !ENGINE_init(in->engine)) {
  617. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
  618. return 0;
  619. }
  620. #endif
  621. EVP_CIPHER_CTX_reset(out);
  622. memcpy(out, in, sizeof(*out));
  623. if (in->cipher_data && in->cipher->ctx_size) {
  624. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  625. if (out->cipher_data == NULL) {
  626. out->cipher = NULL;
  627. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
  628. return 0;
  629. }
  630. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  631. }
  632. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  633. if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
  634. out->cipher = NULL;
  635. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
  636. return 0;
  637. }
  638. return 1;
  639. }