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