瀏覽代碼

[rng] Allow HMAC_DRBG to use multiple underlying hash algorithms

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 年之前
父節點
當前提交
b9d9c3f1d5
共有 4 個文件被更改,包括 442 次插入267 次删除
  1. 73
    59
      src/crypto/hmac_drbg.c
  2. 23
    7
      src/include/ipxe/drbg.h
  3. 130
    30
      src/include/ipxe/hmac_drbg.h
  4. 216
    171
      src/tests/hmac_drbg_test.c

+ 73
- 59
src/crypto/hmac_drbg.c 查看文件

@@ -46,6 +46,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
46 46
 /**
47 47
  * Update the HMAC_DRBG key
48 48
  *
49
+ * @v hash		Underlying hash algorithm
49 50
  * @v state		HMAC_DRBG internal state
50 51
  * @v data		Provided data
51 52
  * @v len		Length of provided data
@@ -57,40 +58,40 @@ FILE_LICENCE ( GPL2_OR_LATER );
57 58
  *
58 59
  * as used by hmac_drbg_update()
59 60
  */
60
-static void hmac_drbg_update_key ( struct hmac_drbg_state *state,
61
+static void hmac_drbg_update_key ( struct digest_algorithm *hash,
62
+				   struct hmac_drbg_state *state,
61 63
 				   const void *data, size_t len,
62 64
 				   const uint8_t single ) {
63
-	uint8_t context[HMAC_DRBG_CTX_SIZE];
64
-	size_t key_len = sizeof ( state->key );
65
+	uint8_t context[ hash->ctxsize ];
66
+	size_t out_len = hash->digestsize;
65 67
 
66
-	DBGC ( state, "HMAC_DRBG %p provided data :\n", state );
68
+	DBGC ( state, "HMAC_DRBG_%s %p provided data :\n", hash->name, state );
67 69
 	DBGC_HDA ( state, 0, data, len );
68 70
 
69 71
 	/* Sanity checks */
72
+	assert ( hash != NULL );
70 73
 	assert ( state != NULL );
71 74
 	assert ( ( data != NULL ) || ( len == 0 ) );
72 75
 	assert ( ( single == 0x00 ) || ( single == 0x01 ) );
73 76
 
74 77
 	/* K = HMAC ( K, V || single || provided_data ) */
75
-	hmac_init ( &hmac_drbg_algorithm, context, state->key, &key_len );
76
-	assert ( key_len == sizeof ( state->key ) );
77
-	hmac_update ( &hmac_drbg_algorithm, context,
78
-		      state->value, sizeof ( state->value ) );
79
-	hmac_update ( &hmac_drbg_algorithm, context,
80
-		      &single, sizeof ( single ) );
81
-	hmac_update ( &hmac_drbg_algorithm, context, data, len );
82
-	hmac_final ( &hmac_drbg_algorithm, context, state->key, &key_len,
83
-		     state->key );
84
-	assert ( key_len == sizeof ( state->key ) );
85
-
86
-	DBGC ( state, "HMAC_DRBG %p K = HMAC ( K, V || %#02x || "
87
-	       "provided_data ) :\n", state, single );
88
-	DBGC_HDA ( state, 0, state->key, sizeof ( state->key ) );
78
+	hmac_init ( hash, context, state->key, &out_len );
79
+	assert ( out_len == hash->digestsize );
80
+	hmac_update ( hash, context, state->value, out_len );
81
+	hmac_update ( hash, context, &single, sizeof ( single ) );
82
+	hmac_update ( hash, context, data, len );
83
+	hmac_final ( hash, context, state->key, &out_len, state->key );
84
+	assert ( out_len == hash->digestsize );
85
+
86
+	DBGC ( state, "HMAC_DRBG_%s %p K = HMAC ( K, V || %#02x || "
87
+	       "provided_data ) :\n", hash->name, state, single );
88
+	DBGC_HDA ( state, 0, state->key, out_len );
89 89
 }
90 90
 
91 91
 /**
92 92
  * Update the HMAC_DRBG value
93 93
  *
94
+ * @v hash		Underlying hash algorithm
94 95
  * @v state		HMAC_DRBG internal state
95 96
  * @v data		Provided data
96 97
  * @v len		Length of provided data
@@ -102,29 +103,31 @@ static void hmac_drbg_update_key ( struct hmac_drbg_state *state,
102 103
  *
103 104
  * as used by hmac_drbg_update() and hmac_drbg_generate()
104 105
  */
105
-static void hmac_drbg_update_value ( struct hmac_drbg_state *state ) {
106
-	uint8_t context[HMAC_DRBG_CTX_SIZE];
107
-	size_t key_len = sizeof ( state->key );
106
+static void hmac_drbg_update_value ( struct digest_algorithm *hash,
107
+				     struct hmac_drbg_state *state ) {
108
+	uint8_t context[ hash->ctxsize ];
109
+	size_t out_len = hash->digestsize;
108 110
 
109 111
 	/* Sanity checks */
112
+	assert ( hash != NULL );
110 113
 	assert ( state != NULL );
111 114
 
112 115
 	/* V = HMAC ( K, V ) */
113
-	hmac_init ( &hmac_drbg_algorithm, context, state->key, &key_len );
114
-	assert ( key_len == sizeof ( state->key ) );
115
-	hmac_update ( &hmac_drbg_algorithm, context,
116
-		      state->value, sizeof ( state->value ) );
117
-	hmac_final ( &hmac_drbg_algorithm, context, state->key, &key_len,
118
-		     state->value );
119
-	assert ( key_len == sizeof ( state->key ) );
120
-
121
-	DBGC ( state, "HMAC_DRBG %p V = HMAC ( K, V ) :\n", state );
122
-	DBGC_HDA ( state, 0, state->value, sizeof ( state->value ) );
116
+	hmac_init ( hash, context, state->key, &out_len );
117
+	assert ( out_len == hash->digestsize );
118
+	hmac_update ( hash, context, state->value, out_len );
119
+	hmac_final ( hash, context, state->key, &out_len, state->value );
120
+	assert ( out_len == hash->digestsize );
121
+
122
+	DBGC ( state, "HMAC_DRBG_%s %p V = HMAC ( K, V ) :\n",
123
+	       hash->name, state );
124
+	DBGC_HDA ( state, 0, state->value, out_len );
123 125
 }
124 126
 
125 127
 /**
126 128
  * Update HMAC_DRBG internal state
127 129
  *
130
+ * @v hash		Underlying hash algorithm
128 131
  * @v state		HMAC_DRBG internal state
129 132
  * @v data		Provided data
130 133
  * @v len		Length of provided data
@@ -135,30 +138,32 @@ static void hmac_drbg_update_value ( struct hmac_drbg_state *state ) {
135 138
  * The key and value are updated in-place within the HMAC_DRBG
136 139
  * internal state.
137 140
  */
138
-static void hmac_drbg_update ( struct hmac_drbg_state *state,
141
+static void hmac_drbg_update ( struct digest_algorithm *hash,
142
+			       struct hmac_drbg_state *state,
139 143
 			       const void *data, size_t len ) {
140 144
 
141
-	DBGC ( state, "HMAC_DRBG %p update\n", state );
145
+	DBGC ( state, "HMAC_DRBG_%s %p update\n", hash->name, state );
142 146
 
143 147
 	/* Sanity checks */
148
+	assert ( hash != NULL );
144 149
 	assert ( state != NULL );
145 150
 	assert ( ( data != NULL ) || ( len == 0 ) );
146 151
 
147 152
 	/* 1.  K = HMAC ( K, V || 0x00 || provided_data ) */
148
-	hmac_drbg_update_key ( state, data, len, 0x00 );
153
+	hmac_drbg_update_key ( hash, state, data, len, 0x00 );
149 154
 
150 155
 	/* 2.  V = HMAC ( K, V ) */
151
-	hmac_drbg_update_value ( state );
156
+	hmac_drbg_update_value ( hash, state );
152 157
 
153 158
 	/* 3.  If ( provided_data = Null ), then return K and V */
154 159
 	if ( ! len )
155 160
 		return;
156 161
 
157 162
 	/* 4.  K = HMAC ( K, V || 0x01 || provided_data ) */
158
-	hmac_drbg_update_key ( state, data, len, 0x01 );
163
+	hmac_drbg_update_key ( hash, state, data, len, 0x01 );
159 164
 
160 165
 	/* 5.  V = HMAC ( K, V ) */
161
-	hmac_drbg_update_value ( state );
166
+	hmac_drbg_update_value ( hash, state );
162 167
 
163 168
 	/* 6.  Return K and V */
164 169
 }
@@ -166,6 +171,7 @@ static void hmac_drbg_update ( struct hmac_drbg_state *state,
166 171
 /**
167 172
  * Instantiate HMAC_DRBG
168 173
  *
174
+ * @v hash		Underlying hash algorithm
169 175
  * @v state		HMAC_DRBG internal state to be initialised
170 176
  * @v entropy		Entropy input
171 177
  * @v entropy_len	Length of entropy input
@@ -184,17 +190,18 @@ static void hmac_drbg_update ( struct hmac_drbg_state *state,
184 190
  * The key, value and reseed counter are updated in-place within the
185 191
  * HMAC_DRBG internal state.
186 192
  */
187
-void hmac_drbg_instantiate ( struct hmac_drbg_state *state,
193
+void hmac_drbg_instantiate ( struct digest_algorithm *hash,
194
+			     struct hmac_drbg_state *state,
188 195
 			     const void *entropy, size_t entropy_len,
189 196
 			     const void *personal, size_t personal_len ){
197
+	size_t out_len = hash->digestsize;
190 198
 
191
-	DBGC ( state, "HMAC_DRBG %p instantiate\n", state );
199
+	DBGC ( state, "HMAC_DRBG_%s %p instantiate\n", hash->name, state );
192 200
 
193 201
 	/* Sanity checks */
202
+	assert ( hash != NULL );
194 203
 	assert ( state != NULL );
195 204
 	assert ( entropy != NULL );
196
-	assert ( ( 8 * entropy_len ) >=
197
-		 ( 3 * HMAC_DRBG_SECURITY_STRENGTH / 2 ) );
198 205
 	assert ( ( personal != NULL ) || ( personal_len == 0 ) );
199 206
 
200 207
 	/* 1.  seed_material = entropy_input || nonce ||
@@ -202,23 +209,24 @@ void hmac_drbg_instantiate ( struct hmac_drbg_state *state,
202 209
 	 */
203 210
 
204 211
 	/* 2.  Key = 0x00 00..00 */
205
-	memset ( state->key, 0x00, sizeof ( state->key ) );
212
+	memset ( state->key, 0x00, out_len );
206 213
 
207 214
 	/* 3.  V = 0x01 01...01 */
208
-	memset ( state->value, 0x01, sizeof ( state->value ) );
215
+	memset ( state->value, 0x01, out_len );
209 216
 
210 217
 	/* 4.  ( Key, V ) = HMAC_DBRG_Update ( seed_material, Key, V )
211 218
 	 * 5.  reseed_counter = 1
212 219
 	 * 6.  Return V, Key and reseed_counter as the
213 220
 	 *     initial_working_state
214 221
 	 */
215
-	hmac_drbg_reseed ( state, entropy, entropy_len,
222
+	hmac_drbg_reseed ( hash, state, entropy, entropy_len,
216 223
 			   personal, personal_len );
217 224
 }
218 225
 
219 226
 /**
220 227
  * Reseed HMAC_DRBG
221 228
  *
229
+ * @v hash		Underlying hash algorithm
222 230
  * @v state		HMAC_DRBG internal state
223 231
  * @v entropy		Entropy input
224 232
  * @v entropy_len	Length of entropy input
@@ -231,27 +239,29 @@ void hmac_drbg_instantiate ( struct hmac_drbg_state *state,
231 239
  * The key, value and reseed counter are updated in-place within the
232 240
  * HMAC_DRBG internal state.
233 241
  */
234
-void hmac_drbg_reseed ( struct hmac_drbg_state *state,
242
+void hmac_drbg_reseed ( struct digest_algorithm *hash,
243
+			struct hmac_drbg_state *state,
235 244
 			const void *entropy, size_t entropy_len,
236 245
 			const void *additional, size_t additional_len ) {
237 246
 	uint8_t seed_material[ entropy_len + additional_len ];
238 247
 
239
-	DBGC ( state, "HMAC_DRBG %p (re)seed\n", state );
248
+	DBGC ( state, "HMAC_DRBG_%s %p (re)seed\n", hash->name, state );
240 249
 
241 250
 	/* Sanity checks */
251
+	assert ( hash != NULL );
242 252
 	assert ( state != NULL );
243 253
 	assert ( entropy != NULL );
244
-	assert ( ( 8 * entropy_len ) >= HMAC_DRBG_SECURITY_STRENGTH );
245 254
 	assert ( ( additional != NULL ) || ( additional_len == 0 ) );
246 255
 
247 256
 	/* 1.  seed_material = entropy_input || additional_input */
248 257
 	memcpy ( seed_material, entropy, entropy_len );
249 258
 	memcpy ( ( seed_material + entropy_len ), additional, additional_len );
250
-	DBGC ( state, "HMAC_DRBG %p seed material :\n", state );
259
+	DBGC ( state, "HMAC_DRBG_%s %p seed material :\n", hash->name, state );
251 260
 	DBGC_HDA ( state, 0, seed_material, sizeof ( seed_material ) );
252 261
 
253 262
 	/* 2.  ( Key, V ) = HMAC_DBRG_Update ( seed_material, Key, V ) */
254
-	hmac_drbg_update ( state, seed_material, sizeof ( seed_material ) );
263
+	hmac_drbg_update ( hash, state, seed_material,
264
+			   sizeof ( seed_material ) );
255 265
 
256 266
 	/* 3.  reseed_counter = 1 */
257 267
 	state->reseed_counter = 1;
@@ -262,6 +272,7 @@ void hmac_drbg_reseed ( struct hmac_drbg_state *state,
262 272
 /**
263 273
  * Generate pseudorandom bits using HMAC_DRBG
264 274
  *
275
+ * @v hash		Underlying hash algorithm
265 276
  * @v state		HMAC_DRBG internal state
266 277
  * @v additional	Additional input
267 278
  * @v additional_len	Length of additional input
@@ -279,16 +290,19 @@ void hmac_drbg_reseed ( struct hmac_drbg_state *state,
279 290
  *
280 291
  * Note that the only permitted error is "reseed required".
281 292
  */
282
-int hmac_drbg_generate ( struct hmac_drbg_state *state,
293
+int hmac_drbg_generate ( struct digest_algorithm *hash,
294
+			 struct hmac_drbg_state *state,
283 295
 			 const void *additional, size_t additional_len,
284 296
 			 void *data, size_t len ) {
297
+	size_t out_len = hash->digestsize;
285 298
 	void *orig_data = data;
286 299
 	size_t orig_len = len;
287 300
 	size_t frag_len;
288 301
 
289
-	DBGC ( state, "HMAC_DRBG %p generate\n", state );
302
+	DBGC ( state, "HMAC_DRBG_%s %p generate\n", hash->name, state );
290 303
 
291 304
 	/* Sanity checks */
305
+	assert ( hash != NULL );
292 306
 	assert ( state != NULL );
293 307
 	assert ( data != NULL );
294 308
 	assert ( ( additional != NULL ) || ( additional_len == 0 ) );
@@ -297,8 +311,8 @@ int hmac_drbg_generate ( struct hmac_drbg_state *state,
297 311
 	 *     indication that a reseed is required
298 312
 	 */
299 313
 	if ( state->reseed_counter > HMAC_DRBG_RESEED_INTERVAL ) {
300
-		DBGC ( state, "HMAC_DRBG %p reseed interval exceeded\n",
301
-		       state );
314
+		DBGC ( state, "HMAC_DRBG_%s %p reseed interval exceeded\n",
315
+		       hash->name, state );
302 316
 		return -ESTALE;
303 317
 	}
304 318
 
@@ -306,7 +320,7 @@ int hmac_drbg_generate ( struct hmac_drbg_state *state,
306 320
 	 *     ( Key, V ) = HMAC_DRBG_Update ( additional_input, Key, V )
307 321
 	 */
308 322
 	if ( additional_len )
309
-		hmac_drbg_update ( state, additional, additional_len );
323
+		hmac_drbg_update ( hash, state, additional, additional_len );
310 324
 
311 325
 	/* 3.  temp = Null
312 326
 	 * 4.  While ( len ( temp ) < requested_number_of_bits ) do:
@@ -314,27 +328,27 @@ int hmac_drbg_generate ( struct hmac_drbg_state *state,
314 328
 	while ( len ) {
315 329
 
316 330
 		/* 4.1  V = HMAC ( Key, V ) */
317
-		hmac_drbg_update_value ( state );
331
+		hmac_drbg_update_value ( hash, state );
318 332
 
319 333
 		/* 4.2.  temp = temp || V
320 334
 		 * 5.    returned_bits = Leftmost requested_number_of_bits
321 335
 		 *       of temp
322 336
 		 */
323 337
 		frag_len = len;
324
-		if ( frag_len > sizeof ( state->value ) )
325
-			frag_len = sizeof ( state->value );
338
+		if ( frag_len > out_len )
339
+			frag_len = out_len;
326 340
 		memcpy ( data, state->value, frag_len );
327 341
 		data += frag_len;
328 342
 		len -= frag_len;
329 343
 	}
330 344
 
331 345
 	/* 6.  ( Key, V ) = HMAC_DRBG_Update ( additional_input, Key, V ) */
332
-	hmac_drbg_update ( state, additional, additional_len );
346
+	hmac_drbg_update ( hash, state, additional, additional_len );
333 347
 
334 348
 	/* 7.  reseed_counter = reseed_counter + 1 */
335 349
 	state->reseed_counter++;
336 350
 
337
-	DBGC ( state, "HMAC_DRBG %p generated :\n", state );
351
+	DBGC ( state, "HMAC_DRBG_%s %p generated :\n", hash->name, state );
338 352
 	DBGC_HDA ( state, 0, orig_data, orig_len );
339 353
 
340 354
 	/* 8.  Return SUCCESS, returned_bits, and the new values of

+ 23
- 7
src/include/ipxe/drbg.h 查看文件

@@ -10,16 +10,29 @@
10 10
 FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12 12
 #include <stdint.h>
13
+#include <ipxe/sha1.h>
13 14
 #include <ipxe/hmac_drbg.h>
14 15
 
16
+/** Choose HMAC_DRBG using SHA-1
17
+ *
18
+ * HMAC_DRBG using SHA-1 is an Approved algorithm in ANS X9.82.
19
+ */
20
+#define HMAC_DRBG_ALGORITHM HMAC_DRBG_SHA1
21
+
15 22
 /** Maximum security strength */
16
-#define DRBG_MAX_SECURITY_STRENGTH HMAC_DRBG_MAX_SECURITY_STRENGTH
23
+#define DRBG_MAX_SECURITY_STRENGTH \
24
+	HMAC_DRBG_MAX_SECURITY_STRENGTH ( HMAC_DRBG_ALGORITHM )
17 25
 
18
-/** Security strength */
19
-#define DRBG_SECURITY_STRENGTH HMAC_DRBG_SECURITY_STRENGTH
26
+/** Security strength
27
+ *
28
+ * We choose to operate at the maximum security strength supported by
29
+ * the algorithm.
30
+ */
31
+#define DRBG_SECURITY_STRENGTH DRBG_MAX_SECURITY_STRENGTH
20 32
 
21 33
 /** Minimum entropy input length */
22
-#define DRBG_MIN_ENTROPY_LEN_BYTES HMAC_DRBG_MIN_ENTROPY_LEN_BYTES
34
+#define DRBG_MIN_ENTROPY_LEN_BYTES \
35
+	HMAC_DRBG_MIN_ENTROPY_LEN_BYTES ( DRBG_SECURITY_STRENGTH )
23 36
 
24 37
 /** Maximum entropy input length */
25 38
 #define DRBG_MAX_ENTROPY_LEN_BYTES HMAC_DRBG_MAX_ENTROPY_LEN_BYTES
@@ -60,7 +73,8 @@ static inline void drbg_instantiate_algorithm ( struct drbg_state *state,
60 73
 						size_t entropy_len,
61 74
 						const void *personal,
62 75
 						size_t personal_len ) {
63
-	hmac_drbg_instantiate ( &state->internal, entropy, entropy_len,
76
+	hmac_drbg_instantiate ( HMAC_DRBG_HASH ( HMAC_DRBG_ALGORITHM ),
77
+				&state->internal, entropy, entropy_len,
64 78
 				personal, personal_len );
65 79
 }
66 80
 
@@ -81,7 +95,8 @@ static inline void drbg_reseed_algorithm ( struct drbg_state *state,
81 95
 					   size_t entropy_len,
82 96
 					   const void *additional,
83 97
 					   size_t additional_len ) {
84
-	hmac_drbg_reseed ( &state->internal, entropy, entropy_len,
98
+	hmac_drbg_reseed ( HMAC_DRBG_HASH ( HMAC_DRBG_ALGORITHM ),
99
+			   &state->internal, entropy, entropy_len,
85 100
 			   additional, additional_len );
86 101
 }
87 102
 
@@ -104,7 +119,8 @@ static inline int drbg_generate_algorithm ( struct drbg_state *state,
104 119
 					    const void *additional,
105 120
 					    size_t additional_len,
106 121
 					    void *data, size_t len ) {
107
-	return hmac_drbg_generate ( &state->internal, additional,
122
+	return hmac_drbg_generate ( HMAC_DRBG_HASH ( HMAC_DRBG_ALGORITHM ),
123
+				    &state->internal, additional,
108 124
 				    additional_len, data, len );
109 125
 }
110 126
 

+ 130
- 30
src/include/ipxe/hmac_drbg.h 查看文件

@@ -10,48 +10,148 @@
10 10
 FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12 12
 #include <stdint.h>
13
-#include <ipxe/sha1.h>
13
+#include <ipxe/crypto.h>
14 14
 
15
-/** Use SHA-1 as the underlying hash algorithm
15
+/** Declare an HMAC_DRBG algorithm
16 16
  *
17
- * HMAC_DRBG using SHA-1 is an Approved algorithm in ANS X9.82.
17
+ * @v hash			Underlying hash algorithm
18
+ * @v max_security_strength	Maxmimum security strength
19
+ * @v out_len_bits		Output block length, in bits
20
+ * @ret hmac_drbg		HMAC_DRBG algorithm
18 21
  */
19
-#define hmac_drbg_algorithm sha1_algorithm
22
+#define HMAC_DRBG( hash, max_security_strength, out_len_bits ) \
23
+	( hash, max_security_strength, out_len_bits )
20 24
 
21
-/** Maximum security strength
25
+/** HMAC_DRBG using SHA-1
22 26
  *
23 27
  * The maximum security strength of HMAC_DRBG using SHA-1 is 128 bits
24
- * (according to the list of maximum security strengths documented in
25
- * NIST SP 800-57 Part 1 Section 5.6.1 Table 3).
28
+ * according to the list of maximum security strengths documented in
29
+ * NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
30
+ *
31
+ * The output block length of HMAC_DRBG using SHA-1 is 160 bits
32
+ * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
33
+ * 800-90 Section 10.1 Table 2).
26 34
  */
27
-#define HMAC_DRBG_MAX_SECURITY_STRENGTH 128
35
+#define HMAC_DRBG_SHA1 HMAC_DRBG ( &sha1_algorithm, 128, 160 )
28 36
 
29
-/** Security strength
37
+/** HMAC_DRBG using SHA-224
38
+ *
39
+ * The maximum security strength of HMAC_DRBG using SHA-224 is 192
40
+ * bits according to the list of maximum security strengths documented
41
+ * in NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
30 42
  *
31
- * For the sake of implementation simplicity, only a single security
32
- * strength is supported, which is the maximum security strength
33
- * supported by the algorithm.
43
+ * The output block length of HMAC_DRBG using SHA-224 is 224 bits
44
+ * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
45
+ * 800-90 Section 10.1 Table 2).
34 46
  */
35
-#define HMAC_DRBG_SECURITY_STRENGTH HMAC_DRBG_MAX_SECURITY_STRENGTH
47
+#define HMAC_DRBG_SHA224 HMAC_DRBG ( &sha224_algorithm, 192, 224 )
36 48
 
37
-/** Underlying hash algorithm output length (in bytes) */
38
-#define HMAC_DRBG_OUTLEN_BYTES SHA1_DIGEST_SIZE
49
+/** HMAC_DRBG using SHA-256
50
+ *
51
+ * The maximum security strength of HMAC_DRBG using SHA-256 is 256
52
+ * bits according to the list of maximum security strengths documented
53
+ * in NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
54
+ *
55
+ * The output block length of HMAC_DRBG using SHA-256 is 256 bits
56
+ * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
57
+ * 800-90 Section 10.1 Table 2).
58
+ */
59
+#define HMAC_DRBG_SHA256 HMAC_DRBG ( &sha256_algorithm, 256, 256 )
60
+
61
+/** HMAC_DRBG using SHA-384
62
+ *
63
+ * The maximum security strength of HMAC_DRBG using SHA-384 is 256
64
+ * bits according to the list of maximum security strengths documented
65
+ * in NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
66
+ *
67
+ * The output block length of HMAC_DRBG using SHA-384 is 384 bits
68
+ * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
69
+ * 800-90 Section 10.1 Table 2).
70
+ */
71
+#define HMAC_DRBG_SHA384 HMAC_DRBG ( &sha384_algorithm, 256, 384 )
72
+
73
+/** HMAC_DRBG using SHA-512
74
+ *
75
+ * The maximum security strength of HMAC_DRBG using SHA-512 is 256
76
+ * bits according to the list of maximum security strengths documented
77
+ * in NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
78
+ *
79
+ * The output block length of HMAC_DRBG using SHA-512 is 512 bits
80
+ * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
81
+ * 800-90 Section 10.1 Table 2).
82
+ */
83
+#define HMAC_DRBG_SHA512 HMAC_DRBG ( &sha512_algorithm, 256, 512 )
84
+
85
+/** Underlying hash algorithm
86
+ *
87
+ * @v hmac_drbg			HMAC_DRBG algorithm
88
+ * @ret hash			Underlying hash algorithm
89
+ */
90
+#define HMAC_DRBG_HASH( hmac_drbg ) \
91
+	HMAC_DRBG_EXTRACT_HASH hmac_drbg
92
+#define HMAC_DRBG_EXTRACT_HASH( hash, max_security_strength, out_len_bits ) \
93
+	hash
94
+
95
+/** Maximum security strength
96
+ *
97
+ * @v hmac_drbg			HMAC_DRBG algorithm
98
+ * @ret max_security_strength	Maxmimum security strength
99
+ */
100
+#define HMAC_DRBG_MAX_SECURITY_STRENGTH( hmac_drbg ) \
101
+	HMAC_DRBG_EXTRACT_MAX_SECURITY_STRENGTH hmac_drbg
102
+#define HMAC_DRBG_EXTRACT_MAX_SECURITY_STRENGTH( hash, max_security_strength, \
103
+						 out_len_bits ) \
104
+	max_security_strength
105
+
106
+/** Output block length, in bits
107
+ *
108
+ * @v hmac_drbg			HMAC_DRBG algorithm
109
+ * @ret out_len_bits		Output block length, in bits
110
+ */
111
+#define HMAC_DRBG_OUTLEN_BITS( hmac_drbg ) \
112
+	HMAC_DRBG_EXTRACT_OUTLEN_BITS hmac_drbg
113
+#define HMAC_DRBG_EXTRACT_OUTLEN_BITS( hash, max_security_strength, \
114
+				       out_len_bits ) \
115
+	out_len_bits
116
+
117
+/** Output block length, in bytes
118
+ *
119
+ * @v hmac_drbg			HMAC_DRBG algorithm
120
+ * @ret out_len_bytes		Output block length, in bytes
121
+ */
122
+#define HMAC_DRBG_OUTLEN_BYTES( hmac_drbg ) \
123
+	( HMAC_DRBG_OUTLEN_BITS ( hmac_drbg ) / 8 )
124
+
125
+/** Maximum output block length, in bytes
126
+ *
127
+ * The maximum output block length for HMAC_DRBG is 512 bits for
128
+ * SHA-512 according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2
129
+ * (NIST SP 800-90 Section 10.1 Table 2).
130
+ */
131
+#define HMAC_DRBG_MAX_OUTLEN_BYTES HMAC_DRBG_OUTLEN_BYTES ( HMAC_DRBG_SHA512 )
39 132
 
40 133
 /** Required minimum entropy for instantiate and reseed
134
+ *
135
+ * @v security_strength		Security strength
136
+ * @ret min_entropy		Required minimum entropy
41 137
  *
42 138
  * The minimum required entropy for HMAC_DRBG is equal to the security
43 139
  * strength according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2
44 140
  * (NIST SP 800-90 Section 10.1 Table 2).
45 141
  */
46
-#define HMAC_DRBG_MIN_ENTROPY_BYTES ( HMAC_DRBG_SECURITY_STRENGTH / 8 )
142
+#define HMAC_DRBG_MIN_ENTROPY( security_strength ) (security_strength)
47 143
 
48 144
 /** Minimum entropy input length
145
+ *
146
+ * @v security_strength		Security strength
147
+ * @ret min_entropy_len_bytes	Required minimum entropy length (in bytes)
49 148
  *
50 149
  * The minimum entropy input length for HMAC_DRBG is equal to the
51 150
  * security strength according to ANS X9.82 Part 3-2007 Section 10.2.1
52 151
  * Table 2 (NIST SP 800-90 Section 10.1 Table 2).
53 152
  */
54
-#define HMAC_DRBG_MIN_ENTROPY_LEN_BYTES ( HMAC_DRBG_SECURITY_STRENGTH / 8 )
153
+#define HMAC_DRBG_MIN_ENTROPY_LEN_BYTES( security_strength ) \
154
+	( (security_strength) / 8 )
55 155
 
56 156
 /** Maximum entropy input length
57 157
  *
@@ -95,19 +195,16 @@ FILE_LICENCE ( GPL2_OR_LATER );
95 195
 
96 196
 /** Reseed interval
97 197
  *
98
- * The maximum permitted reseed interval for HMAC_DRBG using SHA-1 is
99
- * 2^48 according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2
100
- * (NIST SP 800-90 Section 10.1 Table 2).  However, the sample
101
- * implementation given in ANS X9.82 Part 3-2007 Annex E.2.1 (NIST SP
102
- * 800-90 Appendix F.2) shows a reseed interval of 10000.
198
+ * The maximum permitted reseed interval for HMAC_DRBG is 2^48
199
+ * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
200
+ * 800-90 Section 10.1 Table 2).  However, the sample implementation
201
+ * given in ANS X9.82 Part 3-2007 Annex E.2.1 (NIST SP 800-90 Appendix
202
+ * F.2) shows a reseed interval of 10000.
103 203
  *
104 204
  * We choose a very conservative reseed interval.
105 205
  */
106 206
 #define HMAC_DRBG_RESEED_INTERVAL 1024
107 207
 
108
-/** Underlying hash algorithm context size (in bytes) */
109
-#define HMAC_DRBG_CTX_SIZE SHA1_CTX_SIZE
110
-
111 208
 /**
112 209
  * HMAC_DRBG internal state
113 210
  *
@@ -124,13 +221,13 @@ struct hmac_drbg_state {
124 221
 	 * "The value V of outlen bits, which is updated each time
125 222
 	 * another outlen bits of output are produced"
126 223
 	 */
127
-	uint8_t value[HMAC_DRBG_OUTLEN_BYTES];
224
+	uint8_t value[HMAC_DRBG_MAX_OUTLEN_BYTES];
128 225
 	/** Current key
129 226
 	 *
130 227
 	 * "The outlen-bit Key, which is updated at least once each
131 228
 	 * time that the DRBG mechanism generates pseudorandom bits."
132 229
 	 */
133
-	uint8_t key[HMAC_DRBG_OUTLEN_BYTES];
230
+	uint8_t key[HMAC_DRBG_MAX_OUTLEN_BYTES];
134 231
 	/** Reseed counter
135 232
 	 *
136 233
 	 * "A counter (reseed_counter) that indicates the number of
@@ -140,13 +237,16 @@ struct hmac_drbg_state {
140 237
 	unsigned int reseed_counter;
141 238
 };
142 239
 
143
-extern void hmac_drbg_instantiate ( struct hmac_drbg_state *state,
240
+extern void hmac_drbg_instantiate ( struct digest_algorithm *hash,
241
+				    struct hmac_drbg_state *state,
144 242
 				    const void *entropy, size_t entropy_len,
145 243
 				    const void *personal, size_t personal_len );
146
-extern void hmac_drbg_reseed ( struct hmac_drbg_state *state,
244
+extern void hmac_drbg_reseed ( struct digest_algorithm *hash,
245
+			       struct hmac_drbg_state *state,
147 246
 			       const void *entropy, size_t entropy_len,
148 247
 			       const void *additional, size_t additional_len );
149
-extern int hmac_drbg_generate ( struct hmac_drbg_state *state,
248
+extern int hmac_drbg_generate ( struct digest_algorithm *hash,
249
+				struct hmac_drbg_state *state,
150 250
 				const void *additional, size_t additional_len,
151 251
 				void *data, size_t len );
152 252
 

+ 216
- 171
src/tests/hmac_drbg_test.c 查看文件

@@ -35,6 +35,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
35 35
 #include <assert.h>
36 36
 #include <string.h>
37 37
 #include <ipxe/hmac_drbg.h>
38
+#include <ipxe/sha1.h>
38 39
 #include <ipxe/test.h>
39 40
 
40 41
 /** Define inline expected data */
@@ -42,10 +43,18 @@ FILE_LICENCE ( GPL2_OR_LATER );
42 43
 
43 44
 /** An HMAC_DRBG instantiation test */
44 45
 struct hmac_drbg_test_instantiate {
45
-	/** Entropy (including nonce) */
46
+	/** Underlying hash algorithm */
47
+	struct digest_algorithm *hash;
48
+	/** Output block length */
49
+	size_t out_len;
50
+	/** Entropy */
46 51
 	const void *entropy;
47
-	/** Length of entropy (including nonce) */
52
+	/** Length of entropy */
48 53
 	size_t entropy_len;
54
+	/** Nonce */
55
+	const void *nonce;
56
+	/** Length of nonce */
57
+	size_t nonce_len;
49 58
 	/** Personalisation string */
50 59
 	const void *personal;
51 60
 	/** Length of personalisation string */
@@ -64,19 +73,26 @@ struct hmac_drbg_test_instantiate {
64 73
  * Define an HMAC_DRBG instantiation test
65 74
  *
66 75
  * @v name		Test name
67
- * @v entropy_array	Entropy input (including nonce)
76
+ * @v hmac_drbg		HMAC_DRBG algorithm
77
+ * @v entropy_array	Entropy input
78
+ * @v nonce_array	Nonce
68 79
  * @v personal_array	Personalisation string
69 80
  * @v key		Expected key
70 81
  * @v value		Expected value
71 82
  * @ret test		Instantiation test
72 83
  */
73
-#define HMAC_DRBG_TEST_INSTANTIATE( name, entropy_array,		\
74
-				    personal_array, key, value )	\
84
+#define HMAC_DRBG_TEST_INSTANTIATE( name, hmac_drbg, entropy_array,	\
85
+				    nonce_array, personal_array,	\
86
+				    key, value )			\
75 87
 	static const uint8_t name ## _key [] = key;			\
76 88
 	static const uint8_t name ## _value [] = value;			\
77 89
 	static const struct hmac_drbg_test_instantiate name = {		\
90
+		.hash = HMAC_DRBG_HASH ( hmac_drbg ),			\
91
+		.out_len = HMAC_DRBG_OUTLEN_BYTES ( hmac_drbg ),	\
78 92
 		.entropy = entropy_array,				\
79 93
 		.entropy_len = sizeof ( entropy_array ),		\
94
+		.nonce = nonce_array,					\
95
+		.nonce_len = sizeof ( nonce_array ),			\
80 96
 		.personal = personal_array,				\
81 97
 		.personal_len = sizeof ( personal_array ),		\
82 98
 		.expected_key = name ## _key,				\
@@ -92,19 +108,33 @@ struct hmac_drbg_test_instantiate {
92 108
  * @v test		Instantiation test
93 109
  */
94 110
 #define instantiate_ok( state, test ) do {				\
95
-	assert ( (test)->expected_key_len == HMAC_DRBG_OUTLEN_BYTES );	\
96
-	assert ( (test)->expected_value_len == HMAC_DRBG_OUTLEN_BYTES ); \
97
-	hmac_drbg_instantiate ( (state), (test)->entropy,		\
98
-				(test)->entropy_len, (test)->personal,	\
111
+	struct {							\
112
+		uint8_t entropy[(test)->entropy_len];			\
113
+		uint8_t nonce[(test)->nonce_len];			\
114
+	} __attribute__ (( packed )) entropy_nonce;			\
115
+									\
116
+	assert ( (test)->expected_key_len == (test)->out_len );		\
117
+	assert ( (test)->expected_value_len == (test)->out_len );	\
118
+	memcpy ( entropy_nonce.entropy, (test)->entropy,		\
119
+		 sizeof ( entropy_nonce.entropy ) );			\
120
+	memcpy ( entropy_nonce.nonce, (test)->nonce,			\
121
+		 sizeof ( entropy_nonce.nonce ) );			\
122
+	hmac_drbg_instantiate ( (test)->hash, (state), &entropy_nonce,	\
123
+				sizeof ( entropy_nonce ),		\
124
+				(test)->personal,			\
99 125
 				(test)->personal_len );			\
100 126
 	ok ( memcmp ( (state)->key, (test)->expected_key,		\
101
-		      sizeof ( (state)->key ) ) == 0 );			\
127
+		      (test)->expected_key_len ) == 0 );		\
102 128
 	ok ( memcmp ( (state)->value, (test)->expected_value,		\
103
-		      sizeof ( (state)->value ) ) == 0 );		\
129
+		      (test)->expected_value_len ) == 0 );		\
104 130
 	} while ( 0 )
105 131
 
106 132
 /** An HMAC_DRBG reseed test */
107 133
 struct hmac_drbg_test_reseed {
134
+	/** Underlying hash algorithm */
135
+	struct digest_algorithm *hash;
136
+	/** Output block length */
137
+	size_t out_len;
108 138
 	/** Entropy */
109 139
 	const void *entropy;
110 140
 	/** Length of entropy */
@@ -127,17 +157,20 @@ struct hmac_drbg_test_reseed {
127 157
  * Define an HMAC_DRBG reseed test
128 158
  *
129 159
  * @v name		Test name
160
+ * @v hmac_drbg		HMAC_DRBG algorithm
130 161
  * @v entropy_array	Entropy input
131 162
  * @v additional_array	Additional input
132 163
  * @v key		Expected key
133 164
  * @v value		Expected value
134 165
  * @ret test		Reseed test
135 166
  */
136
-#define HMAC_DRBG_TEST_RESEED( name, entropy_array, additional_array,	\
137
-			       key, value )		\
167
+#define HMAC_DRBG_TEST_RESEED( name, hmac_drbg, entropy_array,		\
168
+			       additional_array, key, value )		\
138 169
 	static const uint8_t name ## _key [] = key;			\
139 170
 	static const uint8_t name ## _value [] = value;			\
140 171
 	static const struct hmac_drbg_test_reseed name = {		\
172
+		.hash = HMAC_DRBG_HASH ( hmac_drbg ),			\
173
+		.out_len = HMAC_DRBG_OUTLEN_BYTES ( hmac_drbg ),	\
141 174
 		.entropy = entropy_array,				\
142 175
 		.entropy_len = sizeof ( entropy_array ),		\
143 176
 		.additional = additional_array,				\
@@ -155,19 +188,23 @@ struct hmac_drbg_test_reseed {
155 188
  * @v test		Reseed test
156 189
  */
157 190
 #define reseed_ok( state, test ) do {					\
158
-	assert ( (test)->expected_key_len == HMAC_DRBG_OUTLEN_BYTES );	\
159
-	assert ( (test)->expected_value_len == HMAC_DRBG_OUTLEN_BYTES ); \
160
-	hmac_drbg_reseed ( (state), (test)->entropy,			\
191
+	assert ( (test)->expected_key_len == (test)->out_len );		\
192
+	assert ( (test)->expected_value_len == (test)->out_len );	\
193
+	hmac_drbg_reseed ( (test)->hash, (state), (test)->entropy,	\
161 194
 			   (test)->entropy_len, (test)->additional,	\
162 195
 			   (test)->additional_len );			\
163 196
 	ok ( memcmp ( (state)->key, (test)->expected_key,		\
164
-		      sizeof ( (state)->key ) ) == 0 );			\
197
+		      (test)->expected_key_len ) == 0 );		\
165 198
 	ok ( memcmp ( (state)->value, (test)->expected_value,		\
166
-		      sizeof ( (state)->value ) ) == 0 );		\
199
+		      (test)->expected_value_len ) == 0 );		\
167 200
 	} while ( 0 )
168 201
 
169 202
 /** An HMAC_DRBG generation test */
170 203
 struct hmac_drbg_test_generate {
204
+	/** Underlying hash algorithm */
205
+	struct digest_algorithm *hash;
206
+	/** Output block length */
207
+	size_t out_len;
171 208
 	/** Additional input */
172 209
 	const void *additional;
173 210
 	/** Length of additional_input */
@@ -183,25 +220,28 @@ struct hmac_drbg_test_generate {
183 220
 	/** Expected pseudorandom data */
184 221
 	const void *expected_data;
185 222
 	/** Length of data */
186
-	size_t len;
223
+	size_t expected_data_len;
187 224
 };
188 225
 
189 226
 /**
190 227
  * Define an HMAC_DRBG generation test
191 228
  *
192 229
  * @v name		Test name
230
+ * @v hmac_drbg		HMAC_DRBG algorithm
193 231
  * @v additional_array	Additional input
194 232
  * @v key		Expected key
195 233
  * @v value		Expected value
196 234
  * @v data		Expected pseudorandom data
197 235
  * @ret test		Generation test
198 236
  */
199
-#define HMAC_DRBG_TEST_GENERATE( name, additional_array, key, value,	\
200
-				 data )					\
237
+#define HMAC_DRBG_TEST_GENERATE( name, hmac_drbg, additional_array,	\
238
+				 key, value, data )			\
201 239
 	static const uint8_t name ## _key [] = key;			\
202 240
 	static const uint8_t name ## _value [] = value;			\
203 241
 	static const uint8_t name ## _data [] = data;			\
204 242
 	static const struct hmac_drbg_test_generate name = {		\
243
+		.hash = HMAC_DRBG_HASH ( hmac_drbg ),			\
244
+		.out_len = HMAC_DRBG_OUTLEN_BYTES ( hmac_drbg ),	\
205 245
 		.additional = additional_array,				\
206 246
 		.additional_len = sizeof ( additional_array ),		\
207 247
 		.expected_key = name ## _key,				\
@@ -209,7 +249,7 @@ struct hmac_drbg_test_generate {
209 249
 		.expected_value = name ## _value,			\
210 250
 		.expected_value_len = sizeof ( name ## _value ),	\
211 251
 		.expected_data = name ## _data,				\
212
-		.len = sizeof ( name ## _data ),			\
252
+		.expected_data_len = sizeof ( name ## _data ),		\
213 253
 	}
214 254
 
215 255
 /**
@@ -219,25 +259,28 @@ struct hmac_drbg_test_generate {
219 259
  * @v test		Generation test
220 260
  */
221 261
 #define generate_ok( state, test ) do {					\
222
-	uint8_t data[ (test)->len ];					\
262
+	uint8_t data[ (test)->expected_data_len ];			\
223 263
 	int rc;								\
224 264
 									\
225
-	assert ( (test)->expected_key_len == HMAC_DRBG_OUTLEN_BYTES );	\
226
-	assert ( (test)->expected_value_len == HMAC_DRBG_OUTLEN_BYTES );\
227
-	rc = hmac_drbg_generate ( (state), (test)->additional,		\
228
-				  (test)->additional_len, data,		\
229
-				  sizeof ( data ) );			\
265
+	assert ( (test)->expected_key_len == (test)->out_len );		\
266
+	assert ( (test)->expected_value_len == (test)->out_len );	\
267
+	rc = hmac_drbg_generate ( (test)->hash, (state),		\
268
+				  (test)->additional,			\
269
+				  (test)->additional_len,		\
270
+				  data, sizeof ( data ) );		\
230 271
 	ok ( rc == 0 );							\
231 272
 	ok ( memcmp ( (state)->key, (test)->expected_key,		\
232
-		      sizeof ( (state)->key ) ) == 0 );			\
273
+		      (test)->expected_key_len ) == 0 );		\
233 274
 	ok ( memcmp ( (state)->value, (test)->expected_value,		\
234
-		      sizeof ( (state)->value ) ) == 0 );		\
275
+		      (test)->expected_value_len ) == 0 );		\
235 276
 	ok ( memcmp ( data, (test)->expected_data,			\
236
-		      sizeof ( data ) ) == 0 );				\
277
+		      (test)->expected_data_len ) == 0 );		\
237 278
 	} while ( 0 )
238 279
 
239 280
 /** An HMAC_DRBG generation failure test */
240 281
 struct hmac_drbg_test_generate_fail {
282
+	/** Underlying hash algorithm */
283
+	struct digest_algorithm *hash;
241 284
 	/** Additional input */
242 285
 	const void *additional;
243 286
 	/** Length of additional_input */
@@ -250,11 +293,14 @@ struct hmac_drbg_test_generate_fail {
250 293
  * Define an HMAC_DRBG generation failure test
251 294
  *
252 295
  * @v name		Test name
296
+ * @v hmac_drbg		HMAC_DRBG algorithm
253 297
  * @v additional_array	Additional input
254 298
  * @ret test		Generation failure test
255 299
  */
256
-#define HMAC_DRBG_TEST_GENERATE_FAIL( name, additional_array, len )	\
300
+#define HMAC_DRBG_TEST_GENERATE_FAIL( name, hmac_drbg,			\
301
+				      additional_array, len )		\
257 302
 	static const struct hmac_drbg_test_generate_fail name = {	\
303
+		.hash = HMAC_DRBG_HASH ( hmac_drbg ),			\
258 304
 		.additional = additional_array,				\
259 305
 		.additional_len = sizeof ( additional_array ),		\
260 306
 		.requested_len = len,					\
@@ -270,25 +316,24 @@ struct hmac_drbg_test_generate_fail {
270 316
 	uint8_t data[ (test)->requested_len ];				\
271 317
 	int rc;								\
272 318
 									\
273
-	rc = hmac_drbg_generate ( (state), (test)->additional,		\
319
+	rc = hmac_drbg_generate ( (test)->hash, (state),		\
320
+				  (test)->additional,			\
274 321
 				  (test)->additional_len, data,		\
275 322
 				  sizeof ( data ) );			\
276 323
 	ok ( rc != 0 );							\
277 324
 	} while ( 0 )
278 325
 
279
-/** "EntropyInput" and "Nonce"
280
- *
281
- * These are pre-concatenated since our implementation expects to
282
- * receive the nonce in the form of additional entropy.
283
- */
326
+/** "EntropyInput" */
284 327
 static const uint8_t entropy_input[] = {
285
-	/* "EntropyInput" */
286 328
 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
287 329
 	0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
288 330
 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
289 331
 	0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
290
-	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
291
-	/* "Nonce" */
332
+	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
333
+};
334
+
335
+/** "Nonce" for SHA-1 */
336
+static const uint8_t nonce_sha1[] = {
292 337
 	0x20, 0x21, 0x22, 0x23, 0x24
293 338
 };
294 339
 
@@ -343,16 +388,16 @@ static const uint8_t additional_input_2[] = {
343 388
 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6
344 389
 };
345 390
 
346
-/** Test 1 : Instantiation */
347
-HMAC_DRBG_TEST_INSTANTIATE ( instantiate_1,
348
-	entropy_input, personalisation_string_empty,
391
+/** SHA-1 Test 1 : Instantiation */
392
+HMAC_DRBG_TEST_INSTANTIATE ( sha1_instantiate_1, HMAC_DRBG_SHA1,
393
+	entropy_input, nonce_sha1, personalisation_string_empty,
349 394
 	EXPECT ( 0xab, 0x16, 0x0d, 0xd2, 0x1c, 0x30, 0x98, 0x0c, 0xa3, 0xca,
350 395
 		 0x5a, 0x9c, 0x77, 0xb7, 0xbd, 0xf0, 0x50, 0xe6, 0x4e, 0xe9 ),
351 396
 	EXPECT ( 0x61, 0x44, 0x99, 0xea, 0x98, 0x0c, 0xfb, 0x3d, 0xaa, 0x2c,
352 397
 		 0xa8, 0x6d, 0x65, 0xa4, 0x6b, 0xf4, 0x48, 0x8d, 0x8c, 0xc5 ) );
353 398
 
354
-/** Test 1.1 : First call to Generate */
355
-HMAC_DRBG_TEST_GENERATE ( generate_1_1,
399
+/** SHA-1 Test 1.1 : First call to Generate */
400
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_1_1, HMAC_DRBG_SHA1,
356 401
 	additional_input_empty,
357 402
 	EXPECT ( 0x7b, 0xb1, 0x80, 0x28, 0xe0, 0x1d, 0x03, 0x42, 0xdf, 0x4f,
358 403
 		 0x54, 0xda, 0x51, 0x22, 0xfa, 0x5f, 0x2c, 0x3a, 0x05, 0xe4 ),
@@ -363,8 +408,8 @@ HMAC_DRBG_TEST_GENERATE ( generate_1_1,
363 408
 		 0x8c, 0x0b, 0x22, 0xcd, 0x06, 0x30, 0xbf, 0xb0, 0x12, 0x7f,
364 409
 		 0xb5, 0x40, 0x8c, 0x8e, 0xfc, 0x17, 0xa9, 0x29, 0x89, 0x6e ) );
365 410
 
366
-/** Test 1.2 : Second call to Generate */
367
-HMAC_DRBG_TEST_GENERATE ( generate_1_2,
411
+/** SHA-1 Test 1.2 : Second call to Generate */
412
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_1_2, HMAC_DRBG_SHA1,
368 413
 	additional_input_empty,
369 414
 	EXPECT ( 0x3d, 0x4d, 0x73, 0x77, 0xe9, 0x17, 0x2a, 0xaf, 0xa7, 0x76,
370 415
 		 0xb0, 0xdd, 0xcb, 0x89, 0x42, 0x00, 0x4a, 0x44, 0xb7, 0xfd ),
@@ -375,11 +420,11 @@ HMAC_DRBG_TEST_GENERATE ( generate_1_2,
375 420
 		 0x9c, 0xe3, 0x67, 0xd0, 0x3a, 0xea, 0xde, 0x37, 0x82, 0x7f,
376 421
 		 0xa8, 0xe9, 0xcb, 0x6a, 0x08, 0x19, 0x61, 0x15, 0xd9, 0x48 ) );
377 422
 
378
-/** Test 2 : Instantiation */
379
-#define instantiate_2 instantiate_1
423
+/** SHA-1 Test 2 : Instantiation */
424
+#define sha1_instantiate_2 sha1_instantiate_1
380 425
 
381
-/** Test 2.1 : First call to Generate */
382
-HMAC_DRBG_TEST_GENERATE ( generate_2_1,
426
+/** SHA-1 Test 2.1 : First call to Generate */
427
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_2_1, HMAC_DRBG_SHA1,
383 428
 	additional_input_1,
384 429
 	EXPECT ( 0x3a, 0x06, 0x2e, 0x6b, 0x79, 0xfe, 0x70, 0xdb, 0xff, 0xeb,
385 430
 		 0x3a, 0x2b, 0x6b, 0xe8, 0x03, 0x23, 0xf7, 0xd6, 0x74, 0xc5 ),
@@ -390,8 +435,8 @@ HMAC_DRBG_TEST_GENERATE ( generate_2_1,
390 435
 		 0x9a, 0xd0, 0xbb, 0x26, 0xfa, 0xc0, 0x49, 0x7b, 0x5c, 0x57,
391 436
 		 0xe1, 0x61, 0xe3, 0x66, 0x81, 0xbc, 0xc9, 0x30, 0xce, 0x80 ) );
392 437
 
393
-/** Test 2.2 : Second call to Generate */
394
-HMAC_DRBG_TEST_GENERATE ( generate_2_2,
438
+/** SHA-1 Test 2.2 : Second call to Generate */
439
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_2_2, HMAC_DRBG_SHA1,
395 440
 	additional_input_2,
396 441
 	EXPECT ( 0x8a, 0xd7, 0xe3, 0x47, 0x72, 0xb5, 0xfc, 0x7c, 0x3b, 0x3b,
397 442
 		 0x27, 0x62, 0x4f, 0x0b, 0x91, 0x77, 0x6a, 0x8a, 0x71, 0x12 ),
@@ -402,16 +447,16 @@ HMAC_DRBG_TEST_GENERATE ( generate_2_2,
402 447
 		 0xb9, 0xc3, 0x7b, 0x7f, 0xe8, 0x1b, 0xa9, 0x40, 0x45, 0xa1,
403 448
 		 0x4a, 0x7c, 0xb5, 0x14, 0xb4, 0x46, 0x66, 0x6e, 0xa5, 0xa7 ) );
404 449
 
405
-/** Test 3 : Instantiation */
406
-HMAC_DRBG_TEST_INSTANTIATE ( instantiate_3,
407
-	entropy_input, personalisation_string,
450
+/** SHA-1 Test 3 : Instantiation */
451
+HMAC_DRBG_TEST_INSTANTIATE ( sha1_instantiate_3, HMAC_DRBG_SHA1,
452
+	entropy_input, nonce_sha1, personalisation_string,
408 453
 	EXPECT ( 0xb7, 0xd9, 0x66, 0xd7, 0x0d, 0x4e, 0x27, 0xa7, 0xfa, 0x83,
409 454
 		 0x8f, 0x7d, 0x61, 0x12, 0x6c, 0x0e, 0xdc, 0x84, 0x76, 0x1c ),
410 455
 	EXPECT ( 0xda, 0xb2, 0xa7, 0x18, 0x83, 0xf1, 0x00, 0x5c, 0x5d, 0xd0,
411 456
 		 0x39, 0x32, 0x4d, 0x3c, 0x36, 0x4d, 0x6e, 0x18, 0xf9, 0x54 ) );
412 457
 
413
-/** Test 3.1 : First call to Generate */
414
-HMAC_DRBG_TEST_GENERATE ( generate_3_1,
458
+/** SHA-1 Test 3.1 : First call to Generate */
459
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_3_1, HMAC_DRBG_SHA1,
415 460
 	additional_input_empty,
416 461
 	EXPECT ( 0x87, 0xd3, 0x82, 0x8b, 0xe0, 0x3a, 0x80, 0x7d, 0xd3, 0x40,
417 462
 		 0x29, 0x41, 0xbe, 0xd6, 0xde, 0x98, 0x6e, 0xe7, 0xa2, 0x86 ),
@@ -422,8 +467,8 @@ HMAC_DRBG_TEST_GENERATE ( generate_3_1,
422 467
 		 0x9b, 0xd0, 0x60, 0x20, 0x8e, 0xea, 0x7d, 0x71, 0xf9, 0xd1,
423 468
 		 0x23, 0xdf, 0x47, 0xb3, 0xce, 0x06, 0x9d, 0x98, 0xed, 0xe6 ) );
424 469
 
425
-/** Test 3.2 : Second call to Generate */
426
-HMAC_DRBG_TEST_GENERATE ( generate_3_2,
470
+/** SHA-1 Test 3.2 : Second call to Generate */
471
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_3_2, HMAC_DRBG_SHA1,
427 472
 	additional_input_empty,
428 473
 	EXPECT ( 0x26, 0xab, 0xbf, 0x54, 0xb2, 0x8b, 0x93, 0xff, 0x90, 0x08,
429 474
 		 0x67, 0x0e, 0xbf, 0xee, 0x86, 0xcd, 0xd7, 0x22, 0x8e, 0xd5 ),
@@ -434,11 +479,11 @@ HMAC_DRBG_TEST_GENERATE ( generate_3_2,
434 479
 		 0x63, 0x97, 0x65, 0x47, 0x2b, 0x71, 0xac, 0xeb, 0xe2, 0xea,
435 480
 		 0x8b, 0x1b, 0x6b, 0x49, 0x62, 0x9c, 0xb6, 0x73, 0x17, 0xe0 ) );
436 481
 
437
-/** Test 4 : Instantiation */
438
-#define instantiate_4 instantiate_3
482
+/** SHA-1 Test 4 : Instantiation */
483
+#define sha1_instantiate_4 sha1_instantiate_3
439 484
 
440
-/** Test 4.1 : First call to Generate */
441
-HMAC_DRBG_TEST_GENERATE ( generate_4_1,
485
+/** SHA-1 Test 4.1 : First call to Generate */
486
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_4_1, HMAC_DRBG_SHA1,
442 487
 	additional_input_1,
443 488
 	EXPECT ( 0x17, 0xa5, 0xd7, 0x9f, 0x07, 0x67, 0x87, 0x6f, 0x3a, 0x45,
444 489
 		 0xe0, 0xc9, 0xc3, 0x3e, 0xc8, 0x8b, 0x03, 0xce, 0xea, 0x13 ),
@@ -449,8 +494,8 @@ HMAC_DRBG_TEST_GENERATE ( generate_4_1,
449 494
 		 0x6d, 0xfc, 0x30, 0x2e, 0xad, 0x4c, 0xe3, 0xf5, 0x54, 0xc7,
450 495
 		 0x9b, 0x0d, 0x44, 0x23, 0x9e, 0xba, 0x56, 0xa7, 0xea, 0x2d ) );
451 496
 
452
-/** Test 4.2 : Second call to Generate */
453
-HMAC_DRBG_TEST_GENERATE ( generate_4_2,
497
+/** SHA-1 Test 4.2 : Second call to Generate */
498
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_4_2, HMAC_DRBG_SHA1,
454 499
 	additional_input_2,
455 500
 	EXPECT ( 0x07, 0x9b, 0x57, 0xd9, 0x40, 0x6e, 0x11, 0xc2, 0xf8, 0x7c,
456 501
 		 0x8c, 0x82, 0x8c, 0x8c, 0x6f, 0xa7, 0x6e, 0x40, 0xea, 0x01 ),
@@ -461,23 +506,23 @@ HMAC_DRBG_TEST_GENERATE ( generate_4_2,
461 506
 		 0xdc, 0x91, 0x21, 0x5d, 0x44, 0xb1, 0x07, 0xb4, 0xd5, 0xa7,
462 507
 		 0x79, 0x01, 0x59, 0x25, 0x09, 0x76, 0x52, 0x80, 0xf9, 0x69 ) );
463 508
 
464
-/** Test 5 : Instantiation */
465
-#define instantiate_5 instantiate_1
509
+/** SHA-1 Test 5 : Instantiation */
510
+#define sha1_instantiate_5 sha1_instantiate_1
466 511
 
467
-/** Test 5.1 : First call to Generate */
468
-HMAC_DRBG_TEST_GENERATE_FAIL ( generate_fail_5_1,
512
+/** SHA-1 Test 5.1 : First call to Generate */
513
+HMAC_DRBG_TEST_GENERATE_FAIL ( sha1_generate_fail_5_1, HMAC_DRBG_SHA1,
469 514
 	additional_input_empty, ( 320 / 8 ) );
470 515
 
471
-/** Test 5.2 : Reseed */
472
-HMAC_DRBG_TEST_RESEED ( reseed_5_2,
516
+/** SHA-1 Test 5.2 : Reseed */
517
+HMAC_DRBG_TEST_RESEED ( sha1_reseed_5_2, HMAC_DRBG_SHA1,
473 518
 	entropy_input_1, additional_input_empty,
474 519
 	EXPECT ( 0xcd, 0x4c, 0xab, 0x38, 0xc8, 0xad, 0x65, 0x71, 0x22, 0xbf,
475 520
 		 0x5d, 0x3d, 0x00, 0xd0, 0xac, 0x9b, 0x13, 0xd6, 0x29, 0xbb ),
476 521
 	EXPECT ( 0xf6, 0x60, 0xe2, 0x3e, 0x91, 0x00, 0x6b, 0x62, 0xc6, 0x54,
477 522
 		 0x3a, 0xb1, 0x34, 0x4d, 0x23, 0xa3, 0x1a, 0xb4, 0xcf, 0x2c ) );
478 523
 
479
-/** Test 5.3 : Retried first call to Generate */
480
-HMAC_DRBG_TEST_GENERATE ( generate_5_3,
524
+/** SHA-1 Test 5.3 : Retried first call to Generate */
525
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_5_3, HMAC_DRBG_SHA1,
481 526
 	additional_input_empty,
482 527
 	EXPECT ( 0x58, 0x7f, 0xd8, 0x21, 0xef, 0x6c, 0x9d, 0xa4, 0xa8, 0x3c,
483 528
 		 0x19, 0x21, 0x1f, 0x10, 0x56, 0xca, 0xcd, 0x23, 0xfc, 0x1a ),
@@ -488,20 +533,20 @@ HMAC_DRBG_TEST_GENERATE ( generate_5_3,
488 533
 		 0xbc, 0x0e, 0xfc, 0x28, 0x2a, 0xbd, 0x87, 0x60, 0x5c, 0xc9,
489 534
 		 0x0c, 0xba, 0x9b, 0x86, 0x33, 0xdc, 0xb1, 0xda, 0xe0, 0x2e ) );
490 535
 
491
-/** Test 5.4 : Second call to Generate */
492
-HMAC_DRBG_TEST_GENERATE_FAIL ( generate_fail_5_4,
536
+/** SHA-1 Test 5.4 : Second call to Generate */
537
+HMAC_DRBG_TEST_GENERATE_FAIL ( sha1_generate_fail_5_4, HMAC_DRBG_SHA1,
493 538
 	additional_input_empty, ( 320 / 8 ) );
494 539
 
495
-/** Test 5.5 : Reseed */
496
-HMAC_DRBG_TEST_RESEED ( reseed_5_5,
540
+/** SHA-1 Test 5.5 : Reseed */
541
+HMAC_DRBG_TEST_RESEED ( sha1_reseed_5_5, HMAC_DRBG_SHA1,
497 542
 	entropy_input_2, additional_input_empty,
498 543
 	EXPECT ( 0xdb, 0xa1, 0xcf, 0xf4, 0x87, 0x95, 0x46, 0xa0, 0x38, 0xa5,
499 544
 		 0x59, 0xb2, 0xa2, 0x4d, 0xf2, 0xc0, 0x30, 0x08, 0x9a, 0x41 ),
500 545
 	EXPECT ( 0x2f, 0x88, 0x3c, 0x46, 0x48, 0xe1, 0x31, 0xe8, 0x6d, 0xdf,
501 546
 		 0x9d, 0xca, 0x0d, 0x74, 0xf3, 0x0c, 0xa1, 0xce, 0x6e, 0xfb ) );
502 547
 
503
-/** Test 5.6 : Retried second call to Generate */
504
-HMAC_DRBG_TEST_GENERATE ( generate_5_6,
548
+/** SHA-1 Test 5.6 : Retried second call to Generate */
549
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_5_6, HMAC_DRBG_SHA1,
505 550
 	additional_input_empty,
506 551
 	EXPECT ( 0xf9, 0x39, 0xa5, 0xab, 0x08, 0xa3, 0x9f, 0x23, 0x10, 0x70,
507 552
 		 0xb0, 0xd4, 0xc9, 0x6d, 0xc2, 0x37, 0x90, 0xba, 0x01, 0x53 ),
@@ -512,23 +557,23 @@ HMAC_DRBG_TEST_GENERATE ( generate_5_6,
512 557
 		 0x1a, 0x0a, 0xd3, 0x9b, 0xd1, 0x5e, 0x86, 0x2e, 0x64, 0x4f,
513 558
 		 0x31, 0xe4, 0xa2, 0xd7, 0xd8, 0x43, 0xe5, 0x7c, 0x59, 0x68 ) );
514 559
 
515
-/** Test 6 : Instantiate */
516
-#define instantiate_6 instantiate_1
560
+/** SHA-1 Test 6 : Instantiate */
561
+#define sha1_instantiate_6 sha1_instantiate_1
517 562
 
518
-/** Test 6.1 : First call to Generate */
519
-HMAC_DRBG_TEST_GENERATE_FAIL ( generate_fail_6_1,
563
+/** SHA-1 Test 6.1 : First call to Generate */
564
+HMAC_DRBG_TEST_GENERATE_FAIL ( sha1_generate_fail_6_1, HMAC_DRBG_SHA1,
520 565
 	additional_input_1, ( 320 / 8 ) );
521 566
 
522
-/** Test 6.2 : Reseed */
523
-HMAC_DRBG_TEST_RESEED ( reseed_6_2,
567
+/** SHA-1 Test 6.2 : Reseed */
568
+HMAC_DRBG_TEST_RESEED ( sha1_reseed_6_2, HMAC_DRBG_SHA1,
524 569
 	entropy_input_1, additional_input_1,
525 570
 	EXPECT ( 0x52, 0x28, 0xa4, 0xb6, 0xa4, 0x46, 0x92, 0x90, 0x5e, 0xc0,
526 571
 		 0x44, 0xbf, 0xf0, 0xbb, 0x4e, 0x25, 0xa3, 0x87, 0xca, 0xc1 ),
527 572
 	EXPECT ( 0x24, 0x77, 0x32, 0xd0, 0x4c, 0xb8, 0x4e, 0xd4, 0x1a, 0xdd,
528 573
 		 0x95, 0xa4, 0xb7, 0x8b, 0x50, 0xcd, 0x9b, 0x3d, 0x3f, 0x32 ) );
529 574
 
530
-/** Test 6.3 : Retried first call to Generate */
531
-HMAC_DRBG_TEST_GENERATE ( generate_6_3,
575
+/** SHA-1 Test 6.3 : Retried first call to Generate */
576
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_6_3, HMAC_DRBG_SHA1,
532 577
 	additional_input_empty,
533 578
 	EXPECT ( 0xab, 0x3d, 0xd4, 0x89, 0x5b, 0xc8, 0xcd, 0x22, 0x71, 0xde,
534 579
 		 0xba, 0x5f, 0x3c, 0x13, 0x63, 0x52, 0x6b, 0x8b, 0x74, 0x52 ),
@@ -539,20 +584,20 @@ HMAC_DRBG_TEST_GENERATE ( generate_6_3,
539 584
 		 0x57, 0xae, 0xe8, 0x15, 0xb9, 0xc0, 0x70, 0x04, 0xc7, 0xe9,
540 585
 		 0x92, 0xeb, 0x8c, 0x7e, 0x59, 0x19, 0x64, 0xaf, 0xee, 0xa2 ) );
541 586
 
542
-/** Test 6.4 : Second call to Generate */
543
-HMAC_DRBG_TEST_GENERATE_FAIL ( generate_fail_6_4,
587
+/** SHA-1 Test 6.4 : Second call to Generate */
588
+HMAC_DRBG_TEST_GENERATE_FAIL ( sha1_generate_fail_6_4, HMAC_DRBG_SHA1,
544 589
 	additional_input_2, ( 320 / 8 ) );
545 590
 
546
-/** Test 6.5 : Reseed */
547
-HMAC_DRBG_TEST_RESEED ( reseed_6_5,
591
+/** SHA-1 Test 6.5 : Reseed */
592
+HMAC_DRBG_TEST_RESEED ( sha1_reseed_6_5, HMAC_DRBG_SHA1,
548 593
 	entropy_input_2, additional_input_2,
549 594
 	EXPECT ( 0xe5, 0x73, 0x9f, 0x9c, 0xf7, 0xff, 0x43, 0x84, 0xd1, 0x27,
550 595
 		 0x3e, 0x02, 0x6b, 0x45, 0x31, 0x21, 0x36, 0x49, 0x4f, 0x41 ),
551 596
 	EXPECT ( 0x30, 0xc3, 0x43, 0x05, 0xc2, 0xc6, 0x48, 0xb0, 0x57, 0xa6,
552 597
 		 0x40, 0x22, 0x1b, 0x5c, 0x56, 0x57, 0x26, 0xcd, 0x32, 0xb2 ) );
553 598
 
554
-/** Test 6.6 : Retried second call to Generate */
555
-HMAC_DRBG_TEST_GENERATE ( generate_6_6,
599
+/** SHA-1 Test 6.6 : Retried second call to Generate */
600
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_6_6, HMAC_DRBG_SHA1,
556 601
 	additional_input_empty,
557 602
 	EXPECT ( 0x61, 0x91, 0xca, 0x9b, 0xf0, 0x00, 0xd1, 0x0a, 0x71, 0x69,
558 603
 		 0x0a, 0xc1, 0x0e, 0x09, 0xff, 0xc8, 0x92, 0xab, 0xde, 0x9a ),
@@ -563,23 +608,23 @@ HMAC_DRBG_TEST_GENERATE ( generate_6_6,
563 608
 		 0x2d, 0xc6, 0x4a, 0x16, 0x42, 0x11, 0x88, 0x9a, 0x01, 0x0f,
564 609
 		 0x24, 0x71, 0xa0, 0x91, 0x2f, 0xfe, 0xa1, 0xbf, 0x01, 0x95 ) );
565 610
 
566
-/** Test 7 : Instantiation */
567
-#define instantiate_7 instantiate_3
611
+/** SHA-1 Test 7 : Instantiation */
612
+#define sha1_instantiate_7 sha1_instantiate_3
568 613
 
569
-/** Test 7.1 : First call to Generate */
570
-HMAC_DRBG_TEST_GENERATE_FAIL ( generate_fail_7_1,
614
+/** SHA-1 Test 7.1 : First call to Generate */
615
+HMAC_DRBG_TEST_GENERATE_FAIL ( sha1_generate_fail_7_1, HMAC_DRBG_SHA1,
571 616
 	additional_input_empty, ( 320 / 8 ) );
572 617
 
573
-/** Test 7.2 : Reseed */
574
-HMAC_DRBG_TEST_RESEED ( reseed_7_2,
618
+/** SHA-1 Test 7.2 : Reseed */
619
+HMAC_DRBG_TEST_RESEED ( sha1_reseed_7_2, HMAC_DRBG_SHA1,
575 620
 	entropy_input_1, additional_input_empty,
576 621
 	EXPECT ( 0xb9, 0x25, 0x4d, 0x8a, 0xac, 0xba, 0x43, 0xfb, 0xda, 0xe6,
577 622
 		 0x39, 0x4f, 0x2b, 0x3a, 0xfc, 0x5d, 0x58, 0x08, 0x00, 0xbf ),
578 623
 	EXPECT ( 0x28, 0x40, 0x3b, 0x60, 0x36, 0x38, 0xd0, 0x7d, 0x79, 0x66,
579 624
 		 0x66, 0x1e, 0xf6, 0x7b, 0x9d, 0x39, 0x05, 0xf4, 0x6d, 0xb9 ) );
580 625
 
581
-/** Test 7.3 : Retried first call to Generate */
582
-HMAC_DRBG_TEST_GENERATE ( generate_7_3,
626
+/** SHA-1 Test 7.3 : Retried first call to Generate */
627
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_7_3, HMAC_DRBG_SHA1,
583 628
 	additional_input_empty,
584 629
 	EXPECT ( 0x64, 0xfe, 0x07, 0x4a, 0x6e, 0x77, 0x97, 0xd1, 0xa4, 0x35,
585 630
 		 0xda, 0x89, 0x64, 0x48, 0x4d, 0x6c, 0xf8, 0xbd, 0xc0, 0x1b ),
@@ -590,20 +635,20 @@ HMAC_DRBG_TEST_GENERATE ( generate_7_3,
590 635
 		 0xb5, 0x70, 0x81, 0xe4, 0x22, 0x0f, 0x22, 0xc5, 0xc2, 0x83,
591 636
 		 0xe2, 0xc9, 0x1b, 0x8e, 0x30, 0x5a, 0xb8, 0x69, 0xc6, 0x25 ) );
592 637
 
593
-/** Test 7.4 : Second call to Generate */
594
-HMAC_DRBG_TEST_GENERATE_FAIL ( generate_fail_7_4,
638
+/** SHA-1 Test 7.4 : Second call to Generate */
639
+HMAC_DRBG_TEST_GENERATE_FAIL ( sha1_generate_fail_7_4, HMAC_DRBG_SHA1,
595 640
 	additional_input_empty, ( 320 / 8 ) );
596 641
 
597
-/** Test 7.5 : Reseed */
598
-HMAC_DRBG_TEST_RESEED ( reseed_7_5,
642
+/** SHA-1 Test 7.5 : Reseed */
643
+HMAC_DRBG_TEST_RESEED ( sha1_reseed_7_5, HMAC_DRBG_SHA1,
599 644
 	entropy_input_2, additional_input_empty,
600 645
 	EXPECT ( 0x02, 0xbc, 0x57, 0x7f, 0xd1, 0x0e, 0xf7, 0x19, 0x3c, 0x1d,
601 646
 		 0xb0, 0x98, 0xbd, 0x5b, 0x75, 0xc7, 0xc4, 0xb6, 0x79, 0x59 ),
602 647
 	EXPECT ( 0xbc, 0xbd, 0xf0, 0x52, 0xe0, 0xe0, 0x2a, 0xe8, 0x9a, 0x77,
603 648
 		 0x67, 0x94, 0x3f, 0x98, 0x65, 0xb8, 0xb7, 0x22, 0x90, 0x2d ) );
604 649
 
605
-/** Test 7.6 : Retried second call to Generate */
606
-HMAC_DRBG_TEST_GENERATE ( generate_7_6,
650
+/** SHA-1 Test 7.6 : Retried second call to Generate */
651
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_7_6, HMAC_DRBG_SHA1,
607 652
 	additional_input_empty,
608 653
 	EXPECT ( 0x1a, 0xa4, 0x24, 0x1c, 0x69, 0x5e, 0x29, 0xc0, 0xa5, 0x9a,
609 654
 		 0xd1, 0x8a, 0x60, 0x70, 0xe3, 0x38, 0xa5, 0x48, 0xbe, 0x92 ),
@@ -614,23 +659,23 @@ HMAC_DRBG_TEST_GENERATE ( generate_7_6,
614 659
 		 0x61, 0x48, 0x2c, 0xa5, 0x4d, 0x5f, 0xa7, 0x23, 0xf4, 0xc8,
615 660
 		 0x8b, 0x4f, 0xa5, 0x04, 0xbf, 0x03, 0x27, 0x7f, 0xa7, 0x83 ) );
616 661
 
617
-/** Test 8 : Instantiate */
618
-#define instantiate_8 instantiate_3
662
+/** SHA-1 Test 8 : Instantiate */
663
+#define sha1_instantiate_8 sha1_instantiate_3
619 664
 
620
-/** Test 8.1 : First call to Generate */
621
-HMAC_DRBG_TEST_GENERATE_FAIL ( generate_fail_8_1,
665
+/** SHA-1 Test 8.1 : First call to Generate */
666
+HMAC_DRBG_TEST_GENERATE_FAIL ( sha1_generate_fail_8_1, HMAC_DRBG_SHA1,
622 667
 	additional_input_1, ( 320 / 8 ) );
623 668
 
624
-/** Test 8.2 : Reseed */
625
-HMAC_DRBG_TEST_RESEED ( reseed_8_2,
669
+/** SHA-1 Test 8.2 : Reseed */
670
+HMAC_DRBG_TEST_RESEED ( sha1_reseed_8_2, HMAC_DRBG_SHA1,
626 671
 	entropy_input_1, additional_input_1,
627 672
 	EXPECT ( 0xc0, 0x95, 0x48, 0xc0, 0xd3, 0xc8, 0x61, 0xd7, 0x40, 0xf2,
628 673
 		 0x83, 0x7d, 0x72, 0xb5, 0x07, 0x23, 0x5c, 0x26, 0xdb, 0x82 ),
629 674
 	EXPECT ( 0x17, 0x4b, 0x3f, 0x84, 0xc3, 0x53, 0x1f, 0x7c, 0x0a, 0x2e,
630 675
 		 0x54, 0x21, 0x23, 0x4e, 0xa1, 0x6b, 0x70, 0x8d, 0xdf, 0x0d ) );
631 676
 
632
-/** Test 8.3 : Retried first call to Generate */
633
-HMAC_DRBG_TEST_GENERATE ( generate_8_3,
677
+/** SHA-1 Test 8.3 : Retried first call to Generate */
678
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_8_3, HMAC_DRBG_SHA1,
634 679
 	additional_input_empty,
635 680
 	EXPECT ( 0x60, 0x3f, 0x09, 0x49, 0x27, 0x9c, 0x70, 0xe8, 0xc6, 0x6c,
636 681
 		 0x0f, 0x56, 0x37, 0xc0, 0xf3, 0x75, 0x60, 0x07, 0xe5, 0xac ),
@@ -641,20 +686,20 @@ HMAC_DRBG_TEST_GENERATE ( generate_8_3,
641 686
 		 0x34, 0x71, 0xfd, 0xa5, 0x5c, 0x6d, 0xdd, 0x2c, 0x03, 0xef,
642 687
 		 0xa3, 0xb9, 0x64, 0x3a, 0xb3, 0xbb, 0x22, 0xf6, 0xc9, 0xf2 ) );
643 688
 
644
-/** Test 8.4 : Second call to Generate */
645
-HMAC_DRBG_TEST_GENERATE_FAIL ( generate_fail_8_4,
689
+/** SHA-1 Test 8.4 : Second call to Generate */
690
+HMAC_DRBG_TEST_GENERATE_FAIL ( sha1_generate_fail_8_4, HMAC_DRBG_SHA1,
646 691
 	additional_input_2, ( 320 / 8 ) );
647 692
 
648
-/** Test 8.5 : Reseed */
649
-HMAC_DRBG_TEST_RESEED ( reseed_8_5,
693
+/** SHA-1 Test 8.5 : Reseed */
694
+HMAC_DRBG_TEST_RESEED ( sha1_reseed_8_5, HMAC_DRBG_SHA1,
650 695
 	entropy_input_2, additional_input_2,
651 696
 	EXPECT ( 0x89, 0x42, 0xa5, 0x4f, 0x34, 0x9e, 0x28, 0x1b, 0x84, 0xaa,
652 697
 		 0x46, 0x95, 0x87, 0xfb, 0xdd, 0xaf, 0x9d, 0x11, 0x40, 0x82 ),
653 698
 	EXPECT ( 0x07, 0x73, 0x0e, 0x3c, 0xbf, 0xfd, 0x3c, 0xaf, 0xd7, 0xa8,
654 699
 		 0xaa, 0xe2, 0xbf, 0x01, 0xd6, 0x01, 0x43, 0x01, 0xe2, 0x4d ) );
655 700
 
656
-/** Test 8.6 : Retried second call to Generate */
657
-HMAC_DRBG_TEST_GENERATE ( generate_8_6,
701
+/** SHA-1 Test 8.6 : Retried second call to Generate */
702
+HMAC_DRBG_TEST_GENERATE ( sha1_generate_8_6, HMAC_DRBG_SHA1,
658 703
 	additional_input_empty,
659 704
 	EXPECT ( 0xbd, 0xe1, 0xb4, 0x6c, 0xdc, 0x54, 0x13, 0xb3, 0xd9, 0xf7,
660 705
 		 0x35, 0xac, 0xdb, 0x80, 0xb1, 0x3c, 0x57, 0xbf, 0xe4, 0x73 ),
@@ -724,69 +769,69 @@ static void hmac_drbg_test_exec ( void ) {
724 769
 	 * greater than the reseed_interval.
725 770
 	 */
726 771
 
727
-	/* Test 1 */
728
-	instantiate_ok ( &state, &instantiate_1 );
729
-	generate_ok ( &state, &generate_1_1 );
730
-	generate_ok ( &state, &generate_1_2 );
772
+	/* SHA-1 Test 1 */
773
+	instantiate_ok ( &state, &sha1_instantiate_1 );
774
+	generate_ok ( &state, &sha1_generate_1_1 );
775
+	generate_ok ( &state, &sha1_generate_1_2 );
731 776
 
732
-	/* Test 2 */
733
-	instantiate_ok ( &state, &instantiate_2 );
734
-	generate_ok ( &state, &generate_2_1 );
735
-	generate_ok ( &state, &generate_2_2 );
777
+	/* SHA-1 Test 2 */
778
+	instantiate_ok ( &state, &sha1_instantiate_2 );
779
+	generate_ok ( &state, &sha1_generate_2_1 );
780
+	generate_ok ( &state, &sha1_generate_2_2 );
736 781
 
737
-	/* Test 3 */
738
-	instantiate_ok ( &state, &instantiate_3 );
739
-	generate_ok ( &state, &generate_3_1 );
740
-	generate_ok ( &state, &generate_3_2 );
782
+	/* SHA-1 Test 3 */
783
+	instantiate_ok ( &state, &sha1_instantiate_3 );
784
+	generate_ok ( &state, &sha1_generate_3_1 );
785
+	generate_ok ( &state, &sha1_generate_3_2 );
741 786
 
742
-	/* Test 4 */
743
-	instantiate_ok ( &state, &instantiate_4 );
744
-	generate_ok ( &state, &generate_4_1 );
745
-	generate_ok ( &state, &generate_4_2 );
787
+	/* SHA-1 Test 4 */
788
+	instantiate_ok ( &state, &sha1_instantiate_4 );
789
+	generate_ok ( &state, &sha1_generate_4_1 );
790
+	generate_ok ( &state, &sha1_generate_4_2 );
746 791
 
747
-	/* Test 5 */
748
-	instantiate_ok ( &state, &instantiate_5 );
792
+	/* SHA-1 Test 5 */
793
+	instantiate_ok ( &state, &sha1_instantiate_5 );
749 794
 	force_reseed_required ( &state ); /* See above comments */
750
-	generate_fail_ok ( &state, &generate_fail_5_1 );
751
-	reseed_ok ( &state, &reseed_5_2 );
752
-	generate_ok ( &state, &generate_5_3 );
795
+	generate_fail_ok ( &state, &sha1_generate_fail_5_1 );
796
+	reseed_ok ( &state, &sha1_reseed_5_2 );
797
+	generate_ok ( &state, &sha1_generate_5_3 );
753 798
 	force_reseed_required ( &state ); /* See above comments */
754
-	generate_fail_ok ( &state, &generate_fail_5_4 );
755
-	reseed_ok ( &state, &reseed_5_5 );
756
-	generate_ok ( &state, &generate_5_6 );
799
+	generate_fail_ok ( &state, &sha1_generate_fail_5_4 );
800
+	reseed_ok ( &state, &sha1_reseed_5_5 );
801
+	generate_ok ( &state, &sha1_generate_5_6 );
757 802
 
758
-	/* Test 6 */
759
-	instantiate_ok ( &state, &instantiate_6 );
803
+	/* SHA-1 Test 6 */
804
+	instantiate_ok ( &state, &sha1_instantiate_6 );
760 805
 	force_reseed_required ( &state ); /* See above comments */
761
-	generate_fail_ok ( &state, &generate_fail_6_1 );
762
-	reseed_ok ( &state, &reseed_6_2 );
763
-	generate_ok ( &state, &generate_6_3 );
806
+	generate_fail_ok ( &state, &sha1_generate_fail_6_1 );
807
+	reseed_ok ( &state, &sha1_reseed_6_2 );
808
+	generate_ok ( &state, &sha1_generate_6_3 );
764 809
 	force_reseed_required ( &state ); /* See above comments */
765
-	generate_fail_ok ( &state, &generate_fail_6_4 );
766
-	reseed_ok ( &state, &reseed_6_5 );
767
-	generate_ok ( &state, &generate_6_6 );
810
+	generate_fail_ok ( &state, &sha1_generate_fail_6_4 );
811
+	reseed_ok ( &state, &sha1_reseed_6_5 );
812
+	generate_ok ( &state, &sha1_generate_6_6 );
768 813
 
769
-	/* Test 7 */
770
-	instantiate_ok ( &state, &instantiate_7 );
814
+	/* SHA-1 Test 7 */
815
+	instantiate_ok ( &state, &sha1_instantiate_7 );
771 816
 	force_reseed_required ( &state ); /* See above comments */
772
-	generate_fail_ok ( &state, &generate_fail_7_1 );
773
-	reseed_ok ( &state, &reseed_7_2 );
774
-	generate_ok ( &state, &generate_7_3 );
817
+	generate_fail_ok ( &state, &sha1_generate_fail_7_1 );
818
+	reseed_ok ( &state, &sha1_reseed_7_2 );
819
+	generate_ok ( &state, &sha1_generate_7_3 );
775 820
 	force_reseed_required ( &state ); /* See above comments */
776
-	generate_fail_ok ( &state, &generate_fail_7_4 );
777
-	reseed_ok ( &state, &reseed_7_5 );
778
-	generate_ok ( &state, &generate_7_6 );
821
+	generate_fail_ok ( &state, &sha1_generate_fail_7_4 );
822
+	reseed_ok ( &state, &sha1_reseed_7_5 );
823
+	generate_ok ( &state, &sha1_generate_7_6 );
779 824
 
780
-	/* Test 8 */
781
-	instantiate_ok ( &state, &instantiate_8 );
825
+	/* SHA-1 Test 8 */
826
+	instantiate_ok ( &state, &sha1_instantiate_8 );
782 827
 	force_reseed_required ( &state ); /* See above comments */
783
-	generate_fail_ok ( &state, &generate_fail_8_1 );
784
-	reseed_ok ( &state, &reseed_8_2 );
785
-	generate_ok ( &state, &generate_8_3 );
828
+	generate_fail_ok ( &state, &sha1_generate_fail_8_1 );
829
+	reseed_ok ( &state, &sha1_reseed_8_2 );
830
+	generate_ok ( &state, &sha1_generate_8_3 );
786 831
 	force_reseed_required ( &state ); /* See above comments */
787
-	generate_fail_ok ( &state, &generate_fail_8_4 );
788
-	reseed_ok ( &state, &reseed_8_5 );
789
-	generate_ok ( &state, &generate_8_6 );
832
+	generate_fail_ok ( &state, &sha1_generate_fail_8_4 );
833
+	reseed_ok ( &state, &sha1_reseed_8_5 );
834
+	generate_ok ( &state, &sha1_generate_8_6 );
790 835
 }
791 836
 
792 837
 /** HMAC_DRBG self-test */

Loading…
取消
儲存