Coverage Report

Created: 2022-08-24 06:31

/src/libressl/crypto/evp/evp_enc.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: evp_enc.c,v 1.45 2022/07/26 19:50:06 tb Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
59
#include <limits.h>
60
#include <stdio.h>
61
#include <stdlib.h>
62
#include <string.h>
63
64
#include <sys/types.h>
65
66
#include <openssl/opensslconf.h>
67
68
#include <openssl/err.h>
69
#include <openssl/evp.h>
70
71
#ifndef OPENSSL_NO_ENGINE
72
#include <openssl/engine.h>
73
#endif
74
75
#include "evp_locl.h"
76
77
0
#define M_do_cipher(ctx, out, in, inl) ctx->cipher->do_cipher(ctx, out, in, inl)
78
79
int
80
EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
81
    const unsigned char *key, const unsigned char *iv, int enc)
82
0
{
83
0
  if (cipher)
84
0
    EVP_CIPHER_CTX_init(ctx);
85
0
  return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
86
0
}
87
88
int
89
EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
90
    const unsigned char *key, const unsigned char *iv, int enc)
91
0
{
92
0
  if (enc == -1)
93
0
    enc = ctx->encrypt;
94
0
  else {
95
0
    if (enc)
96
0
      enc = 1;
97
0
    ctx->encrypt = enc;
98
0
  }
99
0
#ifndef OPENSSL_NO_ENGINE
100
  /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts
101
   * so this context may already have an ENGINE! Try to avoid releasing
102
   * the previous handle, re-querying for an ENGINE, and having a
103
   * reinitialisation, when it may all be unecessary. */
104
0
  if (ctx->engine && ctx->cipher &&
105
0
      (!cipher || (cipher && (cipher->nid == ctx->cipher->nid))))
106
0
    goto skip_to_init;
107
0
#endif
108
0
  if (cipher) {
109
    /* Ensure a context left lying around from last time is cleared
110
     * (the previous check attempted to avoid this if the same
111
     * ENGINE and EVP_CIPHER could be used). */
112
0
    if (ctx->cipher) {
113
0
      unsigned long flags = ctx->flags;
114
0
      EVP_CIPHER_CTX_cleanup(ctx);
115
      /* Restore encrypt and flags */
116
0
      ctx->encrypt = enc;
117
0
      ctx->flags = flags;
118
0
    }
119
0
#ifndef OPENSSL_NO_ENGINE
120
0
    if (impl) {
121
0
      if (!ENGINE_init(impl)) {
122
0
        EVPerror(EVP_R_INITIALIZATION_ERROR);
123
0
        return 0;
124
0
      }
125
0
    } else
126
      /* Ask if an ENGINE is reserved for this job */
127
0
      impl = ENGINE_get_cipher_engine(cipher->nid);
128
0
    if (impl) {
129
      /* There's an ENGINE for this job ... (apparently) */
130
0
      const EVP_CIPHER *c =
131
0
          ENGINE_get_cipher(impl, cipher->nid);
132
0
      if (!c) {
133
0
        EVPerror(EVP_R_INITIALIZATION_ERROR);
134
0
        return 0;
135
0
      }
136
      /* We'll use the ENGINE's private cipher definition */
137
0
      cipher = c;
138
      /* Store the ENGINE functional reference so we know
139
       * 'cipher' came from an ENGINE and we need to release
140
       * it when done. */
141
0
      ctx->engine = impl;
142
0
    } else
143
0
      ctx->engine = NULL;
144
0
#endif
145
146
0
    ctx->cipher = cipher;
147
0
    if (ctx->cipher->ctx_size) {
148
0
      ctx->cipher_data = calloc(1, ctx->cipher->ctx_size);
149
0
      if (ctx->cipher_data == NULL) {
150
0
        EVPerror(ERR_R_MALLOC_FAILURE);
151
0
        return 0;
152
0
      }
153
0
    } else {
154
0
      ctx->cipher_data = NULL;
155
0
    }
156
0
    ctx->key_len = cipher->key_len;
157
0
    ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
158
0
    if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
159
0
      if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
160
0
        EVPerror(EVP_R_INITIALIZATION_ERROR);
161
0
        return 0;
162
0
      }
163
0
    }
164
0
  } else if (!ctx->cipher) {
165
0
    EVPerror(EVP_R_NO_CIPHER_SET);
166
0
    return 0;
167
0
  }
168
0
#ifndef OPENSSL_NO_ENGINE
169
0
skip_to_init:
170
0
#endif
171
  /* we assume block size is a power of 2 in *cryptUpdate */
172
0
  if (ctx->cipher->block_size != 1 &&
173
0
      ctx->cipher->block_size != 8 &&
174
0
      ctx->cipher->block_size != 16) {
175
0
    EVPerror(EVP_R_BAD_BLOCK_LENGTH);
176
0
    return 0;
177
0
  }
178
179
0
  if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW) &&
180
0
      EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
181
0
    EVPerror(EVP_R_WRAP_MODE_NOT_ALLOWED);
182
0
    return 0;
183
0
  }
184
185
0
  if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
186
0
    switch (EVP_CIPHER_CTX_mode(ctx)) {
187
188
0
    case EVP_CIPH_STREAM_CIPHER:
189
0
    case EVP_CIPH_ECB_MODE:
190
0
      break;
191
192
0
    case EVP_CIPH_CFB_MODE:
193
0
    case EVP_CIPH_OFB_MODE:
194
195
0
      ctx->num = 0;
196
      /* fall-through */
197
198
0
    case EVP_CIPH_CBC_MODE:
199
200
0
      if ((size_t)EVP_CIPHER_CTX_iv_length(ctx) >
201
0
          sizeof(ctx->iv)) {
202
0
        EVPerror(EVP_R_IV_TOO_LARGE);
203
0
        return 0;
204
0
      }
205
0
      if (iv)
206
0
        memcpy(ctx->oiv, iv,
207
0
            EVP_CIPHER_CTX_iv_length(ctx));
208
0
      memcpy(ctx->iv, ctx->oiv,
209
0
          EVP_CIPHER_CTX_iv_length(ctx));
210
0
      break;
211
212
0
    case EVP_CIPH_CTR_MODE:
213
0
      ctx->num = 0;
214
      /* Don't reuse IV for CTR mode */
215
0
      if (iv)
216
0
        memcpy(ctx->iv, iv,
217
0
            EVP_CIPHER_CTX_iv_length(ctx));
218
0
      break;
219
220
0
    default:
221
0
      return 0;
222
0
      break;
223
0
    }
224
0
  }
225
226
0
  if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
227
0
    if (!ctx->cipher->init(ctx, key, iv, enc))
228
0
      return 0;
229
0
  }
230
0
  ctx->buf_len = 0;
231
0
  ctx->final_used = 0;
232
0
  ctx->block_mask = ctx->cipher->block_size - 1;
233
0
  return 1;
234
0
}
235
236
int
237
EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
238
    const unsigned char *in, int inl)
239
0
{
240
0
  if (ctx->encrypt)
241
0
    return EVP_EncryptUpdate(ctx, out, outl, in, inl);
242
0
  else
243
0
    return EVP_DecryptUpdate(ctx, out, outl, in, inl);
244
0
}
245
246
int
247
EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
248
0
{
249
0
  if (ctx->encrypt)
250
0
    return EVP_EncryptFinal_ex(ctx, out, outl);
251
0
  else
252
0
    return EVP_DecryptFinal_ex(ctx, out, outl);
253
0
}
254
255
__warn_references(EVP_CipherFinal,
256
    "EVP_CipherFinal is often misused, please use EVP_CipherFinal_ex and EVP_CIPHER_CTX_cleanup");
257
258
int
259
EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
260
0
{
261
0
  int ret;
262
0
  if (ctx->encrypt)
263
0
    ret = EVP_EncryptFinal_ex(ctx, out, outl);
264
0
  else
265
0
    ret = EVP_DecryptFinal_ex(ctx, out, outl);
266
0
  return ret;
267
0
}
268
269
int
270
EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
271
    const unsigned char *key, const unsigned char *iv)
272
0
{
273
0
  return EVP_CipherInit(ctx, cipher, key, iv, 1);
274
0
}
275
276
int
277
EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
278
    const unsigned char *key, const unsigned char *iv)
279
0
{
280
0
  return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
281
0
}
282
283
int
284
EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
285
    const unsigned char *key, const unsigned char *iv)
286
0
{
287
0
  return EVP_CipherInit(ctx, cipher, key, iv, 0);
288
0
}
289
290
int
291
EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
292
    const unsigned char *key, const unsigned char *iv)
293
0
{
294
0
  return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
295
0
}
296
297
int
298
EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
299
    const unsigned char *in, int inl)
300
0
{
301
0
  int i, j, bl;
302
303
0
  *outl = 0;
304
305
0
  if (inl < 0)
306
0
    return 0;
307
308
0
  if (inl == 0 && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)
309
0
    return 1;
310
311
0
  if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
312
0
    i = M_do_cipher(ctx, out, in, inl);
313
0
    if (i < 0)
314
0
      return 0;
315
0
    else
316
0
      *outl = i;
317
0
    return 1;
318
0
  }
319
320
0
  if (ctx->buf_len == 0 && (inl&(ctx->block_mask)) == 0) {
321
0
    if (M_do_cipher(ctx, out, in, inl)) {
322
0
      *outl = inl;
323
0
      return 1;
324
0
    } else {
325
0
      *outl = 0;
326
0
      return 0;
327
0
    }
328
0
  }
329
0
  i = ctx->buf_len;
330
0
  bl = ctx->cipher->block_size;
331
0
  if ((size_t)bl > sizeof(ctx->buf)) {
332
0
    EVPerror(EVP_R_BAD_BLOCK_LENGTH);
333
0
    *outl = 0;
334
0
    return 0;
335
0
  }
336
0
  if (i != 0) {
337
0
    if (bl - i > inl) {
338
0
      memcpy(&(ctx->buf[i]), in, inl);
339
0
      ctx->buf_len += inl;
340
0
      *outl = 0;
341
0
      return 1;
342
0
    } else {
343
0
      j = bl - i;
344
345
      /*
346
       * Once we've processed the first j bytes from in, the
347
       * amount of data left that is a multiple of the block
348
       * length is (inl - j) & ~(bl - 1).  Ensure this plus
349
       * the block processed from ctx-buf doesn't overflow.
350
       */
351
0
      if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
352
0
        EVPerror(EVP_R_TOO_LARGE);
353
0
        return 0;
354
0
      }
355
0
      memcpy(&(ctx->buf[i]), in, j);
356
0
      if (!M_do_cipher(ctx, out, ctx->buf, bl))
357
0
        return 0;
358
0
      inl -= j;
359
0
      in += j;
360
0
      out += bl;
361
0
      *outl = bl;
362
0
    }
363
0
  } else
364
0
    *outl = 0;
365
0
  i = inl&(bl - 1);
366
0
  inl -= i;
367
0
  if (inl > 0) {
368
0
    if (!M_do_cipher(ctx, out, in, inl))
369
0
      return 0;
370
0
    *outl += inl;
371
0
  }
372
373
0
  if (i != 0)
374
0
    memcpy(ctx->buf, &(in[inl]), i);
375
0
  ctx->buf_len = i;
376
0
  return 1;
377
0
}
378
379
__warn_references(EVP_EncryptFinal,
380
    "EVP_EncryptFinal is often misused, please use EVP_EncryptFinal_ex and EVP_CIPHER_CTX_cleanup");
381
382
int
383
EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
384
0
{
385
0
  int ret;
386
387
0
  ret = EVP_EncryptFinal_ex(ctx, out, outl);
388
0
  return ret;
389
0
}
390
391
int
392
EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
393
0
{
394
0
  int n, ret;
395
0
  unsigned int i, b, bl;
396
397
0
  if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
398
0
    ret = M_do_cipher(ctx, out, NULL, 0);
399
0
    if (ret < 0)
400
0
      return 0;
401
0
    else
402
0
      *outl = ret;
403
0
    return 1;
404
0
  }
405
406
0
  b = ctx->cipher->block_size;
407
0
  if (b > sizeof ctx->buf) {
408
0
    EVPerror(EVP_R_BAD_BLOCK_LENGTH);
409
0
    return 0;
410
0
  }
411
0
  if (b == 1) {
412
0
    *outl = 0;
413
0
    return 1;
414
0
  }
415
0
  bl = ctx->buf_len;
416
0
  if (ctx->flags & EVP_CIPH_NO_PADDING) {
417
0
    if (bl) {
418
0
      EVPerror(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
419
0
      return 0;
420
0
    }
421
0
    *outl = 0;
422
0
    return 1;
423
0
  }
424
425
0
  n = b - bl;
426
0
  for (i = bl; i < b; i++)
427
0
    ctx->buf[i] = n;
428
0
  ret = M_do_cipher(ctx, out, ctx->buf, b);
429
430
431
0
  if (ret)
432
0
    *outl = b;
433
434
0
  return ret;
435
0
}
436
437
int
438
EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
439
    const unsigned char *in, int inl)
440
0
{
441
0
  int fix_len;
442
0
  unsigned int b;
443
444
0
  *outl = 0;
445
446
0
  if (inl < 0)
447
0
    return 0;
448
449
0
  if (inl == 0 && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)
450
0
    return 1;
451
452
0
  if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
453
0
    fix_len = M_do_cipher(ctx, out, in, inl);
454
0
    if (fix_len < 0) {
455
0
      *outl = 0;
456
0
      return 0;
457
0
    } else
458
0
      *outl = fix_len;
459
0
    return 1;
460
0
  }
461
462
0
  if (ctx->flags & EVP_CIPH_NO_PADDING)
463
0
    return EVP_EncryptUpdate(ctx, out, outl, in, inl);
464
465
0
  b = ctx->cipher->block_size;
466
0
  if (b > sizeof ctx->final) {
467
0
    EVPerror(EVP_R_BAD_BLOCK_LENGTH);
468
0
    return 0;
469
0
  }
470
471
0
  if (ctx->final_used) {
472
    /*
473
     * final_used is only ever set if buf_len is 0. Therefore the
474
     * maximum length output we will ever see from EVP_EncryptUpdate
475
     * is inl & ~(b - 1). Since final_used is set, the final output
476
     * length is (inl & ~(b - 1)) + b. Ensure it doesn't overflow.
477
     */
478
0
    if ((inl & ~(b - 1)) > INT_MAX - b) {
479
0
      EVPerror(EVP_R_TOO_LARGE);
480
0
      return 0;
481
0
    }
482
0
    memcpy(out, ctx->final, b);
483
0
    out += b;
484
0
    fix_len = 1;
485
0
  } else
486
0
    fix_len = 0;
487
488
489
0
  if (!EVP_EncryptUpdate(ctx, out, outl, in, inl))
490
0
    return 0;
491
492
  /* if we have 'decrypted' a multiple of block size, make sure
493
   * we have a copy of this last block */
494
0
  if (b > 1 && !ctx->buf_len) {
495
0
    *outl -= b;
496
0
    ctx->final_used = 1;
497
0
    memcpy(ctx->final, &out[*outl], b);
498
0
  } else
499
0
    ctx->final_used = 0;
500
501
0
  if (fix_len)
502
0
    *outl += b;
503
504
0
  return 1;
505
0
}
506
507
__warn_references(EVP_DecryptFinal,
508
    "EVP_DecryptFinal is often misused, please use EVP_DecryptFinal_ex and EVP_CIPHER_CTX_cleanup");
509
510
int
511
EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
512
0
{
513
0
  int ret;
514
515
0
  ret = EVP_DecryptFinal_ex(ctx, out, outl);
516
0
  return ret;
517
0
}
518
519
int
520
EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
521
0
{
522
0
  int i, n;
523
0
  unsigned int b;
524
0
  *outl = 0;
525
526
0
  if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
527
0
    i = M_do_cipher(ctx, out, NULL, 0);
528
0
    if (i < 0)
529
0
      return 0;
530
0
    else
531
0
      *outl = i;
532
0
    return 1;
533
0
  }
534
535
0
  b = ctx->cipher->block_size;
536
0
  if (ctx->flags & EVP_CIPH_NO_PADDING) {
537
0
    if (ctx->buf_len) {
538
0
      EVPerror(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
539
0
      return 0;
540
0
    }
541
0
    *outl = 0;
542
0
    return 1;
543
0
  }
544
0
  if (b > 1) {
545
0
    if (ctx->buf_len || !ctx->final_used) {
546
0
      EVPerror(EVP_R_WRONG_FINAL_BLOCK_LENGTH);
547
0
      return (0);
548
0
    }
549
0
    if (b > sizeof ctx->final) {
550
0
      EVPerror(EVP_R_BAD_BLOCK_LENGTH);
551
0
      return 0;
552
0
    }
553
0
    n = ctx->final[b - 1];
554
0
    if (n == 0 || n > (int)b) {
555
0
      EVPerror(EVP_R_BAD_DECRYPT);
556
0
      return (0);
557
0
    }
558
0
    for (i = 0; i < n; i++) {
559
0
      if (ctx->final[--b] != n) {
560
0
        EVPerror(EVP_R_BAD_DECRYPT);
561
0
        return (0);
562
0
      }
563
0
    }
564
0
    n = ctx->cipher->block_size - n;
565
0
    for (i = 0; i < n; i++)
566
0
      out[i] = ctx->final[i];
567
0
    *outl = n;
568
0
  } else
569
0
    *outl = 0;
570
0
  return (1);
571
0
}
572
573
EVP_CIPHER_CTX *
574
EVP_CIPHER_CTX_new(void)
575
0
{
576
0
  return calloc(1, sizeof(EVP_CIPHER_CTX));
577
0
}
578
579
void
580
EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
581
0
{
582
0
  if (ctx == NULL)
583
0
    return;
584
585
0
  EVP_CIPHER_CTX_cleanup(ctx);
586
587
0
  free(ctx);
588
0
}
589
590
void
591
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
592
0
{
593
0
  memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
594
0
}
595
596
int
597
EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *a)
598
0
{
599
0
  return EVP_CIPHER_CTX_cleanup(a);
600
0
}
601
602
int
603
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
604
0
{
605
0
  if (c->cipher != NULL) {
606
0
    if (c->cipher->cleanup && !c->cipher->cleanup(c))
607
0
      return 0;
608
    /* Cleanse cipher context data */
609
0
    if (c->cipher_data)
610
0
      explicit_bzero(c->cipher_data, c->cipher->ctx_size);
611
0
  }
612
  /* XXX - store size of cipher_data so we can always freezero(). */
613
0
  free(c->cipher_data);
614
0
#ifndef OPENSSL_NO_ENGINE
615
0
  ENGINE_finish(c->engine);
616
0
#endif
617
0
  explicit_bzero(c, sizeof(EVP_CIPHER_CTX));
618
0
  return 1;
619
0
}
620
621
int
622
EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
623
0
{
624
0
  if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
625
0
    return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH,
626
0
        keylen, NULL);
627
0
  if (c->key_len == keylen)
628
0
    return 1;
629
0
  if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
630
0
    c->key_len = keylen;
631
0
    return 1;
632
0
  }
633
0
  EVPerror(EVP_R_INVALID_KEY_LENGTH);
634
0
  return 0;
635
0
}
636
637
int
638
EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
639
0
{
640
0
  if (pad)
641
0
    ctx->flags &= ~EVP_CIPH_NO_PADDING;
642
0
  else
643
0
    ctx->flags |= EVP_CIPH_NO_PADDING;
644
0
  return 1;
645
0
}
646
647
int
648
EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
649
0
{
650
0
  int ret;
651
652
0
  if (!ctx->cipher) {
653
0
    EVPerror(EVP_R_NO_CIPHER_SET);
654
0
    return 0;
655
0
  }
656
657
0
  if (!ctx->cipher->ctrl) {
658
0
    EVPerror(EVP_R_CTRL_NOT_IMPLEMENTED);
659
0
    return 0;
660
0
  }
661
662
0
  ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
663
0
  if (ret == -1) {
664
0
    EVPerror(EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
665
0
    return 0;
666
0
  }
667
0
  return ret;
668
0
}
669
670
int
671
EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
672
0
{
673
0
  if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
674
0
    return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
675
0
  arc4random_buf(key, ctx->key_len);
676
0
  return 1;
677
0
}
678
679
int
680
EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
681
0
{
682
0
  if ((in == NULL) || (in->cipher == NULL)) {
683
0
    EVPerror(EVP_R_INPUT_NOT_INITIALIZED);
684
0
    return 0;
685
0
  }
686
0
#ifndef OPENSSL_NO_ENGINE
687
  /* Make sure it's safe to copy a cipher context using an ENGINE */
688
0
  if (in->engine && !ENGINE_init(in->engine)) {
689
0
    EVPerror(ERR_R_ENGINE_LIB);
690
0
    return 0;
691
0
  }
692
0
#endif
693
694
0
  EVP_CIPHER_CTX_cleanup(out);
695
0
  memcpy(out, in, sizeof *out);
696
697
0
  if (in->cipher_data && in->cipher->ctx_size) {
698
0
    out->cipher_data = calloc(1, in->cipher->ctx_size);
699
0
    if (out->cipher_data == NULL) {
700
0
      EVPerror(ERR_R_MALLOC_FAILURE);
701
0
      return 0;
702
0
    }
703
0
    memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
704
0
  }
705
706
0
  if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) {
707
0
    if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY,
708
0
        0, out)) {
709
      /*
710
       * If the custom copy control failed, assume that there
711
       * may still be pointers copied in the cipher_data that
712
       * we do not own. This may result in a leak from a bad
713
       * custom copy control, but that's preferable to a
714
       * double free...
715
       */
716
0
      freezero(out->cipher_data, in->cipher->ctx_size);
717
0
      out->cipher_data = NULL;
718
0
      return 0;
719
0
    }
720
0
  }
721
722
0
  return 1;
723
0
}