]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/crypto/evp.h
Fix race condition & allow operation cache to grow.
[thirdparty/openssl.git] / include / crypto / evp.h
1 /*
2 * Copyright 2015-2020 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 #include <openssl/evp.h>
11 #include <openssl/core_dispatch.h>
12 #include "internal/refcount.h"
13 #include "crypto/ecx.h"
14
15 /*
16 * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
17 * values in evp.h
18 */
19 #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
20
21 /*
22 * An EVP_PKEY can have the following support states:
23 *
24 * Supports legacy implementations only:
25 *
26 * engine != NULL || keytype == NULL
27 *
28 * Supports provided implementations:
29 *
30 * engine == NULL && keytype != NULL
31 */
32 #define evp_pkey_ctx_is_legacy(ctx) \
33 ((ctx)->engine != NULL || (ctx)->keytype == NULL)
34 #define evp_pkey_ctx_is_provided(ctx) \
35 (!evp_pkey_ctx_is_legacy(ctx))
36
37 struct evp_pkey_ctx_st {
38 /* Actual operation */
39 int operation;
40
41 /*
42 * Library context, property query, keytype and keymgmt associated with
43 * this context
44 */
45 OSSL_LIB_CTX *libctx;
46 char *propquery;
47 const char *keytype;
48 EVP_KEYMGMT *keymgmt;
49
50 union {
51 struct {
52 void *genctx;
53 } keymgmt;
54
55 struct {
56 EVP_KEYEXCH *exchange;
57 void *exchprovctx;
58 } kex;
59
60 struct {
61 EVP_SIGNATURE *signature;
62 void *sigprovctx;
63 } sig;
64
65 struct {
66 EVP_ASYM_CIPHER *cipher;
67 void *ciphprovctx;
68 } ciph;
69 struct {
70 EVP_KEM *kem;
71 void *kemprovctx;
72 } encap;
73 } op;
74
75 /*
76 * Cached parameters. Inits of operations that depend on these should
77 * call evp_pkey_ctx_use_delayed_data() when the operation has been set
78 * up properly.
79 */
80 struct {
81 /* Distinguishing Identifier, ISO/IEC 15946-3, FIPS 196 */
82 char *dist_id_name; /* The name used with EVP_PKEY_CTX_ctrl_str() */
83 void *dist_id; /* The distinguishing ID itself */
84 size_t dist_id_len; /* The length of the distinguishing ID */
85
86 /* Indicators of what has been set. Keep them together! */
87 unsigned int dist_id_set : 1;
88 } cached_parameters;
89
90 /* Application specific data, usually used by the callback */
91 void *app_data;
92 /* Keygen callback */
93 EVP_PKEY_gen_cb *pkey_gencb;
94 /* implementation specific keygen data */
95 int *keygen_info;
96 int keygen_info_count;
97
98 /* Legacy fields below */
99
100 /* EVP_PKEY identity */
101 int legacy_keytype;
102 /* Method associated with this operation */
103 const EVP_PKEY_METHOD *pmeth;
104 /* Engine that implements this method or NULL if builtin */
105 ENGINE *engine;
106 /* Key: may be NULL */
107 EVP_PKEY *pkey;
108 /* Peer key for key agreement, may be NULL */
109 EVP_PKEY *peerkey;
110 /* Algorithm specific data */
111 void *data;
112 /* Indicator if digest_custom needs to be called */
113 unsigned int flag_call_digest_custom:1;
114 /*
115 * Used to support taking custody of memory in the case of a provider being
116 * used with the deprecated EVP_PKEY_CTX_set_rsa_keygen_pubexp() API. This
117 * member should NOT be used for any other purpose and should be removed
118 * when said deprecated API is excised completely.
119 */
120 BIGNUM *rsa_pubexp;
121 } /* EVP_PKEY_CTX */ ;
122
123 #define EVP_PKEY_FLAG_DYNAMIC 1
124
125 struct evp_pkey_method_st {
126 int pkey_id;
127 int flags;
128 int (*init) (EVP_PKEY_CTX *ctx);
129 int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
130 void (*cleanup) (EVP_PKEY_CTX *ctx);
131 int (*paramgen_init) (EVP_PKEY_CTX *ctx);
132 int (*paramgen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
133 int (*keygen_init) (EVP_PKEY_CTX *ctx);
134 int (*keygen) (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
135 int (*sign_init) (EVP_PKEY_CTX *ctx);
136 int (*sign) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
137 const unsigned char *tbs, size_t tbslen);
138 int (*verify_init) (EVP_PKEY_CTX *ctx);
139 int (*verify) (EVP_PKEY_CTX *ctx,
140 const unsigned char *sig, size_t siglen,
141 const unsigned char *tbs, size_t tbslen);
142 int (*verify_recover_init) (EVP_PKEY_CTX *ctx);
143 int (*verify_recover) (EVP_PKEY_CTX *ctx,
144 unsigned char *rout, size_t *routlen,
145 const unsigned char *sig, size_t siglen);
146 int (*signctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
147 int (*signctx) (EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
148 EVP_MD_CTX *mctx);
149 int (*verifyctx_init) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
150 int (*verifyctx) (EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
151 EVP_MD_CTX *mctx);
152 int (*encrypt_init) (EVP_PKEY_CTX *ctx);
153 int (*encrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
154 const unsigned char *in, size_t inlen);
155 int (*decrypt_init) (EVP_PKEY_CTX *ctx);
156 int (*decrypt) (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
157 const unsigned char *in, size_t inlen);
158 int (*derive_init) (EVP_PKEY_CTX *ctx);
159 int (*derive) (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
160 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
161 int (*ctrl_str) (EVP_PKEY_CTX *ctx, const char *type, const char *value);
162 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
163 const unsigned char *tbs, size_t tbslen);
164 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
165 size_t siglen, const unsigned char *tbs,
166 size_t tbslen);
167 int (*check) (EVP_PKEY *pkey);
168 int (*public_check) (EVP_PKEY *pkey);
169 int (*param_check) (EVP_PKEY *pkey);
170
171 int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
172 } /* EVP_PKEY_METHOD */ ;
173
174 DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
175
176 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
177
178 const EVP_PKEY_METHOD *dh_pkey_method(void);
179 const EVP_PKEY_METHOD *dhx_pkey_method(void);
180 const EVP_PKEY_METHOD *dsa_pkey_method(void);
181 const EVP_PKEY_METHOD *ec_pkey_method(void);
182 const EVP_PKEY_METHOD *ecx25519_pkey_method(void);
183 const EVP_PKEY_METHOD *ecx448_pkey_method(void);
184 const EVP_PKEY_METHOD *ed25519_pkey_method(void);
185 const EVP_PKEY_METHOD *ed448_pkey_method(void);
186 const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void);
187 const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void);
188
189 struct evp_mac_st {
190 OSSL_PROVIDER *prov;
191 int name_id;
192
193 CRYPTO_REF_COUNT refcnt;
194 CRYPTO_RWLOCK *lock;
195
196 OSSL_FUNC_mac_newctx_fn *newctx;
197 OSSL_FUNC_mac_dupctx_fn *dupctx;
198 OSSL_FUNC_mac_freectx_fn *freectx;
199 OSSL_FUNC_mac_init_fn *init;
200 OSSL_FUNC_mac_update_fn *update;
201 OSSL_FUNC_mac_final_fn *final;
202 OSSL_FUNC_mac_gettable_params_fn *gettable_params;
203 OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params;
204 OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params;
205 OSSL_FUNC_mac_get_params_fn *get_params;
206 OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params;
207 OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params;
208 };
209
210 struct evp_kdf_st {
211 OSSL_PROVIDER *prov;
212 int name_id;
213 CRYPTO_REF_COUNT refcnt;
214 CRYPTO_RWLOCK *lock;
215
216 OSSL_FUNC_kdf_newctx_fn *newctx;
217 OSSL_FUNC_kdf_dupctx_fn *dupctx;
218 OSSL_FUNC_kdf_freectx_fn *freectx;
219 OSSL_FUNC_kdf_reset_fn *reset;
220 OSSL_FUNC_kdf_derive_fn *derive;
221 OSSL_FUNC_kdf_gettable_params_fn *gettable_params;
222 OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params;
223 OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params;
224 OSSL_FUNC_kdf_get_params_fn *get_params;
225 OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params;
226 OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params;
227 };
228
229 struct evp_md_st {
230 /* nid */
231 int type;
232
233 /* Legacy structure members */
234 /* TODO(3.0): Remove these */
235 int pkey_type;
236 int md_size;
237 unsigned long flags;
238 int (*init) (EVP_MD_CTX *ctx);
239 int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
240 int (*final) (EVP_MD_CTX *ctx, unsigned char *md);
241 int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from);
242 int (*cleanup) (EVP_MD_CTX *ctx);
243 int block_size;
244 int ctx_size; /* how big does the ctx->md_data need to be */
245 /* control function */
246 int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
247
248 /* New structure members */
249 /* TODO(3.0): Remove above comment when legacy has gone */
250 int name_id;
251 OSSL_PROVIDER *prov;
252 CRYPTO_REF_COUNT refcnt;
253 CRYPTO_RWLOCK *lock;
254 OSSL_FUNC_digest_newctx_fn *newctx;
255 OSSL_FUNC_digest_init_fn *dinit;
256 OSSL_FUNC_digest_update_fn *dupdate;
257 OSSL_FUNC_digest_final_fn *dfinal;
258 OSSL_FUNC_digest_digest_fn *digest;
259 OSSL_FUNC_digest_freectx_fn *freectx;
260 OSSL_FUNC_digest_dupctx_fn *dupctx;
261 OSSL_FUNC_digest_get_params_fn *get_params;
262 OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params;
263 OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params;
264 OSSL_FUNC_digest_gettable_params_fn *gettable_params;
265 OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params;
266 OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params;
267
268 } /* EVP_MD */ ;
269
270 struct evp_cipher_st {
271 int nid;
272
273 int block_size;
274 /* Default value for variable length ciphers */
275 int key_len;
276 int iv_len;
277
278 /* Legacy structure members */
279 /* TODO(3.0): Remove these */
280 /* Various flags */
281 unsigned long flags;
282 /* init key */
283 int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
284 const unsigned char *iv, int enc);
285 /* encrypt/decrypt data */
286 int (*do_cipher) (EVP_CIPHER_CTX *ctx, unsigned char *out,
287 const unsigned char *in, size_t inl);
288 /* cleanup ctx */
289 int (*cleanup) (EVP_CIPHER_CTX *);
290 /* how big ctx->cipher_data needs to be */
291 int ctx_size;
292 /* Populate a ASN1_TYPE with parameters */
293 int (*set_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
294 /* Get parameters from a ASN1_TYPE */
295 int (*get_asn1_parameters) (EVP_CIPHER_CTX *, ASN1_TYPE *);
296 /* Miscellaneous operations */
297 int (*ctrl) (EVP_CIPHER_CTX *, int type, int arg, void *ptr);
298 /* Application data */
299 void *app_data;
300
301 /* New structure members */
302 /* TODO(3.0): Remove above comment when legacy has gone */
303 int name_id;
304 OSSL_PROVIDER *prov;
305 CRYPTO_REF_COUNT refcnt;
306 CRYPTO_RWLOCK *lock;
307 OSSL_FUNC_cipher_newctx_fn *newctx;
308 OSSL_FUNC_cipher_encrypt_init_fn *einit;
309 OSSL_FUNC_cipher_decrypt_init_fn *dinit;
310 OSSL_FUNC_cipher_update_fn *cupdate;
311 OSSL_FUNC_cipher_final_fn *cfinal;
312 OSSL_FUNC_cipher_cipher_fn *ccipher;
313 OSSL_FUNC_cipher_freectx_fn *freectx;
314 OSSL_FUNC_cipher_dupctx_fn *dupctx;
315 OSSL_FUNC_cipher_get_params_fn *get_params;
316 OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params;
317 OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params;
318 OSSL_FUNC_cipher_gettable_params_fn *gettable_params;
319 OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params;
320 OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params;
321 } /* EVP_CIPHER */ ;
322
323 /* Macros to code block cipher wrappers */
324
325 /* Wrapper functions for each cipher mode */
326
327 #define EVP_C_DATA(kstruct, ctx) \
328 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
329
330 #define BLOCK_CIPHER_ecb_loop() \
331 size_t i, bl; \
332 bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
333 if (inl < bl) return 1;\
334 inl -= bl; \
335 for (i=0; i <= inl; i+=bl)
336
337 #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
338 static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
339 {\
340 BLOCK_CIPHER_ecb_loop() \
341 cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \
342 return 1;\
343 }
344
345 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
346
347 #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
348 static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
349 {\
350 while(inl>=EVP_MAXCHUNK) {\
351 int num = EVP_CIPHER_CTX_num(ctx);\
352 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
353 EVP_CIPHER_CTX_set_num(ctx, num);\
354 inl-=EVP_MAXCHUNK;\
355 in +=EVP_MAXCHUNK;\
356 out+=EVP_MAXCHUNK;\
357 }\
358 if (inl) {\
359 int num = EVP_CIPHER_CTX_num(ctx);\
360 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
361 EVP_CIPHER_CTX_set_num(ctx, num);\
362 }\
363 return 1;\
364 }
365
366 #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
367 static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
368 {\
369 while(inl>=EVP_MAXCHUNK) \
370 {\
371 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
372 inl-=EVP_MAXCHUNK;\
373 in +=EVP_MAXCHUNK;\
374 out+=EVP_MAXCHUNK;\
375 }\
376 if (inl)\
377 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
378 return 1;\
379 }
380
381 #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
382 static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
383 {\
384 size_t chunk = EVP_MAXCHUNK;\
385 if (cbits == 1) chunk >>= 3;\
386 if (inl < chunk) chunk = inl;\
387 while (inl && inl >= chunk)\
388 {\
389 int num = EVP_CIPHER_CTX_num(ctx);\
390 cprefix##_cfb##cbits##_encrypt(in, out, (long) \
391 ((cbits == 1) \
392 && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
393 ? chunk*8 : chunk), \
394 &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\
395 &num, EVP_CIPHER_CTX_encrypting(ctx));\
396 EVP_CIPHER_CTX_set_num(ctx, num);\
397 inl -= chunk;\
398 in += chunk;\
399 out += chunk;\
400 if (inl < chunk) chunk = inl;\
401 }\
402 return 1;\
403 }
404
405 #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
406 BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
407 BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
408 BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
409 BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
410
411 #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
412 key_len, iv_len, flags, init_key, cleanup, \
413 set_asn1, get_asn1, ctrl) \
414 static const EVP_CIPHER cname##_##mode = { \
415 nid##_##nmode, block_size, key_len, iv_len, \
416 flags | EVP_CIPH_##MODE##_MODE, \
417 init_key, \
418 cname##_##mode##_cipher, \
419 cleanup, \
420 sizeof(kstruct), \
421 set_asn1, get_asn1,\
422 ctrl, \
423 NULL \
424 }; \
425 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
426
427 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
428 iv_len, flags, init_key, cleanup, set_asn1, \
429 get_asn1, ctrl) \
430 BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
431 iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
432
433 #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
434 iv_len, cbits, flags, init_key, cleanup, \
435 set_asn1, get_asn1, ctrl) \
436 BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
437 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
438 get_asn1, ctrl)
439
440 #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
441 iv_len, cbits, flags, init_key, cleanup, \
442 set_asn1, get_asn1, ctrl) \
443 BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
444 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
445 get_asn1, ctrl)
446
447 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
448 flags, init_key, cleanup, set_asn1, \
449 get_asn1, ctrl) \
450 BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
451 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
452
453 #define BLOCK_CIPHER_defs(cname, kstruct, \
454 nid, block_size, key_len, iv_len, cbits, flags, \
455 init_key, cleanup, set_asn1, get_asn1, ctrl) \
456 BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
457 init_key, cleanup, set_asn1, get_asn1, ctrl) \
458 BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
459 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
460 BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
461 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
462 BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
463 init_key, cleanup, set_asn1, get_asn1, ctrl)
464
465 /*-
466 #define BLOCK_CIPHER_defs(cname, kstruct, \
467 nid, block_size, key_len, iv_len, flags,\
468 init_key, cleanup, set_asn1, get_asn1, ctrl)\
469 static const EVP_CIPHER cname##_cbc = {\
470 nid##_cbc, block_size, key_len, iv_len, \
471 flags | EVP_CIPH_CBC_MODE,\
472 init_key,\
473 cname##_cbc_cipher,\
474 cleanup,\
475 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
476 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
477 set_asn1, get_asn1,\
478 ctrl, \
479 NULL \
480 };\
481 const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
482 static const EVP_CIPHER cname##_cfb = {\
483 nid##_cfb64, 1, key_len, iv_len, \
484 flags | EVP_CIPH_CFB_MODE,\
485 init_key,\
486 cname##_cfb_cipher,\
487 cleanup,\
488 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
489 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
490 set_asn1, get_asn1,\
491 ctrl,\
492 NULL \
493 };\
494 const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
495 static const EVP_CIPHER cname##_ofb = {\
496 nid##_ofb64, 1, key_len, iv_len, \
497 flags | EVP_CIPH_OFB_MODE,\
498 init_key,\
499 cname##_ofb_cipher,\
500 cleanup,\
501 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
502 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
503 set_asn1, get_asn1,\
504 ctrl,\
505 NULL \
506 };\
507 const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
508 static const EVP_CIPHER cname##_ecb = {\
509 nid##_ecb, block_size, key_len, iv_len, \
510 flags | EVP_CIPH_ECB_MODE,\
511 init_key,\
512 cname##_ecb_cipher,\
513 cleanup,\
514 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
515 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
516 set_asn1, get_asn1,\
517 ctrl,\
518 NULL \
519 };\
520 const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
521 */
522
523 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
524 block_size, key_len, iv_len, cbits, \
525 flags, init_key, \
526 cleanup, set_asn1, get_asn1, ctrl) \
527 BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
528 BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
529 cbits, flags, init_key, cleanup, set_asn1, \
530 get_asn1, ctrl)
531
532 #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
533 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
534 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
535 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
536 (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
537 cipher##_init_key, NULL, NULL, NULL, NULL)
538
539 typedef struct {
540 unsigned char iv[EVP_MAX_IV_LENGTH];
541 unsigned int iv_len;
542 unsigned int tag_len;
543 } evp_cipher_aead_asn1_params;
544
545 int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
546 evp_cipher_aead_asn1_params *params);
547
548 int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
549 evp_cipher_aead_asn1_params *params);
550
551 /*
552 * To support transparent execution of operation in backends other
553 * than the "origin" key, we support transparent export/import to
554 * those providers, and maintain a cache of the imported keydata,
555 * so we don't need to redo the export/import every time we perform
556 * the same operation in that same provider.
557 * This requires that the "origin" backend (whether it's a legacy or a
558 * provider "origin") implements exports, and that the target provider
559 * has an EVP_KEYMGMT that implements import.
560 */
561 typedef struct {
562 EVP_KEYMGMT *keymgmt;
563 void *keydata;
564 } OP_CACHE_ELEM;
565
566 DEFINE_STACK_OF(OP_CACHE_ELEM)
567
568 /*
569 * An EVP_PKEY can have the following states:
570 *
571 * untyped & empty:
572 *
573 * type == EVP_PKEY_NONE && keymgmt == NULL
574 *
575 * typed & empty:
576 *
577 * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only)
578 * || (keymgmt != NULL && keydata == NULL) ## provider side
579 *
580 * fully assigned:
581 *
582 * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only)
583 * || (keymgmt != NULL && keydata != NULL) ## provider side
584 *
585 * The easiest way to detect a legacy key is:
586 *
587 * keymgmt == NULL && type != EVP_PKEY_NONE
588 *
589 * The easiest way to detect a provider side key is:
590 *
591 * keymgmt != NULL
592 */
593 #define evp_pkey_is_blank(pk) \
594 ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL)
595 #define evp_pkey_is_typed(pk) \
596 ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL)
597 #define evp_pkey_is_assigned(pk) \
598 ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
599 #define evp_pkey_is_legacy(pk) \
600 ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL)
601 #define evp_pkey_is_provided(pk) \
602 ((pk)->keymgmt != NULL)
603
604 struct evp_pkey_st {
605 /* == Legacy attributes == */
606 int type;
607 int save_type;
608
609 # ifndef FIPS_MODULE
610 /*
611 * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD,
612 * a pointer to a low level key and possibly a pointer to an engine.
613 */
614 const EVP_PKEY_ASN1_METHOD *ameth;
615 ENGINE *engine;
616 ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
617 union {
618 void *ptr;
619 struct rsa_st *rsa; /* RSA */
620 # ifndef OPENSSL_NO_DSA
621 struct dsa_st *dsa; /* DSA */
622 # endif
623 # ifndef OPENSSL_NO_DH
624 struct dh_st *dh; /* DH */
625 # endif
626 # ifndef OPENSSL_NO_EC
627 struct ec_key_st *ec; /* ECC */
628 ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
629 # endif
630 } pkey;
631 # endif
632
633 /* == Common attributes == */
634 /* If these are modified, so must evp_pkey_downgrade() */
635 CRYPTO_REF_COUNT references;
636 CRYPTO_RWLOCK *lock;
637 STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
638 int save_parameters;
639 #ifndef FIPS_MODULE
640 CRYPTO_EX_DATA ex_data;
641 #endif
642
643 /* == Provider attributes == */
644
645 /*
646 * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT
647 * and a pointer to the provider side key data. This is never used at
648 * the same time as the legacy key data above.
649 */
650 EVP_KEYMGMT *keymgmt;
651 void *keydata;
652 /*
653 * If any libcrypto code does anything that may modify the keydata
654 * contents, this dirty counter must be incremented.
655 */
656 size_t dirty_cnt;
657
658 /*
659 * To support transparent execution of operation in backends other
660 * than the "origin" key, we support transparent export/import to
661 * those providers, and maintain a cache of the imported keydata,
662 * so we don't need to redo the export/import every time we perform
663 * the same operation in that same provider.
664 */
665 STACK_OF(OP_CACHE_ELEM) *operation_cache;
666
667 /*
668 * We keep a copy of that "origin"'s dirty count, so we know if the
669 * operation cache needs flushing.
670 */
671 size_t dirty_cnt_copy;
672
673 /* Cache of key object information */
674 struct {
675 int bits;
676 int security_bits;
677 int size;
678 } cache;
679 } /* EVP_PKEY */ ;
680
681 #define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \
682 ((ctx)->operation == EVP_PKEY_OP_SIGN \
683 || (ctx)->operation == EVP_PKEY_OP_SIGNCTX \
684 || (ctx)->operation == EVP_PKEY_OP_VERIFY \
685 || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \
686 || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER)
687
688 #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
689 ((ctx)->operation == EVP_PKEY_OP_DERIVE)
690
691 #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
692 ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \
693 || (ctx)->operation == EVP_PKEY_OP_DECRYPT)
694
695 #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
696 ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
697 || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
698
699 #define EVP_PKEY_CTX_IS_KEM_OP(ctx) \
700 ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \
701 || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE)
702
703 void openssl_add_all_ciphers_int(void);
704 void openssl_add_all_digests_int(void);
705 void evp_cleanup_int(void);
706 void evp_app_cleanup_int(void);
707 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
708 EVP_KEYMGMT **keymgmt,
709 const char *propquery);
710 #ifndef FIPS_MODULE
711 int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src);
712 int evp_pkey_downgrade(EVP_PKEY *pk);
713 void evp_pkey_free_legacy(EVP_PKEY *x);
714 #endif
715
716 /*
717 * KEYMGMT utility functions
718 */
719
720 /*
721 * Key import structure and helper function, to be used as an export callback
722 */
723 struct evp_keymgmt_util_try_import_data_st {
724 EVP_KEYMGMT *keymgmt;
725 void *keydata;
726
727 int selection;
728 };
729 int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg);
730 int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
731 void *keydata);
732 EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata);
733
734 int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
735 OSSL_CALLBACK *export_cb, void *export_cbarg);
736 void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt);
737 OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk,
738 EVP_KEYMGMT *keymgmt);
739 int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk, int locking);
740 int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk,
741 EVP_KEYMGMT *keymgmt, void *keydata);
742 void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk);
743 void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
744 int selection, const OSSL_PARAM params[]);
745 int evp_keymgmt_util_has(EVP_PKEY *pk, int selection);
746 int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection);
747 int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection);
748 void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
749 void *genctx, OSSL_CALLBACK *cb, void *cbarg);
750 int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
751 void *keydata,
752 char *mdname, size_t mdname_sz);
753
754 /*
755 * KEYMGMT provider interface functions
756 */
757 void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt);
758 void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata);
759 int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt,
760 void *keydata, OSSL_PARAM params[]);
761 int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt,
762 void *keydata, const OSSL_PARAM params[]);
763 void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection);
764 int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
765 void *template);
766 int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
767 const OSSL_PARAM params[]);
768 void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
769 OSSL_CALLBACK *cb, void *cbarg);
770 void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx);
771
772 void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
773 const void *objref, size_t objref_sz);
774
775 int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection);
776 int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
777 int selection);
778 int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
779 const void *keydata1, const void *keydata2,
780 int selection);
781
782 int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
783 int selection, const OSSL_PARAM params[]);
784 const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
785 int selection);
786 int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
787 int selection, OSSL_CALLBACK *param_cb, void *cbarg);
788 const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
789 int selection);
790 int evp_keymgmt_copy(const EVP_KEYMGMT *keymgmt,
791 void *keydata_to, const void *keydata_from,
792 int selection);
793
794 /* Pulling defines out of C source files */
795
796 #define EVP_RC4_KEY_SIZE 16
797 #ifndef TLS1_1_VERSION
798 # define TLS1_1_VERSION 0x0302
799 #endif
800
801 void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
802
803 /* EVP_ENCODE_CTX flags */
804 /* Don't generate new lines when encoding */
805 #define EVP_ENCODE_CTX_NO_NEWLINES 1
806 /* Use the SRP base64 alphabet instead of the standard one */
807 #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2
808
809 const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,
810 const char *name);
811 const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx,
812 const char *name);
813
814 int pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
815 const unsigned char *salt, int saltlen, int iter,
816 const EVP_MD *digest, int keylen, unsigned char *out,
817 OSSL_LIB_CTX *libctx, const char *propq);
818
819 #ifndef FIPS_MODULE
820 /*
821 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
822 *
823 * Return 1 on success, 0 or negative for errors.
824 *
825 * In particular they return -2 if any of the params is not supported.
826 *
827 * They are not available in FIPS_MODULE as they depend on
828 * - EVP_PKEY_CTX_{get,set}_params()
829 * - EVP_PKEY_CTX_{gettable,settable}_params()
830 *
831 */
832 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
833 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
834
835 EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
836 OSSL_LIB_CTX *libctx, const char *propq);
837 int evp_pkey_name2type(const char *name);
838 const char *evp_pkey_type2name(int type);
839
840 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len);
841 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id);
842 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len);
843
844 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx);
845 #endif /* !defined(FIPS_MODULE) */
846 void evp_method_store_flush(OSSL_LIB_CTX *libctx);
847 int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
848 int loadconfig);
849
850 void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force);