gentleknife / rpms / libgcrypt

Forked from rpms/libgcrypt 4 years ago
Clone

Blame SOURCES/libgcrypt-1.5.3-drbg.patch

241384
diff -up libgcrypt-1.5.3/random/drbg.c.drbg libgcrypt-1.5.3/random/drbg.c
241384
--- libgcrypt-1.5.3/random/drbg.c.drbg	2014-09-26 17:00:16.050215868 +0200
241384
+++ libgcrypt-1.5.3/random/drbg.c	2014-09-26 17:15:41.576110018 +0200
241384
@@ -0,0 +1,2352 @@
241384
+/*
241384
+ * DRBG: Deterministic Random Bits Generator
241384
+ *       Based on NIST Recommended DRBG from NIST SP800-90A with the following
241384
+ *       properties:
241384
+ *		* CTR DRBG with DF with AES-128, AES-192, AES-256 cores
241384
+ * 		* Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
241384
+ * 		* HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
241384
+ * 		* with and without prediction resistance
241384
+ *
241384
+ * Copyright Stephan Mueller <smueller@chronox.de>, 2014
241384
+ *
241384
+ * Redistribution and use in source and binary forms, with or without
241384
+ * modification, are permitted provided that the following conditions
241384
+ * are met:
241384
+ * 1. Redistributions of source code must retain the above copyright
241384
+ *    notice, and the entire permission notice in its entirety,
241384
+ *    including the disclaimer of warranties.
241384
+ * 2. Redistributions in binary form must reproduce the above copyright
241384
+ *    notice, this list of conditions and the following disclaimer in the
241384
+ *    documentation and/or other materials provided with the distribution.
241384
+ * 3. The name of the author may not be used to endorse or promote
241384
+ *    products derived from this software without specific prior
241384
+ *    written permission.
241384
+ *
241384
+ * ALTERNATIVELY, this product may be distributed under the terms of
241384
+ * LGPLv2+, in which case the provisions of the LGPL are
241384
+ * required INSTEAD OF the above restrictions.  (This clause is
241384
+ * necessary due to a potential bad interaction between the LGPL and
241384
+ * the restrictions contained in a BSD-style copyright.)
241384
+ *
241384
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
241384
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241384
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
241384
+ * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
241384
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241384
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
241384
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
241384
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
241384
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241384
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
241384
+ * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
241384
+ * DAMAGE.
241384
+ *
241384
+ *
241384
+ * gcry_control GCRYCTL_DRBG_REINIT
241384
+ * ================================
241384
+ * This control request re-initializes the DRBG completely, i.e. the entire
241384
+ * state of the DRBG is zeroized (with two exceptions listed in
241384
+ * GCRYCTL_DRBG_SET_ENTROPY).
241384
+ *
241384
+ * The control request takes the following values which influences how the DRBG
241384
+ * is re-initialized:
241384
+ *   * u32 flags: This variable specifies the DRBG type to be used for the
241384
+ *		    next initialization. If set to 0, the previous DRBG type is
241384
+ * 		    used for the initialization. The DRBG type is an OR of the
241384
+ * 		    mandatory flags of the requested DRBG strength and DRBG
241384
+ * 		    cipher type. Optionally, the prediction resistance flag
241384
+ * 		    can be ORed into the flags variable. For example:
241384
+ * 		    - CTR-DRBG with AES-128 without prediction resistance:
241384
+ * 			DRBG_CTRAES128
241384
+ * 		    - HMAC-DRBG with SHA-512 with prediction resistance:
241384
+ * 			DRBG_HMACSHA512 | DRBG_PREDICTION_RESIST
241384
+ *   * struct gcry_drbg_string *pers: personalization string to be used for
241384
+ *			   	 initialization.
241384
+ * The variable of flags is independent from the pers/perslen variables. If
241384
+ * flags is set to 0 and perslen is set to 0, the current DRBG type is
241384
+ * completely reset without using a personalization string.
241384
+ *
241384
+ * DRBG Usage
241384
+ * ==========
241384
+ * The SP 800-90A DRBG allows the user to specify a personalization string
241384
+ * for initialization as well as an additional information string for each
241384
+ * random number request. The following code fragments show how a caller
241384
+ * uses the kernel crypto API to use the full functionality of the DRBG.
241384
+ *
241384
+ * Usage without any additional data
241384
+ * ---------------------------------
241384
+ * gcry_randomize(outbuf, OUTLEN, GCRY_STRONG_RANDOM);
241384
+ *
241384
+ *
241384
+ * Usage with personalization string during initialization
241384
+ * -------------------------------------------------------
241384
+ * struct gcry_drbg_string pers;
241384
+ * char personalization[11] = "some-string";
241384
+ *
241384
+ * gcry_drbg_string_fill(&pers, personalization, strlen(personalization));
241384
+ * // The reset completely re-initializes the DRBG with the provided
241384
+ * // personalization string without changing the DRBG type
241384
+ * ret = gcry_control(GCRYCTL_DRBG_REINIT, 0, &pers;;
241384
+ * gcry_randomize(outbuf, OUTLEN, GCRY_STRONG_RANDOM);
241384
+ *
241384
+ *
241384
+ * Usage with additional information string during random number request
241384
+ * ---------------------------------------------------------------------
241384
+ * struct gcry_drbg_string addtl;
241384
+ * char addtl_string[11] = "some-string";
241384
+ *
241384
+ * gcry_drbg_string_fill(&addtl, addtl_string, strlen(addtl_string));
241384
+ * // The following call is a wrapper to gcry_randomize() and returns
241384
+ * // the same error codes.
241384
+ * gcry_randomize_drbg(outbuf, OUTLEN, GCRY_STRONG_RANDOM, &addtl);
241384
+ *
241384
+ *
241384
+ * Usage with personalization and additional information strings
241384
+ * -------------------------------------------------------------
241384
+ * Just mix both scenarios above.
241384
+ *
241384
+ *
241384
+ * Switch the DRBG type to some other type
241384
+ * ---------------------------------------
241384
+ * // Switch to CTR DRBG AES-128 without prediction resistance
241384
+ * ret = gcry_control(GCRYCTL_DRBG_REINIT, DRBG_NOPR_CTRAES128, NULL);
241384
+ * gcry_randomize(outbuf, OUTLEN, GCRY_STRONG_RANDOM);
241384
+ */
241384
+
241384
+#include <sys/types.h>
241384
+#include <asm/types.h>
241384
+#include <string.h>
241384
+#include <unistd.h>
241384
+#include <stdint.h>
241384
+
241384
+#include <config.h>
241384
+
241384
+#include "g10lib.h"
241384
+#include "random.h"
241384
+#include "rand-internal.h"
241384
+#include "../cipher/bithelp.h"
241384
+#include "ath.h"
241384
+
241384
+/******************************************************************
241384
+ * Common data structures
241384
+ ******************************************************************/
241384
+
241384
+struct gcry_drbg_state;
241384
+
241384
+struct gcry_drbg_core
241384
+{
241384
+  u32 flags;			/* flags for the cipher */
241384
+  ushort statelen;		/* maximum state length */
241384
+  ushort blocklen_bytes;	/* block size of output in bytes */
241384
+  int backend_cipher;		/* libgcrypt backend cipher */
241384
+};
241384
+
241384
+struct gcry_drbg_state_ops
241384
+{
241384
+  gpg_err_code_t (*update) (struct gcry_drbg_state * drbg,
241384
+			    struct gcry_drbg_string * seed, int reseed);
241384
+  gpg_err_code_t (*generate) (struct gcry_drbg_state * drbg,
241384
+			      unsigned char *buf, unsigned int buflen,
241384
+			      struct gcry_drbg_string * addtl);
241384
+};
241384
+
241384
+/* DRBG test data */
241384
+struct gcry_drbg_test_data
241384
+{
241384
+  struct gcry_drbg_string *testentropy;	/* TEST PARAMETER: test entropy */
241384
+  int fail_seed_source:1;	/* if set, the seed function will return an error */
241384
+};
241384
+
241384
+
241384
+struct gcry_drbg_state
241384
+{
241384
+  unsigned char *V;		/* internal state 10.1.1.1 1a) */
241384
+  unsigned char *C;		/* hash: static value 10.1.1.1 1b)
241384
+				 * hmac / ctr: key */
241384
+  size_t reseed_ctr;		/* Number of RNG requests since last reseed --
241384
+				 * 10.1.1.1 1c) */
241384
+  unsigned char *scratchpad;	/* some memory the DRBG can use for its
241384
+				 * operation -- allocated during init */
241384
+  int seeded:1;			/* DRBG fully seeded? */
241384
+  int pr:1;			/* Prediction resistance enabled? */
241384
+  int fips_primed:1;		/* Continuous test primed? */
241384
+  unsigned char *prev;		/* previous output value of gcry_drbg_blocklen
241384
+				 * for FIPS 140-2 continuous test */
241384
+  /* Taken from libgcrypt ANSI X9.31 DRNG: We need to keep track of the
241384
+   * process which did the initialization so that we can detect a fork.
241384
+   * The volatile modifier is required so that the compiler does not
241384
+   * optimize it away in case the getpid function is badly attributed. */
241384
+  pid_t seed_init_pid;
241384
+  const struct gcry_drbg_state_ops *d_ops;
241384
+  const struct gcry_drbg_core *core;
241384
+  struct gcry_drbg_test_data *test_data;
241384
+};
241384
+
241384
+enum gcry_drbg_prefixes
241384
+{
241384
+  DRBG_PREFIX0 = 0x00,
241384
+  DRBG_PREFIX1,
241384
+  DRBG_PREFIX2,
241384
+  DRBG_PREFIX3
241384
+};
241384
+
241384
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
241384
+
241384
+/***************************************************************
241384
+ * Backend cipher definitions available to DRBG
241384
+ ***************************************************************/
241384
+
241384
+static const struct gcry_drbg_core gcry_drbg_cores[] = {
241384
+  /* Hash DRBGs */
241384
+  {GCRY_DRBG_HASHSHA1, 55, 20, GCRY_MD_SHA1},
241384
+  {GCRY_DRBG_HASHSHA256, 55, 32, GCRY_MD_SHA256},
241384
+  {GCRY_DRBG_HASHSHA384, 111, 48, GCRY_MD_SHA384},
241384
+  {GCRY_DRBG_HASHSHA512, 111, 64, GCRY_MD_SHA512},
241384
+  /* HMAC DRBGs */
241384
+  {GCRY_DRBG_HASHSHA1 | GCRY_DRBG_HMAC, 20, 20, GCRY_MD_SHA1},
241384
+  {GCRY_DRBG_HASHSHA256 | GCRY_DRBG_HMAC, 32, 32, GCRY_MD_SHA256},
241384
+  {GCRY_DRBG_HASHSHA384 | GCRY_DRBG_HMAC, 48, 48, GCRY_MD_SHA384},
241384
+  {GCRY_DRBG_HASHSHA512 | GCRY_DRBG_HMAC, 64, 64, GCRY_MD_SHA512},
241384
+  /* block ciphers */
241384
+  {GCRY_DRBG_CTRAES | GCRY_DRBG_SYM128, 32, 16, GCRY_CIPHER_AES128},
241384
+  {GCRY_DRBG_CTRAES | GCRY_DRBG_SYM192, 40, 16, GCRY_CIPHER_AES192},
241384
+  {GCRY_DRBG_CTRAES | GCRY_DRBG_SYM256, 48, 16, GCRY_CIPHER_AES256},
241384
+};
241384
+
241384
+static gpg_err_code_t gcry_drbg_sym (struct gcry_drbg_state *drbg,
241384
+				     const unsigned char *key,
241384
+				     unsigned char *outval,
241384
+				     const struct gcry_drbg_string *buf);
241384
+static gpg_err_code_t gcry_drbg_hmac (struct gcry_drbg_state *drbg,
241384
+				      const unsigned char *key,
241384
+				      unsigned char *outval,
241384
+				      const struct gcry_drbg_string *buf);
241384
+
241384
+/******************************************************************
241384
+ ******************************************************************
241384
+ ******************************************************************
241384
+ * Generic DRBG code
241384
+ ******************************************************************
241384
+ ******************************************************************
241384
+ ******************************************************************/
241384
+
241384
+/******************************************************************
241384
+ * Generic helper functions
241384
+ ******************************************************************/
241384
+
241384
+/* Byte swap for 32-bit and 64-bit integers. */
241384
+static inline u32
241384
+_gcry_bswap32(u32 x)
241384
+{
241384
+	return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L));
241384
+}
241384
+
241384
+#ifdef HAVE_U64_TYPEDEF
241384
+static inline u64
241384
+_gcry_bswap64(u64 x)
241384
+{
241384
+	return ((u64)_gcry_bswap32(x) << 32) | (_gcry_bswap32(x >> 32));
241384
+}
241384
+#endif
241384
+
241384
+/* Endian dependent byte swap operations.  */
241384
+#ifdef WORDS_BIGENDIAN
241384
+# define le_bswap32(x) _gcry_bswap32(x)
241384
+# define be_bswap32(x) ((u32)(x))
241384
+# ifdef HAVE_U64_TYPEDEF
241384
+#  define le_bswap64(x) _gcry_bswap64(x)
241384
+#  define be_bswap64(x) ((u64)(x))
241384
+# endif
241384
+#else
241384
+# define le_bswap32(x) ((u32)(x))
241384
+# define be_bswap32(x) _gcry_bswap32(x)
241384
+# ifdef HAVE_U64_TYPEDEF
241384
+#  define le_bswap64(x) ((u64)(x))
241384
+#  define be_bswap64(x) _gcry_bswap64(x)
241384
+# endif
241384
+#endif
241384
+
241384
+#define xcalloc_secure(a,b) gcry_xcalloc_secure((a),(b))
241384
+#define xfree(a) _gcry_free(a)
241384
+
241384
+#if 0
241384
+#define dbg(x) do { log_debug x; } while(0)
241384
+#else
241384
+#define dbg(x)
241384
+#endif
241384
+
241384
+static inline ushort
241384
+gcry_drbg_statelen (struct gcry_drbg_state *drbg)
241384
+{
241384
+  if (drbg && drbg->core)
241384
+    return drbg->core->statelen;
241384
+  return 0;
241384
+}
241384
+
241384
+static inline ushort
241384
+gcry_drbg_blocklen (struct gcry_drbg_state *drbg)
241384
+{
241384
+  if (drbg && drbg->core)
241384
+    return drbg->core->blocklen_bytes;
241384
+  return 0;
241384
+}
241384
+
241384
+static inline ushort
241384
+gcry_drbg_keylen (struct gcry_drbg_state *drbg)
241384
+{
241384
+  if (drbg && drbg->core)
241384
+    return (drbg->core->statelen - drbg->core->blocklen_bytes);
241384
+  return 0;
241384
+}
241384
+
241384
+static inline size_t
241384
+gcry_drbg_max_request_bytes (void)
241384
+{
241384
+  /* SP800-90A requires the limit 2**19 bits, but we return bytes */
241384
+  return (1 << 16);
241384
+}
241384
+
241384
+static inline size_t
241384
+gcry_drbg_max_addtl (void)
241384
+{
241384
+  /* SP800-90A requires 2**35 bytes additional info str / pers str */
241384
+#ifdef __LP64__
241384
+  return (1UL << 35);
241384
+#else
241384
+  /*
241384
+   * SP800-90A allows smaller maximum numbers to be returned -- we
241384
+   * return SIZE_MAX - 1 to allow the verification of the enforcement
241384
+   * of this value in gcry_drbg_healthcheck_sanity.
241384
+   */
241384
+  return (SIZE_MAX - 1);
241384
+#endif
241384
+}
241384
+
241384
+static inline size_t
241384
+gcry_drbg_max_requests (void)
241384
+{
241384
+  /* SP800-90A requires 2**48 maximum requests before reseeding */
241384
+#ifdef __LP64__
241384
+  return (1UL << 48);
241384
+#else
241384
+  return SIZE_MAX;
241384
+#endif
241384
+}
241384
+
241384
+/*
241384
+ * Return strength of DRBG according to SP800-90A section 8.4
241384
+ *
241384
+ * flags: DRBG flags reference
241384
+ *
241384
+ * Return: normalized strength value or 32 as a default to counter
241384
+ * 	   programming errors
241384
+ */
241384
+static inline unsigned short
241384
+gcry_drbg_sec_strength (u32 flags)
241384
+{
241384
+  if ((flags & GCRY_DRBG_HASHSHA1) || (flags & GCRY_DRBG_SYM128))
241384
+    return 16;
241384
+  else if (flags & GCRY_DRBG_SYM192)
241384
+    return 24;
241384
+  else if ((flags & GCRY_DRBG_SYM256) || (flags & GCRY_DRBG_HASHSHA256) ||
241384
+	   (flags & GCRY_DRBG_HASHSHA384) || (flags & GCRY_DRBG_HASHSHA512))
241384
+    return 32;
241384
+  else
241384
+    return 32;
241384
+}
241384
+
241384
+/*
241384
+ * FIPS 140-2 continuous self test
241384
+ * The test is performed on the result of one round of the output
241384
+ * function. Thus, the function implicitly knows the size of the
241384
+ * buffer.
241384
+ *
241384
+ * @drbg DRBG handle
241384
+ * @buf output buffer of random data to be checked
241384
+ *
241384
+ * return:
241384
+ *	false on error
241384
+ * 	true on success
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_fips_continuous_test (struct gcry_drbg_state *drbg,
241384
+				const unsigned char *buf)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  /* skip test if we test the overall system */
241384
+  if (drbg->test_data)
241384
+    return 1;
241384
+  /* only perform test in FIPS mode */
241384
+  if (0 == fips_mode ())
241384
+    return 1;
241384
+  if (!drbg->fips_primed)
241384
+    {
241384
+      /* Priming of FIPS test */
241384
+      memcpy (drbg->prev, buf, gcry_drbg_blocklen (drbg));
241384
+      drbg->fips_primed = 1;
241384
+      /* return false due to priming, i.e. another round is needed */
241384
+      return 0;
241384
+    }
241384
+  ret = memcmp (drbg->prev, buf, gcry_drbg_blocklen (drbg));
241384
+  memcpy (drbg->prev, buf, gcry_drbg_blocklen (drbg));
241384
+  /* the test shall pass when the two compared values are not equal */
241384
+  return ret != 0;
241384
+}
241384
+
241384
+/*
241384
+ * Convert an integer into a byte representation of this integer.
241384
+ * The byte representation is big-endian
241384
+ *
241384
+ * @val value to be converted
241384
+ * @buf buffer holding the converted integer -- caller must ensure that
241384
+ *      buffer size is at least 32 bit
241384
+ */
241384
+static inline void
241384
+gcry_drbg_cpu_to_be32 (u32 val, unsigned char *buf)
241384
+{
241384
+  struct s
241384
+  {
241384
+    u32 conv;
241384
+  };
241384
+  struct s *conversion = (struct s *) buf;
241384
+
241384
+  conversion->conv = be_bswap32 (val);
241384
+}
241384
+
241384
+static void
241384
+gcry_drbg_add_buf (unsigned char *dst, size_t dstlen,
241384
+		   unsigned char *add, size_t addlen)
241384
+{
241384
+  /* implied: dstlen > addlen */
241384
+  unsigned char *dstptr, *addptr;
241384
+  unsigned int remainder = 0;
241384
+  size_t len = addlen;
241384
+
241384
+  dstptr = dst + (dstlen - 1);
241384
+  addptr = add + (addlen - 1);
241384
+  while (len)
241384
+    {
241384
+      remainder += *dstptr + *addptr;
241384
+      *dstptr = remainder & 0xff;
241384
+      remainder >>= 8;
241384
+      len--;
241384
+      dstptr--;
241384
+      addptr--;
241384
+    }
241384
+  len = dstlen - addlen;
241384
+  while (len && remainder > 0)
241384
+    {
241384
+      remainder = *dstptr + 1;
241384
+      *dstptr = remainder & 0xff;
241384
+      remainder >>= 8;
241384
+      len--;
241384
+      dstptr--;
241384
+    }
241384
+}
241384
+
241384
+/* Helper variables for read_cb().
241384
+ *
241384
+ *   The _gcry_rnd*_gather_random interface does not allow to provide a
241384
+ *   data pointer.  Thus we need to use a global variable for
241384
+ *   communication.  However, the then required locking is anyway a good
241384
+ *   idea because it does not make sense to have several readers of (say
241384
+ *   /dev/random).  It is easier to serve them one after the other.  */
241384
+static unsigned char *read_cb_buffer;	/* The buffer.  */
241384
+static size_t read_cb_size;	/* Size of the buffer.  */
241384
+static size_t read_cb_len;	/* Used length.  */
241384
+
241384
+/* Callback for generating seed from kernel device. */
241384
+static void
241384
+gcry_drbg_read_cb (const void *buffer, size_t length,
241384
+		   enum random_origins origin)
241384
+{
241384
+  const unsigned char *p = buffer;
241384
+
241384
+  (void) origin;
241384
+  gcry_assert (read_cb_buffer);
241384
+
241384
+  /* Note that we need to protect against gatherers returning more
241384
+   * than the requested bytes (e.g. rndw32).  */
241384
+  while (length-- && read_cb_len < read_cb_size)
241384
+    read_cb_buffer[read_cb_len++] = *p++;
241384
+}
241384
+
241384
+static inline int
241384
+gcry_drbg_get_entropy (struct gcry_drbg_state *drbg, unsigned char *buffer,
241384
+		       size_t len)
241384
+{
241384
+  int rc = 0;
241384
+
241384
+  /* Perform testing as defined in 11.3.2 */
241384
+  if (drbg->test_data && drbg->test_data->fail_seed_source)
241384
+    return -1;
241384
+
241384
+  read_cb_buffer = buffer;
241384
+  read_cb_size = len;
241384
+  read_cb_len = 0;
241384
+#if USE_RNDLINUX
241384
+  rc = _gcry_rndlinux_gather_random (gcry_drbg_read_cb, 0, len,
241384
+				     GCRY_VERY_STRONG_RANDOM);
241384
+#elif USE_RNDUNIX
241384
+  rc = _gcry_rndunix_gather_random (read_cb, 0, length,
241384
+				    GCRY_VERY_STRONG_RANDOM);
241384
+#elif USE_RNDW32
241384
+  do
241384
+    {
241384
+      rc = _gcry_rndw32_gather_random (read_cb, 0, length,
241384
+				       GCRY_VERY_STRONG_RANDOM);
241384
+    }
241384
+  while (rc >= 0 && read_cb_len < read_cb_size);
241384
+#else
241384
+  rc = -1;
241384
+#endif
241384
+  return rc;
241384
+}
241384
+
241384
+/******************************************************************
241384
+ * CTR DRBG callback functions
241384
+ ******************************************************************/
241384
+
241384
+/* BCC function for CTR DRBG as defined in 10.4.3 */
241384
+static gpg_err_code_t
241384
+gcry_drbg_ctr_bcc (struct gcry_drbg_state *drbg,
241384
+		   unsigned char *out, const unsigned char *key,
241384
+		   struct gcry_drbg_string *in)
241384
+{
241384
+  gpg_err_code_t ret = GPG_ERR_GENERAL;
241384
+  struct gcry_drbg_string *curr = in;
241384
+  size_t inpos = curr->len;
241384
+  const unsigned char *pos = curr->buf;
241384
+  struct gcry_drbg_string data;
241384
+
241384
+  gcry_drbg_string_fill (&data, out, gcry_drbg_blocklen (drbg));
241384
+
241384
+  /* 10.4.3 step 1 */
241384
+  memset (out, 0, gcry_drbg_blocklen (drbg));
241384
+
241384
+  /* 10.4.3 step 2 / 4 */
241384
+  while (inpos)
241384
+    {
241384
+      short cnt = 0;
241384
+      /* 10.4.3 step 4.1 */
241384
+      for (cnt = 0; cnt < gcry_drbg_blocklen (drbg); cnt++)
241384
+	{
241384
+	  out[cnt] ^= *pos;
241384
+	  pos++;
241384
+	  inpos--;
241384
+	  /* the following branch implements the linked list
241384
+	   * iteration. If we are at the end of the current data
241384
+	   * set, we have to start using the next data set if
241384
+	   * available -- the inpos value always points to the
241384
+	   * current byte and will be zero if we have processed
241384
+	   * the last byte of the last linked list member */
241384
+	  if (0 == inpos)
241384
+	    {
241384
+	      curr = curr->next;
241384
+	      if (NULL != curr)
241384
+		{
241384
+		  pos = curr->buf;
241384
+		  inpos = curr->len;
241384
+		}
241384
+	      else
241384
+		{
241384
+		  inpos = 0;
241384
+		  break;
241384
+		}
241384
+	    }
241384
+	}
241384
+      /* 10.4.3 step 4.2 */
241384
+      ret = gcry_drbg_sym (drbg, key, out, &data);
241384
+      if (ret)
241384
+	return ret;
241384
+      /* 10.4.3 step 2 */
241384
+    }
241384
+  return 0;
241384
+}
241384
+
241384
+
241384
+/*
241384
+ * scratchpad usage: gcry_drbg_ctr_update is interlinked with gcry_drbg_ctr_df
241384
+ * (and gcry_drbg_ctr_bcc, but this function does not need any temporary buffers),
241384
+ * the scratchpad is used as follows:
241384
+ * gcry_drbg_ctr_update:
241384
+ *	temp
241384
+ *		start: drbg->scratchpad
241384
+ *		length: gcry_drbg_statelen(drbg) + gcry_drbg_blocklen(drbg)
241384
+ *			note: the cipher writing into this variable works
241384
+ *			blocklen-wise. Now, when the statelen is not a multiple
241384
+ *			of blocklen, the generateion loop below "spills over"
241384
+ *			by at most blocklen. Thus, we need to give sufficient
241384
+ *			memory.
241384
+ *	df_data
241384
+ *		start: drbg->scratchpad +
241384
+ *				gcry_drbg_statelen(drbg) +
241384
+ *				gcry_drbg_blocklen(drbg)
241384
+ *		length: gcry_drbg_statelen(drbg)
241384
+ *
241384
+ * gcry_drbg_ctr_df:
241384
+ *	pad
241384
+ *		start: df_data + gcry_drbg_statelen(drbg)
241384
+ *		length: gcry_drbg_blocklen(drbg)
241384
+ *	iv
241384
+ *		start: pad + gcry_drbg_blocklen(drbg)
241384
+ *		length: gcry_drbg_blocklen(drbg)
241384
+ *	temp
241384
+ *		start: iv + gcry_drbg_blocklen(drbg)
241384
+ *		length: gcry_drbg_satelen(drbg) + gcry_drbg_blocklen(drbg)
241384
+ *			note: temp is the buffer that the BCC function operates
241384
+ *			on. BCC operates blockwise. gcry_drbg_statelen(drbg)
241384
+ *			is sufficient when the DRBG state length is a multiple
241384
+ *			of the block size. For AES192 (and maybe other ciphers)
241384
+ *			this is not correct and the length for temp is
241384
+ *			insufficient (yes, that also means for such ciphers,
241384
+ *			the final output of all BCC rounds are truncated).
241384
+ *			Therefore, add gcry_drbg_blocklen(drbg) to cover all
241384
+ *			possibilities.
241384
+ */
241384
+
241384
+/* Derivation Function for CTR DRBG as defined in 10.4.2 */
241384
+static gpg_err_code_t
241384
+gcry_drbg_ctr_df (struct gcry_drbg_state *drbg, unsigned char *df_data,
241384
+		  size_t bytes_to_return, struct gcry_drbg_string *addtl)
241384
+{
241384
+  gpg_err_code_t ret = GPG_ERR_GENERAL;
241384
+  unsigned char L_N[8];
241384
+  /* S3 is input */
241384
+  struct gcry_drbg_string S1, S2, S4, cipherin;
241384
+  struct gcry_drbg_string *tempstr = addtl;
241384
+  unsigned char *pad = df_data + gcry_drbg_statelen (drbg);
241384
+  unsigned char *iv = pad + gcry_drbg_blocklen (drbg);
241384
+  unsigned char *temp = iv + gcry_drbg_blocklen (drbg);
241384
+  size_t padlen = 0;
241384
+  unsigned int templen = 0;
241384
+  /* 10.4.2 step 7 */
241384
+  unsigned int i = 0;
241384
+  /* 10.4.2 step 8 */
241384
+  const unsigned char *K = (unsigned char *)
241384
+    "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
241384
+    "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
241384
+  unsigned char *X;
241384
+  size_t generated_len = 0;
241384
+  size_t inputlen = 0;
241384
+
241384
+  memset (pad, 0, gcry_drbg_blocklen (drbg));
241384
+  memset (iv, 0, gcry_drbg_blocklen (drbg));
241384
+  memset (temp, 0, gcry_drbg_statelen (drbg));
241384
+
241384
+  /* 10.4.2 step 1 is implicit as we work byte-wise */
241384
+
241384
+  /* 10.4.2 step 2 */
241384
+  if ((512 / 8) < bytes_to_return)
241384
+    return GPG_ERR_INV_ARG;
241384
+
241384
+  /* 10.4.2 step 2 -- calculate the entire length of all input data */
241384
+  for (; NULL != tempstr; tempstr = tempstr->next)
241384
+    inputlen += tempstr->len;
241384
+  gcry_drbg_cpu_to_be32 (inputlen, &L_N[0]);
241384
+
241384
+  /* 10.4.2 step 3 */
241384
+  gcry_drbg_cpu_to_be32 (bytes_to_return, &L_N[4]);
241384
+
241384
+  /* 10.4.2 step 5: length is size of L_N, input_string, one byte, padding */
241384
+  padlen = (inputlen + sizeof (L_N) + 1) % (gcry_drbg_blocklen (drbg));
241384
+  /* wrap the padlen appropriately */
241384
+  if (padlen)
241384
+    padlen = gcry_drbg_blocklen (drbg) - padlen;
241384
+  /* pad / padlen contains the 0x80 byte and the following zero bytes, so
241384
+   * add one for byte for 0x80 */
241384
+  padlen++;
241384
+  pad[0] = 0x80;
241384
+
241384
+  /* 10.4.2 step 4 -- first fill the linked list and then order it */
241384
+  gcry_drbg_string_fill (&S1, iv, gcry_drbg_blocklen (drbg));
241384
+  gcry_drbg_string_fill (&S2, L_N, sizeof (L_N));
241384
+  gcry_drbg_string_fill (&S4, pad, padlen);
241384
+  S1.next = &S2;
241384
+  S2.next = addtl;
241384
+
241384
+  /* Splice in addtl between S2 and S4 -- we place S4 at the end of the
241384
+   * input data chain. As this code is only triggered when addtl is not
241384
+   * NULL, no NULL checks are necessary.*/
241384
+  tempstr = addtl;
241384
+  while (tempstr->next)
241384
+    tempstr = tempstr->next;
241384
+  tempstr->next = &S4;
241384
+
241384
+  /* 10.4.2 step 9 */
241384
+  while (templen < (gcry_drbg_keylen (drbg) + (gcry_drbg_blocklen (drbg))))
241384
+    {
241384
+      /* 10.4.2 step 9.1 - the padding is implicit as the buffer
241384
+       * holds zeros after allocation -- even the increment of i
241384
+       * is irrelevant as the increment remains within length of i */
241384
+      gcry_drbg_cpu_to_be32 (i, iv);
241384
+      /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
241384
+      ret = gcry_drbg_ctr_bcc (drbg, temp + templen, K, &S1;;
241384
+      if (ret)
241384
+	goto out;
241384
+      /* 10.4.2 step 9.3 */
241384
+      i++;
241384
+      templen += gcry_drbg_blocklen (drbg);
241384
+    }
241384
+
241384
+  /* 10.4.2 step 11 */
241384
+  /* implicit key len with seedlen - blocklen according to table 3 */
241384
+  X = temp + (gcry_drbg_keylen (drbg));
241384
+  gcry_drbg_string_fill (&cipherin, X, gcry_drbg_blocklen (drbg));
241384
+
241384
+  /* 10.4.2 step 12: overwriting of outval */
241384
+
241384
+  /* 10.4.2 step 13 */
241384
+  while (generated_len < bytes_to_return)
241384
+    {
241384
+      short blocklen = 0;
241384
+      /* 10.4.2 step 13.1 */
241384
+      /* the truncation of the key length is implicit as the key
241384
+       * is only gcry_drbg_blocklen in size -- check for the implementation
241384
+       * of the cipher function callback */
241384
+      ret = gcry_drbg_sym (drbg, temp, X, &cipherin);
241384
+      if (ret)
241384
+	goto out;
241384
+      blocklen = (gcry_drbg_blocklen (drbg) <
241384
+		  (bytes_to_return - generated_len)) ?
241384
+	gcry_drbg_blocklen (drbg) : (bytes_to_return - generated_len);
241384
+      /* 10.4.2 step 13.2 and 14 */
241384
+      memcpy (df_data + generated_len, X, blocklen);
241384
+      generated_len += blocklen;
241384
+    }
241384
+
241384
+  ret = 0;
241384
+
241384
+out:
241384
+  memset (iv, 0, gcry_drbg_blocklen (drbg));
241384
+  memset (temp, 0, gcry_drbg_statelen (drbg));
241384
+  memset (pad, 0, gcry_drbg_blocklen (drbg));
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * update function of CTR DRBG as defined in 10.2.1.2
241384
+ *
241384
+ * The reseed variable has an enhanced meaning compared to the update
241384
+ * functions of the other DRBGs as follows:
241384
+ * 0 => initial seed from initialization
241384
+ * 1 => reseed via gcry_drbg_seed
241384
+ * 2 => first invocation from gcry_drbg_ctr_update when addtl is present. In
241384
+ *      this case, the df_data scratchpad is not deleted so that it is
241384
+ *      available for another calls to prevent calling the DF function
241384
+ *      again.
241384
+ * 3 => second invocation from gcry_drbg_ctr_update. When the update function
241384
+ *      was called with addtl, the df_data memory already contains the
241384
+ *      DFed addtl information and we do not need to call DF again.
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_ctr_update (struct gcry_drbg_state *drbg,
241384
+		      struct gcry_drbg_string *addtl, int reseed)
241384
+{
241384
+  gpg_err_code_t ret = GPG_ERR_GENERAL;
241384
+  /* 10.2.1.2 step 1 */
241384
+  unsigned char *temp = drbg->scratchpad;
241384
+  unsigned char *df_data = drbg->scratchpad +
241384
+    gcry_drbg_statelen (drbg) + gcry_drbg_blocklen (drbg);
241384
+  unsigned char *temp_p, *df_data_p;	/* pointer to iterate over buffers */
241384
+  unsigned int len = 0;
241384
+  struct gcry_drbg_string cipherin;
241384
+  unsigned char prefix = DRBG_PREFIX1;
241384
+
241384
+  memset (temp, 0, gcry_drbg_statelen (drbg) + gcry_drbg_blocklen (drbg));
241384
+  if (3 > reseed)
241384
+    memset (df_data, 0, gcry_drbg_statelen (drbg));
241384
+
241384
+  /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
241384
+  /* TODO use reseed variable to avoid re-doing DF operation */
241384
+  (void) reseed;
241384
+  if (addtl && 0 < addtl->len)
241384
+    {
241384
+      ret =
241384
+	gcry_drbg_ctr_df (drbg, df_data, gcry_drbg_statelen (drbg), addtl);
241384
+      if (ret)
241384
+	goto out;
241384
+    }
241384
+
241384
+  gcry_drbg_string_fill (&cipherin, drbg->V, gcry_drbg_blocklen (drbg));
241384
+  /* 10.2.1.3.2 step 2 and 3 -- are already covered as we memset(0)
241384
+   * all memory during initialization */
241384
+  while (len < (gcry_drbg_statelen (drbg)))
241384
+    {
241384
+      /* 10.2.1.2 step 2.1 */
241384
+      gcry_drbg_add_buf (drbg->V, gcry_drbg_blocklen (drbg), &prefix, 1);
241384
+      /* 10.2.1.2 step 2.2 */
241384
+      /* using target of temp + len: 10.2.1.2 step 2.3 and 3 */
241384
+      ret = gcry_drbg_sym (drbg, drbg->C, temp + len, &cipherin);
241384
+      if (ret)
241384
+	goto out;
241384
+      /* 10.2.1.2 step 2.3 and 3 */
241384
+      len += gcry_drbg_blocklen (drbg);
241384
+    }
241384
+
241384
+  /* 10.2.1.2 step 4 */
241384
+  temp_p = temp;
241384
+  df_data_p = df_data;
241384
+  for (len = 0; len < gcry_drbg_statelen (drbg); len++)
241384
+    {
241384
+      *temp_p ^= *df_data_p;
241384
+      df_data_p++;
241384
+      temp_p++;
241384
+    }
241384
+
241384
+  /* 10.2.1.2 step 5 */
241384
+  memcpy (drbg->C, temp, gcry_drbg_keylen (drbg));
241384
+  /* 10.2.1.2 step 6 */
241384
+  memcpy (drbg->V, temp + gcry_drbg_keylen (drbg), gcry_drbg_blocklen (drbg));
241384
+  ret = 0;
241384
+
241384
+out:
241384
+  memset (temp, 0, gcry_drbg_statelen (drbg) + gcry_drbg_blocklen (drbg));
241384
+  if (2 != reseed)
241384
+    memset (df_data, 0, gcry_drbg_statelen (drbg));
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * scratchpad use: gcry_drbg_ctr_update is called independently from
241384
+ * gcry_drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
241384
+ */
241384
+/* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
241384
+static gpg_err_code_t
241384
+gcry_drbg_ctr_generate (struct gcry_drbg_state *drbg,
241384
+			unsigned char *buf, unsigned int buflen,
241384
+			struct gcry_drbg_string *addtl)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  unsigned int len = 0;
241384
+  struct gcry_drbg_string data;
241384
+  unsigned char prefix = DRBG_PREFIX1;
241384
+
241384
+  memset (drbg->scratchpad, 0, gcry_drbg_blocklen (drbg));
241384
+
241384
+  /* 10.2.1.5.2 step 2 */
241384
+  if (addtl && 0 < addtl->len)
241384
+    {
241384
+      addtl->next = NULL;
241384
+      ret = gcry_drbg_ctr_update (drbg, addtl, 2);
241384
+      if (ret)
241384
+	return ret;
241384
+    }
241384
+
241384
+  /* 10.2.1.5.2 step 4.1 */
241384
+  gcry_drbg_add_buf (drbg->V, gcry_drbg_blocklen (drbg), &prefix, 1);
241384
+  gcry_drbg_string_fill (&data, drbg->V, gcry_drbg_blocklen (drbg));
241384
+  while (len < buflen)
241384
+    {
241384
+      unsigned int outlen = 0;
241384
+      /* 10.2.1.5.2 step 4.2 */
241384
+      ret = gcry_drbg_sym (drbg, drbg->C, drbg->scratchpad, &data);
241384
+      if (ret)
241384
+	goto out;
241384
+      outlen = (gcry_drbg_blocklen (drbg) < (buflen - len)) ?
241384
+	gcry_drbg_blocklen (drbg) : (buflen - len);
241384
+      if (!gcry_drbg_fips_continuous_test (drbg, drbg->scratchpad))
241384
+	{
241384
+	  /* 10.2.1.5.2 step 6 */
241384
+	  gcry_drbg_add_buf (drbg->V, gcry_drbg_blocklen (drbg), &prefix, 1);
241384
+	  continue;
241384
+	}
241384
+      /* 10.2.1.5.2 step 4.3 */
241384
+      memcpy (buf + len, drbg->scratchpad, outlen);
241384
+      len += outlen;
241384
+      /* 10.2.1.5.2 step 6 */
241384
+      if (len < buflen)
241384
+	gcry_drbg_add_buf (drbg->V, gcry_drbg_blocklen (drbg), &prefix, 1);
241384
+    }
241384
+
241384
+  /* 10.2.1.5.2 step 6 */
241384
+  if (addtl)
241384
+    addtl->next = NULL;
241384
+  ret = gcry_drbg_ctr_update (drbg, addtl, 3);
241384
+
241384
+out:
241384
+  memset (drbg->scratchpad, 0, gcry_drbg_blocklen (drbg));
241384
+  return ret;
241384
+}
241384
+
241384
+static struct gcry_drbg_state_ops gcry_drbg_ctr_ops = {
241384
+  gcry_drbg_ctr_update,
241384
+  gcry_drbg_ctr_generate,
241384
+};
241384
+
241384
+/******************************************************************
241384
+ * HMAC DRBG callback functions
241384
+ ******************************************************************/
241384
+
241384
+static gpg_err_code_t
241384
+gcry_drbg_hmac_update (struct gcry_drbg_state *drbg,
241384
+		       struct gcry_drbg_string *seed, int reseed)
241384
+{
241384
+  gpg_err_code_t ret = GPG_ERR_GENERAL;
241384
+  int i = 0;
241384
+  struct gcry_drbg_string seed1, seed2, cipherin;
241384
+
241384
+  if (!reseed)
241384
+    /* 10.1.2.3 step 2 already implicitly covered with
241384
+     * the initial memset(0) of drbg->C */
241384
+    memset (drbg->V, 1, gcry_drbg_statelen (drbg));
241384
+
241384
+  /* build linked list which implements the concatenation and fill
241384
+   * first part*/
241384
+  gcry_drbg_string_fill (&seed1, drbg->V, gcry_drbg_statelen (drbg));
241384
+  /* buffer will be filled in for loop below with one byte */
241384
+  gcry_drbg_string_fill (&seed2, NULL, 1);
241384
+  seed1.next = &seed2;
241384
+  /* seed may be NULL */
241384
+  seed2.next = seed;
241384
+
241384
+  gcry_drbg_string_fill (&cipherin, drbg->V, gcry_drbg_statelen (drbg));
241384
+  /* we execute two rounds of V/K massaging */
241384
+  for (i = 2; 0 < i; i--)
241384
+    {
241384
+      /* first round uses 0x0, second 0x1 */
241384
+      unsigned char prefix = DRBG_PREFIX0;
241384
+      if (1 == i)
241384
+	prefix = DRBG_PREFIX1;
241384
+      /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
241384
+      seed2.buf = &prefix;
241384
+      ret = gcry_drbg_hmac (drbg, drbg->C, drbg->C, &seed1);
241384
+      if (ret)
241384
+	return ret;
241384
+
241384
+      /* 10.1.2.2 step 2 and 5 -- HMAC for V */
241384
+      ret = gcry_drbg_hmac (drbg, drbg->C, drbg->V, &cipherin);
241384
+      if (ret)
241384
+	return ret;
241384
+
241384
+      /* 10.1.2.2 step 3 */
241384
+      if (!seed || 0 == seed->len)
241384
+	return ret;
241384
+    }
241384
+  return 0;
241384
+}
241384
+
241384
+/* generate function of HMAC DRBG as defined in 10.1.2.5 */
241384
+static gpg_err_code_t
241384
+gcry_drbg_hmac_generate (struct gcry_drbg_state *drbg,
241384
+			 unsigned char *buf,
241384
+			 unsigned int buflen, struct gcry_drbg_string *addtl)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  unsigned int len = 0;
241384
+  struct gcry_drbg_string data;
241384
+
241384
+  /* 10.1.2.5 step 2 */
241384
+  if (addtl && 0 < addtl->len)
241384
+    {
241384
+      addtl->next = NULL;
241384
+      ret = gcry_drbg_hmac_update (drbg, addtl, 1);
241384
+      if (ret)
241384
+	return ret;
241384
+    }
241384
+
241384
+  gcry_drbg_string_fill (&data, drbg->V, gcry_drbg_statelen (drbg));
241384
+  while (len < buflen)
241384
+    {
241384
+      unsigned int outlen = 0;
241384
+      /* 10.1.2.5 step 4.1 */
241384
+      ret = gcry_drbg_hmac (drbg, drbg->C, drbg->V, &data);
241384
+      if (ret)
241384
+	return ret;
241384
+      outlen = (gcry_drbg_blocklen (drbg) < (buflen - len)) ?
241384
+	gcry_drbg_blocklen (drbg) : (buflen - len);
241384
+      if (!gcry_drbg_fips_continuous_test (drbg, drbg->V))
241384
+	continue;
241384
+
241384
+      /* 10.1.2.5 step 4.2 */
241384
+      memcpy (buf + len, drbg->V, outlen);
241384
+      len += outlen;
241384
+    }
241384
+
241384
+  /* 10.1.2.5 step 6 */
241384
+  if (addtl)
241384
+    addtl->next = NULL;
241384
+  ret = gcry_drbg_hmac_update (drbg, addtl, 1);
241384
+
241384
+  return ret;
241384
+}
241384
+
241384
+static struct gcry_drbg_state_ops gcry_drbg_hmac_ops = {
241384
+  gcry_drbg_hmac_update,
241384
+  gcry_drbg_hmac_generate,
241384
+};
241384
+
241384
+/******************************************************************
241384
+ * Hash DRBG callback functions
241384
+ ******************************************************************/
241384
+
241384
+/*
241384
+ * scratchpad usage: as gcry_drbg_hash_update and gcry_drbg_hash_df are used
241384
+ * interlinked, the scratchpad is used as follows:
241384
+ * gcry_drbg_hash_update
241384
+ *	start: drbg->scratchpad
241384
+ *	length: gcry_drbg_statelen(drbg)
241384
+ * gcry_drbg_hash_df:
241384
+ *	start: drbg->scratchpad + gcry_drbg_statelen(drbg)
241384
+ *	length: gcry_drbg_blocklen(drbg)
241384
+ */
241384
+/* Derivation Function for Hash DRBG as defined in 10.4.1 */
241384
+static gpg_err_code_t
241384
+gcry_drbg_hash_df (struct gcry_drbg_state *drbg,
241384
+		   unsigned char *outval, size_t outlen,
241384
+		   struct gcry_drbg_string *entropy)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  size_t len = 0;
241384
+  unsigned char input[5];
241384
+  unsigned char *tmp = drbg->scratchpad + gcry_drbg_statelen (drbg);
241384
+  struct gcry_drbg_string data1;
241384
+
241384
+  memset (tmp, 0, gcry_drbg_blocklen (drbg));
241384
+
241384
+  /* 10.4.1 step 3 */
241384
+  input[0] = 1;
241384
+  gcry_drbg_cpu_to_be32 ((outlen * 8), &input[1]);
241384
+
241384
+  /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
241384
+  gcry_drbg_string_fill (&data1, input, 5);
241384
+  data1.next = entropy;
241384
+
241384
+  /* 10.4.1 step 4 */
241384
+  while (len < outlen)
241384
+    {
241384
+      short blocklen = 0;
241384
+      /* 10.4.1 step 4.1 */
241384
+      ret = gcry_drbg_hmac (drbg, NULL, tmp, &data1);
241384
+      if (ret)
241384
+	goto out;
241384
+      /* 10.4.1 step 4.2 */
241384
+      input[0]++;
241384
+      blocklen = (gcry_drbg_blocklen (drbg) < (outlen - len)) ?
241384
+	gcry_drbg_blocklen (drbg) : (outlen - len);
241384
+      memcpy (outval + len, tmp, blocklen);
241384
+      len += blocklen;
241384
+    }
241384
+
241384
+out:
241384
+  memset (tmp, 0, gcry_drbg_blocklen (drbg));
241384
+  return ret;
241384
+}
241384
+
241384
+/* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
241384
+static gpg_err_code_t
241384
+gcry_drbg_hash_update (struct gcry_drbg_state *drbg,
241384
+		       struct gcry_drbg_string *seed, int reseed)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  struct gcry_drbg_string data1, data2;
241384
+  unsigned char *V = drbg->scratchpad;
241384
+  unsigned char prefix = DRBG_PREFIX1;
241384
+
241384
+  memset (drbg->scratchpad, 0, gcry_drbg_statelen (drbg));
241384
+  if (!seed)
241384
+    return GPG_ERR_INV_ARG;
241384
+
241384
+  if (reseed)
241384
+    {
241384
+      /* 10.1.1.3 step 1: string length is concatenation of
241384
+       * 1 byte, V and seed (which is concatenated entropy/addtl
241384
+       * input)
241384
+       */
241384
+      memcpy (V, drbg->V, gcry_drbg_statelen (drbg));
241384
+      gcry_drbg_string_fill (&data1, &prefix, 1);
241384
+      gcry_drbg_string_fill (&data2, V, gcry_drbg_statelen (drbg));
241384
+      data1.next = &data2;
241384
+      data2.next = seed;
241384
+    }
241384
+  else
241384
+    {
241384
+      gcry_drbg_string_fill (&data1, seed->buf, seed->len);
241384
+      data1.next = seed->next;
241384
+    }
241384
+
241384
+  /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
241384
+  ret = gcry_drbg_hash_df (drbg, drbg->V, gcry_drbg_statelen (drbg), &data1);
241384
+  if (ret)
241384
+    goto out;
241384
+
241384
+  /* 10.1.1.2 / 10.1.1.3 step 4 -- concatenation  */
241384
+  prefix = DRBG_PREFIX0;
241384
+  gcry_drbg_string_fill (&data1, &prefix, 1);
241384
+  gcry_drbg_string_fill (&data2, drbg->V, gcry_drbg_statelen (drbg));
241384
+  data1.next = &data2;
241384
+  /* 10.1.1.2 / 10.1.1.3 step 4 -- df operation */
241384
+  ret = gcry_drbg_hash_df (drbg, drbg->C, gcry_drbg_statelen (drbg), &data1);
241384
+
241384
+out:
241384
+  memset (drbg->scratchpad, 0, gcry_drbg_statelen (drbg));
241384
+  return ret;
241384
+}
241384
+
241384
+/* processing of additional information string for Hash DRBG */
241384
+static gpg_err_code_t
241384
+gcry_drbg_hash_process_addtl (struct gcry_drbg_state *drbg,
241384
+			      struct gcry_drbg_string *addtl)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  struct gcry_drbg_string data1, data2;
241384
+  struct gcry_drbg_string *data3;
241384
+  unsigned char prefix = DRBG_PREFIX2;
241384
+
241384
+  /* this is value w as per documentation */
241384
+  memset (drbg->scratchpad, 0, gcry_drbg_blocklen (drbg));
241384
+
241384
+  /* 10.1.1.4 step 2 */
241384
+  if (!addtl || 0 == addtl->len)
241384
+    return 0;
241384
+
241384
+  /* 10.1.1.4 step 2a -- concatenation */
241384
+  gcry_drbg_string_fill (&data1, &prefix, 1);
241384
+  gcry_drbg_string_fill (&data2, drbg->V, gcry_drbg_statelen (drbg));
241384
+  data3 = addtl;
241384
+  data1.next = &data2;
241384
+  data2.next = data3;
241384
+  data3->next = NULL;
241384
+  /* 10.1.1.4 step 2a -- cipher invocation */
241384
+  ret = gcry_drbg_hmac (drbg, NULL, drbg->scratchpad, &data1);
241384
+  if (ret)
241384
+    goto out;
241384
+
241384
+  /* 10.1.1.4 step 2b */
241384
+  gcry_drbg_add_buf (drbg->V, gcry_drbg_statelen (drbg),
241384
+		     drbg->scratchpad, gcry_drbg_blocklen (drbg));
241384
+
241384
+out:
241384
+  memset (drbg->scratchpad, 0, gcry_drbg_blocklen (drbg));
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * Hashgen defined in 10.1.1.4
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_hash_hashgen (struct gcry_drbg_state *drbg,
241384
+			unsigned char *buf, unsigned int buflen)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  unsigned int len = 0;
241384
+  unsigned char *src = drbg->scratchpad;
241384
+  unsigned char *dst = drbg->scratchpad + gcry_drbg_statelen (drbg);
241384
+  struct gcry_drbg_string data;
241384
+  unsigned char prefix = DRBG_PREFIX1;
241384
+
241384
+  /* use the scratchpad as a lookaside buffer */
241384
+  memset (src, 0, gcry_drbg_statelen (drbg));
241384
+  memset (dst, 0, gcry_drbg_blocklen (drbg));
241384
+
241384
+  /* 10.1.1.4 step hashgen 2 */
241384
+  memcpy (src, drbg->V, gcry_drbg_statelen (drbg));
241384
+
241384
+  gcry_drbg_string_fill (&data, src, gcry_drbg_statelen (drbg));
241384
+  while (len < buflen)
241384
+    {
241384
+      unsigned int outlen = 0;
241384
+      /* 10.1.1.4 step hashgen 4.1 */
241384
+      ret = gcry_drbg_hmac (drbg, NULL, dst, &data);
241384
+      if (ret)
241384
+	goto out;
241384
+      outlen = (gcry_drbg_blocklen (drbg) < (buflen - len)) ?
241384
+	gcry_drbg_blocklen (drbg) : (buflen - len);
241384
+      if (!gcry_drbg_fips_continuous_test (drbg, dst))
241384
+	{
241384
+	  gcry_drbg_add_buf (src, gcry_drbg_statelen (drbg), &prefix, 1);
241384
+	  continue;
241384
+	}
241384
+      /* 10.1.1.4 step hashgen 4.2 */
241384
+      memcpy (buf + len, dst, outlen);
241384
+      len += outlen;
241384
+      /* 10.1.1.4 hashgen step 4.3 */
241384
+      if (len < buflen)
241384
+	gcry_drbg_add_buf (src, gcry_drbg_statelen (drbg), &prefix, 1);
241384
+    }
241384
+
241384
+out:
241384
+  memset (drbg->scratchpad, 0,
241384
+	  (gcry_drbg_statelen (drbg) + gcry_drbg_blocklen (drbg)));
241384
+  return ret;
241384
+}
241384
+
241384
+/* generate function for Hash DRBG as defined in  10.1.1.4 */
241384
+static gpg_err_code_t
241384
+gcry_drbg_hash_generate (struct gcry_drbg_state *drbg,
241384
+			 unsigned char *buf, unsigned int buflen,
241384
+			 struct gcry_drbg_string *addtl)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  unsigned char prefix = DRBG_PREFIX3;
241384
+  struct gcry_drbg_string data1, data2;
241384
+  union
241384
+  {
241384
+    unsigned char req[8];
241384
+    u64 req_int;
241384
+  } u;
241384
+
241384
+  /*
241384
+   * scratchpad usage: gcry_drbg_hash_process_addtl uses the scratchpad, but
241384
+   * fully completes before returning. Thus, we can reuse the scratchpad
241384
+   */
241384
+  /* 10.1.1.4 step 2 */
241384
+  ret = gcry_drbg_hash_process_addtl (drbg, addtl);
241384
+  if (ret)
241384
+    return ret;
241384
+  /* 10.1.1.4 step 3 -- invocation of the Hashgen function defined in
241384
+   * 10.1.1.4 */
241384
+  ret = gcry_drbg_hash_hashgen (drbg, buf, buflen);
241384
+  if (ret)
241384
+    return ret;
241384
+
241384
+  /* this is the value H as documented in 10.1.1.4 */
241384
+  memset (drbg->scratchpad, 0, gcry_drbg_blocklen (drbg));
241384
+  /* 10.1.1.4 step 4 */
241384
+  gcry_drbg_string_fill (&data1, &prefix, 1);
241384
+  gcry_drbg_string_fill (&data2, drbg->V, gcry_drbg_statelen (drbg));
241384
+  data1.next = &data2;
241384
+  ret = gcry_drbg_hmac (drbg, NULL, drbg->scratchpad, &data1);
241384
+  if (ret)
241384
+    goto out;
241384
+
241384
+  /* 10.1.1.4 step 5 */
241384
+  gcry_drbg_add_buf (drbg->V, gcry_drbg_statelen (drbg),
241384
+		     drbg->scratchpad, gcry_drbg_blocklen (drbg));
241384
+  gcry_drbg_add_buf (drbg->V, gcry_drbg_statelen (drbg), drbg->C,
241384
+		     gcry_drbg_statelen (drbg));
241384
+  u.req_int = be_bswap64 (drbg->reseed_ctr);
241384
+  gcry_drbg_add_buf (drbg->V, gcry_drbg_statelen (drbg), u.req,
241384
+		     sizeof (u.req));
241384
+
241384
+out:
241384
+  memset (drbg->scratchpad, 0, gcry_drbg_blocklen (drbg));
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * scratchpad usage: as update and generate are used isolated, both
241384
+ * can use the scratchpad
241384
+ */
241384
+static struct gcry_drbg_state_ops gcry_drbg_hash_ops = {
241384
+  gcry_drbg_hash_update,
241384
+  gcry_drbg_hash_generate,
241384
+};
241384
+
241384
+/******************************************************************
241384
+ * Functions common for DRBG implementations
241384
+ ******************************************************************/
241384
+
241384
+/*
241384
+ * Seeding or reseeding of the DRBG
241384
+ *
241384
+ * @drbg: DRBG state struct
241384
+ * @pers: personalization / additional information buffer
241384
+ * @reseed: 0 for initial seed process, 1 for reseeding
241384
+ *
241384
+ * return:
241384
+ *	0 on success
241384
+ *	error value otherwise
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_seed (struct gcry_drbg_state *drbg, struct gcry_drbg_string *pers,
241384
+		int reseed)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  unsigned char *entropy = NULL;
241384
+  size_t entropylen = 0;
241384
+  struct gcry_drbg_string data1;
241384
+
241384
+  /* 9.1 / 9.2 / 9.3.1 step 3 */
241384
+  if (pers && pers->len > (gcry_drbg_max_addtl ()))
241384
+    {
241384
+      dbg (("DRBG: personalization string too long %lu\n", pers->len));
241384
+      return GPG_ERR_INV_ARG;
241384
+    }
241384
+  if (drbg->test_data && drbg->test_data->testentropy)
241384
+    {
241384
+      gcry_drbg_string_fill (&data1, drbg->test_data->testentropy->buf,
241384
+			     drbg->test_data->testentropy->len);
241384
+      dbg (("DRBG: using test entropy\n"));
241384
+    }
241384
+  else
241384
+    {
241384
+      /* Gather entropy equal to the security strength of the DRBG.
241384
+       * With a derivation function, a nonce is required in addition
241384
+       * to the entropy. A nonce must be at least 1/2 of the security
241384
+       * strength of the DRBG in size. Thus, entropy * nonce is 3/2
241384
+       * of the strength. The consideration of a nonce is only
241384
+       * applicable during initial seeding. */
241384
+      entropylen = gcry_drbg_sec_strength (drbg->core->flags);
241384
+      if (!entropylen)
241384
+	return GPG_ERR_GENERAL;
241384
+      if (0 == reseed)
241384
+	/* make sure we round up strength/2 in
241384
+	 * case it is not divisible by 2 */
241384
+	entropylen = ((entropylen + 1) / 2) * 3;
241384
+      dbg (("DRBG: (re)seeding with %lu bytes of entropy\n", entropylen));
241384
+      entropy = xcalloc_secure (1, entropylen);
241384
+      if (!entropy)
241384
+	return GPG_ERR_ENOMEM;
241384
+      ret = gcry_drbg_get_entropy (drbg, entropy, entropylen);
241384
+      if (ret)
241384
+	goto out;
241384
+      gcry_drbg_string_fill (&data1, entropy, entropylen);
241384
+    }
241384
+
241384
+  /* concatenation of entropy with personalization str / addtl input)
241384
+   * the variable pers is directly handed by the caller, check its
241384
+   * contents whether it is appropriate */
241384
+  if (pers && pers->buf && 0 < pers->len && NULL == pers->next)
241384
+    {
241384
+      data1.next = pers;
241384
+      dbg (("DRBG: using personalization string\n"));
241384
+    }
241384
+
241384
+  ret = drbg->d_ops->update (drbg, &data1, reseed);
241384
+  dbg (("DRBG: state updated with seed\n"));
241384
+  if (ret)
241384
+    goto out;
241384
+  drbg->seeded = 1;
241384
+  /* 10.1.1.2 / 10.1.1.3 step 5 */
241384
+  drbg->reseed_ctr = 1;
241384
+
241384
+out:
241384
+  xfree (entropy);
241384
+  return ret;
241384
+}
241384
+
241384
+/*************************************************************************
241384
+ * exported interfaces
241384
+ *************************************************************************/
241384
+
241384
+/*
241384
+ * DRBG generate function as required by SP800-90A - this function
241384
+ * generates random numbers
241384
+ *
241384
+ * @drbg DRBG state handle
241384
+ * @buf Buffer where to store the random numbers -- the buffer must already
241384
+ *      be pre-allocated by caller
241384
+ * @buflen Length of output buffer - this value defines the number of random
241384
+ *	   bytes pulled from DRBG
241384
+ * @addtl Additional input that is mixed into state, may be NULL -- note
241384
+ *	  the entropy is pulled by the DRBG internally unconditionally
241384
+ *	  as defined in SP800-90A. The additional input is mixed into
241384
+ *	  the state in addition to the pulled entropy.
241384
+ *
241384
+ * return: generated number of bytes
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_generate (struct gcry_drbg_state *drbg,
241384
+		    unsigned char *buf, unsigned int buflen,
241384
+		    struct gcry_drbg_string *addtl)
241384
+{
241384
+  gpg_err_code_t ret = GPG_ERR_INV_ARG;
241384
+
241384
+  if (0 == buflen || !buf)
241384
+    {
241384
+      dbg (("DRBG: no buffer provided\n"));
241384
+      return ret;
241384
+    }
241384
+  if (addtl && NULL == addtl->buf && 0 < addtl->len)
241384
+    {
241384
+      dbg (("DRBG: wrong format of additional information\n"));
241384
+      return ret;
241384
+    }
241384
+
241384
+  /* 9.3.1 step 2 */
241384
+  if (buflen > (gcry_drbg_max_request_bytes ()))
241384
+    {
241384
+      dbg (("DRBG: requested random numbers too large %u\n", buflen));
241384
+      return ret;
241384
+    }
241384
+  /* 9.3.1 step 3 is implicit with the chosen DRBG */
241384
+  /* 9.3.1 step 4 */
241384
+  if (addtl && addtl->len > (gcry_drbg_max_addtl ()))
241384
+    {
241384
+      dbg (("DRBG: additional information string too long %lu\n",
241384
+	    addtl->len));
241384
+      return ret;
241384
+    }
241384
+  /* 9.3.1 step 5 is implicit with the chosen DRBG */
241384
+  /* 9.3.1 step 6 and 9 supplemented by 9.3.2 step c -- the spec is a
241384
+   * bit convoluted here, we make it simpler */
241384
+  if ((gcry_drbg_max_requests ()) < drbg->reseed_ctr)
241384
+    drbg->seeded = 0;
241384
+
241384
+  if (drbg->pr || !drbg->seeded)
241384
+    {
241384
+      dbg (("DRBG: reseeding before generation (prediction resistance: %s, state %s)\n", drbg->pr ? "true" : "false", drbg->seeded ? "seeded" : "unseeded"));
241384
+      /* 9.3.1 steps 7.1 through 7.3 */
241384
+      ret = gcry_drbg_seed (drbg, addtl, 1);
241384
+      if (ret)
241384
+	return ret;
241384
+      /* 9.3.1 step 7.4 */
241384
+      addtl = NULL;
241384
+    }
241384
+
241384
+  if (addtl && addtl->buf)
241384
+    {
241384
+      dbg (("DRBG: using additional information string\n"));
241384
+    }
241384
+
241384
+  /* 9.3.1 step 8 and 10 */
241384
+  ret = drbg->d_ops->generate (drbg, buf, buflen, addtl);
241384
+
241384
+  /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
241384
+  drbg->reseed_ctr++;
241384
+  if (ret)
241384
+    return ret;
241384
+
241384
+  /* 11.3.3 -- re-perform self tests after some generated random
241384
+   * numbers, the chosen value after which self test is performed
241384
+   * is arbitrary, but it should be reasonable */
241384
+  /* Here we do not perform the self tests because of the following
241384
+   * reasons: it is mathematically impossible that the initial self tests
241384
+   * were successfully and the following are not. If the initial would
241384
+   * pass and the following would not, the system integrity is violated.
241384
+   * In this case, the entire system operation is questionable and it
241384
+   * is unlikely that the integrity violation only affects to the
241384
+   * correct operation of the DRBG.
241384
+   */
241384
+#if 0
241384
+  if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096))
241384
+    {
241384
+      dbg (("DRBG: start to perform self test\n"));
241384
+      ret = gcry_drbg_healthcheck ();
241384
+      if (ret)
241384
+	{
241384
+	  log_fatal (("DRBG: self test failed\n"));
241384
+	  return ret;
241384
+	}
241384
+      else
241384
+	{
241384
+	  dbg (("DRBG: self test successful\n"));
241384
+	}
241384
+    }
241384
+#endif
241384
+
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * Wrapper around gcry_drbg_generate which can pull arbitrary long strings
241384
+ * from the DRBG without hitting the maximum request limitation.
241384
+ *
241384
+ * Parameters: see gcry_drbg_generate
241384
+ * Return codes: see gcry_drbg_generate -- if one gcry_drbg_generate request fails,
241384
+ *		 the entire gcry_drbg_generate_long request fails
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_generate_long (struct gcry_drbg_state *drbg,
241384
+			 unsigned char *buf, unsigned int buflen,
241384
+			 struct gcry_drbg_string *addtl)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  unsigned int slice = 0;
241384
+  unsigned char *buf_p = buf;
241384
+  unsigned len = 0;
241384
+  do
241384
+    {
241384
+      unsigned int chunk = 0;
241384
+      slice = ((buflen - len) / gcry_drbg_max_request_bytes ());
241384
+      chunk = slice ? gcry_drbg_max_request_bytes () : (buflen - len);
241384
+      ret = gcry_drbg_generate (drbg, buf_p, chunk, addtl);
241384
+      if (ret)
241384
+	return ret;
241384
+      buf_p += chunk;
241384
+      len += chunk;
241384
+    }
241384
+  while (slice > 0 && (len < buflen));
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * DRBG uninstantiate function as required by SP800-90A - this function
241384
+ * frees all buffers and the DRBG handle
241384
+ *
241384
+ * @drbg DRBG state handle
241384
+ *
241384
+ * return
241384
+ * 	0 on success
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_uninstantiate (struct gcry_drbg_state *drbg)
241384
+{
241384
+  if (!drbg)
241384
+    return GPG_ERR_INV_ARG;
241384
+  xfree (drbg->V);
241384
+  drbg->V = NULL;
241384
+  xfree (drbg->C);
241384
+  drbg->C = NULL;
241384
+  drbg->reseed_ctr = 0;
241384
+  xfree (drbg->scratchpad);
241384
+  drbg->scratchpad = NULL;
241384
+  drbg->seeded = 0;
241384
+  drbg->pr = 0;
241384
+  drbg->fips_primed = 0;
241384
+  xfree (drbg->prev);
241384
+  drbg->prev = NULL;
241384
+  drbg->seed_init_pid = 0;
241384
+  return 0;
241384
+}
241384
+
241384
+/*
241384
+ * DRBG instantiation function as required by SP800-90A - this function
241384
+ * sets up the DRBG handle, performs the initial seeding and all sanity
241384
+ * checks required by SP800-90A
241384
+ *
241384
+ * @drbg memory of state -- if NULL, new memory is allocated
241384
+ * @pers Personalization string that is mixed into state, may be NULL -- note
241384
+ *	 the entropy is pulled by the DRBG internally unconditionally
241384
+ *	 as defined in SP800-90A. The additional input is mixed into
241384
+ *	 the state in addition to the pulled entropy.
241384
+ * @coreref reference to core
241384
+ * @flags Flags defining the requested DRBG type and cipher type. The flags
241384
+ * 	  are defined in drbg.h and may be XORed. Beware, if you XOR multiple
241384
+ * 	  cipher types together, the code picks the core on a first come first
241384
+ * 	  serve basis as it iterates through the available cipher cores and
241384
+ * 	  uses the one with the first match. The minimum required flags are:
241384
+ * 		cipher type flag
241384
+ *
241384
+ * return
241384
+ *	0 on success
241384
+ *	error value otherwise
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_instantiate (struct gcry_drbg_state *drbg,
241384
+		       struct gcry_drbg_string *pers, int coreref, int pr)
241384
+{
241384
+  gpg_err_code_t ret = GPG_ERR_ENOMEM;
241384
+  unsigned int sb_size = 0;
241384
+
241384
+  if (!drbg)
241384
+    return GPG_ERR_INV_ARG;
241384
+
241384
+  dbg (("DRBG: Initializing DRBG core %d with prediction resistance %s\n",
241384
+	coreref, pr ? "enabled" : "disabled"));
241384
+  drbg->core = &gcry_drbg_cores[coreref];
241384
+  drbg->pr = pr;
241384
+  drbg->seeded = 0;
241384
+  if (drbg->core->flags & GCRY_DRBG_HMAC)
241384
+    drbg->d_ops = &gcry_drbg_hmac_ops;
241384
+  else if (drbg->core->flags & GCRY_DRBG_HASH_MASK)
241384
+    drbg->d_ops = &gcry_drbg_hash_ops;
241384
+  else if (drbg->core->flags & GCRY_DRBG_CTR_MASK)
241384
+    drbg->d_ops = &gcry_drbg_ctr_ops;
241384
+  else
241384
+    return GPG_ERR_GENERAL;
241384
+  /* 9.1 step 1 is implicit with the selected DRBG type -- see
241384
+   * gcry_drbg_sec_strength() */
241384
+
241384
+  /* 9.1 step 2 is implicit as caller can select prediction resistance
241384
+   * and the flag is copied into drbg->flags --
241384
+   * all DRBG types support prediction resistance */
241384
+
241384
+  /* 9.1 step 4 is implicit in  gcry_drbg_sec_strength */
241384
+
241384
+  /* no allocation of drbg as this is done by the kernel crypto API */
241384
+  drbg->V = xcalloc_secure (1, gcry_drbg_statelen (drbg));
241384
+  if (!drbg->V)
241384
+    goto err;
241384
+  drbg->C = xcalloc_secure (1, gcry_drbg_statelen (drbg));
241384
+  if (!drbg->C)
241384
+    goto err;
241384
+  drbg->prev = xcalloc_secure (1, gcry_drbg_blocklen (drbg));
241384
+  if (!drbg->prev)
241384
+    goto err;
241384
+  drbg->fips_primed = 0;
241384
+  /* scratchpad is only generated for CTR and Hash */
241384
+  if (drbg->core->flags & GCRY_DRBG_HMAC)
241384
+    sb_size = 0;
241384
+  else if (drbg->core->flags & GCRY_DRBG_CTR_MASK)
241384
+    sb_size = gcry_drbg_statelen (drbg) + gcry_drbg_blocklen (drbg) +	/* temp */
241384
+      gcry_drbg_statelen (drbg) +	/* df_data */
241384
+      gcry_drbg_blocklen (drbg) +	/* pad */
241384
+      gcry_drbg_blocklen (drbg) +	/* iv */
241384
+      gcry_drbg_statelen (drbg) + gcry_drbg_blocklen (drbg);	/* temp */
241384
+  else
241384
+    sb_size = gcry_drbg_statelen (drbg) + gcry_drbg_blocklen (drbg);
241384
+
241384
+  if (0 < sb_size)
241384
+    {
241384
+      drbg->scratchpad = xcalloc_secure (1, sb_size);
241384
+      if (!drbg->scratchpad)
241384
+	goto err;
241384
+    }
241384
+  dbg (("DRBG: state allocated with scratchpad size %u bytes\n", sb_size));
241384
+
241384
+  /* 9.1 step 6 through 11 */
241384
+  ret = gcry_drbg_seed (drbg, pers, 0);
241384
+  if (ret)
241384
+    goto err;
241384
+
241384
+  dbg (("DRBG: core %d %s prediction resistance successfully initialized\n",
241384
+	coreref, pr ? "with" : "without"));
241384
+  return 0;
241384
+
241384
+err:
241384
+  gcry_drbg_uninstantiate (drbg);
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * DRBG reseed function as required by SP800-90A
241384
+ *
241384
+ * @drbg DRBG state handle
241384
+ * @addtl Additional input that is mixed into state, may be NULL -- note
241384
+ * 		the entropy is pulled by the DRBG internally unconditionally
241384
+ * 		as defined in SP800-90A. The additional input is mixed into
241384
+ * 		the state in addition to the pulled entropy.
241384
+ *
241384
+ * return
241384
+ * 	0 on success
241384
+ * 	error value otherwise
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_reseed (struct gcry_drbg_state *drbg,
241384
+		  struct gcry_drbg_string *addtl)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  ret = gcry_drbg_seed (drbg, addtl, 1);
241384
+  return ret;
241384
+}
241384
+
241384
+/******************************************************************
241384
+ ******************************************************************
241384
+ ******************************************************************
241384
+ * libgcrypt integration code
241384
+ ******************************************************************
241384
+ ******************************************************************
241384
+ ******************************************************************/
241384
+
241384
+/***************************************************************
241384
+ * libgcrypt backend functions to the RNG API code
241384
+ ***************************************************************/
241384
+
241384
+/* global state variable holding the current instance of the DRBG -- the
241384
+ * default DRBG type is defined in _gcry_gcry_drbg_init */
241384
+static struct gcry_drbg_state *gcry_drbg = NULL;
241384
+
241384
+/* This is the lock we use to serialize access to this RNG. */
241384
+static ath_mutex_t drbg_lock;
241384
+
241384
+static inline void
241384
+gcry_drbg_lock (void)
241384
+{
241384
+  int my_errno;
241384
+
241384
+  my_errno = ath_mutex_lock (&drbg_lock);
241384
+  if (my_errno)
241384
+    log_fatal ("failed to acquire the RNG lock: %s\n", strerror (my_errno));
241384
+}
241384
+
241384
+static inline void
241384
+gcry_drbg_unlock (void)
241384
+{
241384
+  int my_errno;
241384
+
241384
+  my_errno = ath_mutex_unlock (&drbg_lock);
241384
+  if (my_errno)
241384
+    log_fatal ("failed to release the RNG lock: %s\n", strerror (my_errno));
241384
+}
241384
+
241384
+/* Basic initialization is required to initialize mutexes and
241384
+   do a few checks on the implementation.  */
241384
+static void
241384
+basic_initialization (void)
241384
+{
241384
+  static int initialized;
241384
+  int my_errno;
241384
+
241384
+  if (initialized)
241384
+    return;
241384
+  initialized = 1;
241384
+
241384
+  my_errno = ath_mutex_init (&drbg_lock);
241384
+  if (my_errno)
241384
+    log_fatal ("failed to create the RNG lock: %s\n", strerror (my_errno));
241384
+}
241384
+
241384
+/****** helper functions where lock must be held by caller *****/
241384
+
241384
+/* Check whether given flags are known to point to an applicable DRBG */
241384
+static gpg_err_code_t
241384
+gcry_drbg_algo_available (u32 flags, int *coreref)
241384
+{
241384
+  int i = 0;
241384
+  for (i = 0; ARRAY_SIZE (gcry_drbg_cores) > i; i++)
241384
+    {
241384
+      if ((gcry_drbg_cores[i].flags & GCRY_DRBG_CIPHER_MASK) ==
241384
+	  (flags & GCRY_DRBG_CIPHER_MASK))
241384
+	{
241384
+	  *coreref = i;
241384
+	  return 0;
241384
+	}
241384
+    }
241384
+  return GPG_ERR_GENERAL;
241384
+}
241384
+
241384
+static gpg_err_code_t
241384
+_gcry_drbg_init_internal (u32 flags, struct gcry_drbg_string *pers)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  static u32 oldflags = 0;
241384
+  int coreref = 0;
241384
+  int pr = 0;
241384
+
241384
+  /* If a caller provides 0 as flags, use the flags of the previous
241384
+   * initialization, otherwise use the current flags and remember them
241384
+   * for the next invocation
241384
+   */
241384
+  if (0 == flags)
241384
+    flags = oldflags;
241384
+  else
241384
+    oldflags = flags;
241384
+
241384
+  ret = gcry_drbg_algo_available (flags, &coreref);
241384
+  if (ret)
241384
+    return ret;
241384
+
241384
+  if (NULL != gcry_drbg)
241384
+    {
241384
+      gcry_drbg_uninstantiate (gcry_drbg);
241384
+    }
241384
+  else
241384
+    {
241384
+      gcry_drbg = xcalloc_secure (1, sizeof (struct gcry_drbg_state));
241384
+      if (!gcry_drbg)
241384
+	return GPG_ERR_ENOMEM;
241384
+    }
241384
+  if (flags & GCRY_DRBG_PREDICTION_RESIST)
241384
+    pr = 1;
241384
+  ret = gcry_drbg_instantiate (gcry_drbg, pers, coreref, pr);
241384
+  if (ret)
241384
+    fips_signal_error ("DRBG cannot be initialized");
241384
+  else
241384
+    gcry_drbg->seed_init_pid = getpid ();
241384
+  return ret;
241384
+}
241384
+
241384
+/************* calls available to common RNG code **************/
241384
+
241384
+/*
241384
+ * Initialize one DRBG invoked by the libgcrypt API
241384
+ */
241384
+void
241384
+_gcry_drbg_init (int full)
241384
+{
241384
+  (void) full; /* ignore for now */
241384
+  /* default DRBG */
241384
+  u32 flags = GCRY_DRBG_NOPR_HMACSHA256;
241384
+  basic_initialization ();
241384
+  gcry_drbg_lock ();
241384
+  _gcry_drbg_init_internal (flags, NULL);
241384
+  gcry_drbg_unlock ();
241384
+}
241384
+
241384
+/*
241384
+ * Backend handler function for GCRYCTL_DRBG_REINIT
241384
+ *
241384
+ * Select a different DRBG type and initialize it.
241384
+ * Function checks whether requested DRBG type exists and returns an error in
241384
+ * case it does not. In case of an error, the previous instantiated DRBG is
241384
+ * left untouched and alive. Thus, in case of an error, a DRBG is always
241384
+ * available, even if it is not the chosen one.
241384
+ *
241384
+ * Re-initialization will be performed in any case regardless whether flags
241384
+ * or personalization string are set.
241384
+ *
241384
+ * If flags == 0, do not change current DRBG
241384
+ * If personalization string is NULL or its length is 0, re-initialize without
241384
+ * personalization string
241384
+ */
241384
+gpg_err_code_t
241384
+_gcry_drbg_reinit (u32 flags, struct gcry_drbg_string *pers)
241384
+{
241384
+  gpg_err_code_t ret = GPG_ERR_GENERAL;
241384
+  dbg (("DRBG: reinitialize internal DRBG state with flags %u\n", flags));
241384
+  gcry_drbg_lock ();
241384
+  ret = _gcry_drbg_init_internal (flags, pers);
241384
+  gcry_drbg_unlock ();
241384
+  return ret;
241384
+}
241384
+
241384
+/* Print some statistics about the RNG.  */
241384
+void
241384
+_gcry_drbg_dump_stats (void)
241384
+{
241384
+  /* Not yet implemented.  */
241384
+  /* Maybe dumping of reseed counter? */
241384
+}
241384
+
241384
+/* This function returns true if no real RNG is available or the
241384
+ * quality of the RNG has been degraded for test purposes.  */
241384
+int
241384
+_gcry_drbg_is_faked (void)
241384
+{
241384
+  return 0;			/* Faked random is not allowed.  */
241384
+}
241384
+
241384
+/* Add BUFLEN bytes from BUF to the internal random pool.  QUALITY
241384
+ * should be in the range of 0..100 to indicate the goodness of the
241384
+ * entropy added, or -1 for goodness not known. */
241384
+gcry_error_t
241384
+_gcry_drbg_add_bytes (const void *buf, size_t buflen, int quality)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  struct gcry_drbg_string seed;
241384
+  (void) quality;
241384
+  if (NULL == gcry_drbg)
241384
+    return GPG_ERR_GENERAL;
241384
+  gcry_drbg_string_fill (&seed, (unsigned char *) buf, buflen);
241384
+  gcry_drbg_lock ();
241384
+  ret = gcry_drbg_reseed (gcry_drbg, &seed);
241384
+  gcry_drbg_unlock ();
241384
+  return ret;
241384
+}
241384
+
241384
+/* This function is to be used for all types of random numbers, including
241384
+ * nonces
241384
+ */
241384
+void
241384
+_gcry_drbg_randomize (void *buffer, size_t length,
241384
+		      enum gcry_random_level level)
241384
+{
241384
+  (void) level;
241384
+  gcry_drbg_lock ();
241384
+  if (NULL == gcry_drbg)
241384
+    {
241384
+      fips_signal_error ("DRBG is not initialized");
241384
+      goto bailout;
241384
+    }
241384
+
241384
+  /* As reseeding changes the entire state of the DRBG, including any
241384
+   * key, either a re-init or a reseed is sufficient for a fork */
241384
+  if (gcry_drbg->seed_init_pid != getpid ())
241384
+    {
241384
+      /* We are in a child of us. Perform a reseeding. */
241384
+      if (gcry_drbg_reseed (gcry_drbg, NULL))
241384
+	{
241384
+	  fips_signal_error ("reseeding upon fork failed");
241384
+	  log_fatal ("severe error getting random\n");
241384
+	  goto bailout;
241384
+	}
241384
+      gcry_drbg->seed_init_pid = getpid ();
241384
+    }
241384
+  /* potential integer overflow is covered by gcry_drbg_generate which
241384
+   * ensures that length cannot overflow an unsigned int */
241384
+  if (0 < length)
241384
+    {
241384
+      if (!buffer)
241384
+	goto bailout;
241384
+      if (gcry_drbg_generate_long
241384
+	  (gcry_drbg, buffer, (unsigned int) length, NULL))
241384
+	log_fatal ("No random numbers generated\n");
241384
+    }
241384
+  else
241384
+    {
241384
+      struct gcry_drbg_gen *data = (struct gcry_drbg_gen *) buffer;
241384
+      /* catch NULL pointer */
241384
+      if (!data || !data->outbuf)
241384
+	{
241384
+	  fips_signal_error ("No output buffer provided");
241384
+	  goto bailout;
241384
+	}
241384
+      if (gcry_drbg_generate_long (gcry_drbg, data->outbuf, data->outlen,
241384
+				   data->addtl))
241384
+	log_fatal ("No random numbers generated\n");
241384
+    }
241384
+bailout:
241384
+  gcry_drbg_unlock ();
241384
+  return;
241384
+
241384
+}
241384
+
241384
+/***************************************************************
241384
+ * Self-test code
241384
+ ***************************************************************/
241384
+
241384
+/*
241384
+ * Test vectors from
241384
+ * http://csrc.nist.gov/groups/STM/cavp/documents/drbg/drbgtestvectors.zip
241384
+ */
241384
+struct gcry_drbg_test_vector gcry_drbg_test_pr[] = {
241384
+  {
241384
+   .flags = (GCRY_DRBG_PR_HASHSHA256),
241384
+   .entropy = (unsigned char *)
241384
+   "\x5d\xf2\x14\xbc\xf6\xb5\x4e\x0b\xf0\x0d\x6f\x2d"
241384
+   "\xe2\x01\x66\x7b\xd0\xa4\x73\xa4\x21\xdd\xb0\xc0"
241384
+   "\x51\x79\x09\xf4\xea\xa9\x08\xfa\xa6\x67\xe0\xe1"
241384
+   "\xd1\x88\xa8\xad\xee\x69\x74\xb3\x55\x06\x9b\xf6",
241384
+   .entropylen = 48,
241384
+   .entpra = (unsigned char *)
241384
+   "\xef\x48\x06\xa2\xc2\x45\xf1\x44\xfa\x34\x2c\xeb"
241384
+   "\x8d\x78\x3c\x09\x8f\x34\x72\x20\xf2\xe7\xfd\x13"
241384
+   "\x76\x0a\xf6\xdc\x3c\xf5\xc0\x15",
241384
+   .entprb = (unsigned char *)
241384
+   "\x4b\xbe\xe5\x24\xed\x6a\x2d\x0c\xdb\x73\x5e\x09"
241384
+   "\xf9\xad\x67\x7c\x51\x47\x8b\x6b\x30\x2a\xc6\xde"
241384
+   "\x76\xaa\x55\x04\x8b\x0a\x72\x95",
241384
+   .entprlen = 32,
241384
+   .expected = (unsigned char *)
241384
+   "\x3b\x14\x71\x99\xa1\xda\xa0\x42\xe6\xc8\x85\x32"
241384
+   "\x70\x20\x32\x53\x9a\xbe\xd1\x1e\x15\xef\xfb\x4c"
241384
+   "\x25\x6e\x19\x3a\xf0\xb9\xcb\xde\xf0\x3b\xc6\x18"
241384
+   "\x4d\x85\x5a\x9b\xf1\xe3\xc2\x23\x03\x93\x08\xdb"
241384
+   "\xa7\x07\x4b\x33\x78\x40\x4d\xeb\x24\xf5\x6e\x81"
241384
+   "\x4a\x1b\x6e\xa3\x94\x52\x43\xb0\xaf\x2e\x21\xf4"
241384
+   "\x42\x46\x8e\x90\xed\x34\x21\x75\xea\xda\x67\xb6"
241384
+   "\xe4\xf6\xff\xc6\x31\x6c\x9a\x5a\xdb\xb3\x97\x13"
241384
+   "\x09\xd3\x20\x98\x33\x2d\x6d\xd7\xb5\x6a\xa8\xa9"
241384
+   "\x9a\x5b\xd6\x87\x52\xa1\x89\x2b\x4b\x9c\x64\x60"
241384
+   "\x50\x47\xa3\x63\x81\x16\xaf\x19",
241384
+   .expectedlen = 128,
241384
+   .addtla = (unsigned char *)
241384
+   "\xbe\x13\xdb\x2a\xe9\xa8\xfe\x09\x97\xe1\xce\x5d"
241384
+   "\xe8\xbb\xc0\x7c\x4f\xcb\x62\x19\x3f\x0f\xd2\xad"
241384
+   "\xa9\xd0\x1d\x59\x02\xc4\xff\x70",
241384
+   .addtlb = (unsigned char *)
241384
+   "\x6f\x96\x13\xe2\xa7\xf5\x6c\xfe\xdf\x66\xe3\x31"
241384
+   "\x63\x76\xbf\x20\x27\x06\x49\xf1\xf3\x01\x77\x41"
241384
+   "\x9f\xeb\xe4\x38\xfe\x67\x00\xcd",
241384
+   .addtllen = 32,
241384
+   .pers = NULL,
241384
+   .perslen = 0,
241384
+   },
241384
+  {
241384
+   .flags = (GCRY_DRBG_PR_HMACSHA256),
241384
+   .entropy = (unsigned char *)
241384
+   "\x13\x54\x96\xfc\x1b\x7d\x28\xf3\x18\xc9\xa7\x89"
241384
+   "\xb6\xb3\xc8\x72\xac\x00\xd4\x59\x36\x25\x05\xaf"
241384
+   "\xa5\xdb\x96\xcb\x3c\x58\x46\x87\xa5\xaa\xbf\x20"
241384
+   "\x3b\xfe\x23\x0e\xd1\xc7\x41\x0f\x3f\xc9\xb3\x67",
241384
+   .entropylen = 48,
241384
+   .entpra = (unsigned char *)
241384
+   "\xe2\xbd\xb7\x48\x08\x06\xf3\xe1\x93\x3c\xac\x79"
241384
+   "\xa7\x2b\x11\xda\xe3\x2e\xe1\x91\xa5\x02\x19\x57"
241384
+   "\x20\x28\xad\xf2\x60\xd7\xcd\x45",
241384
+   .entprb = (unsigned char *)
241384
+   "\x8b\xd4\x69\xfc\xff\x59\x95\x95\xc6\x51\xde\x71"
241384
+   "\x68\x5f\xfc\xf9\x4a\xab\xec\x5a\xcb\xbe\xd3\x66"
241384
+   "\x1f\xfa\x74\xd3\xac\xa6\x74\x60",
241384
+   .entprlen = 32,
241384
+   .expected = (unsigned char *)
241384
+   "\x1f\x9e\xaf\xe4\xd2\x46\xb7\x47\x41\x4c\x65\x99"
241384
+   "\x01\xe9\x3b\xbb\x83\x0c\x0a\xb0\xc1\x3a\xe2\xb3"
241384
+   "\x31\x4e\xeb\x93\x73\xee\x0b\x26\xc2\x63\xa5\x75"
241384
+   "\x45\x99\xd4\x5c\x9f\xa1\xd4\x45\x87\x6b\x20\x61"
241384
+   "\x40\xea\x78\xa5\x32\xdf\x9e\x66\x17\xaf\xb1\x88"
241384
+   "\x9e\x2e\x23\xdd\xc1\xda\x13\x97\x88\xa5\xb6\x5e"
241384
+   "\x90\x14\x4e\xef\x13\xab\x5c\xd9\x2c\x97\x9e\x7c"
241384
+   "\xd7\xf8\xce\xea\x81\xf5\xcd\x71\x15\x49\x44\xce"
241384
+   "\x83\xb6\x05\xfb\x7d\x30\xb5\x57\x2c\x31\x4f\xfc"
241384
+   "\xfe\x80\xb6\xc0\x13\x0c\x5b\x9b\x2e\x8f\x3d\xfc"
241384
+   "\xc2\xa3\x0c\x11\x1b\x80\x5f\xf3",
241384
+   .expectedlen = 128,
241384
+   .addtla = NULL,
241384
+   .addtlb = NULL,
241384
+   .addtllen = 0,
241384
+   .pers = (unsigned char *)
241384
+   "\x64\xb6\xfc\x60\xbc\x61\x76\x23\x6d\x3f\x4a\x0f"
241384
+   "\xe1\xb4\xd5\x20\x9e\x70\xdd\x03\x53\x6d\xbf\xce"
241384
+   "\xcd\x56\x80\xbc\xb8\x15\xc8\xaa",
241384
+   .perslen = 32,
241384
+   },
241384
+  {
241384
+   .flags = (GCRY_DRBG_PR_CTRAES128),
241384
+   .entropy = (unsigned char *)
241384
+   "\x92\x89\x8f\x31\xfa\x1c\xff\x6d\x18\x2f\x26\x06"
241384
+   "\x43\xdf\xf8\x18\xc2\xa4\xd9\x72\xc3\xb9\xb6\x97",
241384
+   .entropylen = 24,
241384
+   .entpra = (unsigned char *)
241384
+   "\x20\x72\x8a\x06\xf8\x6f\x8d\xd4\x41\xe2\x72\xb7"
241384
+   "\xc4\x2c\xe8\x10",
241384
+   .entprb = (unsigned char *)
241384
+   "\x3d\xb0\xf0\x94\xf3\x05\x50\x33\x17\x86\x3e\x22"
241384
+   "\x08\xf7\xa5\x01",
241384
+   .entprlen = 16,
241384
+   .expected = (unsigned char *)
241384
+   "\x5a\x35\x39\x87\x0f\x4d\x22\xa4\x09\x24\xee\x71"
241384
+   "\xc9\x6f\xac\x72\x0a\xd6\xf0\x88\x82\xd0\x83\x28"
241384
+   "\x73\xec\x3f\x93\xd8\xab\x45\x23\xf0\x7e\xac\x45"
241384
+   "\x14\x5e\x93\x9f\xb1\xd6\x76\x43\x3d\xb6\xe8\x08"
241384
+   "\x88\xf6\xda\x89\x08\x77\x42\xfe\x1a\xf4\x3f\xc4"
241384
+   "\x23\xc5\x1f\x68",
241384
+   .expectedlen = 64,
241384
+   .addtla = (unsigned char *)
241384
+   "\x1a\x40\xfa\xe3\xcc\x6c\x7c\xa0\xf8\xda\xba\x59"
241384
+   "\x23\x6d\xad\x1d",
241384
+   .addtlb = (unsigned char *)
241384
+   "\x9f\x72\x76\x6c\xc7\x46\xe5\xed\x2e\x53\x20\x12"
241384
+   "\xbc\x59\x31\x8c",
241384
+   .addtllen = 16,
241384
+   .pers = (unsigned char *)
241384
+   "\xea\x65\xee\x60\x26\x4e\x7e\xb6\x0e\x82\x68\xc4"
241384
+   "\x37\x3c\x5c\x0b",
241384
+   .perslen = 16,
241384
+   },
241384
+};
241384
+
241384
+struct gcry_drbg_test_vector gcry_drbg_test_nopr[] = {
241384
+  {
241384
+   .flags = GCRY_DRBG_NOPR_HASHSHA256,
241384
+   .entropy = (unsigned char *)
241384
+   "\x73\xd3\xfb\xa3\x94\x5f\x2b\x5f\xb9\x8f\xf6\x9c"
241384
+   "\x8a\x93\x17\xae\x19\xc3\x4c\xc3\xd6\xca\xa3\x2d"
241384
+   "\x16\xfc\x42\xd2\x2d\xd5\x6f\x56\xcc\x1d\x30\xff"
241384
+   "\x9e\x06\x3e\x09\xce\x58\xe6\x9a\x35\xb3\xa6\x56",
241384
+   .entropylen = 48,
241384
+   .expected = (unsigned char *)
241384
+   "\x71\x7b\x93\x46\x1a\x40\xaa\x35\xa4\xaa\xc5\xe7"
241384
+   "\x6d\x5b\x5b\x8a\xa0\xdf\x39\x7d\xae\x71\x58\x5b"
241384
+   "\x3c\x7c\xb4\xf0\x89\xfa\x4a\x8c\xa9\x5c\x54\xc0"
241384
+   "\x40\xdf\xbc\xce\x26\x81\x34\xf8\xba\x7d\x1c\xe8"
241384
+   "\xad\x21\xe0\x74\xcf\x48\x84\x30\x1f\xa1\xd5\x4f"
241384
+   "\x81\x42\x2f\xf4\xdb\x0b\x23\xf8\x73\x27\xb8\x1d"
241384
+   "\x42\xf8\x44\x58\xd8\x5b\x29\x27\x0a\xf8\x69\x59"
241384
+   "\xb5\x78\x44\xeb\x9e\xe0\x68\x6f\x42\x9a\xb0\x5b"
241384
+   "\xe0\x4e\xcb\x6a\xaa\xe2\xd2\xd5\x33\x25\x3e\xe0"
241384
+   "\x6c\xc7\x6a\x07\xa5\x03\x83\x9f\xe2\x8b\xd1\x1c"
241384
+   "\x70\xa8\x07\x59\x97\xeb\xf6\xbe",
241384
+   .expectedlen = 128,
241384
+   .addtla = (unsigned char *)
241384
+   "\xf4\xd5\x98\x3d\xa8\xfc\xfa\x37\xb7\x54\x67\x73"
241384
+   "\xc7\xc3\xdd\x47\x34\x71\x02\x5d\xc1\xa0\xd3\x10"
241384
+   "\xc1\x8b\xbd\xf5\x66\x34\x6f\xdd",
241384
+   .addtlb = (unsigned char *)
241384
+   "\xf7\x9e\x6a\x56\x0e\x73\xe9\xd9\x7a\xd1\x69\xe0"
241384
+   "\x6f\x8c\x55\x1c\x44\xd1\xce\x6f\x28\xcc\xa4\x4d"
241384
+   "\xa8\xc0\x85\xd1\x5a\x0c\x59\x40",
241384
+   .addtllen = 32,
241384
+   .pers = NULL,
241384
+   .perslen = 0,
241384
+   },
241384
+  {
241384
+   .flags = GCRY_DRBG_NOPR_HMACSHA256,
241384
+   .entropy = (unsigned char *)
241384
+   "\x8d\xf0\x13\xb4\xd1\x03\x52\x30\x73\x91\x7d\xdf"
241384
+   "\x6a\x86\x97\x93\x05\x9e\x99\x43\xfc\x86\x54\x54"
241384
+   "\x9e\x7a\xb2\x2f\x7c\x29\xf1\x22\xda\x26\x25\xaf"
241384
+   "\x2d\xdd\x4a\xbc\xce\x3c\xf4\xfa\x46\x59\xd8\x4e",
241384
+   .entropylen = 48,
241384
+   .expected = (unsigned char *)
241384
+   "\xb9\x1c\xba\x4c\xc8\x4f\xa2\x5d\xf8\x61\x0b\x81"
241384
+   "\xb6\x41\x40\x27\x68\xa2\x09\x72\x34\x93\x2e\x37"
241384
+   "\xd5\x90\xb1\x15\x4c\xbd\x23\xf9\x74\x52\xe3\x10"
241384
+   "\xe2\x91\xc4\x51\x46\x14\x7f\x0d\xa2\xd8\x17\x61"
241384
+   "\xfe\x90\xfb\xa6\x4f\x94\x41\x9c\x0f\x66\x2b\x28"
241384
+   "\xc1\xed\x94\xda\x48\x7b\xb7\xe7\x3e\xec\x79\x8f"
241384
+   "\xbc\xf9\x81\xb7\x91\xd1\xbe\x4f\x17\x7a\x89\x07"
241384
+   "\xaa\x3c\x40\x16\x43\xa5\xb6\x2b\x87\xb8\x9d\x66"
241384
+   "\xb3\xa6\x0e\x40\xd4\xa8\xe4\xe9\xd8\x2a\xf6\xd2"
241384
+   "\x70\x0e\x6f\x53\x5c\xdb\x51\xf7\x5c\x32\x17\x29"
241384
+   "\x10\x37\x41\x03\x0c\xcc\x3a\x56",
241384
+   .expectedlen = 128,
241384
+   .addtla = NULL,
241384
+   .addtlb = NULL,
241384
+   .addtllen = 0,
241384
+   .pers = (unsigned char *)
241384
+   "\xb5\x71\xe6\x6d\x7c\x33\x8b\xc0\x7b\x76\xad\x37"
241384
+   "\x57\xbb\x2f\x94\x52\xbf\x7e\x07\x43\x7a\xe8\x58"
241384
+   "\x1c\xe7\xbc\x7c\x3a\xc6\x51\xa9",
241384
+   .perslen = 32,
241384
+   },
241384
+  {
241384
+   .flags = GCRY_DRBG_NOPR_CTRAES128,
241384
+   .entropy = (unsigned char *)
241384
+   "\xc0\x70\x1f\x92\x50\x75\x8f\xcd\xf2\xbe\x73\x98"
241384
+   "\x80\xdb\x66\xeb\x14\x68\xb4\xa5\x87\x9c\x2d\xa6",
241384
+   .entropylen = 24,
241384
+   .expected = (unsigned char *)
241384
+   "\x97\xc0\xc0\xe5\xa0\xcc\xf2\x4f\x33\x63\x48\x8a"
241384
+   "\xdb\x13\x0a\x35\x89\xbf\x80\x65\x62\xee\x13\x95"
241384
+   "\x7c\x33\xd3\x7d\xf4\x07\x77\x7a\x2b\x65\x0b\x5f"
241384
+   "\x45\x5c\x13\xf1\x90\x77\x7f\xc5\x04\x3f\xcc\x1a"
241384
+   "\x38\xf8\xcd\x1b\xbb\xd5\x57\xd1\x4a\x4c\x2e\x8a"
241384
+   "\x2b\x49\x1e\x5c",
241384
+   .expectedlen = 64,
241384
+   .addtla = (unsigned char *)
241384
+   "\xf9\x01\xf8\x16\x7a\x1d\xff\xde\x8e\x3c\x83\xe2"
241384
+   "\x44\x85\xe7\xfe",
241384
+   .addtlb = (unsigned char *)
241384
+   "\x17\x1c\x09\x38\xc2\x38\x9f\x97\x87\x60\x55\xb4"
241384
+   "\x82\x16\x62\x7f",
241384
+   .addtllen = 16,
241384
+   .pers = (unsigned char *)
241384
+   "\x80\x08\xae\xe8\xe9\x69\x40\xc5\x08\x73\xc7\x9f"
241384
+   "\x8e\xcf\xe0\x02",
241384
+   .perslen = 16,
241384
+   },
241384
+};
241384
+
241384
+
241384
+/*
241384
+ * Tests implement the CAVS test approach as documented in
241384
+ * http://csrc.nist.gov/groups/STM/cavp/documents/drbg/DRBGVS.pdf
241384
+ */
241384
+
241384
+/*
241384
+ * CAVS test
241384
+ *
241384
+ * This function is not static as it is needed for as a private API
241384
+ * call for the CAVS test tool.
241384
+ */
241384
+gpg_err_code_t
241384
+gcry_drbg_cavs_test (struct gcry_drbg_test_vector *test, unsigned char *buf)
241384
+{
241384
+  gpg_err_code_t ret = 0;
241384
+  struct gcry_drbg_state *drbg = NULL;
241384
+  struct gcry_drbg_test_data test_data;
241384
+  struct gcry_drbg_string addtl, pers, testentropy;
241384
+  int coreref = 0;
241384
+  int pr = 0;
241384
+
241384
+  ret = gcry_drbg_algo_available (test->flags, &coreref);
241384
+  if (ret)
241384
+    goto outbuf;
241384
+
241384
+  drbg = gcry_xcalloc (1, sizeof (struct gcry_drbg_state));
241384
+  if (!drbg)
241384
+    {
241384
+      ret = GPG_ERR_ENOMEM;
241384
+      goto outbuf;
241384
+    }
241384
+
241384
+  if (test->flags & GCRY_DRBG_PREDICTION_RESIST)
241384
+    pr = 1;
241384
+
241384
+  test_data.testentropy = &testentropy;
241384
+  gcry_drbg_string_fill (&testentropy, test->entropy, test->entropylen);
241384
+  drbg->test_data = &test_data;
241384
+  gcry_drbg_string_fill (&pers, test->pers, test->perslen);
241384
+  ret = gcry_drbg_instantiate (drbg, &pers, coreref, pr);
241384
+  if (ret)
241384
+    goto outbuf;
241384
+
241384
+  gcry_drbg_string_fill (&addtl, test->addtla, test->addtllen);
241384
+  if (test->entpra)
241384
+    {
241384
+      gcry_drbg_string_fill (&testentropy, test->entpra, test->entprlen);
241384
+      drbg->test_data = &test_data;
241384
+    }
241384
+  gcry_drbg_generate_long (drbg, buf, test->expectedlen, &addtl);
241384
+
241384
+  gcry_drbg_string_fill (&addtl, test->addtlb, test->addtllen);
241384
+  if (test->entprb)
241384
+    {
241384
+      gcry_drbg_string_fill (&testentropy, test->entprb, test->entprlen);
241384
+      drbg->test_data = &test_data;
241384
+    }
241384
+  gcry_drbg_generate_long (drbg, buf, test->expectedlen, &addtl);
241384
+  gcry_drbg_uninstantiate (drbg);
241384
+
241384
+outbuf:
241384
+  xfree (drbg);
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * Invoke the CAVS test and perform the final check whether the
241384
+ * calculated random value matches the expected one.
241384
+ *
241384
+ * This function is not static as it is needed for as a private API
241384
+ * call for the CAVS test tool.
241384
+ */
241384
+gpg_err_code_t
241384
+gcry_drbg_healthcheck_one (struct gcry_drbg_test_vector * test)
241384
+{
241384
+  gpg_err_code_t ret = GPG_ERR_ENOMEM;
241384
+  unsigned char *buf = gcry_xcalloc (1, test->expectedlen);
241384
+  if (!buf)
241384
+    return GPG_ERR_ENOMEM;
241384
+
241384
+  ret = gcry_drbg_cavs_test (test, buf);
241384
+  ret = memcmp (test->expected, buf, test->expectedlen);
241384
+
241384
+  xfree (buf);
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * Tests as defined in 11.3.2 in addition to the cipher tests: testing
241384
+ * of the error handling.
241384
+ *
241384
+ * Note, testing the reseed counter is not done as an automatic reseeding
241384
+ * is performed in gcry_drbg_generate when the reseed counter is too large.
241384
+ */
241384
+static gpg_err_code_t
241384
+gcry_drbg_healthcheck_sanity (struct gcry_drbg_test_vector *test)
241384
+{
241384
+  unsigned int len = 0;
241384
+  struct gcry_drbg_state *drbg = NULL;
241384
+  gpg_err_code_t ret = GPG_ERR_GENERAL;
241384
+  gpg_err_code_t tmpret = GPG_ERR_GENERAL;
241384
+  struct gcry_drbg_test_data test_data;
241384
+  struct gcry_drbg_string addtl, testentropy;
241384
+  int coreref = 0;
241384
+  unsigned char *buf = NULL;
241384
+  size_t max_addtllen, max_request_bytes;
241384
+
241384
+  /* only perform test in FIPS mode */
241384
+  if (0 == fips_mode ())
241384
+    return 0;
241384
+
241384
+  buf = gcry_xcalloc (1, test->expectedlen);
241384
+  if (!buf)
241384
+    return GPG_ERR_ENOMEM;
241384
+  tmpret = gcry_drbg_algo_available (test->flags, &coreref);
241384
+  if (tmpret)
241384
+    goto outbuf;
241384
+  drbg = gcry_xcalloc (1, sizeof (struct gcry_drbg_state));
241384
+  if (!drbg)
241384
+    goto outbuf;
241384
+
241384
+  /* if the following tests fail, it is likely that there is a buffer
241384
+   * overflow and we get a SIGSEV */
241384
+  ret = gcry_drbg_instantiate (drbg, NULL, coreref, 1);
241384
+  if (ret)
241384
+    goto outbuf;
241384
+  max_addtllen = gcry_drbg_max_addtl ();
241384
+  max_request_bytes = gcry_drbg_max_request_bytes ();
241384
+  /* overflow addtllen with additonal info string */
241384
+  gcry_drbg_string_fill (&addtl, test->addtla, (max_addtllen + 1));
241384
+  len = gcry_drbg_generate (drbg, buf, test->expectedlen, &addtl);
241384
+  if (len)
241384
+    goto outdrbg;
241384
+
241384
+  /* overflow max_bits */
241384
+  len = gcry_drbg_generate (drbg, buf, (max_request_bytes + 1), NULL);
241384
+  if (len)
241384
+    goto outdrbg;
241384
+  gcry_drbg_uninstantiate (drbg);
241384
+
241384
+  /* test failing entropy source as defined in 11.3.2 */
241384
+  test_data.testentropy = NULL;
241384
+  test_data.fail_seed_source = 1;
241384
+  drbg->test_data = &test_data;
241384
+  tmpret = gcry_drbg_instantiate (drbg, NULL, coreref, 0);
241384
+  if (!tmpret)
241384
+    goto outdrbg;
241384
+  test_data.fail_seed_source = 0;
241384
+
241384
+  test_data.testentropy = &testentropy;
241384
+  gcry_drbg_string_fill (&testentropy, test->entropy, test->entropylen);
241384
+  /* overflow max addtllen with personalization string */
241384
+  tmpret = gcry_drbg_instantiate (drbg, &addtl, coreref, 0);
241384
+  if (!tmpret)
241384
+    goto outdrbg;
241384
+
241384
+  dbg (("DRBG: Sanity tests for failure code paths successfully completed\n"));
241384
+  ret = 0;
241384
+
241384
+outdrbg:
241384
+  gcry_drbg_uninstantiate (drbg);
241384
+outbuf:
241384
+  xfree (buf);
241384
+  xfree (drbg);
241384
+  return ret;
241384
+}
241384
+
241384
+/*
241384
+ * DRBG Healthcheck function as required in SP800-90A
241384
+ *
241384
+ * return:
241384
+ * 	0 on success (all tests pass)
241384
+ * 	>0 on error (return code indicate the number of failures)
241384
+ */
241384
+static int
241384
+gcry_drbg_healthcheck (void)
241384
+{
241384
+  int ret = 0;
241384
+  ret += gcry_drbg_healthcheck_one (&gcry_drbg_test_nopr[0]);
241384
+  ret += gcry_drbg_healthcheck_one (&gcry_drbg_test_nopr[1]);
241384
+  ret += gcry_drbg_healthcheck_one (&gcry_drbg_test_nopr[2]);
241384
+  ret += gcry_drbg_healthcheck_one (&gcry_drbg_test_pr[0]);
241384
+  ret += gcry_drbg_healthcheck_one (&gcry_drbg_test_pr[1]);
241384
+  ret += gcry_drbg_healthcheck_one (&gcry_drbg_test_pr[2]);
241384
+  ret += gcry_drbg_healthcheck_sanity (&gcry_drbg_test_nopr[0]);
241384
+  return ret;
241384
+}
241384
+
241384
+/* Run the self-tests.  */
241384
+gcry_error_t
241384
+_gcry_drbg_selftest (selftest_report_func_t report)
241384
+{
241384
+  gcry_err_code_t ec;
241384
+  const char *errtxt = NULL;
241384
+  gcry_drbg_lock ();
241384
+  if (0 != gcry_drbg_healthcheck ())
241384
+    errtxt = "RNG output does not match known value";
241384
+  gcry_drbg_unlock ();
241384
+  if (report && errtxt)
241384
+    report ("random", 0, "KAT", errtxt);
241384
+  ec = errtxt ? GPG_ERR_SELFTEST_FAILED : 0;
241384
+  return gpg_error (ec);
241384
+}
241384
+
241384
+/***************************************************************
241384
+ * Cipher invocations requested by DRBG
241384
+ ***************************************************************/
241384
+
241384
+static gpg_err_code_t
241384
+gcry_drbg_hmac (struct gcry_drbg_state *drbg, const unsigned char *key,
241384
+		unsigned char *outval, const struct gcry_drbg_string *buf)
241384
+{
241384
+  gpg_error_t err;
241384
+  gcry_md_hd_t hd;
241384
+
241384
+  if (key)
241384
+    {
241384
+      err =
241384
+	_gcry_md_open (&hd, drbg->core->backend_cipher, GCRY_MD_FLAG_HMAC);
241384
+      if (err)
241384
+	return err;
241384
+      err = _gcry_md_setkey (hd, key, gcry_drbg_statelen (drbg));
241384
+      if (err)
241384
+	return err;
241384
+    }
241384
+  else
241384
+    {
241384
+      err = _gcry_md_open (&hd, drbg->core->backend_cipher, 0);
241384
+      if (err)
241384
+	return err;
241384
+    }
241384
+  for (; NULL != buf; buf = buf->next)
241384
+    _gcry_md_write (hd, buf->buf, buf->len);
241384
+  gcry_md_final (hd);
241384
+  memcpy (outval, _gcry_md_read (hd, drbg->core->backend_cipher),
241384
+	  gcry_drbg_blocklen (drbg));
241384
+  _gcry_md_close (hd);
241384
+  return 0;
241384
+}
241384
+
241384
+static gpg_err_code_t
241384
+gcry_drbg_sym (struct gcry_drbg_state *drbg, const unsigned char *key,
241384
+	       unsigned char *outval, const struct gcry_drbg_string *buf)
241384
+{
241384
+  gpg_error_t err;
241384
+  gcry_cipher_hd_t hd;
241384
+
241384
+  if (gcry_drbg_blocklen (drbg) !=
241384
+      _gcry_cipher_get_algo_blklen (drbg->core->backend_cipher))
241384
+    return -GPG_ERR_NO_ERROR;
241384
+  if (gcry_drbg_blocklen (drbg) < buf->len)
241384
+    return -GPG_ERR_NO_ERROR;
241384
+  err =
241384
+    _gcry_cipher_open (&hd, drbg->core->backend_cipher, GCRY_CIPHER_MODE_ECB,
241384
+		       0);
241384
+  if (err)
241384
+    return err;
241384
+  err = _gcry_cipher_setkey (hd, key, gcry_drbg_keylen (drbg));
241384
+  if (err)
241384
+    {
241384
+      _gcry_cipher_close (hd);
241384
+      return err;
241384
+    }
241384
+  /* in is only component */
241384
+  _gcry_cipher_encrypt (hd, outval, gcry_drbg_blocklen (drbg), buf->buf,
241384
+			buf->len);
241384
+  _gcry_cipher_close (hd);
241384
+  return 0;
241384
+}
241384
diff -up libgcrypt-1.5.3/random/Makefile.am.drbg libgcrypt-1.5.3/random/Makefile.am
241384
--- libgcrypt-1.5.3/random/Makefile.am.drbg	2013-07-25 11:10:04.000000000 +0200
241384
+++ libgcrypt-1.5.3/random/Makefile.am	2014-09-26 17:00:16.051215890 +0200
241384
@@ -35,6 +35,7 @@ random.c random.h \
241384
 rand-internal.h \
241384
 random-csprng.c \
241384
 random-fips.c \
241384
+drbg.c \
241384
 rndhw.c
241384
 
241384
 if USE_RANDOM_DAEMON
241384
diff -up libgcrypt-1.5.3/random/Makefile.in.drbg libgcrypt-1.5.3/random/Makefile.in
241384
--- libgcrypt-1.5.3/random/Makefile.in.drbg	2013-07-25 11:22:44.000000000 +0200
241384
+++ libgcrypt-1.5.3/random/Makefile.in	2014-09-26 17:00:16.051215890 +0200
241384
@@ -91,10 +91,10 @@ CONFIG_CLEAN_VPATH_FILES =
241384
 LTLIBRARIES = $(noinst_LTLIBRARIES)
241384
 am__DEPENDENCIES_1 =
241384
 am__librandom_la_SOURCES_DIST = random.c random.h rand-internal.h \
241384
-	random-csprng.c random-fips.c rndhw.c random-daemon.c
241384
+	random-csprng.c random-fips.c drbg.c rndhw.c random-daemon.c
241384
 @USE_RANDOM_DAEMON_TRUE@am__objects_1 = random-daemon.lo
241384
 am_librandom_la_OBJECTS = random.lo random-csprng.lo random-fips.lo \
241384
-	rndhw.lo $(am__objects_1)
241384
+	drbg.lo rndhw.lo $(am__objects_1)
241384
 librandom_la_OBJECTS = $(am_librandom_la_OBJECTS)
241384
 DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
241384
 depcomp = $(SHELL) $(top_srcdir)/depcomp
241384
@@ -283,7 +283,7 @@ GCRYPT_MODULES = @GCRYPT_RANDOM@
241384
 librandom_la_DEPENDENCIES = $(GCRYPT_MODULES)
241384
 librandom_la_LIBADD = $(GCRYPT_MODULES)
241384
 librandom_la_SOURCES = random.c random.h rand-internal.h \
241384
-	random-csprng.c random-fips.c rndhw.c $(am__append_1)
241384
+	random-csprng.c random-fips.c drbg.c rndhw.c $(am__append_1)
241384
 EXTRA_librandom_la_SOURCES = \
241384
 rndlinux.c \
241384
 rndegd.c \
241384
@@ -346,6 +346,7 @@ distclean-compile:
241384
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/random-csprng.Plo@am__quote@
241384
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/random-daemon.Plo@am__quote@
241384
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/random-fips.Plo@am__quote@
241384
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/drbg.Plo@am__quote@
241384
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/random.Plo@am__quote@
241384
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rndegd.Plo@am__quote@
241384
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rndhw.Plo@am__quote@
241384
diff -up libgcrypt-1.5.3/random/rand-internal.h.drbg libgcrypt-1.5.3/random/rand-internal.h
241384
--- libgcrypt-1.5.3/random/rand-internal.h.drbg	2013-07-25 11:10:04.000000000 +0200
241384
+++ libgcrypt-1.5.3/random/rand-internal.h	2014-09-26 17:00:16.051215890 +0200
241384
@@ -91,6 +91,15 @@ gcry_err_code_t _gcry_rngfips_run_extern
241384
                                                  char *buffer, size_t buflen);
241384
 void _gcry_rngfips_deinit_external_test (void *context);
241384
 
241384
+/* drbg-gcry.h */
241384
+void _gcry_drbg_init(int full);
241384
+void _gcry_drbg_close_fds(void);
241384
+void _gcry_drbg_dump_stats(void);
241384
+int _gcry_drbg_is_faked (void);
241384
+gcry_error_t _gcry_drbg_add_bytes (const void *buf, size_t buflen, int quality);
241384
+void _gcry_drbg_randomize (void *buffer, size_t length,
241384
+			   enum gcry_random_level level);
241384
+gcry_error_t _gcry_drbg_selftest (selftest_report_func_t report);
241384
 
241384
 
241384
 
241384
diff -up libgcrypt-1.5.3/random/random.c.drbg libgcrypt-1.5.3/random/random.c
241384
--- libgcrypt-1.5.3/random/random.c.drbg	2013-07-25 11:10:04.000000000 +0200
241384
+++ libgcrypt-1.5.3/random/random.c	2014-09-26 17:00:16.051215890 +0200
241384
@@ -76,7 +76,7 @@ void
241384
 _gcry_random_initialize (int full)
241384
 {
241384
   if (fips_mode ())
241384
-    _gcry_rngfips_initialize (full);
241384
+    _gcry_drbg_init (full);
241384
   else
241384
     _gcry_rngcsprng_initialize (full);
241384
 }
241384
@@ -86,7 +86,7 @@ void
241384
 _gcry_random_dump_stats (void)
241384
 {
241384
   if (fips_mode ())
241384
-    _gcry_rngfips_dump_stats ();
241384
+    _gcry_drbg_dump_stats ();
241384
   else
241384
     _gcry_rngcsprng_dump_stats ();
241384
 }
241384
@@ -145,7 +145,7 @@ int
241384
 _gcry_random_is_faked (void)
241384
 {
241384
   if (fips_mode ())
241384
-    return _gcry_rngfips_is_faked ();
241384
+    return _gcry_drbg_is_faked ();
241384
   else
241384
     return _gcry_rngcsprng_is_faked ();
241384
 }
241384
@@ -169,7 +169,7 @@ static void
241384
 do_randomize (void *buffer, size_t length, enum gcry_random_level level)
241384
 {
241384
   if (fips_mode ())
241384
-    _gcry_rngfips_randomize (buffer, length, level);
241384
+    _gcry_drbg_randomize (buffer, length, level);
241384
   else
241384
     _gcry_rngcsprng_randomize (buffer, length, level);
241384
 }
241384
@@ -266,7 +266,7 @@ void
241384
 gcry_create_nonce (void *buffer, size_t length)
241384
 {
241384
   if (fips_mode ())
241384
-    _gcry_rngfips_create_nonce (buffer, length);
241384
+    _gcry_drbg_randomize (buffer, length, GCRY_WEAK_RANDOM);
241384
   else
241384
     _gcry_rngcsprng_create_nonce (buffer, length);
241384
 }
241384
@@ -278,7 +278,7 @@ gpg_error_t
241384
 _gcry_random_selftest (selftest_report_func_t report)
241384
 {
241384
   if (fips_mode ())
241384
-    return _gcry_rngfips_selftest (report);
241384
+    return _gcry_drbg_selftest (report);
241384
   else
241384
     return 0; /* No selftests yet.  */
241384
 }
241384
@@ -294,13 +294,7 @@ _gcry_random_init_external_test (void **
241384
                                  const void *seed, size_t seedlen,
241384
                                  const void *dt, size_t dtlen)
241384
 {
241384
-  (void)flags;
241384
-  if (fips_mode ())
241384
-    return _gcry_rngfips_init_external_test (r_context, flags, key, keylen,
241384
-                                             seed, seedlen,
241384
-                                             dt, dtlen);
241384
-  else
241384
-    return GPG_ERR_NOT_SUPPORTED;
241384
+  return GPG_ERR_NOT_SUPPORTED;
241384
 }
241384
 
241384
 /* Get BUFLEN bytes from the RNG using the test CONTEXT and store them
241384
@@ -308,16 +302,12 @@ _gcry_random_init_external_test (void **
241384
 gcry_err_code_t
241384
 _gcry_random_run_external_test (void *context, char *buffer, size_t buflen)
241384
 {
241384
-  if (fips_mode ())
241384
-    return _gcry_rngfips_run_external_test (context, buffer, buflen);
241384
-  else
241384
-    return GPG_ERR_NOT_SUPPORTED;
241384
+  return GPG_ERR_NOT_SUPPORTED;
241384
 }
241384
 
241384
 /* Release the test CONTEXT.  */
241384
 void
241384
 _gcry_random_deinit_external_test (void *context)
241384
 {
241384
-  if (fips_mode ())
241384
-    _gcry_rngfips_deinit_external_test (context);
241384
+  return;
241384
 }
241384
diff -up libgcrypt-1.5.3/random/random.h.drbg libgcrypt-1.5.3/random/random.h
241384
--- libgcrypt-1.5.3/random/random.h.drbg	2013-07-25 11:10:04.000000000 +0200
241384
+++ libgcrypt-1.5.3/random/random.h	2014-09-26 17:00:16.051215890 +0200
241384
@@ -51,7 +51,29 @@ gcry_err_code_t _gcry_random_run_externa
241384
                                                 char *buffer, size_t buflen);
241384
 void            _gcry_random_deinit_external_test (void *context);
241384
 
241384
+/*-- drbg.c --*/
241384
+gpg_err_code_t _gcry_drbg_reinit (u32 flags, struct gcry_drbg_string *pers);
241384
+/* private interfaces for testing of DRBG */
241384
+struct gcry_drbg_test_vector
241384
+{
241384
+  u32 flags;
241384
+  unsigned char *entropy;
241384
+  size_t entropylen;
241384
+  unsigned char *entpra;
241384
+  unsigned char *entprb;
241384
+  size_t entprlen;
241384
+  unsigned char *addtla;
241384
+  unsigned char *addtlb;
241384
+  size_t addtllen;
241384
+  unsigned char *pers;
241384
+  size_t perslen;
241384
+  unsigned char *expected;
241384
+  size_t expectedlen;
241384
+};
241384
 
241384
+gpg_err_code_t gcry_drbg_cavs_test (struct gcry_drbg_test_vector *test,
241384
+				    unsigned char *buf);
241384
+gpg_err_code_t gcry_drbg_healthcheck_one (struct gcry_drbg_test_vector *test);
241384
 /*-- rndegd.c --*/
241384
 gpg_error_t _gcry_rndegd_set_socket_name (const char *name);
241384
 
241384
diff -up libgcrypt-1.5.3/src/gcrypt.h.in.drbg libgcrypt-1.5.3/src/gcrypt.h.in
241384
--- libgcrypt-1.5.3/src/gcrypt.h.in.drbg	2013-07-25 11:10:04.000000000 +0200
241384
+++ libgcrypt-1.5.3/src/gcrypt.h.in	2014-09-26 17:16:30.965224999 +0200
241384
@@ -423,7 +423,9 @@ enum gcry_ctl_cmds
241384
     GCRYCTL_SELFTEST = 57,
241384
     /* Note: 58 .. 62 are used internally.  */
241384
     GCRYCTL_DISABLE_HWF = 63,
241384
-    GCRYCTL_SET_ENFORCED_FIPS_FLAG = 64
241384
+    GCRYCTL_SET_ENFORCED_FIPS_FLAG = 64,
241384
+    GCRYCTL_DRBG_REINIT = 74
241384
+    /* Note: 75 is used internally */
241384
   };
241384
 
241384
 /* Perform various operations defined by CMD. */
241384
@@ -1907,6 +1909,109 @@ int gcry_is_secure (const void *a) _GCRY
241384
 /* Return true if Libgcrypt is in FIPS mode.  */
241384
 #define gcry_fips_mode_active()  !!gcry_control (GCRYCTL_FIPS_MODE_P, 0)
241384
 
241384
+/* DRBG input data structure for DRBG generate with additional information
241384
+ * string */
241384
+struct gcry_drbg_gen {
241384
+	unsigned char *outbuf;	/* output buffer for random numbers */
241384
+	unsigned int outlen;	/* size of output buffer */
241384
+	struct gcry_drbg_string *addtl;	/* input buffer for
241384
+					 * additional information string */
241384
+};
241384
+
241384
+/*
241384
+ * Concatenation Helper and string operation helper
241384
+ *
241384
+ * SP800-90A requires the concatenation of different data. To avoid copying
241384
+ * buffers around or allocate additional memory, the following data structure
241384
+ * is used to point to the original memory with its size. In addition, it
241384
+ * is used to build a linked list. The linked list defines the concatenation
241384
+ * of individual buffers. The order of memory block referenced in that
241384
+ * linked list determines the order of concatenation.
241384
+ */
241384
+/* DRBG string definition */
241384
+struct gcry_drbg_string {
241384
+	const unsigned char *buf;
241384
+	size_t len;
241384
+	struct gcry_drbg_string *next;
241384
+};
241384
+
241384
+#define gcry_drbg_string_fill(s, b, l) \
241384
+do { \
241384
+	(s)->buf = (b); \
241384
+	(s)->len = (l); \
241384
+	(s)->next = NULL; \
241384
+} while (0)
241384
+
241384
+/* this is a wrapper function for users of libgcrypt */
241384
+#define gcry_randomize_drbg(ob, ol, lvl, add) \
241384
+do { \
241384
+	struct gcry_drbg_gen genbuf; \
241384
+	genbuf.outbuf = (unsigned char *)(ob); \
241384
+	genbuf.outlen = (ol); \
241384
+	genbuf.addtl = (add); \
241384
+	gcry_randomize(&genbuf, 0, (lvl)); \
241384
+} while (0)
241384
+
241384
+/*
241384
+ * DRBG flags bitmasks
241384
+ *
241384
+ * 31 (B) 28      19         (A)         0
241384
+ *  +-+-+-+--------+---+-----------+-----+
241384
+ *  |~|~|u|~~~~~~~~| 3 |     2     |  1  |
241384
+ *  +-+-+-+--------+- -+-----------+-----+
241384
+ * ctl flg|        |drbg use selection flags
241384
+ *
241384
+ */
241384
+
241384
+/* internal state control flags (B) */
241384
+#define GCRY_DRBG_PREDICTION_RESIST	((u_int32_t)1<<28)
241384
+
241384
+/* CTR type modifiers (A.1)*/
241384
+#define GCRY_DRBG_CTRAES		((u_int32_t)1<<0)
241384
+#define GCRY_DRBG_CTRSERPENT		((u_int32_t)1<<1)
241384
+#define GCRY_DRBG_CTRTWOFISH		((u_int32_t)1<<2)
241384
+#define GCRY_DRBG_CTR_MASK	(GCRY_DRBG_CTRAES | GCRY_DRBG_CTRSERPENT | GCRY_DRBG_CTRTWOFISH)
241384
+
241384
+/* HASH type modifiers (A.2)*/
241384
+#define GCRY_DRBG_HASHSHA1		((u_int32_t)1<<4)
241384
+#define GCRY_DRBG_HASHSHA224		((u_int32_t)1<<5)
241384
+#define GCRY_DRBG_HASHSHA256		((u_int32_t)1<<6)
241384
+#define GCRY_DRBG_HASHSHA384		((u_int32_t)1<<7)
241384
+#define GCRY_DRBG_HASHSHA512		((u_int32_t)1<<8)
241384
+#define GCRY_DRBG_HASH_MASK		(GCRY_DRBG_HASHSHA1 | GCRY_DRBG_HASHSHA224 | \
241384
+				 GCRY_DRBG_HASHSHA256 | GCRY_DRBG_HASHSHA384 | \
241384
+				 GCRY_DRBG_HASHSHA512)
241384
+/* type modifiers (A.3)*/
241384
+#define GCRY_DRBG_HMAC		((u_int32_t)1<<12)
241384
+#define GCRY_DRBG_SYM128		((u_int32_t)1<<13)
241384
+#define GCRY_DRBG_SYM192		((u_int32_t)1<<14)
241384
+#define GCRY_DRBG_SYM256		((u_int32_t)1<<15)
241384
+#define GCRY_DRBG_TYPE_MASK		(GCRY_DRBG_HMAC | GCRY_DRBG_SYM128 | GCRY_DRBG_SYM192 | \
241384
+				 GCRY_DRBG_SYM256)
241384
+#define GCRY_DRBG_CIPHER_MASK (GCRY_DRBG_CTR_MASK | GCRY_DRBG_HASH_MASK | GCRY_DRBG_TYPE_MASK)
241384
+
241384
+#define GCRY_DRBG_PR_CTRAES128   (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_CTRAES | GCRY_DRBG_SYM128)
241384
+#define GCRY_DRBG_PR_CTRAES192   (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_CTRAES | GCRY_DRBG_SYM192)
241384
+#define GCRY_DRBG_PR_CTRAES256   (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_CTRAES | GCRY_DRBG_SYM256)
241384
+#define GCRY_DRBG_NOPR_CTRAES128 (GCRY_DRBG_CTRAES | GCRY_DRBG_SYM128)
241384
+#define GCRY_DRBG_NOPR_CTRAES192 (GCRY_DRBG_CTRAES | GCRY_DRBG_SYM192)
241384
+#define GCRY_DRBG_NOPR_CTRAES256 (GCRY_DRBG_CTRAES | GCRY_DRBG_SYM256)
241384
+#define GCRY_DRBG_PR_HASHSHA1    (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_HASHSHA1)
241384
+#define GCRY_DRBG_PR_HASHSHA256  (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_HASHSHA256)
241384
+#define GCRY_DRBG_PR_HASHSHA384  (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_HASHSHA384)
241384
+#define GCRY_DRBG_PR_HASHSHA512  (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_HASHSHA512)
241384
+#define GCRY_DRBG_NOPR_HASHSHA1  (GCRY_DRBG_HASHSHA1)
241384
+#define GCRY_DRBG_NOPR_HASHSHA256 (GCRY_DRBG_HASHSHA256)
241384
+#define GCRY_DRBG_NOPR_HASHSHA384 (GCRY_DRBG_HASHSHA384)
241384
+#define GCRY_DRBG_NOPR_HASHSHA512 (GCRY_DRBG_HASHSHA512)
241384
+#define GCRY_DRBG_PR_HMACSHA1    (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_HASHSHA1 | GCRY_DRBG_HMAC)
241384
+#define GCRY_DRBG_PR_HMACSHA256  (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_HASHSHA256 | GCRY_DRBG_HMAC)
241384
+#define GCRY_DRBG_PR_HMACSHA384  (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_HASHSHA384 | GCRY_DRBG_HMAC)
241384
+#define GCRY_DRBG_PR_HMACSHA512  (GCRY_DRBG_PREDICTION_RESIST | GCRY_DRBG_HASHSHA512 | GCRY_DRBG_HMAC)
241384
+#define GCRY_DRBG_NOPR_HMACSHA1  (GCRY_DRBG_HASHSHA1 | GCRY_DRBG_HMAC)
241384
+#define GCRY_DRBG_NOPR_HMACSHA256 (GCRY_DRBG_HASHSHA256 | GCRY_DRBG_HMAC)
241384
+#define GCRY_DRBG_NOPR_HMACSHA384 (GCRY_DRBG_HASHSHA384 | GCRY_DRBG_HMAC)
241384
+#define DRBG_NOPR_HMACSHA512 (GCRY_DRBG_HASHSHA512 | GCRY_DRBG_HMAC)
241384
 
241384
 /* Include support for Libgcrypt modules.  */
241384
 #include <gcrypt-module.h>
241384
diff -up libgcrypt-1.5.3/src/global.c.drbg libgcrypt-1.5.3/src/global.c
241384
--- libgcrypt-1.5.3/src/global.c.drbg	2013-07-25 11:10:04.000000000 +0200
241384
+++ libgcrypt-1.5.3/src/global.c	2014-09-26 17:00:16.052215913 +0200
241384
@@ -609,6 +609,28 @@ _gcry_vcontrol (enum gcry_ctl_cmds cmd,
241384
         err = GPG_ERR_GENERAL;
241384
       break;
241384
 
241384
+    case GCRYCTL_DRBG_REINIT:
241384
+      {
241384
+        u32 flags = va_arg (arg_ptr, u32);
241384
+        struct gcry_drbg_string *pers = va_arg (arg_ptr,
241384
+						struct gcry_drbg_string *);
241384
+        err = _gcry_drbg_reinit(flags, pers);
241384
+      }
241384
+      break;
241384
+
241384
+    case 75:
241384
+      {
241384
+        struct gcry_drbg_test_vector *test =
241384
+	  va_arg (arg_ptr, struct gcry_drbg_test_vector *);
241384
+        unsigned char *buf = va_arg (arg_ptr, unsigned char *);
241384
+
241384
+        if (buf)
241384
+          err = gcry_drbg_cavs_test (test, buf);
241384
+        else
241384
+          err = gcry_drbg_healthcheck_one (test);
241384
+      }
241384
+      break;
241384
+
241384
     default:
241384
       err = GPG_ERR_INV_OP;
241384
     }