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 "crypto/asn1.h"
26 #include "internal/provider.h"
27 #include "evp_local.h"
29 #if !defined(FIPS_MODULE)
30 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
32 return evp_cipher_param_to_asn1_ex(c
, type
, NULL
);
35 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
37 return evp_cipher_asn1_to_param_ex(c
, type
, NULL
);
40 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX
*ctx
, ASN1_TYPE
*type
)
46 unsigned char iv
[EVP_MAX_IV_LENGTH
];
48 l
= EVP_CIPHER_CTX_iv_length(ctx
);
49 if (!ossl_assert(l
<= sizeof(iv
)))
51 i
= ASN1_TYPE_get_octetstring(type
, iv
, l
);
55 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, NULL
, iv
, -1))
61 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
65 unsigned char *oiv
= NULL
;
68 oiv
= (unsigned char *)EVP_CIPHER_CTX_original_iv(c
);
69 j
= EVP_CIPHER_CTX_iv_length(c
);
70 OPENSSL_assert(j
<= sizeof(c
->iv
));
71 i
= ASN1_TYPE_set_octetstring(type
, oiv
, j
);
76 int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
77 evp_cipher_aead_asn1_params
*asn1_params
)
79 int ret
= -1; /* Assume the worst */
80 const EVP_CIPHER
*cipher
= c
->cipher
;
83 * For legacy implementations, we detect custom AlgorithmIdentifier
84 * parameter handling by checking if the function pointer
85 * cipher->set_asn1_parameters is set. We know that this pointer
86 * is NULL for provided implementations.
88 * Otherwise, for any implementation, we check the flag
89 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
90 * default AI parameter extraction.
92 * Otherwise, for provided implementations, we convert |type| to
93 * a DER encoded blob and pass to the implementation in OSSL_PARAM
96 * If none of the above applies, this operation is unsupported.
98 if (cipher
->set_asn1_parameters
!= NULL
) {
99 ret
= cipher
->set_asn1_parameters(c
, type
);
100 } else if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_CUSTOM_ASN1
) == 0) {
101 switch (EVP_CIPHER_mode(cipher
)) {
102 case EVP_CIPH_WRAP_MODE
:
103 if (EVP_CIPHER_is_a(cipher
, SN_id_smime_alg_CMS3DESwrap
))
104 ASN1_TYPE_set(type
, V_ASN1_NULL
, NULL
);
108 case EVP_CIPH_GCM_MODE
:
109 ret
= evp_cipher_set_asn1_aead_params(c
, type
, asn1_params
);
112 case EVP_CIPH_CCM_MODE
:
113 case EVP_CIPH_XTS_MODE
:
114 case EVP_CIPH_OCB_MODE
:
119 ret
= EVP_CIPHER_set_asn1_iv(c
, type
);
121 } else if (cipher
->prov
!= NULL
) {
122 OSSL_PARAM params
[3], *p
= params
;
123 unsigned char *der
= NULL
, *derp
;
126 * We make two passes, the first to get the appropriate buffer size,
127 * and the second to get the actual value.
129 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID
,
131 *p
= OSSL_PARAM_construct_end();
133 if (!EVP_CIPHER_CTX_get_params(c
, params
))
136 /* ... but, we should get a return size too! */
137 if (OSSL_PARAM_modified(params
)
138 && params
[0].return_size
!= 0
139 && (der
= OPENSSL_malloc(params
[0].return_size
)) != NULL
) {
140 params
[0].data
= der
;
141 params
[0].data_size
= params
[0].return_size
;
142 OSSL_PARAM_set_all_unmodified(params
);
144 if (EVP_CIPHER_CTX_get_params(c
, params
)
145 && OSSL_PARAM_modified(params
)
146 && d2i_ASN1_TYPE(&type
, (const unsigned char **)&derp
,
147 params
[0].return_size
) != NULL
) {
158 EVPerr(0, EVP_R_UNSUPPORTED_CIPHER
);
160 EVPerr(0, EVP_R_CIPHER_PARAMETER_ERROR
);
166 int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
167 evp_cipher_aead_asn1_params
*asn1_params
)
169 int ret
= -1; /* Assume the worst */
170 const EVP_CIPHER
*cipher
= c
->cipher
;
173 * For legacy implementations, we detect custom AlgorithmIdentifier
174 * parameter handling by checking if there the function pointer
175 * cipher->get_asn1_parameters is set. We know that this pointer
176 * is NULL for provided implementations.
178 * Otherwise, for any implementation, we check the flag
179 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
180 * default AI parameter creation.
182 * Otherwise, for provided implementations, we get the AI parameter
183 * in DER encoded form from the implementation by requesting the
184 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
186 * If none of the above applies, this operation is unsupported.
188 if (cipher
->get_asn1_parameters
!= NULL
) {
189 ret
= cipher
->get_asn1_parameters(c
, type
);
190 } else if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_CUSTOM_ASN1
) == 0) {
191 switch (EVP_CIPHER_mode(cipher
)) {
192 case EVP_CIPH_WRAP_MODE
:
196 case EVP_CIPH_GCM_MODE
:
197 ret
= evp_cipher_get_asn1_aead_params(c
, type
, asn1_params
);
200 case EVP_CIPH_CCM_MODE
:
201 case EVP_CIPH_XTS_MODE
:
202 case EVP_CIPH_OCB_MODE
:
207 ret
= EVP_CIPHER_get_asn1_iv(c
, type
);
209 } else if (cipher
->prov
!= NULL
) {
210 OSSL_PARAM params
[3], *p
= params
;
211 unsigned char *der
= NULL
;
214 if ((derl
= i2d_ASN1_TYPE(type
, &der
)) >= 0) {
216 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID
,
218 *p
= OSSL_PARAM_construct_end();
219 if (EVP_CIPHER_CTX_set_params(c
, params
))
228 EVPerr(0, EVP_R_UNSUPPORTED_CIPHER
);
230 EVPerr(0, EVP_R_CIPHER_PARAMETER_ERROR
);
236 int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
237 evp_cipher_aead_asn1_params
*asn1_params
)
241 unsigned char iv
[EVP_MAX_IV_LENGTH
];
243 if (type
== NULL
|| asn1_params
== NULL
)
246 i
= asn1_type_get_octetstring_int(type
, &tl
, NULL
, EVP_MAX_IV_LENGTH
);
249 asn1_type_get_octetstring_int(type
, &tl
, iv
, i
);
251 memcpy(asn1_params
->iv
, iv
, i
);
252 asn1_params
->iv_len
= i
;
257 int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
,
258 evp_cipher_aead_asn1_params
*asn1_params
)
260 if (type
== NULL
|| asn1_params
== NULL
)
263 return asn1_type_set_octetstring_int(type
, asn1_params
->tag_len
,
264 asn1_params
->iv
, asn1_params
->iv_len
);
266 #endif /* !defined(FIPS_MODULE) */
268 /* Convert the various cipher NIDs and dummies to a proper OID NID */
269 int EVP_CIPHER_type(const EVP_CIPHER
*ctx
)
272 nid
= EVP_CIPHER_nid(ctx
);
287 case NID_aes_128_cfb128
:
288 case NID_aes_128_cfb8
:
289 case NID_aes_128_cfb1
:
291 return NID_aes_128_cfb128
;
293 case NID_aes_192_cfb128
:
294 case NID_aes_192_cfb8
:
295 case NID_aes_192_cfb1
:
297 return NID_aes_192_cfb128
;
299 case NID_aes_256_cfb128
:
300 case NID_aes_256_cfb8
:
301 case NID_aes_256_cfb1
:
303 return NID_aes_256_cfb128
;
309 return NID_des_cfb64
;
311 case NID_des_ede3_cfb64
:
312 case NID_des_ede3_cfb8
:
313 case NID_des_ede3_cfb1
:
315 return NID_des_cfb64
;
322 /* Check it has an OID and it is valid */
323 ASN1_OBJECT
*otmp
= OBJ_nid2obj(nid
);
325 if (OBJ_get0_data(otmp
) == NULL
)
327 ASN1_OBJECT_free(otmp
);
334 int evp_cipher_cache_constants(EVP_CIPHER
*cipher
)
340 unsigned int mode
= 0;
341 unsigned long flags
= 0;
342 OSSL_PARAM params
[6];
344 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE
, &blksz
);
345 params
[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &ivlen
);
346 params
[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &keylen
);
347 params
[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE
, &mode
);
348 params
[4] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS
, &flags
);
349 params
[5] = OSSL_PARAM_construct_end();
350 ok
= evp_do_ciph_getparams(cipher
, params
);
352 /* Provided implementations may have a custom cipher_cipher */
353 if (cipher
->prov
!= NULL
&& cipher
->ccipher
!= NULL
)
354 flags
|= EVP_CIPH_FLAG_CUSTOM_CIPHER
;
355 cipher
->block_size
= blksz
;
356 cipher
->iv_len
= ivlen
;
357 cipher
->key_len
= keylen
;
358 cipher
->flags
= flags
| mode
;
363 int EVP_CIPHER_block_size(const EVP_CIPHER
*cipher
)
365 return cipher
->block_size
;
368 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX
*ctx
)
370 return EVP_CIPHER_block_size(ctx
->cipher
);
373 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER
*e
)
378 int EVP_Cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
379 const unsigned char *in
, unsigned int inl
)
381 if (ctx
->cipher
->prov
!= NULL
) {
383 * If the provided implementation has a ccipher function, we use it,
384 * and translate its return value like this: 0 => -1, 1 => outlen
386 * Otherwise, we call the cupdate function if in != NULL, or cfinal
387 * if in == NULL. Regardless of which, we return what we got.
391 size_t blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
393 if (ctx
->cipher
->ccipher
!= NULL
)
394 ret
= ctx
->cipher
->ccipher(ctx
->provctx
, out
, &outl
,
395 inl
+ (blocksize
== 1 ? 0 : blocksize
),
399 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &outl
,
400 inl
+ (blocksize
== 1 ? 0 : blocksize
),
403 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &outl
,
404 blocksize
== 1 ? 0 : blocksize
);
409 return ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
412 const EVP_CIPHER
*EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX
*ctx
)
417 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX
*ctx
)
422 unsigned long EVP_CIPHER_flags(const EVP_CIPHER
*cipher
)
424 return cipher
->flags
;
427 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX
*ctx
)
429 return ctx
->app_data
;
432 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX
*ctx
, void *data
)
434 ctx
->app_data
= data
;
437 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX
*ctx
)
439 return ctx
->cipher_data
;
442 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX
*ctx
, void *cipher_data
)
444 void *old_cipher_data
;
446 old_cipher_data
= ctx
->cipher_data
;
447 ctx
->cipher_data
= cipher_data
;
449 return old_cipher_data
;
452 int EVP_CIPHER_iv_length(const EVP_CIPHER
*cipher
)
454 return cipher
->iv_len
;
457 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX
*ctx
)
459 int rv
, len
= EVP_CIPHER_iv_length(ctx
->cipher
);
461 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
463 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &v
);
464 rv
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
465 if (rv
== EVP_CTRL_RET_UNSUPPORTED
)
467 return rv
!= 0 ? (int)v
: -1;
468 /* TODO (3.0) Remove legacy support */
470 if ((EVP_CIPHER_flags(ctx
->cipher
) & EVP_CIPH_CUSTOM_IV_LENGTH
) != 0) {
471 rv
= EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX
*)ctx
, EVP_CTRL_GET_IVLEN
,
473 return (rv
== 1) ? len
: -1;
478 int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX
*ctx
)
482 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
484 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN
, &v
);
485 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
486 return ret
== 1 ? (int)v
: 0;
489 #ifndef OPENSSL_NO_DEPRECATED_3_0
490 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX
*ctx
)
493 const unsigned char *v
= ctx
->oiv
;
494 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
497 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
,
498 (void **)&v
, sizeof(ctx
->oiv
));
499 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
501 return ok
!= 0 ? v
: NULL
;
505 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
507 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX
*ctx
)
510 const unsigned char *v
= ctx
->iv
;
511 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
514 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV_STATE
, (void **)&v
,
516 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
518 return ok
!= 0 ? v
: NULL
;
521 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX
*ctx
)
524 unsigned char *v
= ctx
->iv
;
525 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
528 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV_STATE
, (void **)&v
,
530 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
532 return ok
!= 0 ? v
: NULL
;
534 #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
536 int EVP_CIPHER_CTX_get_iv_state(EVP_CIPHER_CTX
*ctx
, void *buf
, size_t len
)
538 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
541 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV_STATE
, buf
, len
);
542 return evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
545 int EVP_CIPHER_CTX_get_iv(EVP_CIPHER_CTX
*ctx
, void *buf
, size_t len
)
547 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
550 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV
, buf
, len
);
551 return evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
554 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX
*ctx
)
559 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX
*ctx
)
562 unsigned int v
= (unsigned int)ctx
->num
;
563 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
565 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &v
);
566 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
568 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
571 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX
*ctx
, int num
)
574 unsigned int n
= (unsigned int)num
;
575 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
577 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &n
);
578 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
585 int EVP_CIPHER_key_length(const EVP_CIPHER
*cipher
)
587 return cipher
->key_len
;
590 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
593 size_t v
= ctx
->key_len
;
594 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
596 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &v
);
597 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
599 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
602 int EVP_CIPHER_nid(const EVP_CIPHER
*cipher
)
607 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX
*ctx
)
609 return ctx
->cipher
->nid
;
612 int EVP_CIPHER_is_a(const EVP_CIPHER
*cipher
, const char *name
)
614 if (cipher
->prov
!= NULL
)
615 return evp_is_a(cipher
->prov
, cipher
->name_id
, NULL
, name
);
616 return evp_is_a(NULL
, 0, EVP_CIPHER_name(cipher
), name
);
619 int EVP_CIPHER_number(const EVP_CIPHER
*cipher
)
621 return cipher
->name_id
;
624 const char *EVP_CIPHER_name(const EVP_CIPHER
*cipher
)
626 if (cipher
->prov
!= NULL
)
627 return evp_first_name(cipher
->prov
, cipher
->name_id
);
629 return OBJ_nid2sn(EVP_CIPHER_nid(cipher
));
635 void EVP_CIPHER_names_do_all(const EVP_CIPHER
*cipher
,
636 void (*fn
)(const char *name
, void *data
),
639 if (cipher
->prov
!= NULL
)
640 evp_names_do_all(cipher
->prov
, cipher
->name_id
, fn
, data
);
643 const OSSL_PROVIDER
*EVP_CIPHER_provider(const EVP_CIPHER
*cipher
)
648 int EVP_CIPHER_mode(const EVP_CIPHER
*cipher
)
650 return EVP_CIPHER_flags(cipher
) & EVP_CIPH_MODE
;
653 int EVP_MD_is_a(const EVP_MD
*md
, const char *name
)
655 if (md
->prov
!= NULL
)
656 return evp_is_a(md
->prov
, md
->name_id
, NULL
, name
);
657 return evp_is_a(NULL
, 0, EVP_MD_name(md
), name
);
660 int EVP_MD_number(const EVP_MD
*md
)
665 const char *EVP_MD_name(const EVP_MD
*md
)
667 if (md
->prov
!= NULL
)
668 return evp_first_name(md
->prov
, md
->name_id
);
670 return OBJ_nid2sn(EVP_MD_nid(md
));
676 void EVP_MD_names_do_all(const EVP_MD
*md
,
677 void (*fn
)(const char *name
, void *data
),
680 if (md
->prov
!= NULL
)
681 evp_names_do_all(md
->prov
, md
->name_id
, fn
, data
);
684 const OSSL_PROVIDER
*EVP_MD_provider(const EVP_MD
*md
)
689 int EVP_MD_block_size(const EVP_MD
*md
)
693 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
696 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
700 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE
, &v
);
701 ok
= evp_do_md_getparams(md
, params
);
703 return ok
!= 0 ? (int)v
: -1;
706 int EVP_MD_type(const EVP_MD
*md
)
711 int EVP_MD_pkey_type(const EVP_MD
*md
)
713 return md
->pkey_type
;
716 int EVP_MD_size(const EVP_MD
*md
)
720 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
723 EVPerr(EVP_F_EVP_MD_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
727 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE
, &v
);
728 ok
= evp_do_md_getparams(md
, params
);
730 return ok
!= 0 ? (int)v
: -1;
733 unsigned long EVP_MD_flags(const EVP_MD
*md
)
736 unsigned long v
= md
->flags
;
737 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
739 params
[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS
, &v
);
740 ok
= evp_do_md_getparams(md
, params
);
742 return ok
!= 0 ? v
: 0;
745 EVP_MD
*EVP_MD_meth_new(int md_type
, int pkey_type
)
747 EVP_MD
*md
= evp_md_new();
751 md
->pkey_type
= pkey_type
;
756 EVP_MD
*EVP_MD_meth_dup(const EVP_MD
*md
)
761 * Non-legacy EVP_MDs can't be duplicated like this.
762 * Use EVP_MD_up_ref() instead.
764 if (md
->prov
!= NULL
)
767 if ((to
= EVP_MD_meth_new(md
->type
, md
->pkey_type
)) != NULL
) {
768 CRYPTO_RWLOCK
*lock
= to
->lock
;
770 memcpy(to
, md
, sizeof(*to
));
776 void EVP_MD_meth_free(EVP_MD
*md
)
780 int EVP_MD_meth_set_input_blocksize(EVP_MD
*md
, int blocksize
)
782 if (md
->block_size
!= 0)
785 md
->block_size
= blocksize
;
788 int EVP_MD_meth_set_result_size(EVP_MD
*md
, int resultsize
)
790 if (md
->md_size
!= 0)
793 md
->md_size
= resultsize
;
796 int EVP_MD_meth_set_app_datasize(EVP_MD
*md
, int datasize
)
798 if (md
->ctx_size
!= 0)
801 md
->ctx_size
= datasize
;
804 int EVP_MD_meth_set_flags(EVP_MD
*md
, unsigned long flags
)
812 int EVP_MD_meth_set_init(EVP_MD
*md
, int (*init
)(EVP_MD_CTX
*ctx
))
814 if (md
->init
!= NULL
)
820 int EVP_MD_meth_set_update(EVP_MD
*md
, int (*update
)(EVP_MD_CTX
*ctx
,
824 if (md
->update
!= NULL
)
830 int EVP_MD_meth_set_final(EVP_MD
*md
, int (*final
)(EVP_MD_CTX
*ctx
,
833 if (md
->final
!= NULL
)
839 int EVP_MD_meth_set_copy(EVP_MD
*md
, int (*copy
)(EVP_MD_CTX
*to
,
840 const EVP_MD_CTX
*from
))
842 if (md
->copy
!= NULL
)
848 int EVP_MD_meth_set_cleanup(EVP_MD
*md
, int (*cleanup
)(EVP_MD_CTX
*ctx
))
850 if (md
->cleanup
!= NULL
)
853 md
->cleanup
= cleanup
;
856 int EVP_MD_meth_set_ctrl(EVP_MD
*md
, int (*ctrl
)(EVP_MD_CTX
*ctx
, int cmd
,
859 if (md
->md_ctrl
!= NULL
)
866 int EVP_MD_meth_get_input_blocksize(const EVP_MD
*md
)
868 return md
->block_size
;
870 int EVP_MD_meth_get_result_size(const EVP_MD
*md
)
874 int EVP_MD_meth_get_app_datasize(const EVP_MD
*md
)
878 unsigned long EVP_MD_meth_get_flags(const EVP_MD
*md
)
882 int (*EVP_MD_meth_get_init(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
886 int (*EVP_MD_meth_get_update(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
892 int (*EVP_MD_meth_get_final(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
897 int (*EVP_MD_meth_get_copy(const EVP_MD
*md
))(EVP_MD_CTX
*to
,
898 const EVP_MD_CTX
*from
)
902 int (*EVP_MD_meth_get_cleanup(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
906 int (*EVP_MD_meth_get_ctrl(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
, int cmd
,
912 const EVP_MD
*EVP_MD_CTX_md(const EVP_MD_CTX
*ctx
)
916 return ctx
->reqdigest
;
919 EVP_PKEY_CTX
*EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX
*ctx
)
924 #if !defined(FIPS_MODULE)
925 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
926 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX
*ctx
, EVP_PKEY_CTX
*pctx
)
929 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
930 * we have to deal with the cleanup job here.
932 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
))
933 EVP_PKEY_CTX_free(ctx
->pctx
);
938 /* make sure pctx is not freed when destroying EVP_MD_CTX */
939 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
941 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
944 #endif /* !defined(FIPS_MODULE) */
946 void *EVP_MD_CTX_md_data(const EVP_MD_CTX
*ctx
)
951 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX
*ctx
))(EVP_MD_CTX
*ctx
,
952 const void *data
, size_t count
)
957 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX
*ctx
,
958 int (*update
) (EVP_MD_CTX
*ctx
,
959 const void *data
, size_t count
))
961 ctx
->update
= update
;
964 void EVP_MD_CTX_set_flags(EVP_MD_CTX
*ctx
, int flags
)
969 void EVP_MD_CTX_clear_flags(EVP_MD_CTX
*ctx
, int flags
)
971 ctx
->flags
&= ~flags
;
974 int EVP_MD_CTX_test_flags(const EVP_MD_CTX
*ctx
, int flags
)
976 return (ctx
->flags
& flags
);
979 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
984 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
986 ctx
->flags
&= ~flags
;
989 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX
*ctx
, int flags
)
991 return (ctx
->flags
& flags
);
994 int EVP_str2ctrl(int (*cb
)(void *ctx
, int cmd
, void *buf
, size_t buflen
),
995 void *ctx
, int cmd
, const char *value
)
1002 return cb(ctx
, cmd
, (void *)value
, len
);
1005 int EVP_hex2ctrl(int (*cb
)(void *ctx
, int cmd
, void *buf
, size_t buflen
),
1006 void *ctx
, int cmd
, const char *hex
)
1012 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
1015 if (binlen
<= INT_MAX
)
1016 rv
= cb(ctx
, cmd
, bin
, binlen
);
1021 int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX
*ctx
, const char *name
)
1023 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1024 OSSL_PARAM
*p
= params
;
1027 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1028 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1032 if (!EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1036 /* Could be a legacy key, try and convert to a ctrl */
1037 if (ctx
->pmeth
!= NULL
&& (nid
= OBJ_txt2nid(name
)) != NID_undef
) {
1038 # ifndef OPENSSL_NO_DH
1039 if (ctx
->pmeth
->pkey_id
== EVP_PKEY_DH
)
1040 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DH
,
1041 EVP_PKEY_OP_PARAMGEN
1042 | EVP_PKEY_OP_KEYGEN
,
1043 EVP_PKEY_CTRL_DH_NID
, nid
, NULL
);
1045 # ifndef OPENSSL_NO_EC
1046 if (ctx
->pmeth
->pkey_id
== EVP_PKEY_EC
)
1047 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_EC
,
1048 EVP_PKEY_OP_PARAMGEN
|EVP_PKEY_OP_KEYGEN
,
1049 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID
,
1054 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1055 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1062 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
1064 return EVP_PKEY_CTX_set_params(ctx
, params
);
1067 int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX
*ctx
, char *name
, size_t namelen
)
1069 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1070 OSSL_PARAM
*p
= params
;
1072 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
1073 /* There is no legacy support for this */
1074 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
1075 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1082 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
1084 if (!EVP_PKEY_CTX_get_params(ctx
, params
))