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