2 * Copyright 1995-2018 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 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include <openssl/dh.h>
17 #include "crypto/evp.h"
18 #include "internal/provider.h"
19 #include "evp_local.h"
21 #if !defined(FIPS_MODE)
22 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
24 int ret
= -1; /* Assume the worst */
25 const EVP_CIPHER
*cipher
= c
->cipher
;
28 * For legacy implementations, we detect custom AlgorithmIdentifier
29 * parameter handling by checking if the function pointer
30 * cipher->set_asn1_parameters is set. We know that this pointer
31 * is NULL for provided implementations.
33 * Otherwise, for any implementation, we check the flag
34 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
35 * default AI parameter extraction.
37 * Otherwise, for provided implementations, we convert |type| to
38 * a DER encoded blob and pass to the implementation in OSSL_PARAM
41 * If none of the above applies, this operation is unsupported.
43 if (cipher
->set_asn1_parameters
!= NULL
) {
44 ret
= cipher
->set_asn1_parameters(c
, type
);
45 } else if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_CUSTOM_ASN1
) == 0) {
46 switch (EVP_CIPHER_mode(cipher
)) {
47 case EVP_CIPH_WRAP_MODE
:
48 if (EVP_CIPHER_is_a(cipher
, SN_id_smime_alg_CMS3DESwrap
))
49 ASN1_TYPE_set(type
, V_ASN1_NULL
, NULL
);
53 case EVP_CIPH_GCM_MODE
:
54 case EVP_CIPH_CCM_MODE
:
55 case EVP_CIPH_XTS_MODE
:
56 case EVP_CIPH_OCB_MODE
:
61 ret
= EVP_CIPHER_set_asn1_iv(c
, type
);
63 } else if (cipher
->prov
!= NULL
) {
64 OSSL_PARAM params
[3], *p
= params
;
65 unsigned char *der
= NULL
, *derp
;
68 * We make two passes, the first to get the appropriate buffer size,
69 * and the second to get the actual value.
71 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID
,
73 *p
= OSSL_PARAM_construct_end();
75 if (!EVP_CIPHER_CTX_get_params(c
, params
))
78 /* ... but, we should get a return size too! */
79 if (params
[0].return_size
!= 0
80 && (der
= OPENSSL_malloc(params
[0].return_size
)) != NULL
) {
82 params
[0].data_size
= params
[0].return_size
;
83 params
[0].return_size
= 0;
85 if (EVP_CIPHER_CTX_get_params(c
, params
)
86 && d2i_ASN1_TYPE(&type
, (const unsigned char **)&derp
,
87 params
[0].return_size
) != NULL
) {
98 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1
, ASN1_R_UNSUPPORTED_CIPHER
);
100 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1
, EVP_R_CIPHER_PARAMETER_ERROR
);
106 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
108 int ret
= -1; /* Assume the worst */
109 const EVP_CIPHER
*cipher
= c
->cipher
;
112 * For legacy implementations, we detect custom AlgorithmIdentifier
113 * parameter handling by checking if there the function pointer
114 * cipher->get_asn1_parameters is set. We know that this pointer
115 * is NULL for provided implementations.
117 * Otherwise, for any implementation, we check the flag
118 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
119 * default AI parameter creation.
121 * Otherwise, for provided implementations, we get the AI parameter
122 * in DER encoded form from the implementation by requesting the
123 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
125 * If none of the above applies, this operation is unsupported.
127 if (cipher
->get_asn1_parameters
!= NULL
) {
128 ret
= cipher
->get_asn1_parameters(c
, type
);
129 } else if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_CUSTOM_ASN1
) == 0) {
130 switch (EVP_CIPHER_mode(cipher
)) {
131 case EVP_CIPH_WRAP_MODE
:
135 case EVP_CIPH_GCM_MODE
:
136 case EVP_CIPH_CCM_MODE
:
137 case EVP_CIPH_XTS_MODE
:
138 case EVP_CIPH_OCB_MODE
:
143 ret
= EVP_CIPHER_get_asn1_iv(c
, type
);
145 } else if (cipher
->prov
!= NULL
) {
146 OSSL_PARAM params
[3], *p
= params
;
147 unsigned char *der
= NULL
;
150 if ((derl
= i2d_ASN1_TYPE(type
, &der
)) >= 0) {
152 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID
,
154 *p
= OSSL_PARAM_construct_end();
155 if (EVP_CIPHER_CTX_set_params(c
, params
))
164 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM
, EVP_R_UNSUPPORTED_CIPHER
);
166 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM
, EVP_R_CIPHER_PARAMETER_ERROR
);
172 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX
*ctx
, ASN1_TYPE
*type
)
178 unsigned char iv
[EVP_MAX_IV_LENGTH
];
180 l
= EVP_CIPHER_CTX_iv_length(ctx
);
181 if (!ossl_assert(l
<= sizeof(iv
)))
183 i
= ASN1_TYPE_get_octetstring(type
, iv
, l
);
187 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, NULL
, iv
, -1))
193 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
197 unsigned char *oiv
= NULL
;
200 oiv
= (unsigned char *)EVP_CIPHER_CTX_original_iv(c
);
201 j
= EVP_CIPHER_CTX_iv_length(c
);
202 OPENSSL_assert(j
<= sizeof(c
->iv
));
203 i
= ASN1_TYPE_set_octetstring(type
, oiv
, j
);
207 #endif /* !defined(FIPS_MODE) */
209 /* Convert the various cipher NIDs and dummies to a proper OID NID */
210 int EVP_CIPHER_type(const EVP_CIPHER
*ctx
)
213 nid
= EVP_CIPHER_nid(ctx
);
228 case NID_aes_128_cfb128
:
229 case NID_aes_128_cfb8
:
230 case NID_aes_128_cfb1
:
232 return NID_aes_128_cfb128
;
234 case NID_aes_192_cfb128
:
235 case NID_aes_192_cfb8
:
236 case NID_aes_192_cfb1
:
238 return NID_aes_192_cfb128
;
240 case NID_aes_256_cfb128
:
241 case NID_aes_256_cfb8
:
242 case NID_aes_256_cfb1
:
244 return NID_aes_256_cfb128
;
250 return NID_des_cfb64
;
252 case NID_des_ede3_cfb64
:
253 case NID_des_ede3_cfb8
:
254 case NID_des_ede3_cfb1
:
256 return NID_des_cfb64
;
263 /* Check it has an OID and it is valid */
264 ASN1_OBJECT
*otmp
= OBJ_nid2obj(nid
);
266 if (OBJ_get0_data(otmp
) == NULL
)
268 ASN1_OBJECT_free(otmp
);
275 int evp_cipher_cache_constants(EVP_CIPHER
*cipher
)
281 unsigned int mode
= 0;
282 unsigned long flags
= 0;
283 OSSL_PARAM params
[6];
285 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE
, &blksz
);
286 params
[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &ivlen
);
287 params
[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &keylen
);
288 params
[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE
, &mode
);
289 params
[4] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS
, &flags
);
290 params
[5] = OSSL_PARAM_construct_end();
291 ok
= evp_do_ciph_getparams(cipher
, params
);
293 /* Provided implementations may have a custom cipher_cipher */
294 if (cipher
->prov
!= NULL
&& cipher
->ccipher
!= NULL
)
295 flags
|= EVP_CIPH_FLAG_CUSTOM_CIPHER
;
296 cipher
->block_size
= blksz
;
297 cipher
->iv_len
= ivlen
;
298 cipher
->key_len
= keylen
;
299 cipher
->flags
= flags
| mode
;
304 int EVP_CIPHER_block_size(const EVP_CIPHER
*cipher
)
306 return cipher
->block_size
;
309 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX
*ctx
)
311 return EVP_CIPHER_block_size(ctx
->cipher
);
314 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER
*e
)
319 int EVP_Cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
320 const unsigned char *in
, unsigned int inl
)
322 if (ctx
->cipher
->prov
!= NULL
) {
324 * If the provided implementation has a ccipher function, we use it,
325 * and translate its return value like this: 0 => -1, 1 => outlen
327 * Otherwise, we call the cupdate function if in != NULL, or cfinal
328 * if in == NULL. Regardless of which, we return what we got.
332 size_t blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
334 if (ctx
->cipher
->ccipher
!= NULL
)
335 ret
= ctx
->cipher
->ccipher(ctx
->provctx
, out
, &outl
,
336 inl
+ (blocksize
== 1 ? 0 : blocksize
),
340 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &outl
,
341 inl
+ (blocksize
== 1 ? 0 : blocksize
),
344 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &outl
,
345 blocksize
== 1 ? 0 : blocksize
);
350 return ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
353 const EVP_CIPHER
*EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX
*ctx
)
358 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX
*ctx
)
363 unsigned long EVP_CIPHER_flags(const EVP_CIPHER
*cipher
)
365 return cipher
->flags
;
368 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX
*ctx
)
370 return ctx
->app_data
;
373 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX
*ctx
, void *data
)
375 ctx
->app_data
= data
;
378 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX
*ctx
)
380 return ctx
->cipher_data
;
383 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX
*ctx
, void *cipher_data
)
385 void *old_cipher_data
;
387 old_cipher_data
= ctx
->cipher_data
;
388 ctx
->cipher_data
= cipher_data
;
390 return old_cipher_data
;
393 int EVP_CIPHER_iv_length(const EVP_CIPHER
*cipher
)
395 return cipher
->iv_len
;
398 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX
*ctx
)
400 int rv
, len
= EVP_CIPHER_iv_length(ctx
->cipher
);
402 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
404 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &v
);
405 rv
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
406 if (rv
== EVP_CTRL_RET_UNSUPPORTED
)
408 return rv
!= 0 ? (int)v
: -1;
409 /* TODO (3.0) Remove legacy support */
411 if ((EVP_CIPHER_flags(ctx
->cipher
) & EVP_CIPH_CUSTOM_IV_LENGTH
) != 0) {
412 rv
= EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX
*)ctx
, EVP_CTRL_GET_IVLEN
,
414 return (rv
== 1) ? len
: -1;
419 int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX
*ctx
)
423 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
425 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN
, &v
);
426 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
427 return ret
== 1 ? (int)v
: 0;
430 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX
*ctx
)
433 const unsigned char *v
= ctx
->oiv
;
434 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
437 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
,
438 (void **)&v
, sizeof(ctx
->oiv
));
439 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
441 return ok
!= 0 ? v
: NULL
;
445 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
447 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX
*ctx
)
450 const unsigned char *v
= ctx
->iv
;
451 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
454 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
, (void **)&v
,
456 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
458 return ok
!= 0 ? v
: NULL
;
461 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX
*ctx
)
464 unsigned char *v
= ctx
->iv
;
465 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
468 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV
, (void **)&v
,
470 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
472 return ok
!= 0 ? v
: NULL
;
475 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX
*ctx
)
480 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX
*ctx
)
483 unsigned int v
= (unsigned int)ctx
->num
;
484 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
486 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &v
);
487 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
489 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
492 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX
*ctx
, int num
)
495 unsigned int n
= (unsigned int)num
;
496 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
498 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM
, &n
);
499 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
506 int EVP_CIPHER_key_length(const EVP_CIPHER
*cipher
)
508 return cipher
->key_len
;
511 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX
*ctx
)
514 size_t v
= ctx
->key_len
;
515 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
517 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &v
);
518 ok
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
520 return ok
!= 0 ? (int)v
: EVP_CTRL_RET_UNSUPPORTED
;
523 int EVP_CIPHER_nid(const EVP_CIPHER
*cipher
)
528 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX
*ctx
)
530 return ctx
->cipher
->nid
;
533 int EVP_CIPHER_is_a(const EVP_CIPHER
*cipher
, const char *name
)
535 if (cipher
->prov
!= NULL
)
536 return evp_is_a(cipher
->prov
, cipher
->name_id
, NULL
, name
);
537 return evp_is_a(NULL
, 0, EVP_CIPHER_name(cipher
), name
);
540 int EVP_CIPHER_number(const EVP_CIPHER
*cipher
)
542 return cipher
->name_id
;
545 const char *EVP_CIPHER_name(const EVP_CIPHER
*cipher
)
547 if (cipher
->prov
!= NULL
)
548 return evp_first_name(cipher
->prov
, cipher
->name_id
);
550 return OBJ_nid2sn(EVP_CIPHER_nid(cipher
));
556 void EVP_CIPHER_names_do_all(const EVP_CIPHER
*cipher
,
557 void (*fn
)(const char *name
, void *data
),
560 if (cipher
->prov
!= NULL
)
561 evp_names_do_all(cipher
->prov
, cipher
->name_id
, fn
, data
);
564 const OSSL_PROVIDER
*EVP_CIPHER_provider(const EVP_CIPHER
*cipher
)
569 int EVP_CIPHER_mode(const EVP_CIPHER
*cipher
)
571 return EVP_CIPHER_flags(cipher
) & EVP_CIPH_MODE
;
574 int EVP_MD_is_a(const EVP_MD
*md
, const char *name
)
576 if (md
->prov
!= NULL
)
577 return evp_is_a(md
->prov
, md
->name_id
, NULL
, name
);
578 return evp_is_a(NULL
, 0, EVP_MD_name(md
), name
);
581 int EVP_MD_number(const EVP_MD
*md
)
586 const char *EVP_MD_name(const EVP_MD
*md
)
588 if (md
->prov
!= NULL
)
589 return evp_first_name(md
->prov
, md
->name_id
);
591 return OBJ_nid2sn(EVP_MD_nid(md
));
597 void EVP_MD_names_do_all(const EVP_MD
*md
,
598 void (*fn
)(const char *name
, void *data
),
601 if (md
->prov
!= NULL
)
602 evp_names_do_all(md
->prov
, md
->name_id
, fn
, data
);
605 const OSSL_PROVIDER
*EVP_MD_provider(const EVP_MD
*md
)
610 int EVP_MD_block_size(const EVP_MD
*md
)
613 size_t v
= md
->block_size
;
614 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
617 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
621 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE
, &v
);
622 ok
= evp_do_md_getparams(md
, params
);
624 return ok
!= 0 ? (int)v
: -1;
627 int EVP_MD_type(const EVP_MD
*md
)
632 int EVP_MD_pkey_type(const EVP_MD
*md
)
634 return md
->pkey_type
;
637 int EVP_MD_size(const EVP_MD
*md
)
640 size_t v
= md
->md_size
;
641 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
644 EVPerr(EVP_F_EVP_MD_SIZE
, EVP_R_MESSAGE_DIGEST_IS_NULL
);
648 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE
, &v
);
649 ok
= evp_do_md_getparams(md
, params
);
651 return ok
!= 0 ? (int)v
: -1;
654 unsigned long EVP_MD_flags(const EVP_MD
*md
)
657 unsigned long v
= md
->flags
;
658 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
660 params
[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS
, &v
);
661 ok
= evp_do_md_getparams(md
, params
);
663 return ok
!= 0 ? v
: 0;
666 EVP_MD
*EVP_MD_meth_new(int md_type
, int pkey_type
)
668 EVP_MD
*md
= evp_md_new();
672 md
->pkey_type
= pkey_type
;
677 EVP_MD
*EVP_MD_meth_dup(const EVP_MD
*md
)
682 * Non-legacy EVP_MDs can't be duplicated like this.
683 * Use EVP_MD_up_ref() instead.
685 if (md
->prov
!= NULL
)
688 if ((to
= EVP_MD_meth_new(md
->type
, md
->pkey_type
)) != NULL
) {
689 CRYPTO_RWLOCK
*lock
= to
->lock
;
691 memcpy(to
, md
, sizeof(*to
));
697 void EVP_MD_meth_free(EVP_MD
*md
)
701 int EVP_MD_meth_set_input_blocksize(EVP_MD
*md
, int blocksize
)
703 if (md
->block_size
!= 0)
706 md
->block_size
= blocksize
;
709 int EVP_MD_meth_set_result_size(EVP_MD
*md
, int resultsize
)
711 if (md
->md_size
!= 0)
714 md
->md_size
= resultsize
;
717 int EVP_MD_meth_set_app_datasize(EVP_MD
*md
, int datasize
)
719 if (md
->ctx_size
!= 0)
722 md
->ctx_size
= datasize
;
725 int EVP_MD_meth_set_flags(EVP_MD
*md
, unsigned long flags
)
733 int EVP_MD_meth_set_init(EVP_MD
*md
, int (*init
)(EVP_MD_CTX
*ctx
))
735 if (md
->init
!= NULL
)
741 int EVP_MD_meth_set_update(EVP_MD
*md
, int (*update
)(EVP_MD_CTX
*ctx
,
745 if (md
->update
!= NULL
)
751 int EVP_MD_meth_set_final(EVP_MD
*md
, int (*final
)(EVP_MD_CTX
*ctx
,
754 if (md
->final
!= NULL
)
760 int EVP_MD_meth_set_copy(EVP_MD
*md
, int (*copy
)(EVP_MD_CTX
*to
,
761 const EVP_MD_CTX
*from
))
763 if (md
->copy
!= NULL
)
769 int EVP_MD_meth_set_cleanup(EVP_MD
*md
, int (*cleanup
)(EVP_MD_CTX
*ctx
))
771 if (md
->cleanup
!= NULL
)
774 md
->cleanup
= cleanup
;
777 int EVP_MD_meth_set_ctrl(EVP_MD
*md
, int (*ctrl
)(EVP_MD_CTX
*ctx
, int cmd
,
780 if (md
->md_ctrl
!= NULL
)
787 int EVP_MD_meth_get_input_blocksize(const EVP_MD
*md
)
789 return md
->block_size
;
791 int EVP_MD_meth_get_result_size(const EVP_MD
*md
)
795 int EVP_MD_meth_get_app_datasize(const EVP_MD
*md
)
799 unsigned long EVP_MD_meth_get_flags(const EVP_MD
*md
)
803 int (*EVP_MD_meth_get_init(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
807 int (*EVP_MD_meth_get_update(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
813 int (*EVP_MD_meth_get_final(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
,
818 int (*EVP_MD_meth_get_copy(const EVP_MD
*md
))(EVP_MD_CTX
*to
,
819 const EVP_MD_CTX
*from
)
823 int (*EVP_MD_meth_get_cleanup(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
)
827 int (*EVP_MD_meth_get_ctrl(const EVP_MD
*md
))(EVP_MD_CTX
*ctx
, int cmd
,
833 const EVP_MD
*EVP_MD_CTX_md(const EVP_MD_CTX
*ctx
)
837 return ctx
->reqdigest
;
840 EVP_PKEY_CTX
*EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX
*ctx
)
845 #if !defined(FIPS_MODE)
846 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
847 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX
*ctx
, EVP_PKEY_CTX
*pctx
)
850 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
851 * we have to deal with the cleanup job here.
853 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
))
854 EVP_PKEY_CTX_free(ctx
->pctx
);
859 /* make sure pctx is not freed when destroying EVP_MD_CTX */
860 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
862 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
865 #endif /* !defined(FIPS_MODE) */
867 void *EVP_MD_CTX_md_data(const EVP_MD_CTX
*ctx
)
872 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX
*ctx
))(EVP_MD_CTX
*ctx
,
873 const void *data
, size_t count
)
878 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX
*ctx
,
879 int (*update
) (EVP_MD_CTX
*ctx
,
880 const void *data
, size_t count
))
882 ctx
->update
= update
;
885 void EVP_MD_CTX_set_flags(EVP_MD_CTX
*ctx
, int flags
)
890 void EVP_MD_CTX_clear_flags(EVP_MD_CTX
*ctx
, int flags
)
892 ctx
->flags
&= ~flags
;
895 int EVP_MD_CTX_test_flags(const EVP_MD_CTX
*ctx
, int flags
)
897 return (ctx
->flags
& flags
);
900 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
905 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX
*ctx
, int flags
)
907 ctx
->flags
&= ~flags
;
910 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX
*ctx
, int flags
)
912 return (ctx
->flags
& flags
);
915 int EVP_str2ctrl(int (*cb
)(void *ctx
, int cmd
, void *buf
, size_t buflen
),
916 void *ctx
, int cmd
, const char *value
)
923 return cb(ctx
, cmd
, (void *)value
, len
);
926 int EVP_hex2ctrl(int (*cb
)(void *ctx
, int cmd
, void *buf
, size_t buflen
),
927 void *ctx
, int cmd
, const char *hex
)
933 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
936 if (binlen
<= INT_MAX
)
937 rv
= cb(ctx
, cmd
, bin
, binlen
);