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