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