Skip to content

Commit dbc3435

Browse files
committed
linuxkm/lkcapi_sha_glue.c: in wc_linuxkm_drbg_seed(), prefix the supplied seed with the CPU ID of each DRBG, to avoid duplicate states;
wolfcrypt/src/random.c: in Hash_DRBG_Generate(), always put digest[] on the stack even in WOLFSSL_SMALL_STACK configuration (it's only 32 bytes); configure.ac: default smallstackcache on when linuxkm-defaults.
1 parent 29cf3eb commit dbc3435

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

configure.ac

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7181,10 +7181,16 @@ then
71817181
fi
71827182
71837183
# Small Stack - Cache on object
7184+
if test "$ENABLED_LINUXKM_DEFAULTS" = "yes"
7185+
then
7186+
ENABLED_SMALL_STACK_CACHE_DEFAULT=yes
7187+
else
7188+
ENABLED_SMALL_STACK_CACHE_DEFAULT=no
7189+
fi
71847190
AC_ARG_ENABLE([smallstackcache],
71857191
[AS_HELP_STRING([--enable-smallstackcache],[Enable Small Stack Usage Caching (default: disabled)])],
71867192
[ ENABLED_SMALL_STACK_CACHE=$enableval ],
7187-
[ ENABLED_SMALL_STACK_CACHE=no ]
7193+
[ ENABLED_SMALL_STACK_CACHE=$ENABLED_SMALL_STACK_CACHE_DEFAULT ]
71887194
)
71897195
71907196
if test "x$ENABLED_SMALL_STACK_CACHE" = "xyes"

linuxkm/lkcapi_sha_glue.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,19 +1007,31 @@ static int wc_linuxkm_drbg_seed(struct crypto_rng *tfm,
10071007
const u8 *seed, unsigned int slen)
10081008
{
10091009
struct wc_linuxkm_drbg_ctx *ctx = (struct wc_linuxkm_drbg_ctx *)crypto_rng_ctx(tfm);
1010+
u8 *seed_copy = NULL;
10101011
int ret;
10111012
unsigned int i;
10121013

10131014
if (slen == 0)
10141015
return 0;
10151016

1017+
seed_copy = (u8 *)malloc(slen + 2);
1018+
if (! seed_copy)
1019+
return -ENOMEM;
1020+
XMEMCPY(seed_copy + 2, seed, slen);
1021+
10161022
for (i = 0; i < nr_cpu_ids; ++i) {
10171023
wolfSSL_Mutex *lock = &ctx->rngs[i].lock;
10181024
WC_RNG *rng = &ctx->rngs[i].rng;
10191025

1026+
/* perturb the seed with the CPU ID, so that no DRBG has the exact same
1027+
* seed.
1028+
*/
1029+
seed_copy[0] = (u8)(i >> 8);
1030+
seed_copy[1] = (u8)i;
1031+
10201032
wc_LockMutex(lock);
10211033

1022-
ret = wc_RNG_DRBG_Reseed(rng, seed, slen);
1034+
ret = wc_RNG_DRBG_Reseed(rng, seed_copy, slen + 2);
10231035
if (ret != 0) {
10241036
ret = -EINVAL;
10251037
}
@@ -1030,6 +1042,8 @@ static int wc_linuxkm_drbg_seed(struct crypto_rng *tfm,
10301042
break;
10311043
}
10321044

1045+
free(seed_copy);
1046+
10331047
return ret;
10341048
}
10351049

wolfcrypt/src/random.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,7 @@ static int Hash_DRBG_Generate(DRBG_internal* drbg, byte* out, word32 outSz)
647647
return DRBG_NEED_RESEED;
648648
}
649649
else {
650-
#ifndef WOLFSSL_SMALL_STACK
651650
byte digest[WC_SHA256_DIGEST_SIZE];
652-
#else
653-
byte* digest = (byte*)XMALLOC(WC_SHA256_DIGEST_SIZE, drbg->heap,
654-
DYNAMIC_TYPE_DIGEST);
655-
if (digest == NULL)
656-
return DRBG_FAILURE;
657-
#endif
658651

659652
type = drbgGenerateH;
660653
reseedCtr = drbg->reseedCtr;
@@ -692,9 +685,6 @@ static int Hash_DRBG_Generate(DRBG_internal* drbg, byte* out, word32 outSz)
692685
drbg->reseedCtr++;
693686
}
694687
ForceZero(digest, WC_SHA256_DIGEST_SIZE);
695-
#ifdef WOLFSSL_SMALL_STACK
696-
XFREE(digest, drbg->heap, DYNAMIC_TYPE_DIGEST);
697-
#endif
698688
}
699689

700690
return (ret == 0) ? DRBG_SUCCESS : DRBG_FAILURE;

0 commit comments

Comments
 (0)