cipher.c 17 KB

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