diff options
author | slontis <shane.lontis@oracle.com> | 2022-07-15 21:22:01 +1000 |
---|---|---|
committer | Hugo Landau <hlandau@openssl.org> | 2022-11-30 07:31:53 +0000 |
commit | f3090fc710e30a749acaf9e5dfbe20dd163cf15d (patch) | |
tree | 720d4b3cada6e81a69a2b2b68f6e8cf592c3e003 /providers/implementations | |
parent | 9ba4f489ecd30901603d66a8ec578cbca08fac06 (diff) |
Implement deterministic ECDSA sign (RFC6979)
This PR is based off the contributions in PR #9223 by Jemmy1228.
It has been modified and reworked to:
(1) Work with providers
(2) Support ECDSA and DSA
(3) Add a KDF HMAC_DRBG implementation that shares code with the RAND HMAC_DRBG.
A nonce_type is passed around inside the Signing API's, in order to support any
future deterministic algorithms.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/18809)
Diffstat (limited to 'providers/implementations')
-rw-r--r-- | providers/implementations/include/prov/hmac_drbg.h | 33 | ||||
-rw-r--r-- | providers/implementations/include/prov/implementations.h | 1 | ||||
-rw-r--r-- | providers/implementations/include/prov/names.h | 1 | ||||
-rw-r--r-- | providers/implementations/kdfs/build.info | 3 | ||||
-rw-r--r-- | providers/implementations/kdfs/hmacdrbg_kdf.c | 259 | ||||
-rw-r--r-- | providers/implementations/rands/drbg_hmac.c | 61 | ||||
-rw-r--r-- | providers/implementations/rands/drbg_local.h | 2 | ||||
-rw-r--r-- | providers/implementations/signature/dsa_sig.c | 12 | ||||
-rw-r--r-- | providers/implementations/signature/ecdsa_sig.c | 18 |
9 files changed, 359 insertions, 31 deletions
diff --git a/providers/implementations/include/prov/hmac_drbg.h b/providers/implementations/include/prov/hmac_drbg.h new file mode 100644 index 0000000000..28aa5bc1ad --- /dev/null +++ b/providers/implementations/include/prov/hmac_drbg.h @@ -0,0 +1,33 @@ +/* + * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef OSSL_PROV_HMAC_DRBG_H +# define OSSL_PROV_HMAC_DRBG_H +# pragma once + +#include <openssl/evp.h> +#include "prov/provider_util.h" + +typedef struct drbg_hmac_st { + EVP_MAC_CTX *ctx; /* H(x) = HMAC_hash OR H(x) = KMAC */ + PROV_DIGEST digest; /* H(x) = hash(x) */ + size_t blocklen; + unsigned char K[EVP_MAX_MD_SIZE]; + unsigned char V[EVP_MAX_MD_SIZE]; +} PROV_DRBG_HMAC; + +int ossl_drbg_hmac_init(PROV_DRBG_HMAC *drbg, + const unsigned char *ent, size_t ent_len, + const unsigned char *nonce, size_t nonce_len, + const unsigned char *pstr, size_t pstr_len); +int ossl_drbg_hmac_generate(PROV_DRBG_HMAC *hmac, + unsigned char *out, size_t outlen, + const unsigned char *adin, size_t adin_len); + +#endif /* OSSL_PROV_HMAC_DRBG_H */ diff --git a/providers/implementations/include/prov/implementations.h b/providers/implementations/include/prov/implementations.h index 1b887059c7..d552b895fa 100644 --- a/providers/implementations/include/prov/implementations.h +++ b/providers/implementations/include/prov/implementations.h @@ -279,6 +279,7 @@ extern const OSSL_DISPATCH ossl_kdf_x963_kdf_functions[]; extern const OSSL_DISPATCH ossl_kdf_kbkdf_functions[]; extern const OSSL_DISPATCH ossl_kdf_x942_kdf_functions[]; extern const OSSL_DISPATCH ossl_kdf_krb5kdf_functions[]; +extern const OSSL_DISPATCH ossl_kdf_hmac_drbg_functions[]; /* RNGs */ extern const OSSL_DISPATCH ossl_test_rng_functions[]; diff --git a/providers/implementations/include/prov/names.h b/providers/implementations/include/prov/names.h index fa9715d5cd..20978a3c11 100644 --- a/providers/implementations/include/prov/names.h +++ b/providers/implementations/include/prov/names.h @@ -278,6 +278,7 @@ #define PROV_NAMES_SCRYPT "SCRYPT:id-scrypt:1.3.6.1.4.1.11591.4.11" #define PROV_DESCS_SCRYPT_SIGN "OpenSSL SCRYPT via EVP_PKEY implementation" #define PROV_NAMES_KRB5KDF "KRB5KDF" +#define PROV_NAMES_HMAC_DRBG_KDF "HMAC-DRBG-KDF" /*- * MACs diff --git a/providers/implementations/kdfs/build.info b/providers/implementations/kdfs/build.info index ddc3eabca2..aa8b9da969 100644 --- a/providers/implementations/kdfs/build.info +++ b/providers/implementations/kdfs/build.info @@ -13,6 +13,7 @@ $SSKDF_GOAL=../../libdefault.a ../../libfips.a $SCRYPT_GOAL=../../libdefault.a $SSHKDF_GOAL=../../libdefault.a ../../libfips.a $X942KDF_GOAL=../../libdefault.a ../../libfips.a +$HMAC_DRBG_KDF_GOAL=../../libdefault.a SOURCE[$TLS1_PRF_GOAL]=tls1_prf.c @@ -39,3 +40,5 @@ SOURCE[$SCRYPT_GOAL]=scrypt.c SOURCE[$SSHKDF_GOAL]=sshkdf.c SOURCE[$X942KDF_GOAL]=x942kdf.c DEPEND[x942kdf.o]=../../common/include/prov/der_wrap.h + +SOURCE[$HMAC_DRBG_KDF_GOAL]=hmacdrbg_kdf.c diff --git a/providers/implementations/kdfs/hmacdrbg_kdf.c b/providers/implementations/kdfs/hmacdrbg_kdf.c new file mode 100644 index 0000000000..6a37541120 --- /dev/null +++ b/providers/implementations/kdfs/hmacdrbg_kdf.c @@ -0,0 +1,259 @@ +/* + * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include <stdlib.h> +#include <string.h> +#include <openssl/crypto.h> +#include <openssl/err.h> +#include <openssl/kdf.h> +#include <openssl/proverr.h> +#include <openssl/core_names.h> +#include "prov/providercommon.h" +#include "prov/implementations.h" +#include "prov/hmac_drbg.h" +#include "prov/provider_ctx.h" + +static OSSL_FUNC_kdf_newctx_fn hmac_drbg_kdf_new; +static OSSL_FUNC_kdf_dupctx_fn hmac_drbg_kdf_dup; +static OSSL_FUNC_kdf_freectx_fn hmac_drbg_kdf_free; +static OSSL_FUNC_kdf_reset_fn hmac_drbg_kdf_reset; +static OSSL_FUNC_kdf_derive_fn hmac_drbg_kdf_derive; +static OSSL_FUNC_kdf_settable_ctx_params_fn hmac_drbg_kdf_settable_ctx_params; +static OSSL_FUNC_kdf_set_ctx_params_fn hmac_drbg_kdf_set_ctx_params; +static OSSL_FUNC_kdf_gettable_ctx_params_fn hmac_drbg_kdf_gettable_ctx_params; +static OSSL_FUNC_kdf_get_ctx_params_fn hmac_drbg_kdf_get_ctx_params; + +typedef struct { + PROV_DRBG_HMAC base; + void *provctx; + unsigned char *entropy, *nonce; + size_t entropylen, noncelen; + int init; +} KDF_HMAC_DRBG; + +static void *hmac_drbg_kdf_new(void *provctx) +{ + KDF_HMAC_DRBG *ctx; + + if (!ossl_prov_is_running()) + return NULL; + + ctx = OPENSSL_zalloc(sizeof(*ctx)); + if (ctx == NULL) { + ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); + return NULL; + } + ctx->provctx = provctx; + return ctx; +} + +static void hmac_drbg_kdf_reset(void *vctx) +{ + KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx; + PROV_DRBG_HMAC *drbg = &ctx->base; + void *provctx = ctx->provctx; + + EVP_MAC_CTX_free(drbg->ctx); + ossl_prov_digest_reset(&drbg->digest); + OPENSSL_clear_free(ctx->entropy, ctx->entropylen); + OPENSSL_clear_free(ctx->nonce, ctx->noncelen); + OPENSSL_cleanse(ctx, sizeof(*ctx)); + ctx->provctx = provctx; +} + +static void hmac_drbg_kdf_free(void *vctx) +{ + KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx; + + if (ctx != NULL) { + hmac_drbg_kdf_reset(ctx); + OPENSSL_free(ctx); + } +} + +static int ossl_drbg_hmac_dup(PROV_DRBG_HMAC *dst, const PROV_DRBG_HMAC *src) { + if (src->ctx != NULL) { + dst->ctx = EVP_MAC_CTX_dup(src->ctx); + if (dst->ctx == NULL) + return 0; + } + if (!ossl_prov_digest_copy(&dst->digest, &src->digest)) + return 0; + memcpy(dst->K, src->K, sizeof(dst->K)); + memcpy(dst->V, src->V, sizeof(dst->V)); + dst->blocklen = src->blocklen; + return 1; +} + +static void *hmac_drbg_kdf_dup(void *vctx) +{ + const KDF_HMAC_DRBG *src = (const KDF_HMAC_DRBG *)vctx; + KDF_HMAC_DRBG *dst; + + dst = hmac_drbg_kdf_new(src->provctx); + if (dst != NULL) { + if (!ossl_drbg_hmac_dup(&dst->base, &src->base) + || !ossl_prov_memdup(src->entropy, src->entropylen, + &dst->entropy , &dst->entropylen) + || !ossl_prov_memdup(src->nonce, src->noncelen, + &dst->nonce, &dst->noncelen)) + goto err; + dst->init = src->init; + } + return dst; + + err: + hmac_drbg_kdf_free(dst); + return NULL; +} + +static int hmac_drbg_kdf_derive(void *vctx, unsigned char *out, size_t outlen, + const OSSL_PARAM params[]) +{ + KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx; + PROV_DRBG_HMAC *drbg = &ctx->base; + + if (!ossl_prov_is_running() + || !hmac_drbg_kdf_set_ctx_params(vctx, params)) + return 0; + if (!ctx->init) { + if (ctx->entropy == NULL + || ctx->entropylen == 0 + || ctx->nonce == NULL + || ctx->noncelen == 0 + || !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen, + ctx->nonce, ctx->noncelen, NULL, 0)) + return 0; + ctx->init = 1; + } + + return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0); +} + +static int hmac_drbg_kdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) +{ + KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx; + PROV_DRBG_HMAC *drbg = &hmac->base; + const char *name; + const EVP_MD *md; + OSSL_PARAM *p; + + p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_MAC); + if (p != NULL) { + if (drbg->ctx == NULL) + return 0; + name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(drbg->ctx)); + if (!OSSL_PARAM_set_utf8_string(p, name)) + return 0; + } + + p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_DIGEST); + if (p != NULL) { + md = ossl_prov_digest_md(&drbg->digest); + if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md))) + return 0; + } + return 1; +} + +static const OSSL_PARAM *hmac_drbg_kdf_gettable_ctx_params( + ossl_unused void *vctx, ossl_unused void *p_ctx) +{ + static const OSSL_PARAM known_gettable_ctx_params[] = { + OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), + OSSL_PARAM_END + }; + return known_gettable_ctx_params; +} + +static int hmac_drbg_kdf_set_ctx_params(void *vctx, + const OSSL_PARAM params[]) +{ + KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx; + PROV_DRBG_HMAC *drbg = &hmac->base; + OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(hmac->provctx); + const EVP_MD *md; + const OSSL_PARAM *p; + void *ptr = NULL; + size_t size = 0; + + if (params == NULL) + return 1; + + p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_ENTROPY); + if (p != NULL) { + if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size)) + return 0; + OPENSSL_free(hmac->entropy); + hmac->entropy = ptr; + hmac->entropylen = size; + hmac->init = 0; + ptr = NULL; + } + + p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_NONCE); + if (p != NULL) { + if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size)) + return 0; + OPENSSL_free(hmac->nonce); + hmac->nonce = ptr; + hmac->noncelen = size; + hmac->init = 0; + } + + p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST); + if (p != NULL) { + if (!ossl_prov_digest_load_from_params(&drbg->digest, params, libctx)) + return 0; + + /* Confirm digest is allowed. Allow all digests that are not XOF */ + md = ossl_prov_digest_md(&drbg->digest); + if (md != NULL) { + if ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) { + ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); + return 0; + } + drbg->blocklen = EVP_MD_get_size(md); + } + return ossl_prov_macctx_load_from_params(&drbg->ctx, params, + "HMAC", NULL, NULL, libctx); + } + return 1; +} + +static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params( + ossl_unused void *vctx, ossl_unused void *p_ctx) +{ + static const OSSL_PARAM known_settable_ctx_params[] = { + OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY, NULL, 0), + OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), + OSSL_PARAM_END + }; + return known_settable_ctx_params; +} + +const OSSL_DISPATCH ossl_kdf_hmac_drbg_functions[] = { + { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))hmac_drbg_kdf_new }, + { OSSL_FUNC_KDF_FREECTX, (void(*)(void))hmac_drbg_kdf_free }, + { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))hmac_drbg_kdf_dup }, + { OSSL_FUNC_KDF_RESET, (void(*)(void))hmac_drbg_kdf_reset }, + { OSSL_FUNC_KDF_DERIVE, (void(*)(void))hmac_drbg_kdf_derive }, + { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, + (void(*)(void))hmac_drbg_kdf_settable_ctx_params }, + { OSSL_FUNC_KDF_SET_CTX_PARAMS, + (void(*)(void))hmac_drbg_kdf_set_ctx_params }, + { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, + (void(*)(void))hmac_drbg_kdf_gettable_ctx_params }, + { OSSL_FUNC_KDF_GET_CTX_PARAMS, + (void(*)(void))hmac_drbg_kdf_get_ctx_params }, + { 0, NULL } +}; diff --git a/providers/implementations/rands/drbg_hmac.c b/providers/implementations/rands/drbg_hmac.c index ffeb70f8c3..44241223a6 100644 --- a/providers/implementations/rands/drbg_hmac.c +++ b/providers/implementations/rands/drbg_hmac.c @@ -13,11 +13,11 @@ #include <openssl/err.h> #include <openssl/rand.h> #include <openssl/proverr.h> -#include "prov/provider_util.h" #include "internal/thread_once.h" #include "prov/providercommon.h" #include "prov/implementations.h" #include "prov/provider_ctx.h" +#include "prov/hmac_drbg.h" #include "drbg_local.h" static OSSL_FUNC_rand_newctx_fn drbg_hmac_new_wrapper; @@ -32,14 +32,6 @@ static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hmac_gettable_ctx_params; static OSSL_FUNC_rand_get_ctx_params_fn drbg_hmac_get_ctx_params; static OSSL_FUNC_rand_verify_zeroization_fn drbg_hmac_verify_zeroization; -typedef struct rand_drbg_hmac_st { - EVP_MAC_CTX *ctx; /* H(x) = HMAC_hash OR H(x) = KMAC */ - PROV_DIGEST digest; /* H(x) = hash(x) */ - size_t blocklen; - unsigned char K[EVP_MAX_MD_SIZE]; - unsigned char V[EVP_MAX_MD_SIZE]; -} PROV_DRBG_HMAC; - /* * Called twice by SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process. * @@ -91,13 +83,11 @@ static int do_hmac(PROV_DRBG_HMAC *hmac, unsigned char inbyte, * * Returns zero if an error occurs otherwise it returns 1. */ -static int drbg_hmac_update(PROV_DRBG *drbg, +static int drbg_hmac_update(PROV_DRBG_HMAC *hmac, const unsigned char *in1, size_t in1len, const unsigned char *in2, size_t in2len, const unsigned char *in3, size_t in3len) { - PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data; - /* (Steps 1-2) K = HMAC(K, V||0x00||provided_data). V = HMAC(K,V) */ if (!do_hmac(hmac, 0x00, in1, in1len, in2, in2len, in3, in3len)) return 0; @@ -119,13 +109,11 @@ static int drbg_hmac_update(PROV_DRBG *drbg, * * Returns zero if an error occurs otherwise it returns 1. */ -static int drbg_hmac_instantiate(PROV_DRBG *drbg, - const unsigned char *ent, size_t ent_len, - const unsigned char *nonce, size_t nonce_len, - const unsigned char *pstr, size_t pstr_len) +int ossl_drbg_hmac_init(PROV_DRBG_HMAC *hmac, + const unsigned char *ent, size_t ent_len, + const unsigned char *nonce, size_t nonce_len, + const unsigned char *pstr, size_t pstr_len) { - PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data; - if (hmac->ctx == NULL) { ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC); return 0; @@ -136,9 +124,17 @@ static int drbg_hmac_instantiate(PROV_DRBG *drbg, /* (Step 3) V = 0x01 01...01 */ memset(hmac->V, 0x01, hmac->blocklen); /* (Step 4) (K,V) = HMAC_DRBG_Update(entropy||nonce||pers string, K, V) */ - return drbg_hmac_update(drbg, ent, ent_len, nonce, nonce_len, pstr, + return drbg_hmac_update(hmac, ent, ent_len, nonce, nonce_len, pstr, pstr_len); } +static int drbg_hmac_instantiate(PROV_DRBG *drbg, + const unsigned char *ent, size_t ent_len, + const unsigned char *nonce, size_t nonce_len, + const unsigned char *pstr, size_t pstr_len) +{ + return ossl_drbg_hmac_init((PROV_DRBG_HMAC *)drbg->data, ent, ent_len, + nonce, nonce_len, pstr, pstr_len); +} static int drbg_hmac_instantiate_wrapper(void *vdrbg, unsigned int strength, int prediction_resistance, @@ -168,8 +164,10 @@ static int drbg_hmac_reseed(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len, const unsigned char *adin, size_t adin_len) { + PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data; + /* (Step 2) (K,V) = HMAC_DRBG_Update(entropy||additional_input, K, V) */ - return drbg_hmac_update(drbg, ent, ent_len, adin, adin_len, NULL, 0); + return drbg_hmac_update(hmac, ent, ent_len, adin, adin_len, NULL, 0); } static int drbg_hmac_reseed_wrapper(void *vdrbg, int prediction_resistance, @@ -191,18 +189,17 @@ static int drbg_hmac_reseed_wrapper(void *vdrbg, int prediction_resistance, * * Returns zero if an error occurs otherwise it returns 1. */ -static int drbg_hmac_generate(PROV_DRBG *drbg, - unsigned char *out, size_t outlen, - const unsigned char *adin, size_t adin_len) +int ossl_drbg_hmac_generate(PROV_DRBG_HMAC *hmac, + unsigned char *out, size_t outlen, + const unsigned char *adin, size_t adin_len) { - PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data; EVP_MAC_CTX *ctx = hmac->ctx; const unsigned char *temp = hmac->V; /* (Step 2) if adin != NULL then (K,V) = HMAC_DRBG_Update(adin, K, V) */ if (adin != NULL && adin_len > 0 - && !drbg_hmac_update(drbg, adin, adin_len, NULL, 0, NULL, 0)) + && !drbg_hmac_update(hmac, adin, adin_len, NULL, 0, NULL, 0)) return 0; /* @@ -231,14 +228,22 @@ static int drbg_hmac_generate(PROV_DRBG *drbg, outlen -= hmac->blocklen; } /* (Step 6) (K,V) = HMAC_DRBG_Update(adin, K, V) */ - if (!drbg_hmac_update(drbg, adin, adin_len, NULL, 0, NULL, 0)) + if (!drbg_hmac_update(hmac, adin, adin_len, NULL, 0, NULL, 0)) return 0; return 1; } -static int drbg_hmac_generate_wrapper - (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength, +static int drbg_hmac_generate(PROV_DRBG *drbg, + unsigned char *out, size_t outlen, + const unsigned char *adin, size_t adin_len) +{ + return ossl_drbg_hmac_generate((PROV_DRBG_HMAC *)drbg->data, out, outlen, + adin, adin_len); +} + +static int drbg_hmac_generate_wrapper(void *vdrbg, + unsigned char *out, size_t outlen, unsigned int strength, int prediction_resistance, const unsigned char *adin, size_t adin_len) { PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; diff --git a/providers/implementations/rands/drbg_local.h b/providers/implementations/rands/drbg_local.h index 58228dc829..d3b439d13e 100644 --- a/providers/implementations/rands/drbg_local.h +++ b/providers/implementations/rands/drbg_local.h @@ -224,7 +224,7 @@ OSSL_FUNC_rand_unlock_fn ossl_drbg_unlock; int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]); int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]); -#define OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON \ +#define OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON \ OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \ OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL) diff --git a/providers/implementations/signature/dsa_sig.c b/providers/implementations/signature/dsa_sig.c index 413559a747..6393dd4dec 100644 --- a/providers/implementations/signature/dsa_sig.c +++ b/providers/implementations/signature/dsa_sig.c @@ -74,6 +74,9 @@ typedef struct { */ unsigned int flag_allow_md : 1; + /* If this is set to 1 then the generated k is not random */ + unsigned int nonce_type; + char mdname[OSSL_MAX_NAME_SIZE]; /* The Algorithm Identifier of the combined signature algorithm */ @@ -249,7 +252,9 @@ static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen, if (mdsize != 0 && tbslen != mdsize) return 0; - ret = ossl_dsa_sign_int(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa); + ret = ossl_dsa_sign_int(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa, + pdsactx->nonce_type, pdsactx->mdname, + pdsactx->libctx, pdsactx->propq); if (ret <= 0) return 0; @@ -497,6 +502,10 @@ static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[]) if (!dsa_setup_md(pdsactx, mdname, mdprops)) return 0; } + p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE); + if (p != NULL + && !OSSL_PARAM_get_uint(p, &pdsactx->nonce_type)) + return 0; return 1; } @@ -504,6 +513,7 @@ static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[]) static const OSSL_PARAM settable_ctx_params[] = { OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), + OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL), OSSL_PARAM_END }; diff --git a/providers/implementations/signature/ecdsa_sig.c b/providers/implementations/signature/ecdsa_sig.c index c013140fec..9c4b6d3638 100644 --- a/providers/implementations/signature/ecdsa_sig.c +++ b/providers/implementations/signature/ecdsa_sig.c @@ -25,6 +25,7 @@ #include "internal/nelem.h" #include "internal/sizes.h" #include "internal/cryptlib.h" +#include "internal/deterministic_nonce.h" #include "prov/providercommon.h" #include "prov/implementations.h" #include "prov/provider_ctx.h" @@ -102,6 +103,8 @@ typedef struct { */ unsigned int kattest; #endif + /* If this is set then the generated k is not random */ + unsigned int nonce_type; } PROV_ECDSA_CTX; static void *ecdsa_newctx(void *provctx, const char *propq) @@ -192,7 +195,15 @@ static int ecdsa_sign(void *vctx, unsigned char *sig, size_t *siglen, if (ctx->mdsize != 0 && tbslen != ctx->mdsize) return 0; - ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, ctx->ec); + if (ctx->nonce_type != 0) { + ret = ossl_ecdsa_deterministic_sign(tbs, tbslen, sig, &sltmp, + ctx->ec, ctx->nonce_type, + ctx->mdname, + ctx->libctx, ctx->propq); + } else { + ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, + ctx->ec); + } if (ret <= 0) return 0; @@ -513,6 +524,10 @@ static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[]) return 0; ctx->mdsize = mdsize; } + p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE); + if (p != NULL + && !OSSL_PARAM_get_uint(p, &ctx->nonce_type)) + return 0; return 1; } @@ -522,6 +537,7 @@ static const OSSL_PARAM settable_ctx_params[] = { OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL), + OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL), OSSL_PARAM_END }; |