[svn] gcry - r1293 - in trunk: . cipher random src

svn author wk cvs at cvs.gnupg.org
Sat Jul 5 18:44:08 CEST 2008


Author: wk
Date: 2008-07-05 18:44:03 +0200 (Sat, 05 Jul 2008)
New Revision: 1293

Added:
   trunk/random/
   trunk/random/ChangeLog
   trunk/random/Makefile.am
   trunk/random/rand-internal.h
   trunk/random/random-daemon.c
   trunk/random/random.c
   trunk/random/random.h
   trunk/random/rndegd.c
   trunk/random/rndhw.c
   trunk/random/rndlinux.c
   trunk/random/rndunix.c
   trunk/random/rndw32.c
Removed:
   trunk/cipher/rand-internal.h
   trunk/cipher/random-daemon.c
   trunk/cipher/random.c
   trunk/cipher/random.h
   trunk/cipher/rndegd.c
   trunk/cipher/rndhw.c
   trunk/cipher/rndlinux.c
   trunk/cipher/rndunix.c
   trunk/cipher/rndw32.c
Modified:
   trunk/ChangeLog
   trunk/Makefile.am
   trunk/cipher/ChangeLog
   trunk/cipher/Makefile.am
   trunk/configure.ac
   trunk/src/ChangeLog
   trunk/src/Makefile.am
   trunk/src/cipher.h
Log:
Moved random stuff into its own directory.


[The diff below has been truncated]

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-06-24 08:11:04 UTC (rev 1292)
+++ trunk/ChangeLog	2008-07-05 16:44:03 UTC (rev 1293)
@@ -1,3 +1,9 @@
+2008-07-05  Werner Koch  <wk at g10code.com>
+
+	* random/: New.
+	* Makefile.am (DIST_SUBDIRS): Add random.
+	* configure.ac (AC_CONFIG_FILES): Add random/Makefile.
+
 2008-04-25  Werner Koch  <wk at g10code.com>
 
 	Release 1.4.1.

Modified: trunk/cipher/ChangeLog
===================================================================
--- trunk/cipher/ChangeLog	2008-06-24 08:11:04 UTC (rev 1292)
+++ trunk/cipher/ChangeLog	2008-07-05 16:44:03 UTC (rev 1293)
@@ -1,3 +1,9 @@
+2008-07-05  Werner Koch  <wk at g10code.com>
+
+	* random-daemon.c, random.h, rndhw.c, rndunix.c, rand-internal.h
+	* random.c, rndegd.c, rndlinux.c, rndw32.c: Move to ../cipher.
+	* Makefile.am: Remove random stuff.
+
 2008-06-24  Szakats Istvan <szaki.ms at gmail.com>  (wk)
 
 	* ac.c (_gcry_ac_key_destroy, _gcry_ac_key_pair_generate): Relase

Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2008-06-24 08:11:04 UTC (rev 1292)
+++ trunk/src/ChangeLog	2008-07-05 16:44:03 UTC (rev 1293)
@@ -1,3 +1,7 @@
+2008-07-05  Werner Koch  <wk at g10code.com>
+
+	* Makefile.am: Include librandom.la.
+
 2008-04-18  Werner Koch  <wk at g10code.com>
 
 	* missing-string.c (vasprintf): Remove.  It is not used.  Reported

Modified: trunk/Makefile.am
===================================================================
--- trunk/Makefile.am	2008-06-24 08:11:04 UTC (rev 1292)
+++ trunk/Makefile.am	2008-07-05 16:44:03 UTC (rev 1293)
@@ -22,8 +22,8 @@
 DISTCHECK_CONFIGURE_FLAGS = --enable-random-daemon \
   --enable-ciphers=arcfour:blowfish:cast5:des:aes:twofish:serpent:rfc2268:seed:camellia
 
-DIST_SUBDIRS = m4 mpi cipher src doc tests
-SUBDIRS = mpi cipher src doc tests
+DIST_SUBDIRS = m4 mpi cipher random src doc tests
+SUBDIRS = mpi cipher random src doc tests
 EXTRA_DIST = BUGS autogen.sh README.SVN
 DISTCLEANFILES = 
 

Modified: trunk/cipher/Makefile.am
===================================================================
--- trunk/cipher/Makefile.am	2008-06-24 08:11:04 UTC (rev 1292)
+++ trunk/cipher/Makefile.am	2008-07-05 16:44:03 UTC (rev 1293)
@@ -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/>.
 
 # Process this file with automake to produce Makefile.in
 
@@ -30,8 +29,7 @@
 
 noinst_LTLIBRARIES = libcipher.la
 
-GCRYPT_MODULES = @GCRYPT_CIPHERS@ @GCRYPT_PUBKEY_CIPHERS@ @GCRYPT_DIGESTS@ \
-                 @GCRYPT_RANDOM@
+GCRYPT_MODULES = @GCRYPT_CIPHERS@ @GCRYPT_PUBKEY_CIPHERS@ @GCRYPT_DIGESTS@
 
 libcipher_la_DEPENDENCIES = $(GCRYPT_MODULES)
 libcipher_la_LIBADD = $(GCRYPT_MODULES)
@@ -40,16 +38,8 @@
 cipher.c pubkey.c ac.c md.c \
 bithelp.h  \
 primegen.c  \
-random.c random.h \
-rndhw.c \
-rand-internal.h \
 rmd.h
 
-if USE_RANDOM_DAEMON
-libcipher_la_SOURCES += random-daemon.c 
-endif USE_RANDOM_DAEMON
-
-
 EXTRA_libcipher_la_SOURCES = \
 arcfour.c \
 blowfish.c \
@@ -63,10 +53,6 @@
 md5.c \
 rijndael.c rijndael-tables.h \
 rmd160.c \
-rndlinux.c \
-rndegd.c \
-rndunix.c \
-rndw32.c \
 rsa.c \
 seed.c \
 serpent.c \

Deleted: trunk/cipher/rand-internal.h

Deleted: trunk/cipher/random-daemon.c

Deleted: trunk/cipher/random.c

Deleted: trunk/cipher/random.h

Deleted: trunk/cipher/rndegd.c

Deleted: trunk/cipher/rndhw.c

Deleted: trunk/cipher/rndlinux.c

Deleted: trunk/cipher/rndunix.c

Deleted: trunk/cipher/rndw32.c

Modified: trunk/configure.ac
===================================================================
--- trunk/configure.ac	2008-06-24 08:11:04 UTC (rev 1292)
+++ trunk/configure.ac	2008-07-05 16:44:03 UTC (rev 1293)
@@ -1081,6 +1081,7 @@
 m4/Makefile
 mpi/Makefile
 cipher/Makefile
+random/Makefile
 doc/Makefile
 src/Makefile
 src/gcrypt.h

Added: trunk/random/ChangeLog
===================================================================
--- trunk/random/ChangeLog	2008-06-24 08:11:04 UTC (rev 1292)
+++ trunk/random/ChangeLog	2008-07-05 16:44:03 UTC (rev 1293)
@@ -0,0 +1,17 @@
+2008-07-05  Werner Koch  <wk at g10code.com>
+
+	* random-daemon.c, random.h, rndhw.c, rndunix.c, rand-internal.h *
+	random.c, rndegd.c, rndlinux.c, rndw32.c: Move from ../cipher/ to
+	here.
+	* Makefile.am: New.
+
+ Copyright 2008 Free Software Foundation, Inc.
+
+ This file is free software; as a special exception the author gives
+ unlimited permission to copy and/or distribute it, with or without
+ modifications, as long as this notice is preserved.
+
+ This file is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
+ implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+	
\ No newline at end of file

Added: trunk/random/Makefile.am
===================================================================
--- trunk/random/Makefile.am	2008-06-24 08:11:04 UTC (rev 1292)
+++ trunk/random/Makefile.am	2008-07-05 16:44:03 UTC (rev 1293)
@@ -0,0 +1,47 @@
+# Makefile for cipher modules
+# Copyright (C) 2008 Free Software Foundation, Inc.
+#
+# This file is part of Libgcrypt.
+#
+# Libgcrypt is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of
+# the License, or (at your option) any later version.
+#
+# Libgcrypt is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# 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, see <http://www.gnu.org/licenses/>.
+
+# Process this file with automake to produce Makefile.in
+
+# Need to include ../src in addition to top_srcdir because gcrypt.h is
+# a built header.
+AM_CPPFLAGS = -I../src -I$(top_srcdir)/src 
+AM_CFLAGS = $(GPG_ERROR_CFLAGS)
+
+noinst_LTLIBRARIES = librandom.la
+
+GCRYPT_MODULES = @GCRYPT_RANDOM@
+
+librandom_la_DEPENDENCIES = $(GCRYPT_MODULES)
+librandom_la_LIBADD = $(GCRYPT_MODULES)
+
+librandom_la_SOURCES = \
+random.c random.h \
+rndhw.c \
+rand-internal.h
+
+if USE_RANDOM_DAEMON
+librandom_la_SOURCES += random-daemon.c 
+endif USE_RANDOM_DAEMON
+
+
+EXTRA_librandom_la_SOURCES = \
+rndlinux.c \
+rndegd.c \
+rndunix.c \
+rndw32.c 

Copied: trunk/random/rand-internal.h (from rev 1290, trunk/cipher/rand-internal.h)

Copied: trunk/random/random-daemon.c (from rev 1290, trunk/cipher/random-daemon.c)

Copied: trunk/random/random.c (from rev 1290, trunk/cipher/random.c)
===================================================================
--- trunk/cipher/random.c	2008-04-25 16:53:39 UTC (rev 1290)
+++ trunk/random/random.c	2008-07-05 16:44:03 UTC (rev 1293)
@@ -0,0 +1,1446 @@
+/* random.c  -	random number generator
+ * Copyright (C) 1998, 2000, 2001, 2002, 2003,
+ *               2004, 2005, 2006, 2007  Free Software Foundation, Inc.
+ *
+ * This file is part of Libgcrypt.
+ *
+ * Libgcrypt is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * Libgcrypt is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+   This random number generator is modelled after the one described in
+   Peter Gutmann's paper: "Software Generation of Practically Strong
+   Random Numbers". See also chapter 6 in his book "Cryptographic
+   Security Architecture", New York, 2004, ISBN 0-387-95387-6.
+ */
+
+
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <time.h>
+#ifdef	HAVE_GETHRTIME
+#include <sys/times.h>
+#endif
+#ifdef HAVE_GETTIMEOFDAY
+#include <sys/time.h>
+#endif
+#ifdef HAVE_GETRUSAGE
+#include <sys/resource.h>
+#endif
+#ifdef __MINGW32__
+#include <process.h>
+#endif
+#include "g10lib.h"
+#include "../cipher/rmd.h"
+#include "random.h"
+#include "rand-internal.h"
+#include "cipher.h" /* Required for the rmd160_hash_buffer() prototype.  */
+#include "ath.h"
+
+#ifndef RAND_MAX   /* For SunOS. */
+#define RAND_MAX 32767
+#endif
+
+/* Check whether we can lock the seed file read write. */
+#if defined(HAVE_FCNTL) && defined(HAVE_FTRUNCATE) && !defined(HAVE_W32_SYSTEM)
+#define LOCK_SEED_FILE 1
+#else
+#define LOCK_SEED_FILE 0
+#endif
+
+/* Define the constant we use for transforming the pool at read-out. */
+#if SIZEOF_UNSIGNED_LONG == 8
+#define ADD_VALUE 0xa5a5a5a5a5a5a5a5
+#elif SIZEOF_UNSIGNED_LONG == 4
+#define ADD_VALUE 0xa5a5a5a5
+#else
+#error weird size for an unsigned long
+#endif
+
+/* Contstants pertaining to the hash pool. */
+#define BLOCKLEN  64   /* Hash this amount of bytes... */
+#define DIGESTLEN 20   /* ... into a digest of this length (rmd160). */
+/* POOLBLOCKS is the number of digests which make up the pool.  */
+#define POOLBLOCKS 30
+/* POOLSIZE must be a multiple of the digest length to make the AND
+   operations faster, the size should also be a multiple of unsigned
+   long.  */
+#define POOLSIZE (POOLBLOCKS*DIGESTLEN)
+#if (POOLSIZE % SIZEOF_UNSIGNED_LONG)
+#error Please make sure that poolsize is a multiple of unsigned long
+#endif
+#define POOLWORDS (POOLSIZE / SIZEOF_UNSIGNED_LONG)
+
+
+/* RNDPOOL is the pool we use to collect the entropy and to stir it
+   up.  Its allocated size is POOLSIZE+BLOCKLEN.  Note that this is
+   also an indication on whether the module has been fully
+   initialized. */
+static unsigned char *rndpool;	
+
+/* KEYPOOL is used as a scratch copy to read out random from RNDPOOL.
+   Its allocated size is also POOLSIZE+BLOCKLEN.  */
+static unsigned char *keypool;	
+
+/* This is the offset into RNDPOOL where the next random bytes are to
+   be mixed in.  */
+static size_t pool_writepos;
+
+/* When reading data out of KEYPOOL, we start the read at different
+   positions.  This variable keeps track on where to read next.  */
+static size_t pool_readpos;
+
+/* This flag is set to true as soon as the pool has been completely
+   filled the first time.  This may happen either by rereading a seed
+   file or by adding enough entropy.  */
+static int pool_filled;
+
+/* This counter is used to track whether the initial seeding has been
+   done with enough bytes from a reliable entropy source.  */
+static size_t pool_filled_counter;
+
+/* If random of level GCRY_VERY_STRONG_RANDOM has been requested we
+   have stricter requirements on what kind of entropy is in the pool.
+   In particular POOL_FILLED is not sufficient.  Thus we add some
+   extra seeding and set this flag to true if the extra seeding has
+   been done.  */
+static int did_initial_extra_seeding;
+
+/* This variable is used to estimated the amount of fresh entropy
+   available in RNDPOOL.  */
+static int pool_balance;
+
+/* After a mixing operation this variable will be set to true and
+   cleared if new entropy has been added or a remix is required for
+   otehr reasons.  */
+static int just_mixed;
+
+/* The name of the seed file or NULL if no seed file has been defined.
+   The seed file needs to be regsitered at initialiation time.  We
+   keep a malloced copy here.  */
+static char *seed_file_name;
+
+/* If a seed file has been registered and maybe updated on exit this
+   flag set. */
+static int allow_seed_file_update;
+
+/* Option flag set at initialiation time to force allocation of the
+   pool in secure memory.  */
+static int secure_alloc;
+
+/* This function pointer is set to the actual entropy gathering
+   function during initailization.  After initialization it is
+   guaranteed to point to function.  (On systems without a random
+   gatherer module a dummy function is used).*/
+static int (*slow_gather_fnc)(void (*)(const void*, size_t,
+                                       enum random_origins),
+                              enum random_origins, size_t, int);
+
+/* This function is set to the actual fast entropy gathering fucntion
+   during initialization.  If it is NULL, no such function is
+   available. */
+static void (*fast_gather_fnc)(void (*)(const void*, size_t,
+                                        enum random_origins),
+                               enum random_origins);
+
+
+/* Option flag useful for debugging and the test suite.  If set
+   requests for very strong random are degraded to strong random.  Not
+   used by regular applications.  */
+static int quick_test;
+
+/* On systems without entropy gathering modules, this flag is set to
+   indicate that the random generator is not working properly.  A
+   warning message is issued as well.  This is useful only for
+   debugging and during development.  */
+static int faked_rng;
+
+/* This is the lock we use to protect all pool operations.  */
+static ath_mutex_t pool_lock = ATH_MUTEX_INITIALIZER;
+
+/* This is a helper for assert calls.  These calls are used to assert
+   that functions are called in a locked state.  It is not meant to be
+   thread-safe but as a method to get aware of missing locks in the
+   test suite.  */
+static int pool_is_locked;
+
+/* This is the lock we use to protect the buffer used by the nonce
+   generation.  */
+static ath_mutex_t nonce_buffer_lock = ATH_MUTEX_INITIALIZER;
+
+
+/* We keep some counters in this structure for the sake of the
+   _gcry_random_dump_stats () function.  */
+static struct
+{
+  unsigned long mixrnd;
+  unsigned long mixkey;
+  unsigned long slowpolls;
+  unsigned long fastpolls;
+  unsigned long getbytes1;
+  unsigned long ngetbytes1;
+  unsigned long getbytes2;
+  unsigned long ngetbytes2;
+  unsigned long addbytes;
+  unsigned long naddbytes;
+} rndstats;
+
+
+/* If not NULL a progress function called from certain places and the
+   opaque value passed along.  Registred by
+   _gcry_register_random_progress (). */
+static void (*progress_cb) (void *,const char*,int,int, int );
+static void *progress_cb_data;
+
+
+/* --- Stuff pertaining to the random daemon support. --- */
+#ifdef USE_RANDOM_DAEMON
+
+/* If ALLOW_DAEMON is true, the module will try to use the random
+   daemon first.  If the daemon has failed, this variable is set to
+   back to false and the code continues as normal.  Note, we don't
+   test this flag in a locked state because a wrong value does not
+   harm and the trhead will find out itself that the daemon does not
+   work and set it (again) to false.  */
+static int allow_daemon;       
+
+/* During initialization, the user may set a non-default socket name
+   for accessing the random daemon.  If this value is NULL, the
+   default name will be used. */
+static char *daemon_socket_name;
+
+#endif /*USE_RANDOM_DAEMON*/
+
+
+
+/* ---  Prototypes  --- */
+static void read_pool (byte *buffer, size_t length, int level );
+static void add_randomness (const void *buffer, size_t length, 
+                            enum random_origins origin);
+static void random_poll (void);
+static void do_fast_random_poll (void);
+static int (*getfnc_gather_random (void))(void (*)(const void*, size_t, 
+                                                   enum random_origins), 
+                                          enum random_origins, size_t, int);
+static void (*getfnc_fast_random_poll (void))(void (*)(const void*, size_t,
+                                                       enum random_origins),
+                                              enum random_origins);
+static void read_random_source (enum random_origins origin,
+                                size_t length, int level);
+static int gather_faked (void (*add)(const void*, size_t, enum random_origins),
+                         enum random_origins, size_t length, int level );
+
+
+
+/* ---  Functions  --- */
+
+
+/* Basic initialization which is required to initialize mutexes and
+   such.  It does not run a full initialization so that the filling of
+   the random pool can be delayed until it is actually needed.  We
+   assume that this function is used before any concurrent access
+   happens. */
+static void
+initialize_basics(void)
+{
+  static int initialized;
+  int err;
+
+  if (!initialized)
+    {
+      initialized = 1;
+      err = ath_mutex_init (&pool_lock);
+      if (err)
+        log_fatal ("failed to create the pool lock: %s\n", strerror (err) );
+      
+      err = ath_mutex_init (&nonce_buffer_lock);
+      if (err)
+        log_fatal ("failed to create the nonce buffer lock: %s\n",
+                   strerror (err) );
+
+#ifdef USE_RANDOM_DAEMON
+      _gcry_daemon_initialize_basics ();
+#endif /*USE_RANDOM_DAEMON*/
+
+      /* Make sure that we are still using the values we have
+         traditionally used for the random levels.  */
+      assert ( GCRY_WEAK_RANDOM == 0 
+               && GCRY_STRONG_RANDOM == 1
+               && GCRY_VERY_STRONG_RANDOM == 2);
+    }
+}
+
+/* Take the pool lock. */
+static void
+lock_pool (void)
+{
+  int err; 
+
+  err = ath_mutex_lock (&pool_lock);
+  if (err)
+    log_fatal ("failed to acquire the pool lock: %s\n", strerror (err));
+  pool_is_locked = 1;
+}
+
+/* Release the pool lock. */
+static void
+unlock_pool (void)
+{
+  int err; 
+
+  pool_is_locked = 0;
+  err = ath_mutex_unlock (&pool_lock);
+  if (err)
+    log_fatal ("failed to release the pool lock: %s\n", strerror (err));




More information about the Gnupg-commits mailing list