3 * FIPS140 randomness tests
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/bits.h>
34 /*----- Main code ---------------------------------------------------------*/
36 /* --- @monobit@ --- *
38 * Arguments: @const octet *p@ = pointer to buffer
40 * Returns: Zero if OK, @FIPSTEST_MONOBIT@ on failure.
42 * Use: Performs the monobit test on a buffer of data. If %$n_1$% is
43 * the number of 1 bits in the buffer, then the monobit test is
44 * passed when %$9654 < n_1 < 10346$%.
47 static unsigned monobit(const octet *p)
52 for (i = 0; i < FIPSTEST_BUFSZ; i++) {
54 for (j = 0; j < 8; j++) {
61 if (9725 >= n1 || n1 >= 10275)
62 return (FIPSTEST_MONOBIT);
68 * Arguments: @const octet *p@ = pointer to buffer
70 * Returns: Zero if OK, @FIPSTEST_POKER@ on failure.
72 * Use: Performs the poker test on a buffer of data. The buffer is
73 * divided into 4-bit nibbles %$x_i$%. If
74 * %$f(x) = \sum_{x_i = x} 1$% is the frequency of each nibble,
75 * then the test is passed if
76 * %$2.16 < 16/5000 \sum_i f(i)^2 - 5000 < 46.17$%.
79 static unsigned poker(const octet *p)
81 unsigned long f[16] = { 0 };
85 /* --- Compute the frequencies --- */
87 for (i = 0; i < FIPSTEST_BUFSZ; i++) {
93 /* --- Now do the comparison --- *
95 * This can be simplified. Multiply through the inequality by 5000 and
96 * we get %$5150 < 16 \sum_i f(i)^2 - 5000^2 < 287000$%.
99 for (i = 0; i < 16; i++)
102 q -= 5000ul * 5000ul;
104 if (10800 >= q || q >= 230850)
105 return (FIPSTEST_POKER);
111 * Arguments: @const octet *p@ = pointer to buffer
113 * Returns: Zero for success, @FIPSTEST_RUNS@ or @FIPSTEST_LONGRUNS@ on
116 * Use: Performs the runs and long runs tests. The frequency of each
117 * `run', or sequence of equal bits, is counted and tested.
120 static unsigned runs(const octet *p)
126 unsigned f[2][6] = { { 0 } };
128 /* --- Count the run lengths --- */
130 for (i = 0; i < FIPSTEST_BUFSZ; i++) {
132 for (j = 0; j < 8; j++) {
140 rc |= FIPSTEST_LONGRUNS;
152 rc |= FIPSTEST_LONGRUNS;
157 /* --- Check the results --- */
159 if (2343 > f[0][0] || f[0][0] > 2657 || 2343 > f[1][0] || f[1][0] > 2657 ||
160 1135 > f[0][1] || f[0][1] > 1365 || 1135 > f[1][1] || f[1][1] > 1365 ||
161 542 > f[0][2] || f[0][2] > 708 || 542 > f[1][2] || f[1][2] > 708 ||
162 251 > f[0][3] || f[0][3] > 373 || 251 > f[1][3] || f[1][3] > 373 ||
163 111 > f[0][4] || f[0][4] > 201 || 111 > f[1][4] || f[1][4] > 201 ||
164 111 > f[0][5] || f[0][5] > 201 || 111 > f[1][5] || f[1][5] > 201)
170 /* --- @fipstest@ --- *
172 * Arguments: @const octet *p@ = pointer to a buffer of @FIPSTEST_BUFSZ@
175 * Returns: Zero if OK, or a bitmask of failed tests.
177 * Use: Performs the FIPS140 randomness tests on a block of data.
180 unsigned fipstest(const octet *p)
189 /*----- That's all, folks -------------------------------------------------*/