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