2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef OSSL_CRYPTO_EVP_H
11 # define OSSL_CRYPTO_EVP_H
14 # include <openssl/evp.h>
15 # include <openssl/core_dispatch.h>
16 # include "internal/refcount.h"
17 # include "crypto/ecx.h"
20 * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
23 #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
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))
30 struct evp_pkey_ctx_st
{
31 /* Actual operation */
35 * Library context, property query, keytype and keymgmt associated with
49 EVP_KEYEXCH
*exchange
;
51 * Opaque ctx returned from a providers exchange algorithm
52 * implementation OSSL_FUNC_keyexch_newctx()
58 EVP_SIGNATURE
*signature
;
60 * Opaque ctx returned from a providers signature algorithm
61 * implementation OSSL_FUNC_signature_newctx()
67 EVP_ASYM_CIPHER
*cipher
;
69 * Opaque ctx returned from a providers asymmetric cipher algorithm
70 * implementation OSSL_FUNC_asym_cipher_newctx()
77 * Opaque ctx returned from a providers KEM algorithm
78 * implementation OSSL_FUNC_kem_newctx()
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
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 */
95 /* Indicators of what has been set. Keep them together! */
96 unsigned int dist_id_set
: 1;
99 /* Application specific data, usually used by the callback */
101 /* Keygen callback */
102 EVP_PKEY_gen_cb
*pkey_gencb
;
103 /* implementation specific keygen data */
105 int keygen_info_count
;
107 /* Legacy fields below */
109 /* EVP_PKEY identity */
111 /* Method associated with this operation */
112 const EVP_PKEY_METHOD
*pmeth
;
113 /* Engine that implements this method or NULL if builtin */
115 /* Key: may be NULL */
117 /* Peer key for key agreement, may be NULL */
119 /* Algorithm specific data */
121 /* Indicator if digest_custom needs to be called */
122 unsigned int flag_call_digest_custom
:1;
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.
130 } /* EVP_PKEY_CTX */ ;
132 #define EVP_PKEY_FLAG_DYNAMIC 1
134 struct evp_pkey_method_st
{
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
,
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
,
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
,
176 int (*check
) (EVP_PKEY
*pkey
);
177 int (*public_check
) (EVP_PKEY
*pkey
);
178 int (*param_check
) (EVP_PKEY
*pkey
);
180 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
181 } /* EVP_PKEY_METHOD */ ;
183 DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD
)
185 void evp_pkey_set_cb_translate(BN_GENCB
*cb
, EVP_PKEY_CTX
*ctx
);
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);
202 const char *description
;
204 CRYPTO_REF_COUNT refcnt
;
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
;
225 const char *description
;
226 CRYPTO_REF_COUNT refcnt
;
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
;
242 #define EVP_ORIG_DYNAMIC 0
243 #define EVP_ORIG_GLOBAL 1
244 #define EVP_ORIG_METH 2
250 /* Legacy structure members */
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
);
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
);
265 /* New structure members */
266 /* Above comment to be removed when legacy has gone */
269 const char *description
;
271 CRYPTO_REF_COUNT refcnt
;
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
;
289 struct evp_cipher_st
{
293 /* Default value for variable length ciphers */
297 /* Legacy structure members */
300 /* How the EVP_CIPHER was created. */
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
);
309 int (*cleanup
) (EVP_CIPHER_CTX
*);
310 /* how big ctx->cipher_data needs to be */
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 */
321 /* New structure members */
322 /* Above comment to be removed when legacy has gone */
325 const char *description
;
327 CRYPTO_REF_COUNT refcnt
;
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
;
345 /* Macros to code block cipher wrappers */
347 /* Wrapper functions for each cipher mode */
349 #define EVP_C_DATA(kstruct, ctx) \
350 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
352 #define BLOCK_CIPHER_ecb_loop() \
354 bl = EVP_CIPHER_CTX_get0_cipher(ctx)->block_size; \
355 if (inl < bl) return 1;\
357 for (i=0; i <= inl; i+=bl)
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) \
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)); \
367 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
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) \
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);\
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);\
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) \
391 while(inl>=EVP_MAXCHUNK) \
393 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\
399 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));\
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) \
406 size_t chunk = EVP_MAXCHUNK;\
407 if (cbits == 1) chunk >>= 3;\
408 if (inl < chunk) chunk = inl;\
409 while (inl && inl >= chunk)\
411 int num = EVP_CIPHER_CTX_get_num(ctx);\
412 cprefix##_cfb##cbits##_encrypt(in, out, (long) \
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);\
422 if (inl < chunk) chunk = inl;\
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)
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, \
441 cname##_##mode##_cipher, \
448 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
450 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
451 iv_len, flags, init_key, cleanup, set_asn1, \
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)
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, \
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, \
470 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
471 flags, init_key, cleanup, set_asn1, \
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)
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)
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,\
499 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
500 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
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,\
513 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
514 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
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,\
527 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
528 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
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,\
541 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
542 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
547 const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
550 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
551 block_size, key_len, iv_len, cbits, \
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, \
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)
567 unsigned char iv
[EVP_MAX_IV_LENGTH
];
569 unsigned int tag_len
;
570 } evp_cipher_aead_asn1_params
;
572 int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
573 evp_cipher_aead_asn1_params
*params
);
575 int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
576 evp_cipher_aead_asn1_params
*params
);
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.
589 EVP_KEYMGMT
*keymgmt
;
593 DEFINE_STACK_OF(OP_CACHE_ELEM
)
596 * An EVP_PKEY can have the following states:
600 * type == EVP_PKEY_NONE && keymgmt == NULL
604 * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only)
605 * || (keymgmt != NULL && keydata == NULL) ## provider side
609 * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only)
610 * || (keymgmt != NULL && keydata != NULL) ## provider side
612 * The easiest way to detect a legacy key is:
614 * keymgmt == NULL && type != EVP_PKEY_NONE
616 * The easiest way to detect a provider side key is:
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)
625 # define evp_pkey_is_assigned(pk) \
626 ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
628 # define evp_pkey_is_assigned(pk) \
629 ((pk)->keydata != NULL)
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)
636 union legacy_pkey_st
{
638 struct rsa_st
*rsa
; /* RSA */
639 # ifndef OPENSSL_NO_DSA
640 struct dsa_st
*dsa
; /* DSA */
642 # ifndef OPENSSL_NO_DH
643 struct dh_st
*dh
; /* DH */
645 # ifndef OPENSSL_NO_EC
646 struct ec_key_st
*ec
; /* ECC */
647 ECX_KEY
*ecx
; /* X25519, X448, Ed25519, Ed448 */
652 /* == Legacy attributes == */
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.
661 const EVP_PKEY_ASN1_METHOD
*ameth
;
663 ENGINE
*pmeth_engine
; /* If not NULL public key ENGINE to use */
665 /* Union to store the reference to an origin legacy key */
666 union legacy_pkey_st pkey
;
668 /* Union to store the reference to a non-origin legacy key */
669 union legacy_pkey_st legacy_cache_pkey
;
672 /* == Common attributes == */
673 CRYPTO_REF_COUNT references
;
676 STACK_OF(X509_ATTRIBUTE
) *attributes
; /* [ 0 ] */
678 unsigned int foreign
:1; /* the low-level key is using an engine or an app-method */
679 CRYPTO_EX_DATA ex_data
;
682 /* == Provider attributes == */
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.
689 EVP_KEYMGMT
*keymgmt
;
692 * If any libcrypto code does anything that may modify the keydata
693 * contents, this dirty counter must be incremented.
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.
704 STACK_OF(OP_CACHE_ELEM
) *operation_cache
;
707 * We keep a copy of that "origin"'s dirty count, so we know if the
708 * operation cache needs flushing.
710 size_t dirty_cnt_copy
;
712 /* Cache of key object information */
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)
727 #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
728 ((ctx)->operation == EVP_PKEY_OP_DERIVE)
730 #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
731 ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \
732 || (ctx)->operation == EVP_PKEY_OP_DECRYPT)
734 #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
735 ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
736 || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
738 #define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \
739 ((ctx)->operation == EVP_PKEY_OP_FROMDATA)
741 #define EVP_PKEY_CTX_IS_KEM_OP(ctx) \
742 ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \
743 || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE)
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
);
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
);
761 * KEYMGMT utility functions
765 * Key import structure and helper function, to be used as an export callback
767 struct evp_keymgmt_util_try_import_data_st
{
768 EVP_KEYMGMT
*keymgmt
;
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
,
776 EVP_PKEY
*evp_keymgmt_util_make_pkey(EVP_KEYMGMT
*keymgmt
, void *keydata
);
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
,
796 char *mdname
, size_t mdname_sz
);
799 * KEYMGMT provider interface functions
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
,
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
);
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
);
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
,
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
,
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
,
836 void *evp_keymgmt_dup(const EVP_KEYMGMT
*keymgmt
,
837 const void *keydata_from
, int selection
);
839 /* Pulling defines out of C source files */
841 # define EVP_RC4_KEY_SIZE 16
842 # ifndef TLS1_1_VERSION
843 # define TLS1_1_VERSION 0x0302
846 void evp_encode_ctx_set_flags(EVP_ENCODE_CTX
*ctx
, unsigned int flags
);
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
854 const EVP_CIPHER
*evp_get_cipherbyname_ex(OSSL_LIB_CTX
*libctx
,
856 const EVP_MD
*evp_get_digestbyname_ex(OSSL_LIB_CTX
*libctx
,
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
,
863 OSSL_LIB_CTX
*libctx
, const char *propq
);
867 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
869 * Return 1 on success, 0 or negative for errors.
871 * In particular they return -2 if any of the params is not supported.
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()
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
);
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
);
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
);
890 int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX
*ctx
);
891 # endif /* !defined(FIPS_MODULE) */
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
);
898 void evp_md_ctx_clear_digest(EVP_MD_CTX
*ctx
, int force
);
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
);
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
);
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
);
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
);
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
);
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
);
939 #endif /* OSSL_CRYPTO_EVP_H */