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