Merge branch 'master' of git+ssh://metalzone.distorted.org.uk/~mdw/public-git/catacomb/
[u/mdw/catacomb] / fipstest.c
index 9febfd4..c780a33 100644 (file)
@@ -1,8 +1,8 @@
 /* -*-c-*-
  *
- * $Id: fipstest.c,v 1.2 2000/06/17 12:21:39 mdw Exp $
+ * $Id: fipstest.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
  *
- * FIPS 140-1 randomness tests
+ * FIPS140 randomness tests
  *
  * (c) 2000 Straylight/Edgeware
  */
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: fipstest.c,v $
- * Revision 1.2  2000/06/17 12:21:39  mdw
- * Add braces to shut compiler up.  Reformat code slightly.
- *
- * Revision 1.1  2000/06/17 10:55:38  mdw
- * FIPS 140-1 random generator test.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <mLib/bits.h>
@@ -71,7 +60,7 @@ static unsigned monobit(const octet *p)
     }
   }
 
-  if (9654 >= n1 || n1 >= 10346)
+  if (9725 >= n1 || n1 >= 10275)
     return (FIPSTEST_MONOBIT);
   return (0);
 }
@@ -86,7 +75,7 @@ static unsigned monobit(const octet *p)
  *             divided into 4-bit nibbles %$x_i$%.  If
  *             %$f(x) = \sum_{x_i = x} 1$% is the frequency of each nibble,
  *             then the test is passed if
- *             %$1.03 < 16/5000 \sum_i f(i)^2 - 5000 < 57.4$%.
+ *             %$2.16 < 16/5000 \sum_i f(i)^2 - 5000 < 46.17$%.
  */
 
 static unsigned poker(const octet *p)
@@ -112,9 +101,9 @@ static unsigned poker(const octet *p)
   for (i = 0; i < 16; i++)
     q += f[i] * f[i];
   q <<= 4;
-  q -= 5000 * 5000;
+  q -= 5000ul * 5000ul;
 
-  if (5150 >= q || q >= 287000)
+  if (10800 >= q || q >= 230850)
     return (FIPSTEST_POKER);
   return (0);
 }
@@ -161,7 +150,7 @@ static unsigned runs(const octet *p)
     }
   }
   
-  if (r >= 34)
+  if (r >= 26)
     rc |= FIPSTEST_LONGRUNS;
   if (r > 6)
     r = 6;
@@ -169,12 +158,12 @@ static unsigned runs(const octet *p)
 
   /* --- Check the results --- */
 
-  if (2267 > f[0][0] || f[0][0] > 2733 || 2267 > f[1][0] || f[1][0] > 2733 ||
-      1079 > f[0][1] || f[0][1] > 1421 || 1079 > f[1][1] || f[1][1] > 1421 ||
-       502 > f[0][2] || f[0][2] >  748 ||  502 > f[1][2] || f[1][2] >  748 ||
-       223 > f[0][3] || f[0][3] >  402 ||  223 > f[1][3] || f[1][3] >  402 ||
-        90 > f[0][4] || f[0][4] >  223 ||   90 > f[1][4] || f[1][4] >  223 ||
-        90 > f[0][5] || f[0][5] >  223 ||   90 > f[1][5] || f[1][5] >  223)
+  if (2343 > f[0][0] || f[0][0] > 2657 || 2343 > f[1][0] || f[1][0] > 2657 ||
+      1135 > f[0][1] || f[0][1] > 1365 || 1135 > f[1][1] || f[1][1] > 1365 ||
+       542 > f[0][2] || f[0][2] >  708 ||  542 > f[1][2] || f[1][2] >  708 ||
+       251 > f[0][3] || f[0][3] >  373 ||  251 > f[1][3] || f[1][3] >  373 ||
+       111 > f[0][4] || f[0][4] >  201 ||  111 > f[1][4] || f[1][4] >  201 ||
+       111 > f[0][5] || f[0][5] >  201 ||  111 > f[1][5] || f[1][5] >  201)
     rc |= FIPSTEST_RUNS;
 
   return (rc);      
@@ -187,7 +176,7 @@ static unsigned runs(const octet *p)
  *
  * Returns:    Zero if OK, or a bitmask of failed tests.
  *
- * Use:                Performs the FIPS 140-1 randomness tests on a block of data.
+ * Use:                Performs the FIPS140 randomness tests on a block of data.
  */
 
 unsigned fipstest(const octet *p)