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