2 * Copyright 1995-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
11 * EVP _meth_ APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include "e_os.h" /* strcasecmp */
19 #include "internal/cryptlib.h"
20 #include <openssl/evp.h>
21 #include <openssl/objects.h>
22 #include <openssl/params.h>
23 #include <openssl/core_names.h>
24 #include <openssl/rsa.h>
25 #include <openssl/dh.h>
26 #include <openssl/ec.h>
27 #include "crypto/evp.h"
28 #include "crypto/asn1.h"
29 #include "internal/provider.h"
30 #include "evp_local.h"
32 #if !defined(FIPS_MODULE)
34 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
36 return evp_cipher_param_to_asn1_ex(c
, type
, NULL
);
39 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
41 return evp_cipher_asn1_to_param_ex(c
, type
, NULL
);
44 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX
*ctx
, ASN1_TYPE
*type
)
50 unsigned char iv
[EVP_MAX_IV_LENGTH
];
52 l
= EVP_CIPHER_CTX_iv_length(ctx
);
53 if (!ossl_assert(l
<= sizeof(iv
)))
55 i
= ASN1_TYPE_get_octetstring(type
, iv
, l
);
59 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, NULL
, iv
, -1))
65 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
69 unsigned char *oiv
= NULL
;
72 oiv
= (unsigned char *)EVP_CIPHER_CTX_original_iv(c
);
73 j
= EVP_CIPHER_CTX_iv_length(c
);
74 OPENSSL_assert(j
<= sizeof(c
->iv
));
75 i
= ASN1_TYPE_set_octetstring(type
, oiv
, j
);
80 int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
81 evp_cipher_aead_asn1_params
*asn1_params
)
83 int ret
= -1; /* Assume the worst */
84 const EVP_CIPHER
*cipher
= c
->cipher
;
87 * For legacy implementations, we detect custom AlgorithmIdentifier
88 * parameter handling by checking if the function pointer
89 * cipher->set_asn1_parameters is set. We know that this pointer
90 * is NULL for provided implementations.
92 * Otherwise, for any implementation, we check the flag
93 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
94 * default AI parameter extraction.
96 * Otherwise, for provided implementations, we convert |type| to
97 * a DER encoded blob and pass to the implementation in OSSL_PARAM
100 * If none of the above applies, this operation is unsupported.
102 if (cipher
->set_asn1_parameters
!= NULL
) {
103 ret
= cipher
->set_asn1_parameters(c
, type
);
104 } else if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_CUSTOM_ASN1
) == 0) {
105 switch (EVP_CIPHER_mode(cipher
)) {
106 case EVP_CIPH_WRAP_MODE
:
107 if (EVP_CIPHER_is_a(cipher
, SN_id_smime_alg_CMS3DESwrap
))
108 ASN1_TYPE_set(type
, V_ASN1_NULL
, NULL
);
112 case EVP_CIPH_GCM_MODE
:
113 ret
= evp_cipher_set_asn1_aead_params(c
, type
, asn1_params
);
116 case EVP_CIPH_CCM_MODE
:
117 case EVP_CIPH_XTS_MODE
:
118 case EVP_CIPH_OCB_MODE
:
123 ret
= EVP_CIPHER_set_asn1_iv(c
, type
);
125 } else if (cipher
->prov
!= NULL
) {
126 OSSL_PARAM params
[3], *p
= params
;
127 unsigned char *der
= NULL
, *derp
;
130 * We make two passes, the first to get the appropriate buffer size,
131 * and the second to get the actual value.
133 *p
++ = OSSL_PARAM_construct_octet_string(
134 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS
,
136 *p
= OSSL_PARAM_construct_end();
138 if (!EVP_CIPHER_CTX_get_params(c
, params
))
141 /* ... but, we should get a return size too! */
142 if (OSSL_PARAM_modified(params
)
143 && params
[0].return_size
!= 0
144 && (der
= OPENSSL_malloc(params
[0].return_size
)) != NULL
) {
145 params
[0].data
= der
;
146 params
[0].data_size
= params
[0].return_size
;
147 OSSL_PARAM_set_all_unmodified(params
);
149 if (EVP_CIPHER_CTX_get_params(c
, params
)
150 && OSSL_PARAM_modified(params
)
151 && d2i_ASN1_TYPE(&type
, (const unsigned char **)&derp
,
152 params
[0].return_size
) != NULL
) {
163 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_CIPHER
);
165 ERR_raise(ERR_LIB_EVP
, EVP_R_CIPHER_PARAMETER_ERROR
);
171 int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
172 evp_cipher_aead_asn1_params
*asn1_params
)
174 int ret
= -1; /* Assume the worst */
175 const EVP_CIPHER
*cipher
= c
->cipher
;
178 * For legacy implementations, we detect custom AlgorithmIdentifier
179 * parameter handling by checking if there the function pointer
180 * cipher->get_asn1_parameters is set. We know that this pointer
181 * is NULL for provided implementations.
183 * Otherwise, for any implementation, we check the flag
184 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
185 * default AI parameter creation.
187 * Otherwise, for provided implementations, we get the AI parameter
188 * in DER encoded form from the implementation by requesting the
189 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
191 * If none of the above applies, this operation is unsupported.
193 if (cipher
->get_asn1_parameters
!= NULL
) {
194 ret
= cipher
->get_asn1_parameters(c
, type
);
195 } else if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_CUSTOM_ASN1
) == 0) {
196 switch (EVP_CIPHER_mode(cipher
)) {
197 case EVP_CIPH_WRAP_MODE
:
201 case EVP_CIPH_GCM_MODE
:
202 ret
= evp_cipher_get_asn1_aead_params(c
, type
, asn1_params
);
205 case EVP_CIPH_CCM_MODE
:
206 case EVP_CIPH_XTS_MODE
:
207 case EVP_CIPH_OCB_MODE
:
212 ret
= EVP_CIPHER_get_asn1_iv(c
, type
);
214 } else if (cipher
->prov
!= NULL
) {
215 OSSL_PARAM params
[3], *p
= params
;
216 unsigned char *der
= NULL
;
219 if ((derl
= i2d_ASN1_TYPE(type
, &der
)) >= 0) {
221 OSSL_PARAM_construct_octet_string(
222 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS
,
224 *p
= OSSL_PARAM_construct_end();
225 if (EVP_CIPHER_CTX_set_params(c
, params
))
234 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_CIPHER
);
236 ERR_raise(ERR_LIB_EVP
, EVP_R_CIPHER_PARAMETER_ERROR
);
242 int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
243 evp_cipher_aead_asn1_params
*asn1_params
)
247 unsigned char iv
[EVP_MAX_IV_LENGTH
];
249 if (type
== NULL
|| asn1_params
== NULL
)
252 i
= ossl_asn1_type_get_octetstring_int(type
, &tl
, NULL
, EVP_MAX_IV_LENGTH
);
255 ossl_asn1_type_get_octetstring_int(type
, &tl
, iv
, i
);
257 memcpy(asn1_params
->iv
, iv
, i
);
258 asn1_params
->iv_len
= i
;
263 int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
264 evp_cipher_aead_asn1_params
*asn1_params
)
266 if (type
== NULL
|| asn1_params
== NULL
)
269 return ossl_asn1_type_set_octetstring_int(type
, asn1_params
->tag_len
,
271 asn1_params
->iv_len
);
273 #endif /* !defined(FIPS_MODULE) */
275 /* Convert the various cipher NIDs and dummies to a proper OID NID */
276 int EVP_CIPHER_type(const EVP_CIPHER
*cipher
)
279 nid
= EVP_CIPHER_nid(cipher
);
294 case NID_aes_128_cfb128
:
295 case NID_aes_128_cfb8
:
296 case NID_aes_128_cfb1
:
298 return NID_aes_128_cfb128
;
300 case NID_aes_192_cfb128
:
301 case NID_aes_192_cfb8
:
302 case NID_aes_192_cfb1
:
304 return NID_aes_192_cfb128
;
306 case NID_aes_256_cfb128
:
307 case NID_aes_256_cfb8
:
308 case NID_aes_256_cfb1
:
310 return NID_aes_256_cfb128
;
316 return NID_des_cfb64
;
318 case NID_des_ede3_cfb64
:
319 case NID_des_ede3_cfb8
:
320 case NID_des_ede3_cfb1
:
322 return NID_des_cfb64
;
329 /* Check it has an OID and it is valid */
330 ASN1_OBJECT
*otmp
= OBJ_nid2obj(nid
);
332 if (OBJ_get0_data(otmp
) == NULL
)
334 ASN1_OBJECT_free(otmp
);
341 int evp_cipher_cache_constants(EVP_CIPHER
*cipher
)
343 int ok
, aead
= 0, custom_iv
= 0, cts
= 0, multiblock
= 0;
347 unsigned int mode
= 0;
348 OSSL_PARAM params
[9];
350 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE
, &blksz
);
351 params
[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &ivlen
);
352 params
[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &keylen
);
353 params
[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE
, &mode
);
354 params
[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD
, &aead
);
355 params
[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV
,
357 params
[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS
, &cts
);
358 params
[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK
,
360 params
[8] = OSSL_PARAM_construct_end();
361 ok
= evp_do_ciph_getparams(cipher
, params
);
363 cipher
->block_size
= blksz
;
364 cipher
->iv_len
= ivlen
;
365 cipher
->key_len
= keylen
;
366 cipher
->flags
= mode
;
368 cipher
->flags
|= EVP_CIPH_FLAG_AEAD_CIPHER
;
370 cipher
->flags
|= EVP_CIPH_CUSTOM_IV
;
372 cipher
->flags
|= EVP_CIPH_FLAG_CTS
;
374 cipher
->flags
|= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
;
375 /* Provided implementations may have a custom cipher_cipher */
376 if (cipher
->prov
!= NULL
&& cipher
->ccipher
!= NULL
)
377 cipher
->flags
|= EVP_CIPH_FLAG_CUSTOM_CIPHER
;
378 /* Provided implementations may also have custom ASN1 algorithm parameters */
379 if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher
),
380 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS
))
381 cipher
->flags
|= EVP_CIPH_FLAG_CUSTOM_ASN1
;
386 int EVP_CIPHER_block_size(const EVP_CIPHER
*cipher
)
388 return cipher
->block_size
;
391 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX
*ctx
)
393 return EVP_CIPHER_block_size(ctx
->cipher
);
396 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER
*e
)
401 int EVP_Cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
402 const unsigned char *in
, unsigned int inl
)
404 if (ctx
->cipher
->prov
!= NULL
) {
406 * If the provided implementation has a ccipher function, we use it,
407 * and translate its return value like this: 0 => -1, 1 => outlen
409 * Otherwise, we call the cupdate function if in != NULL, or cfinal
410 * if in == NULL. Regardless of which, we return what we got.
414 size_t blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
416 if (ctx
->cipher
->ccipher
!= NULL
)
417 ret
= ctx
->cipher
->ccipher(ctx
->provctx
, out
, &outl
,
418 inl
+ (blocksize
== 1 ? 0 : blocksize
),
422 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &outl
,
423 inl
+ (blocksize
== 1 ? 0 : blocksize
),
426 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &outl
,
427 blocksize
== 1 ? 0 : blocksize
);
432 return ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
435 #ifndef OPENSSL_NO_DEPRECATED_3_0
436 const EVP_CIPHER
*EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX
*ctx
)
444 const EVP_CIPHER
*EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX
*ctx
)
451 EVP_CIPHER
*EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX
*ctx
)
457 cipher
= (EVP_CIPHER
*)ctx
->cipher
;
458 if (!EVP_CIPHER_up_ref(cipher
))
463 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX
*ctx
)
468 unsigned long EVP_CIPHER_flags(const EVP_CIPHER
*cipher
)
470 return cipher
->flags
;
473 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX
*ctx
)
475 return ctx
->app_data
;
478 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX
*ctx
, void *data
)
480 ctx
->app_data
= data
;
483 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX
*ctx
)
485 return ctx
->cipher_data
;
488 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX
*ctx
, void *cipher_data
)
490 void *old_cipher_data
;
492 old_cipher_data
= ctx
->cipher_data
;
493 ctx
->cipher_data
= cipher_data
;
495 return old_cipher_data
;
498 int EVP_CIPHER_iv_length(const EVP_CIPHER
*cipher
)
500 return cipher
->iv_len
;
503 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX
*ctx
)
505 int rv
, len
= EVP_CIPHER_iv_length(ctx
->cipher
);
507 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
509 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &v
);
510 rv
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
511 if (rv
== EVP_CTRL_RET_UNSUPPORTED
)
513 return rv
!= 0 ? (int)v
: -1;
514 /* Code below to be removed when legacy support is dropped. */
516 if ((EVP_CIPHER_flags(ctx
->cipher
) & EVP_CIPH_CUSTOM_IV_LENGTH
) != 0) {
517 rv
= EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX
*)ctx
, EVP_CTRL_GET_IVLEN
,
519 return (rv
== 1) ? len
: -1;
524 int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX
*ctx
)
528 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
530 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN
, &v
);
531 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
532 return ret
== 1 ? (int)v
: 0;
535 #ifndef OPENSSL_NO_DEPRECATED_3_0
536 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX
*ctx
)
539 const unsigned char *v
= ctx
->oiv
;
540 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
543 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
,
544 (void **)&v
, sizeof(ctx
->oiv
));
545 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
547 return ok
!= 0 ? v
: NULL
;
551 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
553 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX
*ctx
)
556 const unsigned char *v
= ctx
->iv
;
557 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
560 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV
,
561 (void **)&v
, sizeof(ctx
->iv
));
562 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
564 return ok
!= 0 ? v
: NULL
;
567 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX
*ctx
)
570 unsigned char *v
= ctx
->iv
;
571 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
574 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV
,
575 (void **)&v
, sizeof(ctx
->iv
));
576 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
578 return ok
!= 0 ? v
: NULL
;
580 #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
582 int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX
*ctx
, void *buf
, size_t len
)
584 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
587 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV
, buf
, len
);
588 return evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
591 int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX
*ctx
, void *buf
, size_t len
)
593 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
596 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV
, buf
, len
);
597 return evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
600 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX
*ctx
)
605 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX
*ctx
)
608 unsigned int v
= (unsigned int)ctx
->num
;
609 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
611 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &v
);
612 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
614 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
617 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX
*ctx
, int num
)
620 unsigned int n
= (unsigned int)num
;
621 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
623 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &n
);
624 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
631 int EVP_CIPHER_key_length(const EVP_CIPHER
*cipher
)
633 return cipher
->key_len
;
636 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
639 size_t v
= ctx
->key_len
;
640 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
642 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &v
);
643 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
645 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
648 int EVP_CIPHER_nid(const EVP_CIPHER
*cipher
)
653 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX
*ctx
)
655 return ctx
->cipher
->nid
;
658 int EVP_CIPHER_is_a(const EVP_CIPHER
*cipher
, const char *name
)
660 if (cipher
->prov
!= NULL
)
661 return evp_is_a(cipher
->prov
, cipher
->name_id
, NULL
, name
);
662 return evp_is_a(NULL
, 0, EVP_CIPHER_name(cipher
), name
);
665 int EVP_CIPHER_number(const EVP_CIPHER
*cipher
)
667 return cipher
->name_id
;
670 const char *EVP_CIPHER_name(const EVP_CIPHER
*cipher
)
672 if (cipher
->type_name
!= NULL
)
673 return cipher
->type_name
;
675 return OBJ_nid2sn(EVP_CIPHER_nid(cipher
));
681 const char *EVP_CIPHER_description(const EVP_CIPHER
*cipher
)
683 if (cipher
->description
!= NULL
)
684 return cipher
->description
;
686 return OBJ_nid2ln(EVP_CIPHER_nid(cipher
));
692 int EVP_CIPHER_names_do_all(const EVP_CIPHER
*cipher
,
693 void (*fn
)(const char *name
, void *data
),
696 if (cipher
->prov
!= NULL
)
697 return evp_names_do_all(cipher
->prov
, cipher
->name_id
, fn
, data
);
702 const OSSL_PROVIDER
*EVP_CIPHER_provider(const EVP_CIPHER
*cipher
)
707 int EVP_CIPHER_mode(const EVP_CIPHER
*cipher
)
709 return EVP_CIPHER_flags(cipher
) & EVP_CIPH_MODE
;
712 int EVP_MD_is_a(const EVP_MD
*md
, const char *name
)
714 if (md
->prov
!= NULL
)
715 return evp_is_a(md
->prov
, md
->name_id
, NULL
, name
);
716 return evp_is_a(NULL
, 0, EVP_MD_name(md
), name
);
719 int EVP_MD_number(const EVP_MD
*md
)
724 const char *EVP_MD_description(const EVP_MD
*md
)
726 if (md
->description
!= NULL
)
727 return md
->description
;
729 return OBJ_nid2ln(EVP_MD_nid(md
));
735 const char *EVP_MD_name(const EVP_MD
*md
)
739 if (md
->type_name
!= NULL
)
740 return md
->type_name
;
742 return OBJ_nid2sn(EVP_MD_nid(md
));
748 int EVP_MD_names_do_all(const EVP_MD
*md
,
749 void (*fn
)(const char *name
, void *data
),
752 if (md
->prov
!= NULL
)
753 return evp_names_do_all(md
->prov
, md
->name_id
, fn
, data
);
758 const OSSL_PROVIDER
*EVP_MD_provider(const EVP_MD
*md
)
763 int EVP_MD_type(const EVP_MD
*md
)
768 int EVP_MD_pkey_type(const EVP_MD
*md
)
770 return md
->pkey_type
;
773 int EVP_MD_block_size(const EVP_MD
*md
)
776 ERR_raise(ERR_LIB_EVP
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
779 return md
->block_size
;
782 int EVP_MD_size(const EVP_MD
*md
)
785 ERR_raise(ERR_LIB_EVP
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
791 unsigned long EVP_MD_flags(const EVP_MD
*md
)
796 EVP_MD
*EVP_MD_meth_new(int md_type
, int pkey_type
)
798 EVP_MD
*md
= evp_md_new();
802 md
->pkey_type
= pkey_type
;
803 md
->origin
= EVP_ORIG_METH
;
808 EVP_MD
*EVP_MD_meth_dup(const EVP_MD
*md
)
813 * Non-legacy EVP_MDs can't be duplicated like this.
814 * Use EVP_MD_up_ref() instead.
816 if (md
->prov
!= NULL
)
819 if ((to
= EVP_MD_meth_new(md
->type
, md
->pkey_type
)) != NULL
) {
820 CRYPTO_RWLOCK
*lock
= to
->lock
;
822 memcpy(to
, md
, sizeof(*to
));
828 void evp_md_free_int(EVP_MD
*md
)
830 OPENSSL_free(md
->type_name
);
831 ossl_provider_free(md
->prov
);
832 CRYPTO_THREAD_lock_free(md
->lock
);
836 void EVP_MD_meth_free(EVP_MD
*md
)
838 if (md
== NULL
|| md
->origin
!= EVP_ORIG_METH
)
844 int EVP_MD_meth_set_input_blocksize(EVP_MD
*md
, int blocksize
)
846 if (md
->block_size
!= 0)
849 md
->block_size
= blocksize
;
852 int EVP_MD_meth_set_result_size(EVP_MD
*md
, int resultsize
)
854 if (md
->md_size
!= 0)
857 md
->md_size
= resultsize
;
860 int EVP_MD_meth_set_app_datasize(EVP_MD
*md
, int datasize
)
862 if (md
->ctx_size
!= 0)
865 md
->ctx_size
= datasize
;
868 int EVP_MD_meth_set_flags(EVP_MD
*md
, unsigned long flags
)
876 int EVP_MD_meth_set_init(EVP_MD
*md
, int (*init
)(EVP_MD_CTX
*ctx
))
878 if (md
->init
!= NULL
)
884 int EVP_MD_meth_set_update(EVP_MD
*md
, int (*update
)(EVP_MD_CTX
*ctx
,
888 if (md
->update
!= NULL
)
894 int EVP_MD_meth_set_final(EVP_MD
*md
, int (*final
)(EVP_MD_CTX
*ctx
,
897 if (md
->final
!= NULL
)
903 int EVP_MD_meth_set_copy(EVP_MD
*md
, int (*copy
)(EVP_MD_CTX
*to
,
904 const EVP_MD_CTX
*from
))
906 if (md
->copy
!= NULL
)
912 int EVP_MD_meth_set_cleanup(EVP_MD
*md
, int (*cleanup
)(EVP_MD_CTX
*ctx
))
914 if (md
->cleanup
!= NULL
)
917 md
->cleanup
= cleanup
;
920 int EVP_MD_meth_set_ctrl(EVP_MD
*md
, int (*ctrl
)(EVP_MD_CTX
*ctx
, int cmd
,
923 if (md
->md_ctrl
!= NULL
)
930 int EVP_MD_meth_get_input_blocksize(const EVP_MD
*md
)
932 return md
->block_size
;
934 int EVP_MD_meth_get_result_size(const EVP_MD
*md
)
938 int EVP_MD_meth_get_app_datasize(const EVP_MD
*md
)
942 unsigned long EVP_MD_meth_get_flags(const EVP_MD
*md
)
946 int (*EVP_MD_meth_get_init(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
950 int (*EVP_MD_meth_get_update(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
956 int (*EVP_MD_meth_get_final(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
961 int (*EVP_MD_meth_get_copy(const EVP_MD
*md
))(EVP_MD_CTX
*to
,
962 const EVP_MD_CTX
*from
)
966 int (*EVP_MD_meth_get_cleanup(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
970 int (*EVP_MD_meth_get_ctrl(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
, int cmd
,
976 #ifndef OPENSSL_NO_DEPRECATED_3_0
977 const EVP_MD
*EVP_MD_CTX_md(const EVP_MD_CTX
*ctx
)
981 return ctx
->reqdigest
;
985 const EVP_MD
*EVP_MD_CTX_get0_md(const EVP_MD_CTX
*ctx
)
989 return ctx
->reqdigest
;
992 EVP_MD
*EVP_MD_CTX_get1_md(EVP_MD_CTX
*ctx
)
998 md
= (EVP_MD
*)ctx
->reqdigest
;
999 if (!EVP_MD_up_ref(md
))
1004 EVP_PKEY_CTX
*EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX
*ctx
)
1009 #if !defined(FIPS_MODULE)
1010 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
1011 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX
*ctx
, EVP_PKEY_CTX
*pctx
)
1014 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
1015 * we have to deal with the cleanup job here.
1017 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
))
1018 EVP_PKEY_CTX_free(ctx
->pctx
);
1023 /* make sure pctx is not freed when destroying EVP_MD_CTX */
1024 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
1026 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
1029 #endif /* !defined(FIPS_MODULE) */
1031 void *EVP_MD_CTX_md_data(const EVP_MD_CTX
*ctx
)
1033 return ctx
->md_data
;
1036 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX
*ctx
))(EVP_MD_CTX
*ctx
,
1037 const void *data
, size_t count
)
1042 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX
*ctx
,
1043 int (*update
) (EVP_MD_CTX
*ctx
,
1044 const void *data
, size_t count
))
1046 ctx
->update
= update
;
1049 void EVP_MD_CTX_set_flags(EVP_MD_CTX
*ctx
, int flags
)
1051 ctx
->flags
|= flags
;
1054 void EVP_MD_CTX_clear_flags(EVP_MD_CTX
*ctx
, int flags
)
1056 ctx
->flags
&= ~flags
;
1059 int EVP_MD_CTX_test_flags(const EVP_MD_CTX
*ctx
, int flags
)
1061 return (ctx
->flags
& flags
);
1064 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
1066 ctx
->flags
|= flags
;
1069 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
1071 ctx
->flags
&= ~flags
;
1074 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX
*ctx
, int flags
)
1076 return (ctx
->flags
& flags
);
1079 int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX
*ctx
, const char *name
)
1081 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1083 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1084 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1085 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1092 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
1094 return EVP_PKEY_CTX_set_params(ctx
, params
);
1097 int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX
*ctx
, char *name
, size_t namelen
)
1099 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1100 OSSL_PARAM
*p
= params
;
1102 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1103 /* There is no legacy support for this */
1104 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1105 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1112 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
1114 if (!EVP_PKEY_CTX_get_params(ctx
, params
))
1120 * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
1121 * while providing a generic way of generating a new asymmetric key pair
1122 * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
1123 * The library context I<libctx> and property query I<propq>
1124 * are used when fetching algorithms from providers.
1125 * The I<params> specify algorithm-specific parameters
1126 * such as the RSA modulus size or the name of an EC curve.
1128 static EVP_PKEY
*evp_pkey_keygen(OSSL_LIB_CTX
*libctx
, const char *name
,
1129 const char *propq
, OSSL_PARAM
*params
)
1131 EVP_PKEY
*pkey
= NULL
;
1132 EVP_PKEY_CTX
*ctx
= EVP_PKEY_CTX_new_from_name(libctx
, name
, propq
);
1135 && EVP_PKEY_keygen_init(ctx
) > 0
1136 && EVP_PKEY_CTX_set_params(ctx
, params
))
1137 (void)EVP_PKEY_generate(ctx
, &pkey
);
1139 EVP_PKEY_CTX_free(ctx
);
1143 EVP_PKEY
*EVP_PKEY_Q_keygen(OSSL_LIB_CTX
*libctx
, const char *propq
,
1144 const char *type
, ...)
1149 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1150 EVP_PKEY
*ret
= NULL
;
1152 va_start(args
, type
);
1154 if (strcasecmp(type
, "RSA") == 0) {
1155 bits
= va_arg(args
, size_t);
1156 params
[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS
, &bits
);
1157 } else if (strcasecmp(type
, "EC") == 0) {
1158 name
= va_arg(args
, char *);
1159 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
1161 } else if (strcasecmp(type
, "ED25519") != 0
1162 && strcasecmp(type
, "X25519") != 0
1163 && strcasecmp(type
, "ED448") != 0
1164 && strcasecmp(type
, "X448") != 0) {
1165 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_INVALID_ARGUMENT
);
1168 ret
= evp_pkey_keygen(libctx
, type
, propq
, params
);