Skip to content

Commit 67d8ca1

Browse files
committed
ensure that fortuna has been seeded properly
(cherry picked from commit 04ce8cf)
1 parent 2438989 commit 67d8ca1

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/prngs/fortuna.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ static int _fortuna_reseed(prng_state *prng)
6666
{
6767
unsigned char tmp[MAXBLOCKSIZE];
6868
hash_state md;
69+
ulong64 reset_cnt;
6970
int err, x;
7071

71-
++prng->fortuna.reset_cnt;
7272

7373
/* new K == LTC_SHA256(K || s) where s == LTC_SHA256(P0) || LTC_SHA256(P1) ... */
7474
sha256_init(&md);
@@ -77,8 +77,10 @@ static int _fortuna_reseed(prng_state *prng)
7777
return err;
7878
}
7979

80+
reset_cnt = prng->fortuna.reset_cnt + 1;
81+
8082
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
81-
if (x == 0 || ((prng->fortuna.reset_cnt >> (x-1)) & 1) == 0) {
83+
if (x == 0 || ((reset_cnt >> (x-1)) & 1) == 0) {
8284
/* terminate this hash */
8385
if ((err = sha256_done(&prng->fortuna.pool[x], tmp)) != CRYPT_OK) {
8486
sha256_done(&md, tmp);
@@ -108,9 +110,10 @@ static int _fortuna_reseed(prng_state *prng)
108110
}
109111
_fortuna_update_iv(prng);
110112

111-
/* reset pool len */
113+
/* reset/update internals */
112114
prng->fortuna.pool0_len = 0;
113115
prng->fortuna.wd = 0;
116+
prng->fortuna.reset_cnt = reset_cnt;
114117

115118

116119
#ifdef LTC_CLEAN_STACK
@@ -251,6 +254,11 @@ unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state
251254
}
252255
}
253256

257+
/* ensure that one reseed happened before allowing to read */
258+
if (prng->fortuna.reset_cnt == 0) {
259+
goto LBL_UNLOCK;
260+
}
261+
254262
/* now generate the blocks required */
255263
tlen = outlen;
256264

0 commit comments

Comments
 (0)