base/dispatch.c, rand/rand.c, and asm: Support `rdseed' for quick noise.
[catacomb] / rand / rand.c
index c2540f7..3b05633 100644 (file)
@@ -163,11 +163,14 @@ static int trivial_quick(rand_pool *r) { return (-1); }
 
 #if CPUFAM_X86 || CPUFAM_AMD64
 extern int rand_quick_x86ish_rdrand(rand_pool */*r*/);
+extern int rand_quick_x86ish_rdseed(rand_pool */*r*/);
 #endif
 
 static quick__functype *pick_quick(void)
 {
 #if CPUFAM_X86 || CPUFAM_AMD64
+  DISPATCH_PICK_COND(rand_quick, rand_quick_x86ish_rdseed,
+                    cpu_feature_p(CPUFEAT_X86_RDSEED));
   DISPATCH_PICK_COND(rand_quick, rand_quick_x86ish_rdrand,
                     cpu_feature_p(CPUFEAT_X86_RDRAND));
 #endif
@@ -251,9 +254,7 @@ void rand_add(rand_pool *r, const void *p, size_t sz, unsigned goodbits)
   const octet *c = p;
   int i, rot;
 
-#if RAND_POOLSZ != 128
-#  error Polynomial in rand_add is out of date.  Fix it.
-#endif
+  STATIC_ASSERT(RAND_POOLSZ == 128, "Polynomial doesn't match pool size");
 
   RAND_RESOLVE(r);
 
@@ -306,6 +307,8 @@ void rand_gate(rand_pool *r)
   HASH_CTX hc;
   CIPHER_CTX cc;
 
+  STATIC_ASSERT(CIPHER_KEYSZ <= HASH_SZ, "rand cipher keysize too long");
+
   RAND_RESOLVE(r);
   QUICK(r);
 
@@ -313,6 +316,7 @@ void rand_gate(rand_pool *r)
 
   HASH_INIT(&hc);
   STORE32(g, r->gen); HASH(&hc, g, sizeof(g));
+  HASH(&hc, r->k.k, RAND_KEYSZ);
   HASH(&hc, r->pool, RAND_POOLSZ);
   HASH(&hc, r->buf, RAND_BUFSZ);
   HASH_DONE(&hc, h);
@@ -320,7 +324,6 @@ void rand_gate(rand_pool *r)
 
   /* --- Now mangle all of the data based on the hash --- */
 
-  assert(CIPHER_KEYSZ <= HASH_SZ);
   CIPHER_INIT(&cc, h, CIPHER_KEYSZ, 0);
   CIPHER_ENCRYPT(&cc, r->pool, r->pool, RAND_POOLSZ);
   CIPHER_ENCRYPT(&cc, r->buf, r->buf, RAND_BUFSZ);
@@ -356,6 +359,8 @@ void rand_stretch(rand_pool *r)
   HASH_CTX hc;
   CIPHER_CTX cc;
 
+  STATIC_ASSERT(CIPHER_KEYSZ <= HASH_SZ, "rand cipher keysize too long");
+
   RAND_RESOLVE(r);
   QUICK(r);
 
@@ -363,6 +368,7 @@ void rand_stretch(rand_pool *r)
 
   HASH_INIT(&hc);
   STORE32(g, r->gen); HASH(&hc, g, sizeof(g));
+  HASH(&hc, r->k.k, RAND_KEYSZ);
   HASH(&hc, r->pool, RAND_POOLSZ);
   HASH(&hc, r->buf, RAND_BUFSZ);
   HASH_DONE(&hc, h);
@@ -370,7 +376,6 @@ void rand_stretch(rand_pool *r)
 
   /* --- Now mangle the buffer based on the hash --- */
 
-  assert(CIPHER_KEYSZ <= HASH_SZ);
   CIPHER_INIT(&cc, h, CIPHER_KEYSZ, 0);
   CIPHER_ENCRYPT(&cc, r->buf, r->buf, RAND_BUFSZ);
   BURN(cc);