2 * Copyright 1995-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
11 * EVP _meth_ APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/evp.h>
19 #include <openssl/objects.h>
20 #include <openssl/params.h>
21 #include <openssl/core_names.h>
22 #include <openssl/dh.h>
23 #include <openssl/ec.h>
24 #include "crypto/evp.h"
25 #include "internal/provider.h"
26 #include "evp_local.h"
28 #if !defined(FIPS_MODULE)
29 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
31 int ret
= -1; /* Assume the worst */
32 const EVP_CIPHER
*cipher
= c
->cipher
;
35 * For legacy implementations, we detect custom AlgorithmIdentifier
36 * parameter handling by checking if the function pointer
37 * cipher->set_asn1_parameters is set. We know that this pointer
38 * is NULL for provided implementations.
40 * Otherwise, for any implementation, we check the flag
41 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
42 * default AI parameter extraction.
44 * Otherwise, for provided implementations, we convert |type| to
45 * a DER encoded blob and pass to the implementation in OSSL_PARAM
48 * If none of the above applies, this operation is unsupported.
50 if (cipher
->set_asn1_parameters
!= NULL
) {
51 ret
= cipher
->set_asn1_parameters(c
, type
);
52 } else if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_CUSTOM_ASN1
) == 0) {
53 switch (EVP_CIPHER_mode(cipher
)) {
54 case EVP_CIPH_WRAP_MODE
:
55 if (EVP_CIPHER_is_a(cipher
, SN_id_smime_alg_CMS3DESwrap
))
56 ASN1_TYPE_set(type
, V_ASN1_NULL
, NULL
);
60 case EVP_CIPH_GCM_MODE
:
61 case EVP_CIPH_CCM_MODE
:
62 case EVP_CIPH_XTS_MODE
:
63 case EVP_CIPH_OCB_MODE
:
68 ret
= EVP_CIPHER_set_asn1_iv(c
, type
);
70 } else if (cipher
->prov
!= NULL
) {
71 OSSL_PARAM params
[3], *p
= params
;
72 unsigned char *der
= NULL
, *derp
;
75 * We make two passes, the first to get the appropriate buffer size,
76 * and the second to get the actual value.
78 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID
,
80 *p
= OSSL_PARAM_construct_end();
82 if (!EVP_CIPHER_CTX_get_params(c
, params
))
85 /* ... but, we should get a return size too! */
86 if (OSSL_PARAM_modified(params
)
87 && params
[0].return_size
!= 0
88 && (der
= OPENSSL_malloc(params
[0].return_size
)) != NULL
) {
90 params
[0].data_size
= params
[0].return_size
;
91 OSSL_PARAM_set_all_unmodified(params
);
93 if (EVP_CIPHER_CTX_get_params(c
, params
)
94 && OSSL_PARAM_modified(params
)
95 && d2i_ASN1_TYPE(&type
, (const unsigned char **)&derp
,
96 params
[0].return_size
) != NULL
) {
107 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1
, ASN1_R_UNSUPPORTED_CIPHER
);
109 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1
, EVP_R_CIPHER_PARAMETER_ERROR
);
115 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
117 int ret
= -1; /* Assume the worst */
118 const EVP_CIPHER
*cipher
= c
->cipher
;
121 * For legacy implementations, we detect custom AlgorithmIdentifier
122 * parameter handling by checking if there the function pointer
123 * cipher->get_asn1_parameters is set. We know that this pointer
124 * is NULL for provided implementations.
126 * Otherwise, for any implementation, we check the flag
127 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
128 * default AI parameter creation.
130 * Otherwise, for provided implementations, we get the AI parameter
131 * in DER encoded form from the implementation by requesting the
132 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
134 * If none of the above applies, this operation is unsupported.
136 if (cipher
->get_asn1_parameters
!= NULL
) {
137 ret
= cipher
->get_asn1_parameters(c
, type
);
138 } else if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_CUSTOM_ASN1
) == 0) {
139 switch (EVP_CIPHER_mode(cipher
)) {
140 case EVP_CIPH_WRAP_MODE
:
144 case EVP_CIPH_GCM_MODE
:
145 case EVP_CIPH_CCM_MODE
:
146 case EVP_CIPH_XTS_MODE
:
147 case EVP_CIPH_OCB_MODE
:
152 ret
= EVP_CIPHER_get_asn1_iv(c
, type
);
154 } else if (cipher
->prov
!= NULL
) {
155 OSSL_PARAM params
[3], *p
= params
;
156 unsigned char *der
= NULL
;
159 if ((derl
= i2d_ASN1_TYPE(type
, &der
)) >= 0) {
161 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID
,
163 *p
= OSSL_PARAM_construct_end();
164 if (EVP_CIPHER_CTX_set_params(c
, params
))
173 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM
, EVP_R_UNSUPPORTED_CIPHER
);
175 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM
, EVP_R_CIPHER_PARAMETER_ERROR
);
181 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX
*ctx
, ASN1_TYPE
*type
)
187 unsigned char iv
[EVP_MAX_IV_LENGTH
];
189 l
= EVP_CIPHER_CTX_iv_length(ctx
);
190 if (!ossl_assert(l
<= sizeof(iv
)))
192 i
= ASN1_TYPE_get_octetstring(type
, iv
, l
);
196 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, NULL
, iv
, -1))
202 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
206 unsigned char *oiv
= NULL
;
209 oiv
= (unsigned char *)EVP_CIPHER_CTX_original_iv(c
);
210 j
= EVP_CIPHER_CTX_iv_length(c
);
211 OPENSSL_assert(j
<= sizeof(c
->iv
));
212 i
= ASN1_TYPE_set_octetstring(type
, oiv
, j
);
216 #endif /* !defined(FIPS_MODULE) */
218 /* Convert the various cipher NIDs and dummies to a proper OID NID */
219 int EVP_CIPHER_type(const EVP_CIPHER
*ctx
)
222 nid
= EVP_CIPHER_nid(ctx
);
237 case NID_aes_128_cfb128
:
238 case NID_aes_128_cfb8
:
239 case NID_aes_128_cfb1
:
241 return NID_aes_128_cfb128
;
243 case NID_aes_192_cfb128
:
244 case NID_aes_192_cfb8
:
245 case NID_aes_192_cfb1
:
247 return NID_aes_192_cfb128
;
249 case NID_aes_256_cfb128
:
250 case NID_aes_256_cfb8
:
251 case NID_aes_256_cfb1
:
253 return NID_aes_256_cfb128
;
259 return NID_des_cfb64
;
261 case NID_des_ede3_cfb64
:
262 case NID_des_ede3_cfb8
:
263 case NID_des_ede3_cfb1
:
265 return NID_des_cfb64
;
272 /* Check it has an OID and it is valid */
273 ASN1_OBJECT
*otmp
= OBJ_nid2obj(nid
);
275 if (OBJ_get0_data(otmp
) == NULL
)
277 ASN1_OBJECT_free(otmp
);
284 int evp_cipher_cache_constants(EVP_CIPHER
*cipher
)
290 unsigned int mode
= 0;
291 unsigned long flags
= 0;
292 OSSL_PARAM params
[6];
294 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE
, &blksz
);
295 params
[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &ivlen
);
296 params
[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &keylen
);
297 params
[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE
, &mode
);
298 params
[4] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS
, &flags
);
299 params
[5] = OSSL_PARAM_construct_end();
300 ok
= evp_do_ciph_getparams(cipher
, params
);
302 /* Provided implementations may have a custom cipher_cipher */
303 if (cipher
->prov
!= NULL
&& cipher
->ccipher
!= NULL
)
304 flags
|= EVP_CIPH_FLAG_CUSTOM_CIPHER
;
305 cipher
->block_size
= blksz
;
306 cipher
->iv_len
= ivlen
;
307 cipher
->key_len
= keylen
;
308 cipher
->flags
= flags
| mode
;
313 int EVP_CIPHER_block_size(const EVP_CIPHER
*cipher
)
315 return cipher
->block_size
;
318 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX
*ctx
)
320 return EVP_CIPHER_block_size(ctx
->cipher
);
323 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER
*e
)
328 int EVP_Cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
329 const unsigned char *in
, unsigned int inl
)
331 if (ctx
->cipher
->prov
!= NULL
) {
333 * If the provided implementation has a ccipher function, we use it,
334 * and translate its return value like this: 0 => -1, 1 => outlen
336 * Otherwise, we call the cupdate function if in != NULL, or cfinal
337 * if in == NULL. Regardless of which, we return what we got.
341 size_t blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
343 if (ctx
->cipher
->ccipher
!= NULL
)
344 ret
= ctx
->cipher
->ccipher(ctx
->provctx
, out
, &outl
,
345 inl
+ (blocksize
== 1 ? 0 : blocksize
),
349 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &outl
,
350 inl
+ (blocksize
== 1 ? 0 : blocksize
),
353 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &outl
,
354 blocksize
== 1 ? 0 : blocksize
);
359 return ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
362 const EVP_CIPHER
*EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX
*ctx
)
367 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX
*ctx
)
372 unsigned long EVP_CIPHER_flags(const EVP_CIPHER
*cipher
)
374 return cipher
->flags
;
377 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX
*ctx
)
379 return ctx
->app_data
;
382 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX
*ctx
, void *data
)
384 ctx
->app_data
= data
;
387 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX
*ctx
)
389 return ctx
->cipher_data
;
392 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX
*ctx
, void *cipher_data
)
394 void *old_cipher_data
;
396 old_cipher_data
= ctx
->cipher_data
;
397 ctx
->cipher_data
= cipher_data
;
399 return old_cipher_data
;
402 int EVP_CIPHER_iv_length(const EVP_CIPHER
*cipher
)
404 return cipher
->iv_len
;
407 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX
*ctx
)
409 int rv
, len
= EVP_CIPHER_iv_length(ctx
->cipher
);
411 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
413 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &v
);
414 rv
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
415 if (rv
== EVP_CTRL_RET_UNSUPPORTED
)
417 return rv
!= 0 ? (int)v
: -1;
418 /* TODO (3.0) Remove legacy support */
420 if ((EVP_CIPHER_flags(ctx
->cipher
) & EVP_CIPH_CUSTOM_IV_LENGTH
) != 0) {
421 rv
= EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX
*)ctx
, EVP_CTRL_GET_IVLEN
,
423 return (rv
== 1) ? len
: -1;
428 int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX
*ctx
)
432 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
434 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN
, &v
);
435 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
436 return ret
== 1 ? (int)v
: 0;
439 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX
*ctx
)
442 const unsigned char *v
= ctx
->oiv
;
443 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
446 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
,
447 (void **)&v
, sizeof(ctx
->oiv
));
448 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
450 return ok
!= 0 ? v
: NULL
;
454 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
456 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX
*ctx
)
459 const unsigned char *v
= ctx
->iv
;
460 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
463 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
, (void **)&v
,
465 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
467 return ok
!= 0 ? v
: NULL
;
470 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX
*ctx
)
473 unsigned char *v
= ctx
->iv
;
474 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
477 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
, (void **)&v
,
479 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
481 return ok
!= 0 ? v
: NULL
;
484 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX
*ctx
)
489 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX
*ctx
)
492 unsigned int v
= (unsigned int)ctx
->num
;
493 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
495 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &v
);
496 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
498 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
501 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX
*ctx
, int num
)
504 unsigned int n
= (unsigned int)num
;
505 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
507 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &n
);
508 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
515 int EVP_CIPHER_key_length(const EVP_CIPHER
*cipher
)
517 return cipher
->key_len
;
520 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
523 size_t v
= ctx
->key_len
;
524 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
526 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &v
);
527 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
529 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
532 int EVP_CIPHER_nid(const EVP_CIPHER
*cipher
)
537 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX
*ctx
)
539 return ctx
->cipher
->nid
;
542 int EVP_CIPHER_is_a(const EVP_CIPHER
*cipher
, const char *name
)
544 if (cipher
->prov
!= NULL
)
545 return evp_is_a(cipher
->prov
, cipher
->name_id
, NULL
, name
);
546 return evp_is_a(NULL
, 0, EVP_CIPHER_name(cipher
), name
);
549 int EVP_CIPHER_number(const EVP_CIPHER
*cipher
)
551 return cipher
->name_id
;
554 const char *EVP_CIPHER_name(const EVP_CIPHER
*cipher
)
556 if (cipher
->prov
!= NULL
)
557 return evp_first_name(cipher
->prov
, cipher
->name_id
);
559 return OBJ_nid2sn(EVP_CIPHER_nid(cipher
));
565 void EVP_CIPHER_names_do_all(const EVP_CIPHER
*cipher
,
566 void (*fn
)(const char *name
, void *data
),
569 if (cipher
->prov
!= NULL
)
570 evp_names_do_all(cipher
->prov
, cipher
->name_id
, fn
, data
);
573 const OSSL_PROVIDER
*EVP_CIPHER_provider(const EVP_CIPHER
*cipher
)
578 int EVP_CIPHER_mode(const EVP_CIPHER
*cipher
)
580 return EVP_CIPHER_flags(cipher
) & EVP_CIPH_MODE
;
583 int EVP_MD_is_a(const EVP_MD
*md
, const char *name
)
585 if (md
->prov
!= NULL
)
586 return evp_is_a(md
->prov
, md
->name_id
, NULL
, name
);
587 return evp_is_a(NULL
, 0, EVP_MD_name(md
), name
);
590 int EVP_MD_number(const EVP_MD
*md
)
595 const char *EVP_MD_name(const EVP_MD
*md
)
597 if (md
->prov
!= NULL
)
598 return evp_first_name(md
->prov
, md
->name_id
);
600 return OBJ_nid2sn(EVP_MD_nid(md
));
606 void EVP_MD_names_do_all(const EVP_MD
*md
,
607 void (*fn
)(const char *name
, void *data
),
610 if (md
->prov
!= NULL
)
611 evp_names_do_all(md
->prov
, md
->name_id
, fn
, data
);
614 const OSSL_PROVIDER
*EVP_MD_provider(const EVP_MD
*md
)
619 int EVP_MD_block_size(const EVP_MD
*md
)
622 size_t v
= md
->block_size
;
623 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
626 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
630 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE
, &v
);
631 ok
= evp_do_md_getparams(md
, params
);
633 return ok
!= 0 ? (int)v
: -1;
636 int EVP_MD_type(const EVP_MD
*md
)
641 int EVP_MD_pkey_type(const EVP_MD
*md
)
643 return md
->pkey_type
;
646 int EVP_MD_size(const EVP_MD
*md
)
649 size_t v
= md
->md_size
;
650 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
653 EVPerr(EVP_F_EVP_MD_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
657 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE
, &v
);
658 ok
= evp_do_md_getparams(md
, params
);
660 return ok
!= 0 ? (int)v
: -1;
663 unsigned long EVP_MD_flags(const EVP_MD
*md
)
666 unsigned long v
= md
->flags
;
667 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
669 params
[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS
, &v
);
670 ok
= evp_do_md_getparams(md
, params
);
672 return ok
!= 0 ? v
: 0;
675 EVP_MD
*EVP_MD_meth_new(int md_type
, int pkey_type
)
677 EVP_MD
*md
= evp_md_new();
681 md
->pkey_type
= pkey_type
;
686 EVP_MD
*EVP_MD_meth_dup(const EVP_MD
*md
)
691 * Non-legacy EVP_MDs can't be duplicated like this.
692 * Use EVP_MD_up_ref() instead.
694 if (md
->prov
!= NULL
)
697 if ((to
= EVP_MD_meth_new(md
->type
, md
->pkey_type
)) != NULL
) {
698 CRYPTO_RWLOCK
*lock
= to
->lock
;
700 memcpy(to
, md
, sizeof(*to
));
706 void EVP_MD_meth_free(EVP_MD
*md
)
710 int EVP_MD_meth_set_input_blocksize(EVP_MD
*md
, int blocksize
)
712 if (md
->block_size
!= 0)
715 md
->block_size
= blocksize
;
718 int EVP_MD_meth_set_result_size(EVP_MD
*md
, int resultsize
)
720 if (md
->md_size
!= 0)
723 md
->md_size
= resultsize
;
726 int EVP_MD_meth_set_app_datasize(EVP_MD
*md
, int datasize
)
728 if (md
->ctx_size
!= 0)
731 md
->ctx_size
= datasize
;
734 int EVP_MD_meth_set_flags(EVP_MD
*md
, unsigned long flags
)
742 int EVP_MD_meth_set_init(EVP_MD
*md
, int (*init
)(EVP_MD_CTX
*ctx
))
744 if (md
->init
!= NULL
)
750 int EVP_MD_meth_set_update(EVP_MD
*md
, int (*update
)(EVP_MD_CTX
*ctx
,
754 if (md
->update
!= NULL
)
760 int EVP_MD_meth_set_final(EVP_MD
*md
, int (*final
)(EVP_MD_CTX
*ctx
,
763 if (md
->final
!= NULL
)
769 int EVP_MD_meth_set_copy(EVP_MD
*md
, int (*copy
)(EVP_MD_CTX
*to
,
770 const EVP_MD_CTX
*from
))
772 if (md
->copy
!= NULL
)
778 int EVP_MD_meth_set_cleanup(EVP_MD
*md
, int (*cleanup
)(EVP_MD_CTX
*ctx
))
780 if (md
->cleanup
!= NULL
)
783 md
->cleanup
= cleanup
;
786 int EVP_MD_meth_set_ctrl(EVP_MD
*md
, int (*ctrl
)(EVP_MD_CTX
*ctx
, int cmd
,
789 if (md
->md_ctrl
!= NULL
)
796 int EVP_MD_meth_get_input_blocksize(const EVP_MD
*md
)
798 return md
->block_size
;
800 int EVP_MD_meth_get_result_size(const EVP_MD
*md
)
804 int EVP_MD_meth_get_app_datasize(const EVP_MD
*md
)
808 unsigned long EVP_MD_meth_get_flags(const EVP_MD
*md
)
812 int (*EVP_MD_meth_get_init(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
816 int (*EVP_MD_meth_get_update(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
822 int (*EVP_MD_meth_get_final(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
827 int (*EVP_MD_meth_get_copy(const EVP_MD
*md
))(EVP_MD_CTX
*to
,
828 const EVP_MD_CTX
*from
)
832 int (*EVP_MD_meth_get_cleanup(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
836 int (*EVP_MD_meth_get_ctrl(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
, int cmd
,
842 const EVP_MD
*EVP_MD_CTX_md(const EVP_MD_CTX
*ctx
)
846 return ctx
->reqdigest
;
849 EVP_PKEY_CTX
*EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX
*ctx
)
854 #if !defined(FIPS_MODULE)
855 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
856 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX
*ctx
, EVP_PKEY_CTX
*pctx
)
859 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
860 * we have to deal with the cleanup job here.
862 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
))
863 EVP_PKEY_CTX_free(ctx
->pctx
);
868 /* make sure pctx is not freed when destroying EVP_MD_CTX */
869 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
871 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
874 #endif /* !defined(FIPS_MODULE) */
876 void *EVP_MD_CTX_md_data(const EVP_MD_CTX
*ctx
)
881 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX
*ctx
))(EVP_MD_CTX
*ctx
,
882 const void *data
, size_t count
)
887 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX
*ctx
,
888 int (*update
) (EVP_MD_CTX
*ctx
,
889 const void *data
, size_t count
))
891 ctx
->update
= update
;
894 void EVP_MD_CTX_set_flags(EVP_MD_CTX
*ctx
, int flags
)
899 void EVP_MD_CTX_clear_flags(EVP_MD_CTX
*ctx
, int flags
)
901 ctx
->flags
&= ~flags
;
904 int EVP_MD_CTX_test_flags(const EVP_MD_CTX
*ctx
, int flags
)
906 return (ctx
->flags
& flags
);
909 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
914 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
916 ctx
->flags
&= ~flags
;
919 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX
*ctx
, int flags
)
921 return (ctx
->flags
& flags
);
924 int EVP_str2ctrl(int (*cb
)(void *ctx
, int cmd
, void *buf
, size_t buflen
),
925 void *ctx
, int cmd
, const char *value
)
932 return cb(ctx
, cmd
, (void *)value
, len
);
935 int EVP_hex2ctrl(int (*cb
)(void *ctx
, int cmd
, void *buf
, size_t buflen
),
936 void *ctx
, int cmd
, const char *hex
)
942 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
945 if (binlen
<= INT_MAX
)
946 rv
= cb(ctx
, cmd
, bin
, binlen
);
951 int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX
*ctx
, const char *name
)
953 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
954 OSSL_PARAM
*p
= params
;
957 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
958 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
962 if (!EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
966 /* Could be a legacy key, try and convert to a ctrl */
967 if (ctx
->pmeth
!= NULL
&& (nid
= OBJ_txt2nid(name
)) != NID_undef
) {
968 # ifndef OPENSSL_NO_DH
969 if (ctx
->pmeth
->pkey_id
== EVP_PKEY_DH
)
970 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
,
972 | EVP_PKEY_OP_KEYGEN
,
973 EVP_PKEY_CTRL_DH_NID
, nid
, NULL
);
975 # ifndef OPENSSL_NO_EC
976 if (ctx
->pmeth
->pkey_id
== EVP_PKEY_EC
)
977 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_EC
,
978 EVP_PKEY_OP_PARAMGEN
|EVP_PKEY_OP_KEYGEN
,
979 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
,
984 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
985 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
992 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
994 return EVP_PKEY_CTX_set_params(ctx
, params
);
997 int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX
*ctx
, char *name
, size_t namelen
)
999 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1000 OSSL_PARAM
*p
= params
;
1002 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1003 /* There is no legacy support for this */
1004 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1005 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1012 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
1014 if (!EVP_PKEY_CTX_get_params(ctx
, params
))