44d4e7aa |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: fipstest.c,v 1.1 2000/06/17 10:55:38 mdw Exp $ |
4 | * |
5 | * FIPS 140-1 randomness tests |
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 $ |
33 | * Revision 1.1 2000/06/17 10:55:38 mdw |
34 | * FIPS 140-1 random generator test. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include <mLib/bits.h> |
41 | |
42 | #include "fipstest.h" |
43 | |
44 | /*----- Main code ---------------------------------------------------------*/ |
45 | |
46 | /* --- @monobit@ --- * |
47 | * |
48 | * Arguments: @const octet *p@ = pointer to buffer |
49 | * |
50 | * Returns: Zero if OK, @FIPSTEST_MONOBIT@ on failure. |
51 | * |
52 | * Use: Performs the monobit test on a buffer of data. If %$n_1$% is |
53 | * the number of 1 bits in the buffer, then the monobit test is |
54 | * passed when %$9654 < n_1 < 10346$%. |
55 | */ |
56 | |
57 | static unsigned monobit(const octet *p) |
58 | { |
59 | unsigned n1 = 0; |
60 | unsigned i, j; |
61 | |
62 | for (i = 0; i < FIPSTEST_BUFSZ; i++) { |
63 | octet x = p[i]; |
64 | for (j = 0; j < 8; j++) { |
65 | if (x & 1) |
66 | n1++; |
67 | x >>= 1; |
68 | } |
69 | } |
70 | |
71 | if (9654 >= n1 || n1 >= 10346) |
72 | return (FIPSTEST_MONOBIT); |
73 | return (0); |
74 | } |
75 | |
76 | /* --- @poker@ --- * |
77 | * |
78 | * Arguments: @const octet *p@ = pointer to buffer |
79 | * |
80 | * Returns: Zero if OK, @FIPSTEST_POKER@ on failure. |
81 | * |
82 | * Use: Performs the poker test on a buffer of data. The buffer is |
83 | * divided into 4-bit nibbles %$x_i$%. If |
84 | * %$f(x) = \sum_{x_i = x} 1$% is the frequency of each nibble, |
85 | * then the test is passed if |
86 | * %$1.03 < 16/5000 \sum_i f(i)^2 - 5000 < 57.4$%. |
87 | */ |
88 | |
89 | static unsigned poker(const octet *p) |
90 | { |
91 | unsigned long f[16] = { 0 }; |
92 | unsigned i; |
93 | unsigned long q = 0; |
94 | |
95 | /* --- Compute the frequencies --- */ |
96 | |
97 | for (i = 0; i < FIPSTEST_BUFSZ; i++) { |
98 | octet x = p[i]; |
99 | f[x & 0xf]++; |
100 | f[(x >> 4) & 0xf]++; |
101 | } |
102 | |
103 | /* --- Now do the comparison --- * |
104 | * |
105 | * This can be simplified. Multiply through the inequality by 5000 and |
106 | * we get %$5150 < 16 \sum_i f(i)^2 - 5000^2 < 287000$%. |
107 | */ |
108 | |
109 | for (i = 0; i < 16; i++) |
110 | q += f[i] * f[i]; |
111 | q <<= 4; |
112 | q -= 5000 * 5000; |
113 | |
114 | if (5150 >= q || q >= 287000) |
115 | return (FIPSTEST_POKER); |
116 | return (0); |
117 | } |
118 | |
119 | /* --- @runs@ --- * |
120 | * |
121 | * Arguments: @const octet *p@ = pointer to buffer |
122 | * |
123 | * Returns: Zero for success, @FIPSTEST_RUNS@ or @FIPSTEST_LONGRUNS@ on |
124 | * failure. |
125 | * |
126 | * Use: Performs the runs and long runs tests. The frequency of each |
127 | * `run', or sequence of equal bits, is counted and tested. |
128 | */ |
129 | |
130 | static unsigned runs(const octet *p) |
131 | { |
132 | unsigned rc = 0; |
133 | unsigned i, j; |
134 | unsigned r = 0; |
135 | unsigned bb = 0; |
136 | unsigned f[2][6] = { 0 }; |
137 | |
138 | /* --- Count the run lengths --- */ |
139 | |
140 | for (i = 0; i < FIPSTEST_BUFSZ; i++) { |
141 | octet x = p[i]; |
142 | for (j = 0; j < 8; j++) { |
143 | unsigned b = x & 1; |
144 | x >>= 1; |
145 | if (b == bb) |
146 | r++; |
147 | else { |
148 | if (r) { |
149 | if (r >= 34) |
150 | rc |= FIPSTEST_LONGRUNS; |
151 | if (r > 6) |
152 | r = 6; |
153 | f[bb][r - 1]++; |
154 | } |
155 | r = 1; |
156 | bb = b; |
157 | } |
158 | } |
159 | } |
160 | |
161 | if (r >= 34) |
162 | rc |= FIPSTEST_LONGRUNS; |
163 | if (r > 6) |
164 | r = 6; |
165 | f[bb][r - 1]++; |
166 | |
167 | /* --- Check the results --- */ |
168 | |
169 | if (2267 > f[0][0] || f[0][0] > 2733 || |
170 | 2267 > f[1][0] || f[1][0] > 2733 || |
171 | 1079 > f[0][1] || f[0][1] > 1421 || |
172 | 1079 > f[1][1] || f[1][1] > 1421 || |
173 | 502 > f[0][2] || f[0][2] > 748 || |
174 | 502 > f[1][2] || f[1][2] > 748 || |
175 | 223 > f[0][3] || f[0][3] > 402 || |
176 | 223 > f[1][3] || f[1][3] > 402 || |
177 | 90 > f[0][4] || f[0][4] > 223 || |
178 | 90 > f[1][4] || f[1][4] > 223 || |
179 | 90 > f[0][5] || f[0][5] > 223 || |
180 | 90 > f[1][5] || f[1][5] > 223) |
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 | * |
193 | * Use: Performs the FIPS 140-1 randomness tests on a block of data. |
194 | */ |
195 | |
196 | unsigned 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 -------------------------------------------------*/ |