Skip to content

Commit affb3d7

Browse files
committed
improve fortuna_import()
This makes fortuna_import() kinda compliant to the "Update seed file" behavior of the original paper. It differs from the original behavior in that it allows to import seed files which are larger than 64 bytes. (cherry picked from commit 39d4a14)
1 parent 89dffe6 commit affb3d7

1 file changed

Lines changed: 48 additions & 13 deletions

File tree

src/prngs/fortuna.c

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,46 @@ static int _fortuna_reseed(prng_state *prng)
124124
return CRYPT_OK;
125125
}
126126

127+
/**
128+
"Update Seed File"-compliant update of K
129+
130+
@param in The PRNG state
131+
@param inlen Size of the state
132+
@param prng The PRNG to import
133+
@return CRYPT_OK if successful
134+
*/
135+
static int _fortuna_update_seed(const unsigned char *in, unsigned long inlen, prng_state *prng)
136+
{
137+
int err;
138+
unsigned char tmp[MAXBLOCKSIZE];
139+
hash_state md;
140+
141+
LTC_MUTEX_LOCK(&prng->lock);
142+
/* new K = LTC_SHA256(K || in) */
143+
sha256_init(&md);
144+
if ((err = sha256_process(&md, prng->fortuna.K, 32)) != CRYPT_OK) {
145+
sha256_done(&md, tmp);
146+
goto LBL_UNLOCK;
147+
}
148+
if ((err = sha256_process(&md, in, inlen)) != CRYPT_OK) {
149+
sha256_done(&md, tmp);
150+
goto LBL_UNLOCK;
151+
}
152+
/* finish key */
153+
if ((err = sha256_done(&md, prng->fortuna.K)) != CRYPT_OK) {
154+
goto LBL_UNLOCK;
155+
}
156+
_fortuna_update_iv(prng);
157+
158+
LBL_UNLOCK:
159+
LTC_MUTEX_UNLOCK(&prng->lock);
160+
#ifdef LTC_CLEAN_STACK
161+
zeromem(&md, sizeof(md));
162+
#endif
163+
164+
return err;
165+
}
166+
127167
/**
128168
Start the PRNG
129169
@param prng [out] The PRNG state to initialize
@@ -412,11 +452,10 @@ int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
412452
*/
413453
int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
414454
{
415-
int err, x;
416-
unsigned long len;
455+
int err;
417456

418-
LTC_ARGCHK(in != NULL);
419-
LTC_ARGCHK(prng != NULL);
457+
LTC_ARGCHK(in != NULL);
458+
LTC_ARGCHK(prng != NULL);
420459

421460
if (inlen < (unsigned long)fortuna_desc.export_size) {
422461
return CRYPT_INVALID_ARG;
@@ -425,16 +464,12 @@ int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prn
425464
if ((err = fortuna_start(prng)) != CRYPT_OK) {
426465
return err;
427466
}
428-
x = 0;
429-
while (inlen > 0) {
430-
len = MIN(inlen, 32);
431-
if ((err = fortuna_add_entropy(in+x*32, len, prng)) != CRYPT_OK) {
432-
return err;
433-
}
434-
x++;
435-
inlen -= len;
467+
468+
if ((err = _fortuna_update_seed(in, inlen, prng)) != CRYPT_OK) {
469+
return err;
436470
}
437-
return CRYPT_OK;
471+
472+
return err;
438473
}
439474

440475
/**

0 commit comments

Comments
 (0)