Coverage Report

Created: 2026-02-14 07:20

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