Skip to content

Commit dedf194

Browse files
committed
adjusted legacy counter test to try different amounts of blocks
1 parent f984d8e commit dedf194

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

chacha20/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ zeroize = { version = "1.8.1", optional = true }
2929
cpufeatures = "0.2"
3030

3131
[dev-dependencies]
32-
chacha20_0_7 = { package = "chacha20", version = "0.7.0", features = ["legacy"] }
3332
cipher = { version = "=0.5.0-pre.4", features = ["dev"] }
3433
hex-literal = "0.4"
3534
rand_chacha = "0.3.1"

chacha20/tests/mod.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -234,43 +234,36 @@ mod legacy {
234234
}
235235
}
236236

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");
240240
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
242243
use rand_chacha::{ChaCha20Rng as OgRng, rand_core::{RngCore, SeedableRng}};
243244
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
246245
let mut rng = OgRng::from_seed([0u8; 32]);
247246

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();
251248
rng.fill_bytes(&mut expected);
252-
//og_cipher.apply_keystream(&mut expected);
253-
let mut result = TEST.clone();
249+
let mut result = test.clone();
254250
cipher.apply_keystream(&mut result);
255251
assert_eq!(expected, result);
256252

257253
const SEEK_POS: u64 = (u32::MAX - 10) as u64 * 64;
258254
cipher.seek(SEEK_POS);
259255
rng.set_word_pos(SEEK_POS as u128 / 4);
260-
og_cipher.seek(SEEK_POS);
261256

262257
let pos: u64 = cipher.current_pos();
263-
//assert_eq!(pos, og_cipher.current_pos());
264258
assert_eq!(pos, rng.get_word_pos() as u64 * 4);
265259
let block_pos = cipher.get_core().get_block_pos();
266260
assert!(block_pos < u32::MAX as u64);
267261
// Apply keystream blocks until some point after the u32 boundary
268-
for i in 1..80 {
262+
for i in 1..16 {
269263
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();
271265
rng.fill_bytes(&mut expected);
272-
//og_cipher.apply_keystream(&mut expected);
273-
let mut result = TEST.clone();
266+
let mut result = test.clone();
274267
cipher.apply_keystream(&mut result);
275268
if expected != result {
276269
let mut index: usize = 0;
@@ -286,7 +279,7 @@ mod legacy {
286279
};
287280
panic!("Index {} did not match;\n iteration: {}\n expected: {} != {}\nstart block pos - u32::MAX: {}", index, i, expected_u8, found_u8, starting_block_pos);
288281
}
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;
290283
assert!(expected_block_pos == cipher.get_core().get_block_pos(),
291284
"Block pos did not increment as expected; Expected block pos: {}\n actual block_pos: {}\n iteration: {}",
292285
expected_block_pos,
@@ -296,6 +289,21 @@ mod legacy {
296289
}
297290
// this test assures us that the counter is in fact over u32::MAX, in
298291
// 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]);
300308
}
301309
}

0 commit comments

Comments
 (0)