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
;
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
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 */
70 /* Indicators of what has been set. Keep them together! */
71 unsigned int dist_id_set
: 1;
74 /* Application specific data, usually used by the callback */
77 EVP_PKEY_gen_cb
*pkey_gencb
;
78 /* implementation specific keygen data */
80 int keygen_info_count
;
82 /* Legacy fields below */
84 /* EVP_PKEY identity */
86 /* Method associated with this operation */
87 const EVP_PKEY_METHOD
*pmeth
;
88 /* Engine that implements this method or NULL if builtin */
90 /* Key: may be NULL */
92 /* Peer key for key agreement, may be NULL */
94 /* Algorithm specific data */
96 /* Indicator if digest_custom needs to be called */
97 unsigned int flag_call_digest_custom
:1;
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.
105 } /* EVP_PKEY_CTX */ ;
107 #define EVP_PKEY_FLAG_DYNAMIC 1
109 struct evp_pkey_method_st
{
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
,
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
,
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
,
151 int (*check
) (EVP_PKEY
*pkey
);
152 int (*public_check
) (EVP_PKEY
*pkey
);
153 int (*param_check
) (EVP_PKEY
*pkey
);
155 int (*digest_custom
) (EVP_PKEY_CTX
*ctx
, EVP_MD_CTX
*mctx
);
156 } /* EVP_PKEY_METHOD */ ;
158 DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD
)
160 void evp_pkey_set_cb_translate(BN_GENCB
*cb
, EVP_PKEY_CTX
*ctx
);
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);
177 CRYPTO_REF_COUNT refcnt
;
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
;
198 CRYPTO_REF_COUNT refcnt
;
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
;
218 /* Legacy structure members */
219 /* TODO(3.0): Remove these */
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
);
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
);
233 /* New structure members */
234 /* TODO(3.0): Remove above comment when legacy has gone */
237 CRYPTO_REF_COUNT refcnt
;
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
;
255 struct evp_cipher_st
{
259 /* Default value for variable length ciphers */
263 /* Legacy structure members */
264 /* TODO(3.0): Remove these */
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
);
274 int (*cleanup
) (EVP_CIPHER_CTX
*);
275 /* how big ctx->cipher_data needs to be */
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 */
286 /* New structure members */
287 /* TODO(3.0): Remove above comment when legacy has gone */
290 CRYPTO_REF_COUNT refcnt
;
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
;
308 /* Macros to code block cipher wrappers */
310 /* Wrapper functions for each cipher mode */
312 #define EVP_C_DATA(kstruct, ctx) \
313 ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
315 #define BLOCK_CIPHER_ecb_loop() \
317 bl = EVP_CIPHER_CTX_cipher(ctx)->block_size; \
318 if (inl < bl) return 1;\
320 for (i=0; i <= inl; i+=bl)
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) \
325 BLOCK_CIPHER_ecb_loop() \
326 cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct,ctx)->ksched, EVP_CIPHER_CTX_encrypting(ctx)); \
330 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
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) \
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);\
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);\
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) \
354 while(inl>=EVP_MAXCHUNK) \
356 cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
362 cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct,ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_encrypting(ctx));\
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) \
369 size_t chunk = EVP_MAXCHUNK;\
370 if (cbits == 1) chunk >>= 3;\
371 if (inl < chunk) chunk = inl;\
372 while (inl && inl >= chunk)\
374 int num = EVP_CIPHER_CTX_num(ctx);\
375 cprefix##_cfb##cbits##_encrypt(in, out, (long) \
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);\
385 if (inl < chunk) chunk = inl;\
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)
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, \
403 cname##_##mode##_cipher, \
410 const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
412 #define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, \
413 iv_len, flags, init_key, cleanup, set_asn1, \
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)
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, \
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, \
432 #define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, \
433 flags, init_key, cleanup, set_asn1, \
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)
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)
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,\
460 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
461 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
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,\
473 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
474 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
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,\
486 sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
487 sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
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,\
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##_ecb(void) { return &cname##_ecb; }
508 #define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid, \
509 block_size, key_len, iv_len, cbits, \
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, \
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)
525 unsigned char iv
[EVP_MAX_IV_LENGTH
];
527 unsigned int tag_len
;
528 } evp_cipher_aead_asn1_params
;
530 int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
531 evp_cipher_aead_asn1_params
*params
);
533 int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
534 evp_cipher_aead_asn1_params
*params
);
537 * An EVP_PKEY can have the following states:
541 * type == EVP_PKEY_NONE && keymgmt == NULL
545 * (type != EVP_PKEY_NONE && pkey.ptr == NULL) ## legacy (libcrypto only)
546 * || (keymgmt != NULL && keydata == NULL) ## provider side
550 * (type != EVP_PKEY_NONE && pkey.ptr != NULL) ## legacy (libcrypto only)
551 * || (keymgmt != NULL && keydata != NULL) ## provider side
553 * The easiest way to detect a legacy key is:
555 * keymgmt == NULL && type != EVP_PKEY_NONE
557 * The easiest way to detect a provider side key is:
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)
573 /* == Legacy attributes == */
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.
582 const EVP_PKEY_ASN1_METHOD
*ameth
;
584 ENGINE
*pmeth_engine
; /* If not NULL public key ENGINE to use */
587 # ifndef OPENSSL_NO_RSA
588 struct rsa_st
*rsa
; /* RSA */
590 # ifndef OPENSSL_NO_DSA
591 struct dsa_st
*dsa
; /* DSA */
593 # ifndef OPENSSL_NO_DH
594 struct dh_st
*dh
; /* DH */
596 # ifndef OPENSSL_NO_EC
597 struct ec_key_st
*ec
; /* ECC */
598 ECX_KEY
*ecx
; /* X25519, X448, Ed25519, Ed448 */
603 /* == Common attributes == */
604 /* If these are modified, so must evp_pkey_downgrade() */
605 CRYPTO_REF_COUNT references
;
607 STACK_OF(X509_ATTRIBUTE
) *attributes
; /* [ 0 ] */
610 CRYPTO_EX_DATA ex_data
;
613 /* == Provider attributes == */
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.
620 EVP_KEYMGMT
*keymgmt
;
623 * If any libcrypto code does anything that may modify the keydata
624 * contents, this dirty counter must be incremented.
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.
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
643 EVP_KEYMGMT
*keymgmt
;
645 } operation_cache
[10];
647 * We keep a copy of that "origin"'s dirty count, so we know if the
648 * operation cache needs flushing.
650 size_t dirty_cnt_copy
;
652 /* Cache of key object information */
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)
667 #define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
668 ((ctx)->operation == EVP_PKEY_OP_DERIVE)
670 #define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) \
671 ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \
672 || (ctx)->operation == EVP_PKEY_OP_DECRYPT)
674 #define EVP_PKEY_CTX_IS_GEN_OP(ctx) \
675 ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
676 || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
678 #define EVP_PKEY_CTX_IS_KEM_OP(ctx) \
679 ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \
680 || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE)
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
);
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
);
696 * KEYMGMT utility functions
700 * Key import structure and helper function, to be used as an export callback
702 struct evp_keymgmt_util_try_import_data_st
{
703 EVP_KEYMGMT
*keymgmt
;
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
,
711 EVP_PKEY
*evp_keymgmt_util_make_pkey(EVP_KEYMGMT
*keymgmt
, void *keydata
);
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
,
731 char *mdname
, size_t mdname_sz
);
734 * KEYMGMT provider interface functions
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
,
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
);
751 void *evp_keymgmt_load(const EVP_KEYMGMT
*keymgmt
,
752 const void *objref
, size_t objref_sz
);
754 int evp_keymgmt_has(const EVP_KEYMGMT
*keymgmt
, void *keyddata
, int selection
);
755 int evp_keymgmt_validate(const EVP_KEYMGMT
*keymgmt
, void *keydata
,
757 int evp_keymgmt_match(const EVP_KEYMGMT
*keymgmt
,
758 const void *keydata1
, const void *keydata2
,
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
,
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
,
769 int evp_keymgmt_copy(const EVP_KEYMGMT
*keymgmt
,
770 void *keydata_to
, const void *keydata_from
,
773 /* Pulling defines out of C source files */
775 #define EVP_RC4_KEY_SIZE 16
776 #ifndef TLS1_1_VERSION
777 # define TLS1_1_VERSION 0x0302
780 void evp_encode_ctx_set_flags(EVP_ENCODE_CTX
*ctx
, unsigned int flags
);
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
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
);
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
,
795 OPENSSL_CTX
*libctx
, const char *propq
);
799 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
801 * Return 1 on success, 0 or negative for errors.
803 * In particular they return -2 if any of the params is not supported.
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()
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
);
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
);
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
);
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
,
828 void evp_md_ctx_clear_digest(EVP_MD_CTX
*ctx
, int force
);