Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/include/crypto/evp.h
Line
Count
Source
1
/*
2
 * Copyright 2015-2025 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
#ifndef OSSL_CRYPTO_EVP_H
11
#define OSSL_CRYPTO_EVP_H
12
#pragma once
13
14
#include <openssl/evp.h>
15
#include <openssl/core_dispatch.h>
16
#include "internal/refcount.h"
17
#include "crypto/ecx.h"
18
19
/*
20
 * Default PKCS5 PBE KDF salt lengths
21
 * In RFC 8018, PBE1 uses 8 bytes (64 bits) for its salt length.
22
 * It also specifies to use at least 8 bytes for PBES2.
23
 * The NIST requirement for PBKDF2 is 128 bits so we use this as the
24
 * default for PBE2 (scrypt and HKDF2)
25
 */
26
0
#define PKCS5_DEFAULT_PBE1_SALT_LEN PKCS5_SALT_LEN
27
0
#define PKCS5_DEFAULT_PBE2_SALT_LEN 16
28
/*
29
 * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
30
 * values in evp.h
31
 */
32
31.1M
#define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
33
1.28G
#define EVP_MD_CTX_FLAG_FINALISED 0x0800
34
35
#define evp_pkey_ctx_is_legacy(ctx) \
36
276k
    ((ctx)->keymgmt == NULL)
37
#define evp_pkey_ctx_is_provided(ctx) \
38
62.9k
    (!evp_pkey_ctx_is_legacy(ctx))
39
40
struct evp_pkey_ctx_st {
41
    /* Actual operation */
42
    int operation;
43
44
    /*
45
     * Library context, property query, keytype and keymgmt associated with
46
     * this context
47
     */
48
    OSSL_LIB_CTX *libctx;
49
    char *propquery;
50
    const char *keytype;
51
    /* If |pkey| below is set, this field is always a reference to its keymgmt */
52
    EVP_KEYMGMT *keymgmt;
53
54
    union {
55
        struct {
56
            void *genctx;
57
        } keymgmt;
58
59
        struct {
60
            EVP_KEYEXCH *exchange;
61
            /*
62
             * Opaque ctx returned from a providers exchange algorithm
63
             * implementation OSSL_FUNC_keyexch_newctx()
64
             */
65
            void *algctx;
66
        } kex;
67
68
        struct {
69
            EVP_SIGNATURE *signature;
70
            /*
71
             * Opaque ctx returned from a providers signature algorithm
72
             * implementation OSSL_FUNC_signature_newctx()
73
             */
74
            void *algctx;
75
        } sig;
76
77
        struct {
78
            EVP_ASYM_CIPHER *cipher;
79
            /*
80
             * Opaque ctx returned from a providers asymmetric cipher algorithm
81
             * implementation OSSL_FUNC_asym_cipher_newctx()
82
             */
83
            void *algctx;
84
        } ciph;
85
        struct {
86
            EVP_KEM *kem;
87
            /*
88
             * Opaque ctx returned from a providers KEM algorithm
89
             * implementation OSSL_FUNC_kem_newctx()
90
             */
91
            void *algctx;
92
        } encap;
93
    } op;
94
95
    /*
96
     * Cached parameters.  Inits of operations that depend on these should
97
     * call evp_pkey_ctx_use_delayed_data() when the operation has been set
98
     * up properly.
99
     */
100
    struct {
101
        /* Distinguishing Identifier, ISO/IEC 15946-3, FIPS 196 */
102
        char *dist_id_name; /* The name used with EVP_PKEY_CTX_ctrl_str() */
103
        void *dist_id; /* The distinguishing ID itself */
104
        size_t dist_id_len; /* The length of the distinguishing ID */
105
106
        /* Indicators of what has been set.  Keep them together! */
107
        unsigned int dist_id_set : 1;
108
    } cached_parameters;
109
110
    /* Application specific data, usually used by the callback */
111
    void *app_data;
112
    /* Keygen callback */
113
    EVP_PKEY_gen_cb *pkey_gencb;
114
    /* implementation specific keygen data */
115
    int *keygen_info;
116
    int keygen_info_count;
117
118
    /* Legacy fields below */
119
120
    /* EVP_PKEY identity */
121
    int legacy_keytype;
122
    /* Method associated with this operation */
123
    const EVP_PKEY_METHOD *pmeth;
124
    /* Engine that implements this method or NULL if builtin */
125
    ENGINE *engine;
126
    /* Key: may be NULL */
127
    EVP_PKEY *pkey;
128
    /* Peer key for key agreement, may be NULL */
129
    EVP_PKEY *peerkey;
130
    /* Algorithm specific data */
131
    void *data;
132
    /* Indicator if digest_custom needs to be called */
133
    unsigned int flag_call_digest_custom : 1;
134
    /*
135
     * Used to support taking custody of memory in the case of a provider being
136
     * used with the deprecated EVP_PKEY_CTX_set_rsa_keygen_pubexp() API. This
137
     * member should NOT be used for any other purpose and should be removed
138
     * when said deprecated API is excised completely.
139
     */
140
    BIGNUM *rsa_pubexp;
141
} /* EVP_PKEY_CTX */;
142
143
0
#define EVP_PKEY_FLAG_DYNAMIC 1
144
145
struct evp_pkey_method_st {
146
    int pkey_id;
147
    int flags;
148
    int (*init)(EVP_PKEY_CTX *ctx);
149
    int (*copy)(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
150
    void (*cleanup)(EVP_PKEY_CTX *ctx);
151
    int (*paramgen_init)(EVP_PKEY_CTX *ctx);
152
    int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
153
    int (*keygen_init)(EVP_PKEY_CTX *ctx);
154
    int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
155
    int (*sign_init)(EVP_PKEY_CTX *ctx);
156
    int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
157
        const unsigned char *tbs, size_t tbslen);
158
    int (*verify_init)(EVP_PKEY_CTX *ctx);
159
    int (*verify)(EVP_PKEY_CTX *ctx,
160
        const unsigned char *sig, size_t siglen,
161
        const unsigned char *tbs, size_t tbslen);
162
    int (*verify_recover_init)(EVP_PKEY_CTX *ctx);
163
    int (*verify_recover)(EVP_PKEY_CTX *ctx,
164
        unsigned char *rout, size_t *routlen,
165
        const unsigned char *sig, size_t siglen);
166
    int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
167
    int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
168
        EVP_MD_CTX *mctx);
169
    int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
170
    int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
171
        EVP_MD_CTX *mctx);
172
    int (*encrypt_init)(EVP_PKEY_CTX *ctx);
173
    int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
174
        const unsigned char *in, size_t inlen);
175
    int (*decrypt_init)(EVP_PKEY_CTX *ctx);
176
    int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
177
        const unsigned char *in, size_t inlen);
178
    int (*derive_init)(EVP_PKEY_CTX *ctx);
179
    int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
180
    int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
181
    int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value);
182
    int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
183
        const unsigned char *tbs, size_t tbslen);
184
    int (*digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig,
185
        size_t siglen, const unsigned char *tbs,
186
        size_t tbslen);
187
    int (*check)(EVP_PKEY *pkey);
188
    int (*public_check)(EVP_PKEY *pkey);
189
    int (*param_check)(EVP_PKEY *pkey);
190
191
    int (*digest_custom)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
192
} /* EVP_PKEY_METHOD */;
193
194
DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
195
196
void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
197
198
const EVP_PKEY_METHOD *ossl_dh_pkey_method(void);
199
const EVP_PKEY_METHOD *ossl_dhx_pkey_method(void);
200
const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void);
201
const EVP_PKEY_METHOD *ossl_ec_pkey_method(void);
202
const EVP_PKEY_METHOD *ossl_ecx25519_pkey_method(void);
203
const EVP_PKEY_METHOD *ossl_ecx448_pkey_method(void);
204
const EVP_PKEY_METHOD *ossl_ed25519_pkey_method(void);
205
const EVP_PKEY_METHOD *ossl_ed448_pkey_method(void);
206
const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void);
207
const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void);
208
209
struct evp_mac_st {
210
    OSSL_PROVIDER *prov;
211
    int name_id;
212
    char *type_name;
213
    const char *description;
214
215
    CRYPTO_REF_COUNT refcnt;
216
217
    OSSL_FUNC_mac_newctx_fn *newctx;
218
    OSSL_FUNC_mac_dupctx_fn *dupctx;
219
    OSSL_FUNC_mac_freectx_fn *freectx;
220
    OSSL_FUNC_mac_init_fn *init;
221
    OSSL_FUNC_mac_update_fn *update;
222
    OSSL_FUNC_mac_final_fn *final;
223
    OSSL_FUNC_mac_gettable_params_fn *gettable_params;
224
    OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params;
225
    OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params;
226
    OSSL_FUNC_mac_get_params_fn *get_params;
227
    OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params;
228
    OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params;
229
    OSSL_FUNC_mac_init_skey_fn *init_skey;
230
};
231
232
struct evp_kdf_st {
233
    OSSL_PROVIDER *prov;
234
    int name_id;
235
    char *type_name;
236
    const char *description;
237
    CRYPTO_REF_COUNT refcnt;
238
239
    OSSL_FUNC_kdf_newctx_fn *newctx;
240
    OSSL_FUNC_kdf_dupctx_fn *dupctx;
241
    OSSL_FUNC_kdf_freectx_fn *freectx;
242
    OSSL_FUNC_kdf_reset_fn *reset;
243
    OSSL_FUNC_kdf_derive_fn *derive;
244
    OSSL_FUNC_kdf_gettable_params_fn *gettable_params;
245
    OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params;
246
    OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params;
247
    OSSL_FUNC_kdf_get_params_fn *get_params;
248
    OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params;
249
    OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params;
250
    OSSL_FUNC_kdf_set_skey_fn *set_skey;
251
    OSSL_FUNC_kdf_derive_skey_fn *derive_skey;
252
};
253
254
42.1M
#define EVP_ORIG_DYNAMIC 0
255
997
#define EVP_ORIG_GLOBAL 1
256
456M
#define EVP_ORIG_METH 2
257
258
struct evp_md_st {
259
    /* nid */
260
    int type;
261
262
    /* Legacy structure members */
263
    int pkey_type;
264
    int md_size;
265
    unsigned long flags;
266
    int origin;
267
    int (*init)(EVP_MD_CTX *ctx);
268
    int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
269
    int (*final)(EVP_MD_CTX *ctx, unsigned char *md);
270
    int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from);
271
    int (*cleanup)(EVP_MD_CTX *ctx);
272
    int block_size;
273
    int ctx_size; /* how big does the ctx->md_data need to be */
274
    /* control function */
275
    int (*md_ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
276
277
    /* New structure members */
278
    /* Above comment to be removed when legacy has gone */
279
    int name_id;
280
    char *type_name;
281
    const char *description;
282
    OSSL_PROVIDER *prov;
283
    CRYPTO_REF_COUNT refcnt;
284
    OSSL_FUNC_digest_newctx_fn *newctx;
285
    OSSL_FUNC_digest_init_fn *dinit;
286
    OSSL_FUNC_digest_update_fn *dupdate;
287
    OSSL_FUNC_digest_final_fn *dfinal;
288
    OSSL_FUNC_digest_squeeze_fn *dsqueeze;
289
    OSSL_FUNC_digest_digest_fn *digest;
290
    OSSL_FUNC_digest_freectx_fn *freectx;
291
    OSSL_FUNC_digest_copyctx_fn *copyctx;
292
    OSSL_FUNC_digest_dupctx_fn *dupctx;
293
    OSSL_FUNC_digest_get_params_fn *get_params;
294
    OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params;
295
    OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params;
296
    OSSL_FUNC_digest_gettable_params_fn *gettable_params;
297
    OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params;
298
    OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params;
299
300
} /* EVP_MD */;
301
302
struct evp_cipher_st {
303
    int nid;
304
305
    int block_size;
306
    /* Default value for variable length ciphers */
307
    int key_len;
308
    int iv_len;
309
310
    /* Legacy structure members */
311
    /* Various flags */
312
    unsigned long flags;
313
    /* How the EVP_CIPHER was created. */
314
    int origin;
315
    /* init key */
316
    int (*init)(EVP_CIPHER_CTX *ctx, const unsigned char *key,
317
        const unsigned char *iv, int enc);
318
    /* encrypt/decrypt data */
319
    int (*do_cipher)(EVP_CIPHER_CTX *ctx, unsigned char *out,
320
        const unsigned char *in, size_t inl);
321
    /* cleanup ctx */
322
    int (*cleanup)(EVP_CIPHER_CTX *);
323
    /* how big ctx->cipher_data needs to be */
324
    int ctx_size;
325
    /* Populate a ASN1_TYPE with parameters */
326
    int (*set_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
327
    /* Get parameters from a ASN1_TYPE */
328
    int (*get_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
329
    /* Miscellaneous operations */
330
    int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
331
    /* Application data */
332
    void *app_data;
333
334
    /* New structure members */
335
    /* Above comment to be removed when legacy has gone */
336
    int name_id;
337
    char *type_name;
338
    const char *description;
339
    OSSL_PROVIDER *prov;
340
    CRYPTO_REF_COUNT refcnt;
341
    OSSL_FUNC_cipher_newctx_fn *newctx;
342
    OSSL_FUNC_cipher_encrypt_init_fn *einit;
343
    OSSL_FUNC_cipher_decrypt_init_fn *dinit;
344
    OSSL_FUNC_cipher_update_fn *cupdate;
345
    OSSL_FUNC_cipher_final_fn *cfinal;
346
    OSSL_FUNC_cipher_cipher_fn *ccipher;
347
    OSSL_FUNC_cipher_pipeline_encrypt_init_fn *p_einit;
348
    OSSL_FUNC_cipher_pipeline_decrypt_init_fn *p_dinit;
349
    OSSL_FUNC_cipher_pipeline_update_fn *p_cupdate;
350
    OSSL_FUNC_cipher_pipeline_final_fn *p_cfinal;
351
    OSSL_FUNC_cipher_freectx_fn *freectx;
352
    OSSL_FUNC_cipher_dupctx_fn *dupctx;
353
    OSSL_FUNC_cipher_get_params_fn *get_params;
354
    OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params;
355
    OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params;
356
    OSSL_FUNC_cipher_gettable_params_fn *gettable_params;
357
    OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params;
358
    OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params;
359
    OSSL_FUNC_cipher_encrypt_skey_init_fn *einit_skey;
360
    OSSL_FUNC_cipher_decrypt_skey_init_fn *dinit_skey;
361
} /* EVP_CIPHER */;
362
363
/* Macros to code block cipher wrappers */
364
365
/* Wrapper functions for each cipher mode */
366
367
#define EVP_C_DATA(kstruct, ctx) \
368
0
    ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
369
370
#define BLOCK_CIPHER_ecb_loop()                       \
371
0
    size_t i, bl;                                     \
372
0
    bl = EVP_CIPHER_CTX_get0_cipher(ctx)->block_size; \
373
0
    if (inl < bl)                                     \
374
0
        return 1;                                     \
375
0
    inl -= bl;                                        \
376
0
    for (i = 0; i <= inl; i += bl)
377
378
#define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched)                                                            \
379
    static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)           \
380
0
    {                                                                                                                     \
381
0
        BLOCK_CIPHER_ecb_loop()                                                                                           \
382
0
            cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct, ctx)->ksched, EVP_CIPHER_CTX_is_encrypting(ctx)); \
383
0
        return 1;                                                                                                         \
384
0
    }
Unexecuted instantiation: e_aria.c:aria_128_ecb_cipher
Unexecuted instantiation: e_aria.c:aria_192_ecb_cipher
Unexecuted instantiation: e_aria.c:aria_256_ecb_cipher
Unexecuted instantiation: e_bf.c:bf_ecb_cipher
Unexecuted instantiation: e_cast.c:cast5_ecb_cipher
Unexecuted instantiation: e_rc2.c:rc2_ecb_cipher
Unexecuted instantiation: e_rc5.c:rc5_32_12_16_ecb_cipher
Unexecuted instantiation: e_seed.c:seed_ecb_cipher
385
386
0
#define EVP_MAXCHUNK ((size_t)1 << 30)
387
388
#define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)                                                      \
389
    static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)            \
390
0
    {                                                                                                                      \
391
0
        while (inl >= EVP_MAXCHUNK) {                                                                                      \
392
0
            int num = EVP_CIPHER_CTX_get_num(ctx);                                                                         \
393
0
            cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv, &num); \
394
0
            EVP_CIPHER_CTX_set_num(ctx, num);                                                                              \
395
0
            inl -= EVP_MAXCHUNK;                                                                                           \
396
0
            in += EVP_MAXCHUNK;                                                                                            \
397
0
            out += EVP_MAXCHUNK;                                                                                           \
398
0
        }                                                                                                                  \
399
0
        if (inl) {                                                                                                         \
400
0
            int num = EVP_CIPHER_CTX_get_num(ctx);                                                                         \
401
0
            cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv, &num);          \
402
0
            EVP_CIPHER_CTX_set_num(ctx, num);                                                                              \
403
0
        }                                                                                                                  \
404
0
        return 1;                                                                                                          \
405
0
    }
Unexecuted instantiation: e_aria.c:aria_128_ofb_cipher
Unexecuted instantiation: e_aria.c:aria_192_ofb_cipher
Unexecuted instantiation: e_aria.c:aria_256_ofb_cipher
Unexecuted instantiation: e_bf.c:bf_ofb_cipher
Unexecuted instantiation: e_cast.c:cast5_ofb_cipher
Unexecuted instantiation: e_idea.c:idea_ofb_cipher
Unexecuted instantiation: e_rc2.c:rc2_ofb_cipher
Unexecuted instantiation: e_rc5.c:rc5_32_12_16_ofb_cipher
Unexecuted instantiation: e_seed.c:seed_ofb_cipher
406
407
#define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched)                                                                                 \
408
    static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)                                \
409
0
    {                                                                                                                                          \
410
0
        while (inl >= EVP_MAXCHUNK) {                                                                                                          \
411
0
            cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx)); \
412
0
            inl -= EVP_MAXCHUNK;                                                                                                               \
413
0
            in += EVP_MAXCHUNK;                                                                                                                \
414
0
            out += EVP_MAXCHUNK;                                                                                                               \
415
0
        }                                                                                                                                      \
416
0
        if (inl)                                                                                                                               \
417
0
            cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));          \
418
0
        return 1;                                                                                                                              \
419
0
    }
Unexecuted instantiation: e_aria.c:aria_128_cbc_cipher
Unexecuted instantiation: e_aria.c:aria_192_cbc_cipher
Unexecuted instantiation: e_aria.c:aria_256_cbc_cipher
Unexecuted instantiation: e_bf.c:bf_cbc_cipher
Unexecuted instantiation: e_cast.c:cast5_cbc_cipher
Unexecuted instantiation: e_idea.c:idea_cbc_cipher
Unexecuted instantiation: e_rc2.c:rc2_cbc_cipher
Unexecuted instantiation: e_rc5.c:rc5_32_12_16_cbc_cipher
Unexecuted instantiation: e_seed.c:seed_cbc_cipher
420
421
#define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched)                                                                                       \
422
    static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)                                    \
423
0
    {                                                                                                                                                       \
424
0
        size_t chunk = EVP_MAXCHUNK;                                                                                                                        \
425
0
        if (cbits == 1)                                                                                                                                     \
426
0
            chunk >>= 3;                                                                                                                                    \
427
0
        if (inl < chunk)                                                                                                                                    \
428
0
            chunk = inl;                                                                                                                                    \
429
0
        while (inl && inl >= chunk) {                                                                                                                       \
430
0
            int num = EVP_CIPHER_CTX_get_num(ctx);                                                                                                          \
431
0
            cprefix##_cfb##cbits##_encrypt(in, out, (long)((cbits == 1) && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) ? chunk * 8 : chunk), \
432
0
                &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,                                                                                                 \
433
0
                &num, EVP_CIPHER_CTX_is_encrypting(ctx));                                                                                                   \
434
0
            EVP_CIPHER_CTX_set_num(ctx, num);                                                                                                               \
435
0
            inl -= chunk;                                                                                                                                   \
436
0
            in += chunk;                                                                                                                                    \
437
0
            out += chunk;                                                                                                                                   \
438
0
            if (inl < chunk)                                                                                                                                \
439
0
                chunk = inl;                                                                                                                                \
440
0
        }                                                                                                                                                   \
441
0
        return 1;                                                                                                                                           \
442
0
    }
Unexecuted instantiation: e_aria.c:aria_128_cfb128_cipher
Unexecuted instantiation: e_aria.c:aria_192_cfb128_cipher
Unexecuted instantiation: e_aria.c:aria_256_cfb128_cipher
Unexecuted instantiation: e_aria.c:aria_128_cfb1_cipher
Unexecuted instantiation: e_aria.c:aria_192_cfb1_cipher
Unexecuted instantiation: e_aria.c:aria_256_cfb1_cipher
Unexecuted instantiation: e_aria.c:aria_128_cfb8_cipher
Unexecuted instantiation: e_aria.c:aria_192_cfb8_cipher
Unexecuted instantiation: e_aria.c:aria_256_cfb8_cipher
Unexecuted instantiation: e_bf.c:bf_cfb64_cipher
Unexecuted instantiation: e_cast.c:cast5_cfb64_cipher
Unexecuted instantiation: e_idea.c:idea_cfb64_cipher
Unexecuted instantiation: e_rc2.c:rc2_cfb64_cipher
Unexecuted instantiation: e_rc5.c:rc5_32_12_16_cfb64_cipher
Unexecuted instantiation: e_seed.c:seed_cfb128_cipher
443
444
#define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
445
    BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched)             \
446
        BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched)  \
447
            BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched)     \
448
                BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
449
450
#define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
451
    key_len, iv_len, flags, init_key, cleanup,                                \
452
    set_asn1, get_asn1, ctrl)                                                 \
453
    static const EVP_CIPHER cname##_##mode = {                                \
454
        nid##_##nmode, block_size, key_len, iv_len,                           \
455
        flags | EVP_CIPH_##MODE##_MODE,                                       \
456
        EVP_ORIG_GLOBAL,                                                      \
457
        init_key,                                                             \
458
        cname##_##mode##_cipher,                                              \
459
        cleanup,                                                              \
460
        sizeof(kstruct),                                                      \
461
        set_asn1, get_asn1,                                                   \
462
        ctrl,                                                                 \
463
        NULL                                                                  \
464
    };                                                                        \
465
9.18k
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb128
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb128
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb128
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb1
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb1
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb1
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb8
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb8
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb8
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_cfb64
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_cfb64
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb64
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb1
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb8
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_cfb64
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
Unexecuted instantiation: EVP_des_ede_ecb
EVP_des_ede3_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_cfb64
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
Unexecuted instantiation: EVP_des_ede3_ecb
EVP_des_ede3_cfb1
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_cfb8
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_cfb64
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_cfb64
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc5_32_12_16_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc5_32_12_16_cfb64
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc5_32_12_16_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc5_32_12_16_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_cbc
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_cfb128
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_ofb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_ecb
Line
Count
Source
465
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
466
467
#define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len,         \
468
    iv_len, flags, init_key, cleanup, set_asn1,                                \
469
    get_asn1, ctrl)                                                            \
470
    BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
471
        iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
472
473
#define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len,                 \
474
    iv_len, cbits, flags, init_key, cleanup,                               \
475
    set_asn1, get_asn1, ctrl)                                              \
476
    BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
477
        key_len, iv_len, flags, init_key, cleanup, set_asn1,               \
478
        get_asn1, ctrl)
479
480
#define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len,          \
481
    iv_len, cbits, flags, init_key, cleanup,                        \
482
    set_asn1, get_asn1, ctrl)                                       \
483
    BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
484
        key_len, iv_len, flags, init_key, cleanup, set_asn1,        \
485
        get_asn1, ctrl)
486
487
#define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len,         \
488
    flags, init_key, cleanup, set_asn1,                                        \
489
    get_asn1, ctrl)                                                            \
490
    BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
491
        0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
492
493
#define BLOCK_CIPHER_defs(cname, kstruct,                                             \
494
    nid, block_size, key_len, iv_len, cbits, flags,                                   \
495
    init_key, cleanup, set_asn1, get_asn1, ctrl)                                      \
496
    BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags,     \
497
        init_key, cleanup, set_asn1, get_asn1, ctrl)                                  \
498
        BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits,             \
499
            flags, init_key, cleanup, set_asn1, get_asn1, ctrl)                       \
500
            BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits,         \
501
                flags, init_key, cleanup, set_asn1, get_asn1, ctrl)                   \
502
                BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
503
                    init_key, cleanup, set_asn1, get_asn1, ctrl)
504
505
/*-
506
#define BLOCK_CIPHER_defs(cname, kstruct, \
507
                                nid, block_size, key_len, iv_len, flags,\
508
                                 init_key, cleanup, set_asn1, get_asn1, ctrl)\
509
static const EVP_CIPHER cname##_cbc = {\
510
        nid##_cbc, block_size, key_len, iv_len, \
511
        flags | EVP_CIPH_CBC_MODE,\
512
        EVP_ORIG_GLOBAL,\
513
        init_key,\
514
        cname##_cbc_cipher,\
515
        cleanup,\
516
        sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
517
                sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
518
        set_asn1, get_asn1,\
519
        ctrl, \
520
        NULL \
521
};\
522
const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
523
static const EVP_CIPHER cname##_cfb = {\
524
        nid##_cfb64, 1, key_len, iv_len, \
525
        flags | EVP_CIPH_CFB_MODE,\
526
        EVP_ORIG_GLOBAL,\
527
        init_key,\
528
        cname##_cfb_cipher,\
529
        cleanup,\
530
        sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
531
                sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
532
        set_asn1, get_asn1,\
533
        ctrl,\
534
        NULL \
535
};\
536
const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
537
static const EVP_CIPHER cname##_ofb = {\
538
        nid##_ofb64, 1, key_len, iv_len, \
539
        flags | EVP_CIPH_OFB_MODE,\
540
        EVP_ORIG_GLOBAL,\
541
        init_key,\
542
        cname##_ofb_cipher,\
543
        cleanup,\
544
        sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
545
                sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
546
        set_asn1, get_asn1,\
547
        ctrl,\
548
        NULL \
549
};\
550
const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
551
static const EVP_CIPHER cname##_ecb = {\
552
        nid##_ecb, block_size, key_len, iv_len, \
553
        flags | EVP_CIPH_ECB_MODE,\
554
        EVP_ORIG_GLOBAL,\
555
        init_key,\
556
        cname##_ecb_cipher,\
557
        cleanup,\
558
        sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
559
                sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
560
        set_asn1, get_asn1,\
561
        ctrl,\
562
        NULL \
563
};\
564
const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
565
*/
566
567
#define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid,        \
568
    block_size, key_len, iv_len, cbits,                                     \
569
    flags, init_key,                                                        \
570
    cleanup, set_asn1, get_asn1, ctrl)                                      \
571
    BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched)          \
572
        BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
573
            cbits, flags, init_key, cleanup, set_asn1,                      \
574
            get_asn1, ctrl)
575
576
#define IMPLEMENT_CFBR(cipher, cprefix, kstruct, ksched, keysize, cbits, iv_len, fl) \
577
    BLOCK_CIPHER_func_cfb(cipher##_##keysize, cprefix, cbits, kstruct, ksched)       \
578
        BLOCK_CIPHER_def_cfb(cipher##_##keysize, kstruct,                            \
579
            NID_##cipher##_##keysize, keysize / 8, iv_len, cbits,                    \
580
            (fl) | EVP_CIPH_FLAG_DEFAULT_ASN1,                                       \
581
            cipher##_init_key, NULL, NULL, NULL, NULL)
582
583
typedef struct {
584
    unsigned char iv[EVP_MAX_IV_LENGTH];
585
    unsigned int iv_len;
586
    unsigned int tag_len;
587
} evp_cipher_aead_asn1_params;
588
589
int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
590
    evp_cipher_aead_asn1_params *params);
591
592
int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
593
    evp_cipher_aead_asn1_params *params);
594
595
/*
596
 * To support transparent execution of operation in backends other
597
 * than the "origin" key, we support transparent export/import to
598
 * those providers, and maintain a cache of the imported keydata,
599
 * so we don't need to redo the export/import every time we perform
600
 * the same operation in that same provider.
601
 * This requires that the "origin" backend (whether it's a legacy or a
602
 * provider "origin") implements exports, and that the target provider
603
 * has an EVP_KEYMGMT that implements import.
604
 */
605
typedef struct {
606
    EVP_KEYMGMT *keymgmt;
607
    void *keydata;
608
    int selection;
609
} OP_CACHE_ELEM;
610
611
DEFINE_STACK_OF(OP_CACHE_ELEM)
612
613
/*
614
 * An EVP_PKEY can have the following states:
615
 *
616
 * untyped & empty:
617
 *
618
 *     type == EVP_PKEY_NONE && keymgmt == NULL
619
 *
620
 * typed & empty:
621
 *
622
 *     (type != EVP_PKEY_NONE && pkey.ptr == NULL)      ## legacy (libcrypto only)
623
 *     || (keymgmt != NULL && keydata == NULL)          ## provider side
624
 *
625
 * fully assigned:
626
 *
627
 *     (type != EVP_PKEY_NONE && pkey.ptr != NULL)      ## legacy (libcrypto only)
628
 *     || (keymgmt != NULL && keydata != NULL)          ## provider side
629
 *
630
 * The easiest way to detect a legacy key is:
631
 *
632
 *     keymgmt == NULL && type != EVP_PKEY_NONE
633
 *
634
 * The easiest way to detect a provider side key is:
635
 *
636
 *     keymgmt != NULL
637
 */
638
#define evp_pkey_is_blank(pk) \
639
207k
    ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL)
640
#define evp_pkey_is_typed(pk) \
641
0
    ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL)
642
#ifndef FIPS_MODULE
643
#define evp_pkey_is_assigned(pk) \
644
999k
    ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
645
#else
646
#define evp_pkey_is_assigned(pk) \
647
    ((pk)->keydata != NULL)
648
#endif
649
#define evp_pkey_is_legacy(pk) \
650
1.90M
    ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL)
651
#define evp_pkey_is_provided(pk) \
652
4.33M
    ((pk)->keymgmt != NULL)
653
654
union legacy_pkey_st {
655
    void *ptr;
656
    struct rsa_st *rsa; /* RSA */
657
#ifndef OPENSSL_NO_DSA
658
    struct dsa_st *dsa; /* DSA */
659
#endif
660
#ifndef OPENSSL_NO_DH
661
    struct dh_st *dh; /* DH */
662
#endif
663
#ifndef OPENSSL_NO_EC
664
    struct ec_key_st *ec; /* ECC */
665
#ifndef OPENSSL_NO_ECX
666
    ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
667
#endif
668
#endif
669
};
670
671
struct evp_pkey_st {
672
    /* == Legacy attributes == */
673
    int type;
674
    int save_type;
675
676
#ifndef FIPS_MODULE
677
    /*
678
     * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD,
679
     * a pointer to a low level key and possibly a pointer to an engine.
680
     */
681
    const EVP_PKEY_ASN1_METHOD *ameth;
682
    ENGINE *engine;
683
    ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
684
685
    /* Union to store the reference to an origin legacy key */
686
    union legacy_pkey_st pkey;
687
688
    /* Union to store the reference to a non-origin legacy key */
689
    union legacy_pkey_st legacy_cache_pkey;
690
#endif
691
692
    /* == Common attributes == */
693
    CRYPTO_REF_COUNT references;
694
    CRYPTO_RWLOCK *lock;
695
#ifndef FIPS_MODULE
696
    STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
697
    int save_parameters;
698
    unsigned int foreign : 1; /* the low-level key is using an engine or an app-method */
699
    CRYPTO_EX_DATA ex_data;
700
#endif
701
702
    /* == Provider attributes == */
703
704
    /*
705
     * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT
706
     * and a pointer to the provider side key data.  This is never used at
707
     * the same time as the legacy key data above.
708
     */
709
    EVP_KEYMGMT *keymgmt;
710
    void *keydata;
711
    /*
712
     * If any libcrypto code does anything that may modify the keydata
713
     * contents, this dirty counter must be incremented.
714
     */
715
    size_t dirty_cnt;
716
717
    /*
718
     * To support transparent execution of operation in backends other
719
     * than the "origin" key, we support transparent export/import to
720
     * those providers, and maintain a cache of the imported keydata,
721
     * so we don't need to redo the export/import every time we perform
722
     * the same operation in that same provider.
723
     */
724
    STACK_OF(OP_CACHE_ELEM) *operation_cache;
725
726
    /*
727
     * We keep a copy of that "origin"'s dirty count, so we know if the
728
     * operation cache needs flushing.
729
     */
730
    size_t dirty_cnt_copy;
731
732
    /* Cache of key object information */
733
    struct {
734
        int bits;
735
        int security_bits;
736
        int security_category;
737
        int size;
738
    } cache;
739
}; /* EVP_PKEY */
740
741
/* The EVP_PKEY_OP_TYPE_ macros are found in include/openssl/evp.h */
742
743
#define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \
744
2.10G
    (((ctx)->operation & EVP_PKEY_OP_TYPE_SIG) != 0)
745
746
#define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
747
6.41M
    (((ctx)->operation & EVP_PKEY_OP_TYPE_DERIVE) != 0)
748
749
#define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
750
5.01M
    (((ctx)->operation & EVP_PKEY_OP_TYPE_CRYPT) != 0)
751
752
#define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
753
5.02M
    (((ctx)->operation & EVP_PKEY_OP_TYPE_GEN) != 0)
754
755
#define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \
756
42.2k
    (((ctx)->operation & EVP_PKEY_OP_TYPE_DATA) != 0)
757
758
#define EVP_PKEY_CTX_IS_KEM_OP(ctx) \
759
3.98M
    (((ctx)->operation & EVP_PKEY_OP_TYPE_KEM) != 0)
760
761
struct evp_skey_st {
762
    /* == Common attributes == */
763
    CRYPTO_REF_COUNT references;
764
    CRYPTO_RWLOCK *lock;
765
766
    void *keydata; /* Alg-specific key data */
767
    EVP_SKEYMGMT *skeymgmt; /* Import, export, manage */
768
}; /* EVP_SKEY */
769
770
void openssl_add_all_ciphers_int(void);
771
void openssl_add_all_digests_int(void);
772
void evp_cleanup_int(void);
773
void evp_app_cleanup_int(void);
774
void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
775
    EVP_KEYMGMT **keymgmt,
776
    const char *propquery);
777
#ifndef FIPS_MODULE
778
int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src);
779
void *evp_pkey_get_legacy(EVP_PKEY *pk);
780
void evp_pkey_free_legacy(EVP_PKEY *x);
781
EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8inf,
782
    OSSL_LIB_CTX *libctx, const char *propq);
783
#endif
784
785
/*
786
 * KEYMGMT utility functions
787
 */
788
789
/*
790
 * Key import structure and helper function, to be used as an export callback
791
 */
792
struct evp_keymgmt_util_try_import_data_st {
793
    EVP_KEYMGMT *keymgmt;
794
    void *keydata;
795
796
    int selection;
797
};
798
int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg);
799
int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
800
    void *keydata);
801
EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata);
802
803
int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
804
    OSSL_CALLBACK *export_cb, void *export_cbarg);
805
void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
806
    int selection);
807
OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk,
808
    EVP_KEYMGMT *keymgmt,
809
    int selection);
810
int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk);
811
int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
812
    void *keydata, int selection);
813
void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk);
814
void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
815
    int selection, const OSSL_PARAM params[]);
816
int evp_keymgmt_util_has(EVP_PKEY *pk, int selection);
817
int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection);
818
int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection);
819
void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
820
    void *genctx, OSSL_CALLBACK *cb, void *cbarg);
821
int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
822
    void *keydata,
823
    char *mdname, size_t mdname_sz);
824
const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt,
825
    int op_id);
826
827
/*
828
 * KEYMGMT provider interface functions
829
 */
830
void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt);
831
void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata);
832
int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt,
833
    void *keydata, OSSL_PARAM params[]);
834
int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt,
835
    void *keydata, const OSSL_PARAM params[]);
836
void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
837
    const OSSL_PARAM params[]);
838
int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
839
    void *templ);
840
int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
841
    const OSSL_PARAM params[]);
842
int evp_keymgmt_gen_get_params(const EVP_KEYMGMT *keymgmt,
843
    void *genctx, OSSL_PARAM params[]);
844
void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
845
    OSSL_CALLBACK *cb, void *cbarg);
846
void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx);
847
848
int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt);
849
void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
850
    const void *objref, size_t objref_sz);
851
852
int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection);
853
int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
854
    int selection, int checktype);
855
int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
856
    const void *keydata1, const void *keydata2,
857
    int selection);
858
859
int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
860
    int selection, const OSSL_PARAM params[]);
861
const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
862
    int selection);
863
int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
864
    int selection, OSSL_CALLBACK *param_cb, void *cbarg);
865
const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
866
    int selection);
867
void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt,
868
    const void *keydata_from, int selection);
869
EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
870
    const char *name,
871
    const char *properties);
872
873
/*
874
 * SKEYMGMT provider interface functions
875
 */
876
EVP_SKEY *evp_skey_alloc(EVP_SKEYMGMT *skeymgmt);
877
void evp_skeymgmt_freedata(const EVP_SKEYMGMT *keymgmt, void *keyddata);
878
void *evp_skeymgmt_import(const EVP_SKEYMGMT *skeymgmt, int selection, const OSSL_PARAM params[]);
879
int evp_skeymgmt_export(const EVP_SKEYMGMT *skeymgmt, void *keydata,
880
    int selection, OSSL_CALLBACK *param_cb, void *cbarg);
881
void *evp_skeymgmt_generate(const EVP_SKEYMGMT *skeymgmt, const OSSL_PARAM params[]);
882
EVP_SKEYMGMT *evp_skeymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
883
    const char *name,
884
    const char *properties);
885
886
/* Pulling defines out of C source files */
887
888
#define EVP_RC4_KEY_SIZE 16
889
#ifndef TLS1_1_VERSION
890
9.31k
#define TLS1_1_VERSION 0x0302
891
#endif
892
893
void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
894
895
/* EVP_ENCODE_CTX flags */
896
/* Don't generate new lines when encoding */
897
573
#define EVP_ENCODE_CTX_NO_NEWLINES 1
898
/* Use the SRP base64 alphabet instead of the standard one */
899
3.34M
#define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2
900
901
const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,
902
    const char *name);
903
const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx,
904
    const char *name);
905
906
int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
907
    const unsigned char *salt, int saltlen, int iter,
908
    const EVP_MD *digest, int keylen,
909
    unsigned char *out,
910
    OSSL_LIB_CTX *libctx, const char *propq);
911
912
#ifndef FIPS_MODULE
913
/*
914
 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
915
 *
916
 * Return 1 on success, 0 or negative for errors.
917
 *
918
 * In particular they return -2 if any of the params is not supported.
919
 *
920
 * They are not available in FIPS_MODULE as they depend on
921
 *      - EVP_PKEY_CTX_{get,set}_params()
922
 *      - EVP_PKEY_CTX_{gettable,settable}_params()
923
 *
924
 */
925
int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
926
int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
927
928
EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
929
    OSSL_LIB_CTX *libctx, const char *propq);
930
int evp_pkey_name2type(const char *name);
931
const char *evp_pkey_type2name(int type);
932
933
int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx);
934
#endif /* !defined(FIPS_MODULE) */
935
936
int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx);
937
int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov);
938
939
int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
940
    int loadconfig);
941
int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
942
    int loadconfig, int mirrored);
943
char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig);
944
945
void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_digest);
946
/* just free the algctx if set, returns 0 on inconsistent state of ctx */
947
int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx);
948
949
/* Three possible states: */
950
0
#define EVP_PKEY_STATE_UNKNOWN 0
951
0
#define EVP_PKEY_STATE_LEGACY 1
952
950k
#define EVP_PKEY_STATE_PROVIDER 2
953
int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx);
954
955
/* These two must ONLY be called for provider side operations */
956
int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *ctx,
957
    int keytype, int optype,
958
    int cmd, int p1, void *p2);
959
int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *ctx,
960
    const char *name, const char *value);
961
962
/* These two must ONLY be called for legacy operations */
963
int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params);
964
int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
965
966
/* This must ONLY be called for legacy EVP_PKEYs */
967
int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params);
968
969
/* Same as the public get0 functions but are not const */
970
#ifndef OPENSSL_NO_DEPRECATED_3_0
971
DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey);
972
EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey);
973
RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey);
974
#endif
975
976
/* Get internal identification number routines */
977
int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher);
978
int evp_cipher_get_number(const EVP_CIPHER *cipher);
979
int evp_kdf_get_number(const EVP_KDF *kdf);
980
int evp_kem_get_number(const EVP_KEM *wrap);
981
int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch);
982
int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt);
983
int evp_keymgmt_get_legacy_alg(const EVP_KEYMGMT *keymgmt);
984
int evp_mac_get_number(const EVP_MAC *mac);
985
int evp_md_get_number(const EVP_MD *md);
986
int evp_rand_get_number(const EVP_RAND *rand);
987
int evp_rand_can_seed(EVP_RAND_CTX *ctx);
988
size_t evp_rand_get_seed(EVP_RAND_CTX *ctx,
989
    unsigned char **buffer,
990
    int entropy, size_t min_len, size_t max_len,
991
    int prediction_resistance,
992
    const unsigned char *adin, size_t adin_len);
993
void evp_rand_clear_seed(EVP_RAND_CTX *ctx,
994
    unsigned char *buffer, size_t b_len);
995
int evp_signature_get_number(const EVP_SIGNATURE *signature);
996
997
int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp,
998
    size_t *outlenp, size_t expected_outlen,
999
    const unsigned char *in, size_t inlen);
1000
1001
int ossl_md2hmacnid(int mdnid);
1002
int ossl_hmac2mdnid(int hmac_nid);
1003
1004
#endif /* OSSL_CRYPTO_EVP_H */