You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

652 rivejä
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) {
  89. if (c->cipher->cleanup) {
  90. c->cipher->cleanup(c);
  91. }
  92. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  93. }
  94. OPENSSL_free(c->cipher_data);
  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. ctx->cipher = NULL;
  149. OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, ERR_R_MALLOC_FAILURE);
  150. return 0;
  151. }
  152. } else {
  153. ctx->cipher_data = NULL;
  154. }
  155. ctx->key_len = cipher->key_len;
  156. ctx->flags = 0;
  157. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  158. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  159. ctx->cipher = NULL;
  160. OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, CIPHER_R_INITIALIZATION_ERROR);
  161. return 0;
  162. }
  163. }
  164. } else if (!ctx->cipher) {
  165. OPENSSL_PUT_ERROR(CIPHER, EVP_CipherInit_ex, CIPHER_R_NO_CIPHER_SET);
  166. return 0;
  167. }
  168. /* we assume block size is a power of 2 in *cryptUpdate */
  169. assert(ctx->cipher->block_size == 1 || ctx->cipher->block_size == 8 ||
  170. ctx->cipher->block_size == 16);
  171. if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
  172. switch (EVP_CIPHER_CTX_mode(ctx)) {
  173. case EVP_CIPH_STREAM_CIPHER:
  174. case EVP_CIPH_ECB_MODE:
  175. break;
  176. case EVP_CIPH_CFB_MODE:
  177. ctx->num = 0;
  178. /* fall-through */
  179. case EVP_CIPH_CBC_MODE:
  180. assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
  181. if (iv) {
  182. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  183. }
  184. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  185. break;
  186. case EVP_CIPH_CTR_MODE:
  187. case EVP_CIPH_OFB_MODE:
  188. ctx->num = 0;
  189. /* Don't reuse IV for CTR mode */
  190. if (iv) {
  191. memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  192. }
  193. break;
  194. default:
  195. return 0;
  196. }
  197. }
  198. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  199. if (!ctx->cipher->init(ctx, key, iv, enc)) {
  200. return 0;
  201. }
  202. }
  203. ctx->buf_len = 0;
  204. ctx->final_used = 0;
  205. ctx->block_mask = ctx->cipher->block_size - 1;
  206. return 1;
  207. }
  208. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  209. ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
  210. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  211. }
  212. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  213. ENGINE *impl, const uint8_t *key, const uint8_t *iv) {
  214. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  215. }
  216. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  217. const uint8_t *in, int in_len) {
  218. int i, j, bl;
  219. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  220. i = ctx->cipher->cipher(ctx, out, in, in_len);
  221. if (i < 0) {
  222. return 0;
  223. } else {
  224. *out_len = i;
  225. }
  226. return 1;
  227. }
  228. if (in_len <= 0) {
  229. *out_len = 0;
  230. return in_len == 0;
  231. }
  232. if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) {
  233. if (ctx->cipher->cipher(ctx, out, in, in_len)) {
  234. *out_len = in_len;
  235. return 1;
  236. } else {
  237. *out_len = 0;
  238. return 0;
  239. }
  240. }
  241. i = ctx->buf_len;
  242. bl = ctx->cipher->block_size;
  243. assert(bl <= (int)sizeof(ctx->buf));
  244. if (i != 0) {
  245. if (i + in_len < bl) {
  246. memcpy(&ctx->buf[i], in, in_len);
  247. ctx->buf_len += in_len;
  248. *out_len = 0;
  249. return 1;
  250. } else {
  251. j = bl - i;
  252. memcpy(&ctx->buf[i], in, j);
  253. if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) {
  254. return 0;
  255. }
  256. in_len -= j;
  257. in += j;
  258. out += bl;
  259. *out_len = bl;
  260. }
  261. } else {
  262. *out_len = 0;
  263. }
  264. i = in_len & ctx->block_mask;
  265. in_len -= i;
  266. if (in_len > 0) {
  267. if (!ctx->cipher->cipher(ctx, out, in, in_len)) {
  268. return 0;
  269. }
  270. *out_len += in_len;
  271. }
  272. if (i != 0) {
  273. memcpy(ctx->buf, &in[in_len], i);
  274. }
  275. ctx->buf_len = i;
  276. return 1;
  277. }
  278. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
  279. int n, ret;
  280. unsigned int i, b, bl;
  281. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  282. ret = ctx->cipher->cipher(ctx, out, NULL, 0);
  283. if (ret < 0) {
  284. return 0;
  285. } else {
  286. *out_len = ret;
  287. }
  288. return 1;
  289. }
  290. b = ctx->cipher->block_size;
  291. assert(b <= sizeof(ctx->buf));
  292. if (b == 1) {
  293. *out_len = 0;
  294. return 1;
  295. }
  296. bl = ctx->buf_len;
  297. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  298. if (bl) {
  299. OPENSSL_PUT_ERROR(CIPHER, EVP_EncryptFinal_ex,
  300. CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  301. return 0;
  302. }
  303. *out_len = 0;
  304. return 1;
  305. }
  306. n = b - bl;
  307. for (i = bl; i < b; i++) {
  308. ctx->buf[i] = n;
  309. }
  310. ret = ctx->cipher->cipher(ctx, out, ctx->buf, b);
  311. if (ret) {
  312. *out_len = b;
  313. }
  314. return ret;
  315. }
  316. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  317. const uint8_t *in, int in_len) {
  318. int fix_len;
  319. unsigned int b;
  320. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  321. int r = ctx->cipher->cipher(ctx, out, in, in_len);
  322. if (r < 0) {
  323. *out_len = 0;
  324. return 0;
  325. } else {
  326. *out_len = r;
  327. }
  328. return 1;
  329. }
  330. if (in_len <= 0) {
  331. *out_len = 0;
  332. return in_len == 0;
  333. }
  334. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  335. return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
  336. }
  337. b = ctx->cipher->block_size;
  338. assert(b <= sizeof(ctx->final));
  339. if (ctx->final_used) {
  340. memcpy(out, ctx->final, b);
  341. out += b;
  342. fix_len = 1;
  343. } else {
  344. fix_len = 0;
  345. }
  346. if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) {
  347. return 0;
  348. }
  349. /* if we have 'decrypted' a multiple of block size, make sure
  350. * we have a copy of this last block */
  351. if (b > 1 && !ctx->buf_len) {
  352. *out_len -= b;
  353. ctx->final_used = 1;
  354. memcpy(ctx->final, &out[*out_len], b);
  355. } else {
  356. ctx->final_used = 0;
  357. }
  358. if (fix_len) {
  359. *out_len += b;
  360. }
  361. return 1;
  362. }
  363. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) {
  364. int i, n;
  365. unsigned int b;
  366. *out_len = 0;
  367. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  368. i = ctx->cipher->cipher(ctx, out, NULL, 0);
  369. if (i < 0) {
  370. return 0;
  371. } else {
  372. *out_len = i;
  373. }
  374. return 1;
  375. }
  376. b = ctx->cipher->block_size;
  377. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  378. if (ctx->buf_len) {
  379. OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex,
  380. CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  381. return 0;
  382. }
  383. *out_len = 0;
  384. return 1;
  385. }
  386. if (b > 1) {
  387. if (ctx->buf_len || !ctx->final_used) {
  388. OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex,
  389. CIPHER_R_WRONG_FINAL_BLOCK_LENGTH);
  390. return 0;
  391. }
  392. assert(b <= sizeof(ctx->final));
  393. /* The following assumes that the ciphertext has been authenticated.
  394. * Otherwise it provides a padding oracle. */
  395. n = ctx->final[b - 1];
  396. if (n == 0 || n > (int)b) {
  397. OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex, CIPHER_R_BAD_DECRYPT);
  398. return 0;
  399. }
  400. for (i = 0; i < n; i++) {
  401. if (ctx->final[--b] != n) {
  402. OPENSSL_PUT_ERROR(CIPHER, EVP_DecryptFinal_ex, CIPHER_R_BAD_DECRYPT);
  403. return 0;
  404. }
  405. }
  406. n = ctx->cipher->block_size - n;
  407. for (i = 0; i < n; i++) {
  408. out[i] = ctx->final[i];
  409. }
  410. *out_len = n;
  411. } else {
  412. *out_len = 0;
  413. }
  414. return 1;
  415. }
  416. int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  417. size_t in_len) {
  418. return ctx->cipher->cipher(ctx, out, in, in_len);
  419. }
  420. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
  421. const uint8_t *in, int in_len) {
  422. if (ctx->encrypt) {
  423. return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
  424. } else {
  425. return EVP_DecryptUpdate(ctx, out, out_len, in, in_len);
  426. }
  427. }
  428. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
  429. if (ctx->encrypt) {
  430. return EVP_EncryptFinal_ex(ctx, out, out_len);
  431. } else {
  432. return EVP_DecryptFinal_ex(ctx, out, out_len);
  433. }
  434. }
  435. const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) {
  436. return ctx->cipher;
  437. }
  438. int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) {
  439. return ctx->cipher->nid;
  440. }
  441. unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) {
  442. return ctx->cipher->block_size;
  443. }
  444. unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) {
  445. return ctx->key_len;
  446. }
  447. unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) {
  448. return ctx->cipher->iv_len;
  449. }
  450. void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) {
  451. return ctx->app_data;
  452. }
  453. void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) {
  454. ctx->app_data = data;
  455. }
  456. uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) {
  457. return ctx->cipher->flags & ~EVP_CIPH_MODE_MASK;
  458. }
  459. uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx) {
  460. return ctx->cipher->flags & EVP_CIPH_MODE_MASK;
  461. }
  462. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, int arg, void *ptr) {
  463. int ret;
  464. if (!ctx->cipher) {
  465. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl, CIPHER_R_NO_CIPHER_SET);
  466. return 0;
  467. }
  468. if (!ctx->cipher->ctrl) {
  469. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl, CIPHER_R_CTRL_NOT_IMPLEMENTED);
  470. return 0;
  471. }
  472. ret = ctx->cipher->ctrl(ctx, command, arg, ptr);
  473. if (ret == -1) {
  474. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_ctrl,
  475. CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  476. return 0;
  477. }
  478. return ret;
  479. }
  480. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) {
  481. if (pad) {
  482. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  483. } else {
  484. ctx->flags |= EVP_CIPH_NO_PADDING;
  485. }
  486. return 1;
  487. }
  488. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, unsigned key_len) {
  489. if (c->key_len == key_len) {
  490. return 1;
  491. }
  492. if (key_len == 0 || !(c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  493. OPENSSL_PUT_ERROR(CIPHER, EVP_CIPHER_CTX_set_key_length,
  494. CIPHER_R_INVALID_KEY_LENGTH);
  495. return 0;
  496. }
  497. c->key_len = key_len;
  498. return 1;
  499. }
  500. int EVP_CIPHER_nid(const EVP_CIPHER *cipher) { return cipher->nid; }
  501. unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher) {
  502. return cipher->block_size;
  503. }
  504. unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher) {
  505. return cipher->key_len;
  506. }
  507. unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) {
  508. return cipher->iv_len;
  509. }
  510. uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher) {
  511. return cipher->flags & ~EVP_CIPH_MODE_MASK;
  512. }
  513. uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) {
  514. return cipher->flags & EVP_CIPH_MODE_MASK;
  515. }
  516. int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  517. const uint8_t *key, const uint8_t *iv, int enc) {
  518. if (cipher) {
  519. EVP_CIPHER_CTX_init(ctx);
  520. }
  521. return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
  522. }
  523. int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  524. const uint8_t *key, const uint8_t *iv) {
  525. return EVP_CipherInit(ctx, cipher, key, iv, 1);
  526. }
  527. int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  528. const uint8_t *key, const uint8_t *iv) {
  529. return EVP_CipherInit(ctx, cipher, key, iv, 0);
  530. }
  531. int EVP_add_cipher_alias(const char *a, const char *b) {
  532. return 1;
  533. }
  534. const EVP_CIPHER *EVP_get_cipherbyname(const char *name) {
  535. if (OPENSSL_strcasecmp(name, "rc4") == 0) {
  536. return EVP_rc4();
  537. } else if (OPENSSL_strcasecmp(name, "des-cbc") == 0) {
  538. return EVP_des_cbc();
  539. } else if (OPENSSL_strcasecmp(name, "3des-cbc") == 0 ||
  540. OPENSSL_strcasecmp(name, "3des") == 0) {
  541. return EVP_des_ede3_cbc();
  542. } else if (OPENSSL_strcasecmp(name, "aes-128-cbc") == 0) {
  543. return EVP_aes_128_cbc();
  544. } else if (OPENSSL_strcasecmp(name, "aes-256-cbc") == 0) {
  545. return EVP_aes_256_cbc();
  546. } else if (OPENSSL_strcasecmp(name, "aes-128-ctr") == 0) {
  547. return EVP_aes_128_ctr();
  548. } else if (OPENSSL_strcasecmp(name, "aes-256-ctr") == 0) {
  549. return EVP_aes_256_ctr();
  550. } else if (OPENSSL_strcasecmp(name, "aes-128-ecb") == 0) {
  551. return EVP_aes_128_ecb();
  552. } else if (OPENSSL_strcasecmp(name, "aes-256-ecb") == 0) {
  553. return EVP_aes_256_ecb();
  554. }
  555. return NULL;
  556. }