Skip to content

Commit 7b73d5c

Browse files
committed
reverted 'remaining_blocks()' and backends due to #444
1 parent d42b703 commit 7b73d5c

File tree

6 files changed

+16
-28
lines changed

6 files changed

+16
-28
lines changed

chacha20/src/backends/avx2.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ where
5555
f.call(&mut backend);
5656

5757
state[12] = _mm256_extract_epi32(backend.ctr[0], 0) as u32;
58-
if size_of::<V::Counter>() == 8 {
59-
state[13] = _mm256_extract_epi32(backend.ctr[0], 1) as u32;
60-
}
58+
state[13] = _mm256_extract_epi32(backend.ctr[0], 1) as u32;
6159
}
6260

6361
#[inline]

chacha20/src/backends/neon.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,10 @@ where
7070

7171
f.call(&mut backend);
7272

73-
if size_of::<V::Counter>() == 8 {
74-
vst1q_u64(
75-
state.as_mut_ptr().offset(12) as *mut u64,
76-
vreinterpretq_u64_u32(backend.state[3]),
77-
);
78-
} else {
79-
state[12] = vgetq_lane_u32(backend.state[3], 0);
80-
}
73+
vst1q_u64(
74+
state.as_mut_ptr().offset(12) as *mut u64,
75+
vreinterpretq_u64_u32(backend.state[3]),
76+
);
8177
}
8278

8379
#[inline]

chacha20/src/backends/soft.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<'_, R, V> {
3333
self.0.state[12] = v;
3434
} else {
3535
self.0.state[12] = 0;
36-
if size_of::<V::Counter>() == 8 {
37-
self.0.state[13] = self.0.state[13].wrapping_add(1);
38-
}
36+
self.0.state[13] = self.0.state[13].wrapping_add(1);
3937
}
4038

4139
for (chunk, val) in block.chunks_exact_mut(4).zip(res.iter()) {

chacha20/src/backends/sse2.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ where
4343
f.call(&mut backend);
4444

4545
state[12] = _mm_cvtsi128_si32(backend.v[3]) as u32;
46-
if size_of::<V::Counter>() == 8 {
47-
state[13] = _mm_extract_epi32(backend.v[3], 1) as u32;
48-
}
46+
state[13] = _mm_extract_epi32(backend.v[3], 1) as u32;
4947
}
5048

5149
struct Backend<R: Rounds> {

chacha20/src/variants.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@ impl Variant for Ietf {
4545
}
4646
#[inline(always)]
4747
fn remaining_blocks(block_pos: Self::Counter) -> Option<usize> {
48-
let total_blocks = 1u64 << 32;
49-
let rem = total_blocks - block_pos as u64;
50-
#[cfg(target_pointer_width = "32")]
51-
if rem > usize::MAX as u64 {
52-
return None;
53-
}
54-
rem.try_into().ok()
48+
(u32::MAX - block_pos).try_into().ok()
5549
}
5650
}
5751

@@ -76,8 +70,9 @@ impl Variant for Legacy {
7670
}
7771
#[inline(always)]
7872
fn remaining_blocks(block_pos: Self::Counter) -> Option<usize> {
79-
let remaining = (1u128 << 64) - block_pos as u128;
80-
if remaining > usize::MAX as u128 {
73+
let remaining = u64::MAX - block_pos;
74+
#[cfg(target_pointer_width = "32")]
75+
if remaining > usize::MAX as u64 {
8176
return None;
8277
}
8378
remaining.try_into().ok()

chacha20/tests/kats.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ mod chacha20test {
108108
let mut buf_2 = [0u8; $num_blocks * 64 + 1];
109109

110110
// seek to end of keystream
111-
let pos = (1 << 32) * 64 - $num_blocks * 64;
111+
let pos = (1 << 32) * 64 - $num_blocks * 64 - 64;
112112
cipher.try_seek(pos).unwrap();
113113
assert_eq!(cipher.current_pos::<u64>(), pos);
114114

@@ -119,8 +119,11 @@ mod chacha20test {
119119
// exhaust keystream
120120
cipher.apply_keystream(&mut buf_1);
121121

122+
// verify that we cannot write another byte
123+
assert!(cipher.try_apply_keystream(&mut [0u8; 1]).is_err());
124+
122125
// seek to beginning and check if the first block is the same as before
123-
//cipher.seek(0);
126+
cipher.seek(0);
124127
assert_eq!(cipher.current_pos::<u64>(), 0);
125128
cipher.apply_keystream(&mut first_4_blocks);
126129

0 commit comments

Comments
 (0)