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