@@ -234,43 +234,36 @@ mod legacy {
234
234
}
235
235
}
236
236
237
- /// Tests the 64-bit counter
238
- # [ test]
239
- fn legacy_64_bit_counter ( ) {
237
+ /// Tests the 64-bit counter with a given amount of test blocks
238
+ fn legacy_counter_over_u32_max < const N : usize > ( test : & [ u8 ; N ] ) {
239
+ assert ! ( N % 64 == 0 , "N should be a multiple of 64" ) ;
240
240
use cipher:: StreamCipherSeekCore ;
241
- use chacha20_0_7:: { ChaCha20Legacy as OgLegacy , LegacyNonce as OgLegacyNonce , cipher:: { NewCipher , StreamCipher , StreamCipherSeek } } ;
241
+ // using rand_chacha v0.3 because it is already a dev-dependency, and
242
+ // it uses a 64-bit counter
242
243
use rand_chacha:: { ChaCha20Rng as OgRng , rand_core:: { RngCore , SeedableRng } } ;
243
244
let mut cipher = ChaCha20Legacy :: new ( & [ 0u8 ; 32 ] . into ( ) , & LegacyNonce :: from ( [ 0u8 ; 8 ] ) ) ;
244
- let mut og_cipher = OgLegacy :: new ( & [ 0u8 ; 32 ] . into ( ) , & OgLegacyNonce :: from ( [ 0u8 ; 8 ] ) ) ;
245
- //let mut rng = ChaCha20Rng
246
245
let mut rng = OgRng :: from_seed ( [ 0u8 ; 32 ] ) ;
247
246
248
- const TEST_BLOCKS : usize = 4 ;
249
- const TEST : [ u8 ; 64 * TEST_BLOCKS ] = [ 0u8 ; 64 * TEST_BLOCKS ] ;
250
- let mut expected = TEST . clone ( ) ;
247
+ let mut expected = test. clone ( ) ;
251
248
rng. fill_bytes ( & mut expected) ;
252
- //og_cipher.apply_keystream(&mut expected);
253
- let mut result = TEST . clone ( ) ;
249
+ let mut result = test. clone ( ) ;
254
250
cipher. apply_keystream ( & mut result) ;
255
251
assert_eq ! ( expected, result) ;
256
252
257
253
const SEEK_POS : u64 = ( u32:: MAX - 10 ) as u64 * 64 ;
258
254
cipher. seek ( SEEK_POS ) ;
259
255
rng. set_word_pos ( SEEK_POS as u128 / 4 ) ;
260
- og_cipher. seek ( SEEK_POS ) ;
261
256
262
257
let pos: u64 = cipher. current_pos ( ) ;
263
- //assert_eq!(pos, og_cipher.current_pos());
264
258
assert_eq ! ( pos, rng. get_word_pos( ) as u64 * 4 ) ;
265
259
let block_pos = cipher. get_core ( ) . get_block_pos ( ) ;
266
260
assert ! ( block_pos < u32 :: MAX as u64 ) ;
267
261
// Apply keystream blocks until some point after the u32 boundary
268
- for i in 1 ..80 {
262
+ for i in 1 ..16 {
269
263
let starting_block_pos = cipher. get_core ( ) . get_block_pos ( ) as i64 - u32:: MAX as i64 ;
270
- let mut expected = TEST . clone ( ) ;
264
+ let mut expected = test . clone ( ) ;
271
265
rng. fill_bytes ( & mut expected) ;
272
- //og_cipher.apply_keystream(&mut expected);
273
- let mut result = TEST . clone ( ) ;
266
+ let mut result = test. clone ( ) ;
274
267
cipher. apply_keystream ( & mut result) ;
275
268
if expected != result {
276
269
let mut index: usize = 0 ;
@@ -286,7 +279,7 @@ mod legacy {
286
279
} ;
287
280
panic ! ( "Index {} did not match;\n iteration: {}\n expected: {} != {}\n start block pos - u32::MAX: {}" , index, i, expected_u8, found_u8, starting_block_pos) ;
288
281
}
289
- let expected_block_pos = block_pos + i * TEST_BLOCKS as u64 ;
282
+ let expected_block_pos = block_pos + i * ( test . len ( ) / 64 ) as u64 ;
290
283
assert ! ( expected_block_pos == cipher. get_core( ) . get_block_pos( ) ,
291
284
"Block pos did not increment as expected; Expected block pos: {}\n actual block_pos: {}\n iteration: {}" ,
292
285
expected_block_pos,
@@ -296,6 +289,21 @@ mod legacy {
296
289
}
297
290
// this test assures us that the counter is in fact over u32::MAX, in
298
291
// case we change some of the parameters
299
- assert ! ( cipher. get_core( ) . get_block_pos( ) > u32 :: MAX as u64 ) ;
292
+ assert ! ( cipher. get_core( ) . get_block_pos( ) > u32 :: MAX as u64 , "The 64-bit counter test did not surpass u32::MAX" ) ;
293
+ }
294
+
295
+ /// Runs the legacy_64_bit_counter test with different-sized arrays so that
296
+ /// both `gen_ks_block` and `gen_par_ks_blocks` are called with varying
297
+ /// starting positions.
298
+ #[ test]
299
+ fn legacy_64_bit_counter ( ) {
300
+ legacy_counter_over_u32_max ( & [ 0u8 ; 64 * 1 ] ) ;
301
+ legacy_counter_over_u32_max ( & [ 0u8 ; 64 * 2 ] ) ;
302
+ legacy_counter_over_u32_max ( & [ 0u8 ; 64 * 3 ] ) ;
303
+ legacy_counter_over_u32_max ( & [ 0u8 ; 64 * 4 ] ) ;
304
+ legacy_counter_over_u32_max ( & [ 0u8 ; 64 * 5 ] ) ;
305
+ legacy_counter_over_u32_max ( & [ 0u8 ; 64 * 6 ] ) ;
306
+ legacy_counter_over_u32_max ( & [ 0u8 ; 64 * 7 ] ) ;
307
+ legacy_counter_over_u32_max ( & [ 0u8 ; 64 * 8 ] ) ;
300
308
}
301
309
}
0 commit comments