Coverage Report

Created: 2026-02-14 07:20

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