Skip to content

Commit 37d5ad2

Browse files
hpennedhardy
authored andcommitted
Added test vectors for StdRng (Strombergson 2013)
1 parent f9d37d6 commit 37d5ad2

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

src/rngs/std.rs

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,203 @@ mod test {
149149

150150
assert_eq!([x0, x1], target);
151151
}
152+
153+
#[test]
154+
fn test_chacha_true_values_1() {
155+
// Source: Test Vectors for the Stream Cipher ChaCha
156+
// draft-strombergson-chacha-test-vectors-01
157+
// https://datatracker.ietf.org/doc/html/draft-strombergson-chacha-test-vectors-01
158+
// TC: all zero key and IV, rounds 12, 256-bit key
159+
160+
let seed = [0u8; 32];
161+
let mut rng = StdRng::from_seed(seed);
162+
163+
assert_eq!(rng_state(&rng), &[0u32; 12]);
164+
165+
let mut results = [0u8; 64];
166+
rng.fill_bytes(&mut results);
167+
#[rustfmt::skip]
168+
let expected = [
169+
0x9b, 0xf4, 0x9a, 0x6a, 0x07, 0x55, 0xf9, 0x53,
170+
0x81, 0x1f, 0xce, 0x12, 0x5f, 0x26, 0x83, 0xd5,
171+
0x04, 0x29, 0xc3, 0xbb, 0x49, 0xe0, 0x74, 0x14,
172+
0x7e, 0x00, 0x89, 0xa5, 0x2e, 0xae, 0x15, 0x5f,
173+
0x05, 0x64, 0xf8, 0x79, 0xd2, 0x7a, 0xe3, 0xc0,
174+
0x2c, 0xe8, 0x28, 0x34, 0xac, 0xfa, 0x8c, 0x79,
175+
0x3a, 0x62, 0x9f, 0x2c, 0xa0, 0xde, 0x69, 0x19,
176+
0x61, 0x0b, 0xe8, 0x2f, 0x41, 0x13, 0x26, 0xbe,
177+
];
178+
assert_eq!(results, expected);
179+
180+
rng.fill_bytes(&mut results);
181+
#[rustfmt::skip]
182+
let expected = [
183+
0x0b, 0xd5, 0x88, 0x41, 0x20, 0x3e, 0x74, 0xfe,
184+
0x86, 0xfc, 0x71, 0x33, 0x8c, 0xe0, 0x17, 0x3d,
185+
0xc6, 0x28, 0xeb, 0xb7, 0x19, 0xbd, 0xcb, 0xcc,
186+
0x15, 0x15, 0x85, 0x21, 0x4c, 0xc0, 0x89, 0xb4,
187+
0x42, 0x25, 0x8d, 0xcd, 0xa1, 0x4c, 0xf1, 0x11,
188+
0xc6, 0x02, 0xb8, 0x97, 0x1b, 0x8c, 0xc8, 0x43,
189+
0xe9, 0x1e, 0x46, 0xca, 0x90, 0x51, 0x51, 0xc0,
190+
0x27, 0x44, 0xa6, 0xb0, 0x17, 0xe6, 0x93, 0x16,
191+
];
192+
assert_eq!(results, expected);
193+
194+
assert_eq!(rng.0.get_word_pos(), 32);
195+
}
196+
197+
#[test]
198+
fn test_chacha_true_values_2() {
199+
// Source: Test Vectors for the Stream Cipher ChaCha
200+
// draft-strombergson-chacha-test-vectors-01
201+
// https://datatracker.ietf.org/doc/html/draft-strombergson-chacha-test-vectors-01
202+
// TC2: single bit set in key, all zero IV, rounds 12, 256-bit key
203+
204+
let mut seed = [0u8; 32];
205+
seed[0] = 1;
206+
let mut rng = StdRng::from_seed(seed);
207+
208+
let mut expected = [0u32; 12];
209+
expected[0] = 1;
210+
assert_eq!(rng_state(&rng), &expected);
211+
212+
let mut results = [0u8; 64];
213+
rng.fill_bytes(&mut results);
214+
#[rustfmt::skip]
215+
let expected = [
216+
0x12, 0x05, 0x6e, 0x59, 0x5d, 0x56, 0xb0, 0xf6,
217+
0xee, 0xf0, 0x90, 0xf0, 0xcd, 0x25, 0xa2, 0x09,
218+
0x49, 0x24, 0x8c, 0x27, 0x90, 0x52, 0x5d, 0x0f,
219+
0x93, 0x02, 0x18, 0xff, 0x0b, 0x4d, 0xdd, 0x10,
220+
0xa6, 0x00, 0x22, 0x39, 0xd9, 0xa4, 0x54, 0xe2,
221+
0x9e, 0x10, 0x7a, 0x7d, 0x06, 0xfe, 0xfd, 0xfe,
222+
0xf0, 0x21, 0x0f, 0xeb, 0xa0, 0x44, 0xf9, 0xf2,
223+
0x9b, 0x17, 0x72, 0xc9, 0x60, 0xdc, 0x29, 0xc0,
224+
];
225+
assert_eq!(results, expected);
226+
227+
rng.fill_bytes(&mut results);
228+
#[rustfmt::skip]
229+
let expected = [
230+
0x0c, 0x73, 0x66, 0xc5, 0xcb, 0xc6, 0x04, 0x24,
231+
0x0e, 0x66, 0x5e, 0xb0, 0x2a, 0x69, 0x37, 0x2a,
232+
0x7a, 0xf9, 0x79, 0xb2, 0x6f, 0xbb, 0x78, 0x09,
233+
0x2a, 0xc7, 0xc4, 0xb8, 0x80, 0x29, 0xa7, 0xc8,
234+
0x54, 0x51, 0x3b, 0xc2, 0x17, 0xbb, 0xfc, 0x7d,
235+
0x90, 0x43, 0x2e, 0x30, 0x8e, 0xba, 0x15, 0xaf,
236+
0xc6, 0x5a, 0xeb, 0x48, 0xef, 0x10, 0x0d, 0x56,
237+
0x01, 0xe6, 0xaf, 0xba, 0x25, 0x71, 0x17, 0xa9,
238+
];
239+
assert_eq!(results, expected);
240+
241+
assert_eq!(rng.0.get_word_pos(), 32);
242+
}
243+
244+
#[test]
245+
fn test_chacha_true_values_3() {
246+
// Source: Test Vectors for the Stream Cipher ChaCha
247+
// draft-strombergson-chacha-test-vectors-01
248+
// https://datatracker.ietf.org/doc/html/draft-strombergson-chacha-test-vectors-01
249+
// TC3: all zero key, single bit set in IV, rounds 12, 256-bit key
250+
251+
let seed = [0u8; 32];
252+
let mut rng = StdRng::from_seed(seed);
253+
rng.0.set_stream(1);
254+
255+
let mut expected = [0u32; 12];
256+
expected[10] = 1;
257+
assert_eq!(rng_state(&rng), &expected);
258+
259+
let mut results = [0u8; 64];
260+
rng.fill_bytes(&mut results);
261+
#[rustfmt::skip]
262+
let expected = [
263+
0x64, 0xb8, 0xbd, 0xf8, 0x7b, 0x82, 0x8c, 0x4b,
264+
0x6d, 0xba, 0xf7, 0xef, 0x69, 0x8d, 0xe0, 0x3d,
265+
0xf8, 0xb3, 0x3f, 0x63, 0x57, 0x14, 0x41, 0x8f,
266+
0x98, 0x36, 0xad, 0xe5, 0x9b, 0xe1, 0x29, 0x69,
267+
0x46, 0xc9, 0x53, 0xa0, 0xf3, 0x8e, 0xcf, 0xfc,
268+
0x9e, 0xcb, 0x98, 0xe8, 0x1d, 0x5d, 0x99, 0xa5,
269+
0xed, 0xfc, 0x8f, 0x9a, 0x0a, 0x45, 0xb9, 0xe4,
270+
0x1e, 0xf3, 0xb3, 0x1f, 0x02, 0x8f, 0x1d, 0x0f,
271+
];
272+
assert_eq!(results, expected);
273+
274+
rng.fill_bytes(&mut results);
275+
#[rustfmt::skip]
276+
let expected = [
277+
0x55, 0x9d, 0xb4, 0xa7, 0xf2, 0x22, 0xc4, 0x42,
278+
0xfe, 0x23, 0xb9, 0xa2, 0x59, 0x6a, 0x88, 0x28,
279+
0x51, 0x22, 0xee, 0x4f, 0x13, 0x63, 0x89, 0x6e,
280+
0xa7, 0x7c, 0xa1, 0x50, 0x91, 0x2a, 0xc7, 0x23,
281+
0xbf, 0xf0, 0x4b, 0x02, 0x6a, 0x2f, 0x80, 0x7e,
282+
0x03, 0xb2, 0x9c, 0x02, 0x07, 0x7d, 0x7b, 0x06,
283+
0xfc, 0x1a, 0xb9, 0x82, 0x7c, 0x13, 0xc8, 0x01,
284+
0x3a, 0x6d, 0x83, 0xbd, 0x3b, 0x52, 0xa2, 0x6f,
285+
];
286+
assert_eq!(results, expected);
287+
288+
assert_eq!(rng.0.get_word_pos(), 32);
289+
}
290+
291+
#[test]
292+
fn test_chacha_true_values_8() {
293+
// Source: Test Vectors for the Stream Cipher ChaCha
294+
// draft-strombergson-chacha-test-vectors-01
295+
// https://datatracker.ietf.org/doc/html/draft-strombergson-chacha-test-vectors-01
296+
// TC8: key: 'All your base are belong to us!', IV: IETF2013, rounds 12, 256-bit key
297+
298+
#[rustfmt::skip]
299+
let seed = [
300+
0xc4, 0x6e, 0xc1, 0xb1, 0x8c, 0xe8, 0xa8, 0x78,
301+
0x72, 0x5a, 0x37, 0xe7, 0x80, 0xdf, 0xb7, 0x35,
302+
0x1f, 0x68, 0xed, 0x2e, 0x19, 0x4c, 0x79, 0xfb,
303+
0xc6, 0xae, 0xbe, 0xe1, 0xa6, 0x67, 0x97, 0x5d,
304+
];
305+
let iv = [0x1a, 0xda, 0x31, 0xd5, 0xcf, 0x68, 0x82, 0x21];
306+
let mut rng = StdRng::from_seed(seed);
307+
rng.0.set_stream(u64::from_le_bytes(iv));
308+
309+
#[rustfmt::skip]
310+
let expected = [
311+
0xb1c16ec4, 0x78a8e88c,
312+
0xe7375a72, 0x35b7df80,
313+
0x2eed681f, 0xfb794c19,
314+
0xe1beaec6, 0x5d9767a6,
315+
0x00000000, 0x00000000,
316+
0xd531da1a, 0x218268cf,
317+
];
318+
assert_eq!(rng_state(&rng), &expected);
319+
320+
let mut results = [0u8; 64];
321+
rng.fill_bytes(&mut results);
322+
#[rustfmt::skip]
323+
let expected = [
324+
0x14, 0x82, 0x07, 0x27, 0x84, 0xbc, 0x6d, 0x06,
325+
0xb4, 0xe7, 0x3b, 0xdc, 0x11, 0x8b, 0xc0, 0x10,
326+
0x3c, 0x79, 0x76, 0x78, 0x6c, 0xa9, 0x18, 0xe0,
327+
0x69, 0x86, 0xaa, 0x25, 0x1f, 0x7e, 0x9c, 0xc1,
328+
0xb2, 0x74, 0x9a, 0x0a, 0x16, 0xee, 0x83, 0xb4,
329+
0x24, 0x2d, 0x2e, 0x99, 0xb0, 0x8d, 0x7c, 0x20,
330+
0x09, 0x2b, 0x80, 0xbc, 0x46, 0x6c, 0x87, 0x28,
331+
0x3b, 0x61, 0xb1, 0xb3, 0x9d, 0x0f, 0xfb, 0xab,
332+
];
333+
assert_eq!(results, expected);
334+
335+
rng.fill_bytes(&mut results);
336+
#[rustfmt::skip]
337+
let expected = [
338+
0xd9, 0x4b, 0x11, 0x6b, 0xc1, 0xeb, 0xdb, 0x32,
339+
0x9b, 0x9e, 0x4f, 0x62, 0x0d, 0xb6, 0x95, 0x54,
340+
0x4a, 0x8e, 0x3d, 0x9b, 0x68, 0x47, 0x3d, 0x0c,
341+
0x97, 0x5a, 0x46, 0xad, 0x96, 0x6e, 0xd6, 0x31,
342+
0xe4, 0x2a, 0xff, 0x53, 0x0a, 0xd5, 0xea, 0xc7,
343+
0xd8, 0x04, 0x7a, 0xdf, 0xa1, 0xe5, 0x11, 0x3c,
344+
0x91, 0xf3, 0xe3, 0xb8, 0x83, 0xf1, 0xd1, 0x89,
345+
0xac, 0x1c, 0x8f, 0xe0, 0x7b, 0xa5, 0xa4, 0x2b,
346+
];
347+
assert_eq!(results, expected);
348+
349+
assert_eq!(rng.0.get_word_pos(), 32);
350+
}
152351
}

0 commit comments

Comments
 (0)