Coverage Report

Created: 2025-12-31 06:58

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