Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/evp/evp_lib.c
Line
Count
Source (jump to first uncovered line)
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) != NULL) {
153
0
                ret = 1;
154
0
            }
155
0
            OPENSSL_free(der);
156
0
        }
157
0
    } else {
158
0
        ret = -2;
159
0
    }
160
161
0
 err:
162
0
    if (ret == -2)
163
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
164
0
    else if (ret <= 0)
165
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
166
0
    if (ret < -1)
167
0
        ret = -1;
168
0
    return ret;
169
0
}
170
171
int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
172
                                evp_cipher_aead_asn1_params *asn1_params)
173
0
{
174
0
    int ret = -1;                /* Assume the worst */
175
0
    const EVP_CIPHER *cipher = c->cipher;
176
177
    /*
178
     * For legacy implementations, we detect custom AlgorithmIdentifier
179
     * parameter handling by checking if there the function pointer
180
     * cipher->get_asn1_parameters is set.  We know that this pointer
181
     * is NULL for provided implementations.
182
     *
183
     * Otherwise, for any implementation, we check the flag
184
     * EVP_CIPH_FLAG_CUSTOM_ASN1.  If it isn't set, we apply
185
     * default AI parameter creation.
186
     *
187
     * Otherwise, for provided implementations, we get the AI parameter
188
     * in DER encoded form from the implementation by requesting the
189
     * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
190
     *
191
     * If none of the above applies, this operation is unsupported.
192
     */
193
0
    if (cipher->get_asn1_parameters != NULL) {
194
0
        ret = cipher->get_asn1_parameters(c, type);
195
0
    } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
196
0
        switch (EVP_CIPHER_get_mode(cipher)) {
197
0
        case EVP_CIPH_WRAP_MODE:
198
0
            ret = 1;
199
0
            break;
200
201
0
        case EVP_CIPH_GCM_MODE:
202
0
            ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
203
0
            break;
204
205
0
        case EVP_CIPH_CCM_MODE:
206
0
        case EVP_CIPH_XTS_MODE:
207
0
        case EVP_CIPH_OCB_MODE:
208
0
            ret = -2;
209
0
            break;
210
211
0
        default:
212
0
            ret = EVP_CIPHER_get_asn1_iv(c, type) >= 0 ? 1 : -1;
213
0
        }
214
0
    } else if (cipher->prov != NULL) {
215
0
        OSSL_PARAM params[3], *p = params;
216
0
        unsigned char *der = NULL;
217
0
        int derl = -1;
218
219
0
        if ((derl = i2d_ASN1_TYPE(type, &der)) >= 0) {
220
0
            *p++ =
221
0
                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
8.19k
{
278
8.19k
    int nid;
279
8.19k
    nid = EVP_CIPHER_get_nid(cipher);
280
281
8.19k
    switch (nid) {
282
283
135
    case NID_rc2_cbc:
284
225
    case NID_rc2_64_cbc:
285
315
    case NID_rc2_40_cbc:
286
287
315
        return NID_rc2_cbc;
288
289
45
    case NID_rc4:
290
90
    case NID_rc4_40:
291
292
90
        return NID_rc4;
293
294
45
    case NID_aes_128_cfb128:
295
90
    case NID_aes_128_cfb8:
296
135
    case NID_aes_128_cfb1:
297
298
135
        return NID_aes_128_cfb128;
299
300
45
    case NID_aes_192_cfb128:
301
90
    case NID_aes_192_cfb8:
302
135
    case NID_aes_192_cfb1:
303
304
135
        return NID_aes_192_cfb128;
305
306
45
    case NID_aes_256_cfb128:
307
90
    case NID_aes_256_cfb8:
308
135
    case NID_aes_256_cfb1:
309
310
135
        return NID_aes_256_cfb128;
311
312
45
    case NID_des_cfb64:
313
90
    case NID_des_cfb8:
314
135
    case NID_des_cfb1:
315
316
135
        return NID_des_cfb64;
317
318
45
    case NID_des_ede3_cfb64:
319
90
    case NID_des_ede3_cfb8:
320
135
    case NID_des_ede3_cfb1:
321
322
135
        return NID_des_cfb64;
323
324
7.11k
    default:
325
#ifdef FIPS_MODULE
326
        return NID_undef;
327
#else
328
7.11k
        {
329
            /* Check it has an OID and it is valid */
330
7.11k
            ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
331
332
7.11k
            if (OBJ_get0_data(otmp) == NULL)
333
2.16k
                nid = NID_undef;
334
7.11k
            ASN1_OBJECT_free(otmp);
335
7.11k
            return nid;
336
90
        }
337
8.19k
#endif
338
8.19k
    }
339
8.19k
}
340
341
int evp_cipher_cache_constants(EVP_CIPHER *cipher)
342
1.27k
{
343
1.27k
    int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
344
1.27k
    size_t ivlen = 0;
345
1.27k
    size_t blksz = 0;
346
1.27k
    size_t keylen = 0;
347
1.27k
    unsigned int mode = 0;
348
1.27k
    OSSL_PARAM params[10];
349
350
1.27k
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
351
1.27k
    params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
352
1.27k
    params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
353
1.27k
    params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
354
1.27k
    params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
355
1.27k
    params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
356
1.27k
                                         &custom_iv);
357
1.27k
    params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
358
1.27k
    params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
359
1.27k
                                         &multiblock);
360
1.27k
    params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
361
1.27k
                                         &randkey);
362
1.27k
    params[9] = OSSL_PARAM_construct_end();
363
1.27k
    ok = evp_do_ciph_getparams(cipher, params) > 0;
364
1.27k
    if (ok) {
365
1.27k
        cipher->block_size = blksz;
366
1.27k
        cipher->iv_len = ivlen;
367
1.27k
        cipher->key_len = keylen;
368
1.27k
        cipher->flags = mode;
369
1.27k
        if (aead)
370
256
            cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
371
1.27k
        if (custom_iv)
372
380
            cipher->flags |= EVP_CIPH_CUSTOM_IV;
373
1.27k
        if (cts)
374
60
            cipher->flags |= EVP_CIPH_FLAG_CTS;
375
1.27k
        if (multiblock)
376
40
            cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
377
1.27k
        if (cipher->ccipher != NULL)
378
1.15k
            cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
379
1.27k
        if (randkey)
380
110
            cipher->flags |= EVP_CIPH_RAND_KEY;
381
1.27k
        if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
382
1.27k
                                    OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
383
0
            cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
384
1.27k
    }
385
1.27k
    return ok;
386
1.27k
}
387
388
int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
389
1.69M
{
390
1.69M
    return cipher->block_size;
391
1.69M
}
392
393
int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
394
1.63M
{
395
1.63M
    return EVP_CIPHER_get_block_size(ctx->cipher);
396
1.63M
}
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 : -1;
423
0
        else if (in != NULL)
424
0
            ret = ctx->cipher->cupdate(ctx->algctx, out, &outl,
425
0
                                       inl + (blocksize == 1 ? 0 : blocksize),
426
0
                                       in, (size_t)inl);
427
0
        else
428
0
            ret = ctx->cipher->cfinal(ctx->algctx, out, &outl,
429
0
                                      blocksize == 1 ? 0 : blocksize);
430
431
0
        return ret;
432
0
    }
433
434
0
    return ctx->cipher->do_cipher(ctx, out, in, inl);
435
0
}
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
962k
{
448
962k
    if (ctx == NULL)
449
0
        return NULL;
450
962k
    return ctx->cipher;
451
962k
}
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
314k
{
472
314k
    return cipher->flags;
473
314k
}
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
224k
{
502
224k
    return cipher->iv_len;
503
224k
}
504
505
int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
506
1.76M
{
507
1.76M
    if (ctx->iv_len < 0) {
508
135k
        int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher);
509
135k
        size_t v = len;
510
135k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
511
512
135k
        if (ctx->cipher->get_ctx_params != NULL) {
513
135k
            params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN,
514
135k
                                                    &v);
515
135k
            rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
516
135k
            if (rv > 0) {
517
135k
                if (OSSL_PARAM_modified(params)
518
135k
                        && !OSSL_PARAM_get_int(params, &len))
519
0
                    return -1;
520
135k
            } else if (rv != EVP_CTRL_RET_UNSUPPORTED) {
521
0
                return -1;
522
0
            }
523
135k
        }
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) != 0) {
527
0
            rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
528
0
                                     0, &len);
529
0
            if (rv <= 0)
530
0
                return -1;
531
0
        }
532
        /*-
533
         * Casting away the const is annoying but required here.  We need to
534
         * cache the result for performance reasons.
535
         */
536
135k
        ((EVP_CIPHER_CTX *)ctx)->iv_len = len;
537
135k
    }
538
1.76M
    return ctx->iv_len;
539
1.76M
}
540
541
int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
542
0
{
543
0
    int ret;
544
0
    size_t v = 0;
545
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
546
547
0
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
548
0
    ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
549
0
    return ret == 1 ? (int)v : 0;
550
0
}
551
552
#ifndef OPENSSL_NO_DEPRECATED_3_0
553
const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
554
0
{
555
0
    int ok;
556
0
    const unsigned char *v = ctx->oiv;
557
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
558
559
0
    params[0] =
560
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] =
577
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] =
591
0
        OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
592
0
                                       (void **)&v, sizeof(ctx->iv));
593
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
594
595
0
    return ok != 0 ? v : NULL;
596
0
}
597
#endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
598
599
int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
600
0
{
601
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
602
603
0
    params[0] =
604
0
        OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
605
0
    return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
606
0
}
607
608
int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
609
0
{
610
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
611
612
0
    params[0] =
613
0
        OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
614
0
    return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
615
0
}
616
617
unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
618
0
{
619
0
    return ctx->buf;
620
0
}
621
622
int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
623
0
{
624
0
    int ok;
625
0
    unsigned int v = (unsigned int)ctx->num;
626
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
627
628
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
629
0
    ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
630
631
0
    return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
632
0
}
633
634
int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
635
0
{
636
0
    int ok;
637
0
    unsigned int n = (unsigned int)num;
638
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
639
640
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
641
0
    ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
642
643
0
    if (ok != 0)
644
0
        ctx->num = (int)n;
645
0
    return ok != 0;
646
0
}
647
648
int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
649
252k
{
650
252k
    return cipher->key_len;
651
252k
}
652
653
int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
654
214k
{
655
214k
    if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) {
656
129k
        int ok;
657
129k
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
658
129k
        size_t len;
659
660
129k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
661
129k
        ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
662
129k
        if (ok <= 0)
663
0
            return EVP_CTRL_RET_UNSUPPORTED;
664
665
        /*-
666
         * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
667
         * less than INT_MAX but best to be safe.
668
         *
669
         * Casting away the const is annoying but required here.  We need to
670
         * cache the result for performance reasons.
671
         */
672
129k
        if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
673
0
            return -1;
674
129k
        ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
675
129k
    }
676
214k
    return ctx->key_len;
677
214k
}
678
679
int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
680
5.67k
{
681
5.67k
    return cipher->nid;
682
5.67k
}
683
684
int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
685
0
{
686
0
    return ctx->cipher->nid;
687
0
}
688
689
int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
690
99.0k
{
691
99.0k
    if (cipher == NULL)
692
0
        return 0;
693
99.0k
    if (cipher->prov != NULL)
694
99.0k
        return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
695
0
    return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
696
99.0k
}
697
698
int evp_cipher_get_number(const EVP_CIPHER *cipher)
699
0
{
700
0
    return cipher->name_id;
701
0
}
702
703
const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
704
0
{
705
0
    if (cipher->type_name != NULL)
706
0
        return cipher->type_name;
707
0
#ifndef FIPS_MODULE
708
0
    return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
709
#else
710
    return NULL;
711
#endif
712
0
}
713
714
const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
715
0
{
716
0
    if (cipher->description != NULL)
717
0
        return cipher->description;
718
0
#ifndef FIPS_MODULE
719
0
    return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
720
#else
721
    return NULL;
722
#endif
723
0
}
724
725
int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
726
                            void (*fn)(const char *name, void *data),
727
                            void *data)
728
0
{
729
0
    if (cipher->prov != NULL)
730
0
        return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
731
732
0
    return 1;
733
0
}
734
735
const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
736
1.44M
{
737
1.44M
    return cipher->prov;
738
1.44M
}
739
740
int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
741
615k
{
742
615k
    return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
743
615k
}
744
745
int EVP_MD_is_a(const EVP_MD *md, const char *name)
746
616k
{
747
616k
    if (md == NULL)
748
0
        return 0;
749
616k
    if (md->prov != NULL)
750
331k
        return evp_is_a(md->prov, md->name_id, NULL, name);
751
285k
    return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
752
616k
}
753
754
int evp_md_get_number(const EVP_MD *md)
755
0
{
756
0
    return md->name_id;
757
0
}
758
759
const char *EVP_MD_get0_description(const EVP_MD *md)
760
0
{
761
0
    if (md->description != NULL)
762
0
        return md->description;
763
0
#ifndef FIPS_MODULE
764
0
    return OBJ_nid2ln(EVP_MD_nid(md));
765
#else
766
    return NULL;
767
#endif
768
0
}
769
770
const char *EVP_MD_get0_name(const EVP_MD *md)
771
1.30M
{
772
1.30M
    if (md == NULL)
773
0
        return NULL;
774
1.30M
    if (md->type_name != NULL)
775
720k
        return md->type_name;
776
581k
#ifndef FIPS_MODULE
777
581k
    return OBJ_nid2sn(EVP_MD_nid(md));
778
#else
779
    return NULL;
780
#endif
781
1.30M
}
782
783
int EVP_MD_names_do_all(const EVP_MD *md,
784
                        void (*fn)(const char *name, void *data),
785
                        void *data)
786
0
{
787
0
    if (md->prov != NULL)
788
0
        return evp_names_do_all(md->prov, md->name_id, fn, data);
789
790
0
    return 1;
791
0
}
792
793
const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
794
1.04M
{
795
1.04M
    return md->prov;
796
1.04M
}
797
798
int EVP_MD_get_type(const EVP_MD *md)
799
2.68M
{
800
2.68M
    return md->type;
801
2.68M
}
802
803
int EVP_MD_get_pkey_type(const EVP_MD *md)
804
0
{
805
0
    return md->pkey_type;
806
0
}
807
808
int EVP_MD_get_block_size(const EVP_MD *md)
809
2.04M
{
810
2.04M
    if (md == NULL) {
811
0
        ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
812
0
        return -1;
813
0
    }
814
2.04M
    return md->block_size;
815
2.04M
}
816
817
int EVP_MD_get_size(const EVP_MD *md)
818
252M
{
819
252M
    if (md == NULL) {
820
2
        ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
821
2
        return -1;
822
2
    }
823
252M
    return md->md_size;
824
252M
}
825
826
unsigned long EVP_MD_get_flags(const EVP_MD *md)
827
1.13M
{
828
1.13M
    return md->flags;
829
1.13M
}
830
831
EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
832
0
{
833
0
    EVP_MD *md = evp_md_new();
834
835
0
    if (md != NULL) {
836
0
        md->type = md_type;
837
0
        md->pkey_type = pkey_type;
838
0
        md->origin = EVP_ORIG_METH;
839
0
    }
840
0
    return md;
841
0
}
842
843
EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
844
0
{
845
0
    EVP_MD *to = NULL;
846
847
    /*
848
     * Non-legacy EVP_MDs can't be duplicated like this.
849
     * Use EVP_MD_up_ref() instead.
850
     */
851
0
    if (md->prov != NULL)
852
0
        return NULL;
853
854
0
    if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
855
0
        CRYPTO_REF_COUNT refcnt = to->refcnt;
856
857
0
        memcpy(to, md, sizeof(*to));
858
0
        to->refcnt = refcnt;
859
0
        to->origin = EVP_ORIG_METH;
860
0
    }
861
0
    return to;
862
0
}
863
864
void evp_md_free_int(EVP_MD *md)
865
1.03k
{
866
1.03k
    OPENSSL_free(md->type_name);
867
1.03k
    ossl_provider_free(md->prov);
868
1.03k
    CRYPTO_FREE_REF(&md->refcnt);
869
1.03k
    OPENSSL_free(md);
870
1.03k
}
871
872
void EVP_MD_meth_free(EVP_MD *md)
873
0
{
874
0
    if (md == NULL || md->origin != EVP_ORIG_METH)
875
0
       return;
876
877
0
    evp_md_free_int(md);
878
0
}
879
880
int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
881
0
{
882
0
    if (md->block_size != 0)
883
0
        return 0;
884
885
0
    md->block_size = blocksize;
886
0
    return 1;
887
0
}
888
int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
889
0
{
890
0
    if (md->md_size != 0)
891
0
        return 0;
892
893
0
    md->md_size = resultsize;
894
0
    return 1;
895
0
}
896
int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
897
0
{
898
0
    if (md->ctx_size != 0)
899
0
        return 0;
900
901
0
    md->ctx_size = datasize;
902
0
    return 1;
903
0
}
904
int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
905
0
{
906
0
    if (md->flags != 0)
907
0
        return 0;
908
909
0
    md->flags = flags;
910
0
    return 1;
911
0
}
912
int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
913
0
{
914
0
    if (md->init != NULL)
915
0
        return 0;
916
917
0
    md->init = init;
918
0
    return 1;
919
0
}
920
int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
921
                                                     const void *data,
922
                                                     size_t count))
923
0
{
924
0
    if (md->update != NULL)
925
0
        return 0;
926
927
0
    md->update = update;
928
0
    return 1;
929
0
}
930
int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
931
                                                   unsigned char *md))
932
0
{
933
0
    if (md->final != NULL)
934
0
        return 0;
935
936
0
    md->final = final;
937
0
    return 1;
938
0
}
939
int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
940
                                                 const EVP_MD_CTX *from))
941
0
{
942
0
    if (md->copy != NULL)
943
0
        return 0;
944
945
0
    md->copy = copy;
946
0
    return 1;
947
0
}
948
int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
949
0
{
950
0
    if (md->cleanup != NULL)
951
0
        return 0;
952
953
0
    md->cleanup = cleanup;
954
0
    return 1;
955
0
}
956
int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
957
                                                 int p1, void *p2))
958
0
{
959
0
    if (md->md_ctrl != NULL)
960
0
        return 0;
961
962
0
    md->md_ctrl = ctrl;
963
0
    return 1;
964
0
}
965
966
int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
967
0
{
968
0
    return md->block_size;
969
0
}
970
int EVP_MD_meth_get_result_size(const EVP_MD *md)
971
0
{
972
0
    return md->md_size;
973
0
}
974
int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
975
0
{
976
0
    return md->ctx_size;
977
0
}
978
unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
979
0
{
980
0
    return md->flags;
981
0
}
982
int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
983
0
{
984
0
    return md->init;
985
0
}
986
int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
987
                                                const void *data,
988
                                                size_t count)
989
0
{
990
0
    return md->update;
991
0
}
992
int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
993
                                               unsigned char *md)
994
0
{
995
0
    return md->final;
996
0
}
997
int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
998
                                              const EVP_MD_CTX *from)
999
0
{
1000
0
    return md->copy;
1001
0
}
1002
int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
1003
0
{
1004
0
    return md->cleanup;
1005
0
}
1006
int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
1007
                                              int p1, void *p2)
1008
0
{
1009
0
    return md->md_ctrl;
1010
0
}
1011
1012
#ifndef OPENSSL_NO_DEPRECATED_3_0
1013
const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
1014
0
{
1015
0
    if (ctx == NULL)
1016
0
        return NULL;
1017
0
    return ctx->reqdigest;
1018
0
}
1019
#endif
1020
1021
const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
1022
246M
{
1023
246M
    if (ctx == NULL)
1024
114k
        return NULL;
1025
246M
    return ctx->reqdigest;
1026
246M
}
1027
1028
EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
1029
0
{
1030
0
    EVP_MD *md;
1031
1032
0
    if (ctx == NULL)
1033
0
        return NULL;
1034
0
    md = (EVP_MD *)ctx->reqdigest;
1035
0
    if (md == NULL || !EVP_MD_up_ref(md))
1036
0
        return NULL;
1037
0
    return md;
1038
0
}
1039
1040
EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
1041
54.3k
{
1042
54.3k
    return ctx->pctx;
1043
54.3k
}
1044
1045
#if !defined(FIPS_MODULE)
1046
void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
1047
8.40k
{
1048
    /*
1049
     * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
1050
     * we have to deal with the cleanup job here.
1051
     */
1052
8.40k
    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
1053
8.40k
        EVP_PKEY_CTX_free(ctx->pctx);
1054
1055
8.40k
    ctx->pctx = pctx;
1056
1057
8.40k
    if (pctx != NULL) {
1058
        /* make sure pctx is not freed when destroying EVP_MD_CTX */
1059
8.40k
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1060
8.40k
    } else {
1061
0
        EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1062
0
    }
1063
8.40k
}
1064
#endif /* !defined(FIPS_MODULE) */
1065
1066
void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
1067
0
{
1068
0
    return ctx->md_data;
1069
0
}
1070
1071
int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
1072
                                             const void *data, size_t count)
1073
0
{
1074
0
    return ctx->update;
1075
0
}
1076
1077
void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
1078
                              int (*update) (EVP_MD_CTX *ctx,
1079
                                             const void *data, size_t count))
1080
0
{
1081
0
    ctx->update = update;
1082
0
}
1083
1084
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
1085
5.08M
{
1086
5.08M
    ctx->flags |= flags;
1087
5.08M
}
1088
1089
void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
1090
411M
{
1091
411M
    ctx->flags &= ~flags;
1092
411M
}
1093
1094
int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
1095
11.4M
{
1096
11.4M
    return (ctx->flags & flags);
1097
11.4M
}
1098
1099
static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
1100
                                          unsigned int enable)
1101
0
{
1102
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1103
1104
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
1105
0
    return EVP_CIPHER_CTX_set_params(ctx, params);
1106
0
}
1107
1108
void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
1109
12.1k
{
1110
12.1k
    int oldflags = ctx->flags;
1111
1112
12.1k
    ctx->flags |= flags;
1113
12.1k
    if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1114
0
        evp_cipher_ctx_enable_use_bits(ctx, 1);
1115
12.1k
}
1116
1117
void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
1118
0
{
1119
0
    int oldflags = ctx->flags;
1120
1121
0
    ctx->flags &= ~flags;
1122
0
    if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1123
0
        evp_cipher_ctx_enable_use_bits(ctx, 0);
1124
0
}
1125
1126
int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
1127
0
{
1128
0
    return (ctx->flags & flags);
1129
0
}
1130
1131
#if !defined(FIPS_MODULE)
1132
1133
int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
1134
42.4k
{
1135
42.4k
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1136
1137
42.4k
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1138
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1139
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1140
0
        return -2;
1141
0
    }
1142
1143
42.4k
    if (name == NULL)
1144
0
        return -1;
1145
1146
42.4k
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1147
42.4k
                                                 (char *)name, 0);
1148
42.4k
    return EVP_PKEY_CTX_set_params(ctx, params);
1149
42.4k
}
1150
1151
int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1152
0
{
1153
0
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1154
0
    OSSL_PARAM *p = params;
1155
1156
0
    if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1157
        /* There is no legacy support for this */
1158
0
        ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1159
        /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1160
0
        return -2;
1161
0
    }
1162
1163
0
    if (name == NULL)
1164
0
        return -1;
1165
1166
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1167
0
                                            name, namelen);
1168
0
    if (!EVP_PKEY_CTX_get_params(ctx, params))
1169
0
        return -1;
1170
0
    return 1;
1171
0
}
1172
1173
/*
1174
 * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
1175
 * while providing a generic way of generating a new asymmetric key pair
1176
 * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
1177
 * The library context I<libctx> and property query I<propq>
1178
 * are used when fetching algorithms from providers.
1179
 * The I<params> specify algorithm-specific parameters
1180
 * such as the RSA modulus size or the name of an EC curve.
1181
 */
1182
static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
1183
                                 const char *propq, const OSSL_PARAM *params)
1184
36.2k
{
1185
36.2k
    EVP_PKEY *pkey = NULL;
1186
36.2k
    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
1187
1188
36.2k
    if (ctx != NULL
1189
36.2k
            && EVP_PKEY_keygen_init(ctx) > 0
1190
36.2k
            && EVP_PKEY_CTX_set_params(ctx, params))
1191
36.2k
        (void)EVP_PKEY_generate(ctx, &pkey);
1192
1193
36.2k
    EVP_PKEY_CTX_free(ctx);
1194
36.2k
    return pkey;
1195
36.2k
}
1196
1197
EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
1198
                            const char *type, ...)
1199
{
1200
    va_list args;
1201
    size_t bits;
1202
    char *name;
1203
    OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1204
    EVP_PKEY *ret = NULL;
1205
1206
    va_start(args, type);
1207
1208
    if (OPENSSL_strcasecmp(type, "RSA") == 0) {
1209
        bits = va_arg(args, size_t);
1210
        params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
1211
    } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
1212
        name = va_arg(args, char *);
1213
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1214
                                                     name, 0);
1215
    } else if (OPENSSL_strcasecmp(type, "ED25519") != 0
1216
               && OPENSSL_strcasecmp(type, "X25519") != 0
1217
               && OPENSSL_strcasecmp(type, "ED448") != 0
1218
               && OPENSSL_strcasecmp(type, "X448") != 0
1219
               && OPENSSL_strcasecmp(type, "SM2") != 0) {
1220
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
1221
        goto end;
1222
    }
1223
    ret = evp_pkey_keygen(libctx, type, propq, params);
1224
1225
 end:
1226
    va_end(args);
1227
    return ret;
1228
}
1229
1230
#endif /* !defined(FIPS_MODULE) */