Selaa lähdekoodia

[rng] Add ANS X9.82 mandatory continuous tests

ANS X9.82 specifies two mandatory continuous tests to be performed
upon the noise source.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 vuotta sitten
vanhempi
commit
a3b116cea1
2 muutettua tiedostoa jossa 296 lisäystä ja 1 poistoa
  1. 295
    1
      src/crypto/entropy.c
  2. 1
    0
      src/include/ipxe/errfile.h

+ 295
- 1
src/crypto/entropy.c Näytä tiedosto

@@ -30,10 +30,291 @@ FILE_LICENCE ( GPL2_OR_LATER );
30 30
 #include <stdint.h>
31 31
 #include <assert.h>
32 32
 #include <string.h>
33
+#include <errno.h>
33 34
 #include <ipxe/crypto.h>
34 35
 #include <ipxe/hash_df.h>
35 36
 #include <ipxe/entropy.h>
36 37
 
38
+/* Disambiguate the various error causes */
39
+#define EPIPE_REPETITION_COUNT_TEST \
40
+	__einfo_error ( EINFO_EPIPE_REPETITION_COUNT_TEST )
41
+#define EINFO_EPIPE_REPETITION_COUNT_TEST \
42
+	__einfo_uniqify ( EINFO_EPIPE, 0x01, "Repetition count test failed" )
43
+#define EPIPE_ADAPTIVE_PROPORTION_TEST \
44
+	__einfo_error ( EINFO_EPIPE_ADAPTIVE_PROPORTION_TEST )
45
+#define EINFO_EPIPE_ADAPTIVE_PROPORTION_TEST \
46
+	__einfo_uniqify ( EINFO_EPIPE, 0x02, "Adaptive proportion test failed" )
47
+
48
+/**
49
+ * Calculate cutoff value for the repetition count test
50
+ *
51
+ * @ret cutoff		Cutoff value
52
+ *
53
+ * This is the cutoff value for the Repetition Count Test defined in
54
+ * ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.2.
55
+ */
56
+static inline __attribute__ (( always_inline )) unsigned int
57
+repetition_count_cutoff ( void ) {
58
+	double max_repetitions;
59
+	unsigned int cutoff;
60
+
61
+	/* The cutoff formula for the repetition test is:
62
+	 *
63
+	 *   C = ( 1 + ( -log2(W) / H_min ) )
64
+	 *
65
+	 * where W is set at 2^(-30) (in ANS X9.82 Part 2 (October
66
+	 * 2011 Draft) Section 8.5.2.1.3.1).
67
+	 */
68
+	max_repetitions = ( 1 + ( 30 / min_entropy_per_sample() ) );
69
+
70
+	/* Round up to a whole number of repetitions.  We don't have
71
+	 * the ceil() function available, so do the rounding by hand.
72
+	 */
73
+	cutoff = max_repetitions;
74
+	if ( cutoff < max_repetitions )
75
+		cutoff++;
76
+	linker_assert ( ( cutoff >= max_repetitions ), rounding_error );
77
+
78
+	/* Floating-point operations are not allowed in iPXE since we
79
+	 * never set up a suitable environment.  Abort the build
80
+	 * unless the calculated number of repetitions is a
81
+	 * compile-time constant.
82
+	 */
83
+	linker_assert ( __builtin_constant_p ( cutoff ),
84
+			repetition_count_cutoff_not_constant );
85
+
86
+	return cutoff;
87
+}
88
+
89
+/**
90
+ * Perform repetition count test
91
+ *
92
+ * @v sample		Noise sample
93
+ * @ret rc		Return status code
94
+ *
95
+ * This is the Repetition Count Test defined in ANS X9.82 Part 2
96
+ * (October 2011 Draft) Section 8.5.2.1.2.
97
+ */
98
+static int repetition_count_test ( noise_sample_t sample ) {
99
+	static noise_sample_t most_recent_sample;
100
+	static unsigned int repetition_count = 0;
101
+
102
+	/* A = the most recently seen sample value
103
+	 * B = the number of times that value A has been seen in a row
104
+	 * C = the cutoff value above which the repetition test should fail
105
+	 */
106
+
107
+	/* 1.  For each new sample processed:
108
+	 *
109
+	 * (Note that the test for "repetition_count > 0" ensures that
110
+	 * the initial value of most_recent_sample is treated as being
111
+	 * undefined.)
112
+	 */
113
+	if ( ( sample == most_recent_sample ) && ( repetition_count > 0 ) ) {
114
+
115
+		/* a) If the new sample = A, then B is incremented by one. */
116
+		repetition_count++;
117
+
118
+		/*    i.  If B >= C, then an error condition is raised
119
+		 *        due to a failure of the test
120
+		 */
121
+		if ( repetition_count >= repetition_count_cutoff() )
122
+			return -EPIPE_REPETITION_COUNT_TEST;
123
+
124
+	} else {
125
+
126
+		/* b) Else:
127
+		 *    i.  A = new sample
128
+		 */
129
+		most_recent_sample = sample;
130
+
131
+		/*    ii. B = 1 */
132
+		repetition_count = 1;
133
+	}
134
+
135
+	return 0;
136
+}
137
+
138
+/**
139
+ * Window size for the adaptive proportion test
140
+ *
141
+ * ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.1.1 allows
142
+ * five possible window sizes: 16, 64, 256, 4096 and 65536.
143
+ *
144
+ * We expect to generate relatively few (<256) entropy samples during
145
+ * a typical iPXE run; the use of a large window size would mean that
146
+ * the test would never complete a single cycle.  We use a window size
147
+ * of 64, which is the smallest window size that permits values of
148
+ * H_min down to one bit per sample.
149
+ */
150
+#define ADAPTIVE_PROPORTION_WINDOW_SIZE 64
151
+
152
+/**
153
+ * Combine adaptive proportion test window size and min-entropy
154
+ *
155
+ * @v n			N (window size)
156
+ * @v h			H (min-entropy)
157
+ * @ret n_h		(N,H) combined value
158
+ */
159
+#define APC_N_H( n, h ) ( ( (n) << 8 ) | (h) )
160
+
161
+/**
162
+ * Define a row of the adaptive proportion cutoff table
163
+ *
164
+ * @v h			H (min-entropy)
165
+ * @v c16		Cutoff for N=16
166
+ * @v c64		Cutoff for N=64
167
+ * @v c256		Cutoff for N=256
168
+ * @v c4096		Cutoff for N=4096
169
+ * @v c65536		Cutoff for N=65536
170
+ */
171
+#define APC_TABLE_ROW( h, c16, c64, c256, c4096, c65536)	   \
172
+	case APC_N_H ( 16, h ) :	return c16;		   \
173
+	case APC_N_H ( 64, h ) :	return c64;   		   \
174
+	case APC_N_H ( 256, h ) :	return c256;		   \
175
+	case APC_N_H ( 4096, h ) :	return c4096;		   \
176
+	case APC_N_H ( 65536, h ) :	return c65536;
177
+
178
+/** Value used to represent "N/A" in adaptive proportion cutoff table */
179
+#define APC_NA 0
180
+
181
+/**
182
+ * Look up value in adaptive proportion test cutoff table
183
+ *
184
+ * @v n			N (window size)
185
+ * @v h			H (min-entropy)
186
+ * @ret cutoff		Cutoff
187
+ *
188
+ * This is the table of cutoff values defined in ANS X9.82 Part 2
189
+ * (October 2011 Draft) Section 8.5.2.1.3.1.2.
190
+ */
191
+static inline __attribute__ (( always_inline )) unsigned int
192
+adaptive_proportion_cutoff_lookup ( unsigned int n, unsigned int h ) {
193
+	switch ( APC_N_H ( n, h ) ) {
194
+		APC_TABLE_ROW (  1, APC_NA,     51,    168,   2240,  33537 );
195
+		APC_TABLE_ROW (  2, APC_NA,     35,    100,   1193,  17053 );
196
+		APC_TABLE_ROW (  3,     10,     24,     61,    643,   8705 );
197
+		APC_TABLE_ROW (  4,      8,     16,     38,    354,   4473 );
198
+		APC_TABLE_ROW (  5,      6,     12,     25,    200,   2321 );
199
+		APC_TABLE_ROW (  6,      5,      9,     17,    117,   1220 );
200
+		APC_TABLE_ROW (  7,      4,      7,     15,     71,    653 );
201
+		APC_TABLE_ROW (  8,      4,      5,      9,     45,    358 );
202
+		APC_TABLE_ROW (  9,      3,      4,      7,     30,    202 );
203
+		APC_TABLE_ROW ( 10,      3,      4,      5,     21,    118 );
204
+		APC_TABLE_ROW ( 11,      2,      3,      4,     15,     71 );
205
+		APC_TABLE_ROW ( 12,      2,      3,      4,     11,     45 );
206
+		APC_TABLE_ROW ( 13,      2,      2,      3,      9,     30 );
207
+		APC_TABLE_ROW ( 14,      2,      2,      3,      7,     21 );
208
+		APC_TABLE_ROW ( 15,      1,      2,      2,      6,     15 );
209
+		APC_TABLE_ROW ( 16,      1,      2,      2,      5,     11 );
210
+		APC_TABLE_ROW ( 17,      1,      1,      2,      4,      9 );
211
+		APC_TABLE_ROW ( 18,      1,      1,      2,      4,      7 );
212
+		APC_TABLE_ROW ( 19,      1,      1,      1,      3,      6 );
213
+		APC_TABLE_ROW ( 20,      1,      1,      1,      3,      5 );
214
+	default:
215
+		return APC_NA;
216
+	}
217
+}
218
+
219
+/**
220
+ * Calculate cutoff value for the adaptive proportion test
221
+ *
222
+ * @ret cutoff		Cutoff value
223
+ *
224
+ * This is the cutoff value for the Adaptive Proportion Test defined
225
+ * in ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.1.2.
226
+ */
227
+static inline __attribute__ (( always_inline )) unsigned int
228
+adaptive_proportion_cutoff ( void ) {
229
+	unsigned int h;
230
+	unsigned int n;
231
+	unsigned int cutoff;
232
+
233
+	/* Look up cutoff value in cutoff table */
234
+	n = ADAPTIVE_PROPORTION_WINDOW_SIZE;
235
+	h = min_entropy_per_sample();
236
+	cutoff = adaptive_proportion_cutoff_lookup ( n, h );
237
+
238
+	/* Fail unless cutoff value is a build-time constant */
239
+	linker_assert ( __builtin_constant_p ( cutoff ),
240
+			adaptive_proportion_cutoff_not_constant );
241
+
242
+	/* Fail if cutoff value is N/A */
243
+	linker_assert ( ( cutoff != APC_NA ),
244
+			adaptive_proportion_cutoff_not_applicable );
245
+
246
+	return cutoff;
247
+}
248
+
249
+/**
250
+ * Perform adaptive proportion test
251
+ *
252
+ * @v sample		Noise sample
253
+ * @ret rc		Return status code
254
+ *
255
+ * This is the Adaptive Proportion Test for the Most Common Value
256
+ * defined in ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.
257
+ */
258
+static int adaptive_proportion_test ( noise_sample_t sample ) {
259
+	static noise_sample_t current_counted_sample;
260
+	static unsigned int sample_count = ADAPTIVE_PROPORTION_WINDOW_SIZE;
261
+	static unsigned int repetition_count;
262
+
263
+	/* A = the sample value currently being counted
264
+	 * B = the number of samples examined in this run of the test so far
265
+	 * N = the total number of samples that must be observed in
266
+	 *     one run of the test, also known as the "window size" of
267
+	 *     the test
268
+	 * B = the current number of times that S (sic) has been seen
269
+	 *     in the W (sic) samples examined so far
270
+	 * C = the cutoff value above which the repetition test should fail
271
+	 * W = the probability of a false positive: 2^-30
272
+	 */
273
+
274
+	/* 1.  The entropy source draws the current sample from the
275
+	 *     noise source.
276
+	 *
277
+	 * (Nothing to do; we already have the current sample.)
278
+	 */
279
+
280
+	/* 2.  If S = N, then a new run of the test begins: */
281
+	if ( sample_count == ADAPTIVE_PROPORTION_WINDOW_SIZE ) {
282
+
283
+		/* a.  A = the current sample */
284
+		current_counted_sample = sample;
285
+
286
+		/* b.  S = 0 */
287
+		sample_count = 0;
288
+
289
+		/* c. B = 0 */
290
+		repetition_count = 0;
291
+
292
+	} else {
293
+
294
+		/* Else: (the test is already running)
295
+		 * a.  S = S + 1
296
+		 */
297
+		sample_count++;
298
+
299
+		/* b.  If A = the current sample, then: */
300
+		if ( sample == current_counted_sample ) {
301
+
302
+			/* i.   B = B + 1 */
303
+			repetition_count++;
304
+
305
+			/* ii.  If S (sic) > C then raise an error
306
+			 *      condition, because the test has
307
+			 *      detected a failure
308
+			 */
309
+			if ( repetition_count > adaptive_proportion_cutoff() )
310
+				return -EPIPE_ADAPTIVE_PROPORTION_TEST;
311
+
312
+		}
313
+	}
314
+
315
+	return 0;
316
+}
317
+
37 318
 /**
38 319
  * Get entropy sample
39 320
  *
@@ -44,13 +325,26 @@ FILE_LICENCE ( GPL2_OR_LATER );
44 325
  * (October 2011 Draft) Section 6.5.1.
45 326
  */
46 327
 static int get_entropy ( entropy_sample_t *entropy ) {
328
+	static int rc = 0;
47 329
 	noise_sample_t noise;
48
-	int rc;
330
+
331
+	/* Any failure is permanent */
332
+	if ( rc != 0 )
333
+		return rc;
49 334
 
50 335
 	/* Get noise sample */
51 336
 	if ( ( rc = get_noise ( &noise ) ) != 0 )
52 337
 		return rc;
53 338
 
339
+	/* Perform Repetition Count Test and Adaptive Proportion Test
340
+	 * as mandated by ANS X9.82 Part 2 (October 2011 Draft)
341
+	 * Section 8.5.2.1.1.
342
+	 */
343
+	if ( ( rc = repetition_count_test ( noise ) ) != 0 )
344
+		return rc;
345
+	if ( ( rc = adaptive_proportion_test ( noise ) ) != 0 )
346
+		return rc;
347
+
54 348
 	/* We do not use any optional conditioning component */
55 349
 	*entropy = noise;
56 350
 

+ 1
- 0
src/include/ipxe/errfile.h Näytä tiedosto

@@ -244,6 +244,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
244 244
 #define ERRFILE_nvo_cmd		      ( ERRFILE_OTHER | 0x00230000 )
245 245
 #define ERRFILE_hmac_drbg	      ( ERRFILE_OTHER | 0x00240000 )
246 246
 #define ERRFILE_drbg		      ( ERRFILE_OTHER | 0x00250000 )
247
+#define ERRFILE_entropy		      ( ERRFILE_OTHER | 0x00260000 )
247 248
 
248 249
 /** @} */
249 250
 

Loading…
Peruuta
Tallenna