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