您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

653 行
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/nid.h>
  62. #include "internal.h"
  63. const EVP_CIPHER *EVP_get_cipherbynid(int nid) {
  64. switch (nid) {
  65. case NID_rc2_cbc:
  66. return EVP_rc2_cbc();
  67. case NID_rc2_40_cbc:
  68. return EVP_rc2_40_cbc();
  69. case NID_des_ede3_cbc:
  70. return EVP_des_ede3_cbc();
  71. case NID_des_ede_cbc:
  72. return EVP_des_cbc();
  73. case NID_aes_128_cbc:
  74. return EVP_aes_128_cbc();
  75. case NID_aes_192_cbc:
  76. return EVP_aes_192_cbc();
  77. case NID_aes_256_cbc:
  78. return EVP_aes_256_cbc();
  79. default:
  80. return NULL;
  81. }
  82. }
  83. void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) {
  84. memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
  85. }
  86. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) {
  87. EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
  88. if (ctx) {
  89. EVP_CIPHER_CTX_init(ctx);
  90. }
  91. return ctx;
  92. }
  93. int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) {
  94. if (c->cipher != NULL) {
  95. if (c->cipher->cleanup) {
  96. c->cipher->cleanup(c);
  97. }
  98. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  99. }
  100. OPENSSL_free(c->cipher_data);
  101. memset(c, 0, sizeof(EVP_CIPHER_CTX));
  102. return 1;
  103. }
  104. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) {
  105. if (ctx) {
  106. EVP_CIPHER_CTX_cleanup(ctx);
  107. OPENSSL_free(ctx);
  108. }
  109. }
  110. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) {
  111. if (in == NULL || in->cipher == NULL) {
  112. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INPUT_NOT_INITIALIZED);
  113. return 0;
  114. }
  115. EVP_CIPHER_CTX_cleanup(out);
  116. memcpy(out, in, sizeof(EVP_CIPHER_CTX));
  117. if (in->cipher_data && in->cipher->ctx_size) {
  118. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  119. if (!out->cipher_data) {
  120. OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
  121. return 0;
  122. }
  123. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  124. }
  125. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) {
  126. return in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out);
  127. }
  128. return 1;
  129. }
  130. int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  131. ENGINE *engine, const uint8_t *key, const uint8_t *iv,
  132. int enc) {
  133. if (enc == -1) {
  134. enc = ctx->encrypt;
  135. } else {
  136. if (enc) {
  137. enc = 1;
  138. }
  139. ctx->encrypt = enc;
  140. }
  141. if (cipher) {
  142. /* Ensure a context left from last time is cleared (the previous check
  143. * attempted to avoid this if the same ENGINE and EVP_CIPHER could be
  144. * used). */
  145. if (ctx->cipher) {
  146. EVP_CIPHER_CTX_cleanup(ctx);
  147. /* Restore encrypt and flags */
  148. ctx->encrypt = enc;
  149. }
  150. ctx->cipher = cipher;
  151. if (ctx->cipher->ctx_size) {
  152. ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
  153. if (!ctx->cipher_data) {
  154. ctx->cipher = NULL;
  155. OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
  156. return 0;
  157. }
  158. } else {
  159. ctx->cipher_data = NULL;
  160. }
  161. ctx->key_len = cipher->key_len;
  162. ctx->flags = 0;
  163. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  164. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  165. ctx->cipher = NULL;
  166. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INITIALIZATION_ERROR);
  167. return 0;
  168. }
  169. }
  170. } else if (!ctx->cipher) {
  171. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET);
  172. return 0;
  173. }
  174. /* we assume block size is a power of 2 in *cryptUpdate */
  175. assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 ||
  176. ctx->cipher->block_size == 16);
  177. if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
  178. switch (EVP_CIPHER_CTX_mode(ctx)) {
  179. case EVP_CIPH_STREAM_CIPHER:
  180. case EVP_CIPH_ECB_MODE:
  181. break;
  182. case EVP_CIPH_CFB_MODE:
  183. ctx->num = 0;
  184. /* fall-through */
  185. case EVP_CIPH_CBC_MODE:
  186. assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
  187. if (iv) {
  188. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  189. }
  190. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  191. break;
  192. case EVP_CIPH_CTR_MODE:
  193. case EVP_CIPH_OFB_MODE:
  194. ctx->num = 0;
  195. /* Don't reuse IV for CTR mode */
  196. if (iv) {
  197. memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  198. }
  199. break;
  200. default:
  201. return 0;
  202. }
  203. }
  204. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  205. if (!ctx->cipher->init(ctx, key, iv, enc)) {
  206. return 0;
  207. }
  208. }
  209. ctx->buf_len = 0;
  210. ctx->final_used = 0;
  211. ctx->block_mask = ctx->cipher->block_size - 1;
  212. return 1;
  213. }
  214. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  215. ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
  216. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  217. }
  218. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  219. ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
  220. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  221. }
  222. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  223. const uint8_t *in, int in_len) {
  224. int i, j, bl;
  225. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  226. i = ctx->cipher->cipher(ctx, out, in, in_len);
  227. if (i < 0) {
  228. return 0;
  229. } else {
  230. *out_len = i;
  231. }
  232. return 1;
  233. }
  234. if (in_len <= 0) {
  235. *out_len = 0;
  236. return in_len == 0;
  237. }
  238. if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) {
  239. if (ctx->cipher->cipher(ctx, out, in, in_len)) {
  240. *out_len = in_len;
  241. return 1;
  242. } else {
  243. *out_len = 0;
  244. return 0;
  245. }
  246. }
  247. i = ctx->buf_len;
  248. bl = ctx->cipher->block_size;
  249. assert(bl <= (int)sizeof(ctx->buf));
  250. if (i != 0) {
  251. if (bl - i > in_len) {
  252. memcpy(&ctx->buf[i], in, in_len);
  253. ctx->buf_len += in_len;
  254. *out_len = 0;
  255. return 1;
  256. } else {
  257. j = bl - i;
  258. memcpy(&ctx->buf[i], in, j);
  259. if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) {
  260. return 0;
  261. }
  262. in_len -= j;
  263. in += j;
  264. out += bl;
  265. *out_len = bl;
  266. }
  267. } else {
  268. *out_len = 0;
  269. }
  270. i = in_len & ctx->block_mask;
  271. in_len -= i;
  272. if (in_len > 0) {
  273. if (!ctx->cipher->cipher(ctx, out, in, in_len)) {
  274. return 0;
  275. }
  276. *out_len += in_len;
  277. }
  278. if (i != 0) {
  279. memcpy(ctx->buf, &in[in_len], i);
  280. }
  281. ctx->buf_len = i;
  282. return 1;
  283. }
  284. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
  285. int n, ret;
  286. unsigned int i, b, bl;
  287. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  288. ret = ctx->cipher->cipher(ctx, out, NULL, 0);
  289. if (ret < 0) {
  290. return 0;
  291. } else {
  292. *out_len = ret;
  293. }
  294. return 1;
  295. }
  296. b = ctx->cipher->block_size;
  297. assert(b <= sizeof(ctx->buf));
  298. if (b == 1) {
  299. *out_len = 0;
  300. return 1;
  301. }
  302. bl = ctx->buf_len;
  303. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  304. if (bl) {
  305. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  306. return 0;
  307. }
  308. *out_len = 0;
  309. return 1;
  310. }
  311. n = b - bl;
  312. for (i = bl; i < b; i++) {
  313. ctx->buf[i] = n;
  314. }
  315. ret = ctx->cipher->cipher(ctx, out, ctx->buf, b);
  316. if (ret) {
  317. *out_len = b;
  318. }
  319. return ret;
  320. }
  321. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  322. const uint8_t *in, int in_len) {
  323. int fix_len;
  324. unsigned int b;
  325. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  326. int r = ctx->cipher->cipher(ctx, out, in, in_len);
  327. if (r < 0) {
  328. *out_len = 0;
  329. return 0;
  330. } else {
  331. *out_len = r;
  332. }
  333. return 1;
  334. }
  335. if (in_len <= 0) {
  336. *out_len = 0;
  337. return in_len == 0;
  338. }
  339. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  340. return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
  341. }
  342. b = ctx->cipher->block_size;
  343. assert(b <= sizeof(ctx->final));
  344. if (ctx->final_used) {
  345. memcpy(out, ctx->final, b);
  346. out += b;
  347. fix_len = 1;
  348. } else {
  349. fix_len = 0;
  350. }
  351. if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) {
  352. return 0;
  353. }
  354. /* if we have 'decrypted' a multiple of block size, make sure
  355. * we have a copy of this last block */
  356. if (b > 1 && !ctx->buf_len) {
  357. *out_len -= b;
  358. ctx->final_used = 1;
  359. memcpy(ctx->final, &out[*out_len], b);
  360. } else {
  361. ctx->final_used = 0;
  362. }
  363. if (fix_len) {
  364. *out_len += b;
  365. }
  366. return 1;
  367. }
  368. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) {
  369. int i, n;
  370. unsigned int b;
  371. *out_len = 0;
  372. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  373. i = ctx->cipher->cipher(ctx, out, NULL, 0);
  374. if (i < 0) {
  375. return 0;
  376. } else {
  377. *out_len = i;
  378. }
  379. return 1;
  380. }
  381. b = ctx->cipher->block_size;
  382. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  383. if (ctx->buf_len) {
  384. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  385. return 0;
  386. }
  387. *out_len = 0;
  388. return 1;
  389. }
  390. if (b > 1) {
  391. if (ctx->buf_len || !ctx->final_used) {
  392. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_WRONG_FINAL_BLOCK_LENGTH);
  393. return 0;
  394. }
  395. assert(b <= sizeof(ctx->final));
  396. /* The following assumes that the ciphertext has been authenticated.
  397. * Otherwise it provides a padding oracle. */
  398. n = ctx->final[b - 1];
  399. if (n == 0 || n > (int)b) {
  400. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  401. return 0;
  402. }
  403. for (i = 0; i < n; i++) {
  404. if (ctx->final[--b] != n) {
  405. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  406. return 0;
  407. }
  408. }
  409. n = ctx->cipher->block_size - n;
  410. for (i = 0; i < n; i++) {
  411. out[i] = ctx->final[i];
  412. }
  413. *out_len = n;
  414. } else {
  415. *out_len = 0;
  416. }
  417. return 1;
  418. }
  419. int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  420. size_t in_len) {
  421. return ctx->cipher->cipher(ctx, out, in, in_len);
  422. }
  423. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  424. const uint8_t *in, int in_len) {
  425. if (ctx->encrypt) {
  426. return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
  427. } else {
  428. return EVP_DecryptUpdate(ctx, out, out_len, in, in_len);
  429. }
  430. }
  431. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
  432. if (ctx->encrypt) {
  433. return EVP_EncryptFinal_ex(ctx, out, out_len);
  434. } else {
  435. return EVP_DecryptFinal_ex(ctx, out, out_len);
  436. }
  437. }
  438. const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) {
  439. return ctx->cipher;
  440. }
  441. int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) {
  442. return ctx->cipher->nid;
  443. }
  444. unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) {
  445. return ctx->cipher->block_size;
  446. }
  447. unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) {
  448. return ctx->key_len;
  449. }
  450. unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) {
  451. return ctx->cipher->iv_len;
  452. }
  453. void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) {
  454. return ctx->app_data;
  455. }
  456. void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) {
  457. ctx->app_data = data;
  458. }
  459. uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) {
  460. return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK;
  461. }
  462. uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) {
  463. return ctx->cipher->flags & EVP_CIPH_MODE_MASK;
  464. }
  465. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) {
  466. int ret;
  467. if (!ctx->cipher) {
  468. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_CIPHER_SET);
  469. return 0;
  470. }
  471. if (!ctx->cipher->ctrl) {
  472. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_NOT_IMPLEMENTED);
  473. return 0;
  474. }
  475. ret = ctx->cipher->ctrl(ctx, command, arg, ptr);
  476. if (ret == -1) {
  477. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  478. return 0;
  479. }
  480. return ret;
  481. }
  482. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) {
  483. if (pad) {
  484. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  485. } else {
  486. ctx->flags |= EVP_CIPH_NO_PADDING;
  487. }
  488. return 1;
  489. }
  490. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, unsigned key_len) {
  491. if (c->key_len == key_len) {
  492. return 1;
  493. }
  494. if (key_len == 0 || !(c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  495. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_KEY_LENGTH);
  496. return 0;
  497. }
  498. c->key_len = key_len;
  499. return 1;
  500. }
  501. int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { return cipher->nid; }
  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, "des-ede3-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. }