Coverage Report

Created: 2022-08-24 06:31

/src/libressl/crypto/evp/evp_lib.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: evp_lib.c,v 1.24 2022/01/10 13:42:28 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 <stdio.h>
60
#include <string.h>
61
62
#include <openssl/err.h>
63
#include <openssl/evp.h>
64
#include <openssl/objects.h>
65
66
#include "asn1_locl.h"
67
#include "evp_locl.h"
68
69
int
70
EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
71
0
{
72
0
  int ret;
73
74
0
  if (c->cipher->set_asn1_parameters != NULL)
75
0
    ret = c->cipher->set_asn1_parameters(c, type);
76
0
  else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1)
77
0
    ret = EVP_CIPHER_set_asn1_iv(c, type);
78
0
  else
79
0
    ret = -1;
80
0
  return (ret);
81
0
}
82
83
int
84
EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
85
0
{
86
0
  int ret;
87
88
0
  if (c->cipher->get_asn1_parameters != NULL)
89
0
    ret = c->cipher->get_asn1_parameters(c, type);
90
0
  else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1)
91
0
    ret = EVP_CIPHER_get_asn1_iv(c, type);
92
0
  else
93
0
    ret = -1;
94
0
  return (ret);
95
0
}
96
97
int
98
EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
99
0
{
100
0
  int i = 0;
101
0
  unsigned int l;
102
103
0
  if (type != NULL) {
104
0
    l = EVP_CIPHER_CTX_iv_length(c);
105
0
    if (l > sizeof(c->iv)) {
106
0
      EVPerror(EVP_R_IV_TOO_LARGE);
107
0
      return 0;
108
0
    }
109
0
    i = ASN1_TYPE_get_octetstring(type, c->oiv, l);
110
0
    if (i != (int)l)
111
0
      return (-1);
112
0
    else if (i > 0)
113
0
      memcpy(c->iv, c->oiv, l);
114
0
  }
115
0
  return (i);
116
0
}
117
118
int
119
EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
120
0
{
121
0
  int i = 0;
122
0
  unsigned int j;
123
124
0
  if (type != NULL) {
125
0
    j = EVP_CIPHER_CTX_iv_length(c);
126
0
    if (j > sizeof(c->iv)) {
127
0
      EVPerror(EVP_R_IV_TOO_LARGE);
128
0
      return 0;
129
0
    }
130
0
    i = ASN1_TYPE_set_octetstring(type, c->oiv, j);
131
0
  }
132
0
  return (i);
133
0
}
134
135
/* Convert the various cipher NIDs and dummies to a proper OID NID */
136
int
137
EVP_CIPHER_type(const EVP_CIPHER *ctx)
138
628
{
139
628
  int nid;
140
628
  ASN1_OBJECT *otmp;
141
628
  nid = EVP_CIPHER_nid(ctx);
142
143
628
  switch (nid) {
144
0
  case NID_rc2_cbc:
145
0
  case NID_rc2_64_cbc:
146
0
  case NID_rc2_40_cbc:
147
0
    return NID_rc2_cbc;
148
149
42
  case NID_rc4:
150
42
  case NID_rc4_40:
151
42
    return NID_rc4;
152
153
0
  case NID_aes_128_cfb128:
154
0
  case NID_aes_128_cfb8:
155
0
  case NID_aes_128_cfb1:
156
0
    return NID_aes_128_cfb128;
157
158
0
  case NID_aes_192_cfb128:
159
0
  case NID_aes_192_cfb8:
160
0
  case NID_aes_192_cfb1:
161
0
    return NID_aes_192_cfb128;
162
163
0
  case NID_aes_256_cfb128:
164
0
  case NID_aes_256_cfb8:
165
0
  case NID_aes_256_cfb1:
166
0
    return NID_aes_256_cfb128;
167
168
0
  case NID_des_cfb64:
169
0
  case NID_des_cfb8:
170
0
  case NID_des_cfb1:
171
0
    return NID_des_cfb64;
172
173
0
  case NID_des_ede3_cfb64:
174
0
  case NID_des_ede3_cfb8:
175
0
  case NID_des_ede3_cfb1:
176
0
    return NID_des_cfb64;
177
178
586
  default:
179
    /* Check it has an OID and it is valid */
180
586
    otmp = OBJ_nid2obj(nid);
181
586
    if (!otmp || !otmp->data)
182
63
      nid = NID_undef;
183
586
    ASN1_OBJECT_free(otmp);
184
586
    return nid;
185
628
  }
186
628
}
187
188
int
189
EVP_CIPHER_block_size(const EVP_CIPHER *e)
190
0
{
191
0
  return e->block_size;
192
0
}
193
194
int
195
EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
196
760
{
197
760
  return ctx->cipher->block_size;
198
760
}
199
200
int
201
EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in,
202
    unsigned int inl)
203
755
{
204
755
  return ctx->cipher->do_cipher(ctx, out, in, inl);
205
755
}
206
207
const EVP_CIPHER *
208
EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
209
0
{
210
0
  return ctx->cipher;
211
0
}
212
213
int
214
EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
215
0
{
216
0
  return ctx->encrypt;
217
0
}
218
219
unsigned long
220
EVP_CIPHER_flags(const EVP_CIPHER *cipher)
221
1.04k
{
222
1.04k
  return cipher->flags;
223
1.04k
}
224
225
unsigned long
226
EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
227
2.68k
{
228
2.68k
  return ctx->cipher->flags;
229
2.68k
}
230
231
void *
232
EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
233
0
{
234
0
  return ctx->app_data;
235
0
}
236
237
void
238
EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
239
0
{
240
0
  ctx->app_data = data;
241
0
}
242
243
void *
244
EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
245
0
{
246
0
  return ctx->cipher_data;
247
0
}
248
249
void *
250
EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
251
0
{
252
0
  void *old_cipher_data;
253
254
0
  old_cipher_data = ctx->cipher_data;
255
0
  ctx->cipher_data = cipher_data;
256
257
0
  return old_cipher_data;
258
0
}
259
260
int
261
EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
262
1.14k
{
263
1.14k
  return cipher->iv_len;
264
1.14k
}
265
266
int
267
EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
268
2.07k
{
269
2.07k
  return ctx->cipher->iv_len;
270
2.07k
}
271
272
unsigned char *
273
EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
274
0
{
275
0
  return ctx->buf;
276
0
}
277
278
int
279
EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
280
1.14k
{
281
1.14k
  return cipher->key_len;
282
1.14k
}
283
284
int
285
EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
286
42
{
287
42
  return ctx->key_len;
288
42
}
289
290
int
291
EVP_CIPHER_nid(const EVP_CIPHER *cipher)
292
628
{
293
628
  return cipher->nid;
294
628
}
295
296
int
297
EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
298
0
{
299
0
  return ctx->cipher->nid;
300
0
}
301
302
int
303
EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, unsigned char *iv, size_t len)
304
0
{
305
0
  if (ctx == NULL || len != EVP_CIPHER_CTX_iv_length(ctx))
306
0
    return 0;
307
0
  if (len > EVP_MAX_IV_LENGTH)
308
0
    return 0; /* sanity check; shouldn't happen */
309
  /*
310
   * Skip the memcpy entirely when the requested IV length is zero,
311
   * since the iv pointer may be NULL or invalid.
312
   */
313
0
  if (len != 0) {
314
0
    if (iv == NULL)
315
0
      return 0;
316
0
    memcpy(iv, ctx->iv, len);
317
0
  }
318
0
  return 1;
319
0
}
320
321
int
322
EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, const unsigned char *iv, size_t len)
323
0
{
324
0
  if (ctx == NULL || len != EVP_CIPHER_CTX_iv_length(ctx))
325
0
    return 0;
326
0
  if (len > EVP_MAX_IV_LENGTH)
327
0
    return 0; /* sanity check; shouldn't happen */
328
  /*
329
   * Skip the memcpy entirely when the requested IV length is zero,
330
   * since the iv pointer may be NULL or invalid.
331
   */
332
0
  if (len != 0) {
333
0
    if (iv == NULL)
334
0
      return 0;
335
0
    memcpy(ctx->iv, iv, len);
336
0
  }
337
0
  return 1;
338
0
}
339
340
int
341
EVP_MD_block_size(const EVP_MD *md)
342
41.8k
{
343
41.8k
  return md->block_size;
344
41.8k
}
345
346
int
347
EVP_MD_type(const EVP_MD *md)
348
4.52k
{
349
4.52k
  return md->type;
350
4.52k
}
351
352
int
353
EVP_MD_pkey_type(const EVP_MD *md)
354
0
{
355
0
  return md->pkey_type;
356
0
}
357
358
int
359
EVP_MD_size(const EVP_MD *md)
360
20.4k
{
361
20.4k
  if (!md) {
362
0
    EVPerror(EVP_R_MESSAGE_DIGEST_IS_NULL);
363
0
    return -1;
364
0
  }
365
20.4k
  return md->md_size;
366
20.4k
}
367
368
unsigned long
369
EVP_MD_flags(const EVP_MD *md)
370
0
{
371
0
  return md->flags;
372
0
}
373
374
EVP_MD *
375
EVP_MD_meth_new(int md_type, int pkey_type)
376
0
{
377
0
  EVP_MD *md;
378
379
0
  if ((md = calloc(1, sizeof(*md))) == NULL)
380
0
    return NULL;
381
382
0
  md->type = md_type;
383
0
  md->pkey_type = pkey_type;
384
385
0
  return md;
386
0
}
387
388
EVP_MD *
389
EVP_MD_meth_dup(const EVP_MD *md)
390
0
{
391
0
  EVP_MD *to;
392
393
0
  if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) == NULL)
394
0
    return NULL;
395
396
0
  memcpy(to, md, sizeof(*to));
397
398
0
  return to;
399
0
}
400
401
void
402
EVP_MD_meth_free(EVP_MD *md)
403
0
{
404
0
  freezero(md, sizeof(*md));
405
0
}
406
407
int
408
EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
409
0
{
410
0
  md->block_size = blocksize;
411
0
  return 1;
412
0
}
413
414
int
415
EVP_MD_meth_set_result_size(EVP_MD *md, int result_size)
416
0
{
417
0
  md->md_size = result_size;
418
0
  return 1;
419
0
}
420
421
int
422
EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
423
0
{
424
0
  md->ctx_size = datasize;
425
0
  return 1;
426
0
}
427
428
int
429
EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
430
0
{
431
0
  md->flags = flags;
432
0
  return 1;
433
0
}
434
435
int
436
EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
437
0
{
438
0
  md->init = init;
439
0
  return 1;
440
0
}
441
442
int
443
EVP_MD_meth_set_update(EVP_MD *md,
444
    int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count))
445
0
{
446
0
  md->update = update;
447
0
  return 1;
448
0
}
449
450
int
451
EVP_MD_meth_set_final(EVP_MD *md,
452
    int (*final)(EVP_MD_CTX *ctx, unsigned char *md))
453
0
{
454
0
  md->final = final;
455
0
  return 1;
456
0
}
457
458
int
459
EVP_MD_meth_set_copy(EVP_MD *md,
460
    int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from))
461
0
{
462
0
  md->copy = copy;
463
0
  return 1;
464
0
}
465
466
int
467
EVP_MD_meth_set_cleanup(EVP_MD *md,
468
    int (*cleanup)(EVP_MD_CTX *ctx))
469
0
{
470
0
  md->cleanup = cleanup;
471
0
  return 1;
472
0
}
473
474
int
475
EVP_MD_meth_set_ctrl(EVP_MD *md,
476
    int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2))
477
0
{
478
0
  md->md_ctrl = ctrl;
479
0
  return 1;
480
0
}
481
482
const EVP_MD *
483
EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
484
15.9k
{
485
15.9k
  if (!ctx)
486
0
    return NULL;
487
15.9k
  return ctx->digest;
488
15.9k
}
489
490
void *
491
EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
492
0
{
493
0
  return ctx->md_data;
494
0
}
495
496
EVP_PKEY_CTX *
497
EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
498
0
{
499
0
  return ctx->pctx;
500
0
}
501
502
void
503
EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
504
0
{
505
0
  if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
506
0
    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
507
0
  } else {
508
0
    EVP_PKEY_CTX_free(ctx->pctx);
509
0
  }
510
511
0
  ctx->pctx = pctx;
512
513
0
  if (pctx != NULL) {
514
    /*
515
     * For unclear reasons it was decided that the caller keeps
516
     * ownership of pctx. So a flag was invented to make sure we
517
     * don't free it in EVP_MD_CTX_cleanup(). We also need to
518
     * unset it in EVP_MD_CTX_copy_ex(). Fortunately, the flag
519
     * isn't public...
520
     */
521
0
    EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
522
0
  }
523
0
}
524
525
void
526
EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
527
81.8k
{
528
81.8k
  ctx->flags |= flags;
529
81.8k
}
530
531
void
532
EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
533
139k
{
534
139k
  ctx->flags &= ~flags;
535
139k
}
536
537
int
538
EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
539
273k
{
540
273k
  return (ctx->flags & flags);
541
273k
}
542
543
void
544
EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
545
0
{
546
0
  ctx->flags |= flags;
547
0
}
548
549
void
550
EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
551
0
{
552
0
  ctx->flags &= ~flags;
553
0
}
554
555
int
556
EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
557
0
{
558
0
  return (ctx->flags & flags);
559
0
}