Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/evp/evp_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * EVP _meth_ APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include <string.h>
18
#include "internal/cryptlib.h"
19
#include <openssl/evp.h>
20
#include <openssl/x509.h>
21
#include <openssl/objects.h>
22
#include <openssl/params.h>
23
#include <openssl/core_names.h>
24
#include <openssl/rsa.h>
25
#include <openssl/dh.h>
26
#include <openssl/ec.h>
27
#include "crypto/evp.h"
28
#include "crypto/cryptlib.h"
29
#include "internal/provider.h"
30
#include "evp_local.h"
31
32
#if !defined(FIPS_MODULE)
33
#include "crypto/asn1.h"
34
35
int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
36
0
{
37
0
    return evp_cipher_param_to_asn1_ex(c, type, NULL);
38
0
}
39
40
int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
41
470
{
42
470
    return evp_cipher_asn1_to_param_ex(c, type, NULL);
43
470
}
44
45
int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
46
334
{
47
334
    int i = 0;
48
334
    unsigned int l;
49
50
334
    if (type != NULL) {
51
334
        unsigned char iv[EVP_MAX_IV_LENGTH];
52
53
334
        l = EVP_CIPHER_CTX_get_iv_length(ctx);
54
334
        if (!ossl_assert(l <= sizeof(iv)))
55
0
            return -1;
56
334
        i = ASN1_TYPE_get_octetstring(type, iv, l);
57
334
        if (i != (int)l)
58
40
            return -1;
59
60
294
        if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
61
0
            return -1;
62
294
    }
63
294
    return i;
64
334
}
65
66
int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
67
0
{
68
0
    int i = 0;
69
0
    unsigned int j;
70
0
    unsigned char *oiv = NULL;
71
72
0
    if (type != NULL) {
73
0
        oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
74
0
        j = EVP_CIPHER_CTX_get_iv_length(c);
75
0
        OPENSSL_assert(j <= sizeof(c->iv));
76
0
        i = ASN1_TYPE_set_octetstring(type, oiv, j);
77
0
    }
78
0
    return i;
79
0
}
80
81
int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
82
    evp_cipher_aead_asn1_params *asn1_params)
83
0
{
84
0
    int ret = -1; /* Assume the worst */
85
0
    const EVP_CIPHER *cipher;
86
87
0
    if (c == NULL || c->cipher == NULL)
88
0
        goto err;
89
90
0
    cipher = c->cipher;
91
    /*
92
     * For legacy implementations, we detect custom AlgorithmIdentifier
93
     * parameter handling by checking if the function pointer
94
     * cipher->set_asn1_parameters is set.  We know that this pointer
95
     * is NULL for provided implementations.
96
     *
97
     * Otherwise, for any implementation, we check the flag
98
     * EVP_CIPH_FLAG_CUSTOM_ASN1.  If it isn't set, we apply
99
     * default AI parameter extraction.
100
     *
101
     * Otherwise, for provided implementations, we convert |type| to
102
     * a DER encoded blob and pass to the implementation in OSSL_PARAM
103
     * form.
104
     *
105
     * If none of the above applies, this operation is unsupported.
106
     */
107
0
    if (cipher->set_asn1_parameters != NULL) {
108
0
        ret = cipher->set_asn1_parameters(c, type);
109
0
    } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
110
0
        switch (EVP_CIPHER_get_mode(cipher)) {
111
0
        case EVP_CIPH_WRAP_MODE:
112
0
            if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
113
0
                ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
114
0
            ret = 1;
115
0
            break;
116
117
0
        case EVP_CIPH_GCM_MODE:
118
0
            ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
119
0
            break;
120
121
0
        case EVP_CIPH_CCM_MODE:
122
0
        case EVP_CIPH_XTS_MODE:
123
0
        case EVP_CIPH_OCB_MODE:
124
0
            ret = -2;
125
0
            break;
126
127
0
        default:
128
0
            ret = EVP_CIPHER_set_asn1_iv(c, type);
129
0
        }
130
0
    } else if (cipher->prov != NULL) {
131
        /* We cheat, there's no need for an object ID for this use */
132
0
        X509_ALGOR alg;
133
134
0
        alg.algorithm = NULL;
135
0
        alg.parameter = type;
136
137
0
        ret = EVP_CIPHER_CTX_get_algor_params(c, &alg);
138
0
    } else {
139
0
        ret = -2;
140
0
    }
141
142
0
err:
143
0
    if (ret == -2)
144
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
145
0
    else if (ret <= 0)
146
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
147
0
    if (ret < -1)
148
0
        ret = -1;
149
0
    return ret;
150
0
}
151
152
int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
153
    evp_cipher_aead_asn1_params *asn1_params)
154
0
{
155
0
    int ret = -1; /* Assume the worst */
156
0
    const EVP_CIPHER *cipher;
157
158
0
    if (c == NULL || c->cipher == NULL)
159
0
        goto err;
160
161
0
    cipher = c->cipher;
162
    /*
163
     * For legacy implementations, we detect custom AlgorithmIdentifier
164
     * parameter handling by checking if there the function pointer
165
     * cipher->get_asn1_parameters is set.  We know that this pointer
166
     * is NULL for provided implementations.
167
     *
168
     * Otherwise, for any implementation, we check the flag
169
     * EVP_CIPH_FLAG_CUSTOM_ASN1.  If it isn't set, we apply
170
     * default AI parameter creation.
171
     *
172
     * Otherwise, for provided implementations, we get the AI parameter
173
     * in DER encoded form from the implementation by requesting the
174
     * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
175
     *
176
     * If none of the above applies, this operation is unsupported.
177
     */
178
0
    if (cipher->get_asn1_parameters != NULL) {
179
0
        ret = cipher->get_asn1_parameters(c, type);
180
0
    } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
181
0
        switch (EVP_CIPHER_get_mode(cipher)) {
182
0
        case EVP_CIPH_WRAP_MODE:
183
0
            ret = 1;
184
0
            break;
185
186
0
        case EVP_CIPH_GCM_MODE:
187
0
            ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
188
0
            break;
189
190
0
        case EVP_CIPH_CCM_MODE:
191
0
        case EVP_CIPH_XTS_MODE:
192
0
        case EVP_CIPH_OCB_MODE:
193
0
            ret = -2;
194
0
            break;
195
196
0
        default:
197
0
            ret = EVP_CIPHER_get_asn1_iv(c, type);
198
0
            if (ret == 0 && EVP_CIPHER_CTX_get_iv_length(c) == 0)
199
0
                ret = 1;
200
0
        }
201
0
    } else if (cipher->prov != NULL) {
202
        /* We cheat, there's no need for an object ID for this use */
203
0
        X509_ALGOR alg;
204
205
0
        alg.algorithm = NULL;
206
0
        alg.parameter = type;
207
208
0
        ret = EVP_CIPHER_CTX_set_algor_params(c, &alg);
209
0
    } else {
210
0
        ret = -2;
211
0
    }
212
213
0
err:
214
0
    if (ret == -2)
215
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
216
0
    else if (ret <= 0)
217
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
218
0
    if (ret < -1)
219
0
        ret = -1;
220
0
    return ret;
221
0
}
222
223
int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
224
    evp_cipher_aead_asn1_params *asn1_params)
225
24
{
226
24
    int i = 0;
227
24
    long tl;
228
24
    unsigned char iv[EVP_MAX_IV_LENGTH];
229
230
24
    if (type == NULL || asn1_params == NULL)
231
24
        return 0;
232
233
0
    i = ossl_asn1_type_get_octetstring_int(type, &tl, iv, EVP_MAX_IV_LENGTH);
234
0
    if (i <= 0 || i > EVP_MAX_IV_LENGTH)
235
0
        return -1;
236
237
0
    memcpy(asn1_params->iv, iv, i);
238
0
    asn1_params->iv_len = i;
239
240
0
    return i;
241
0
}
242
243
int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
244
    evp_cipher_aead_asn1_params *asn1_params)
245
0
{
246
0
    if (type == NULL || asn1_params == NULL)
247
0
        return 0;
248
249
0
    return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
250
0
        asn1_params->iv,
251
0
        asn1_params->iv_len);
252
0
}
253
#endif /* !defined(FIPS_MODULE) */
254
255
/* Convert the various cipher NIDs and dummies to a proper OID NID */
256
int EVP_CIPHER_get_type(const EVP_CIPHER *cipher)
257
18.6k
{
258
18.6k
    int nid;
259
18.6k
    nid = EVP_CIPHER_get_nid(cipher);
260
261
18.6k
    switch (nid) {
262
263
306
    case NID_rc2_cbc:
264
510
    case NID_rc2_64_cbc:
265
714
    case NID_rc2_40_cbc:
266
267
714
        return NID_rc2_cbc;
268
269
102
    case NID_rc4:
270
204
    case NID_rc4_40:
271
272
204
        return NID_rc4;
273
274
102
    case NID_aes_128_cfb128:
275
204
    case NID_aes_128_cfb8:
276
306
    case NID_aes_128_cfb1:
277
278
306
        return NID_aes_128_cfb128;
279
280
102
    case NID_aes_192_cfb128:
281
204
    case NID_aes_192_cfb8:
282
306
    case NID_aes_192_cfb1:
283
284
306
        return NID_aes_192_cfb128;
285
286
102
    case NID_aes_256_cfb128:
287
204
    case NID_aes_256_cfb8:
288
306
    case NID_aes_256_cfb1:
289
290
306
        return NID_aes_256_cfb128;
291
292
102
    case NID_des_cfb64:
293
204
    case NID_des_cfb8:
294
306
    case NID_des_cfb1:
295
296
306
        return NID_des_cfb64;
297
298
102
    case NID_des_ede3_cfb64:
299
204
    case NID_des_ede3_cfb8:
300
306
    case NID_des_ede3_cfb1:
301
302
306
        return NID_des_ede3_cfb64;
303
304
16.2k
    default:
305
#ifdef FIPS_MODULE
306
        return NID_undef;
307
#else
308
16.2k
    {
309
        /* Check it has an OID and it is valid */
310
16.2k
        ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
311
312
16.2k
        if (OBJ_get0_data(otmp) == NULL)
313
4.89k
            nid = NID_undef;
314
16.2k
        ASN1_OBJECT_free(otmp);
315
16.2k
        return nid;
316
204
    }
317
18.6k
#endif
318
18.6k
    }
319
18.6k
}
320
321
int evp_cipher_cache_constants(EVP_CIPHER *cipher)
322
3.38k
{
323
3.38k
    int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
324
3.38k
    size_t ivlen = 0;
325
3.38k
    size_t blksz = 0;
326
3.38k
    size_t keylen = 0;
327
3.38k
    unsigned int mode = 0;
328
3.38k
    OSSL_PARAM params[10];
329
330
3.38k
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
331
3.38k
    params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
332
3.38k
    params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
333
3.38k
    params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
334
3.38k
    params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
335
3.38k
    params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
336
3.38k
        &custom_iv);
337
3.38k
    params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
338
3.38k
    params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
339
3.38k
        &multiblock);
340
3.38k
    params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
341
3.38k
        &randkey);
342
3.38k
    params[9] = OSSL_PARAM_construct_end();
343
3.38k
    ok = evp_do_ciph_getparams(cipher, params) > 0;
344
3.38k
    if (ok) {
345
3.38k
        cipher->block_size = blksz;
346
3.38k
        cipher->iv_len = ivlen;
347
3.38k
        cipher->key_len = keylen;
348
3.38k
        cipher->flags = mode;
349
3.38k
        if (aead)
350
728
            cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
351
3.38k
        if (custom_iv)
352
1.06k
            cipher->flags |= EVP_CIPH_CUSTOM_IV;
353
3.38k
        if (cts)
354
156
            cipher->flags |= EVP_CIPH_FLAG_CTS;
355
3.38k
        if (multiblock)
356
104
            cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
357
3.38k
        if (cipher->ccipher != NULL)
358
3.06k
            cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
359
3.38k
        if (randkey)
360
286
            cipher->flags |= EVP_CIPH_RAND_KEY;
361
3.38k
        if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
362
3.38k
                OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
363
0
            cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
364
3.38k
    }
365
3.38k
    return ok;
366
3.38k
}
367
368
int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
369
5.79M
{
370
5.79M
    return (cipher == NULL) ? 0 : cipher->block_size;
371
5.79M
}
372
373
int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
374
3.62M
{
375
3.62M
    return (ctx == NULL) ? 0 : EVP_CIPHER_get_block_size(ctx->cipher);
376
3.62M
}
377
378
int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
379
0
{
380
0
    return e->ctx_size;
381
0
}
382
383
int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
384
    const unsigned char *in, unsigned int inl)
385
13.6k
{
386
13.6k
    if (ctx == NULL || ctx->cipher == NULL)
387
0
        return 0;
388
389
13.6k
    if (ctx->cipher->prov != NULL) {
390
        /*
391
         * If the provided implementation has a ccipher function, we use it,
392
         * and translate its return value like this: 0 => -1, 1 => outlen
393
         *
394
         * Otherwise, we call the cupdate function if in != NULL, or cfinal
395
         * if in == NULL.  Regardless of which, we return what we got.
396
         */
397
13.6k
        int ret = -1;
398
13.6k
        size_t outl = 0;
399
13.6k
        size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
400
401
13.6k
        if (blocksize == 0)
402
0
            return 0;
403
404
13.6k
        if (ctx->cipher->ccipher != NULL)
405
13.6k
            ret = ctx->cipher->ccipher(ctx->algctx, out, &outl,
406
13.6k
                      inl + (blocksize == 1 ? 0 : blocksize),
407
13.6k
                      in, (size_t)inl)
408
13.6k
                ? (int)outl
409
13.6k
                : -1;
410
0
        else if (in != NULL)
411
0
            ret = ctx->cipher->cupdate(ctx->algctx, out, &outl,
412
0
                inl + (blocksize == 1 ? 0 : blocksize),
413
0
                in, (size_t)inl);
414
0
        else
415
0
            ret = ctx->cipher->cfinal(ctx->algctx, out, &outl,
416
0
                blocksize == 1 ? 0 : blocksize);
417
418
13.6k
        return ret;
419
13.6k
    }
420
421
0
    return ctx->cipher->do_cipher(ctx, out, in, inl);
422
13.6k
}
423
424
#ifndef OPENSSL_NO_DEPRECATED_3_0
425
const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
426
0
{
427
0
    if (ctx == NULL)
428
0
        return NULL;
429
0
    return ctx->cipher;
430
0
}
431
#endif
432
433
const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx)
434
3.86M
{
435
3.86M
    if (ctx == NULL)
436
0
        return NULL;
437
3.86M
    return ctx->cipher;
438
3.86M
}
439
440
EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX *ctx)
441
0
{
442
0
    EVP_CIPHER *cipher;
443
444
0
    if (ctx == NULL || ctx->cipher == NULL)
445
0
        return NULL;
446
0
    cipher = (EVP_CIPHER *)ctx->cipher;
447
0
    if (!EVP_CIPHER_up_ref(cipher))
448
0
        return NULL;
449
0
    return cipher;
450
0
}
451
452
int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx)
453
0
{
454
0
    return ctx->encrypt;
455
0
}
456
457
unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *cipher)
458
3.49M
{
459
3.49M
    return cipher == NULL ? 0 : cipher->flags;
460
3.49M
}
461
462
void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
463
0
{
464
0
    return ctx->app_data;
465
0
}
466
467
void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
468
0
{
469
0
    ctx->app_data = data;
470
0
}
471
472
void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
473
0
{
474
0
    return ctx->cipher_data;
475
0
}
476
477
void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
478
0
{
479
0
    void *old_cipher_data;
480
481
0
    old_cipher_data = ctx->cipher_data;
482
0
    ctx->cipher_data = cipher_data;
483
484
0
    return old_cipher_data;
485
0
}
486
487
int EVP_CIPHER_get_iv_length(const EVP_CIPHER *cipher)
488
1.01M
{
489
1.01M
    return (cipher == NULL) ? 0 : cipher->iv_len;
490
1.01M
}
491
492
int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
493
4.33M
{
494
4.33M
    if (ctx->cipher == NULL)
495
0
        return 0;
496
497
4.33M
    if (ctx->iv_len < 0) {
498
718k
        int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher);
499
718k
        size_t v = len;
500
718k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
501
502
718k
        if (ctx->cipher->get_ctx_params != NULL) {
503
718k
            params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN,
504
718k
                &v);
505
718k
            rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
506
718k
            if (rv > 0) {
507
718k
                if (OSSL_PARAM_modified(params)
508
718k
                    && !OSSL_PARAM_get_int(params, &len))
509
0
                    return -1;
510
718k
            } else if (rv != EVP_CTRL_RET_UNSUPPORTED) {
511
0
                return -1;
512
0
            }
513
718k
        }
514
        /* Code below to be removed when legacy support is dropped. */
515
0
        else if ((EVP_CIPHER_get_flags(ctx->cipher)
516
0
                     & EVP_CIPH_CUSTOM_IV_LENGTH)
517
0
            != 0) {
518
0
            rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
519
0
                0, &len);
520
0
            if (rv <= 0)
521
0
                return -1;
522
0
        }
523
        /*-
524
         * Casting away the const is annoying but required here.  We need to
525
         * cache the result for performance reasons.
526
         */
527
718k
        ((EVP_CIPHER_CTX *)ctx)->iv_len = len;
528
718k
    }
529
4.33M
    return ctx->iv_len;
530
4.33M
}
531
532
int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
533
0
{
534
0
    int ret;
535
0
    size_t v = 0;
536
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
537
538
0
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
539
0
    ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
540
0
    return ret == 1 ? (int)v : 0;
541
0
}
542
543
#ifndef OPENSSL_NO_DEPRECATED_3_0
544
const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
545
0
{
546
0
    int ok;
547
0
    const unsigned char *v = ctx->oiv;
548
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
549
550
0
    params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
551
0
        (void **)&v, sizeof(ctx->oiv));
552
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
553
554
0
    return ok != 0 ? v : NULL;
555
0
}
556
557
/*
558
 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
559
 */
560
const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
561
0
{
562
0
    int ok;
563
0
    const unsigned char *v = ctx->iv;
564
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
565
566
0
    params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
567
0
        (void **)&v, sizeof(ctx->iv));
568
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
569
570
0
    return ok != 0 ? v : NULL;
571
0
}
572
573
unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
574
0
{
575
0
    int ok;
576
0
    unsigned char *v = ctx->iv;
577
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
578
579
0
    params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
580
0
        (void **)&v, sizeof(ctx->iv));
581
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
582
583
0
    return ok != 0 ? v : NULL;
584
0
}
585
#endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
586
587
int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
588
0
{
589
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
590
591
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
592
0
    return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
593
0
}
594
595
int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
596
0
{
597
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
598
599
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
600
0
    return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
601
0
}
602
603
unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
604
0
{
605
0
    return ctx->buf;
606
0
}
607
608
int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
609
0
{
610
0
    int ok;
611
0
    unsigned int v = (unsigned int)ctx->num;
612
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
613
614
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
615
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
616
617
0
    return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
618
0
}
619
620
int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
621
0
{
622
0
    int ok;
623
0
    unsigned int n = (unsigned int)num;
624
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
625
626
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
627
0
    ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
628
629
0
    if (ok != 0)
630
0
        ctx->num = (int)n;
631
0
    return ok != 0;
632
0
}
633
634
int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
635
447k
{
636
447k
    return cipher->key_len;
637
447k
}
638
639
int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
640
1.22M
{
641
1.22M
    if (ctx->cipher == NULL)
642
11
        return 0;
643
644
1.22M
    if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) {
645
953k
        int ok;
646
953k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
647
953k
        size_t len;
648
649
953k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
650
953k
        ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
651
953k
        if (ok <= 0)
652
0
            return EVP_CTRL_RET_UNSUPPORTED;
653
654
        /*-
655
         * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
656
         * less than INT_MAX but best to be safe.
657
         *
658
         * Casting away the const is annoying but required here.  We need to
659
         * cache the result for performance reasons.
660
         */
661
953k
        if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
662
0
            return -1;
663
953k
        ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
664
953k
    }
665
1.22M
    return ctx->key_len;
666
1.22M
}
667
668
int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
669
28.2k
{
670
28.2k
    return (cipher == NULL) ? NID_undef : cipher->nid;
671
28.2k
}
672
673
int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
674
0
{
675
0
    return EVP_CIPHER_get_nid(ctx->cipher);
676
0
}
677
678
int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
679
232k
{
680
232k
    if (cipher == NULL)
681
0
        return 0;
682
232k
    if (cipher->prov != NULL)
683
232k
        return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
684
0
    return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
685
232k
}
686
687
int evp_cipher_get_number(const EVP_CIPHER *cipher)
688
0
{
689
0
    return cipher->name_id;
690
0
}
691
692
const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
693
299
{
694
299
    if (cipher->type_name != NULL)
695
299
        return cipher->type_name;
696
0
#ifndef FIPS_MODULE
697
0
    return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
698
#else
699
    return NULL;
700
#endif
701
299
}
702
703
const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
704
0
{
705
0
    if (cipher->description != NULL)
706
0
        return cipher->description;
707
0
#ifndef FIPS_MODULE
708
0
    return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
709
#else
710
    return NULL;
711
#endif
712
0
}
713
714
int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
715
    void (*fn)(const char *name, void *data),
716
    void *data)
717
0
{
718
0
    if (cipher->prov != NULL)
719
0
        return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
720
721
0
    return 1;
722
0
}
723
724
const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
725
2.78M
{
726
2.78M
    return cipher->prov;
727
2.78M
}
728
729
int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
730
2.36M
{
731
2.36M
    return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
732
2.36M
}
733
734
int EVP_MD_is_a(const EVP_MD *md, const char *name)
735
892k
{
736
892k
    if (md == NULL)
737
0
        return 0;
738
892k
    if (md->prov != NULL)
739
644k
        return evp_is_a(md->prov, md->name_id, NULL, name);
740
248k
    return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
741
892k
}
742
743
int evp_md_get_number(const EVP_MD *md)
744
0
{
745
0
    return md->name_id;
746
0
}
747
748
const char *EVP_MD_get0_description(const EVP_MD *md)
749
0
{
750
0
    if (md->description != NULL)
751
0
        return md->description;
752
0
#ifndef FIPS_MODULE
753
0
    return OBJ_nid2ln(EVP_MD_nid(md));
754
#else
755
    return NULL;
756
#endif
757
0
}
758
759
const char *EVP_MD_get0_name(const EVP_MD *md)
760
1.78M
{
761
1.78M
    if (md == NULL)
762
0
        return NULL;
763
1.78M
    if (md->type_name != NULL)
764
1.27M
        return md->type_name;
765
508k
#ifndef FIPS_MODULE
766
508k
    return OBJ_nid2sn(EVP_MD_nid(md));
767
#else
768
    return NULL;
769
#endif
770
1.78M
}
771
772
int EVP_MD_names_do_all(const EVP_MD *md,
773
    void (*fn)(const char *name, void *data),
774
    void *data)
775
0
{
776
0
    if (md->prov != NULL)
777
0
        return evp_names_do_all(md->prov, md->name_id, fn, data);
778
779
0
    return 1;
780
0
}
781
782
const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
783
1.73M
{
784
1.73M
    return md->prov;
785
1.73M
}
786
787
int EVP_MD_get_type(const EVP_MD *md)
788
4.00M
{
789
4.00M
    return md->type;
790
4.00M
}
791
792
int EVP_MD_get_pkey_type(const EVP_MD *md)
793
587
{
794
587
    return md->pkey_type;
795
587
}
796
797
int EVP_MD_get_block_size(const EVP_MD *md)
798
3.73M
{
799
3.73M
    if (md == NULL) {
800
0
        ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
801
0
        return -1;
802
0
    }
803
3.73M
    return md->block_size;
804
3.73M
}
805
806
int EVP_MD_get_size(const EVP_MD *md)
807
391M
{
808
391M
    if (md == NULL) {
809
7
        ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
810
7
        return -1;
811
7
    }
812
391M
    return md->md_size;
813
391M
}
814
815
int EVP_MD_xof(const EVP_MD *md)
816
2.84M
{
817
2.84M
    return md != NULL && ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0);
818
2.84M
}
819
820
unsigned long EVP_MD_get_flags(const EVP_MD *md)
821
2.84M
{
822
2.84M
    return md->flags;
823
2.84M
}
824
825
EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
826
0
{
827
0
    EVP_MD *md = evp_md_new();
828
829
0
    if (md != NULL) {
830
0
        md->type = md_type;
831
0
        md->pkey_type = pkey_type;
832
0
        md->origin = EVP_ORIG_METH;
833
0
    }
834
0
    return md;
835
0
}
836
837
EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
838
0
{
839
0
    EVP_MD *to = NULL;
840
841
    /*
842
     * Non-legacy EVP_MDs can't be duplicated like this.
843
     * Use EVP_MD_up_ref() instead.
844
     */
845
0
    if (md->prov != NULL)
846
0
        return NULL;
847
848
0
    if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
849
0
        CRYPTO_REF_COUNT refcnt = to->refcnt;
850
851
0
        memcpy(to, md, sizeof(*to));
852
0
        to->refcnt = refcnt;
853
0
        to->origin = EVP_ORIG_METH;
854
0
    }
855
0
    return to;
856
0
}
857
858
void evp_md_free_int(EVP_MD *md)
859
1.26k
{
860
1.26k
    OPENSSL_free(md->type_name);
861
1.26k
    ossl_provider_free(md->prov);
862
1.26k
    CRYPTO_FREE_REF(&md->refcnt);
863
1.26k
    OPENSSL_free(md);
864
1.26k
}
865
866
void EVP_MD_meth_free(EVP_MD *md)
867
0
{
868
0
    if (md == NULL || md->origin != EVP_ORIG_METH)
869
0
        return;
870
871
0
    evp_md_free_int(md);
872
0
}
873
874
int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
875
0
{
876
0
    if (md->block_size != 0)
877
0
        return 0;
878
879
0
    md->block_size = blocksize;
880
0
    return 1;
881
0
}
882
int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
883
0
{
884
0
    if (md->md_size != 0)
885
0
        return 0;
886
887
0
    md->md_size = resultsize;
888
0
    return 1;
889
0
}
890
int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
891
0
{
892
0
    if (md->ctx_size != 0)
893
0
        return 0;
894
895
0
    md->ctx_size = datasize;
896
0
    return 1;
897
0
}
898
int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
899
0
{
900
0
    if (md->flags != 0)
901
0
        return 0;
902
903
0
    md->flags = flags;
904
0
    return 1;
905
0
}
906
int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
907
0
{
908
0
    if (md->init != NULL)
909
0
        return 0;
910
911
0
    md->init = init;
912
0
    return 1;
913
0
}
914
int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count))
915
0
{
916
0
    if (md->update != NULL)
917
0
        return 0;
918
919
0
    md->update = update;
920
0
    return 1;
921
0
}
922
int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx, unsigned char *md))
923
0
{
924
0
    if (md->final != NULL)
925
0
        return 0;
926
927
0
    md->final = final;
928
0
    return 1;
929
0
}
930
int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from))
931
0
{
932
0
    if (md->copy != NULL)
933
0
        return 0;
934
935
0
    md->copy = copy;
936
0
    return 1;
937
0
}
938
int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
939
0
{
940
0
    if (md->cleanup != NULL)
941
0
        return 0;
942
943
0
    md->cleanup = cleanup;
944
0
    return 1;
945
0
}
946
int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2))
947
0
{
948
0
    if (md->md_ctrl != NULL)
949
0
        return 0;
950
951
0
    md->md_ctrl = ctrl;
952
0
    return 1;
953
0
}
954
955
int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
956
0
{
957
0
    return md->block_size;
958
0
}
959
int EVP_MD_meth_get_result_size(const EVP_MD *md)
960
0
{
961
0
    return md->md_size;
962
0
}
963
int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
964
0
{
965
0
    return md->ctx_size;
966
0
}
967
unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
968
0
{
969
0
    return md->flags;
970
0
}
971
int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
972
0
{
973
0
    return md->init;
974
0
}
975
int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
976
    const void *data,
977
    size_t count)
978
0
{
979
0
    return md->update;
980
0
}
981
int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
982
    unsigned char *md)
983
0
{
984
0
    return md->final;
985
0
}
986
int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
987
    const EVP_MD_CTX *from)
988
0
{
989
0
    return md->copy;
990
0
}
991
int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
992
0
{
993
0
    return md->cleanup;
994
0
}
995
int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
996
    int p1, void *p2)
997
0
{
998
0
    return md->md_ctrl;
999
0
}
1000
1001
#ifndef OPENSSL_NO_DEPRECATED_3_0
1002
const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
1003
0
{
1004
0
    if (ctx == NULL)
1005
0
        return NULL;
1006
0
    return ctx->reqdigest;
1007
0
}
1008
#endif
1009
1010
const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
1011
385M
{
1012
385M
    if (ctx == NULL)
1013
924
        return NULL;
1014
385M
    return ctx->reqdigest;
1015
385M
}
1016
1017
EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
1018
0
{
1019
0
    EVP_MD *md;
1020
1021
0
    if (ctx == NULL)
1022
0
        return NULL;
1023
0
    md = (EVP_MD *)ctx->reqdigest;
1024
0
    if (md == NULL || !EVP_MD_up_ref(md))
1025
0
        return NULL;
1026
0
    return md;
1027
0
}
1028
1029
int EVP_MD_CTX_get_size_ex(const EVP_MD_CTX *ctx)
1030
230M
{
1031
230M
    EVP_MD_CTX *c = (EVP_MD_CTX *)ctx;
1032
230M
    const OSSL_PARAM *gettables;
1033
1034
230M
    gettables = EVP_MD_CTX_gettable_params(c);
1035
230M
    if (gettables != NULL
1036
95.1k
        && OSSL_PARAM_locate_const(gettables,
1037
95.1k
               OSSL_DIGEST_PARAM_SIZE)
1038
95.1k
            != NULL) {
1039
95.1k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1040
95.1k
        size_t sz = 0;
1041
1042
        /*
1043
         * For XOF's EVP_MD_get_size() returns 0
1044
         * So try to get the xoflen instead. This will return -1 if the
1045
         * xof length has not been set.
1046
         */
1047
95.1k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &sz);
1048
95.1k
        if (EVP_MD_CTX_get_params(c, params) != 1
1049
95.1k
            || sz == SIZE_MAX
1050
95.1k
            || sz == 0)
1051
9
            return -1;
1052
95.1k
        return sz;
1053
95.1k
    }
1054
    /* Normal digests have a constant fixed size output */
1055
230M
    return EVP_MD_get_size(EVP_MD_CTX_get0_md(ctx));
1056
230M
}
1057
1058
EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
1059
203k
{
1060
203k
    return ctx->pctx;
1061
203k
}
1062
1063
#if !defined(FIPS_MODULE)
1064
void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
1065
9.86k
{
1066
    /*
1067
     * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
1068
     * we have to deal with the cleanup job here.
1069
     */
1070
9.86k
    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
1071
9.86k
        EVP_PKEY_CTX_free(ctx->pctx);
1072
1073
9.86k
    ctx->pctx = pctx;
1074
1075
9.86k
    if (pctx != NULL) {
1076
        /* make sure pctx is not freed when destroying EVP_MD_CTX */
1077
9.86k
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1078
9.86k
    } else {
1079
0
        EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1080
0
    }
1081
9.86k
}
1082
#endif /* !defined(FIPS_MODULE) */
1083
1084
void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
1085
0
{
1086
0
    return ctx->md_data;
1087
0
}
1088
1089
int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
1090
    const void *data, size_t count)
1091
0
{
1092
0
    return ctx->update;
1093
0
}
1094
1095
void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
1096
    int (*update)(EVP_MD_CTX *ctx,
1097
        const void *data, size_t count))
1098
0
{
1099
0
    ctx->update = update;
1100
0
}
1101
1102
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
1103
10.9M
{
1104
10.9M
    ctx->flags |= flags;
1105
10.9M
}
1106
1107
void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
1108
682M
{
1109
682M
    ctx->flags &= ~flags;
1110
682M
}
1111
1112
int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
1113
29.1M
{
1114
29.1M
    return (ctx->flags & flags);
1115
29.1M
}
1116
1117
static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
1118
    unsigned int enable)
1119
0
{
1120
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1121
1122
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
1123
0
    return EVP_CIPHER_CTX_set_params(ctx, params);
1124
0
}
1125
1126
void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
1127
15.1k
{
1128
15.1k
    int oldflags = ctx->flags;
1129
1130
15.1k
    ctx->flags |= flags;
1131
15.1k
    if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1132
0
        evp_cipher_ctx_enable_use_bits(ctx, 1);
1133
15.1k
}
1134
1135
void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
1136
0
{
1137
0
    int oldflags = ctx->flags;
1138
1139
0
    ctx->flags &= ~flags;
1140
0
    if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1141
0
        evp_cipher_ctx_enable_use_bits(ctx, 0);
1142
0
}
1143
1144
int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
1145
0
{
1146
0
    return (ctx->flags & flags);
1147
0
}
1148
1149
#if !defined(FIPS_MODULE)
1150
1151
int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
1152
160k
{
1153
160k
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1154
1155
160k
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1156
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1157
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1158
0
        return -2;
1159
0
    }
1160
1161
160k
    if (name == NULL)
1162
0
        return -1;
1163
1164
160k
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1165
160k
        (char *)name, 0);
1166
160k
    return EVP_PKEY_CTX_set_params(ctx, params);
1167
160k
}
1168
1169
int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1170
0
{
1171
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1172
0
    OSSL_PARAM *p = params;
1173
1174
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1175
        /* There is no legacy support for this */
1176
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1177
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1178
0
        return -2;
1179
0
    }
1180
1181
0
    if (name == NULL)
1182
0
        return -1;
1183
1184
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1185
0
        name, namelen);
1186
0
    if (!EVP_PKEY_CTX_get_params(ctx, params))
1187
0
        return -1;
1188
0
    return 1;
1189
0
}
1190
#endif /* !FIPS_MODULE */
1191
1192
/*
1193
 * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
1194
 * while providing a generic way of generating a new asymmetric key pair
1195
 * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
1196
 * The library context I<libctx> and property query I<propq>
1197
 * are used when fetching algorithms from providers.
1198
 * The I<params> specify algorithm-specific parameters
1199
 * such as the RSA modulus size or the name of an EC curve.
1200
 */
1201
static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
1202
    const char *propq, const OSSL_PARAM *params)
1203
125k
{
1204
125k
    EVP_PKEY *pkey = NULL;
1205
125k
    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
1206
1207
125k
    if (ctx != NULL
1208
125k
        && EVP_PKEY_keygen_init(ctx) > 0
1209
125k
        && EVP_PKEY_CTX_set_params(ctx, params))
1210
125k
        (void)EVP_PKEY_generate(ctx, &pkey);
1211
1212
125k
    EVP_PKEY_CTX_free(ctx);
1213
125k
    return pkey;
1214
125k
}
1215
1216
EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
1217
    const char *type, ...)
1218
85.8k
{
1219
85.8k
    va_list args;
1220
85.8k
    size_t bits;
1221
85.8k
    char *name;
1222
85.8k
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1223
85.8k
    EVP_PKEY *ret = NULL;
1224
1225
85.8k
    va_start(args, type);
1226
1227
85.8k
    if (OPENSSL_strcasecmp(type, "RSA") == 0) {
1228
0
        bits = va_arg(args, size_t);
1229
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
1230
85.8k
    } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
1231
0
        name = va_arg(args, char *);
1232
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1233
0
            name, 0);
1234
0
    }
1235
1236
85.8k
    ret = evp_pkey_keygen(libctx, type, propq, params);
1237
1238
85.8k
    va_end(args);
1239
85.8k
    return ret;
1240
85.8k
}
1241
1242
#if !defined(FIPS_MODULE)
1243
int EVP_CIPHER_CTX_set_algor_params(EVP_CIPHER_CTX *ctx, const X509_ALGOR *alg)
1244
0
{
1245
0
    int ret = -1; /* Assume the worst */
1246
0
    unsigned char *der = NULL;
1247
0
    int derl = -1;
1248
1249
0
    if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
1250
0
        const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
1251
0
        const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
1252
0
        OSSL_PARAM params[3];
1253
1254
        /*
1255
         * Passing the same data with both the old (deprecated) and the
1256
         * new AlgID parameters OSSL_PARAM key.
1257
         */
1258
0
        params[0] = OSSL_PARAM_construct_octet_string(k_old, der, (size_t)derl);
1259
0
        params[1] = OSSL_PARAM_construct_octet_string(k_new, der, (size_t)derl);
1260
0
        params[2] = OSSL_PARAM_construct_end();
1261
0
        ret = EVP_CIPHER_CTX_set_params(ctx, params);
1262
0
    }
1263
0
    OPENSSL_free(der);
1264
0
    return ret;
1265
0
}
1266
1267
int EVP_CIPHER_CTX_get_algor_params(EVP_CIPHER_CTX *ctx, X509_ALGOR *alg)
1268
0
{
1269
0
    int ret = -1; /* Assume the worst */
1270
0
    unsigned char *der = NULL;
1271
0
    size_t derl;
1272
0
    ASN1_TYPE *type = NULL;
1273
0
    int i = -1;
1274
0
    const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
1275
0
    const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
1276
0
    const char *derk;
1277
0
    OSSL_PARAM params[3];
1278
1279
    /*
1280
     * We make two passes, the first to get the appropriate buffer size,
1281
     * and the second to get the actual value.
1282
     * Also, using both the old (deprecated) and the new AlgID parameters
1283
     * OSSL_PARAM key, and using whichever the provider responds to.
1284
     * Should the provider respond on both, the new key takes priority.
1285
     */
1286
0
    params[0] = OSSL_PARAM_construct_octet_string(k_old, NULL, 0);
1287
0
    params[1] = OSSL_PARAM_construct_octet_string(k_new, NULL, 0);
1288
0
    params[2] = OSSL_PARAM_construct_end();
1289
1290
0
    if (!EVP_CIPHER_CTX_get_params(ctx, params))
1291
0
        goto err;
1292
1293
    /* ... but, we should get a return size too! */
1294
0
    if (OSSL_PARAM_modified(&params[0]) && params[0].return_size != 0)
1295
0
        i = 0;
1296
0
    if (OSSL_PARAM_modified(&params[1]) && params[1].return_size != 0)
1297
0
        i = 1;
1298
0
    if (i < 0)
1299
0
        goto err;
1300
1301
    /*
1302
     * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
1303
     * below.  If it is NULL, the d2i_ASN1_TYPE() call will allocate new
1304
     * space for it.  Either way, alg->parameter can be safely assigned
1305
     * with type after the d2i_ASN1_TYPE() call, with the safety that it
1306
     * will be ok.
1307
     */
1308
0
    type = alg->parameter;
1309
1310
0
    derk = params[i].key;
1311
0
    derl = params[i].return_size;
1312
0
    if ((der = OPENSSL_malloc(derl)) != NULL) {
1313
0
        unsigned char *derp = der;
1314
1315
0
        params[i] = OSSL_PARAM_construct_octet_string(derk, der, derl);
1316
0
        if (EVP_CIPHER_CTX_get_params(ctx, params)
1317
0
            && OSSL_PARAM_modified(&params[i])
1318
0
            && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
1319
0
                   (int)derl)
1320
0
                != NULL) {
1321
            /*
1322
             * Don't free alg->parameter, see comment further up.
1323
             * Worst case, alg->parameter gets assigned its own value.
1324
             */
1325
0
            alg->parameter = type;
1326
0
            ret = 1;
1327
0
        }
1328
0
    }
1329
0
err:
1330
0
    OPENSSL_free(der);
1331
0
    return ret;
1332
0
}
1333
1334
int EVP_CIPHER_CTX_get_algor(EVP_CIPHER_CTX *ctx, X509_ALGOR **alg)
1335
0
{
1336
0
    int ret = -1; /* Assume the worst */
1337
0
    OSSL_PARAM params[2];
1338
0
    size_t aid_len = 0;
1339
0
    const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
1340
1341
0
    params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
1342
0
    params[1] = OSSL_PARAM_construct_end();
1343
1344
0
    if (EVP_CIPHER_CTX_get_params(ctx, params) <= 0)
1345
0
        goto err;
1346
1347
0
    if (OSSL_PARAM_modified(&params[0]))
1348
0
        aid_len = params[0].return_size;
1349
0
    if (aid_len == 0) {
1350
0
        ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
1351
0
        ret = -2;
1352
0
        goto err;
1353
0
    }
1354
0
    if (alg != NULL) {
1355
0
        unsigned char *aid = NULL;
1356
0
        const unsigned char *pp = NULL;
1357
1358
0
        if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
1359
0
            params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
1360
0
            pp = aid;
1361
0
            if (EVP_CIPHER_CTX_get_params(ctx, params)
1362
0
                && OSSL_PARAM_modified(&params[0])
1363
0
                && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
1364
0
                ret = 1;
1365
0
        }
1366
0
        OPENSSL_free(aid);
1367
0
    }
1368
0
err:
1369
0
    return ret;
1370
0
}
1371
1372
int EVP_PKEY_CTX_set_algor_params(EVP_PKEY_CTX *ctx, const X509_ALGOR *alg)
1373
0
{
1374
0
    int ret = -1; /* Assume the worst */
1375
0
    unsigned char *der = NULL;
1376
0
    int derl = -1;
1377
1378
0
    if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
1379
0
        const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
1380
0
        OSSL_PARAM params[2];
1381
1382
        /*
1383
         * Passing the same data with both the old (deprecated) and the
1384
         * new AlgID parameters OSSL_PARAM key.
1385
         */
1386
0
        params[0] = OSSL_PARAM_construct_octet_string(k, der, (size_t)derl);
1387
0
        params[1] = OSSL_PARAM_construct_end();
1388
0
        ret = EVP_PKEY_CTX_set_params(ctx, params);
1389
0
    }
1390
0
    OPENSSL_free(der);
1391
0
    return ret;
1392
0
}
1393
1394
int EVP_PKEY_CTX_get_algor_params(EVP_PKEY_CTX *ctx, X509_ALGOR *alg)
1395
0
{
1396
0
    int ret = -1; /* Assume the worst */
1397
0
    OSSL_PARAM params[2];
1398
0
    unsigned char *der = NULL;
1399
0
    size_t derl;
1400
0
    ASN1_TYPE *type = NULL;
1401
0
    const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
1402
1403
    /*
1404
     * We make two passes, the first to get the appropriate buffer size,
1405
     * and the second to get the actual value.
1406
     * Also, using both the old (deprecated) and the new AlgID parameters
1407
     * OSSL_PARAM key, and using whichever the provider responds to.
1408
     * Should the provider respond on both, the new key takes priority.
1409
     */
1410
0
    params[0] = OSSL_PARAM_construct_octet_string(k, NULL, 0);
1411
0
    params[1] = OSSL_PARAM_construct_end();
1412
1413
0
    if (!EVP_PKEY_CTX_get_params(ctx, params))
1414
0
        goto err;
1415
1416
    /*
1417
     * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
1418
     * below.  If it is NULL, the d2i_ASN1_TYPE() call will allocate new
1419
     * space for it.  Either way, alg->parameter can be safely assigned
1420
     * with type after the d2i_ASN1_TYPE() call, with the safety that it
1421
     * will be ok.
1422
     */
1423
0
    type = alg->parameter;
1424
1425
0
    derl = params[0].return_size;
1426
0
    if (OSSL_PARAM_modified(&params[0])
1427
        /* ... but, we should get a return size too! */
1428
0
        && derl != 0
1429
0
        && (der = OPENSSL_malloc(derl)) != NULL) {
1430
0
        unsigned char *derp = der;
1431
1432
0
        params[0] = OSSL_PARAM_construct_octet_string(k, der, derl);
1433
0
        if (EVP_PKEY_CTX_get_params(ctx, params)
1434
0
            && OSSL_PARAM_modified(&params[0])
1435
0
            && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
1436
0
                   derl)
1437
0
                != NULL) {
1438
            /*
1439
             * Don't free alg->parameter, see comment further up.
1440
             * Worst case, alg->parameter gets assigned its own value.
1441
             */
1442
0
            alg->parameter = type;
1443
0
            ret = 1;
1444
0
        }
1445
0
    }
1446
0
err:
1447
0
    OPENSSL_free(der);
1448
0
    return ret;
1449
0
}
1450
1451
int EVP_PKEY_CTX_get_algor(EVP_PKEY_CTX *ctx, X509_ALGOR **alg)
1452
0
{
1453
0
    int ret = -1; /* Assume the worst */
1454
0
    OSSL_PARAM params[2];
1455
0
    size_t aid_len = 0;
1456
0
    const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
1457
1458
0
    params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
1459
0
    params[1] = OSSL_PARAM_construct_end();
1460
1461
0
    if (EVP_PKEY_CTX_get_params(ctx, params) <= 0)
1462
0
        goto err;
1463
1464
0
    if (OSSL_PARAM_modified(&params[0]))
1465
0
        aid_len = params[0].return_size;
1466
0
    if (aid_len == 0) {
1467
0
        ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
1468
0
        ret = -2;
1469
0
        goto err;
1470
0
    }
1471
0
    if (alg != NULL) {
1472
0
        unsigned char *aid = NULL;
1473
0
        const unsigned char *pp = NULL;
1474
1475
0
        if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
1476
0
            params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
1477
0
            pp = aid;
1478
0
            if (EVP_PKEY_CTX_get_params(ctx, params)
1479
0
                && OSSL_PARAM_modified(&params[0])
1480
0
                && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
1481
0
                ret = 1;
1482
0
        }
1483
0
        OPENSSL_free(aid);
1484
0
    }
1485
0
err:
1486
0
    return ret;
1487
0
}
1488
1489
#endif /* !defined(FIPS_MODULE) */