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 #ifndef OPENSSL_NO_DEPRECATED_3_0
440 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX
*ctx
)
443 const unsigned char *v
= ctx
->oiv
;
444 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
447 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
,
448 (void **)&v
, sizeof(ctx
->oiv
));
449 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
451 return ok
!= 0 ? v
: NULL
;
455 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
457 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX
*ctx
)
460 const unsigned char *v
= ctx
->iv
;
461 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
464 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV_STATE
, (void **)&v
,
466 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
468 return ok
!= 0 ? v
: NULL
;
471 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX
*ctx
)
474 unsigned char *v
= ctx
->iv
;
475 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
478 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV_STATE
, (void **)&v
,
480 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
482 return ok
!= 0 ? v
: NULL
;
484 #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
486 int EVP_CIPHER_CTX_get_iv_state(EVP_CIPHER_CTX
*ctx
, void *buf
, size_t len
)
488 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
491 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV_STATE
, buf
, len
);
492 return evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
495 int EVP_CIPHER_CTX_get_iv(EVP_CIPHER_CTX
*ctx
, void *buf
, size_t len
)
497 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
500 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV
, buf
, len
);
501 return evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
504 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX
*ctx
)
509 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX
*ctx
)
512 unsigned int v
= (unsigned int)ctx
->num
;
513 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
515 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &v
);
516 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
518 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
521 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX
*ctx
, int num
)
524 unsigned int n
= (unsigned int)num
;
525 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
527 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &n
);
528 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
535 int EVP_CIPHER_key_length(const EVP_CIPHER
*cipher
)
537 return cipher
->key_len
;
540 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
543 size_t v
= ctx
->key_len
;
544 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
546 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &v
);
547 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
549 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
552 int EVP_CIPHER_nid(const EVP_CIPHER
*cipher
)
557 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX
*ctx
)
559 return ctx
->cipher
->nid
;
562 int EVP_CIPHER_is_a(const EVP_CIPHER
*cipher
, const char *name
)
564 if (cipher
->prov
!= NULL
)
565 return evp_is_a(cipher
->prov
, cipher
->name_id
, NULL
, name
);
566 return evp_is_a(NULL
, 0, EVP_CIPHER_name(cipher
), name
);
569 int EVP_CIPHER_number(const EVP_CIPHER
*cipher
)
571 return cipher
->name_id
;
574 const char *EVP_CIPHER_name(const EVP_CIPHER
*cipher
)
576 if (cipher
->prov
!= NULL
)
577 return evp_first_name(cipher
->prov
, cipher
->name_id
);
579 return OBJ_nid2sn(EVP_CIPHER_nid(cipher
));
585 void EVP_CIPHER_names_do_all(const EVP_CIPHER
*cipher
,
586 void (*fn
)(const char *name
, void *data
),
589 if (cipher
->prov
!= NULL
)
590 evp_names_do_all(cipher
->prov
, cipher
->name_id
, fn
, data
);
593 const OSSL_PROVIDER
*EVP_CIPHER_provider(const EVP_CIPHER
*cipher
)
598 int EVP_CIPHER_mode(const EVP_CIPHER
*cipher
)
600 return EVP_CIPHER_flags(cipher
) & EVP_CIPH_MODE
;
603 int EVP_MD_is_a(const EVP_MD
*md
, const char *name
)
605 if (md
->prov
!= NULL
)
606 return evp_is_a(md
->prov
, md
->name_id
, NULL
, name
);
607 return evp_is_a(NULL
, 0, EVP_MD_name(md
), name
);
610 int EVP_MD_number(const EVP_MD
*md
)
615 const char *EVP_MD_name(const EVP_MD
*md
)
617 if (md
->prov
!= NULL
)
618 return evp_first_name(md
->prov
, md
->name_id
);
620 return OBJ_nid2sn(EVP_MD_nid(md
));
626 void EVP_MD_names_do_all(const EVP_MD
*md
,
627 void (*fn
)(const char *name
, void *data
),
630 if (md
->prov
!= NULL
)
631 evp_names_do_all(md
->prov
, md
->name_id
, fn
, data
);
634 const OSSL_PROVIDER
*EVP_MD_provider(const EVP_MD
*md
)
639 int EVP_MD_block_size(const EVP_MD
*md
)
642 size_t v
= md
->block_size
;
643 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
646 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
650 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE
, &v
);
651 ok
= evp_do_md_getparams(md
, params
);
653 return ok
!= 0 ? (int)v
: -1;
656 int EVP_MD_type(const EVP_MD
*md
)
661 int EVP_MD_pkey_type(const EVP_MD
*md
)
663 return md
->pkey_type
;
666 int EVP_MD_size(const EVP_MD
*md
)
669 size_t v
= md
->md_size
;
670 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
673 EVPerr(EVP_F_EVP_MD_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
677 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE
, &v
);
678 ok
= evp_do_md_getparams(md
, params
);
680 return ok
!= 0 ? (int)v
: -1;
683 unsigned long EVP_MD_flags(const EVP_MD
*md
)
686 unsigned long v
= md
->flags
;
687 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
689 params
[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS
, &v
);
690 ok
= evp_do_md_getparams(md
, params
);
692 return ok
!= 0 ? v
: 0;
695 EVP_MD
*EVP_MD_meth_new(int md_type
, int pkey_type
)
697 EVP_MD
*md
= evp_md_new();
701 md
->pkey_type
= pkey_type
;
706 EVP_MD
*EVP_MD_meth_dup(const EVP_MD
*md
)
711 * Non-legacy EVP_MDs can't be duplicated like this.
712 * Use EVP_MD_up_ref() instead.
714 if (md
->prov
!= NULL
)
717 if ((to
= EVP_MD_meth_new(md
->type
, md
->pkey_type
)) != NULL
) {
718 CRYPTO_RWLOCK
*lock
= to
->lock
;
720 memcpy(to
, md
, sizeof(*to
));
726 void EVP_MD_meth_free(EVP_MD
*md
)
730 int EVP_MD_meth_set_input_blocksize(EVP_MD
*md
, int blocksize
)
732 if (md
->block_size
!= 0)
735 md
->block_size
= blocksize
;
738 int EVP_MD_meth_set_result_size(EVP_MD
*md
, int resultsize
)
740 if (md
->md_size
!= 0)
743 md
->md_size
= resultsize
;
746 int EVP_MD_meth_set_app_datasize(EVP_MD
*md
, int datasize
)
748 if (md
->ctx_size
!= 0)
751 md
->ctx_size
= datasize
;
754 int EVP_MD_meth_set_flags(EVP_MD
*md
, unsigned long flags
)
762 int EVP_MD_meth_set_init(EVP_MD
*md
, int (*init
)(EVP_MD_CTX
*ctx
))
764 if (md
->init
!= NULL
)
770 int EVP_MD_meth_set_update(EVP_MD
*md
, int (*update
)(EVP_MD_CTX
*ctx
,
774 if (md
->update
!= NULL
)
780 int EVP_MD_meth_set_final(EVP_MD
*md
, int (*final
)(EVP_MD_CTX
*ctx
,
783 if (md
->final
!= NULL
)
789 int EVP_MD_meth_set_copy(EVP_MD
*md
, int (*copy
)(EVP_MD_CTX
*to
,
790 const EVP_MD_CTX
*from
))
792 if (md
->copy
!= NULL
)
798 int EVP_MD_meth_set_cleanup(EVP_MD
*md
, int (*cleanup
)(EVP_MD_CTX
*ctx
))
800 if (md
->cleanup
!= NULL
)
803 md
->cleanup
= cleanup
;
806 int EVP_MD_meth_set_ctrl(EVP_MD
*md
, int (*ctrl
)(EVP_MD_CTX
*ctx
, int cmd
,
809 if (md
->md_ctrl
!= NULL
)
816 int EVP_MD_meth_get_input_blocksize(const EVP_MD
*md
)
818 return md
->block_size
;
820 int EVP_MD_meth_get_result_size(const EVP_MD
*md
)
824 int EVP_MD_meth_get_app_datasize(const EVP_MD
*md
)
828 unsigned long EVP_MD_meth_get_flags(const EVP_MD
*md
)
832 int (*EVP_MD_meth_get_init(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
836 int (*EVP_MD_meth_get_update(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
842 int (*EVP_MD_meth_get_final(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
847 int (*EVP_MD_meth_get_copy(const EVP_MD
*md
))(EVP_MD_CTX
*to
,
848 const EVP_MD_CTX
*from
)
852 int (*EVP_MD_meth_get_cleanup(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
856 int (*EVP_MD_meth_get_ctrl(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
, int cmd
,
862 const EVP_MD
*EVP_MD_CTX_md(const EVP_MD_CTX
*ctx
)
866 return ctx
->reqdigest
;
869 EVP_PKEY_CTX
*EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX
*ctx
)
874 #if !defined(FIPS_MODULE)
875 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
876 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX
*ctx
, EVP_PKEY_CTX
*pctx
)
879 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
880 * we have to deal with the cleanup job here.
882 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
))
883 EVP_PKEY_CTX_free(ctx
->pctx
);
888 /* make sure pctx is not freed when destroying EVP_MD_CTX */
889 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
891 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
894 #endif /* !defined(FIPS_MODULE) */
896 void *EVP_MD_CTX_md_data(const EVP_MD_CTX
*ctx
)
901 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX
*ctx
))(EVP_MD_CTX
*ctx
,
902 const void *data
, size_t count
)
907 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX
*ctx
,
908 int (*update
) (EVP_MD_CTX
*ctx
,
909 const void *data
, size_t count
))
911 ctx
->update
= update
;
914 void EVP_MD_CTX_set_flags(EVP_MD_CTX
*ctx
, int flags
)
919 void EVP_MD_CTX_clear_flags(EVP_MD_CTX
*ctx
, int flags
)
921 ctx
->flags
&= ~flags
;
924 int EVP_MD_CTX_test_flags(const EVP_MD_CTX
*ctx
, int flags
)
926 return (ctx
->flags
& flags
);
929 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
934 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
936 ctx
->flags
&= ~flags
;
939 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX
*ctx
, int flags
)
941 return (ctx
->flags
& flags
);
944 int EVP_str2ctrl(int (*cb
)(void *ctx
, int cmd
, void *buf
, size_t buflen
),
945 void *ctx
, int cmd
, const char *value
)
952 return cb(ctx
, cmd
, (void *)value
, len
);
955 int EVP_hex2ctrl(int (*cb
)(void *ctx
, int cmd
, void *buf
, size_t buflen
),
956 void *ctx
, int cmd
, const char *hex
)
962 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
965 if (binlen
<= INT_MAX
)
966 rv
= cb(ctx
, cmd
, bin
, binlen
);
971 int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX
*ctx
, const char *name
)
973 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
974 OSSL_PARAM
*p
= params
;
977 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
978 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
982 if (!EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
986 /* Could be a legacy key, try and convert to a ctrl */
987 if (ctx
->pmeth
!= NULL
&& (nid
= OBJ_txt2nid(name
)) != NID_undef
) {
988 # ifndef OPENSSL_NO_DH
989 if (ctx
->pmeth
->pkey_id
== EVP_PKEY_DH
)
990 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
,
992 | EVP_PKEY_OP_KEYGEN
,
993 EVP_PKEY_CTRL_DH_NID
, nid
, NULL
);
995 # ifndef OPENSSL_NO_EC
996 if (ctx
->pmeth
->pkey_id
== EVP_PKEY_EC
)
997 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_EC
,
998 EVP_PKEY_OP_PARAMGEN
|EVP_PKEY_OP_KEYGEN
,
999 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
,
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 return EVP_PKEY_CTX_set_params(ctx
, params
);
1017 int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX
*ctx
, char *name
, size_t namelen
)
1019 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1020 OSSL_PARAM
*p
= params
;
1022 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1023 /* There is no legacy support for this */
1024 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1025 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1032 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
1034 if (!EVP_PKEY_CTX_get_params(ctx
, params
))