diff options
author | Jim Meyering <jim@meyering.net> | 1999-04-03 03:27:57 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1999-04-03 03:27:57 +0000 |
commit | a29afcb470ec03aae7606bb87fffd3dd99ec4ab4 (patch) | |
tree | 42db719a11b5bc6caa7fb7186b73999968f1606f | |
parent | bf504d2a46f45f7482edcc425a4a76b56548e9d5 (diff) | |
download | coreutils-a29afcb470ec03aae7606bb87fffd3dd99ec4ab4.tar.xz |
(isaac_seed): Don't overrun the s->mm buffer.
Use gethrtime if available. Don't assume that clock_gettime succeeds.
Put most random sources first.
-rw-r--r-- | src/shred.c | 84 |
1 files changed, 54 insertions, 30 deletions
diff --git a/src/shred.c b/src/shred.c index 198a80759..f747a7b7f 100644 --- a/src/shred.c +++ b/src/shred.c @@ -380,55 +380,79 @@ static void isaac_seed (struct isaac_state *s) { char *p = (char *) s->mm; -#define MIXIN(o) (memcpy (p, (char *) &(o), sizeof (o)), p += sizeof (o)) + char *lim = p + sizeof s->mm; +#define MIXIN_BOUND(s) ((s) < lim - p ? (s) : lim - p) +#define MIXIN(o) \ + do \ + { \ + size_t s = MIXIN_BOUND (sizeof (o)); \ + memcpy (p, (char *) &(o), s); \ + p += s; \ + } \ + while (0) + + /* Mix in bits of random information from the environment. + Mix the most random items first, in case lim - p is small + and we have to truncate. */ { - pid_t t = getpid (); + int fd = open ("/dev/urandom", O_RDONLY); + if (fd >= 0) + { + size_t s = MIXIN_BOUND (32); + read (fd, p, s); + p += s; + close (fd); + } + else + { + fd = open ("/dev/random", O_RDONLY | O_NONBLOCK); + if (fd >= 0) + { + /* /dev/random is more precious, so use less */ + size_t s = MIXIN_BOUND (16); + read (fd, p, s); + p += s; + close (fd); + } + } + } + +#ifdef HAVE_GETHRTIME + { + hrtime_t t = gethrtime (); MIXIN (t); - t = getppid (); + } +#endif + +#ifdef HAVE_CLOCK_GETTIME + { + struct timespec t; + clock_gettime (CLOCK_REALTIME, &t); MIXIN (t); } +#endif { - uid_t t = getuid (); + time_t t = time ((time_t *) 0); MIXIN (t); } { - gid_t t = getgid (); + pid_t t = getpid (); + MIXIN (t); + t = getppid (); MIXIN (t); } { -#ifdef HAVE_CLOCK_GETTIME /* POSIX ns-resolution */ - struct timespec t; - clock_gettime (CLOCK_REALTIME, &t); -#else - time_t t = time ((time_t *) 0); -#endif + uid_t t = getuid (); MIXIN (t); } { - int r = 0; - int fd = open ("/dev/urandom", O_RDONLY); - if (fd >= 0) - { - r = read (fd, p, 32); - close (fd); - } - else - { - fd = open ("/dev/random", O_RDONLY | O_NONBLOCK); - if (fd >= 0) - { - /* /dev/random is more precious, so use less */ - r = read (fd, p, 16); - close (fd); - } - } - if (0 < r) - p += r; + gid_t t = getgid (); + MIXIN (t); } isaac_init (s, s->mm, sizeof (s->mm)); |