Browse Source

Increase entropy of generated pad files, patch from Toby Speight <T.M.Speight.90@cantab.net>.

- Seeding the random number generator from PID and current time
provides very little entropy, as these can be guessed quite closely
by an attacker, so use the kernel's random number generator instead.
master
Alessio Treglia 12 years ago
parent
commit
3b4523fef7
1 changed files with 11 additions and 1 deletions
  1. +11
    -1
      src/pad.c

+ 11
- 1
src/pad.c View File

@ -22,6 +22,7 @@
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h> #include <pwd.h>
#include <time.h> #include <time.h>
#include "conf.h" #include "conf.h"
@ -181,6 +182,8 @@ static void pusb_pad_update(t_pusb_options *opts,
FILE *f_system = NULL; FILE *f_system = NULL;
char magic[1024]; char magic[1024];
int i; int i;
unsigned int seed;
int devrandom;
if (!pusb_pad_should_update(opts, user)) if (!pusb_pad_should_update(opts, user))
return ; return ;
@ -201,7 +204,14 @@ static void pusb_pad_update(t_pusb_options *opts,
pusb_pad_protect(user, fileno(f_system)); pusb_pad_protect(user, fileno(f_system));
log_debug("Generating %d bytes unique pad...\n", sizeof(magic)); log_debug("Generating %d bytes unique pad...\n", sizeof(magic));
srand(getpid() * time(NULL));
devrandom = open("/dev/random", O_RDONLY);
if (devrandom < 0 || read(devrandom, &seed, sizeof seed) != sizeof seed) {
log_debug("/dev/random seeding failed...\n");
seed = getpid() * time(NULL); /* low-entropy fallback */
}
if (devrandom > 0)
close(devrandom);
srand(seed);
for (i = 0; i < sizeof(magic); ++i) for (i = 0; i < sizeof(magic); ++i)
magic[i] = (char)rand(); magic[i] = (char)rand();
log_debug("Writing pad to the device...\n"); log_debug("Writing pad to the device...\n");


Loading…
Cancel
Save