2 * Copyright 2015-2020 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 #include <openssl/evp.h>
11 #include <openssl/core_dispatch.h>
12 #include "internal/refcount.h"
13 #include "crypto/ecx.h"
16 * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
19 #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
21 struct evp_pkey_ctx_st
{
22 /* Actual operation */
26 * Library context, property query, keytype and keymgmt associated with
30 const char *propquery
;
40 EVP_KEYEXCH
*exchange
;
45 EVP_SIGNATURE
*signature
;
50 EVP_ASYM_CIPHER
*cipher
;
55 /* Application specific data, usually used by the callback */
58 EVP_PKEY_gen_cb
*pkey_gencb
;
59 /* implementation specific keygen data */
61 int keygen_info_count
;
63 /* Legacy fields below */
65 /* Method associated with this operation */
66 const EVP_PKEY_METHOD
*pmeth
;
67 /* Engine that implements this method or NULL if builtin */
69 /* Key: may be NULL */
71 /* Peer key for key agreement, may be NULL */
73 /* Algorithm specific data */
75 /* Indicator if digest_custom needs to be called */
76 unsigned int flag_call_digest_custom
:1;
77 } /* EVP_PKEY_CTX */ ;
79 #define EVP_PKEY_FLAG_DYNAMIC 1
81 struct evp_pkey_method_st
{
84 int (*init
) (EVP_PKEY_CTX
*ctx
);
85 int (*copy
) (EVP_PKEY_CTX
*dst
, const EVP_PKEY_CTX
*src
);
86 void (*cleanup
) (EVP_PKEY_CTX
*ctx
);
87 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
);
88 int (*paramgen
) (EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
);
89 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
);
90 int (*keygen
) (EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
);
91 int (*sign_init
) (EVP_PKEY_CTX
*ctx
);
92 int (*sign
) (EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
93 const unsigned char *tbs
, size_t tbslen
);
94 int (*verify_init
) (EVP_PKEY_CTX
*ctx
);
95 int (*verify
) (EVP_PKEY_CTX
*ctx
,
96 const unsigned char *sig
, size_t siglen
,
97 const unsigned char *tbs
, size_t tbslen
);
98 int (*verify_recover_init
) (EVP_PKEY_CTX
*ctx
);
99 int (*verify_recover
) (EVP_PKEY_CTX
*ctx
,
100 unsigned char *rout
, size_t *routlen
,
101 const unsigned char *sig
, size_t siglen
);
102 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
103 int (*signctx
) (EVP_PKEY_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
105 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
106 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
, const unsigned char *sig
, int siglen
,
108 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
);
109 int (*encrypt
) (EVP_PKEY_CTX
*ctx
, unsigned char *out
, size_t *outlen
,
110 const unsigned char *in
, size_t inlen
);
111 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
);
112 int (*decrypt
) (EVP_PKEY_CTX
*ctx
, unsigned char *out
, size_t *outlen
,
113 const unsigned char *in
, size_t inlen
);
114 int (*derive_init
) (EVP_PKEY_CTX
*ctx
);
115 int (*derive
) (EVP_PKEY_CTX
*ctx
, unsigned char *key
, size_t *keylen
);
116 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
);
117 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
, const char *type
, const char *value
);
118 int (*digestsign
) (EVP_MD_CTX
*ctx
, unsigned char *sig
, size_t *siglen
,
119 const unsigned char *tbs
, size_t tbslen
);
120 int (*digestverify
) (EVP_MD_CTX
*ctx
, const unsigned char *sig
,
121 size_t siglen
, const unsigned char *tbs
,
123 int (*check
) (EVP_PKEY
*pkey
);
124 int (*public_check
) (EVP_PKEY
*pkey
);
125 int (*param_check
) (EVP_PKEY
*pkey
);
127 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
128 } /* EVP_PKEY_METHOD */ ;
130 DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD
)
132 void evp_pkey_set_cb_translate(BN_GENCB
*cb
, EVP_PKEY_CTX
*ctx
);
134 const EVP_PKEY_METHOD
*cmac_pkey_method(void);
135 const EVP_PKEY_METHOD
*dh_pkey_method(void);
136 const EVP_PKEY_METHOD
*dhx_pkey_method(void);
137 const EVP_PKEY_METHOD
*dsa_pkey_method(void);
138 const EVP_PKEY_METHOD
*ec_pkey_method(void);
139 const EVP_PKEY_METHOD
*sm2_pkey_method(void);
140 const EVP_PKEY_METHOD
*ecx25519_pkey_method(void);
141 const EVP_PKEY_METHOD
*ecx448_pkey_method(void);
142 const EVP_PKEY_METHOD
*ed25519_pkey_method(void);
143 const EVP_PKEY_METHOD
*ed448_pkey_method(void);
144 const EVP_PKEY_METHOD
*hmac_pkey_method(void);
145 const EVP_PKEY_METHOD
*rsa_pkey_method(void);
146 const EVP_PKEY_METHOD
*rsa_pss_pkey_method(void);
147 const EVP_PKEY_METHOD
*poly1305_pkey_method(void);
148 const EVP_PKEY_METHOD
*siphash_pkey_method(void);
154 CRYPTO_REF_COUNT refcnt
;
157 OSSL_FUNC_mac_newctx_fn
*newctx
;
158 OSSL_FUNC_mac_dupctx_fn
*dupctx
;
159 OSSL_FUNC_mac_freectx_fn
*freectx
;
160 OSSL_FUNC_mac_size_fn
*size
;
161 OSSL_FUNC_mac_init_fn
*init
;
162 OSSL_FUNC_mac_update_fn
*update
;
163 OSSL_FUNC_mac_final_fn
*final
;
164 OSSL_FUNC_mac_gettable_params_fn
*gettable_params
;
165 OSSL_FUNC_mac_gettable_ctx_params_fn
*gettable_ctx_params
;
166 OSSL_FUNC_mac_settable_ctx_params_fn
*settable_ctx_params
;
167 OSSL_FUNC_mac_get_params_fn
*get_params
;
168 OSSL_FUNC_mac_get_ctx_params_fn
*get_ctx_params
;
169 OSSL_FUNC_mac_set_ctx_params_fn
*set_ctx_params
;
175 CRYPTO_REF_COUNT refcnt
;
178 OSSL_FUNC_kdf_newctx_fn
*newctx
;
179 OSSL_FUNC_kdf_dupctx_fn
*dupctx
;
180 OSSL_FUNC_kdf_freectx_fn
*freectx
;
181 OSSL_FUNC_kdf_reset_fn
*reset
;
182 OSSL_FUNC_kdf_derive_fn
*derive
;
183 OSSL_FUNC_kdf_gettable_params_fn
*gettable_params
;
184 OSSL_FUNC_kdf_gettable_ctx_params_fn
*gettable_ctx_params
;
185 OSSL_FUNC_kdf_settable_ctx_params_fn
*settable_ctx_params
;
186 OSSL_FUNC_kdf_get_params_fn
*get_params
;
187 OSSL_FUNC_kdf_get_ctx_params_fn
*get_ctx_params
;
188 OSSL_FUNC_kdf_set_ctx_params_fn
*set_ctx_params
;
195 /* Legacy structure members */
196 /* TODO(3.0): Remove these */
200 int (*init
) (EVP_MD_CTX
*ctx
);
201 int (*update
) (EVP_MD_CTX
*ctx
, const void *data
, size_t count
);
202 int (*final
) (EVP_MD_CTX
*ctx
, unsigned char *md
);
203 int (*copy
) (EVP_MD_CTX
*to
, const EVP_MD_CTX
*from
);
204 int (*cleanup
) (EVP_MD_CTX
*ctx
);
206 int ctx_size
; /* how big does the ctx->md_data need to be */
207 /* control function */
208 int (*md_ctrl
) (EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
);
210 /* New structure members */
211 /* TODO(3.0): Remove above comment when legacy has gone */
214 CRYPTO_REF_COUNT refcnt
;
216 OSSL_FUNC_digest_newctx_fn
*newctx
;
217 OSSL_FUNC_digest_init_fn
*dinit
;
218 OSSL_FUNC_digest_update_fn
*dupdate
;
219 OSSL_FUNC_digest_final_fn
*dfinal
;
220 OSSL_FUNC_digest_digest_fn
*digest
;
221 OSSL_FUNC_digest_freectx_fn
*freectx
;
222 OSSL_FUNC_digest_dupctx_fn
*dupctx
;
223 OSSL_FUNC_digest_get_params_fn
*get_params
;
224 OSSL_FUNC_digest_set_ctx_params_fn
*set_ctx_params
;
225 OSSL_FUNC_digest_get_ctx_params_fn
*get_ctx_params
;
226 OSSL_FUNC_digest_gettable_params_fn
*gettable_params
;
227 OSSL_FUNC_digest_settable_ctx_params_fn
*settable_ctx_params
;
228 OSSL_FUNC_digest_gettable_ctx_params_fn
*gettable_ctx_params
;
232 struct evp_cipher_st
{
236 /* Default value for variable length ciphers */
240 /* Legacy structure members */
241 /* TODO(3.0): Remove these */
245 int (*init
) (EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
246 const unsigned char *iv
, int enc
);
247 /* encrypt/decrypt data */
248 int (*do_cipher
) (EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
249 const unsigned char *in
, size_t inl
);
251 int (*cleanup
) (EVP_CIPHER_CTX
*);
252 /* how big ctx->cipher_data needs to be */
254 /* Populate a ASN1_TYPE with parameters */
255 int (*set_asn1_parameters
) (EVP_CIPHER_CTX
*, ASN1_TYPE
*);
256 /* Get parameters from a ASN1_TYPE */
257 int (*get_asn1_parameters
) (EVP_CIPHER_CTX
*, ASN1_TYPE
*);
258 /* Miscellaneous operations */
259 int (*ctrl
) (EVP_CIPHER_CTX
*, int type
, int arg
, void *ptr
);
260 /* Application data */
263 /* New structure members */
264 /* TODO(3.0): Remove above comment when legacy has gone */
267 CRYPTO_REF_COUNT refcnt
;
269 OSSL_FUNC_cipher_newctx_fn
*newctx
;
270 OSSL_FUNC_cipher_encrypt_init_fn
*einit
;
271 OSSL_FUNC_cipher_decrypt_init_fn
*dinit
;
272 OSSL_FUNC_cipher_update_fn
*cupdate
;
273 OSSL_FUNC_cipher_final_fn
*cfinal
;
274 OSSL_FUNC_cipher_cipher_fn
*ccipher
;
275 OSSL_FUNC_cipher_freectx_fn
*freectx
;
276 OSSL_FUNC_cipher_dupctx_fn
*dupctx
;
277 OSSL_FUNC_cipher_get_params_fn
*get_params
;
278 OSSL_FUNC_cipher_get_ctx_params_fn
*get_ctx_params
;
279 OSSL_FUNC_cipher_set_ctx_params_fn
*set_ctx_params
;
280 OSSL_FUNC_cipher_gettable_params_fn
*gettable_params
;
281 OSSL_FUNC_cipher_gettable_ctx_params_fn
*gettable_ctx_params
;
282 OSSL_FUNC_cipher_settable_ctx_params_fn
*settable_ctx_params
;
285 /* Macros to code block cipher wrappers */
287 /* Wrapper functions for each cipher mode */
289 #define EVP_C_DATA(kstruct, ctx) \
290 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
292 #define BLOCK_CIPHER_ecb_loop() \
294 bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
295 if (inl < bl) return 1;\
297 for (i=0; i <= inl; i+=bl)
299 #define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
300 static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
302 BLOCK_CIPHER_ecb_loop() \
303 cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \
307 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
309 #define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched) \
310 static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
312 while(inl>=EVP_MAXCHUNK) {\
313 int num = EVP_CIPHER_CTX_num(ctx);\
314 cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
315 EVP_CIPHER_CTX_set_num(ctx, num);\
321 int num = EVP_CIPHER_CTX_num(ctx);\
322 cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, &num); \
323 EVP_CIPHER_CTX_set_num(ctx, num);\
328 #define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
329 static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
331 while(inl>=EVP_MAXCHUNK) \
333 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
339 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
343 #define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
344 static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) \
346 size_t chunk = EVP_MAXCHUNK;\
347 if (cbits == 1) chunk >>= 3;\
348 if (inl < chunk) chunk = inl;\
349 while (inl && inl >= chunk)\
351 int num = EVP_CIPHER_CTX_num(ctx);\
352 cprefix##_cfb##cbits##_encrypt(in, out, (long) \
354 && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) \
355 ? chunk*8 : chunk), \
356 &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,\
357 &num, EVP_CIPHER_CTX_encrypting(ctx));\
358 EVP_CIPHER_CTX_set_num(ctx, num);\
362 if (inl < chunk) chunk = inl;\
367 #define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
368 BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched) \
369 BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched) \
370 BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched) \
371 BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
373 #define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
374 key_len, iv_len, flags, init_key, cleanup, \
375 set_asn1, get_asn1, ctrl) \
376 static const EVP_CIPHER cname##_##mode = { \
377 nid##_##nmode, block_size, key_len, iv_len, \
378 flags | EVP_CIPH_##MODE##_MODE, \
380 cname##_##mode##_cipher, \
387 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
389 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
390 iv_len, flags, init_key, cleanup, set_asn1, \
392 BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
393 iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
395 #define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, \
396 iv_len, cbits, flags, init_key, cleanup, \
397 set_asn1, get_asn1, ctrl) \
398 BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
399 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
402 #define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, \
403 iv_len, cbits, flags, init_key, cleanup, \
404 set_asn1, get_asn1, ctrl) \
405 BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
406 key_len, iv_len, flags, init_key, cleanup, set_asn1, \
409 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
410 flags, init_key, cleanup, set_asn1, \
412 BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
413 0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
415 #define BLOCK_CIPHER_defs(cname, kstruct, \
416 nid, block_size, key_len, iv_len, cbits, flags, \
417 init_key, cleanup, set_asn1, get_asn1, ctrl) \
418 BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags, \
419 init_key, cleanup, set_asn1, get_asn1, ctrl) \
420 BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits, \
421 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
422 BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits, \
423 flags, init_key, cleanup, set_asn1, get_asn1, ctrl) \
424 BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
425 init_key, cleanup, set_asn1, get_asn1, ctrl)
428 #define BLOCK_CIPHER_defs(cname, kstruct, \
429 nid, block_size, key_len, iv_len, flags,\
430 init_key, cleanup, set_asn1, get_asn1, ctrl)\
431 static const EVP_CIPHER cname##_cbc = {\
432 nid##_cbc, block_size, key_len, iv_len, \
433 flags | EVP_CIPH_CBC_MODE,\
437 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
438 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
443 const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
444 static const EVP_CIPHER cname##_cfb = {\
445 nid##_cfb64, 1, key_len, iv_len, \
446 flags | EVP_CIPH_CFB_MODE,\
450 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
451 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
456 const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
457 static const EVP_CIPHER cname##_ofb = {\
458 nid##_ofb64, 1, key_len, iv_len, \
459 flags | EVP_CIPH_OFB_MODE,\
463 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
464 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
469 const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
470 static const EVP_CIPHER cname##_ecb = {\
471 nid##_ecb, block_size, key_len, iv_len, \
472 flags | EVP_CIPH_ECB_MODE,\
476 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
477 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
482 const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
485 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
486 block_size, key_len, iv_len, cbits, \
488 cleanup, set_asn1, get_asn1, ctrl) \
489 BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
490 BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
491 cbits, flags, init_key, cleanup, set_asn1, \
494 #define IMPLEMENT_CFBR(cipher,cprefix,kstruct,ksched,keysize,cbits,iv_len,fl) \
495 BLOCK_CIPHER_func_cfb(cipher##_##keysize,cprefix,cbits,kstruct,ksched) \
496 BLOCK_CIPHER_def_cfb(cipher##_##keysize,kstruct, \
497 NID_##cipher##_##keysize, keysize/8, iv_len, cbits, \
498 (fl)|EVP_CIPH_FLAG_DEFAULT_ASN1, \
499 cipher##_init_key, NULL, NULL, NULL, NULL)
502 * An EVP_PKEY can have the following states:
506 * type == EVP_PKEY_NONE && keymgmt == NULL
510 * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only)
511 * || (keymgmt != NULL && keydata == NULL) ## provider side
515 * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only)
516 * || (keymgmt != NULL && keydata != NULL) ## provider side
518 * The easiest way to detect a legacy key is:
520 * keymgmt == NULL && type != EVP_PKEY_NONE
522 * The easiest way to detect a provider side key is:
526 #define evp_pkey_is_blank(pk) \
527 ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL)
528 #define evp_pkey_is_typed(pk) \
529 ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL)
530 #define evp_pkey_is_assigned(pk) \
531 ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
532 #define evp_pkey_is_legacy(pk) \
533 ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL)
534 #define evp_pkey_is_provided(pk) \
535 ((pk)->keymgmt != NULL)
538 /* == Legacy attributes == */
544 * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD,
545 * a pointer to a low level key and possibly a pointer to an engine.
547 const EVP_PKEY_ASN1_METHOD
*ameth
;
549 ENGINE
*pmeth_engine
; /* If not NULL public key ENGINE to use */
552 # ifndef OPENSSL_NO_RSA
553 struct rsa_st
*rsa
; /* RSA */
555 # ifndef OPENSSL_NO_DSA
556 struct dsa_st
*dsa
; /* DSA */
558 # ifndef OPENSSL_NO_DH
559 struct dh_st
*dh
; /* DH */
561 # ifndef OPENSSL_NO_EC
562 struct ec_key_st
*ec
; /* ECC */
563 ECX_KEY
*ecx
; /* X25519, X448, Ed25519, Ed448 */
568 /* == Common attributes == */
569 CRYPTO_REF_COUNT references
;
571 STACK_OF(X509_ATTRIBUTE
) *attributes
; /* [ 0 ] */
574 CRYPTO_EX_DATA ex_data
;
577 /* == Provider attributes == */
580 * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT
581 * and a pointer to the provider side key data. This is never used at
582 * the same time as the legacy key data above.
584 EVP_KEYMGMT
*keymgmt
;
587 * If any libcrypto code does anything that may modify the keydata
588 * contents, this dirty counter must be incremented.
593 * To support transparent execution of operation in backends other
594 * than the "origin" key, we support transparent export/import to
595 * those providers, and maintain a cache of the imported keydata,
596 * so we don't need to redo the export/import every time we perform
597 * the same operation in that same provider.
598 * This requires that the "origin" backend (whether it's a legacy or a
599 * provider "origin") implements exports, and that the target provider
600 * has an EVP_KEYMGMT that implements import.
602 * The cache limit is set at 10 different providers using the same
603 * "origin". It's probably over the top, but is preferable to too
607 EVP_KEYMGMT
*keymgmt
;
609 } operation_cache
[10];
611 * We keep a copy of that "origin"'s dirty count, so we know if the
612 * operation cache needs flushing.
614 size_t dirty_cnt_copy
;
616 /* Cache of key object information */
624 #define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) \
625 ((ctx)->operation == EVP_PKEY_OP_SIGN \
626 || (ctx)->operation == EVP_PKEY_OP_SIGNCTX \
627 || (ctx)->operation == EVP_PKEY_OP_VERIFY \
628 || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \
629 || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER)
631 #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
632 ((ctx)->operation == EVP_PKEY_OP_DERIVE)
634 #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
635 ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \
636 || (ctx)->operation == EVP_PKEY_OP_DECRYPT)
638 #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
639 ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
640 || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
642 void openssl_add_all_ciphers_int(void);
643 void openssl_add_all_digests_int(void);
644 void evp_cleanup_int(void);
645 void evp_app_cleanup_int(void);
646 void *evp_pkey_export_to_provider(EVP_PKEY
*pk
, OPENSSL_CTX
*libctx
,
647 EVP_KEYMGMT
**keymgmt
,
648 const char *propquery
);
650 int evp_pkey_downgrade(EVP_PKEY
*pk
);
651 void evp_pkey_free_legacy(EVP_PKEY
*x
);
655 * KEYMGMT utility functions
659 * Key import structure and helper function, to be used as an export callback
661 struct evp_keymgmt_util_try_import_data_st
{
662 EVP_KEYMGMT
*keymgmt
;
667 int evp_keymgmt_util_try_import(const OSSL_PARAM params
[], void *arg
);
668 int evp_keymgmt_util_assign_pkey(EVP_PKEY
*pkey
, EVP_KEYMGMT
*keymgmt
,
670 EVP_PKEY
*evp_keymgmt_util_make_pkey(EVP_KEYMGMT
*keymgmt
, void *keydata
);
672 void *evp_keymgmt_util_export_to_provider(EVP_PKEY
*pk
, EVP_KEYMGMT
*keymgmt
);
673 size_t evp_keymgmt_util_find_operation_cache_index(EVP_PKEY
*pk
,
674 EVP_KEYMGMT
*keymgmt
);
675 void evp_keymgmt_util_clear_operation_cache(EVP_PKEY
*pk
);
676 int evp_keymgmt_util_cache_keydata(EVP_PKEY
*pk
, size_t index
,
677 EVP_KEYMGMT
*keymgmt
, void *keydata
);
678 void evp_keymgmt_util_cache_keyinfo(EVP_PKEY
*pk
);
679 void *evp_keymgmt_util_fromdata(EVP_PKEY
*target
, EVP_KEYMGMT
*keymgmt
,
680 int selection
, const OSSL_PARAM params
[]);
681 int evp_keymgmt_util_has(EVP_PKEY
*pk
, int selection
);
682 int evp_keymgmt_util_match(EVP_PKEY
*pk1
, EVP_PKEY
*pk2
, int selection
);
683 int evp_keymgmt_util_copy(EVP_PKEY
*to
, EVP_PKEY
*from
, int selection
);
684 void *evp_keymgmt_util_gen(EVP_PKEY
*target
, EVP_KEYMGMT
*keymgmt
,
685 void *genctx
, OSSL_CALLBACK
*cb
, void *cbarg
);
686 int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT
*keymgmt
,
688 char *mdname
, size_t mdname_sz
);
691 * KEYMGMT provider interface functions
693 void *evp_keymgmt_newdata(const EVP_KEYMGMT
*keymgmt
);
694 void evp_keymgmt_freedata(const EVP_KEYMGMT
*keymgmt
, void *keyddata
);
695 int evp_keymgmt_get_params(const EVP_KEYMGMT
*keymgmt
,
696 void *keydata
, OSSL_PARAM params
[]);
697 int evp_keymgmt_set_params(const EVP_KEYMGMT
*keymgmt
,
698 void *keydata
, const OSSL_PARAM params
[]);
699 void *evp_keymgmt_gen_init(const EVP_KEYMGMT
*keymgmt
, int selection
);
700 int evp_keymgmt_gen_set_template(const EVP_KEYMGMT
*keymgmt
, void *genctx
,
702 int evp_keymgmt_gen_set_params(const EVP_KEYMGMT
*keymgmt
, void *genctx
,
703 const OSSL_PARAM params
[]);
704 void *evp_keymgmt_gen(const EVP_KEYMGMT
*keymgmt
, void *genctx
,
705 OSSL_CALLBACK
*cb
, void *cbarg
);
706 void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT
*keymgmt
, void *genctx
);
708 void *evp_keymgmt_load(const EVP_KEYMGMT
*keymgmt
,
709 const void *objref
, size_t objref_sz
);
711 int evp_keymgmt_has(const EVP_KEYMGMT
*keymgmt
, void *keyddata
, int selection
);
712 int evp_keymgmt_validate(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
714 int evp_keymgmt_match(const EVP_KEYMGMT
*keymgmt
,
715 const void *keydata1
, const void *keydata2
,
718 int evp_keymgmt_import(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
719 int selection
, const OSSL_PARAM params
[]);
720 const OSSL_PARAM
*evp_keymgmt_import_types(const EVP_KEYMGMT
*keymgmt
,
722 int evp_keymgmt_export(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
723 int selection
, OSSL_CALLBACK
*param_cb
, void *cbarg
);
724 const OSSL_PARAM
*evp_keymgmt_export_types(const EVP_KEYMGMT
*keymgmt
,
726 int evp_keymgmt_copy(const EVP_KEYMGMT
*keymgmt
,
727 void *keydata_to
, const void *keydata_from
,
730 /* Pulling defines out of C source files */
732 #define EVP_RC4_KEY_SIZE 16
733 #ifndef TLS1_1_VERSION
734 # define TLS1_1_VERSION 0x0302
737 void evp_encode_ctx_set_flags(EVP_ENCODE_CTX
*ctx
, unsigned int flags
);
739 /* EVP_ENCODE_CTX flags */
740 /* Don't generate new lines when encoding */
741 #define EVP_ENCODE_CTX_NO_NEWLINES 1
742 /* Use the SRP base64 alphabet instead of the standard one */
743 #define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2
745 const EVP_CIPHER
*evp_get_cipherbyname_ex(OPENSSL_CTX
*libctx
, const char *name
);
746 const EVP_MD
*evp_get_digestbyname_ex(OPENSSL_CTX
*libctx
, const char *name
);
748 int pkcs5_pbkdf2_hmac_with_libctx(const char *pass
, int passlen
,
749 const unsigned char *salt
, int saltlen
,
750 int iter
, const EVP_MD
*digest
, int keylen
,
752 OPENSSL_CTX
*libctx
, const char *propq
);
756 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
758 * Return 1 on success, 0 or negative for errors.
760 * In particular they return -2 if any of the params is not supported.
762 * They are not available in FIPS_MODULE as they depend on
763 * - EVP_PKEY_CTX_{get,set}_params()
764 * - EVP_PKEY_CTX_{gettable,settable}_params()
767 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
);
768 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX
*ctx
, OSSL_PARAM
*params
);
770 EVP_PKEY
*evp_pkcs82pkey_int(const PKCS8_PRIV_KEY_INFO
*p8
, OPENSSL_CTX
*libctx
,
772 EVP_MD_CTX
*evp_md_ctx_new_with_libctx(EVP_PKEY
*pkey
,
773 const ASN1_OCTET_STRING
*id
,
774 OPENSSL_CTX
*libctx
, const char *propq
);
775 #endif /* !defined(FIPS_MODULE) */
776 void evp_method_store_flush(OPENSSL_CTX
*libctx
);
777 int evp_set_default_properties_int(OPENSSL_CTX
*libctx
, const char *propq
,