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