Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

655 lignes
17 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/cipher.h>
  57. #include <assert.h>
  58. #include <string.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. #include "internal.h"
  63. const EVP_CIPHER *EVP_get_cipherbynid(int nid) {
  64. switch (nid) {
  65. case NID_des_ede3_cbc:
  66. return EVP_des_ede3_cbc();
  67. case NID_des_ede_cbc:
  68. return EVP_des_cbc();
  69. case NID_aes_128_cbc:
  70. return EVP_aes_128_cbc();
  71. case NID_aes_256_cbc:
  72. return EVP_aes_256_cbc();
  73. default:
  74. return NULL;
  75. }
  76. }
  77. void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) {
  78. memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
  79. }
  80. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) {
  81. EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
  82. if (ctx) {
  83. EVP_CIPHER_CTX_init(ctx);
  84. }
  85. return ctx;
  86. }
  87. int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) {
  88. if (c->cipher != NULL && c->cipher->cleanup && !c->cipher->cleanup(c)) {
  89. return 0;
  90. }
  91. if (c->cipher_data) {
  92. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  93. OPENSSL_free(c->cipher_data);
  94. }
  95. memset(c, 0, sizeof(EVP_CIPHER_CTX));
  96. return 1;
  97. }
  98. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) {
  99. if (ctx) {
  100. EVP_CIPHER_CTX_cleanup(ctx);
  101. OPENSSL_free(ctx);
  102. }
  103. }
  104. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) {
  105. if (in == NULL || in->cipher == NULL) {
  106. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_copy, CIPHER_R_INPUT_NOT_INITIALIZED);
  107. return 0;
  108. }
  109. EVP_CIPHER_CTX_cleanup(out);
  110. memcpy(out, in, sizeof(EVP_CIPHER_CTX));
  111. if (in->cipher_data && in->cipher->ctx_size) {
  112. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  113. if (!out->cipher_data) {
  114. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_copy, ERR_R_MALLOC_FAILURE);
  115. return 0;
  116. }
  117. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  118. }
  119. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) {
  120. return in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out);
  121. }
  122. return 1;
  123. }
  124. int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  125. ENGINE *engine, const uint8_t *key, const uint8_t *iv,
  126. int enc) {
  127. if (enc == -1) {
  128. enc = ctx->encrypt;
  129. } else {
  130. if (enc) {
  131. enc = 1;
  132. }
  133. ctx->encrypt = enc;
  134. }
  135. if (cipher) {
  136. /* Ensure a context left from last time is cleared (the previous check
  137. * attempted to avoid this if the same ENGINE and EVP_CIPHER could be
  138. * used). */
  139. if (ctx->cipher) {
  140. EVP_CIPHER_CTX_cleanup(ctx);
  141. /* Restore encrypt and flags */
  142. ctx->encrypt = enc;
  143. }
  144. ctx->cipher = cipher;
  145. if (ctx->cipher->ctx_size) {
  146. ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
  147. if (!ctx->cipher_data) {
  148. OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, ERR_R_MALLOC_FAILURE);
  149. return 0;
  150. }
  151. } else {
  152. ctx->cipher_data = NULL;
  153. }
  154. ctx->key_len = cipher->key_len;
  155. ctx->flags = 0;
  156. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  157. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  158. OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, CIPHER_R_INITIALIZATION_ERROR);
  159. return 0;
  160. }
  161. }
  162. } else if (!ctx->cipher) {
  163. OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, CIPHER_R_NO_CIPHER_SET);
  164. return 0;
  165. }
  166. /* we assume block size is a power of 2 in *cryptUpdate */
  167. assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 ||
  168. ctx->cipher->block_size == 16);
  169. if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
  170. switch (EVP_CIPHER_CTX_mode(ctx)) {
  171. case EVP_CIPH_STREAM_CIPHER:
  172. case EVP_CIPH_ECB_MODE:
  173. break;
  174. case EVP_CIPH_CFB_MODE:
  175. case EVP_CIPH_OFB_MODE:
  176. ctx->num = 0;
  177. /* fall-through */
  178. case EVP_CIPH_CBC_MODE:
  179. assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
  180. if (iv) {
  181. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  182. }
  183. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  184. break;
  185. case EVP_CIPH_CTR_MODE:
  186. ctx->num = 0;
  187. /* Don't reuse IV for CTR mode */
  188. if (iv) {
  189. memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  190. }
  191. break;
  192. default:
  193. return 0;
  194. }
  195. }
  196. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  197. if (!ctx->cipher->init(ctx, key, iv, enc)) {
  198. return 0;
  199. }
  200. }
  201. ctx->buf_len = 0;
  202. ctx->final_used = 0;
  203. ctx->block_mask = ctx->cipher->block_size - 1;
  204. return 1;
  205. }
  206. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  207. ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
  208. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  209. }
  210. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  211. ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
  212. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  213. }
  214. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  215. const uint8_t *in, int in_len) {
  216. int i, j, bl;
  217. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  218. i = ctx->cipher->cipher(ctx, out, in, in_len);
  219. if (i < 0) {
  220. return 0;
  221. } else {
  222. *out_len = i;
  223. }
  224. return 1;
  225. }
  226. if (in_len <= 0) {
  227. *out_len = 0;
  228. return in_len == 0;
  229. }
  230. if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) {
  231. if (ctx->cipher->cipher(ctx, out, in, in_len)) {
  232. *out_len = in_len;
  233. return 1;
  234. } else {
  235. *out_len = 0;
  236. return 0;
  237. }
  238. }
  239. i = ctx->buf_len;
  240. bl = ctx->cipher->block_size;
  241. assert(bl <= (int)sizeof(ctx->buf));
  242. if (i != 0) {
  243. if (i + in_len < bl) {
  244. memcpy(&ctx->buf[i], in, in_len);
  245. ctx->buf_len += in_len;
  246. *out_len = 0;
  247. return 1;
  248. } else {
  249. j = bl - i;
  250. memcpy(&ctx->buf[i], in, j);
  251. if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) {
  252. return 0;
  253. }
  254. in_len -= j;
  255. in += j;
  256. out += bl;
  257. *out_len = bl;
  258. }
  259. } else {
  260. *out_len = 0;
  261. }
  262. i = in_len & ctx->block_mask;
  263. in_len -= i;
  264. if (in_len > 0) {
  265. if (!ctx->cipher->cipher(ctx, out, in, in_len)) {
  266. return 0;
  267. }
  268. *out_len += in_len;
  269. }
  270. if (i != 0) {
  271. memcpy(ctx->buf, &in[in_len], i);
  272. }
  273. ctx->buf_len = i;
  274. return 1;
  275. }
  276. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
  277. int n, ret;
  278. unsigned int i, b, bl;
  279. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  280. ret = ctx->cipher->cipher(ctx, out, NULL, 0);
  281. if (ret < 0) {
  282. return 0;
  283. } else {
  284. *out_len = ret;
  285. }
  286. return 1;
  287. }
  288. b = ctx->cipher->block_size;
  289. assert(b <= sizeof(ctx->buf));
  290. if (b == 1) {
  291. *out_len = 0;
  292. return 1;
  293. }
  294. bl = ctx->buf_len;
  295. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  296. if (bl) {
  297. OPENSSL_PUT_ERROR(CIPHER, EVP_EncryptFinal_ex,
  298. CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  299. return 0;
  300. }
  301. *out_len = 0;
  302. return 1;
  303. }
  304. n = b - bl;
  305. for (i = bl; i < b; i++) {
  306. ctx->buf[i] = n;
  307. }
  308. ret = ctx->cipher->cipher(ctx, out, ctx->buf, b);
  309. if (ret) {
  310. *out_len = b;
  311. }
  312. return ret;
  313. }
  314. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  315. const uint8_t *in, int in_len) {
  316. int fix_len;
  317. unsigned int b;
  318. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  319. int r = ctx->cipher->cipher(ctx, out, in, in_len);
  320. if (r < 0) {
  321. *out_len = 0;
  322. return 0;
  323. } else {
  324. *out_len = r;
  325. }
  326. return 1;
  327. }
  328. if (in_len <= 0) {
  329. *out_len = 0;
  330. return in_len == 0;
  331. }
  332. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  333. return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
  334. }
  335. b = ctx->cipher->block_size;
  336. assert(b <= sizeof(ctx->final));
  337. if (ctx->final_used) {
  338. memcpy(out, ctx->final, b);
  339. out += b;
  340. fix_len = 1;
  341. } else {
  342. fix_len = 0;
  343. }
  344. if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) {
  345. return 0;
  346. }
  347. /* if we have 'decrypted' a multiple of block size, make sure
  348. * we have a copy of this last block */
  349. if (b > 1 && !ctx->buf_len) {
  350. *out_len -= b;
  351. ctx->final_used = 1;
  352. memcpy(ctx->final, &out[*out_len], b);
  353. } else {
  354. ctx->final_used = 0;
  355. }
  356. if (fix_len) {
  357. *out_len += b;
  358. }
  359. return 1;
  360. }
  361. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) {
  362. int i, n;
  363. unsigned int b;
  364. *out_len = 0;
  365. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  366. i = ctx->cipher->cipher(ctx, out, NULL, 0);
  367. if (i < 0) {
  368. return 0;
  369. } else {
  370. *out_len = i;
  371. }
  372. return 1;
  373. }
  374. b = ctx->cipher->block_size;
  375. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  376. if (ctx->buf_len) {
  377. OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex,
  378. CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  379. return 0;
  380. }
  381. *out_len = 0;
  382. return 1;
  383. }
  384. if (b > 1) {
  385. if (ctx->buf_len || !ctx->final_used) {
  386. OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex,
  387. CIPHER_R_WRONG_FINAL_BLOCK_LENGTH);
  388. return 0;
  389. }
  390. assert(b <= sizeof(ctx->final));
  391. /* The following assumes that the ciphertext has been authenticated.
  392. * Otherwise it provides a padding oracle. */
  393. n = ctx->final[b - 1];
  394. if (n == 0 || n > (int)b) {
  395. OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex, CIPHER_R_BAD_DECRYPT);
  396. return 0;
  397. }
  398. for (i = 0; i < n; i++) {
  399. if (ctx->final[--b] != n) {
  400. OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex, CIPHER_R_BAD_DECRYPT);
  401. return 0;
  402. }
  403. }
  404. n = ctx->cipher->block_size - n;
  405. for (i = 0; i < n; i++) {
  406. out[i] = ctx->final[i];
  407. }
  408. *out_len = n;
  409. } else {
  410. *out_len = 0;
  411. }
  412. return 1;
  413. }
  414. int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  415. size_t in_len) {
  416. return ctx->cipher->cipher(ctx, out, in, in_len);
  417. }
  418. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  419. const uint8_t *in, int in_len) {
  420. if (ctx->encrypt) {
  421. return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
  422. } else {
  423. return EVP_DecryptUpdate(ctx, out, out_len, in, in_len);
  424. }
  425. }
  426. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
  427. if (ctx->encrypt) {
  428. return EVP_EncryptFinal_ex(ctx, out, out_len);
  429. } else {
  430. return EVP_DecryptFinal_ex(ctx, out, out_len);
  431. }
  432. }
  433. const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) {
  434. return ctx->cipher;
  435. }
  436. int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) {
  437. return ctx->cipher->nid;
  438. }
  439. unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) {
  440. return ctx->cipher->block_size;
  441. }
  442. unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) {
  443. return ctx->key_len;
  444. }
  445. unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) {
  446. return ctx->cipher->iv_len;
  447. }
  448. void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) {
  449. return ctx->app_data;
  450. }
  451. void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) {
  452. ctx->app_data = data;
  453. }
  454. uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) {
  455. return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK;
  456. }
  457. uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) {
  458. return ctx->cipher->flags & EVP_CIPH_MODE_MASK;
  459. }
  460. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) {
  461. int ret;
  462. if (!ctx->cipher) {
  463. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl, CIPHER_R_NO_CIPHER_SET);
  464. return 0;
  465. }
  466. if (!ctx->cipher->ctrl) {
  467. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl, CIPHER_R_CTRL_NOT_IMPLEMENTED);
  468. return 0;
  469. }
  470. ret = ctx->cipher->ctrl(ctx, command, arg, ptr);
  471. if (ret == -1) {
  472. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl,
  473. CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  474. return 0;
  475. }
  476. return ret;
  477. }
  478. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) {
  479. if (pad) {
  480. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  481. } else {
  482. ctx->flags |= EVP_CIPH_NO_PADDING;
  483. }
  484. return 1;
  485. }
  486. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, unsigned key_len) {
  487. if (c->key_len == key_len) {
  488. return 1;
  489. }
  490. if (key_len == 0 || !(c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  491. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_set_key_length,
  492. CIPHER_R_INVALID_KEY_LENGTH);
  493. return 0;
  494. }
  495. c->key_len = key_len;
  496. return 1;
  497. }
  498. int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { return cipher->nid; }
  499. const char *EVP_CIPHER_name(const EVP_CIPHER *cipher) {
  500. return OBJ_nid2sn(cipher->nid);
  501. }
  502. unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher) {
  503. return cipher->block_size;
  504. }
  505. unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher) {
  506. return cipher->key_len;
  507. }
  508. unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) {
  509. return cipher->iv_len;
  510. }
  511. uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher) {
  512. return cipher->flags & ~EVP_CIPH_MODE_MASK;
  513. }
  514. uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) {
  515. return cipher->flags & EVP_CIPH_MODE_MASK;
  516. }
  517. int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  518. const uint8_t *key, const uint8_t *iv, int enc) {
  519. if (cipher) {
  520. EVP_CIPHER_CTX_init(ctx);
  521. }
  522. return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
  523. }
  524. int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  525. const uint8_t *key, const uint8_t *iv) {
  526. return EVP_CipherInit(ctx, cipher, key, iv, 1);
  527. }
  528. int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  529. const uint8_t *key, const uint8_t *iv) {
  530. return EVP_CipherInit(ctx, cipher, key, iv, 0);
  531. }
  532. int EVP_add_cipher_alias(const char *a, const char *b) {
  533. return 1;
  534. }
  535. const EVP_CIPHER *EVP_get_cipherbyname(const char *name) {
  536. if (OPENSSL_strcasecmp(name, "rc4") == 0) {
  537. return EVP_rc4();
  538. } else if (OPENSSL_strcasecmp(name, "des-cbc") == 0) {
  539. return EVP_des_cbc();
  540. } else if (OPENSSL_strcasecmp(name, "3des-cbc") == 0 ||
  541. OPENSSL_strcasecmp(name, "3des") == 0) {
  542. return EVP_des_ede3_cbc();
  543. } else if (OPENSSL_strcasecmp(name, "aes-128-cbc") == 0) {
  544. return EVP_aes_128_cbc();
  545. } else if (OPENSSL_strcasecmp(name, "aes-256-cbc") == 0) {
  546. return EVP_aes_256_cbc();
  547. } else if (OPENSSL_strcasecmp(name, "aes-128-ctr") == 0) {
  548. return EVP_aes_128_ctr();
  549. } else if (OPENSSL_strcasecmp(name, "aes-256-ctr") == 0) {
  550. return EVP_aes_256_ctr();
  551. } else if (OPENSSL_strcasecmp(name, "aes-128-ecb") == 0) {
  552. return EVP_aes_128_ecb();
  553. } else if (OPENSSL_strcasecmp(name, "aes-256-ecb") == 0) {
  554. return EVP_aes_256_ecb();
  555. }
  556. return NULL;
  557. }