Decrement loop counter only when adding entropy

When using both -L and -t option (looping for a given number of
times, adding entropy only when needed), decrement the loop counter
only when entropy is actually added to the pool, not when we are
merely waiting for the available entropy to fall below the
requested threshold.
develop
Damien Goutte-Gattat 8 years ago
parent 7ae0a37b52
commit 890a132445
  1. 32
      src/scdrand.c

@ -261,20 +261,29 @@ connect_to_scdaemon(void)
return ctx;
}
static int
get_available_entropy(void)
/*
* Wait until the available entropy falls below the specified threshold.
* Available entropy is checked regularly at the specified interval.
*/
static void
wait_for_threshold(unsigned threshold, unsigned interval)
{
int random_fd, entropy;
int entropy, random_fd, loop = 1;
if ( (random_fd = open("/dev/random", O_RDONLY)) == -1 )
err(EXIT_FAILURE, "Cannot open /dev/random");
if ( ioctl(random_fd, RNDGETENTCNT, &entropy) == -1 )
err(EXIT_FAILURE, "Cannot get available entropy");
while ( loop ) {
if ( ioctl(random_fd, RNDGETENTCNT, &entropy) == -1 )
err(EXIT_FAILURE, "Cannot get available entropy");
close(random_fd);
if ( entropy < threshold )
loop = 0;
else
sleep(interval);
}
return entropy;
close(random_fd);
}
/*
@ -433,10 +442,11 @@ main(int argc, char **argv)
ctx = connect_to_scdaemon();
while ( loop == -1 || loop-- > 0 ) {
if ( threshold == 0 || get_available_entropy() < threshold ) {
n = get_challenge(ctx, random_buffer, nbytes);
write(fd, random_buffer, n);
}
if ( threshold != 0 )
wait_for_threshold(threshold, interval);
n = get_challenge(ctx, random_buffer, nbytes);
write(fd, random_buffer, n);
if ( loop != 0 )
sleep(interval);

Loading…
Cancel
Save