$NetBSD: patch-bitstir_c,v 1.3 2015/10/07 23:19:08 joerg Exp $ Get gMaxEntropy and gCriticalEntropyThreshold from the kernel in the approved way, including at tls@'s suggestion a fallback in case the reported threshold is 0. Also, fix a couple C usage issues. --- bitstir.c.orig 2015-09-27 17:10:03.000000000 +0000 +++ bitstir.c @@ -40,11 +40,18 @@ #include #include #include +#include +#include + +#include +#if __NetBSD_Version__ >= 799001000 +# include +#endif const char gRequiredOS[] = "NetBSD"; -const char gRandomDevice[] = "/dev/random"; -const long gMaxEntropy = RND_POOLBITS; -const long gCriticalEntropyThreshold = RND_POOLBITS / 10; +const char gRandomDevice[] = _PATH_RANDOM; +long gMaxEntropy; +long gCriticalEntropyThreshold; const long gMaxPathLen = PATH_MAX; long verbose_flag = 0; @@ -76,8 +83,8 @@ struct search_dir *current_search_dir = void parse_command_line_args (int argc, char *argv[]); long entropy_available(const char *device); -void restore_entropy(); -void restore_entropy_one_shot(); +void restore_entropy(void); +void restore_entropy_one_shot(void); void nullify_fd (int fd); void exec_find (const char *directory); void kill_process (pid_t pid); @@ -88,9 +95,10 @@ void log_err (char *fmt, ... ); void check_os (); int is_directory (const char *path); void setup_find_executable (); +void get_random_stats(void); void print_help (); -main(int argc, char *argv[]) +int main(int argc, char *argv[]) { if( argc == 1 ) { fprintf(stderr, "usage: %s [switches] --search-dir dir [--search-dir dir ...]\n", @@ -120,6 +128,7 @@ main(int argc, char *argv[]) check_os(); } setup_find_executable(); + get_random_stats(); if( root_search_dir == (struct search_dir *) NULL ) { log_err("No directory specified with --search-dir. Exiting.\n"); @@ -578,6 +587,31 @@ void setup_find_executable () } } +void get_random_stats(void) +{ + rndpoolstat_t rs; + int fd; + + fd = open(_PATH_URANDOM, O_RDONLY, 0644); + if (fd < 0) { + log_err("Could not open %s: %s\n", _PATH_URANDOM, strerror(errno)); + exit(1); + } + + if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0) { + log_err("RNDGETPOOLSTAT failed: %s\n", strerror(errno)); + exit(1); + } + + close(fd); + + gMaxEntropy = rs.maxentropy; + gCriticalEntropyThreshold = rs.threshold; + if (gCriticalEntropyThreshold < 2 * SHA1_DIGEST_LENGTH) { + gCriticalEntropyThreshold = 2 * SHA1_DIGEST_LENGTH; + } +} + void print_help() { fprintf(stderr, "Basic help message - see bitstir(8) for more detail:\n");