]>
Commit | Line | Data |
---|---|---|
27af42f9 | 1 | /* |
a28d06f3 | 2 | * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. |
27af42f9 | 3 | * |
48f4ad77 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
27af42f9 DSH |
8 | */ |
9 | ||
80ce21fe F |
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" | |
2f545ae4 | 18 | |
4803717f PY |
19 | /* |
20 | * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag | |
21 | * values in evp.h | |
22 | */ | |
23 | #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400 | |
24 | ||
f21c9c64 | 25 | /* |
6179dfc7 | 26 | * An EVP_PKEY_CTX can have the following support states: |
f21c9c64 RL |
27 | * |
28 | * Supports legacy implementations only: | |
29 | * | |
30 | * engine != NULL || keytype == NULL | |
31 | * | |
32 | * Supports provided implementations: | |
33 | * | |
34 | * engine == NULL && keytype != NULL | |
35 | */ | |
36 | #define evp_pkey_ctx_is_legacy(ctx) \ | |
37 | ((ctx)->engine != NULL || (ctx)->keytype == NULL) | |
38 | #define evp_pkey_ctx_is_provided(ctx) \ | |
39 | (!evp_pkey_ctx_is_legacy(ctx)) | |
40 | ||
27af42f9 | 41 | struct evp_pkey_ctx_st { |
864b89ce MC |
42 | /* Actual operation */ |
43 | int operation; | |
44 | ||
3ee348b0 | 45 | /* |
4b9e90f4 RL |
46 | * Library context, property query, keytype and keymgmt associated with |
47 | * this context | |
3ee348b0 | 48 | */ |
b4250010 | 49 | OSSL_LIB_CTX *libctx; |
ddfd7182 | 50 | char *propquery; |
4b9e90f4 | 51 | const char *keytype; |
c0e0984f RL |
52 | EVP_KEYMGMT *keymgmt; |
53 | ||
864b89ce | 54 | union { |
62924755 RL |
55 | struct { |
56 | void *genctx; | |
57 | } keymgmt; | |
58 | ||
864b89ce MC |
59 | struct { |
60 | EVP_KEYEXCH *exchange; | |
7c14d0c1 SL |
61 | /* |
62 | * Opaque ctx returned from a providers exchange algorithm | |
63 | * implementation OSSL_FUNC_keyexch_newctx() | |
64 | */ | |
65 | void *algctx; | |
864b89ce | 66 | } kex; |
ff64702b | 67 | |
864b89ce MC |
68 | struct { |
69 | EVP_SIGNATURE *signature; | |
7c14d0c1 SL |
70 | /* |
71 | * Opaque ctx returned from a providers signature algorithm | |
72 | * implementation OSSL_FUNC_signature_newctx() | |
73 | */ | |
74 | void *algctx; | |
864b89ce | 75 | } sig; |
2c938e2e MC |
76 | |
77 | struct { | |
78 | EVP_ASYM_CIPHER *cipher; | |
7c14d0c1 SL |
79 | /* |
80 | * Opaque ctx returned from a providers asymmetric cipher algorithm | |
81 | * implementation OSSL_FUNC_asym_cipher_newctx() | |
82 | */ | |
83 | void *algctx; | |
2c938e2e | 84 | } ciph; |
80f4fd18 SL |
85 | struct { |
86 | EVP_KEM *kem; | |
7c14d0c1 SL |
87 | /* |
88 | * Opaque ctx returned from a providers KEM algorithm | |
89 | * implementation OSSL_FUNC_kem_newctx() | |
90 | */ | |
91 | void *algctx; | |
80f4fd18 | 92 | } encap; |
864b89ce | 93 | } op; |
dfcb5d29 | 94 | |
86df26b3 RL |
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 | ||
62924755 RL |
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 | ||
ff64702b MC |
118 | /* Legacy fields below */ |
119 | ||
50914496 RL |
120 | /* EVP_PKEY identity */ |
121 | int legacy_keytype; | |
27af42f9 DSH |
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; | |
27af42f9 DSH |
130 | /* Algorithm specific data */ |
131 | void *data; | |
a5ce329e RL |
132 | /* Indicator if digest_custom needs to be called */ |
133 | unsigned int flag_call_digest_custom:1; | |
3786d748 | 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; | |
27af42f9 DSH |
141 | } /* EVP_PKEY_CTX */ ; |
142 | ||
143 | #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); | |
9fdcc21f | 149 | int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src); |
27af42f9 DSH |
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); | |
f723c98e DSH |
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); | |
2aee35d3 | 187 | int (*check) (EVP_PKEY *pkey); |
b0004708 PY |
188 | int (*public_check) (EVP_PKEY *pkey); |
189 | int (*param_check) (EVP_PKEY *pkey); | |
0a8fdef7 PY |
190 | |
191 | int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); | |
27af42f9 DSH |
192 | } /* EVP_PKEY_METHOD */ ; |
193 | ||
a8eba56e | 194 | DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD) |
4a1f3f27 | 195 | |
27af42f9 | 196 | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); |
8f463dbd | 197 | |
19dbb742 SL |
198 | const EVP_PKEY_METHOD *ossl_dh_pkey_method(void); |
199 | const EVP_PKEY_METHOD *ossl_dhx_pkey_method(void); | |
5af02212 | 200 | const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void); |
32ab57cb SL |
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); | |
23b2fc0b P |
206 | const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void); |
207 | const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void); | |
2db6bf6f | 208 | |
567db2c1 | 209 | struct evp_mac_st { |
e74bd290 | 210 | OSSL_PROVIDER *prov; |
f7c16d48 | 211 | int name_id; |
6c9bc258 | 212 | char *type_name; |
309a78aa | 213 | const char *description; |
e74bd290 RL |
214 | |
215 | CRYPTO_REF_COUNT refcnt; | |
216 | CRYPTO_RWLOCK *lock; | |
217 | ||
363b1e5d DMSP |
218 | OSSL_FUNC_mac_newctx_fn *newctx; |
219 | OSSL_FUNC_mac_dupctx_fn *dupctx; | |
220 | OSSL_FUNC_mac_freectx_fn *freectx; | |
363b1e5d DMSP |
221 | OSSL_FUNC_mac_init_fn *init; |
222 | OSSL_FUNC_mac_update_fn *update; | |
223 | OSSL_FUNC_mac_final_fn *final; | |
224 | OSSL_FUNC_mac_gettable_params_fn *gettable_params; | |
225 | OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params; | |
226 | OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params; | |
227 | OSSL_FUNC_mac_get_params_fn *get_params; | |
228 | OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params; | |
229 | OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params; | |
567db2c1 RL |
230 | }; |
231 | ||
d2ba8123 | 232 | struct evp_kdf_st { |
fb9e6dd6 | 233 | OSSL_PROVIDER *prov; |
f7c16d48 | 234 | int name_id; |
6c9bc258 | 235 | char *type_name; |
309a78aa | 236 | const char *description; |
fb9e6dd6 P |
237 | CRYPTO_REF_COUNT refcnt; |
238 | CRYPTO_RWLOCK *lock; | |
239 | ||
363b1e5d DMSP |
240 | OSSL_FUNC_kdf_newctx_fn *newctx; |
241 | OSSL_FUNC_kdf_dupctx_fn *dupctx; | |
242 | OSSL_FUNC_kdf_freectx_fn *freectx; | |
243 | OSSL_FUNC_kdf_reset_fn *reset; | |
244 | OSSL_FUNC_kdf_derive_fn *derive; | |
245 | OSSL_FUNC_kdf_gettable_params_fn *gettable_params; | |
246 | OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params; | |
247 | OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params; | |
248 | OSSL_FUNC_kdf_get_params_fn *get_params; | |
249 | OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params; | |
250 | OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params; | |
d2ba8123 | 251 | }; |
5a285add | 252 | |
f6c95e46 RS |
253 | #define EVP_ORIG_DYNAMIC 0 |
254 | #define EVP_ORIG_GLOBAL 1 | |
255 | #define EVP_ORIG_METH 2 | |
256 | ||
2db6bf6f | 257 | struct evp_md_st { |
3653d0c2 | 258 | /* nid */ |
2db6bf6f | 259 | int type; |
3653d0c2 MC |
260 | |
261 | /* Legacy structure members */ | |
2db6bf6f RL |
262 | int pkey_type; |
263 | int md_size; | |
264 | unsigned long flags; | |
f6c95e46 | 265 | int origin; |
2db6bf6f RL |
266 | int (*init) (EVP_MD_CTX *ctx); |
267 | int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count); | |
268 | int (*final) (EVP_MD_CTX *ctx, unsigned char *md); | |
269 | int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from); | |
270 | int (*cleanup) (EVP_MD_CTX *ctx); | |
271 | int block_size; | |
272 | int ctx_size; /* how big does the ctx->md_data need to be */ | |
273 | /* control function */ | |
274 | int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2); | |
3653d0c2 MC |
275 | |
276 | /* New structure members */ | |
946bdd12 | 277 | /* Above comment to be removed when legacy has gone */ |
f7c16d48 | 278 | int name_id; |
6c9bc258 | 279 | char *type_name; |
309a78aa | 280 | const char *description; |
3653d0c2 MC |
281 | OSSL_PROVIDER *prov; |
282 | CRYPTO_REF_COUNT refcnt; | |
283 | CRYPTO_RWLOCK *lock; | |
363b1e5d DMSP |
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_digest_fn *digest; | |
289 | OSSL_FUNC_digest_freectx_fn *freectx; | |
290 | OSSL_FUNC_digest_dupctx_fn *dupctx; | |
291 | OSSL_FUNC_digest_get_params_fn *get_params; | |
292 | OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params; | |
293 | OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params; | |
294 | OSSL_FUNC_digest_gettable_params_fn *gettable_params; | |
295 | OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params; | |
296 | OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params; | |
3653d0c2 | 297 | |
2db6bf6f RL |
298 | } /* EVP_MD */ ; |
299 | ||
e79f8773 RL |
300 | struct evp_cipher_st { |
301 | int nid; | |
df05f2ce | 302 | |
e79f8773 RL |
303 | int block_size; |
304 | /* Default value for variable length ciphers */ | |
305 | int key_len; | |
306 | int iv_len; | |
df05f2ce MC |
307 | |
308 | /* Legacy structure members */ | |
e79f8773 RL |
309 | /* Various flags */ |
310 | unsigned long flags; | |
f6c95e46 RS |
311 | /* How the EVP_CIPHER was created. */ |
312 | int origin; | |
e79f8773 RL |
313 | /* init key */ |
314 | int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key, | |
315 | const unsigned char *iv, int enc); | |
316 | /* encrypt/decrypt data */ | |
317 | int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out, | |
318 | const unsigned char *in, size_t inl); | |
319 | /* cleanup ctx */ | |
320 | int (*cleanup) (EVP_CIPHER_CTX *); | |
321 | /* how big ctx->cipher_data needs to be */ | |
322 | int ctx_size; | |
323 | /* Populate a ASN1_TYPE with parameters */ | |
324 | int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); | |
325 | /* Get parameters from a ASN1_TYPE */ | |
326 | int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *); | |
327 | /* Miscellaneous operations */ | |
328 | int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr); | |
329 | /* Application data */ | |
330 | void *app_data; | |
df05f2ce MC |
331 | |
332 | /* New structure members */ | |
946bdd12 | 333 | /* Above comment to be removed when legacy has gone */ |
f7c16d48 | 334 | int name_id; |
6c9bc258 | 335 | char *type_name; |
309a78aa | 336 | const char *description; |
df05f2ce MC |
337 | OSSL_PROVIDER *prov; |
338 | CRYPTO_REF_COUNT refcnt; | |
339 | CRYPTO_RWLOCK *lock; | |
363b1e5d DMSP |
340 | OSSL_FUNC_cipher_newctx_fn *newctx; |
341 | OSSL_FUNC_cipher_encrypt_init_fn *einit; | |
342 | OSSL_FUNC_cipher_decrypt_init_fn *dinit; | |
343 | OSSL_FUNC_cipher_update_fn *cupdate; | |
344 | OSSL_FUNC_cipher_final_fn *cfinal; | |
345 | OSSL_FUNC_cipher_cipher_fn *ccipher; | |
346 | OSSL_FUNC_cipher_freectx_fn *freectx; | |
347 | OSSL_FUNC_cipher_dupctx_fn *dupctx; | |
348 | OSSL_FUNC_cipher_get_params_fn *get_params; | |
349 | OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params; | |
350 | OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params; | |
351 | OSSL_FUNC_cipher_gettable_params_fn *gettable_params; | |
352 | OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params; | |
353 | OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params; | |
e79f8773 RL |
354 | } /* EVP_CIPHER */ ; |
355 | ||
356 | /* Macros to code block cipher wrappers */ | |
357 | ||
358 | /* Wrapper functions for each cipher mode */ | |
359 | ||
44ab2dfd MC |
360 | #define EVP_C_DATA(kstruct, ctx) \ |
361 | ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx)) | |
e79f8773 RL |
362 | |
363 | #define BLOCK_CIPHER_ecb_loop() \ | |
364 | size_t i, bl; \ | |
f6c95e46 | 365 | bl = EVP_CIPHER_CTX_get0_cipher(ctx)->block_size; \ |
e8aa8b6c | 366 | if (inl < bl) return 1;\ |
e79f8773 | 367 | inl -= bl; \ |
e8aa8b6c | 368 | for (i=0; i <= inl; i+=bl) |
e79f8773 RL |
369 | |
370 | #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ | |
371 | static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
372 | {\ | |
373 | BLOCK_CIPHER_ecb_loop() \ | |
ed576acd | 374 | cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_is_encrypting(ctx)); \ |
e79f8773 RL |
375 | return 1;\ |
376 | } | |
377 | ||
378 | #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2)) | |
379 | ||
380 | #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \ | |
381 | static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
382 | {\ | |
383 | while(inl>=EVP_MAXCHUNK) {\ | |
ed576acd | 384 | int num = EVP_CIPHER_CTX_get_num(ctx);\ |
2f5c405a | 385 | cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \ |
e79f8773 RL |
386 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
387 | inl-=EVP_MAXCHUNK;\ | |
388 | in +=EVP_MAXCHUNK;\ | |
389 | out+=EVP_MAXCHUNK;\ | |
390 | }\ | |
391 | if (inl) {\ | |
ed576acd | 392 | int num = EVP_CIPHER_CTX_get_num(ctx);\ |
2f5c405a | 393 | cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \ |
e79f8773 RL |
394 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
395 | }\ | |
396 | return 1;\ | |
397 | } | |
398 | ||
399 | #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ | |
400 | static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
401 | {\ | |
402 | while(inl>=EVP_MAXCHUNK) \ | |
403 | {\ | |
ed576acd | 404 | cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\ |
e79f8773 RL |
405 | inl-=EVP_MAXCHUNK;\ |
406 | in +=EVP_MAXCHUNK;\ | |
407 | out+=EVP_MAXCHUNK;\ | |
408 | }\ | |
409 | if (inl)\ | |
ed576acd | 410 | cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\ |
e79f8773 RL |
411 | return 1;\ |
412 | } | |
413 | ||
414 | #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ | |
415 | static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \ | |
416 | {\ | |
e8aa8b6c F |
417 | size_t chunk = EVP_MAXCHUNK;\ |
418 | if (cbits == 1) chunk >>= 3;\ | |
419 | if (inl < chunk) chunk = inl;\ | |
420 | while (inl && inl >= chunk)\ | |
421 | {\ | |
ed576acd | 422 | int num = EVP_CIPHER_CTX_get_num(ctx);\ |
e8aa8b6c F |
423 | cprefix##_cfb##cbits##_encrypt(in, out, (long) \ |
424 | ((cbits == 1) \ | |
425 | && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \ | |
604e591e | 426 | ? chunk*8 : chunk), \ |
2f5c405a | 427 | &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\ |
ed576acd | 428 | &num, EVP_CIPHER_CTX_is_encrypting(ctx));\ |
e8aa8b6c F |
429 | EVP_CIPHER_CTX_set_num(ctx, num);\ |
430 | inl -= chunk;\ | |
431 | in += chunk;\ | |
432 | out += chunk;\ | |
433 | if (inl < chunk) chunk = inl;\ | |
434 | }\ | |
435 | return 1;\ | |
e79f8773 RL |
436 | } |
437 | ||
438 | #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ | |
439 | BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \ | |
440 | BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \ | |
441 | BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \ | |
442 | BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) | |
443 | ||
444 | #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \ | |
445 | key_len, iv_len, flags, init_key, cleanup, \ | |
446 | set_asn1, get_asn1, ctrl) \ | |
447 | static const EVP_CIPHER cname##_##mode = { \ | |
448 | nid##_##nmode, block_size, key_len, iv_len, \ | |
449 | flags | EVP_CIPH_##MODE##_MODE, \ | |
f6c95e46 | 450 | EVP_ORIG_GLOBAL, \ |
e79f8773 RL |
451 | init_key, \ |
452 | cname##_##mode##_cipher, \ | |
453 | cleanup, \ | |
454 | sizeof(kstruct), \ | |
455 | set_asn1, get_asn1,\ | |
456 | ctrl, \ | |
457 | NULL \ | |
458 | }; \ | |
459 | const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; } | |
460 | ||
461 | #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \ | |
462 | iv_len, flags, init_key, cleanup, set_asn1, \ | |
463 | get_asn1, ctrl) \ | |
464 | BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \ | |
465 | iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) | |
466 | ||
467 | #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \ | |
468 | iv_len, cbits, flags, init_key, cleanup, \ | |
469 | set_asn1, get_asn1, ctrl) \ | |
470 | BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \ | |
471 | key_len, iv_len, flags, init_key, cleanup, set_asn1, \ | |
472 | get_asn1, ctrl) | |
473 | ||
474 | #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \ | |
475 | iv_len, cbits, flags, init_key, cleanup, \ | |
476 | set_asn1, get_asn1, ctrl) \ | |
477 | BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \ | |
478 | key_len, iv_len, flags, init_key, cleanup, set_asn1, \ | |
479 | get_asn1, ctrl) | |
480 | ||
481 | #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \ | |
482 | flags, init_key, cleanup, set_asn1, \ | |
483 | get_asn1, ctrl) \ | |
484 | BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \ | |
485 | 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl) | |
486 | ||
487 | #define BLOCK_CIPHER_defs(cname, kstruct, \ | |
488 | nid, block_size, key_len, iv_len, cbits, flags, \ | |
489 | init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
490 | BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \ | |
491 | init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
492 | BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \ | |
493 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
494 | BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \ | |
495 | flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \ | |
496 | BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \ | |
497 | init_key, cleanup, set_asn1, get_asn1, ctrl) | |
498 | ||
499 | /*- | |
500 | #define BLOCK_CIPHER_defs(cname, kstruct, \ | |
501 | nid, block_size, key_len, iv_len, flags,\ | |
502 | init_key, cleanup, set_asn1, get_asn1, ctrl)\ | |
503 | static const EVP_CIPHER cname##_cbc = {\ | |
504 | nid##_cbc, block_size, key_len, iv_len, \ | |
505 | flags | EVP_CIPH_CBC_MODE,\ | |
f6c95e46 | 506 | EVP_ORIG_GLOBAL,\ |
e79f8773 RL |
507 | init_key,\ |
508 | cname##_cbc_cipher,\ | |
509 | cleanup,\ | |
510 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
511 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
512 | set_asn1, get_asn1,\ | |
513 | ctrl, \ | |
514 | NULL \ | |
515 | };\ | |
516 | const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\ | |
517 | static const EVP_CIPHER cname##_cfb = {\ | |
518 | nid##_cfb64, 1, key_len, iv_len, \ | |
519 | flags | EVP_CIPH_CFB_MODE,\ | |
f6c95e46 | 520 | EVP_ORIG_GLOBAL,\ |
e79f8773 RL |
521 | init_key,\ |
522 | cname##_cfb_cipher,\ | |
523 | cleanup,\ | |
524 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
525 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
526 | set_asn1, get_asn1,\ | |
527 | ctrl,\ | |
528 | NULL \ | |
529 | };\ | |
530 | const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\ | |
531 | static const EVP_CIPHER cname##_ofb = {\ | |
532 | nid##_ofb64, 1, key_len, iv_len, \ | |
533 | flags | EVP_CIPH_OFB_MODE,\ | |
f6c95e46 | 534 | EVP_ORIG_GLOBAL,\ |
e79f8773 RL |
535 | init_key,\ |
536 | cname##_ofb_cipher,\ | |
537 | cleanup,\ | |
538 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
539 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
540 | set_asn1, get_asn1,\ | |
541 | ctrl,\ | |
542 | NULL \ | |
543 | };\ | |
544 | const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\ | |
545 | static const EVP_CIPHER cname##_ecb = {\ | |
546 | nid##_ecb, block_size, key_len, iv_len, \ | |
547 | flags | EVP_CIPH_ECB_MODE,\ | |
f6c95e46 | 548 | EVP_ORIG_GLOBAL,\ |
e79f8773 RL |
549 | init_key,\ |
550 | cname##_ecb_cipher,\ | |
551 | cleanup,\ | |
552 | sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\ | |
553 | sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\ | |
554 | set_asn1, get_asn1,\ | |
555 | ctrl,\ | |
556 | NULL \ | |
557 | };\ | |
558 | const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; } | |
559 | */ | |
560 | ||
561 | #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \ | |
562 | block_size, key_len, iv_len, cbits, \ | |
563 | flags, init_key, \ | |
564 | cleanup, set_asn1, get_asn1, ctrl) \ | |
565 | BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \ | |
566 | BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \ | |
567 | cbits, flags, init_key, cleanup, set_asn1, \ | |
568 | get_asn1, ctrl) | |
569 | ||
570 | #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \ | |
571 | BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \ | |
572 | BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \ | |
573 | NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \ | |
574 | (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \ | |
575 | cipher##_init_key, NULL, NULL, NULL, NULL) | |
576 | ||
924663c3 JZ |
577 | typedef struct { |
578 | unsigned char iv[EVP_MAX_IV_LENGTH]; | |
579 | unsigned int iv_len; | |
580 | unsigned int tag_len; | |
581 | } evp_cipher_aead_asn1_params; | |
582 | ||
583 | int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type, | |
584 | evp_cipher_aead_asn1_params *params); | |
585 | ||
586 | int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type, | |
587 | evp_cipher_aead_asn1_params *params); | |
588 | ||
64954e2f P |
589 | /* |
590 | * To support transparent execution of operation in backends other | |
591 | * than the "origin" key, we support transparent export/import to | |
592 | * those providers, and maintain a cache of the imported keydata, | |
593 | * so we don't need to redo the export/import every time we perform | |
594 | * the same operation in that same provider. | |
595 | * This requires that the "origin" backend (whether it's a legacy or a | |
596 | * provider "origin") implements exports, and that the target provider | |
597 | * has an EVP_KEYMGMT that implements import. | |
598 | */ | |
599 | typedef struct { | |
600 | EVP_KEYMGMT *keymgmt; | |
601 | void *keydata; | |
602 | } OP_CACHE_ELEM; | |
603 | ||
604 | DEFINE_STACK_OF(OP_CACHE_ELEM) | |
605 | ||
3aeb9348 | 606 | /* |
adc9f731 RL |
607 | * An EVP_PKEY can have the following states: |
608 | * | |
609 | * untyped & empty: | |
610 | * | |
611 | * type == EVP_PKEY_NONE && keymgmt == NULL | |
612 | * | |
613 | * typed & empty: | |
614 | * | |
615 | * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only) | |
616 | * || (keymgmt != NULL && keydata == NULL) ## provider side | |
617 | * | |
618 | * fully assigned: | |
619 | * | |
620 | * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only) | |
621 | * || (keymgmt != NULL && keydata != NULL) ## provider side | |
622 | * | |
5e5bc836 RL |
623 | * The easiest way to detect a legacy key is: |
624 | * | |
625 | * keymgmt == NULL && type != EVP_PKEY_NONE | |
626 | * | |
627 | * The easiest way to detect a provider side key is: | |
628 | * | |
629 | * keymgmt != NULL | |
3aeb9348 | 630 | */ |
5e5bc836 RL |
631 | #define evp_pkey_is_blank(pk) \ |
632 | ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL) | |
633 | #define evp_pkey_is_typed(pk) \ | |
634 | ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL) | |
4d4928ed RL |
635 | #ifndef FIPS_MODULE |
636 | # define evp_pkey_is_assigned(pk) \ | |
5e5bc836 | 637 | ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL) |
4d4928ed RL |
638 | #else |
639 | # define evp_pkey_is_assigned(pk) \ | |
640 | ((pk)->keydata != NULL) | |
641 | #endif | |
5e5bc836 RL |
642 | #define evp_pkey_is_legacy(pk) \ |
643 | ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL) | |
644 | #define evp_pkey_is_provided(pk) \ | |
645 | ((pk)->keymgmt != NULL) | |
646 | ||
b574c6a9 MC |
647 | union legacy_pkey_st { |
648 | void *ptr; | |
649 | struct rsa_st *rsa; /* RSA */ | |
650 | # ifndef OPENSSL_NO_DSA | |
651 | struct dsa_st *dsa; /* DSA */ | |
652 | # endif | |
653 | # ifndef OPENSSL_NO_DH | |
654 | struct dh_st *dh; /* DH */ | |
655 | # endif | |
656 | # ifndef OPENSSL_NO_EC | |
657 | struct ec_key_st *ec; /* ECC */ | |
658 | ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */ | |
659 | # endif | |
660 | }; | |
661 | ||
3aeb9348 | 662 | struct evp_pkey_st { |
a94a3e0d | 663 | /* == Legacy attributes == */ |
3aeb9348 DSH |
664 | int type; |
665 | int save_type; | |
3c6ed955 | 666 | |
f844f9eb | 667 | # ifndef FIPS_MODULE |
3c6ed955 RL |
668 | /* |
669 | * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD, | |
670 | * a pointer to a low level key and possibly a pointer to an engine. | |
671 | */ | |
3aeb9348 DSH |
672 | const EVP_PKEY_ASN1_METHOD *ameth; |
673 | ENGINE *engine; | |
d19b01ad | 674 | ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */ |
b574c6a9 MC |
675 | |
676 | /* Union to store the reference to an origin legacy key */ | |
677 | union legacy_pkey_st pkey; | |
678 | ||
679 | /* Union to store the reference to a non-origin legacy key */ | |
680 | union legacy_pkey_st legacy_cache_pkey; | |
adc9f731 | 681 | # endif |
a94a3e0d RL |
682 | |
683 | /* == Common attributes == */ | |
684 | CRYPTO_REF_COUNT references; | |
03273d61 | 685 | CRYPTO_RWLOCK *lock; |
b247113c | 686 | #ifndef FIPS_MODULE |
a94a3e0d RL |
687 | STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */ |
688 | int save_parameters; | |
b247113c | 689 | int foreign:1; /* the low-level key is using an engine or an app-method */ |
ff1f7cde AT |
690 | CRYPTO_EX_DATA ex_data; |
691 | #endif | |
a94a3e0d RL |
692 | |
693 | /* == Provider attributes == */ | |
3c6ed955 RL |
694 | |
695 | /* | |
696 | * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT | |
697 | * and a pointer to the provider side key data. This is never used at | |
698 | * the same time as the legacy key data above. | |
699 | */ | |
700 | EVP_KEYMGMT *keymgmt; | |
701 | void *keydata; | |
702 | /* | |
703 | * If any libcrypto code does anything that may modify the keydata | |
704 | * contents, this dirty counter must be incremented. | |
705 | */ | |
706 | size_t dirty_cnt; | |
707 | ||
a94a3e0d | 708 | /* |
3c6ed955 RL |
709 | * To support transparent execution of operation in backends other |
710 | * than the "origin" key, we support transparent export/import to | |
711 | * those providers, and maintain a cache of the imported keydata, | |
712 | * so we don't need to redo the export/import every time we perform | |
713 | * the same operation in that same provider. | |
a94a3e0d | 714 | */ |
64954e2f P |
715 | STACK_OF(OP_CACHE_ELEM) *operation_cache; |
716 | ||
70a1f7b4 | 717 | /* |
3c6ed955 RL |
718 | * We keep a copy of that "origin"'s dirty count, so we know if the |
719 | * operation cache needs flushing. | |
70a1f7b4 RL |
720 | */ |
721 | size_t dirty_cnt_copy; | |
6508e858 | 722 | |
b305452f | 723 | /* Cache of key object information */ |
6508e858 RL |
724 | struct { |
725 | int bits; | |
726 | int security_bits; | |
727 | int size; | |
728 | } cache; | |
3aeb9348 | 729 | } /* EVP_PKEY */ ; |
7b9f8f7f | 730 | |
864b89ce MC |
731 | #define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \ |
732 | ((ctx)->operation == EVP_PKEY_OP_SIGN \ | |
733 | || (ctx)->operation == EVP_PKEY_OP_SIGNCTX \ | |
734 | || (ctx)->operation == EVP_PKEY_OP_VERIFY \ | |
735 | || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \ | |
736 | || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER) | |
737 | ||
738 | #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \ | |
739 | ((ctx)->operation == EVP_PKEY_OP_DERIVE) | |
7b9f8f7f | 740 | |
2c938e2e MC |
741 | #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \ |
742 | ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \ | |
743 | || (ctx)->operation == EVP_PKEY_OP_DECRYPT) | |
744 | ||
62924755 RL |
745 | #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \ |
746 | ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \ | |
747 | || (ctx)->operation == EVP_PKEY_OP_KEYGEN) | |
748 | ||
9a1c4e41 RL |
749 | #define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \ |
750 | ((ctx)->operation == EVP_PKEY_OP_FROMDATA) | |
751 | ||
80f4fd18 SL |
752 | #define EVP_PKEY_CTX_IS_KEM_OP(ctx) \ |
753 | ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \ | |
754 | || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE) | |
755 | ||
b3599dbb MC |
756 | void openssl_add_all_ciphers_int(void); |
757 | void openssl_add_all_digests_int(void); | |
758 | void evp_cleanup_int(void); | |
0822e89a | 759 | void evp_app_cleanup_int(void); |
b4250010 | 760 | void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx, |
3c6ed955 RL |
761 | EVP_KEYMGMT **keymgmt, |
762 | const char *propquery); | |
f844f9eb | 763 | #ifndef FIPS_MODULE |
4ce1025a | 764 | int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src); |
b574c6a9 | 765 | void *evp_pkey_get_legacy(EVP_PKEY *pk); |
62924755 | 766 | void evp_pkey_free_legacy(EVP_PKEY *x); |
4f0831b8 TM |
767 | EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8inf, |
768 | OSSL_LIB_CTX *libctx, const char *propq); | |
62924755 | 769 | #endif |
9d6fcd42 | 770 | |
68552cde RL |
771 | /* |
772 | * KEYMGMT utility functions | |
773 | */ | |
af836c22 RL |
774 | |
775 | /* | |
776 | * Key import structure and helper function, to be used as an export callback | |
777 | */ | |
778 | struct evp_keymgmt_util_try_import_data_st { | |
779 | EVP_KEYMGMT *keymgmt; | |
780 | void *keydata; | |
781 | ||
782 | int selection; | |
783 | }; | |
784 | int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg); | |
785 | int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt, | |
786 | void *keydata); | |
787 | EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata); | |
788 | ||
655f73ce RL |
789 | int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection, |
790 | OSSL_CALLBACK *export_cb, void *export_cbarg); | |
b305452f | 791 | void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt); |
64954e2f P |
792 | OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk, |
793 | EVP_KEYMGMT *keymgmt); | |
0b07db6f | 794 | int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk, int locking); |
64954e2f | 795 | int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, |
3c6ed955 RL |
796 | EVP_KEYMGMT *keymgmt, void *keydata); |
797 | void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk); | |
68552cde | 798 | void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt, |
b305452f | 799 | int selection, const OSSL_PARAM params[]); |
157ded39 | 800 | int evp_keymgmt_util_has(EVP_PKEY *pk, int selection); |
1e9101c4 | 801 | int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection); |
ff3b59e1 | 802 | int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection); |
62924755 RL |
803 | void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt, |
804 | void *genctx, OSSL_CALLBACK *cb, void *cbarg); | |
3b924da0 RL |
805 | int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt, |
806 | void *keydata, | |
807 | char *mdname, size_t mdname_sz); | |
70a1f7b4 | 808 | |
68552cde RL |
809 | /* |
810 | * KEYMGMT provider interface functions | |
811 | */ | |
b305452f RL |
812 | void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt); |
813 | void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata); | |
814 | int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, | |
815 | void *keydata, OSSL_PARAM params[]); | |
4fe54d67 NT |
816 | int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt, |
817 | void *keydata, const OSSL_PARAM params[]); | |
1be63b3e P |
818 | void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection, |
819 | const OSSL_PARAM params[]); | |
1a5632e0 RL |
820 | int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx, |
821 | void *template); | |
822 | int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx, | |
823 | const OSSL_PARAM params[]); | |
1a5632e0 RL |
824 | void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx, |
825 | OSSL_CALLBACK *cb, void *cbarg); | |
826 | void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx); | |
b305452f | 827 | |
f616ad4b | 828 | int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt); |
5dacb38c RL |
829 | void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt, |
830 | const void *objref, size_t objref_sz); | |
831 | ||
b305452f RL |
832 | int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection); |
833 | int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata, | |
899e2564 | 834 | int selection, int checktype); |
bee5d6cd RL |
835 | int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt, |
836 | const void *keydata1, const void *keydata2, | |
837 | int selection); | |
b305452f RL |
838 | |
839 | int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata, | |
840 | int selection, const OSSL_PARAM params[]); | |
841 | const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt, | |
842 | int selection); | |
843 | int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata, | |
844 | int selection, OSSL_CALLBACK *param_cb, void *cbarg); | |
845 | const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt, | |
846 | int selection); | |
4a9fe33c | 847 | void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt, |
b4f447c0 | 848 | const void *keydata_from, int selection); |
12603de6 | 849 | |
46f4e1be | 850 | /* Pulling defines out of C source files */ |
9d6fcd42 | 851 | |
80ce21fe F |
852 | # define EVP_RC4_KEY_SIZE 16 |
853 | # ifndef TLS1_1_VERSION | |
854 | # define TLS1_1_VERSION 0x0302 | |
855 | # endif | |
c0804614 MC |
856 | |
857 | void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags); | |
858 | ||
859 | /* EVP_ENCODE_CTX flags */ | |
3fd59700 MC |
860 | /* Don't generate new lines when encoding */ |
861 | #define EVP_ENCODE_CTX_NO_NEWLINES 1 | |
862 | /* Use the SRP base64 alphabet instead of the standard one */ | |
863 | #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2 | |
7606bed9 | 864 | |
b4250010 DMSP |
865 | const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx, |
866 | const char *name); | |
867 | const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx, | |
868 | const char *name); | |
e683582b | 869 | |
4e17fb00 SL |
870 | int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen, |
871 | const unsigned char *salt, int saltlen, int iter, | |
872 | const EVP_MD *digest, int keylen, | |
873 | unsigned char *out, | |
874 | OSSL_LIB_CTX *libctx, const char *propq); | |
5ccada09 | 875 | |
80ce21fe | 876 | # ifndef FIPS_MODULE |
4fe54d67 NT |
877 | /* |
878 | * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params(). | |
879 | * | |
880 | * Return 1 on success, 0 or negative for errors. | |
881 | * | |
882 | * In particular they return -2 if any of the params is not supported. | |
883 | * | |
f844f9eb | 884 | * They are not available in FIPS_MODULE as they depend on |
4fe54d67 NT |
885 | * - EVP_PKEY_CTX_{get,set}_params() |
886 | * - EVP_PKEY_CTX_{gettable,settable}_params() | |
887 | * | |
888 | */ | |
889 | int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); | |
890 | int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); | |
472a88b7 | 891 | |
d8652be0 | 892 | EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id, |
b4250010 | 893 | OSSL_LIB_CTX *libctx, const char *propq); |
50914496 | 894 | int evp_pkey_name2type(const char *name); |
977e95b9 | 895 | const char *evp_pkey_type2name(int type); |
86df26b3 RL |
896 | |
897 | int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len); | |
898 | int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id); | |
899 | int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len); | |
900 | ||
901 | int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx); | |
80ce21fe F |
902 | # endif /* !defined(FIPS_MODULE) */ |
903 | ||
860ecfd7 | 904 | int evp_method_store_flush(OSSL_LIB_CTX *libctx); |
b4250010 | 905 | int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq, |
447588b6 MC |
906 | int loadconfig, int mirrored); |
907 | char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig); | |
3101ab60 MC |
908 | |
909 | void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force); | |
80ce21fe | 910 | |
e19246dc RL |
911 | /* Three possible states: */ |
912 | # define EVP_PKEY_STATE_UNKNOWN 0 | |
913 | # define EVP_PKEY_STATE_LEGACY 1 | |
914 | # define EVP_PKEY_STATE_PROVIDER 2 | |
915 | int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx); | |
916 | ||
9a1c4e41 RL |
917 | /* These two must ONLY be called for provider side operations */ |
918 | int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *ctx, | |
919 | int keytype, int optype, | |
920 | int cmd, int p1, void *p2); | |
921 | int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *ctx, | |
922 | const char *name, const char *value); | |
923 | ||
924 | /* These two must ONLY be called for legacy operations */ | |
56784203 | 925 | int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params); |
9a1c4e41 RL |
926 | int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); |
927 | ||
928 | /* This must ONLY be called for legacy EVP_PKEYs */ | |
929 | int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params); | |
930 | ||
7bc0fdd3 MC |
931 | /* Same as the public get0 functions but are not const */ |
932 | # ifndef OPENSSL_NO_DEPRECATED_3_0 | |
933 | DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey); | |
934 | EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey); | |
935 | RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey); | |
936 | # endif | |
937 | ||
f2e3584d P |
938 | /* Get internal identification number routines */ |
939 | int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher); | |
940 | int evp_cipher_get_number(const EVP_CIPHER *cipher); | |
941 | int evp_kdf_get_number(const EVP_KDF *kdf); | |
942 | int evp_kem_get_number(const EVP_KEM *wrap); | |
943 | int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch); | |
944 | int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt); | |
945 | int evp_mac_get_number(const EVP_MAC *mac); | |
946 | int evp_md_get_number(const EVP_MD *md); | |
947 | int evp_rand_get_number(const EVP_RAND *rand); | |
948 | int evp_signature_get_number(const EVP_SIGNATURE *signature); | |
949 | ||
80ce21fe | 950 | #endif /* OSSL_CRYPTO_EVP_H */ |