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