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 #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 <openssl/ec.h>
18 #include "crypto/evp.h"
19 #include "internal/provider.h"
20 #include "evp_local.h"
22 #if !defined(FIPS_MODULE)
23 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
25 int ret = -1; /* Assume the worst */
26 const EVP_CIPHER *cipher = c->cipher;
29 * For legacy implementations, we detect custom AlgorithmIdentifier
30 * parameter handling by checking if the function pointer
31 * cipher->set_asn1_parameters is set. We know that this pointer
32 * is NULL for provided implementations.
34 * Otherwise, for any implementation, we check the flag
35 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
36 * default AI parameter extraction.
38 * Otherwise, for provided implementations, we convert |type| to
39 * a DER encoded blob and pass to the implementation in OSSL_PARAM
42 * If none of the above applies, this operation is unsupported.
44 if (cipher->set_asn1_parameters != NULL) {
45 ret = cipher->set_asn1_parameters(c, type);
46 } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
47 switch (EVP_CIPHER_mode(cipher)) {
48 case EVP_CIPH_WRAP_MODE:
49 if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
50 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
54 case EVP_CIPH_GCM_MODE:
55 case EVP_CIPH_CCM_MODE:
56 case EVP_CIPH_XTS_MODE:
57 case EVP_CIPH_OCB_MODE:
62 ret = EVP_CIPHER_set_asn1_iv(c, type);
64 } else if (cipher->prov != NULL) {
65 OSSL_PARAM params[3], *p = params;
66 unsigned char *der = NULL, *derp;
69 * We make two passes, the first to get the appropriate buffer size,
70 * and the second to get the actual value.
72 *p++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID,
74 *p = OSSL_PARAM_construct_end();
76 if (!EVP_CIPHER_CTX_get_params(c, params))
79 /* ... but, we should get a return size too! */
80 if (OSSL_PARAM_modified(params)
81 && params[0].return_size != 0
82 && (der = OPENSSL_malloc(params[0].return_size)) != NULL) {
84 params[0].data_size = params[0].return_size;
85 OSSL_PARAM_set_all_unmodified(params);
87 if (EVP_CIPHER_CTX_get_params(c, params)
88 && OSSL_PARAM_modified(params)
89 && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
90 params[0].return_size) != NULL) {
101 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ASN1_R_UNSUPPORTED_CIPHER);
103 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, EVP_R_CIPHER_PARAMETER_ERROR);
109 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
111 int ret = -1; /* Assume the worst */
112 const EVP_CIPHER *cipher = c->cipher;
115 * For legacy implementations, we detect custom AlgorithmIdentifier
116 * parameter handling by checking if there the function pointer
117 * cipher->get_asn1_parameters is set. We know that this pointer
118 * is NULL for provided implementations.
120 * Otherwise, for any implementation, we check the flag
121 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
122 * default AI parameter creation.
124 * Otherwise, for provided implementations, we get the AI parameter
125 * in DER encoded form from the implementation by requesting the
126 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
128 * If none of the above applies, this operation is unsupported.
130 if (cipher->get_asn1_parameters != NULL) {
131 ret = cipher->get_asn1_parameters(c, type);
132 } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
133 switch (EVP_CIPHER_mode(cipher)) {
134 case EVP_CIPH_WRAP_MODE:
138 case EVP_CIPH_GCM_MODE:
139 case EVP_CIPH_CCM_MODE:
140 case EVP_CIPH_XTS_MODE:
141 case EVP_CIPH_OCB_MODE:
146 ret = EVP_CIPHER_get_asn1_iv(c, type);
148 } else if (cipher->prov != NULL) {
149 OSSL_PARAM params[3], *p = params;
150 unsigned char *der = NULL;
153 if ((derl = i2d_ASN1_TYPE(type, &der)) >= 0) {
155 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID,
157 *p = OSSL_PARAM_construct_end();
158 if (EVP_CIPHER_CTX_set_params(c, params))
167 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, EVP_R_UNSUPPORTED_CIPHER);
169 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, EVP_R_CIPHER_PARAMETER_ERROR);
175 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
181 unsigned char iv[EVP_MAX_IV_LENGTH];
183 l = EVP_CIPHER_CTX_iv_length(ctx);
184 if (!ossl_assert(l <= sizeof(iv)))
186 i = ASN1_TYPE_get_octetstring(type, iv, l);
190 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
196 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
200 unsigned char *oiv = NULL;
203 oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
204 j = EVP_CIPHER_CTX_iv_length(c);
205 OPENSSL_assert(j <= sizeof(c->iv));
206 i = ASN1_TYPE_set_octetstring(type, oiv, j);
210 #endif /* !defined(FIPS_MODULE) */
212 /* Convert the various cipher NIDs and dummies to a proper OID NID */
213 int EVP_CIPHER_type(const EVP_CIPHER *ctx)
216 nid = EVP_CIPHER_nid(ctx);
231 case NID_aes_128_cfb128:
232 case NID_aes_128_cfb8:
233 case NID_aes_128_cfb1:
235 return NID_aes_128_cfb128;
237 case NID_aes_192_cfb128:
238 case NID_aes_192_cfb8:
239 case NID_aes_192_cfb1:
241 return NID_aes_192_cfb128;
243 case NID_aes_256_cfb128:
244 case NID_aes_256_cfb8:
245 case NID_aes_256_cfb1:
247 return NID_aes_256_cfb128;
253 return NID_des_cfb64;
255 case NID_des_ede3_cfb64:
256 case NID_des_ede3_cfb8:
257 case NID_des_ede3_cfb1:
259 return NID_des_cfb64;
266 /* Check it has an OID and it is valid */
267 ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
269 if (OBJ_get0_data(otmp) == NULL)
271 ASN1_OBJECT_free(otmp);
278 int evp_cipher_cache_constants(EVP_CIPHER *cipher)
284 unsigned int mode = 0;
285 unsigned long flags = 0;
286 OSSL_PARAM params[6];
288 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
289 params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
290 params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
291 params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
292 params[4] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &flags);
293 params[5] = OSSL_PARAM_construct_end();
294 ok = evp_do_ciph_getparams(cipher, params);
296 /* Provided implementations may have a custom cipher_cipher */
297 if (cipher->prov != NULL && cipher->ccipher != NULL)
298 flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
299 cipher->block_size = blksz;
300 cipher->iv_len = ivlen;
301 cipher->key_len = keylen;
302 cipher->flags = flags | mode;
307 int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
309 return cipher->block_size;
312 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
314 return EVP_CIPHER_block_size(ctx->cipher);
317 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
322 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
323 const unsigned char *in, unsigned int inl)
325 if (ctx->cipher->prov != NULL) {
327 * If the provided implementation has a ccipher function, we use it,
328 * and translate its return value like this: 0 => -1, 1 => outlen
330 * Otherwise, we call the cupdate function if in != NULL, or cfinal
331 * if in == NULL. Regardless of which, we return what we got.
335 size_t blocksize = EVP_CIPHER_CTX_block_size(ctx);
337 if (ctx->cipher->ccipher != NULL)
338 ret = ctx->cipher->ccipher(ctx->provctx, out, &outl,
339 inl + (blocksize == 1 ? 0 : blocksize),
343 ret = ctx->cipher->cupdate(ctx->provctx, out, &outl,
344 inl + (blocksize == 1 ? 0 : blocksize),
347 ret = ctx->cipher->cfinal(ctx->provctx, out, &outl,
348 blocksize == 1 ? 0 : blocksize);
353 return ctx->cipher->do_cipher(ctx, out, in, inl);
356 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
361 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
366 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
368 return cipher->flags;
371 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
373 return ctx->app_data;
376 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
378 ctx->app_data = data;
381 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
383 return ctx->cipher_data;
386 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
388 void *old_cipher_data;
390 old_cipher_data = ctx->cipher_data;
391 ctx->cipher_data = cipher_data;
393 return old_cipher_data;
396 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
398 return cipher->iv_len;
401 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
403 int rv, len = EVP_CIPHER_iv_length(ctx->cipher);
405 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
407 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
408 rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
409 if (rv == EVP_CTRL_RET_UNSUPPORTED)
411 return rv != 0 ? (int)v : -1;
412 /* TODO (3.0) Remove legacy support */
414 if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
415 rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
417 return (rv == 1) ? len : -1;
422 int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX *ctx)
426 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
428 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
429 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
430 return ret == 1 ? (int)v : 0;
433 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
436 const unsigned char *v = ctx->oiv;
437 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
440 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
441 (void **)&v, sizeof(ctx->oiv));
442 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
444 return ok != 0 ? v : NULL;
448 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
450 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
453 const unsigned char *v = ctx->iv;
454 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
457 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
459 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
461 return ok != 0 ? v : NULL;
464 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
467 unsigned char *v = ctx->iv;
468 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
471 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
473 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
475 return ok != 0 ? v : NULL;
478 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
483 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
486 unsigned int v = (unsigned int)ctx->num;
487 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
489 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
490 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
492 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
495 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
498 unsigned int n = (unsigned int)num;
499 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
501 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
502 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
509 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
511 return cipher->key_len;
514 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
517 size_t v = ctx->key_len;
518 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
520 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &v);
521 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
523 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
526 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
531 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
533 return ctx->cipher->nid;
536 int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
538 if (cipher->prov != NULL)
539 return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
540 return evp_is_a(NULL, 0, EVP_CIPHER_name(cipher), name);
543 int EVP_CIPHER_number(const EVP_CIPHER *cipher)
545 return cipher->name_id;
548 const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
550 if (cipher->prov != NULL)
551 return evp_first_name(cipher->prov, cipher->name_id);
553 return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
559 void EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
560 void (*fn)(const char *name, void *data),
563 if (cipher->prov != NULL)
564 evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
567 const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher)
572 int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
574 return EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
577 int EVP_MD_is_a(const EVP_MD *md, const char *name)
579 if (md->prov != NULL)
580 return evp_is_a(md->prov, md->name_id, NULL, name);
581 return evp_is_a(NULL, 0, EVP_MD_name(md), name);
584 int EVP_MD_number(const EVP_MD *md)
589 const char *EVP_MD_name(const EVP_MD *md)
591 if (md->prov != NULL)
592 return evp_first_name(md->prov, md->name_id);
594 return OBJ_nid2sn(EVP_MD_nid(md));
600 void EVP_MD_names_do_all(const EVP_MD *md,
601 void (*fn)(const char *name, void *data),
604 if (md->prov != NULL)
605 evp_names_do_all(md->prov, md->name_id, fn, data);
608 const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
613 int EVP_MD_block_size(const EVP_MD *md)
616 size_t v = md->block_size;
617 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
620 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
624 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &v);
625 ok = evp_do_md_getparams(md, params);
627 return ok != 0 ? (int)v : -1;
630 int EVP_MD_type(const EVP_MD *md)
635 int EVP_MD_pkey_type(const EVP_MD *md)
637 return md->pkey_type;
640 int EVP_MD_size(const EVP_MD *md)
643 size_t v = md->md_size;
644 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
647 EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
651 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &v);
652 ok = evp_do_md_getparams(md, params);
654 return ok != 0 ? (int)v : -1;
657 unsigned long EVP_MD_flags(const EVP_MD *md)
660 unsigned long v = md->flags;
661 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
663 params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
664 ok = evp_do_md_getparams(md, params);
666 return ok != 0 ? v : 0;
669 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
671 EVP_MD *md = evp_md_new();
675 md->pkey_type = pkey_type;
680 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
685 * Non-legacy EVP_MDs can't be duplicated like this.
686 * Use EVP_MD_up_ref() instead.
688 if (md->prov != NULL)
691 if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
692 CRYPTO_RWLOCK *lock = to->lock;
694 memcpy(to, md, sizeof(*to));
700 void EVP_MD_meth_free(EVP_MD *md)
704 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
706 if (md->block_size != 0)
709 md->block_size = blocksize;
712 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
714 if (md->md_size != 0)
717 md->md_size = resultsize;
720 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
722 if (md->ctx_size != 0)
725 md->ctx_size = datasize;
728 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
736 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
738 if (md->init != NULL)
744 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
748 if (md->update != NULL)
754 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
757 if (md->final != NULL)
763 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
764 const EVP_MD_CTX *from))
766 if (md->copy != NULL)
772 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
774 if (md->cleanup != NULL)
777 md->cleanup = cleanup;
780 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
783 if (md->md_ctrl != NULL)
790 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
792 return md->block_size;
794 int EVP_MD_meth_get_result_size(const EVP_MD *md)
798 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
802 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
806 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
810 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
816 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
821 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
822 const EVP_MD_CTX *from)
826 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
830 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
836 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
840 return ctx->reqdigest;
843 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
848 #if !defined(FIPS_MODULE)
849 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
850 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
853 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
854 * we have to deal with the cleanup job here.
856 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
857 EVP_PKEY_CTX_free(ctx->pctx);
862 /* make sure pctx is not freed when destroying EVP_MD_CTX */
863 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
865 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
868 #endif /* !defined(FIPS_MODULE) */
870 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
875 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
876 const void *data, size_t count)
881 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
882 int (*update) (EVP_MD_CTX *ctx,
883 const void *data, size_t count))
885 ctx->update = update;
888 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
893 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
895 ctx->flags &= ~flags;
898 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
900 return (ctx->flags & flags);
903 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
908 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
910 ctx->flags &= ~flags;
913 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
915 return (ctx->flags & flags);
918 int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
919 void *ctx, int cmd, const char *value)
926 return cb(ctx, cmd, (void *)value, len);
929 int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
930 void *ctx, int cmd, const char *hex)
936 bin = OPENSSL_hexstr2buf(hex, &binlen);
939 if (binlen <= INT_MAX)
940 rv = cb(ctx, cmd, bin, binlen);
945 int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
947 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
948 OSSL_PARAM *p = params;
951 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
952 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
956 if (!EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
960 /* Could be a legacy key, try and convert to a ctrl */
961 if (ctx->pmeth != NULL && (nid = OBJ_txt2nid(name)) != NID_undef) {
962 # ifndef OPENSSL_NO_DH
963 if (ctx->pmeth->pkey_id == EVP_PKEY_DH)
964 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH,
966 | EVP_PKEY_OP_KEYGEN,
967 EVP_PKEY_CTRL_DH_NID, nid, NULL);
969 # ifndef OPENSSL_NO_EC
970 if (ctx->pmeth->pkey_id == EVP_PKEY_EC)
971 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
972 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN,
973 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID,
978 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
979 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
986 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
988 return EVP_PKEY_CTX_set_params(ctx, params);
991 int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
993 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
994 OSSL_PARAM *p = params;
996 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
997 /* There is no legacy support for this */
998 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
999 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1006 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1008 if (!EVP_PKEY_CTX_get_params(ctx, params))