Add an internal-representation no-op function.
[u/mdw/catacomb] / fipstest.c
CommitLineData
44d4e7aa 1/* -*-c-*-
2 *
5d14574b 3 * $Id: fipstest.c,v 1.3 2000/08/11 21:34:34 mdw Exp $
44d4e7aa 4 *
5d14574b 5 * FIPS140 randomness tests
44d4e7aa 6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: fipstest.c,v $
5d14574b 33 * Revision 1.3 2000/08/11 21:34:34 mdw
34 * Change to use the new thresholds given in the draft FIPS140-2.
35 *
70ec0ae1 36 * Revision 1.2 2000/06/17 12:21:39 mdw
37 * Add braces to shut compiler up. Reformat code slightly.
38 *
44d4e7aa 39 * Revision 1.1 2000/06/17 10:55:38 mdw
40 * FIPS 140-1 random generator test.
41 *
42 */
43
44/*----- Header files ------------------------------------------------------*/
45
46#include <mLib/bits.h>
47
48#include "fipstest.h"
49
50/*----- Main code ---------------------------------------------------------*/
51
52/* --- @monobit@ --- *
53 *
54 * Arguments: @const octet *p@ = pointer to buffer
55 *
56 * Returns: Zero if OK, @FIPSTEST_MONOBIT@ on failure.
57 *
58 * Use: Performs the monobit test on a buffer of data. If %$n_1$% is
59 * the number of 1 bits in the buffer, then the monobit test is
60 * passed when %$9654 < n_1 < 10346$%.
61 */
62
63static unsigned monobit(const octet *p)
64{
65 unsigned n1 = 0;
66 unsigned i, j;
67
68 for (i = 0; i < FIPSTEST_BUFSZ; i++) {
69 octet x = p[i];
70 for (j = 0; j < 8; j++) {
71 if (x & 1)
72 n1++;
73 x >>= 1;
74 }
75 }
76
5d14574b 77 if (9725 >= n1 || n1 >= 10275)
44d4e7aa 78 return (FIPSTEST_MONOBIT);
79 return (0);
80}
81
82/* --- @poker@ --- *
83 *
84 * Arguments: @const octet *p@ = pointer to buffer
85 *
86 * Returns: Zero if OK, @FIPSTEST_POKER@ on failure.
87 *
88 * Use: Performs the poker test on a buffer of data. The buffer is
89 * divided into 4-bit nibbles %$x_i$%. If
90 * %$f(x) = \sum_{x_i = x} 1$% is the frequency of each nibble,
91 * then the test is passed if
5d14574b 92 * %$2.16 < 16/5000 \sum_i f(i)^2 - 5000 < 46.17$%.
44d4e7aa 93 */
94
95static unsigned poker(const octet *p)
96{
97 unsigned long f[16] = { 0 };
98 unsigned i;
99 unsigned long q = 0;
100
101 /* --- Compute the frequencies --- */
102
103 for (i = 0; i < FIPSTEST_BUFSZ; i++) {
104 octet x = p[i];
105 f[x & 0xf]++;
106 f[(x >> 4) & 0xf]++;
107 }
108
109 /* --- Now do the comparison --- *
110 *
111 * This can be simplified. Multiply through the inequality by 5000 and
112 * we get %$5150 < 16 \sum_i f(i)^2 - 5000^2 < 287000$%.
113 */
114
115 for (i = 0; i < 16; i++)
116 q += f[i] * f[i];
117 q <<= 4;
5d14574b 118 q -= 5000ul * 5000ul;
44d4e7aa 119
5d14574b 120 if (10800 >= q || q >= 230850)
44d4e7aa 121 return (FIPSTEST_POKER);
122 return (0);
123}
124
125/* --- @runs@ --- *
126 *
127 * Arguments: @const octet *p@ = pointer to buffer
128 *
129 * Returns: Zero for success, @FIPSTEST_RUNS@ or @FIPSTEST_LONGRUNS@ on
130 * failure.
131 *
132 * Use: Performs the runs and long runs tests. The frequency of each
133 * `run', or sequence of equal bits, is counted and tested.
134 */
135
136static unsigned runs(const octet *p)
137{
138 unsigned rc = 0;
139 unsigned i, j;
140 unsigned r = 0;
141 unsigned bb = 0;
70ec0ae1 142 unsigned f[2][6] = { { 0 } };
44d4e7aa 143
144 /* --- Count the run lengths --- */
145
146 for (i = 0; i < FIPSTEST_BUFSZ; i++) {
147 octet x = p[i];
148 for (j = 0; j < 8; j++) {
149 unsigned b = x & 1;
150 x >>= 1;
151 if (b == bb)
152 r++;
153 else {
154 if (r) {
155 if (r >= 34)
156 rc |= FIPSTEST_LONGRUNS;
157 if (r > 6)
158 r = 6;
159 f[bb][r - 1]++;
160 }
161 r = 1;
162 bb = b;
163 }
164 }
165 }
166
5d14574b 167 if (r >= 26)
44d4e7aa 168 rc |= FIPSTEST_LONGRUNS;
169 if (r > 6)
170 r = 6;
171 f[bb][r - 1]++;
172
173 /* --- Check the results --- */
174
5d14574b 175 if (2343 > f[0][0] || f[0][0] > 2657 || 2343 > f[1][0] || f[1][0] > 2657 ||
176 1135 > f[0][1] || f[0][1] > 1365 || 1135 > f[1][1] || f[1][1] > 1365 ||
177 542 > f[0][2] || f[0][2] > 708 || 542 > f[1][2] || f[1][2] > 708 ||
178 251 > f[0][3] || f[0][3] > 373 || 251 > f[1][3] || f[1][3] > 373 ||
179 111 > f[0][4] || f[0][4] > 201 || 111 > f[1][4] || f[1][4] > 201 ||
180 111 > f[0][5] || f[0][5] > 201 || 111 > f[1][5] || f[1][5] > 201)
44d4e7aa 181 rc |= FIPSTEST_RUNS;
182
183 return (rc);
184}
185
186/* --- @fipstest@ --- *
187 *
188 * Arguments: @const octet *p@ = pointer to a buffer of @FIPSTEST_BUFSZ@
189 * bytes
190 *
191 * Returns: Zero if OK, or a bitmask of failed tests.
192 *
5d14574b 193 * Use: Performs the FIPS140 randomness tests on a block of data.
44d4e7aa 194 */
195
196unsigned fipstest(const octet *p)
197{
198 unsigned rc = 0;
199 rc |= monobit(p);
200 rc |= poker(p);
201 rc |= runs(p);
202 return (rc);
203}
204
205/*----- That's all, folks -------------------------------------------------*/