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