Coverage Report

Created: 2026-09-12 06:55

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