Coverage Report

Created: 2026-02-14 07:20

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