Coverage Report

Created: 2025-12-31 06:58

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