[svn] gcry - r1273 - in trunk: cipher tests

svn author wk cvs at cvs.gnupg.org
Fri Nov 30 17:33:12 CET 2007


Author: wk
Date: 2007-11-30 17:33:09 +0100 (Fri, 30 Nov 2007)
New Revision: 1273

Modified:
   trunk/cipher/ChangeLog
   trunk/cipher/Makefile.am
   trunk/cipher/rand-internal.h
   trunk/cipher/random.c
   trunk/cipher/rndlinux.c
   trunk/cipher/rndw32.c
   trunk/tests/ChangeLog
   trunk/tests/benchmark.c
Log:
Add support for the HW RNG.  However i was not able to test it because it
seems to be disabled by the OS.


Modified: trunk/cipher/ChangeLog
===================================================================
--- trunk/cipher/ChangeLog	2007-11-29 10:55:46 UTC (rev 1272)
+++ trunk/cipher/ChangeLog	2007-11-30 16:33:09 UTC (rev 1273)
@@ -1,3 +1,11 @@
+2007-11-30  Werner Koch  <wk at g10code.com>
+
+	* rndhw.c: New.
+	* rndlinux.c (_gcry_rndlinux_gather_random): Try to read 50%
+	directly from the hwrng.
+	* random.c (do_fast_random_poll): Also run the hw rng fast poll.
+	(_gcry_random_dump_stats): Tell whether the hw rng failed.
+
 2007-11-29  Werner Koch  <wk at g10code.com>
 
 	* rijndael.c (USE_PADLOCK): Define new macro used for ia32.

Modified: trunk/cipher/Makefile.am
===================================================================
--- trunk/cipher/Makefile.am	2007-11-29 10:55:46 UTC (rev 1272)
+++ trunk/cipher/Makefile.am	2007-11-30 16:33:09 UTC (rev 1273)
@@ -41,6 +41,7 @@
 bithelp.h  \
 primegen.c  \
 random.c random.h \
+rndhw.c \
 rand-internal.h \
 rmd.h
 

Modified: trunk/cipher/rand-internal.h
===================================================================
--- trunk/cipher/rand-internal.h	2007-11-29 10:55:46 UTC (rev 1272)
+++ trunk/cipher/rand-internal.h	2007-11-30 16:33:09 UTC (rev 1273)
@@ -61,6 +61,14 @@
                                                   enum random_origins),
                                       enum random_origins origin );
 
+int _gcry_rndhw_failed_p (void);
+void _gcry_rndhw_poll_fast (void (*add)(const void*, size_t,
+                                        enum random_origins),
+                            enum random_origins origin);
+size_t _gcry_rndhw_poll_slow (void (*add)(const void*, size_t,
+                                          enum random_origins),
+                              enum random_origins origin);
 
 
+
 #endif /*G10_RAND_INTERNAL_H*/

Modified: trunk/cipher/random.c
===================================================================
--- trunk/cipher/random.c	2007-11-29 10:55:46 UTC (rev 1272)
+++ trunk/cipher/random.c	2007-11-30 16:33:09 UTC (rev 1273)
@@ -15,8 +15,7 @@
  * GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
@@ -398,11 +397,12 @@
      might_ run into problems.  Needs to be checked.  -wk */
 
   log_info ("random usage: poolsize=%d mixed=%lu polls=%lu/%lu added=%lu/%lu\n"
-	    "              outmix=%lu getlvl1=%lu/%lu getlvl2=%lu/%lu\n",
+	    "              outmix=%lu getlvl1=%lu/%lu getlvl2=%lu/%lu%s\n",
             POOLSIZE, rndstats.mixrnd, rndstats.slowpolls, rndstats.fastpolls,
             rndstats.naddbytes, rndstats.addbytes,
             rndstats.mixkey, rndstats.ngetbytes1, rndstats.getbytes1,
-            rndstats.ngetbytes2, rndstats.getbytes2 );
+            rndstats.ngetbytes2, rndstats.getbytes2,
+            _gcry_rndhw_failed_p()? " (hwrng failed)":"");
 }
 
 
@@ -1251,8 +1251,8 @@
 # endif /*!RUSAGE_SELF*/
 #endif /*HAVE_GETRUSAGE*/
 
-  /* time and clock are availabe on all systems - so we better do it
-     just in case one of the above functions didn't work */
+  /* Time and clock are availabe on all systems - so we better do it
+     just in case one of the above functions didn't work.  */
   {
     time_t x = time(NULL);
     add_randomness( &x, sizeof(x), RANDOM_ORIGIN_FASTPOLL );
@@ -1261,6 +1261,10 @@
     clock_t x = clock();
     add_randomness( &x, sizeof(x), RANDOM_ORIGIN_FASTPOLL );
   }
+
+  /* If the system features a fast hardware RNG, read some bytes from
+     there.  */
+  _gcry_rndhw_poll_fast (add_randomness, RANDOM_ORIGIN_FASTPOLL);
 }
 
 

Modified: trunk/cipher/rndlinux.c
===================================================================
--- trunk/cipher/rndlinux.c	2007-11-29 10:55:46 UTC (rev 1272)
+++ trunk/cipher/rndlinux.c	2007-11-30 16:33:09 UTC (rev 1273)
@@ -1,5 +1,5 @@
 /* rndlinux.c  -  raw random number for OSes with /dev/random
- * Copyright (C) 1998, 2001, 2002, 2003  Free Software Foundation, Inc.
+ * Copyright (C) 1998, 2001, 2002, 2003, 2007  Free Software Foundation, Inc.
  *
  * This file is part of Libgcrypt.
  *
@@ -14,8 +14,7 @@
  * GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 
@@ -96,8 +95,18 @@
   int n;
   int warn=0;
   byte buffer[768];
+  size_t n_hw;
 
-  if( level >= 2 )
+  /* First read from a hardware source.  However let it account only
+     for up to 50% of the requested bytes.  */
+  n_hw = _gcry_rndhw_poll_slow (add, origin);
+  if (n_hw > length/2)
+    n_hw = length/2;
+  if (length > 1)
+    length -= n_hw;
+
+  /* Open the requested device.  */
+  if (level >= 2)
     {
       if( fd_random == -1 )
         fd_random = open_device ( NAME_OF_DEV_RANDOM );
@@ -110,6 +119,7 @@
       fd = fd_urandom;
     }
 
+  /* And enter the read loop.  */
   while (length)
     {
       fd_set rfds;

Modified: trunk/cipher/rndw32.c
===================================================================
--- trunk/cipher/rndw32.c	2007-11-29 10:55:46 UTC (rev 1272)
+++ trunk/cipher/rndw32.c	2007-11-30 16:33:09 UTC (rev 1273)
@@ -958,4 +958,6 @@
           (*add) (&aword, sizeof (aword), origin );
         }
     }
+
+
 }

Modified: trunk/tests/ChangeLog
===================================================================
--- trunk/tests/ChangeLog	2007-11-29 10:55:46 UTC (rev 1272)
+++ trunk/tests/ChangeLog	2007-11-30 16:33:09 UTC (rev 1273)
@@ -1,3 +1,9 @@
+2007-11-30  Werner Koch  <wk at g10code.com>
+
+	* benchmark.c (main): Add optione --verbose and reworked the
+	option parsing.
+	(random_bench): Dump random stats.
+
 2007-10-31  Werner Koch  <wk at g10code.com>
 
 	* benchmark.c (start_timer, stop_timer, elapsed_time) [W32]: Fixed.

Modified: trunk/tests/benchmark.c
===================================================================
--- trunk/tests/benchmark.c	2007-11-29 10:55:46 UTC (rev 1272)
+++ trunk/tests/benchmark.c	2007-11-30 16:33:09 UTC (rev 1273)
@@ -14,8 +14,7 @@
  * GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -34,6 +33,8 @@
 
 #define PGM "benchmark"
 
+static int verbose;
+
 static const char sample_private_dsa_key_1024[] =
 "(private-key\n"
 "  (dsa\n"
@@ -347,6 +348,8 @@
   printf (" %s", elapsed_time ());
 
   putchar ('\n');
+  if (verbose)
+    gcry_control (GCRYCTL_DUMP_RANDOM_STATS);
 }
 
 
@@ -928,6 +931,8 @@
 int
 main( int argc, char **argv )
 {
+  int last_argc = -1;
+
   if (argc)
     { argc--; argv++; }
 
@@ -942,6 +947,33 @@
       gcry_control (GCRYCTL_USE_RANDOM_DAEMON, 1);
       argc--; argv++;
     }
+
+  while (argc && last_argc != argc )
+    {
+      last_argc = argc;
+      if (!strcmp (*argv, "--"))
+        {
+          argc--; argv++;
+          break;
+        }
+      else if (!strcmp (*argv, "--help"))
+        {
+          fputs ("usage: benchmark "
+                 "[md|cipher|random|mpi|rsa|dsa|ecc [algonames]]\n",
+                 stdout);
+          exit (0);
+        }
+      else if (!strcmp (*argv, "--verbose"))
+        {
+          verbose = 1;
+          argc--; argv++;
+        }
+      else if (!strcmp (*argv, "--use-random-daemon"))
+        {
+          gcry_control (GCRYCTL_USE_RANDOM_DAEMON, 1);
+          argc--; argv++;
+        }
+    }          
   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
 
   
@@ -960,10 +992,6 @@
       putchar ('\n');
       random_bench (0);
     }
-  else if ( !strcmp (*argv, "--help"))
-     fputs ("usage: benchmark "
-            "[md|cipher|random|mpi|rsa|dsa|ecc [algonames]]\n",
-            stdout);
   else if ( !strcmp (*argv, "random") || !strcmp (*argv, "strongrandom"))
     {
       if (argc == 1)




More information about the Gnupg-commits mailing list