[git] GCRYPT - branch, master, updated. libgcrypt-1.7.3-98-gf5e7763

by Werner Koch cvs at cvs.gnupg.org
Wed Jun 14 14:11:42 CEST 2017


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "The GNU crypto library".

The branch, master has been updated
       via  f5e7763ddca59dcd9ac9f2f4d50cb41b14a34a9e (commit)
       via  6c882fb1fdb6c7cba2215fa7391110d63e24b9dc (commit)
       via  06f303a633ea2b992259688bef2b023c3f388f73 (commit)
       via  a44c45675f8b631e11048a540bb1fbb7a022ebb4 (commit)
       via  f0ae18ecf48fbe2da0b9fb3f354d0dd3173d91d3 (commit)
      from  cdfd7ea72a44657f037dd0dbba6e5ea0c2b344aa (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit f5e7763ddca59dcd9ac9f2f4d50cb41b14a34a9e
Author: Werner Koch <wk at gnupg.org>
Date:   Wed Jun 14 14:03:05 2017 +0200

    random: Add jitter RND based entropy collector.
    
    * random/rndjent.c: New.
    * random/rndlinux.c (_gcry_rndlinux_gather_random): Use rndjent.
    * random/rndw32.c (_gcry_rndw32_gather_random): Use rndjent.
    (slow_gatherer): Fix compiler warning.
    * random/Makefile.am (librandom_la_SOURCES): Add rndjent.c
    (EXTRA_librandom_la_SOURCES): Add jitterentropy-base.c and
    jitterentropy.h.
    (rndjent.o, rndjent.lo): New rules.
    * configure.ac: New option --disbale-jent-support
    (ENABLE_JENT_SUPPORT): New ac-define.
    
    Signed-off-by: Werner Koch <wk at gnupg.org>

diff --git a/AUTHORS b/AUTHORS
index b80d43e..c1e813d 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -30,13 +30,14 @@ List of Copyright holders
   Copyright (C) 1996-2006 Peter Gutmann, Matt Thomlinson and Blake Coverett
   Copyright (C) 2003 Nikos Mavroyanopoulos
   Copyright (C) 2006-2007 NTT (Nippon Telegraph and Telephone Corporation)
-  Copyright (C) 2012-2016 g10 Code GmbH
+  Copyright (C) 2012-2017 g10 Code GmbH
   Copyright (C) 2012 Simon Josefsson, Niels Möller
   Copyright (c) 2012 Intel Corporation
   Copyright (C) 2013 Christian Grothoff
   Copyright (C) 2013-2016 Jussi Kivilinna
   Copyright (C) 2013-2014 Dmitry Eremin-Solenikov
   Copyright (C) 2014 Stephan Mueller
+  Copyright (C) 2017 Bundesamt für Sicherheit in der Informationstechnik
 
 
 Authors with a FSF copyright assignment
diff --git a/configure.ac b/configure.ac
index 7ea0b6a..d36673f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -577,6 +577,14 @@ if test "$use_hmac_binary_check" = yes ; then
 fi
 
 
+# Implementation of the --disable-jent-support switch.
+AC_MSG_CHECKING([whether jitter entropy support is requested])
+AC_ARG_ENABLE(jent-support,
+              AC_HELP_STRING([--disable-jent-support],
+                        [Disable support for the Jitter entropy collector]),
+	      jentsupport=$enableval,jentsupport=yes)
+AC_MSG_RESULT($jentsupport)
+
 # Implementation of the --disable-padlock-support switch.
 AC_MSG_CHECKING([whether padlock support is requested])
 AC_ARG_ENABLE(padlock-support,
@@ -1185,6 +1193,7 @@ if test "$mpi_cpu_arch" != "x86" ; then
    avxsupport="n/a"
    avx2support="n/a"
    padlocksupport="n/a"
+   jentsupport="n/a"
    drngsupport="n/a"
 fi
 
@@ -1980,6 +1989,10 @@ if test x"$armcryptosupport" = xyes ; then
   AC_DEFINE(ENABLE_ARM_CRYPTO_SUPPORT,1,
             [Enable support for ARMv8 Crypto Extension instructions.])
 fi
+if test x"$jentsupport" = xyes ; then
+  AC_DEFINE(ENABLE_JENT_SUPPORT, 1,
+            [Enable support for the jitter entropy collector.])
+fi
 if test x"$padlocksupport" = xyes ; then
   AC_DEFINE(ENABLE_PADLOCK_SUPPORT, 1,
             [Enable support for the PadLock engine.])
@@ -2619,6 +2632,7 @@ GCRY_MSG_WRAP([Enabled digest algorithms:],[$enabled_digests])
 GCRY_MSG_WRAP([Enabled kdf algorithms:   ],[$enabled_kdfs])
 GCRY_MSG_WRAP([Enabled pubkey algorithms:],[$enabled_pubkey_ciphers])
 GCRY_MSG_SHOW([Random number generator:  ],[$random])
+GCRY_MSG_SHOW([Try using jitter entropy: ],[$jentsupport])
 GCRY_MSG_SHOW([Using linux capabilities: ],[$use_capabilities])
 GCRY_MSG_SHOW([Try using Padlock crypto: ],[$padlocksupport])
 GCRY_MSG_SHOW([Try using AES-NI crypto:  ],[$aesnisupport])
diff --git a/random/Makefile.am b/random/Makefile.am
index 92aba20..aaf205e 100644
--- a/random/Makefile.am
+++ b/random/Makefile.am
@@ -36,6 +36,7 @@ rand-internal.h \
 random-csprng.c \
 random-drbg.c \
 random-system.c \
+rndjent.c \
 rndhw.c
 
 if USE_RANDOM_DAEMON
@@ -48,4 +49,21 @@ rndlinux.c \
 rndegd.c \
 rndunix.c \
 rndw32.c  \
-rndw32ce.c
+rndw32ce.c \
+jitterentropy-base.c jitterentropy.h
+
+
+# The rndjent module needs to be compiled without optimization.  */
+if ENABLE_O_FLAG_MUNGING
+o_flag_munging = sed -e 's/-O\([1-9s][1-9s]*\)/-O0/' -e 's/-Ofast/-O0/g'
+else
+o_flag_munging = cat
+endif
+
+rndjent.o: $(srcdir)/rndjent.c \
+           $(srcdir)/jitterentropy-base.c $(srcdir)/jitterentropy.h
+	`echo $(COMPILE) -c $(srcdir)/rndjent.c | $(o_flag_munging) `
+
+rndjent.lo: $(srcdir)/rndjent.c \
+            $(srcdir)/jitterentropy-base.c $(srcdir)/jitterentropy.h
+	`echo $(LTCOMPILE) -c $(srcdir)/rndjent.c | $(o_flag_munging) `
diff --git a/random/rand-internal.h b/random/rand-internal.h
index 8c8623e..7a798e9 100644
--- a/random/rand-internal.h
+++ b/random/rand-internal.h
@@ -98,7 +98,7 @@ int _gcry_rndunix_gather_random (void (*add) (const void *, size_t,
                                  enum random_origins origin,
                                  size_t length, int level);
 
-/*-- rndelg.c --*/
+/*-- rndegd.c --*/
 int _gcry_rndegd_gather_random (void (*add) (const void *, size_t,
                                              enum random_origins),
                                 enum random_origins origin,
@@ -123,6 +123,12 @@ void _gcry_rndw32ce_gather_random_fast (void (*add)(const void*, size_t,
                                                     enum random_origins),
                                         enum random_origins origin );
 
+/*-- rndjent.c --*/
+size_t _gcry_rndjent_poll (void (*add)(const void*,
+                                       size_t, enum random_origins),
+                           enum random_origins origin,
+                           size_t length);
+
 /*-- rndhw.c --*/
 int _gcry_rndhw_failed_p (void);
 void _gcry_rndhw_poll_fast (void (*add)(const void*, size_t,
diff --git a/random/rndjent.c b/random/rndjent.c
new file mode 100644
index 0000000..fa3bb99
--- /dev/null
+++ b/random/rndjent.c
@@ -0,0 +1,208 @@
+/* rndjent.c  - Driver for the jitterentropy module.
+ * Copyright (C) 2017 g10 Code GmbH
+ * Copyright (C) 2017 Bundesamt für Sicherheit in der Informationstechnik
+ *
+ * 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/>.
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+
+#include "types.h"
+#include "g10lib.h"
+#include "../cipher/bithelp.h"
+#include "rand-internal.h"
+
+/*
+ * Decide whether we can support jent at compile time.
+ */
+#undef USE_JENT
+#ifdef ENABLE_JENT_SUPPORT
+# if defined (__i386__) || defined(__x86_64__)
+#   define USE_JENT 1
+# endif
+#endif /*ENABLE_JENT_SUPPORT*/
+
+
+#ifdef USE_JENT
+
+/* When using the libgcrypt secure memory mechanism, all precautions
+ * are taken to protect our state. If the user disables secmem during
+ * runtime, it is his decision and we thus try not to overrule his
+ * decision for less memory protection. */
+#define JENT_CPU_JITTERENTROPY_SECURE_MEMORY 1
+#define jent_zalloc(n) _gcry_calloc_secure (1, (n))
+
+
+

+/*
+ * Libgcrypt specific platform dependent functions.
+ */
+
+
+static void
+jent_get_nstime(u64 *out)
+{
+  u32 t_eax, t_edx;
+
+  asm volatile (".byte 0x0f,0x31\n\t"
+                : "=a" (t_eax), "=d" (t_edx)
+                );
+  *out = (((u64)t_edx << 32) | t_eax);
+}
+
+
+static GPGRT_INLINE void
+jent_zfree (void *ptr, unsigned int len)
+{
+  if (ptr)
+    {
+      wipememory (ptr, len);
+      _gcry_free (ptr);
+    }
+}
+
+static GPGRT_INLINE int
+jent_fips_enabled(void)
+{
+  return fips_mode();
+}
+
+
+/*
+ * We source include the actual jitter entropy code.  Note that the
+ * included code has been slightly changed from the Linux kernel
+ * version for namespace reasons.  We define MODULE so that the
+ * EXPORT_SYMBOL macro will not be used.
+ */
+#undef CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT
+/* Uncomment the next line to build with statistics.  */
+/* #define CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT 1 */
+
+#undef MODULE
+#define MODULE 1
+
+#ifndef HAVE_STDINT_H
+# error This module needs stdint.h - try ./configure --disable-jent-support
+#endif
+#include "jitterentropy-base.c"
+
+
+/* This is the lock we use to serialize access to this RNG.  The extra
+ * integer variable is only used to check the locking state; that is,
+ * it is not meant to be thread-safe but merely as a failsafe feature
+ * to assert proper locking.  */
+GPGRT_LOCK_DEFINE (jent_rng_lock);
+static int jent_rng_is_locked;
+
+/* This flag tracks whether the RNG has been initialized - either
+ * with error or with success.  Protected by JENT_RNG_LOCK. */
+static int jent_rng_is_initialized;
+
+/* Our collector.  The RNG is in a working state if its value is not
+ * NULL.  Protected by JENT_RNG_LOCK.  */
+struct rand_data *jent_rng_collector;
+
+
+/* Acquire the jent_rng_lock.  */
+static void
+lock_rng (void)
+{
+  gpg_err_code_t rc;
+
+  rc = gpgrt_lock_lock (&jent_rng_lock);
+  if (rc)
+    log_fatal ("failed to acquire the Jent RNG lock: %s\n",
+               gpg_strerror (rc));
+  jent_rng_is_locked = 1;
+}
+
+
+/* Release the jent_rng_lock.  */
+static void
+unlock_rng (void)
+{
+  gpg_err_code_t rc;
+
+  jent_rng_is_locked = 0;
+  rc = gpgrt_lock_unlock (&jent_rng_lock);
+  if (rc)
+    log_fatal ("failed to release the Jent RNG lock: %s\n",
+               gpg_strerror (rc));
+}
+
+#endif /* USE_JENT */
+
+

+/*
+ * The API used by the high level code.
+ */
+
+/* Read up to LENGTH bytes from a jitter RNG and return the number of
+ * bytes actually read.  */
+size_t
+_gcry_rndjent_poll (void (*add)(const void*, size_t, enum random_origins),
+                    enum random_origins origin, size_t length)
+{
+  size_t nbytes = 0;
+
+  (void)add;
+  (void)origin;
+
+#ifdef USE_JENT
+  if ((_gcry_get_hw_features () & HWF_INTEL_RDTSC))
+    {
+      lock_rng ();
+
+      if (!jent_rng_is_initialized)
+        {
+          /* Auto-initialize.  */
+          jent_rng_is_initialized = 1;
+          jent_entropy_collector_free (jent_rng_collector);
+          jent_rng_collector = NULL;
+          if (!jent_entropy_init ())
+            jent_rng_collector = jent_entropy_collector_alloc (1, 0);
+        }
+
+      if (jent_rng_collector)
+        {
+          /* We have a working JENT.  */
+          char buffer[256];
+
+          while (length)
+            {
+              int rc;
+              size_t n = length < sizeof(buffer)? length : sizeof (buffer);
+
+              rc = jent_read_entropy (jent_rng_collector, buffer, n);
+              if (rc < 0)
+                break;
+              (*add) (buffer, rc, origin);
+              length -= rc;
+              nbytes += rc;
+            }
+        }
+
+      unlock_rng ();
+    }
+#endif
+
+  return nbytes;
+}
diff --git a/random/rndlinux.c b/random/rndlinux.c
index d3a144a..f1548fb 100644
--- a/random/rndlinux.c
+++ b/random/rndlinux.c
@@ -158,6 +158,19 @@ _gcry_rndlinux_gather_random (void (*add)(const void*, size_t,
   if (length > 1)
     length -= n_hw;
 
+  /* When using a blocking random generator try to get some entropy
+   * from the jitter based RNG.  In this case we take up to 50% of the
+   * remaining requested bytes.  */
+  if (level >= GCRY_VERY_STRONG_RANDOM)
+    {
+      n_hw = _gcry_rndjent_poll (add, origin, length/2);
+      if (n_hw > length/2)
+        n_hw = length/2;
+      if (length > 1)
+        length -= n_hw;
+    }
+
+
   /* Open the requested device.  The first time a device is to be
      opened we fail with a fatal error if the device does not exists.
      In case the device has ever been closed, further open requests
@@ -165,7 +178,7 @@ _gcry_rndlinux_gather_random (void (*add)(const void*, size_t,
      that we always require the device to be existent but want a more
      graceful behaviour if the rarely needed close operation has been
      used and the device needs to be re-opened later. */
-  if (level >= 2)
+  if (level >= GCRY_VERY_STRONG_RANDOM)
     {
       if (fd_random == -1)
         {
diff --git a/random/rndw32.c b/random/rndw32.c
index 1dec5a7..7e9ac50 100644
--- a/random/rndw32.c
+++ b/random/rndw32.c
@@ -584,8 +584,8 @@ slow_gatherer ( void (*add)(const void*, size_t, enum random_origins),
 
     if (hNetAPI32
         && !pNetStatisticsGet (NULL,
-                               is_workstation ? L"LanmanWorkstation" :
-                               L"LanmanServer", 0, 0, &lpBuffer))
+                               (LPWSTR)(is_workstation ? L"LanmanWorkstation" :
+                                        L"LanmanServer"), 0, 0, &lpBuffer))
       {
         if ( debug_me )
           log_debug ("rndw32#slow_gatherer: get netstats\n" );
@@ -776,6 +776,7 @@ _gcry_rndw32_gather_random (void (*add)(const void*, size_t,
                             size_t length, int level )
 {
   static int is_initialized;
+  size_t n;
 
   if (!level)
     return 0;
@@ -808,6 +809,13 @@ _gcry_rndw32_gather_random (void (*add)(const void*, size_t,
 
   slow_gatherer (add, origin);
 
+  /* Round requested LENGTH up to full 32 bytes.  */
+  n = _gcry_rndjent_poll (add, origin, ((length + 31) / 32) * 32);
+
+  if (debug_me)
+    log_debug ("rndw32#gather_random: jent contributed extra %u bytes\n",
+               (unsigned int)n);
+
   return 0;
 }
 

commit 6c882fb1fdb6c7cba2215fa7391110d63e24b9dc
Author: Werner Koch <wk at gnupg.org>
Date:   Wed Jun 14 13:58:18 2017 +0200

    cipher: New helper function rol64.
    
    * cipher/bithelp.h (rol64): New inline functions.
    
    Signed-off-by: Werner Koch <wk at gnupg.org>

diff --git a/cipher/bithelp.h b/cipher/bithelp.h
index 4575380..26ef7c3 100644
--- a/cipher/bithelp.h
+++ b/cipher/bithelp.h
@@ -35,6 +35,11 @@ static inline u32 ror(u32 x, int n)
 	return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) );
 }
 
+static inline u64 rol64(u64 x, int n)
+{
+  return ( (x << (n&(64-1))) | (x >> ((64-n)&(64-1))) );
+}
+
 /* Byte swap for 32-bit and 64-bit integers.  If available, use compiler
    provided helpers.  */
 #ifdef HAVE_BUILTIN_BSWAP32

commit 06f303a633ea2b992259688bef2b023c3f388f73
Author: Werner Koch <wk at gnupg.org>
Date:   Wed Jun 14 13:57:45 2017 +0200

    New hardware feature flag HWF_INTEL_RDTSC.
    
    * src/g10lib.h (HWF_INTEL_RDTSC): New.
    * src/hwfeatures.c (hwflist): Add "intel-rdtsc".
    * src/hwf-x86.c (detect_x86_gnuc): Get EDX features and test for TSC.
    
    Signed-off-by: Werner Koch <wk at gnupg.org>

diff --git a/doc/gcrypt.texi b/doc/gcrypt.texi
index 464c673..ae66dfc 100644
--- a/doc/gcrypt.texi
+++ b/doc/gcrypt.texi
@@ -570,6 +570,7 @@ are
 @item intel-rdrand
 @item intel-avx
 @item intel-avx2
+ at item intel-rdtsc
 @item arm-neon
 @end table
 
diff --git a/src/g10lib.h b/src/g10lib.h
index 0309a83..82562c7 100644
--- a/src/g10lib.h
+++ b/src/g10lib.h
@@ -227,6 +227,9 @@ char **_gcry_strtokenize (const char *string, const char *delim);
 #define HWF_ARM_SHA2            (1 << 18)
 #define HWF_ARM_PMULL           (1 << 19)
 
+#define HWF_INTEL_RDTSC         (1 << 20)
+
+
 
 gpg_err_code_t _gcry_disable_hw_feature (const char *name);
 void _gcry_detect_hw_features (void);
diff --git a/src/hwf-x86.c b/src/hwf-x86.c
index 53e00d9..0d3a1f4 100644
--- a/src/hwf-x86.c
+++ b/src/hwf-x86.c
@@ -175,7 +175,7 @@ detect_x86_gnuc (void)
     char c[12+1];
     unsigned int ui[3];
   } vendor_id;
-  unsigned int features;
+  unsigned int features, features2;
   unsigned int os_supports_avx_avx2_registers = 0;
   unsigned int max_cpuid_level;
   unsigned int fms, family, model;
@@ -240,8 +240,8 @@ detect_x86_gnuc (void)
   /* Detect Intel features, that might also be supported by other
      vendors.  */
 
-  /* Get CPU family/model/stepping (EAX) and Intel feature flags (ECX).  */
-  get_cpuid(1, &fms, NULL, &features, NULL);
+  /* Get CPU family/model/stepping (EAX) and Intel feature flags (ECX, EDX).  */
+  get_cpuid(1, &fms, NULL, &features, &features2);
 
   family = ((fms & 0xf00) >> 8) + ((fms & 0xff00000) >> 20);
   model = ((fms & 0xf0) >> 4) + ((fms & 0xf0000) >> 12);
@@ -331,6 +331,10 @@ detect_x86_gnuc (void)
      result |= HWF_INTEL_RDRAND;
 #endif /*ENABLE_DRNG_SUPPORT*/
 
+  /* Test bit 4 of EDX for TSC.  */
+  if (features2 & 0x00000010)
+    result |= HWF_INTEL_RDTSC;
+
   /* Check additional Intel feature flags.  Early Intel P5 processors report
    * too high max_cpuid_level, so don't check level 7 if processor does not
    * support SSE3 (as cpuid:7 contains only features for newer processors).
diff --git a/src/hwfeatures.c b/src/hwfeatures.c
index b2ae7c3..45d7680 100644
--- a/src/hwfeatures.c
+++ b/src/hwfeatures.c
@@ -57,6 +57,7 @@ static struct
     { HWF_INTEL_AVX,           "intel-avx" },
     { HWF_INTEL_AVX2,          "intel-avx2" },
     { HWF_INTEL_FAST_VPGATHER, "intel-fast-vpgather" },
+    { HWF_INTEL_RDTSC,         "intel-rdtsc" },
     { HWF_ARM_NEON,            "arm-neon" },
     { HWF_ARM_AES,             "arm-aes" },
     { HWF_ARM_SHA1,            "arm-sha1" },

commit a44c45675f8b631e11048a540bb1fbb7a022ebb4
Author: Werner Koch <wk at gnupg.org>
Date:   Wed Jun 14 13:51:51 2017 +0200

    random: Changes to original Jitter RNG implementation
    
    * random/jitterentropy-base.c: Change double underscore symbols and
    make all functions static.
    * random/jitterentropy.h: Likewise.
    
    Signed-off-by: Werner Koch <wk at gnupg.org>

diff --git a/random/jitterentropy-base.c b/random/jitterentropy-base.c
index 23ac695..e80ca6e 100644
--- a/random/jitterentropy-base.c
+++ b/random/jitterentropy-base.c
@@ -81,6 +81,7 @@
  *
  * Return: Version number of kcapi library
  */
+#if 0
 unsigned int jent_version(void)
 {
         unsigned int version = 0;
@@ -91,6 +92,7 @@ unsigned int jent_version(void)
 
         return version;
 }
+#endif
 
 /**
  * Update of the loop count used for the next round of
@@ -104,11 +106,11 @@ unsigned int jent_version(void)
  *
  * @return Newly calculated loop counter
  */
-static __u64 jent_loop_shuffle(struct rand_data *ec,
+static u64 jent_loop_shuffle(struct rand_data *ec,
                                unsigned int bits, unsigned int min)
 {
-        __u64 time = 0;
-        __u64 shuffle = 0;
+        u64 time = 0;
+        u64 shuffle = 0;
         unsigned int i = 0;
         unsigned int mask = (1<<bits) - 1;
 
@@ -164,15 +166,15 @@ static __u64 jent_loop_shuffle(struct rand_data *ec,
  *
  * @return Number of loops the folding operation is performed
  */
-static __u64 jent_lfsr_time(struct rand_data *ec, __u64 time,
-                            __u64 loop_cnt)
+static u64 jent_lfsr_time(struct rand_data *ec, u64 time,
+                            u64 loop_cnt)
 {
         unsigned int i;
-        __u64 j = 0;
-        __u64 new = 0;
+        u64 j = 0;
+        u64 new = 0;
 #define MAX_FOLD_LOOP_BIT 4
 #define MIN_FOLD_LOOP_BIT 0
-        __u64 fold_loop_cnt =
+        u64 fold_loop_cnt =
                 jent_loop_shuffle(ec, MAX_FOLD_LOOP_BIT, MIN_FOLD_LOOP_BIT);
 
         /*
@@ -184,7 +186,7 @@ static __u64 jent_lfsr_time(struct rand_data *ec, __u64 time,
         for (j = 0; j < fold_loop_cnt; j++) {
                 new = ec->data;
                 for (i = 1; (DATA_SIZE_BITS) >= i; i++) {
-                        __u64 tmp = time << (DATA_SIZE_BITS - i);
+                        u64 tmp = time << (DATA_SIZE_BITS - i);
 
                         tmp = tmp >> (DATA_SIZE_BITS - 1);
 
@@ -240,13 +242,13 @@ static __u64 jent_lfsr_time(struct rand_data *ec, __u64 time,
  *
  * @return Number of memory access operations
  */
-static unsigned int jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
+static unsigned int jent_memaccess(struct rand_data *ec, u64 loop_cnt)
 {
         unsigned int wrap = 0;
-        __u64 i = 0;
+        u64 i = 0;
 #define MAX_ACC_LOOP_BIT 7
 #define MIN_ACC_LOOP_BIT 0
-        __u64 acc_loop_cnt =
+        u64 acc_loop_cnt =
                 jent_loop_shuffle(ec, MAX_ACC_LOOP_BIT, MIN_ACC_LOOP_BIT);
 
         if (NULL == ec || NULL == ec->mem)
@@ -299,10 +301,10 @@ static unsigned int jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
  *      0 jitter measurement not stuck (good bit)
  *      1 jitter measurement stuck (reject bit)
  */
-static int jent_stuck(struct rand_data *ec, __u64 current_delta)
+static int jent_stuck(struct rand_data *ec, u64 current_delta)
 {
-        __s64 delta2 = ec->last_delta - current_delta;
-        __s64 delta3 = delta2 - ec->last_delta2;
+        int64_t delta2 = ec->last_delta - current_delta;
+        int64_t delta3 = delta2 - ec->last_delta2;
 
         ec->last_delta = current_delta;
         ec->last_delta2 = delta2;
@@ -329,8 +331,8 @@ static int jent_stuck(struct rand_data *ec, __u64 current_delta)
  */
 static int jent_measure_jitter(struct rand_data *ec)
 {
-        __u64 time = 0;
-        __u64 current_delta = 0;
+        u64 time = 0;
+        u64 current_delta = 0;
         int stuck;
 
         /* Invoke one noise source before time measurement to add variations */
@@ -389,8 +391,8 @@ static void jent_stir_pool(struct rand_data *entropy_collector)
          * with two 32 bit variables
          */
         union c {
-                __u64 u64;
-                __u32 u32[2];
+                u64 u64;
+                u32 u32[2];
         };
         /*
          * This constant is derived from the first two 32 bit initialization
@@ -449,7 +451,7 @@ static void jent_gen_entropy(struct rand_data *ec)
         jent_measure_jitter(ec);
 
         while (1) {
-                __u64 prev_data = ec->data;
+                u64 prev_data = ec->data;
 
                 /* If a stuck measurement is received, repeat measurement */
                 if (jent_measure_jitter(ec))
@@ -491,7 +493,7 @@ static void jent_gen_entropy(struct rand_data *ec)
  * The following error codes can occur:
  *      -1	entropy_collector is NULL
  */
-ssize_t jent_read_entropy(struct rand_data *ec, char *data, size_t len)
+static int jent_read_entropy(struct rand_data *ec, char *data, size_t len)
 {
         char *p = data;
         size_t orig_len = len;
@@ -540,8 +542,8 @@ EXPORT_SYMBOL(jent_read_entropy);
  * Initialization logic
  ***************************************************************************/
 
-struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
-                                               unsigned int flags)
+static struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
+                                                        unsigned int flags)
 {
         struct rand_data *entropy_collector;
 
@@ -584,7 +586,7 @@ struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
 EXPORT_SYMBOL(jent_entropy_collector_alloc);
 #endif
 
-void jent_entropy_collector_free(struct rand_data *entropy_collector)
+static void jent_entropy_collector_free(struct rand_data *entropy_collector)
 {
         if (NULL != entropy_collector) {
                 if (NULL != entropy_collector->mem) {
@@ -598,11 +600,11 @@ void jent_entropy_collector_free(struct rand_data *entropy_collector)
 EXPORT_SYMBOL(jent_entropy_collector_free);
 #endif
 
-int jent_entropy_init(void)
+static int jent_entropy_init(void)
 {
         int i;
-        __u64 delta_sum = 0;
-        __u64 old_delta = 0;
+        u64 delta_sum = 0;
+        u64 old_delta = 0;
         int time_backwards = 0;
         int count_var = 0;
         int count_mod = 0;
@@ -633,9 +635,9 @@ int jent_entropy_init(void)
 #define TESTLOOPCOUNT 300
 #define CLEARCACHE 100
         for (i = 0; (TESTLOOPCOUNT + CLEARCACHE) > i; i++) {
-                __u64 time = 0;
-                __u64 time2 = 0;
-                __u64 delta = 0;
+                u64 time = 0;
+                u64 time2 = 0;
+                u64 delta = 0;
                 unsigned int lowdelta = 0;
 
                 jent_get_nstime(&time);
@@ -751,10 +753,10 @@ void jent_gen_entropy_stat(struct rand_data *entropy_collector,
  * Statistical test: obtain the distribution of the LFSR state value from
  * jent_lfsr_time
  */
-void jent_lfsr_time_stat(struct rand_data *ec, __u64 *fold, __u64 *loop_cnt)
+void jent_lfsr_time_stat(struct rand_data *ec, u64 *fold, u64 *loop_cnt)
 {
-        __u64 time = 0;
-        __u64 time2 = 0;
+        u64 time = 0;
+        u64 time2 = 0;
         jent_get_nstime(&time);
         jent_memaccess(ec, 0);
         /* implement the priming logic */
@@ -770,10 +772,10 @@ void jent_lfsr_time_stat(struct rand_data *ec, __u64 *fold, __u64 *loop_cnt)
  * is set, perform the given number of foldings. Otherwise, allow the
  * loop count shuffling to define the number of foldings.
  */
-__u64 jent_lfsr_var_stat(struct rand_data *ec, unsigned int min)
+u64 jent_lfsr_var_stat(struct rand_data *ec, unsigned int min)
 {
-        __u64 time = 0;
-        __u64 time2 = 0;
+        u64 time = 0;
+        u64 time2 = 0;
 
         jent_get_nstime(&time);
         jent_memaccess(ec, min);
diff --git a/random/jitterentropy.h b/random/jitterentropy.h
index 8012419..2c33256 100644
--- a/random/jitterentropy.h
+++ b/random/jitterentropy.h
@@ -39,14 +39,14 @@
  * DAMAGE.
  */
 
-#ifndef _JITTERENTROPY_H
-#define _JITTERENTROPY_H
+#ifndef GCRYPT_JITTERENTROPY_H
+#define GCRYPT_JITTERENTROPY_H
 
-#ifdef __KERNEL__
-#include "jitterentropy-base-kernel.h"
-#else
-#include "jitterentropy-base-user.h"
-#endif /* __KERNEL__ */
+/* #ifdef __KERNEL__ */
+/* #include "jitterentropy-base-kernel.h" */
+/* #else */
+/* #include "jitterentropy-base-user.h" */
+/* #endif /\* __KERNEL__ *\/ */
 
 /* Statistical data from the entropy source */
 struct entropy_stat {
@@ -66,10 +66,10 @@ struct entropy_stat {
                                            other hand, the bit statistics
                                            test is not interested in exact
                                            timing */
-        __u64 collection_begin;		/* timer for beginning of one
+        u64 collection_begin;		/* timer for beginning of one
                                            entropy collection round */
-        __u64 collection_end;		/* timer for end of one round */
-        __u64 old_delta;		/* Time delta of previous round to
+        u64 collection_end;		/* timer for end of one round */
+        u64 old_delta;		        /* Time delta of previous round to
                                            calculate delta of deltas */
         unsigned int setbits;		/* see _jent_calc_statistic */
         unsigned int varbits;		/* see _jent_calc_statistic */
@@ -84,11 +84,11 @@ struct rand_data
          * of the RNG are marked as SENSITIVE. A user must not
          * access that information while the RNG executes its loops to
          * calculate the next random value. */
-        __u64 data;		/* SENSITIVE Actual random number */
-        __u64 prev_time;	/* SENSITIVE Previous time stamp */
-#define DATA_SIZE_BITS ((sizeof(__u64)) * 8)
-        __u64 last_delta;	/* SENSITIVE stuck test */
-        __s64 last_delta2;	/* SENSITIVE stuck test */
+        u64 data;		/* SENSITIVE Actual random number */
+        u64 prev_time;	        /* SENSITIVE Previous time stamp */
+#define DATA_SIZE_BITS ((sizeof(u64)) * 8)
+        u64 last_delta;         /* SENSITIVE stuck test */
+        int64_t last_delta2;	/* SENSITIVE stuck test */
         unsigned int osr;	/* Oversample rate */
         unsigned int stir:1;		/* Post-processing stirring */
         unsigned int disable_unbias:1;	/* Deactivate Von-Neuman unbias */
@@ -121,22 +121,26 @@ struct rand_data
 
 /* Number of low bits of the time value that we want to consider */
 /* get raw entropy */
-ssize_t jent_read_entropy(struct rand_data *ec, char *data, size_t len);
+static int jent_read_entropy (struct rand_data *ec,
+                              char *data, size_t len);
+
 /* initialize an instance of the entropy collector */
-struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
-                                               unsigned int flags);
+static struct rand_data *jent_entropy_collector_alloc (unsigned int osr,
+                                                         unsigned int flags);
+
 /* clearing of entropy collector */
-void jent_entropy_collector_free(struct rand_data *entropy_collector);
+static void jent_entropy_collector_free (struct rand_data *ent_coll);
 
 /* initialization of entropy collector */
-int jent_entropy_init(void);
+static int jent_entropy_init (void);
 
 /* return version number of core library */
-unsigned int jent_version(void);
+/* unsigned int jent_version(void); */
 
 /* -- END of Main interface functions -- */
 
 /* -- BEGIN error codes for init function -- */
+/* FIXME!!! */
 #define ENOTIME         1 /* Timer service not available */
 #define ECOARSETIME	2 /* Timer too coarse for RNG */
 #define ENOMONOTONIC	3 /* Timer is not monotonic increasing */
@@ -148,25 +152,26 @@ unsigned int jent_version(void);
 
 /* -- BEGIN statistical test functions only complied with CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT -- */
 
-void _jent_init_statistic(struct rand_data *entropy_collector);
-void _jent_calc_statistic(struct rand_data *entropy_collector,
+#ifdef CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT
+
+static void jent_init_statistic(struct rand_data *entropy_collector);
+static void jent_calc_statistic(struct rand_data *entropy_collector,
                           struct entropy_stat *stat, unsigned int loop_cnt);
-void _jent_bit_count(struct rand_data *entropy_collector, __u64 prev_data);
+static void jent_bit_count (struct rand_data *entropy_collector, u64 prev_data);
+
+static void jent_gen_entropy_stat (struct rand_data *entropy_collector,
+                                   struct entropy_stat *stat);
+static void jent_lfsr_time_stat(struct rand_data *ec, u64 *fold, u64 *loop_cnt);
+static u64 jent_lfsr_var_stat(struct rand_data *ec, unsigned int min);
 
-#ifdef CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT
-#define jent_init_statistic(x)    do { _jent_init_statistic(x); }    while (0)
-#define jent_calc_statistic(x, y, z) do { _jent_calc_statistic(x, y, z); } while (0)
-#define jent_bit_count(x,y)       do { _jent_bit_count(x, y); }      while (0)
-void jent_gen_entropy_stat(struct rand_data *entropy_collector,
-                           struct entropy_stat *stat);
-void jent_lfsr_time_stat(struct rand_data *ec, __u64 *fold, __u64 *loop_cnt);
-__u64 jent_lfsr_var_stat(struct rand_data *ec, unsigned int min);
 #else /* CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT */
-#define jent_init_statistic(x)    do { if(0) _jent_init_statistic(x); }    while (0)
-#define jent_calc_statistic(x, y, z) do { if(0) _jent_calc_statistic(x, y, z); } while (0)
-#define jent_bit_count(x,y)       do { if(0) _jent_bit_count(x,y); }       while (0)
+
+# define jent_init_statistic(x)       do { } while (0)
+# define jent_calc_statistic(x, y, z) do { } while (0)
+# define jent_bit_count(x,y)          do { (void)(y); } while (0)
+
 #endif /* CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT */
 
 /* -- END of statistical test function -- */
 
-#endif /* _JITTERENTROPY_H */
+#endif /* GCRYPT_JITTERENTROPY_H */

commit f0ae18ecf48fbe2da0b9fb3f354d0dd3173d91d3
Author: Stephan Mueller <smueller at chronox.de>
Date:   Tue May 23 18:53:21 2017 +0200

    random: Add original Jitter RNG implementation
    
    * random/jitterentropy-base-user.h: New.
    * random/jitterentropy-base.c: New.
    * random/jitterentropy.h: New.
    --
    
    Signed-off-by: Werner Koch <wk at gnupg.org>
    
    - Tabs and trailing white spaces removed from original source.
    - Source received by mail dated Fri, 27 Jan 2017 17:52:38 +0100 from
      Stephan

diff --git a/LICENSES b/LICENSES
index ebc18b3..9c0267e 100644
--- a/LICENSES
+++ b/LICENSES
@@ -54,6 +54,51 @@ with any binary distributions derived from the GNU C Library.
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #+end_quote
 
+
+  For files:
+  - random/jitterentropy-base-user.h
+  - random/jitterentropy-base.c
+  - random/jitterentropy.h
+
+#+begin_quote
+ * Copyright Stephan Mueller <smueller at chronox.de>, 2013
+ *
+ * License
+ * =======
+ *
+ * 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, and the entire permission notice in its entirety,
+ *    including the disclaimer of warranties.
+ * 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. The name of the author may not be used to endorse or promote
+ *    products derived from this software without specific prior
+ *    written permission.
+ *
+ * ALTERNATIVELY, this product may be distributed under the terms of
+ * the GNU General Public License, in which case the provisions of the GPL are
+ * required INSTEAD OF the above restrictions.  (This clause is
+ * necessary due to a potential bad interaction between the GPL and
+ * the restrictions contained in a BSD-style copyright.)
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
+ * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR 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 NOT ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+#+end_quote
+
 * X License
 
   For files:
diff --git a/random/jitterentropy-base-user.h b/random/jitterentropy-base-user.h
new file mode 100644
index 0000000..2aecf3b
--- /dev/null
+++ b/random/jitterentropy-base-user.h
@@ -0,0 +1,103 @@
+/*
+ * Non-physical true random number generator based on timing jitter.
+ *
+ * Copyright Stephan Mueller <smueller at chronox.de>, 2013
+ *
+ * License
+ * =======
+ *
+ * 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, and the entire permission notice in its entirety,
+ *    including the disclaimer of warranties.
+ * 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. The name of the author may not be used to endorse or promote
+ *    products derived from this software without specific prior
+ *    written permission.
+ *
+ * ALTERNATIVELY, this product may be distributed under the terms of
+ * the GNU General Public License, in which case the provisions of the GPL are
+ * required INSTEAD OF the above restrictions.  (This clause is
+ * necessary due to a potential bad interaction between the GPL and
+ * the restrictions contained in a BSD-style copyright.)
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
+ * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR 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 NOT ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+#ifndef _JITTERENTROPY_BASE_X86_H
+#define _JITTERENTROPY_BASE_X86_H
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <config.h>
+#include "g10lib.h"
+
+typedef uint64_t __u64;
+
+#define RdTSC __asm _emit 0x0f __asm _emit 0x31
+
+static void jent_get_nstime(__u64 *out)
+{
+	__u64 ret = 0;
+	_asm {
+		RdTSC
+		mov DWORD PTR ret, eax
+		mov DWORD PTR[ret + 4], edx
+	}
+	*out = ret;
+}
+
+static inline void *jent_zalloc(size_t len)
+{
+	void *tmp = NULL;
+
+	/* When using the libgcrypt secure memory mechanism, all precautions
+	 * are taken to protect our state. If the user disables secmem during
+	 * runtime, it is his decision and we thus try not to overrule his
+	 * decision for less memory protection. */
+#define CONFIG_CRYPTO_CPU_JITTERENTROPY_SECURE_MEMORY
+	tmp = gcry_xmalloc_secure(len);
+	if(NULL != tmp)
+		memset(tmp, 0, len);
+	return tmp;
+}
+
+static inline void jent_zfree(void *ptr, unsigned int len)
+{
+	memset(ptr, 0, len);
+	gcry_free(ptr);
+}
+
+static inline int jent_fips_enabled(void)
+{
+        return fips_mode();
+}
+
+/* --- helpers needed in user space -- */
+
+/* note: these helper functions are shamelessly stolen from the kernel :-) */
+
+static inline __u64 rol64(__u64 word, unsigned int shift)
+{
+	return (word << shift) | (word >> (64 - shift));
+}
+
+
+#endif /* _JITTERENTROPY_BASE_X86_H */
diff --git a/random/jitterentropy-base.c b/random/jitterentropy-base.c
new file mode 100644
index 0000000..23ac695
--- /dev/null
+++ b/random/jitterentropy-base.c
@@ -0,0 +1,784 @@
+/*
+ * Non-physical true random number generator based on timing jitter.
+ *
+ * Copyright Stephan Mueller <smueller at chronox.de>, 2014
+ *
+ * Design
+ * ======
+ *
+ * See documentation in doc/ folder.
+ *
+ * Interface
+ * =========
+ *
+ * See documentation in doc/ folder.
+ *
+ * License
+ * =======
+ *
+ * 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, and the entire permission notice in its entirety,
+ *    including the disclaimer of warranties.
+ * 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. The name of the author may not be used to endorse or promote
+ *    products derived from this software without specific prior
+ *    written permission.
+ *
+ * ALTERNATIVELY, this product may be distributed under the terms of
+ * the GNU General Public License, in which case the provisions of the GPL2 are
+ * required INSTEAD OF the above restrictions.  (This clause is
+ * necessary due to a potential bad interaction between the GPL and
+ * the restrictions contained in a BSD-style copyright.)
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
+ * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR 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 NOT ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+#include "jitterentropy.h"
+
+#ifndef CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT
+ /* only check optimization in a compilation for real work */
+ #ifdef __OPTIMIZE__
+  #error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy-base.c."
+ #endif
+#endif
+
+#define MAJVERSION 2 /* API / ABI incompatible changes, functional changes that
+                      * require consumer to be updated (as long as this number
+                      * is zero, the API is not considered stable and can
+                      * change without a bump of the major version) */
+#define MINVERSION 0 /* API compatible, ABI may change, functional
+                      * enhancements only, consumer can be left unchanged if
+                      * enhancements are not considered */
+#define PATCHLEVEL 0 /* API / ABI compatible, no functional changes, no
+                      * enhancements, bug fixes only */
+
+/**
+ * jent_version() - Return machine-usable version number of jent library
+ *
+ * The function returns a version number that is monotonic increasing
+ * for newer versions. The version numbers are multiples of 100. For example,
+ * version 1.2.3 is converted to 1020300 -- the last two digits are reserved
+ * for future use.
+ *
+ * The result of this function can be used in comparing the version number
+ * in a calling program if version-specific calls need to be make.
+ *
+ * Return: Version number of kcapi library
+ */
+unsigned int jent_version(void)
+{
+        unsigned int version = 0;
+
+        version =  MAJVERSION * 1000000;
+        version += MINVERSION * 10000;
+        version += PATCHLEVEL * 100;
+
+        return version;
+}
+
+/**
+ * Update of the loop count used for the next round of
+ * an entropy collection.
+ *
+ * Input:
+ * @ec entropy collector struct -- may be NULL
+ * @bits is the number of low bits of the timer to consider
+ * @min is the number of bits we shift the timer value to the right at
+ *      the end to make sure we have a guaranteed minimum value
+ *
+ * @return Newly calculated loop counter
+ */
+static __u64 jent_loop_shuffle(struct rand_data *ec,
+                               unsigned int bits, unsigned int min)
+{
+        __u64 time = 0;
+        __u64 shuffle = 0;
+        unsigned int i = 0;
+        unsigned int mask = (1<<bits) - 1;
+
+        jent_get_nstime(&time);
+        /*
+         * Mix the current state of the random number into the shuffle
+         * calculation to balance that shuffle a bit more.
+         */
+        if (ec)
+                time ^= ec->data;
+        /*
+         * We fold the time value as much as possible to ensure that as many
+         * bits of the time stamp are included as possible.
+         */
+        for (i = 0; (DATA_SIZE_BITS / bits) > i; i++) {
+                shuffle ^= time & mask;
+                time = time >> bits;
+        }
+
+        /*
+         * We add a lower boundary value to ensure we have a minimum
+         * RNG loop count.
+         */
+        return (shuffle + (1<<min));
+}
+
+/***************************************************************************
+ * Noise sources
+ ***************************************************************************/
+
+/**
+ * CPU Jitter noise source -- this is the noise source based on the CPU
+ *                            execution time jitter
+ *
+ * This function injects the individual bits of the time value into the
+ * entropy pool using an LFSR.
+ *
+ * The code is deliberately inefficient with respect to the bit shifting
+ * and shall stay that way. This function is the root cause why the code
+ * shall be compiled without optimization. This function not only acts as
+ * folding operation, but this function's execution is used to measure
+ * the CPU execution time jitter. Any change to the loop in this function
+ * implies that careful retesting must be done.
+ *
+ * Input:
+ * @ec entropy collector struct -- may be NULL
+ * @time time stamp to be injected
+ * @loop_cnt if a value not equal to 0 is set, use the given value as number of
+ *           loops to perform the folding
+ *
+ * Output:
+ * updated ec->data
+ *
+ * @return Number of loops the folding operation is performed
+ */
+static __u64 jent_lfsr_time(struct rand_data *ec, __u64 time,
+                            __u64 loop_cnt)
+{
+        unsigned int i;
+        __u64 j = 0;
+        __u64 new = 0;
+#define MAX_FOLD_LOOP_BIT 4
+#define MIN_FOLD_LOOP_BIT 0
+        __u64 fold_loop_cnt =
+                jent_loop_shuffle(ec, MAX_FOLD_LOOP_BIT, MIN_FOLD_LOOP_BIT);
+
+        /*
+         * testing purposes -- allow test app to set the counter, not
+         * needed during runtime
+         */
+        if (loop_cnt)
+                fold_loop_cnt = loop_cnt;
+        for (j = 0; j < fold_loop_cnt; j++) {
+                new = ec->data;
+                for (i = 1; (DATA_SIZE_BITS) >= i; i++) {
+                        __u64 tmp = time << (DATA_SIZE_BITS - i);
+
+                        tmp = tmp >> (DATA_SIZE_BITS - 1);
+
+                        /*
+                        * Fibonacci LSFR with polynomial of
+                        *  x^64 + x^61 + x^56 + x^31 + x^28 + x^23 + 1 which is
+                        *  primitive according to
+                        *   http://poincare.matf.bg.ac.rs/~ezivkovm/publications/primpol1.pdf
+                        * (the shift values are the polynomial values minus one
+                        * due to counting bits from 0 to 63). As the current
+                        * position is always the LSB, the polynomial only needs
+                        * to shift data in from the left without wrap.
+                        */
+                        new ^= tmp;
+                        new ^= ((new >> 63) & 1);
+                        new ^= ((new >> 60) & 1);
+                        new ^= ((new >> 55) & 1);
+                        new ^= ((new >> 30) & 1);
+                        new ^= ((new >> 27) & 1);
+                        new ^= ((new >> 22) & 1);
+                        new = rol64(new, 1);
+                }
+        }
+        ec->data = new;
+
+        return fold_loop_cnt;
+}
+
+/**
+ * Memory Access noise source -- this is a noise source based on variations in
+ *                               memory access times
+ *
+ * This function performs memory accesses which will add to the timing
+ * variations due to an unknown amount of CPU wait states that need to be
+ * added when accessing memory. The memory size should be larger than the L1
+ * caches as outlined in the documentation and the associated testing.
+ *
+ * The L1 cache has a very high bandwidth, albeit its access rate is  usually
+ * slower than accessing CPU registers. Therefore, L1 accesses only add minimal
+ * variations as the CPU has hardly to wait. Starting with L2, significant
+ * variations are added because L2 typically does not belong to the CPU any more
+ * and therefore a wider range of CPU wait states is necessary for accesses.
+ * L3 and real memory accesses have even a wider range of wait states. However,
+ * to reliably access either L3 or memory, the ec->mem memory must be quite
+ * large which is usually not desirable.
+ *
+ * Input:
+ * @ec Reference to the entropy collector with the memory access data -- if
+ *     the reference to the memory block to be accessed is NULL, this noise
+ *     source is disabled
+ * @loop_cnt if a value not equal to 0 is set, use the given value as number of
+ *           loops to perform the folding
+ *
+ * @return Number of memory access operations
+ */
+static unsigned int jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
+{
+        unsigned int wrap = 0;
+        __u64 i = 0;
+#define MAX_ACC_LOOP_BIT 7
+#define MIN_ACC_LOOP_BIT 0
+        __u64 acc_loop_cnt =
+                jent_loop_shuffle(ec, MAX_ACC_LOOP_BIT, MIN_ACC_LOOP_BIT);
+
+        if (NULL == ec || NULL == ec->mem)
+                return 0;
+        wrap = ec->memblocksize * ec->memblocks;
+
+        /*
+         * testing purposes -- allow test app to set the counter, not
+         * needed during runtime
+         */
+        if (loop_cnt)
+                acc_loop_cnt = loop_cnt;
+
+        for (i = 0; i < (ec->memaccessloops + acc_loop_cnt); i++) {
+                unsigned char *tmpval = ec->mem + ec->memlocation;
+                /*
+                 * memory access: just add 1 to one byte,
+                 * wrap at 255 -- memory access implies read
+                 * from and write to memory location
+                 */
+                *tmpval = (*tmpval + 1) & 0xff;
+                /*
+                 * Addition of memblocksize - 1 to pointer
+                 * with wrap around logic to ensure that every
+                 * memory location is hit evenly
+                 */
+                ec->memlocation = ec->memlocation + ec->memblocksize - 1;
+                ec->memlocation = ec->memlocation % wrap;
+        }
+        return i;
+}
+
+/***************************************************************************
+ * Start of entropy processing logic
+ ***************************************************************************/
+
+/**
+ * Stuck test by checking the:
+ *      1st derivation of the jitter measurement (time delta)
+ *      2nd derivation of the jitter measurement (delta of time deltas)
+ *      3rd derivation of the jitter measurement (delta of delta of time deltas)
+ *
+ * All values must always be non-zero.
+ *
+ * Input:
+ * @ec Reference to entropy collector
+ * @current_delta Jitter time delta
+ *
+ * @return
+ *      0 jitter measurement not stuck (good bit)
+ *      1 jitter measurement stuck (reject bit)
+ */
+static int jent_stuck(struct rand_data *ec, __u64 current_delta)
+{
+        __s64 delta2 = ec->last_delta - current_delta;
+        __s64 delta3 = delta2 - ec->last_delta2;
+
+        ec->last_delta = current_delta;
+        ec->last_delta2 = delta2;
+
+        if (!current_delta || !delta2 || !delta3)
+                return 1;
+
+        return 0;
+}
+
+/**
+ * This is the heart of the entropy generation: calculate time deltas and
+ * use the CPU jitter in the time deltas. The jitter is injected into the
+ * entropy pool.
+ *
+ * WARNING: ensure that ->prev_time is primed before using the output
+ *          of this function! This can be done by calling this function
+ *          and not using its result.
+ *
+ * Input:
+ * @entropy_collector Reference to entropy collector
+ *
+ * @return: result of stuck test
+ */
+static int jent_measure_jitter(struct rand_data *ec)
+{
+        __u64 time = 0;
+        __u64 current_delta = 0;
+        int stuck;
+
+        /* Invoke one noise source before time measurement to add variations */
+        jent_memaccess(ec, 0);
+
+        /*
+         * Get time stamp and calculate time delta to previous
+         * invocation to measure the timing variations
+         */
+        jent_get_nstime(&time);
+        current_delta = time - ec->prev_time;
+        ec->prev_time = time;
+
+        /* Now call the next noise sources which also injects the data */
+        jent_lfsr_time(ec, current_delta, 0);
+
+        /* Check whether we have a stuck measurement. */
+        stuck = jent_stuck(ec, current_delta);
+
+        /*
+         * Rotate the data buffer by a prime number (any odd number would
+         * do) to ensure that every bit position of the input time stamp
+         * has an even chance of being merged with a bit position in the
+         * entropy pool. We do not use one here as the adjacent bits in
+         * successive time deltas may have some form of dependency. The
+         * chosen value of 7 implies that the low 7 bits of the next
+         * time delta value is concatenated with the current time delta.
+         */
+        if (!stuck)
+                ec->data = rol64(ec->data, 7);
+
+        return stuck;
+}
+
+/**
+ * Shuffle the pool a bit by mixing some value with a bijective function (XOR)
+ * into the pool.
+ *
+ * The function generates a mixer value that depends on the bits set and the
+ * location of the set bits in the random number generated by the entropy
+ * source. Therefore, based on the generated random number, this mixer value
+ * can have 2**64 different values. That mixer value is initialized with the
+ * first two SHA-1 constants. After obtaining the mixer value, it is XORed into
+ * the random number.
+ *
+ * The mixer value is not assumed to contain any entropy. But due to the XOR
+ * operation, it can also not destroy any entropy present in the entropy pool.
+ *
+ * Input:
+ * @entropy_collector Reference to entropy collector
+ */
+static void jent_stir_pool(struct rand_data *entropy_collector)
+{
+        /*
+         * to shut up GCC on 32 bit, we have to initialize the 64 variable
+         * with two 32 bit variables
+         */
+        union c {
+                __u64 u64;
+                __u32 u32[2];
+        };
+        /*
+         * This constant is derived from the first two 32 bit initialization
+         * vectors of SHA-1 as defined in FIPS 180-4 section 5.3.1
+         */
+        union c constant;
+        /*
+         * The start value of the mixer variable is derived from the third
+         * and fourth 32 bit initialization vector of SHA-1 as defined in
+         * FIPS 180-4 section 5.3.1
+         */
+        union c mixer;
+        unsigned int i = 0;
+
+        /* Ensure that the function implements a constant time operation. */
+        union c throw_away;
+
+        /*
+         * Store the SHA-1 constants in reverse order to make up the 64 bit
+         * value -- this applies to a little endian system, on a big endian
+         * system, it reverses as expected. But this really does not matter
+         * as we do not rely on the specific numbers. We just pick the SHA-1
+         * constants as they have a good mix of bit set and unset.
+         */
+        constant.u32[1] = 0x67452301;
+        constant.u32[0] = 0xefcdab89;
+        mixer.u32[1] = 0x98badcfe;
+        mixer.u32[0] = 0x10325476;
+
+        for (i = 0; i < DATA_SIZE_BITS; i++) {
+                /*
+                 * get the i-th bit of the input random number and only XOR
+                 * the constant into the mixer value when that bit is set
+                 */
+                if ((entropy_collector->data >> i) & 1)
+                        mixer.u64 ^= constant.u64;
+                else
+                        throw_away.u64 ^= constant.u64;
+                mixer.u64 = rol64(mixer.u64, 1);
+        }
+        entropy_collector->data ^= mixer.u64;
+}
+
+/**
+ * Generator of one 64 bit random number
+ * Function fills rand_data->data
+ *
+ * Input:
+ * @ec Reference to entropy collector
+ */
+static void jent_gen_entropy(struct rand_data *ec)
+{
+        unsigned int k = 0;
+
+        /* priming of the ->prev_time value */
+        jent_measure_jitter(ec);
+
+        while (1) {
+                __u64 prev_data = ec->data;
+
+                /* If a stuck measurement is received, repeat measurement */
+                if (jent_measure_jitter(ec))
+                        continue;
+
+                /* statistics testing only */
+                jent_bit_count(ec, prev_data);
+
+                /*
+                 * We multiply the loop value with ->osr to obtain the
+                 * oversampling rate requested by the caller
+                 */
+                if (++k >= (DATA_SIZE_BITS * ec->osr))
+                        break;
+        }
+        if (ec->stir)
+                jent_stir_pool(ec);
+}
+
+/**
+ * Entry function: Obtain entropy for the caller.
+ *
+ * This function invokes the entropy gathering logic as often to generate
+ * as many bytes as requested by the caller. The entropy gathering logic
+ * creates 64 bit per invocation.
+ *
+ * This function truncates the last 64 bit entropy value output to the exact
+ * size specified by the caller.
+ *
+ * Input:
+ * @ec Reference to entropy collector
+ * @data pointer to buffer for storing random data -- buffer must already
+ *        exist
+ * @len size of the buffer, specifying also the requested number of random
+ *       in bytes
+ *
+ * @return number of bytes returned when request is fulfilled or an error
+ *
+ * The following error codes can occur:
+ *      -1	entropy_collector is NULL
+ */
+ssize_t jent_read_entropy(struct rand_data *ec, char *data, size_t len)
+{
+        char *p = data;
+        size_t orig_len = len;
+
+        if (NULL == ec)
+                return -1;
+
+        while (0 < len) {
+                size_t tocopy;
+                jent_gen_entropy(ec);
+                if ((DATA_SIZE_BITS / 8) < len)
+                        tocopy = (DATA_SIZE_BITS / 8);
+                else
+                        tocopy = len;
+                memcpy(p, &ec->data, tocopy);
+
+                len -= tocopy;
+                p += tocopy;
+        }
+
+        /*
+         * To be on the safe side, we generate one more round of entropy
+         * which we do not give out to the caller. That round shall ensure
+         * that in case the calling application crashes, memory dumps, pages
+         * out, or due to the CPU Jitter RNG lingering in memory for long
+         * time without being moved and an attacker cracks the application,
+         * all he reads in the entropy pool is a value that is NEVER EVER
+         * being used for anything. Thus, he does NOT see the previous value
+         * that was returned to the caller for cryptographic purposes.
+         */
+        /*
+         * If we use secured memory, do not use that precaution as the secure
+         * memory protects the entropy pool. Moreover, note that using this
+         * call reduces the speed of the RNG by up to half
+         */
+#ifndef CONFIG_CRYPTO_CPU_JITTERENTROPY_SECURE_MEMORY
+        jent_gen_entropy(ec);
+#endif
+        return orig_len;
+}
+#if defined(__KERNEL__) && !defined(MODULE)
+EXPORT_SYMBOL(jent_read_entropy);
+#endif
+
+/***************************************************************************
+ * Initialization logic
+ ***************************************************************************/
+
+struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
+                                               unsigned int flags)
+{
+        struct rand_data *entropy_collector;
+
+        entropy_collector = jent_zalloc(sizeof(struct rand_data));
+        if (NULL == entropy_collector)
+                return NULL;
+
+        if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
+                /* Allocate memory for adding variations based on memory
+                 * access
+                 */
+                entropy_collector->mem =
+                        (unsigned char *)jent_zalloc(JENT_MEMORY_SIZE);
+                if (NULL == entropy_collector->mem) {
+                        jent_zfree(entropy_collector, sizeof(struct rand_data));
+                        return NULL;
+                }
+                entropy_collector->memblocksize = JENT_MEMORY_BLOCKSIZE;
+                entropy_collector->memblocks = JENT_MEMORY_BLOCKS;
+                entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
+        }
+
+        /* verify and set the oversampling rate */
+        if (0 == osr)
+                osr = 1; /* minimum sampling rate is 1 */
+        entropy_collector->osr = osr;
+
+        entropy_collector->stir = 1;
+        if (flags & JENT_DISABLE_STIR)
+                entropy_collector->stir = 0;
+        if (flags & JENT_DISABLE_UNBIAS)
+                entropy_collector->disable_unbias = 1;
+
+        /* fill the data pad with non-zero values */
+        jent_gen_entropy(entropy_collector);
+
+        return entropy_collector;
+}
+#if defined(__KERNEL__) && !defined(MODULE)
+EXPORT_SYMBOL(jent_entropy_collector_alloc);
+#endif
+
+void jent_entropy_collector_free(struct rand_data *entropy_collector)
+{
+        if (NULL != entropy_collector) {
+                if (NULL != entropy_collector->mem) {
+                        jent_zfree(entropy_collector->mem, JENT_MEMORY_SIZE);
+                        entropy_collector->mem = NULL;
+                }
+                jent_zfree(entropy_collector, sizeof(struct rand_data));
+        }
+}
+#if defined(__KERNEL__) && !defined(MODULE)
+EXPORT_SYMBOL(jent_entropy_collector_free);
+#endif
+
+int jent_entropy_init(void)
+{
+        int i;
+        __u64 delta_sum = 0;
+        __u64 old_delta = 0;
+        int time_backwards = 0;
+        int count_var = 0;
+        int count_mod = 0;
+        struct rand_data ec;
+
+        /* We could perform statistical tests here, but the problem is
+         * that we only have a few loop counts to do testing. These
+         * loop counts may show some slight skew and we produce
+         * false positives.
+         *
+         * Moreover, only old systems show potentially problematic
+         * jitter entropy that could potentially be caught here. But
+         * the RNG is intended for hardware that is available or widely
+         * used, but not old systems that are long out of favor. Thus,
+         * no statistical tests.
+         */
+
+        /*
+         * We could add a check for system capabilities such as clock_getres or
+         * check for CONFIG_X86_TSC, but it does not make much sense as the
+         * following sanity checks verify that we have a high-resolution
+         * timer.
+         */
+        /*
+         * TESTLOOPCOUNT needs some loops to identify edge systems. 100 is
+         * definitely too little.
+         */
+#define TESTLOOPCOUNT 300
+#define CLEARCACHE 100
+        for (i = 0; (TESTLOOPCOUNT + CLEARCACHE) > i; i++) {
+                __u64 time = 0;
+                __u64 time2 = 0;
+                __u64 delta = 0;
+                unsigned int lowdelta = 0;
+
+                jent_get_nstime(&time);
+                jent_lfsr_time(&ec, time, 1<<MIN_FOLD_LOOP_BIT);
+                jent_get_nstime(&time2);
+
+                /* test whether timer works */
+                if (!time || !time2)
+                        return ENOTIME;
+                delta = time2 - time;
+                /*
+                 * test whether timer is fine grained enough to provide
+                 * delta even when called shortly after each other -- this
+                 * implies that we also have a high resolution timer
+                 */
+                if (!delta)
+                        return ECOARSETIME;
+
+                /*
+                 * up to here we did not modify any variable that will be
+                 * evaluated later, but we already performed some work. Thus we
+                 * already have had an impact on the caches, branch prediction,
+                 * etc. with the goal to clear it to get the worst case
+                 * measurements.
+                 */
+                if (CLEARCACHE > i)
+                        continue;
+
+                /* test whether we have an increasing timer */
+                if (!(time2 > time))
+                        time_backwards++;
+
+                /* use 32 bit value to ensure compilation on 32 bit arches */
+                lowdelta = time2 - time;
+                if (!(lowdelta % 100))
+                        count_mod++;
+
+                /*
+                 * ensure that we have a varying delta timer which is necessary
+                 * for the calculation of entropy -- perform this check
+                 * only after the first loop is executed as we need to prime
+                 * the old_data value
+                 */
+                if (i) {
+                        if (delta != old_delta)
+                                count_var++;
+                        if (delta > old_delta)
+                                delta_sum += (delta - old_delta);
+                        else
+                                delta_sum += (old_delta - delta);
+                }
+                old_delta = delta;
+        }
+
+        /*
+         * we allow up to three times the time running backwards.
+         * CLOCK_REALTIME is affected by adjtime and NTP operations. Thus,
+         * if such an operation just happens to interfere with our test, it
+         * should not fail. The value of 3 should cover the NTP case being
+         * performed during our test run.
+         */
+        if (3 < time_backwards)
+                return ENOMONOTONIC;
+        /* Error if the time variances are always identical */
+        if (!delta_sum)
+                return EVARVAR;
+
+        /*
+         * Variations of deltas of time must on average be larger
+         * than 1 to ensure the entropy estimation
+         * implied with 1 is preserved
+         */
+        if ((delta_sum) <= 1)
+                return EMINVARVAR;
+
+        /*
+         * Ensure that we have variations in the time stamp below 10 for at least
+         * 10% of all checks -- on some platforms, the counter increments in
+         * multiples of 100, but not always
+         */
+        if ((TESTLOOPCOUNT/10 * 9) < count_mod)
+                return ECOARSETIME;
+
+        return 0;
+}
+#if defined(__KERNEL__) && !defined(MODULE)
+EXPORT_SYMBOL(jent_entropy_init);
+#endif
+
+/***************************************************************************
+ * Statistical test logic not compiled for regular operation
+ ***************************************************************************/
+
+#ifdef CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT
+/*
+ * Statistical tests: invoke the entropy collector and sample time results
+ * for it, the random data is never returned - every call to this function
+ * generates one random number.
+ * This function is only meant for statistical analysis purposes and not
+ * for general use
+ */
+void jent_gen_entropy_stat(struct rand_data *entropy_collector,
+                           struct entropy_stat *stat)
+{
+        /* caller is allowed to set the entropy collection loop to a fixed
+         * value -- we still call shuffle for the time measurements */
+        jent_init_statistic(entropy_collector);
+        jent_gen_entropy(entropy_collector);
+        jent_calc_statistic(entropy_collector, stat, DATA_SIZE_BITS);
+}
+
+/*
+ * Statistical test: obtain the distribution of the LFSR state value from
+ * jent_lfsr_time
+ */
+void jent_lfsr_time_stat(struct rand_data *ec, __u64 *fold, __u64 *loop_cnt)
+{
+        __u64 time = 0;
+        __u64 time2 = 0;
+        jent_get_nstime(&time);
+        jent_memaccess(ec, 0);
+        /* implement the priming logic */
+        jent_lfsr_time(ec, time, 0);
+        jent_get_nstime(&time2);
+        time2 = time2 - time;
+        *loop_cnt = jent_lfsr_time(ec, time2, 0);
+        *fold = ec->data;
+}
+
+/*
+ * Statistical test: return the time duration for the folding operation. If min
+ * is set, perform the given number of foldings. Otherwise, allow the
+ * loop count shuffling to define the number of foldings.
+ */
+__u64 jent_lfsr_var_stat(struct rand_data *ec, unsigned int min)
+{
+        __u64 time = 0;
+        __u64 time2 = 0;
+
+        jent_get_nstime(&time);
+        jent_memaccess(ec, min);
+        jent_lfsr_time(ec, time, min);
+        jent_get_nstime(&time2);
+        return ((time2 - time));
+}
+#endif /* CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT */
diff --git a/random/jitterentropy.h b/random/jitterentropy.h
new file mode 100644
index 0000000..8012419
--- /dev/null
+++ b/random/jitterentropy.h
@@ -0,0 +1,172 @@
+/*
+ * Non-physical true random number generator based on timing jitter.
+ *
+ * Copyright Stephan Mueller <smueller at chronox.de>, 2014
+ *
+ * License
+ * =======
+ *
+ * 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, and the entire permission notice in its entirety,
+ *    including the disclaimer of warranties.
+ * 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. The name of the author may not be used to endorse or promote
+ *    products derived from this software without specific prior
+ *    written permission.
+ *
+ * ALTERNATIVELY, this product may be distributed under the terms of
+ * the GNU General Public License, in which case the provisions of the GPL are
+ * required INSTEAD OF the above restrictions.  (This clause is
+ * necessary due to a potential bad interaction between the GPL and
+ * the restrictions contained in a BSD-style copyright.)
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
+ * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR 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 NOT ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+#ifndef _JITTERENTROPY_H
+#define _JITTERENTROPY_H
+
+#ifdef __KERNEL__
+#include "jitterentropy-base-kernel.h"
+#else
+#include "jitterentropy-base-user.h"
+#endif /* __KERNEL__ */
+
+/* Statistical data from the entropy source */
+struct entropy_stat {
+        unsigned int bitslot[64];	/* Counter for the bits set per bit
+                                           position in ->data */
+        unsigned int bitvar[64];	/* Counter for the number of bit
+                                           variations per bit position in
+                                           ->data */
+        unsigned int enable_bit_test;   /* enable bit test
+                                           this flag is vital for the accuracy
+                                           of the statistic tests: when we
+                                           do the time measurements, we want
+                                           the observed entropy collection
+                                           loop executed as fast as the
+                                           unmeasured loop, i.e. without
+                                           the bit statistic logic; on the
+                                           other hand, the bit statistics
+                                           test is not interested in exact
+                                           timing */
+        __u64 collection_begin;		/* timer for beginning of one
+                                           entropy collection round */
+        __u64 collection_end;		/* timer for end of one round */
+        __u64 old_delta;		/* Time delta of previous round to
+                                           calculate delta of deltas */
+        unsigned int setbits;		/* see _jent_calc_statistic */
+        unsigned int varbits;		/* see _jent_calc_statistic */
+        unsigned int obsbits;		/* see _jent_calc_statistic */
+        unsigned int collection_loop_cnt;	/* Collection loop counter */
+};
+
+/* The entropy pool */
+struct rand_data
+{
+        /* all data values that are vital to maintain the security
+         * of the RNG are marked as SENSITIVE. A user must not
+         * access that information while the RNG executes its loops to
+         * calculate the next random value. */
+        __u64 data;		/* SENSITIVE Actual random number */
+        __u64 prev_time;	/* SENSITIVE Previous time stamp */
+#define DATA_SIZE_BITS ((sizeof(__u64)) * 8)
+        __u64 last_delta;	/* SENSITIVE stuck test */
+        __s64 last_delta2;	/* SENSITIVE stuck test */
+        unsigned int osr;	/* Oversample rate */
+        unsigned int stir:1;		/* Post-processing stirring */
+        unsigned int disable_unbias:1;	/* Deactivate Von-Neuman unbias */
+#define JENT_MEMORY_BLOCKS 64
+#define JENT_MEMORY_BLOCKSIZE 32
+#define JENT_MEMORY_ACCESSLOOPS 128
+#define JENT_MEMORY_SIZE (JENT_MEMORY_BLOCKS*JENT_MEMORY_BLOCKSIZE)
+        unsigned char *mem;	/* Memory access location with size of
+                                 * memblocks * memblocksize */
+        unsigned int memlocation; /* Pointer to byte in *mem */
+        unsigned int memblocks;	/* Number of memory blocks in *mem */
+        unsigned int memblocksize; /* Size of one memory block in bytes */
+        unsigned int memaccessloops; /* Number of memory accesses per random
+                                      * bit generation */
+#ifdef CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT
+        struct entropy_stat entropy_stat;
+#endif
+};
+
+/* Flags that can be used to initialize the RNG */
+#define JENT_DISABLE_STIR (1<<0) /* Disable stirring the entropy pool */
+#define JENT_DISABLE_UNBIAS (1<<1) /* Disable the Von-Neuman Unbiaser */
+#define JENT_DISABLE_MEMORY_ACCESS (1<<2) /* Disable memory access for more
+                                             entropy, saves MEMORY_SIZE RAM for
+                                             entropy collector */
+
+#define DRIVER_NAME     "jitterentropy"
+
+/* -- BEGIN Main interface functions -- */
+
+/* Number of low bits of the time value that we want to consider */
+/* get raw entropy */
+ssize_t jent_read_entropy(struct rand_data *ec, char *data, size_t len);
+/* initialize an instance of the entropy collector */
+struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
+                                               unsigned int flags);
+/* clearing of entropy collector */
+void jent_entropy_collector_free(struct rand_data *entropy_collector);
+
+/* initialization of entropy collector */
+int jent_entropy_init(void);
+
+/* return version number of core library */
+unsigned int jent_version(void);
+
+/* -- END of Main interface functions -- */
+
+/* -- BEGIN error codes for init function -- */
+#define ENOTIME         1 /* Timer service not available */
+#define ECOARSETIME	2 /* Timer too coarse for RNG */
+#define ENOMONOTONIC	3 /* Timer is not monotonic increasing */
+#define EMINVARIATION	4 /* Timer variations too small for RNG */
+#define EVARVAR		5 /* Timer does not produce variations of variations
+                             (2nd derivation of time is zero) */
+#define EMINVARVAR	6 /* Timer variations of variations is too small */
+#define EPROGERR	7 /* Programming error */
+
+/* -- BEGIN statistical test functions only complied with CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT -- */
+
+void _jent_init_statistic(struct rand_data *entropy_collector);
+void _jent_calc_statistic(struct rand_data *entropy_collector,
+                          struct entropy_stat *stat, unsigned int loop_cnt);
+void _jent_bit_count(struct rand_data *entropy_collector, __u64 prev_data);
+
+#ifdef CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT
+#define jent_init_statistic(x)    do { _jent_init_statistic(x); }    while (0)
+#define jent_calc_statistic(x, y, z) do { _jent_calc_statistic(x, y, z); } while (0)
+#define jent_bit_count(x,y)       do { _jent_bit_count(x, y); }      while (0)
+void jent_gen_entropy_stat(struct rand_data *entropy_collector,
+                           struct entropy_stat *stat);
+void jent_lfsr_time_stat(struct rand_data *ec, __u64 *fold, __u64 *loop_cnt);
+__u64 jent_lfsr_var_stat(struct rand_data *ec, unsigned int min);
+#else /* CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT */
+#define jent_init_statistic(x)    do { if(0) _jent_init_statistic(x); }    while (0)
+#define jent_calc_statistic(x, y, z) do { if(0) _jent_calc_statistic(x, y, z); } while (0)
+#define jent_bit_count(x,y)       do { if(0) _jent_bit_count(x,y); }       while (0)
+#endif /* CONFIG_CRYPTO_CPU_JITTERENTROPY_STAT */
+
+/* -- END of statistical test function -- */
+
+#endif /* _JITTERENTROPY_H */

-----------------------------------------------------------------------

Summary of changes:
 AUTHORS                          |   3 +-
 LICENSES                         |  45 +++
 cipher/bithelp.h                 |   5 +
 configure.ac                     |  14 +
 doc/gcrypt.texi                  |   1 +
 random/Makefile.am               |  20 +-
 random/jitterentropy-base-user.h | 103 +++++
 random/jitterentropy-base.c      | 786 +++++++++++++++++++++++++++++++++++++++
 random/jitterentropy.h           | 177 +++++++++
 random/rand-internal.h           |   8 +-
 random/rndjent.c                 | 208 +++++++++++
 random/rndlinux.c                |  15 +-
 random/rndw32.c                  |  12 +-
 src/g10lib.h                     |   3 +
 src/hwf-x86.c                    |  10 +-
 src/hwfeatures.c                 |   1 +
 16 files changed, 1402 insertions(+), 9 deletions(-)
 create mode 100644 random/jitterentropy-base-user.h
 create mode 100644 random/jitterentropy-base.c
 create mode 100644 random/jitterentropy.h
 create mode 100644 random/rndjent.c


hooks/post-receive
-- 
The GNU crypto library
http://git.gnupg.org


_______________________________________________
Gnupg-commits mailing list
Gnupg-commits at gnupg.org
http://lists.gnupg.org/mailman/listinfo/gnupg-commits


More information about the Gcrypt-devel mailing list