Now finally works decently
authorMatthew Mondor <mmondor@pulsar-zone.net>
Thu, 21 Sep 2006 20:11:16 +0000 (20:11 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Thu, 21 Sep 2006 20:11:16 +0000 (20:11 +0000)
tests/entropy/entropy_disk.c

index 70b9a00..0a44ba0 100644 (file)
@@ -1,8 +1,38 @@
-/* $Id: entropy_disk.c,v 1.4 2006/09/07 08:08:13 mmondor Exp $ */
+/* $Id: entropy_disk.c,v 1.5 2006/09/21 20:11:16 mmondor Exp $ */
 
 /*
- * Copyright (c) 2006, Matthew Mondor
- * ALL RIGHTS RESERVED.
+ * Copyright (c) 2006 Pulsar-Zone, Reg.
+ * All rights reserved.
+ *
+ * Written by Matthew Mondor for Pulsar-Zone, Reg.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed for the NetBSD Project by
+ *      Pulsar-Zone, Reg.
+ * 4. The name of Pulsar-Zone, Reg. may not be used to endorse
+ *    or promote products derived from this software without specific prior
+ *    written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY PULSAR-ZONE, REG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL PULSAR-ZONE, REG
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
  */
 
 /*
  * XXX Possibly add an option to tell the daemon to automatially exit after a
  * certain number of seconds or minutes elapsed.  We would use setitimer(2)
  * to receive a SIGALRM, acting the same as for SIGTERM upon its reception.
+ *
+ * XXX Make lseek(2)/read(2) errors silent via compile time option to not
+ * disclose any information in case of disk problems for a final version of
+ * this program.
  */
 
 #include <sys/mman.h>
  * Minimum block size to read (could be disk dependent but it doesn't matter
  * much for this task.
  */
-#define BLOCK_SIZE     8192
+#define BLOCK_SIZE     16384
 /*
  * Maximum number of BLOCK_SIZE blocks to read in a single read(2)
  */
 #define BLOCK_MULTIPLE 16
 /*
  * Minimum and maximum number of reads to perform in a single direction before
- * switching direction
+ * switching direction.  If these are too low the disk head thrashing could be
+ * excessive.
+ */
+#define DIRECTION_MIN  256
+#define DIRECTION_MAX  4096
+/*
+ * Minimum and maximum reseeding of PRNG from urandom(4) frequency (in reads).
+ * If we set these this too low, the entropy we're trying to accumulate in
+ * rnd(4) is sucked out as we create it, causing us to loop indefinitely for
+ * nothing.
  */
-#define DIRECTION_MIN  64
-#define DIRECTION_MAX  2048
+#define RESEED_MIN     32
+#define RESEED_MAX     256
 
 enum direction {
        DIR_FORWARD = 0,
@@ -253,7 +296,7 @@ prng_init(void)
        }
 
        srandom(seed);
-       reseed = random() % 20;
+       reseed = RESEED_MIN + (random() % (RESEED_MAX - RESEED_MIN));
 
        return 0;
 
@@ -277,7 +320,7 @@ prng_reseed(void)
                            "read(/dev/urandom): %s\n", strerror(errno));
                }
                srandom(seed);
-               reseed = random() % 20;
+               reseed = RESEED_MIN + (random() % (RESEED_MAX - RESEED_MIN));
        }
 }