evp_enc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright 1995-2021 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. unsigned long flags = ctx->flags;
  84. EVP_CIPHER_CTX_reset(ctx);
  85. /* Restore encrypt and flags */
  86. ctx->encrypt = enc;
  87. ctx->flags = flags;
  88. }
  89. #ifndef OPENSSL_NO_ENGINE
  90. if (impl) {
  91. if (!ENGINE_init(impl)) {
  92. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  93. return 0;
  94. }
  95. } else
  96. /* Ask if an ENGINE is reserved for this job */
  97. impl = ENGINE_get_cipher_engine(cipher->nid);
  98. if (impl) {
  99. /* There's an ENGINE for this job ... (apparently) */
  100. const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
  101. if (!c) {
  102. /*
  103. * One positive side-effect of US's export control history,
  104. * is that we should at least be able to avoid using US
  105. * misspellings of "initialisation"?
  106. */
  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, int 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, cmpl = inl;
  275. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  276. cmpl = (cmpl + 7) / 8;
  277. bl = ctx->cipher->block_size;
  278. /*
  279. * CCM mode needs to know about the case where inl == 0 && in == NULL - it
  280. * means the plaintext/ciphertext length is 0
  281. */
  282. if (inl < 0
  283. || (inl == 0
  284. && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)) {
  285. *outl = 0;
  286. return inl == 0;
  287. }
  288. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  289. /* If block size > 1 then the cipher will have to do this check */
  290. if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
  291. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  292. return 0;
  293. }
  294. i = ctx->cipher->do_cipher(ctx, out, in, inl);
  295. if (i < 0)
  296. return 0;
  297. else
  298. *outl = i;
  299. return 1;
  300. }
  301. if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
  302. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  303. return 0;
  304. }
  305. if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
  306. if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
  307. *outl = inl;
  308. return 1;
  309. } else {
  310. *outl = 0;
  311. return 0;
  312. }
  313. }
  314. i = ctx->buf_len;
  315. OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
  316. if (i != 0) {
  317. if (bl - i > inl) {
  318. memcpy(&(ctx->buf[i]), in, inl);
  319. ctx->buf_len += inl;
  320. *outl = 0;
  321. return 1;
  322. } else {
  323. j = bl - i;
  324. /*
  325. * Once we've processed the first j bytes from in, the amount of
  326. * data left that is a multiple of the block length is:
  327. * (inl - j) & ~(bl - 1)
  328. * We must ensure that this amount of data, plus the one block that
  329. * we process from ctx->buf does not exceed INT_MAX
  330. */
  331. if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
  332. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE,
  333. EVP_R_OUTPUT_WOULD_OVERFLOW);
  334. return 0;
  335. }
  336. memcpy(&(ctx->buf[i]), in, j);
  337. inl -= j;
  338. in += j;
  339. if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
  340. return 0;
  341. out += bl;
  342. *outl = bl;
  343. }
  344. } else
  345. *outl = 0;
  346. i = inl & (bl - 1);
  347. inl -= i;
  348. if (inl > 0) {
  349. if (!ctx->cipher->do_cipher(ctx, out, in, inl))
  350. return 0;
  351. *outl += inl;
  352. }
  353. if (i != 0)
  354. memcpy(ctx->buf, &(in[inl]), i);
  355. ctx->buf_len = i;
  356. return 1;
  357. }
  358. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  359. const unsigned char *in, int inl)
  360. {
  361. /* Prevent accidental use of decryption context when encrypting */
  362. if (!ctx->encrypt) {
  363. EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
  364. return 0;
  365. }
  366. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  367. }
  368. int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  369. {
  370. int ret;
  371. ret = EVP_EncryptFinal_ex(ctx, out, outl);
  372. return ret;
  373. }
  374. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  375. {
  376. int n, ret;
  377. unsigned int i, b, bl;
  378. /* Prevent accidental use of decryption context when encrypting */
  379. if (!ctx->encrypt) {
  380. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  381. return 0;
  382. }
  383. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  384. ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  385. if (ret < 0)
  386. return 0;
  387. else
  388. *outl = ret;
  389. return 1;
  390. }
  391. b = ctx->cipher->block_size;
  392. OPENSSL_assert(b <= sizeof(ctx->buf));
  393. if (b == 1) {
  394. *outl = 0;
  395. return 1;
  396. }
  397. bl = ctx->buf_len;
  398. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  399. if (bl) {
  400. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
  401. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  402. return 0;
  403. }
  404. *outl = 0;
  405. return 1;
  406. }
  407. n = b - bl;
  408. for (i = bl; i < b; i++)
  409. ctx->buf[i] = n;
  410. ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
  411. if (ret)
  412. *outl = b;
  413. return ret;
  414. }
  415. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  416. const unsigned char *in, int inl)
  417. {
  418. int fix_len, cmpl = inl;
  419. unsigned int b;
  420. /* Prevent accidental use of encryption context when decrypting */
  421. if (ctx->encrypt) {
  422. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
  423. return 0;
  424. }
  425. b = ctx->cipher->block_size;
  426. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  427. cmpl = (cmpl + 7) / 8;
  428. /*
  429. * CCM mode needs to know about the case where inl == 0 - it means the
  430. * plaintext/ciphertext length is 0
  431. */
  432. if (inl < 0
  433. || (inl == 0
  434. && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)) {
  435. *outl = 0;
  436. return inl == 0;
  437. }
  438. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  439. if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
  440. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  441. return 0;
  442. }
  443. fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
  444. if (fix_len < 0) {
  445. *outl = 0;
  446. return 0;
  447. } else
  448. *outl = fix_len;
  449. return 1;
  450. }
  451. if (ctx->flags & EVP_CIPH_NO_PADDING)
  452. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  453. OPENSSL_assert(b <= sizeof(ctx->final));
  454. if (ctx->final_used) {
  455. /* see comment about PTRDIFF_T comparison above */
  456. if (((PTRDIFF_T)out == (PTRDIFF_T)in)
  457. || is_partially_overlapping(out, in, b)) {
  458. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  459. return 0;
  460. }
  461. /*
  462. * final_used is only ever set if buf_len is 0. Therefore the maximum
  463. * length output we will ever see from evp_EncryptDecryptUpdate is
  464. * the maximum multiple of the block length that is <= inl, or just:
  465. * inl & ~(b - 1)
  466. * Since final_used has been set then the final output length is:
  467. * (inl & ~(b - 1)) + b
  468. * This must never exceed INT_MAX
  469. */
  470. if ((inl & ~(b - 1)) > INT_MAX - b) {
  471. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_OUTPUT_WOULD_OVERFLOW);
  472. return 0;
  473. }
  474. memcpy(out, ctx->final, b);
  475. out += b;
  476. fix_len = 1;
  477. } else
  478. fix_len = 0;
  479. if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
  480. return 0;
  481. /*
  482. * if we have 'decrypted' a multiple of block size, make sure we have a
  483. * copy of this last block
  484. */
  485. if (b > 1 && !ctx->buf_len) {
  486. *outl -= b;
  487. ctx->final_used = 1;
  488. memcpy(ctx->final, &out[*outl], b);
  489. } else
  490. ctx->final_used = 0;
  491. if (fix_len)
  492. *outl += b;
  493. return 1;
  494. }
  495. int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  496. {
  497. int ret;
  498. ret = EVP_DecryptFinal_ex(ctx, out, outl);
  499. return ret;
  500. }
  501. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  502. {
  503. int i, n;
  504. unsigned int b;
  505. /* Prevent accidental use of encryption context when decrypting */
  506. if (ctx->encrypt) {
  507. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  508. return 0;
  509. }
  510. *outl = 0;
  511. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  512. i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  513. if (i < 0)
  514. return 0;
  515. else
  516. *outl = i;
  517. return 1;
  518. }
  519. b = ctx->cipher->block_size;
  520. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  521. if (ctx->buf_len) {
  522. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
  523. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  524. return 0;
  525. }
  526. *outl = 0;
  527. return 1;
  528. }
  529. if (b > 1) {
  530. if (ctx->buf_len || !ctx->final_used) {
  531. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
  532. return 0;
  533. }
  534. OPENSSL_assert(b <= sizeof(ctx->final));
  535. /*
  536. * The following assumes that the ciphertext has been authenticated.
  537. * Otherwise it provides a padding oracle.
  538. */
  539. n = ctx->final[b - 1];
  540. if (n == 0 || n > (int)b) {
  541. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  542. return 0;
  543. }
  544. for (i = 0; i < n; i++) {
  545. if (ctx->final[--b] != n) {
  546. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  547. return 0;
  548. }
  549. }
  550. n = ctx->cipher->block_size - n;
  551. for (i = 0; i < n; i++)
  552. out[i] = ctx->final[i];
  553. *outl = n;
  554. } else
  555. *outl = 0;
  556. return 1;
  557. }
  558. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
  559. {
  560. if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
  561. return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
  562. if (c->key_len == keylen)
  563. return 1;
  564. if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  565. c->key_len = keylen;
  566. return 1;
  567. }
  568. EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
  569. return 0;
  570. }
  571. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
  572. {
  573. if (pad)
  574. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  575. else
  576. ctx->flags |= EVP_CIPH_NO_PADDING;
  577. return 1;
  578. }
  579. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  580. {
  581. int ret;
  582. if (!ctx->cipher) {
  583. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  584. return 0;
  585. }
  586. if (!ctx->cipher->ctrl) {
  587. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  588. return 0;
  589. }
  590. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  591. if (ret == -1) {
  592. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
  593. EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  594. return 0;
  595. }
  596. return ret;
  597. }
  598. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  599. {
  600. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  601. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  602. if (RAND_priv_bytes(key, ctx->key_len) <= 0)
  603. return 0;
  604. return 1;
  605. }
  606. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  607. {
  608. if ((in == NULL) || (in->cipher == NULL)) {
  609. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
  610. return 0;
  611. }
  612. #ifndef OPENSSL_NO_ENGINE
  613. /* Make sure it's safe to copy a cipher context using an ENGINE */
  614. if (in->engine && !ENGINE_init(in->engine)) {
  615. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
  616. return 0;
  617. }
  618. #endif
  619. EVP_CIPHER_CTX_reset(out);
  620. memcpy(out, in, sizeof(*out));
  621. if (in->cipher_data && in->cipher->ctx_size) {
  622. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  623. if (out->cipher_data == NULL) {
  624. out->cipher = NULL;
  625. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
  626. return 0;
  627. }
  628. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  629. }
  630. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  631. if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
  632. out->cipher = NULL;
  633. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
  634. return 0;
  635. }
  636. return 1;
  637. }