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 "internal/evp_int.h"
18 #include "internal/provider.h"
21 #if !defined(FIPS_MODE)
22 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
25 const EVP_CIPHER *cipher = c->cipher;
27 if (cipher->prov != NULL) {
29 * The cipher has come from a provider and won't have the default flags.
30 * Find the implicit form so we can check the flags.
31 * TODO(3.0): This won't work for 3rd party ciphers we know nothing about
32 * We'll need to think of something else for those.
34 cipher = EVP_get_cipherbynid(cipher->nid);
36 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ASN1_R_UNSUPPORTED_CIPHER);
41 if (cipher->set_asn1_parameters != NULL)
42 ret = cipher->set_asn1_parameters(c, type);
43 else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) {
44 switch (EVP_CIPHER_mode(cipher)) {
45 case EVP_CIPH_WRAP_MODE:
46 if (EVP_CIPHER_nid(cipher) == NID_id_smime_alg_CMS3DESwrap)
47 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
51 case EVP_CIPH_GCM_MODE:
52 case EVP_CIPH_CCM_MODE:
53 case EVP_CIPH_XTS_MODE:
54 case EVP_CIPH_OCB_MODE:
59 ret = EVP_CIPHER_set_asn1_iv(c, type);
64 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ret == -2 ?
65 ASN1_R_UNSUPPORTED_CIPHER :
66 EVP_R_CIPHER_PARAMETER_ERROR);
72 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
75 const EVP_CIPHER *cipher = c->cipher;
77 if (cipher->prov != NULL) {
79 * The cipher has come from a provider and won't have the default flags.
80 * Find the implicit form so we can check the flags.
82 cipher = EVP_get_cipherbynid(cipher->nid);
87 if (cipher->get_asn1_parameters != NULL)
88 ret = cipher->get_asn1_parameters(c, type);
89 else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) {
90 switch (EVP_CIPHER_mode(cipher)) {
92 case EVP_CIPH_WRAP_MODE:
96 case EVP_CIPH_GCM_MODE:
97 case EVP_CIPH_CCM_MODE:
98 case EVP_CIPH_XTS_MODE:
99 case EVP_CIPH_OCB_MODE:
104 ret = EVP_CIPHER_get_asn1_iv(c, type);
110 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, ret == -2 ?
111 EVP_R_UNSUPPORTED_CIPHER :
112 EVP_R_CIPHER_PARAMETER_ERROR);
118 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
124 unsigned char iv[EVP_MAX_IV_LENGTH];
126 l = EVP_CIPHER_CTX_iv_length(ctx);
127 if (!ossl_assert(l <= sizeof(iv)))
129 i = ASN1_TYPE_get_octetstring(type, iv, l);
133 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
139 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
145 j = EVP_CIPHER_CTX_iv_length(c);
146 OPENSSL_assert(j <= sizeof(c->iv));
147 i = ASN1_TYPE_set_octetstring(type, c->oiv, j);
151 #endif /* !defined(FIPS_MODE) */
153 /* Convert the various cipher NIDs and dummies to a proper OID NID */
154 int EVP_CIPHER_type(const EVP_CIPHER *ctx)
157 nid = EVP_CIPHER_nid(ctx);
172 case NID_aes_128_cfb128:
173 case NID_aes_128_cfb8:
174 case NID_aes_128_cfb1:
176 return NID_aes_128_cfb128;
178 case NID_aes_192_cfb128:
179 case NID_aes_192_cfb8:
180 case NID_aes_192_cfb1:
182 return NID_aes_192_cfb128;
184 case NID_aes_256_cfb128:
185 case NID_aes_256_cfb8:
186 case NID_aes_256_cfb1:
188 return NID_aes_256_cfb128;
194 return NID_des_cfb64;
196 case NID_des_ede3_cfb64:
197 case NID_des_ede3_cfb8:
198 case NID_des_ede3_cfb1:
200 return NID_des_cfb64;
207 /* Check it has an OID and it is valid */
208 ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
210 if (OBJ_get0_data(otmp) == NULL)
212 ASN1_OBJECT_free(otmp);
219 int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
221 int ok, v = cipher->block_size;
222 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
224 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_BLOCK_SIZE, &v);
225 ok = evp_do_ciph_getparams(cipher, params);
227 return ok != 0 ? v : -1;
230 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
232 return EVP_CIPHER_block_size(ctx->cipher);
235 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
240 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
241 const unsigned char *in, unsigned int inl)
243 if (ctx->cipher->prov != NULL) {
244 size_t outl = 0; /* ignored */
245 int blocksize = EVP_CIPHER_CTX_block_size(ctx);
247 if (ctx->cipher->ccipher != NULL)
249 ctx->cipher->ccipher(ctx->provctx, out, &outl,
250 inl + (blocksize == 1 ? 0 : blocksize),
255 return ctx->cipher->do_cipher(ctx, out, in, inl);
258 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
263 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
268 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
271 unsigned long v = cipher->flags;
272 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
274 params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
275 ok = evp_do_ciph_getparams(cipher, params);
277 return ok != 0 ? v : 0;
280 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
282 return ctx->app_data;
285 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
287 ctx->app_data = data;
290 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
292 return ctx->cipher_data;
295 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
297 void *old_cipher_data;
299 old_cipher_data = ctx->cipher_data;
300 ctx->cipher_data = cipher_data;
302 return old_cipher_data;
305 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
307 int ok, v = cipher->iv_len;
308 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
310 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_IVLEN, &v);
311 ok = evp_do_ciph_getparams(cipher, params);
313 return ok != 0 ? v : -1;
316 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
318 return EVP_CIPHER_iv_length(ctx->cipher);
321 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
327 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
329 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
332 const unsigned char *v = ctx->iv;
333 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
336 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
338 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
340 return ok != 0 ? v : NULL;
343 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
346 unsigned char *v = ctx->iv;
347 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
350 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
352 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
354 return ok != 0 ? v : NULL;
357 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
362 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
364 int ok, v = ctx->num;
365 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
367 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_NUM, &v);
368 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
370 return ok != 0 ? v : -1;
373 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
376 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
378 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_NUM, &num);
379 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
386 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
388 int ok, v = cipher->key_len;
389 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
391 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &v);
392 ok = evp_do_ciph_getparams(cipher, params);
394 return ok != 0 ? v : -1;
397 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
399 int ok, v = ctx->key_len;
400 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
402 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &v);
403 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
405 return ok != 0 ? v : -1;
408 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
413 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
415 return ctx->cipher->nid;
418 const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
420 if (cipher->prov != NULL)
423 return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
429 const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher)
434 int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
436 int ok, v = EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
437 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
439 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_MODE, &v);
440 ok = evp_do_ciph_getparams(cipher, params);
442 return ok != 0 ? v : 0;
445 const char *EVP_MD_name(const EVP_MD *md)
447 if (md->prov != NULL)
450 return OBJ_nid2sn(EVP_MD_nid(md));
456 const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
461 int EVP_MD_block_size(const EVP_MD *md)
464 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
468 if (md->prov != NULL && md->dblock_size != NULL)
469 return (int)md->dblock_size();
471 return md->block_size;
474 int EVP_MD_type(const EVP_MD *md)
479 int EVP_MD_pkey_type(const EVP_MD *md)
481 return md->pkey_type;
484 int EVP_MD_size(const EVP_MD *md)
487 EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
491 if (md->prov != NULL && md->size != NULL)
492 return (int)md->size();
497 unsigned long EVP_MD_flags(const EVP_MD *md)
502 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
504 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
508 md->pkey_type = pkey_type;
509 md->lock = CRYPTO_THREAD_lock_new();
510 if (md->lock == NULL) {
519 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
521 EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
524 CRYPTO_RWLOCK *lock = to->lock;
525 memcpy(to, md, sizeof(*to));
531 int EVP_MD_up_ref(EVP_MD *md)
535 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
539 void EVP_MD_meth_free(EVP_MD *md)
544 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
547 ossl_provider_free(md->prov);
548 OPENSSL_free(md->name);
549 CRYPTO_THREAD_lock_free(md->lock);
553 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
555 md->block_size = blocksize;
558 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
560 md->md_size = resultsize;
563 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
565 md->ctx_size = datasize;
568 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
573 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
578 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
585 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
591 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
592 const EVP_MD_CTX *from))
597 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
599 md->cleanup = cleanup;
602 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
609 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
611 return md->block_size;
613 int EVP_MD_meth_get_result_size(const EVP_MD *md)
617 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
621 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
625 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
629 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
635 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
640 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
641 const EVP_MD_CTX *from)
645 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
649 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
655 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
659 return ctx->reqdigest;
662 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
667 #if !defined(FIPS_MODE)
668 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
669 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
672 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
673 * we have to deal with the cleanup job here.
675 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
676 EVP_PKEY_CTX_free(ctx->pctx);
681 /* make sure pctx is not freed when destroying EVP_MD_CTX */
682 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
684 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
687 #endif /* !defined(FIPS_MODE) */
689 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
694 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
695 const void *data, size_t count)
700 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
701 int (*update) (EVP_MD_CTX *ctx,
702 const void *data, size_t count))
704 ctx->update = update;
707 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
712 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
714 ctx->flags &= ~flags;
717 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
719 return (ctx->flags & flags);
722 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
727 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
729 ctx->flags &= ~flags;
732 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
734 return (ctx->flags & flags);
737 int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
738 void *ctx, int cmd, const char *value)
745 return cb(ctx, cmd, (void *)value, len);
748 int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
749 void *ctx, int cmd, const char *hex)
755 bin = OPENSSL_hexstr2buf(hex, &binlen);
758 if (binlen <= INT_MAX)
759 rv = cb(ctx, cmd, bin, binlen);
765 # ifndef OPENSSL_NO_DH
767 * TODO(3.0): Temporarily unavailable in FIPS mode. This will need to be added
771 # define MAX_PARAMS 10
773 /* Number of the current param */
776 /* Key for the current param */
778 /* Value for the current param */
779 const BIGNUM *bnparam;
780 /* Size of the buffer required for the BN */
782 } params[MAX_PARAMS];
783 /* Running count of the total size required */
788 static int push_param_bn(PARAMS_TEMPLATE *tmpl, const char *key,
793 sz = BN_num_bytes(bn);
796 tmpl->params[tmpl->curr].key = key;
797 tmpl->params[tmpl->curr].bnparam = bn;
798 tmpl->params[tmpl->curr++].bufsz = (size_t)sz;
799 tmpl->totsz += sizeof(OSSL_PARAM) + (size_t)sz;
804 static OSSL_PARAM *param_template_to_param(PARAMS_TEMPLATE *tmpl, size_t *sz)
808 OSSL_PARAM *param = NULL;
809 unsigned char *currbuf = NULL;
811 if (tmpl->totsz == 0)
814 /* Add some space for the end of OSSL_PARAM marker */
815 tmpl->totsz += sizeof(*param);
818 buf = OPENSSL_zalloc(tmpl->totsz);
820 buf = OPENSSL_secure_zalloc(tmpl->totsz);
825 currbuf = (unsigned char *)buf + (sizeof(*param) * (tmpl->curr + 1));
827 for (i = 0; i < tmpl->curr; i++) {
828 if (!ossl_assert((currbuf - (unsigned char *)buf )
829 + tmpl->params[i].bufsz <= tmpl->totsz))
831 if (BN_bn2nativepad(tmpl->params[i].bnparam, currbuf,
832 tmpl->params[i].bufsz) < 0)
834 param[i] = OSSL_PARAM_construct_BN(tmpl->params[i].key, currbuf,
835 tmpl->params[i].bufsz);
836 currbuf += tmpl->params[i].bufsz;
838 param[i] = OSSL_PARAM_construct_end();
848 OPENSSL_clear_free(param, tmpl->totsz);
852 static OSSL_PARAM *evp_pkey_dh_to_param(EVP_PKEY *pkey, size_t *sz)
854 DH *dh = pkey->pkey.dh;
855 PARAMS_TEMPLATE tmpl = {0};
856 const BIGNUM *p = DH_get0_p(dh), *g = DH_get0_g(dh), *q = DH_get0_q(dh);
857 const BIGNUM *pub_key = DH_get0_pub_key(dh);
858 const BIGNUM *priv_key = DH_get0_priv_key(dh);
860 if (p == NULL || g == NULL || pub_key == NULL)
863 if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_P, p)
864 || !push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_G, g)
865 || !push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_PUB_KEY, pub_key))
869 if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_Q, q))
873 if (priv_key != NULL) {
874 if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_PRIV_KEY, priv_key))
880 return param_template_to_param(&tmpl, sz);
882 # endif /* OPENSSL_NO_DH */
884 OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz)
886 switch (pkey->type) {
887 # ifndef OPENSSL_NO_DH
889 return evp_pkey_dh_to_param(pkey, sz);
896 #endif /* FIPS_MODE */