Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
16.4k
#define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
33
48.6k
#define EVP_MD_CTX_FLAG_FINALISED 0x0800
34
35
#define evp_pkey_ctx_is_legacy(ctx) \
36
0
    ((ctx)->keymgmt == NULL)
37
#define evp_pkey_ctx_is_provided(ctx) \
38
0
    (!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
    /* Key: may be NULL */
123
    EVP_PKEY *pkey;
124
    /* Peer key for key agreement, may be NULL */
125
    EVP_PKEY *peerkey;
126
    /* Algorithm specific data */
127
    void *data;
128
    /*
129
     * Used to support taking custody of memory in the case of a provider being
130
     * used with the deprecated EVP_PKEY_CTX_set_rsa_keygen_pubexp() API. This
131
     * member should NOT be used for any other purpose and should be removed
132
     * when said deprecated API is excised completely.
133
     */
134
    BIGNUM *rsa_pubexp;
135
} /* EVP_PKEY_CTX */;
136
137
#define EVP_PKEY_FLAG_DYNAMIC 1
138
139
void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
140
141
struct evp_mac_st {
142
    OSSL_PROVIDER *prov;
143
    int name_id;
144
    char *type_name;
145
    const char *description;
146
147
    CRYPTO_REF_COUNT refcnt;
148
149
    OSSL_FUNC_mac_newctx_fn *newctx;
150
    OSSL_FUNC_mac_dupctx_fn *dupctx;
151
    OSSL_FUNC_mac_freectx_fn *freectx;
152
    OSSL_FUNC_mac_init_fn *init;
153
    OSSL_FUNC_mac_update_fn *update;
154
    OSSL_FUNC_mac_final_fn *final;
155
    OSSL_FUNC_mac_gettable_params_fn *gettable_params;
156
    OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params;
157
    OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params;
158
    OSSL_FUNC_mac_get_params_fn *get_params;
159
    OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params;
160
    OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params;
161
    OSSL_FUNC_mac_init_skey_fn *init_skey;
162
};
163
164
struct evp_kdf_st {
165
    OSSL_PROVIDER *prov;
166
    int name_id;
167
    char *type_name;
168
    const char *description;
169
    CRYPTO_REF_COUNT refcnt;
170
171
    OSSL_FUNC_kdf_newctx_fn *newctx;
172
    OSSL_FUNC_kdf_dupctx_fn *dupctx;
173
    OSSL_FUNC_kdf_freectx_fn *freectx;
174
    OSSL_FUNC_kdf_reset_fn *reset;
175
    OSSL_FUNC_kdf_derive_fn *derive;
176
    OSSL_FUNC_kdf_gettable_params_fn *gettable_params;
177
    OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params;
178
    OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params;
179
    OSSL_FUNC_kdf_get_params_fn *get_params;
180
    OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params;
181
    OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params;
182
    OSSL_FUNC_kdf_set_skey_fn *set_skey;
183
    OSSL_FUNC_kdf_derive_skey_fn *derive_skey;
184
};
185
186
1.82M
#define EVP_ORIG_DYNAMIC 0
187
18
#define EVP_ORIG_GLOBAL 1
188
189
struct evp_md_st {
190
    /* nid */
191
    int type;
192
193
    int pkey_type;
194
    int md_size;
195
    unsigned long flags;
196
    int origin;
197
    int block_size;
198
199
    int name_id;
200
    char *type_name;
201
    const char *description;
202
    OSSL_PROVIDER *prov;
203
    CRYPTO_REF_COUNT refcnt;
204
    OSSL_FUNC_digest_newctx_fn *newctx;
205
    OSSL_FUNC_digest_init_fn *dinit;
206
    OSSL_FUNC_digest_update_fn *dupdate;
207
    OSSL_FUNC_digest_final_fn *dfinal;
208
    OSSL_FUNC_digest_squeeze_fn *dsqueeze;
209
    OSSL_FUNC_digest_digest_fn *digest;
210
    OSSL_FUNC_digest_freectx_fn *freectx;
211
    OSSL_FUNC_digest_copyctx_fn *copyctx;
212
    OSSL_FUNC_digest_dupctx_fn *dupctx;
213
    OSSL_FUNC_digest_get_params_fn *get_params;
214
    OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params;
215
    OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params;
216
    OSSL_FUNC_digest_gettable_params_fn *gettable_params;
217
    OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params;
218
    OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params;
219
    OSSL_FUNC_digest_serialize_fn *serialize;
220
    OSSL_FUNC_digest_deserialize_fn *deserialize;
221
} /* EVP_MD */;
222
223
struct evp_cipher_st {
224
    int nid;
225
226
    int block_size;
227
    /* Default value for variable length ciphers */
228
    int key_len;
229
    int iv_len;
230
231
    /* Various flags */
232
    unsigned long flags;
233
    /* How the EVP_CIPHER was created. */
234
    int origin;
235
236
    int name_id;
237
    char *type_name;
238
    const char *description;
239
    OSSL_PROVIDER *prov;
240
    CRYPTO_REF_COUNT refcnt;
241
    OSSL_FUNC_cipher_newctx_fn *newctx;
242
    OSSL_FUNC_cipher_encrypt_init_fn *einit;
243
    OSSL_FUNC_cipher_decrypt_init_fn *dinit;
244
    OSSL_FUNC_cipher_update_fn *cupdate;
245
    OSSL_FUNC_cipher_final_fn *cfinal;
246
    OSSL_FUNC_cipher_cipher_fn *ccipher;
247
    OSSL_FUNC_cipher_pipeline_encrypt_init_fn *p_einit;
248
    OSSL_FUNC_cipher_pipeline_decrypt_init_fn *p_dinit;
249
    OSSL_FUNC_cipher_pipeline_update_fn *p_cupdate;
250
    OSSL_FUNC_cipher_pipeline_final_fn *p_cfinal;
251
    OSSL_FUNC_cipher_freectx_fn *freectx;
252
    OSSL_FUNC_cipher_dupctx_fn *dupctx;
253
    OSSL_FUNC_cipher_get_params_fn *get_params;
254
    OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params;
255
    OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params;
256
    OSSL_FUNC_cipher_gettable_params_fn *gettable_params;
257
    OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params;
258
    OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params;
259
    OSSL_FUNC_cipher_encrypt_skey_init_fn *einit_skey;
260
    OSSL_FUNC_cipher_decrypt_skey_init_fn *dinit_skey;
261
} /* EVP_CIPHER */;
262
263
0
#define EVP_MAXCHUNK ((size_t)1 << 30)
264
265
#define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
266
    key_len, iv_len, flags)                                                   \
267
    static const EVP_CIPHER cname##_##mode = {                                \
268
        nid##_##nmode, block_size, key_len, iv_len,                           \
269
        flags | EVP_CIPH_##MODE##_MODE,                                       \
270
        EVP_ORIG_GLOBAL                                                       \
271
    };                                                                        \
272
156
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb128
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb128
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb128
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb1
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb1
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb1
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb8
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb8
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb8
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_cfb64
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_cfb64
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb64
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb1
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb8
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_cfb64
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
Unexecuted instantiation: EVP_des_ede_ecb
EVP_des_ede3_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_cfb64
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
Unexecuted instantiation: EVP_des_ede3_ecb
EVP_des_ede3_cfb1
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_cfb8
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_cfb64
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_cfb64
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_cbc
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_cfb128
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_ofb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_ecb
Line
Count
Source
272
3
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
273
274
#define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len,         \
275
    iv_len, flags)                                                             \
276
    BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
277
        iv_len, flags)
278
279
#define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len,                 \
280
    iv_len, cbits, flags)                                                  \
281
    BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
282
        key_len, iv_len, flags)
283
284
#define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len,          \
285
    iv_len, cbits, flags)                                           \
286
    BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
287
        key_len, iv_len, flags)
288
289
#define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len,         \
290
    flags)                                                                     \
291
    BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
292
        0, flags)
293
294
#define BLOCK_CIPHER_defs(cname, kstruct,                                         \
295
    nid, block_size, key_len, iv_len, cbits, flags)                               \
296
    BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags) \
297
    BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits,             \
298
        flags)                                                                    \
299
    BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits,             \
300
        flags)                                                                    \
301
    BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags)
302
303
#define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid,    \
304
    block_size, key_len, iv_len, cbits,                                 \
305
    flags)                                                              \
306
    BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
307
        cbits, flags)
308
309
#define IMPLEMENT_CFBR(cipher, cprefix, kstruct, ksched, keysize, cbits, iv_len, fl) \
310
    BLOCK_CIPHER_def_cfb(cipher##_##keysize, kstruct,                                \
311
        NID_##cipher##_##keysize, keysize / 8, iv_len, cbits,                        \
312
        (fl) | EVP_CIPH_FLAG_DEFAULT_ASN1)
313
314
typedef struct {
315
    unsigned char iv[EVP_MAX_IV_LENGTH];
316
    unsigned int iv_len;
317
    unsigned int tag_len;
318
} evp_cipher_aead_asn1_params;
319
320
int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
321
    evp_cipher_aead_asn1_params *params);
322
323
int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
324
    evp_cipher_aead_asn1_params *params);
325
326
/*
327
 * To support transparent execution of operation in backends other
328
 * than the "origin" key, we support transparent export/import to
329
 * those providers, and maintain a cache of the imported keydata,
330
 * so we don't need to redo the export/import every time we perform
331
 * the same operation in that same provider.
332
 * This requires that the "origin" backend (whether it's a legacy or a
333
 * provider "origin") implements exports, and that the target provider
334
 * has an EVP_KEYMGMT that implements import.
335
 */
336
typedef struct {
337
    EVP_KEYMGMT *keymgmt;
338
    void *keydata;
339
    int selection;
340
} OP_CACHE_ELEM;
341
342
DEFINE_STACK_OF(OP_CACHE_ELEM)
343
344
/*
345
 * An EVP_PKEY can have the following states:
346
 *
347
 * untyped & empty:
348
 *
349
 *     type == EVP_PKEY_NONE && keymgmt == NULL
350
 *
351
 * typed & empty:
352
 *
353
 *     (type != EVP_PKEY_NONE && pkey.ptr == NULL)      ## legacy (libcrypto only)
354
 *     || (keymgmt != NULL && keydata == NULL)          ## provider side
355
 *
356
 * fully assigned:
357
 *
358
 *     (type != EVP_PKEY_NONE && pkey.ptr != NULL)      ## legacy (libcrypto only)
359
 *     || (keymgmt != NULL && keydata != NULL)          ## provider side
360
 *
361
 * The easiest way to detect a legacy key is:
362
 *
363
 *     keymgmt == NULL && type != EVP_PKEY_NONE
364
 *
365
 * The easiest way to detect a provider side key is:
366
 *
367
 *     keymgmt != NULL
368
 */
369
#define evp_pkey_is_blank(pk) \
370
0
    ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL)
371
#define evp_pkey_is_typed(pk) \
372
0
    ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL)
373
#ifndef FIPS_MODULE
374
#define evp_pkey_is_assigned(pk) \
375
0
    ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
376
#else
377
#define evp_pkey_is_assigned(pk) \
378
    ((pk)->keydata != NULL)
379
#endif
380
#define evp_pkey_is_legacy(pk) \
381
0
    ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL)
382
#define evp_pkey_is_provided(pk) \
383
0
    ((pk)->keymgmt != NULL)
384
385
union legacy_pkey_st {
386
    void *ptr;
387
    struct rsa_st *rsa; /* RSA */
388
#ifndef OPENSSL_NO_DSA
389
    struct dsa_st *dsa; /* DSA */
390
#endif
391
#ifndef OPENSSL_NO_DH
392
    struct dh_st *dh; /* DH */
393
#endif
394
#ifndef OPENSSL_NO_EC
395
    struct ec_key_st *ec; /* ECC */
396
#ifndef OPENSSL_NO_ECX
397
    ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
398
#endif
399
#endif
400
};
401
402
struct evp_pkey_st {
403
    /* == Legacy attributes == */
404
    int type;
405
    int save_type;
406
407
#ifndef FIPS_MODULE
408
    /*
409
     * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD,
410
     * a pointer to a low level key and possibly a pointer to an engine.
411
     */
412
    const EVP_PKEY_ASN1_METHOD *ameth;
413
414
    /* Union to store the reference to an origin legacy key */
415
    union legacy_pkey_st pkey;
416
417
    /* Union to store the reference to a non-origin legacy key */
418
    union legacy_pkey_st legacy_cache_pkey;
419
#endif
420
421
    /* == Common attributes == */
422
    CRYPTO_REF_COUNT references;
423
    CRYPTO_RWLOCK *lock;
424
#ifndef FIPS_MODULE
425
    STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
426
    int save_parameters;
427
    unsigned int foreign : 1; /* the low-level key is using an engine or an app-method */
428
    CRYPTO_EX_DATA ex_data;
429
#endif
430
431
    /* == Provider attributes == */
432
433
    /*
434
     * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT
435
     * and a pointer to the provider side key data.  This is never used at
436
     * the same time as the legacy key data above.
437
     */
438
    EVP_KEYMGMT *keymgmt;
439
    void *keydata;
440
    /*
441
     * If any libcrypto code does anything that may modify the keydata
442
     * contents, this dirty counter must be incremented.
443
     */
444
    size_t dirty_cnt;
445
446
    /*
447
     * To support transparent execution of operation in backends other
448
     * than the "origin" key, we support transparent export/import to
449
     * those providers, and maintain a cache of the imported keydata,
450
     * so we don't need to redo the export/import every time we perform
451
     * the same operation in that same provider.
452
     */
453
    STACK_OF(OP_CACHE_ELEM) *operation_cache;
454
455
    /*
456
     * We keep a copy of that "origin"'s dirty count, so we know if the
457
     * operation cache needs flushing.
458
     */
459
    size_t dirty_cnt_copy;
460
461
    /* Cache of key object information */
462
    struct {
463
        int bits;
464
        int security_bits;
465
        int security_category;
466
        int size;
467
    } cache;
468
}; /* EVP_PKEY */
469
470
/* The EVP_PKEY_OP_TYPE_ macros are found in include/openssl/evp.h */
471
472
#define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \
473
163M
    (((ctx)->operation & EVP_PKEY_OP_TYPE_SIG) != 0)
474
475
#define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
476
0
    (((ctx)->operation & EVP_PKEY_OP_TYPE_DERIVE) != 0)
477
478
#define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
479
0
    (((ctx)->operation & EVP_PKEY_OP_TYPE_CRYPT) != 0)
480
481
#define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
482
0
    (((ctx)->operation & EVP_PKEY_OP_TYPE_GEN) != 0)
483
484
#define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \
485
0
    (((ctx)->operation & EVP_PKEY_OP_TYPE_DATA) != 0)
486
487
#define EVP_PKEY_CTX_IS_KEM_OP(ctx) \
488
0
    (((ctx)->operation & EVP_PKEY_OP_TYPE_KEM) != 0)
489
490
struct evp_skey_st {
491
    /* == Common attributes == */
492
    CRYPTO_REF_COUNT references;
493
    CRYPTO_RWLOCK *lock;
494
495
    void *keydata; /* Alg-specific key data */
496
    EVP_SKEYMGMT *skeymgmt; /* Import, export, manage */
497
}; /* EVP_SKEY */
498
499
void openssl_add_all_ciphers_int(void);
500
void openssl_add_all_digests_int(void);
501
void evp_cleanup_int(void);
502
void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
503
    EVP_KEYMGMT **keymgmt,
504
    const char *propquery);
505
#ifndef FIPS_MODULE
506
int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src);
507
void *evp_pkey_get_legacy(EVP_PKEY *pk);
508
void evp_pkey_free_legacy(EVP_PKEY *x);
509
EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8inf,
510
    OSSL_LIB_CTX *libctx, const char *propq);
511
#endif
512
513
/*
514
 * KEYMGMT utility functions
515
 */
516
517
/*
518
 * Key import structure and helper function, to be used as an export callback
519
 */
520
struct evp_keymgmt_util_try_import_data_st {
521
    EVP_KEYMGMT *keymgmt;
522
    void *keydata;
523
524
    int selection;
525
};
526
int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg);
527
int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
528
    void *keydata);
529
EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata);
530
531
int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
532
    OSSL_CALLBACK *export_cb, void *export_cbarg);
533
void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
534
    int selection);
535
OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk,
536
    EVP_KEYMGMT *keymgmt,
537
    int selection);
538
void evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk);
539
int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
540
    void *keydata, int selection);
541
void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk);
542
void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
543
    int selection, const OSSL_PARAM params[]);
544
int evp_keymgmt_util_has(EVP_PKEY *pk, int selection);
545
int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection);
546
int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection);
547
void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
548
    void *genctx, OSSL_CALLBACK *cb, void *cbarg);
549
int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
550
    void *keydata,
551
    char *mdname, size_t mdname_sz);
552
const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt,
553
    int op_id);
554
555
/*
556
 * KEYMGMT provider interface functions
557
 */
558
void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt, const OSSL_PARAM params[]);
559
void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata);
560
int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt,
561
    void *keydata, OSSL_PARAM params[]);
562
int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt,
563
    void *keydata, const OSSL_PARAM params[]);
564
void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
565
    const OSSL_PARAM params[]);
566
int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
567
    void *templ);
568
int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
569
    const OSSL_PARAM params[]);
570
int evp_keymgmt_gen_get_params(const EVP_KEYMGMT *keymgmt,
571
    void *genctx, OSSL_PARAM params[]);
572
void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
573
    OSSL_CALLBACK *cb, void *cbarg);
574
void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx);
575
576
int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt);
577
void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
578
    const void *objref, size_t objref_sz);
579
580
int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection);
581
int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
582
    int selection, int checktype);
583
int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
584
    const void *keydata1, const void *keydata2,
585
    int selection);
586
587
int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
588
    int selection, const OSSL_PARAM params[]);
589
const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
590
    int selection);
591
int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
592
    int selection, OSSL_CALLBACK *param_cb, void *cbarg);
593
const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
594
    int selection);
595
void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt,
596
    const void *keydata_from, int selection);
597
EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
598
    const char *name,
599
    const char *properties);
600
601
/*
602
 * SKEYMGMT provider interface functions
603
 */
604
EVP_SKEY *evp_skey_alloc(EVP_SKEYMGMT *skeymgmt);
605
void evp_skeymgmt_freedata(const EVP_SKEYMGMT *keymgmt, void *keyddata);
606
void *evp_skeymgmt_import(const EVP_SKEYMGMT *skeymgmt, int selection, const OSSL_PARAM params[]);
607
int evp_skeymgmt_export(const EVP_SKEYMGMT *skeymgmt, void *keydata,
608
    int selection, OSSL_CALLBACK *param_cb, void *cbarg);
609
void *evp_skeymgmt_generate(const EVP_SKEYMGMT *skeymgmt, const OSSL_PARAM params[]);
610
EVP_SKEYMGMT *evp_skeymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
611
    const char *name,
612
    const char *properties);
613
614
/* Pulling defines out of C source files */
615
616
#define EVP_RC4_KEY_SIZE 16
617
#ifndef TLS1_1_VERSION
618
#define TLS1_1_VERSION 0x0302
619
#endif
620
621
void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
622
623
/* EVP_ENCODE_CTX flags */
624
/* Don't generate new lines when encoding */
625
0
#define EVP_ENCODE_CTX_NO_NEWLINES 1
626
/* Use the SRP base64 alphabet instead of the standard one */
627
0
#define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2
628
629
const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,
630
    const char *name);
631
const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx,
632
    const char *name);
633
634
int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
635
    const unsigned char *salt, int saltlen, int iter,
636
    const EVP_MD *digest, int keylen,
637
    unsigned char *out,
638
    OSSL_LIB_CTX *libctx, const char *propq);
639
640
#ifndef FIPS_MODULE
641
/*
642
 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
643
 *
644
 * Return 1 on success, 0 or negative for errors.
645
 *
646
 * In particular they return -2 if any of the params is not supported.
647
 *
648
 * They are not available in FIPS_MODULE as they depend on
649
 *      - EVP_PKEY_CTX_{get,set}_params()
650
 *      - EVP_PKEY_CTX_{gettable,settable}_params()
651
 *
652
 */
653
int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
654
int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
655
656
EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
657
    OSSL_LIB_CTX *libctx, const char *propq);
658
int evp_pkey_name2type(const char *name);
659
const char *evp_pkey_type2name(int type);
660
661
int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx);
662
#endif /* !defined(FIPS_MODULE) */
663
664
int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx);
665
int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov);
666
667
int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
668
    int loadconfig);
669
int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
670
    int loadconfig, int mirrored);
671
char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig);
672
673
void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_digest);
674
/* just free the algctx if set, returns 0 on inconsistent state of ctx */
675
int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx);
676
677
/* Three possible states: */
678
0
#define EVP_PKEY_STATE_UNKNOWN 0
679
0
#define EVP_PKEY_STATE_LEGACY 1
680
0
#define EVP_PKEY_STATE_PROVIDER 2
681
int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx);
682
683
/* These two must ONLY be called for provider side operations */
684
int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *ctx,
685
    int keytype, int optype,
686
    int cmd, int p1, void *p2);
687
int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *ctx,
688
    const char *name, const char *value);
689
690
/* These two must ONLY be called for legacy operations */
691
int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params);
692
int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
693
694
/* This must ONLY be called for legacy EVP_PKEYs */
695
int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params);
696
697
/* Same as the public get0 functions but are not const */
698
#ifndef OPENSSL_NO_DEPRECATED_3_0
699
DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey);
700
EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey);
701
RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey);
702
#endif
703
704
/* Get internal identification number routines */
705
int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher);
706
int evp_cipher_get_number(const EVP_CIPHER *cipher);
707
int evp_kdf_get_number(const EVP_KDF *kdf);
708
int evp_kem_get_number(const EVP_KEM *wrap);
709
int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch);
710
int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt);
711
int evp_keymgmt_get_legacy_alg(const EVP_KEYMGMT *keymgmt);
712
int evp_mac_get_number(const EVP_MAC *mac);
713
int evp_md_get_number(const EVP_MD *md);
714
int evp_rand_get_number(const EVP_RAND *rand);
715
int evp_rand_can_seed(EVP_RAND_CTX *ctx);
716
size_t evp_rand_get_seed(EVP_RAND_CTX *ctx,
717
    unsigned char **buffer,
718
    int entropy, size_t min_len, size_t max_len,
719
    int prediction_resistance,
720
    const unsigned char *adin, size_t adin_len);
721
void evp_rand_clear_seed(EVP_RAND_CTX *ctx,
722
    unsigned char *buffer, size_t b_len);
723
int evp_signature_get_number(const EVP_SIGNATURE *signature);
724
725
int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp,
726
    size_t *outlenp, size_t expected_outlen,
727
    const unsigned char *in, size_t inlen);
728
729
int ossl_md2hmacnid(int mdnid);
730
int ossl_hmac2mdnid(int hmac_nid);
731
732
const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int type);
733
const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find_str(const char *str, int len);
734
int evp_pkey_asn1_get_count(void);
735
const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_get0(int idx);
736
int evp_pkey_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
737
    int *ppkey_flags, const char **pinfo,
738
    const char **ppem_str,
739
    const EVP_PKEY_ASN1_METHOD *ameth);
740
const EVP_PKEY_ASN1_METHOD *evp_pkey_get0_asn1(const EVP_PKEY *pkey);
741
742
#endif /* OSSL_CRYPTO_EVP_H */