]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/crypto/evp.h
en EVP_PKEY_CTX_set_rsa_keygen_pubexp() BIGNUM management
[thirdparty/openssl.git] / include / crypto / evp.h
1 /*
2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
8 */
9
10 #include <openssl/evp.h>
11 #include <openssl/core_dispatch.h>
12 #include "internal/refcount.h"
13 #include "crypto/ecx.h"
14
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
21 struct evp_pkey_ctx_st {
22 /* Actual operation */
23 int operation;
24
25 /*
26 * Library context, property query, keytype and keymgmt associated with
27 * this context
28 */
29 OPENSSL_CTX *libctx;
30 const char *propquery;
31 const char *keytype;
32 EVP_KEYMGMT *keymgmt;
33
34 union {
35 struct {
36 void *genctx;
37 } keymgmt;
38
39 struct {
40 EVP_KEYEXCH *exchange;
41 void *exchprovctx;
42 } kex;
43
44 struct {
45 EVP_SIGNATURE *signature;
46 void *sigprovctx;
47 } sig;
48
49 struct {
50 EVP_ASYM_CIPHER *cipher;
51 void *ciphprovctx;
52 } ciph;
53 struct {
54 EVP_KEM *kem;
55 void *kemprovctx;
56 } encap;
57 } op;
58
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
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
82 /* Legacy fields below */
83
84 /* EVP_PKEY identity */
85 int legacy_keytype;
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;
94 /* Algorithm specific data */
95 void *data;
96 /* Indicator if digest_custom needs to be called */
97 unsigned int flag_call_digest_custom:1;
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;
105 } /* EVP_PKEY_CTX */ ;
106
107 #define EVP_PKEY_FLAG_DYNAMIC 1
108
109 struct evp_pkey_method_st {
110 int pkey_id;
111 int flags;
112 int (*init) (EVP_PKEY_CTX *ctx);
113 int (*copy) (EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
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);
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);
151 int (*check) (EVP_PKEY *pkey);
152 int (*public_check) (EVP_PKEY *pkey);
153 int (*param_check) (EVP_PKEY *pkey);
154
155 int (*digest_custom) (EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
156 } /* EVP_PKEY_METHOD */ ;
157
158 DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
159
160 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
161
162 const EVP_PKEY_METHOD *dh_pkey_method(void);
163 const EVP_PKEY_METHOD *dhx_pkey_method(void);
164 const EVP_PKEY_METHOD *dsa_pkey_method(void);
165 const EVP_PKEY_METHOD *ec_pkey_method(void);
166 const EVP_PKEY_METHOD *ecx25519_pkey_method(void);
167 const EVP_PKEY_METHOD *ecx448_pkey_method(void);
168 const EVP_PKEY_METHOD *ed25519_pkey_method(void);
169 const EVP_PKEY_METHOD *ed448_pkey_method(void);
170 const EVP_PKEY_METHOD *rsa_pkey_method(void);
171 const EVP_PKEY_METHOD *rsa_pss_pkey_method(void);
172
173 struct evp_mac_st {
174 OSSL_PROVIDER *prov;
175 int name_id;
176
177 CRYPTO_REF_COUNT refcnt;
178 CRYPTO_RWLOCK *lock;
179
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;
193 };
194
195 struct evp_kdf_st {
196 OSSL_PROVIDER *prov;
197 int name_id;
198 CRYPTO_REF_COUNT refcnt;
199 CRYPTO_RWLOCK *lock;
200
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;
212 };
213
214 struct evp_md_st {
215 /* nid */
216 int type;
217
218 /* Legacy structure members */
219 /* TODO(3.0): Remove these */
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);
232
233 /* New structure members */
234 /* TODO(3.0): Remove above comment when legacy has gone */
235 int name_id;
236 OSSL_PROVIDER *prov;
237 CRYPTO_REF_COUNT refcnt;
238 CRYPTO_RWLOCK *lock;
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;
252
253 } /* EVP_MD */ ;
254
255 struct evp_cipher_st {
256 int nid;
257
258 int block_size;
259 /* Default value for variable length ciphers */
260 int key_len;
261 int iv_len;
262
263 /* Legacy structure members */
264 /* TODO(3.0): Remove these */
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;
285
286 /* New structure members */
287 /* TODO(3.0): Remove above comment when legacy has gone */
288 int name_id;
289 OSSL_PROVIDER *prov;
290 CRYPTO_REF_COUNT refcnt;
291 CRYPTO_RWLOCK *lock;
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;
306 } /* EVP_CIPHER */ ;
307
308 /* Macros to code block cipher wrappers */
309
310 /* Wrapper functions for each cipher mode */
311
312 #define EVP_C_DATA(kstruct, ctx) \
313 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
314
315 #define BLOCK_CIPHER_ecb_loop() \
316 size_t i, bl; \
317 bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
318 if (inl < bl) return 1;\
319 inl -= bl; \
320 for (i=0; i <= inl; i+=bl)
321
322 #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
323 static 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);\
337 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
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);\
345 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
346 EVP_CIPHER_CTX_set_num(ctx, num);\
347 }\
348 return 1;\
349 }
350
351 #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
352 static 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 {\
356 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
357 inl-=EVP_MAXCHUNK;\
358 in +=EVP_MAXCHUNK;\
359 out+=EVP_MAXCHUNK;\
360 }\
361 if (inl)\
362 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
363 return 1;\
364 }
365
366 #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
367 static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
368 {\
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) \
378 ? chunk*8 : chunk), \
379 &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\
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;\
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) \
399 static 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 }; \
410 const 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) \
415 BLOCK_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) \
421 BLOCK_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) \
428 BLOCK_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) \
435 BLOCK_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) \
441 BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
442 init_key, cleanup, set_asn1, get_asn1, ctrl) \
443 BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
444 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
445 BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
446 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
447 BLOCK_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)\
454 static 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 };\
466 const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
467 static 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 };\
479 const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
480 static 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 };\
492 const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
493 static 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 };\
505 const 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
524 typedef 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
530 int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
531 evp_cipher_aead_asn1_params *params);
532
533 int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
534 evp_cipher_aead_asn1_params *params);
535
536 /*
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 *
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
560 */
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
572 struct evp_pkey_st {
573 /* == Legacy attributes == */
574 int type;
575 int save_type;
576
577 # ifndef FIPS_MODULE
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 */
582 const EVP_PKEY_ASN1_METHOD *ameth;
583 ENGINE *engine;
584 ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
585 union {
586 void *ptr;
587 # ifndef OPENSSL_NO_RSA
588 struct rsa_st *rsa; /* RSA */
589 # endif
590 # ifndef OPENSSL_NO_DSA
591 struct dsa_st *dsa; /* DSA */
592 # endif
593 # ifndef OPENSSL_NO_DH
594 struct dh_st *dh; /* DH */
595 # endif
596 # ifndef OPENSSL_NO_EC
597 struct ec_key_st *ec; /* ECC */
598 ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
599 # endif
600 } pkey;
601 # endif
602
603 /* == Common attributes == */
604 /* If these are modified, so must evp_pkey_downgrade() */
605 CRYPTO_REF_COUNT references;
606 CRYPTO_RWLOCK *lock;
607 STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
608 int save_parameters;
609 #ifndef FIPS_MODULE
610 CRYPTO_EX_DATA ex_data;
611 #endif
612
613 /* == Provider attributes == */
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
628 /*
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.
641 */
642 struct {
643 EVP_KEYMGMT *keymgmt;
644 void *keydata;
645 } operation_cache[10];
646 /*
647 * We keep a copy of that "origin"'s dirty count, so we know if the
648 * operation cache needs flushing.
649 */
650 size_t dirty_cnt_copy;
651
652 /* Cache of key object information */
653 struct {
654 int bits;
655 int security_bits;
656 int size;
657 } cache;
658 } /* EVP_PKEY */ ;
659
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)
669
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
674 #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
675 ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
676 || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
677
678 #define EVP_PKEY_CTX_IS_KEM_OP(ctx) \
679 ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \
680 || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE)
681
682 void openssl_add_all_ciphers_int(void);
683 void openssl_add_all_digests_int(void);
684 void evp_cleanup_int(void);
685 void evp_app_cleanup_int(void);
686 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
687 EVP_KEYMGMT **keymgmt,
688 const char *propquery);
689 #ifndef FIPS_MODULE
690 int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src);
691 int evp_pkey_downgrade(EVP_PKEY *pk);
692 void evp_pkey_free_legacy(EVP_PKEY *x);
693 #endif
694
695 /*
696 * KEYMGMT utility functions
697 */
698
699 /*
700 * Key import structure and helper function, to be used as an export callback
701 */
702 struct evp_keymgmt_util_try_import_data_st {
703 EVP_KEYMGMT *keymgmt;
704 void *keydata;
705
706 int selection;
707 };
708 int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg);
709 int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
710 void *keydata);
711 EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata);
712
713 int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
714 OSSL_CALLBACK *export_cb, void *export_cbarg);
715 void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt);
716 size_t evp_keymgmt_util_find_operation_cache_index(EVP_PKEY *pk,
717 EVP_KEYMGMT *keymgmt);
718 void evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk);
719 int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, size_t index,
720 EVP_KEYMGMT *keymgmt, void *keydata);
721 void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk);
722 void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
723 int selection, const OSSL_PARAM params[]);
724 int evp_keymgmt_util_has(EVP_PKEY *pk, int selection);
725 int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection);
726 int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection);
727 void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
728 void *genctx, OSSL_CALLBACK *cb, void *cbarg);
729 int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
730 void *keydata,
731 char *mdname, size_t mdname_sz);
732
733 /*
734 * KEYMGMT provider interface functions
735 */
736 void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt);
737 void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata);
738 int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt,
739 void *keydata, OSSL_PARAM params[]);
740 int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt,
741 void *keydata, const OSSL_PARAM params[]);
742 void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection);
743 int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
744 void *template);
745 int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
746 const OSSL_PARAM params[]);
747 void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
748 OSSL_CALLBACK *cb, void *cbarg);
749 void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx);
750
751 void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
752 const void *objref, size_t objref_sz);
753
754 int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection);
755 int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
756 int selection);
757 int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
758 const void *keydata1, const void *keydata2,
759 int selection);
760
761 int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
762 int selection, const OSSL_PARAM params[]);
763 const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
764 int selection);
765 int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
766 int selection, OSSL_CALLBACK *param_cb, void *cbarg);
767 const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
768 int selection);
769 int evp_keymgmt_copy(const EVP_KEYMGMT *keymgmt,
770 void *keydata_to, const void *keydata_from,
771 int selection);
772
773 /* Pulling defines out of C source files */
774
775 #define EVP_RC4_KEY_SIZE 16
776 #ifndef TLS1_1_VERSION
777 # define TLS1_1_VERSION 0x0302
778 #endif
779
780 void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
781
782 /* EVP_ENCODE_CTX flags */
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
787
788 const EVP_CIPHER *evp_get_cipherbyname_ex(OPENSSL_CTX *libctx, const char *name);
789 const EVP_MD *evp_get_digestbyname_ex(OPENSSL_CTX *libctx, const char *name);
790
791 int 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
797 #ifndef FIPS_MODULE
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 *
805 * They are not available in FIPS_MODULE as they depend on
806 * - EVP_PKEY_CTX_{get,set}_params()
807 * - EVP_PKEY_CTX_{gettable,settable}_params()
808 *
809 */
810 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
811 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
812
813 EVP_MD_CTX *evp_md_ctx_new_with_libctx(EVP_PKEY *pkey,
814 const ASN1_OCTET_STRING *id,
815 OPENSSL_CTX *libctx, const char *propq);
816 int evp_pkey_name2type(const char *name);
817
818 int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len);
819 int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id);
820 int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len);
821
822 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx);
823 #endif /* !defined(FIPS_MODULE) */
824 void evp_method_store_flush(OPENSSL_CTX *libctx);
825 int evp_set_default_properties_int(OPENSSL_CTX *libctx, const char *propq,
826 int loadconfig);
827
828 void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force);