diff options
Diffstat (limited to 'providers/implementations/rands')
-rw-r--r-- | providers/implementations/rands/drbg_hmac.c | 61 | ||||
-rw-r--r-- | providers/implementations/rands/drbg_local.h | 2 |
2 files changed, 34 insertions, 29 deletions
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) |