gentleknife / rpms / libgcrypt

Forked from rpms/libgcrypt 4 years ago
Clone

Blame SOURCES/libgcrypt-1.8.3-fips-enttest.patch

5bcc82
diff -up libgcrypt-1.8.3/random/random-drbg.c.fips-enttest libgcrypt-1.8.3/random/random-drbg.c
5bcc82
--- libgcrypt-1.8.3/random/random-drbg.c.fips-enttest	2017-11-23 19:16:58.000000000 +0100
5bcc82
+++ libgcrypt-1.8.3/random/random-drbg.c	2019-06-24 10:04:23.219547141 +0200
5bcc82
@@ -317,6 +317,7 @@ struct drbg_state_s
5bcc82
   unsigned char *ctr_null;	/* CTR mode zero buffer */
5bcc82
   int seeded:1;			/* DRBG fully seeded? */
5bcc82
   int pr:1;			/* Prediction resistance enabled? */
5bcc82
+  int ent_primed:1;             /* Previous entropy data primed? */
5bcc82
   /* Taken from libgcrypt ANSI X9.31 DRNG: We need to keep track of the
5bcc82
    * process which did the initialization so that we can detect a fork.
5bcc82
    * The volatile modifier is required so that the compiler does not
5bcc82
@@ -324,6 +325,7 @@ struct drbg_state_s
5bcc82
   pid_t seed_init_pid;
5bcc82
   const struct drbg_state_ops_s *d_ops;
5bcc82
   const struct drbg_core_s *core;
5bcc82
+  unsigned char ent_hash[64];	/* Hash of previous entropy data */
5bcc82
   struct drbg_test_data_s *test_data;
5bcc82
 };
5bcc82
 
5bcc82
@@ -610,11 +612,13 @@ drbg_get_entropy (drbg_state_t drbg, uns
5bcc82
 		       size_t len)
5bcc82
 {
5bcc82
   int rc = 0;
5bcc82
+  unsigned char newhash[64];
5bcc82
 
5bcc82
   /* Perform testing as defined in 11.3.2 */
5bcc82
   if (drbg->test_data && drbg->test_data->fail_seed_source)
5bcc82
     return -1;
5bcc82
 
5bcc82
+redo:
5bcc82
   read_cb_buffer = buffer;
5bcc82
   read_cb_size = len;
5bcc82
   read_cb_len = 0;
5bcc82
@@ -634,6 +638,27 @@ drbg_get_entropy (drbg_state_t drbg, uns
5bcc82
 #else
5bcc82
   rc = -1;
5bcc82
 #endif
5bcc82
+
5bcc82
+  /* to avoid storing the actual entropy obtained for indefinite
5bcc82
+     time, we just store the SHA-512 hash of the entropy gathered
5bcc82
+   */
5bcc82
+  _gcry_md_hash_buffer (GCRY_MD_SHA512, newhash, buffer, len);
5bcc82
+
5bcc82
+  if (!drbg->ent_primed)
5bcc82
+    {
5bcc82
+      memcpy (drbg->ent_hash, newhash, sizeof (drbg->ent_hash));
5bcc82
+      drbg->ent_primed = 1;
5bcc82
+      goto redo;
5bcc82
+    }
5bcc82
+
5bcc82
+  if (memcmp (newhash, drbg->ent_hash, sizeof (drbg->ent_hash)) == 0)
5bcc82
+    {
5bcc82
+      fips_signal_error ("Entropy source failed the continuous test");
5bcc82
+      return -1;  /* continuous entropy test failed */
5bcc82
+    }
5bcc82
+
5bcc82
+  memcpy (drbg->ent_hash, newhash, sizeof (drbg->ent_hash));
5bcc82
+
5bcc82
   return rc;
5bcc82
 }
5bcc82
 
5bcc82
@@ -1341,26 +1366,38 @@ drbg_seed (drbg_state_t drbg, drbg_strin
5bcc82
     }
5bcc82
   else
5bcc82
     {
5bcc82
+      int nonce = 0;
5bcc82
       /* Gather entropy equal to the security strength of the DRBG.
5bcc82
        * With a derivation function, a nonce is required in addition
5bcc82
        * to the entropy. A nonce must be at least 1/2 of the security
5bcc82
        * strength of the DRBG in size. Thus, entropy * nonce is 3/2
5bcc82
        * of the strength. The consideration of a nonce is only
5bcc82
-       * applicable during initial seeding. */
5bcc82
+       * applicable during initial seeding.
5bcc82
+       * To avoid pulling different length of data from entropy
5bcc82
+       * source, we use 2 * strength for initial seeding. */
5bcc82
       entropylen = drbg_sec_strength (drbg->core->flags);
5bcc82
       if (!entropylen)
5bcc82
 	return GPG_ERR_GENERAL;
5bcc82
       if (0 == reseed)
5bcc82
-	/* make sure we round up strength/2 in
5bcc82
-	 * case it is not divisible by 2 */
5bcc82
-	entropylen = ((entropylen + 1) / 2) * 3;
5bcc82
+        {
5bcc82
+	  nonce = 1;
5bcc82
+        }
5bcc82
       dbg (("DRBG: (re)seeding with %lu bytes of entropy\n", entropylen));
5bcc82
-      entropy = xcalloc_secure (1, entropylen);
5bcc82
+      entropy = xcalloc_secure (nonce + 1, entropylen);
5bcc82
       if (!entropy)
5bcc82
 	return GPG_ERR_ENOMEM;
5bcc82
       ret = drbg_get_entropy (drbg, entropy, entropylen);
5bcc82
       if (ret)
5bcc82
 	goto out;
5bcc82
+      if (nonce)
5bcc82
+        {
5bcc82
+          ret = drbg_get_entropy (drbg, entropy + entropylen, entropylen);
5bcc82
+          if (ret)
5bcc82
+	    goto out;
5bcc82
+	  /* make sure we round up strength/2 in
5bcc82
+	   * case it is not divisible by 2 */
5bcc82
+ 	  entropylen = 2 * entropylen;
5bcc82
+        }
5bcc82
       drbg_string_fill (&data1, entropy, entropylen);
5bcc82
     }
5bcc82
 
5bcc82
@@ -1597,6 +1634,7 @@ drbg_instantiate (drbg_state_t drbg,
5bcc82
   drbg->core = &drbg_cores[coreref];
5bcc82
   drbg->pr = pr;
5bcc82
   drbg->seeded = 0;
5bcc82
+  drbg->ent_primed = 0;
5bcc82
   if (drbg->core->flags & DRBG_HMAC)
5bcc82
     drbg->d_ops = &drbg_hmac_ops;
5bcc82
   else if (drbg->core->flags & DRBG_HASH_MASK)