@@ -21,7 +21,7 @@ cipher::stream_cipher_seek_test!(chacha20legacy_seek, ChaCha20Legacy);
2121#[ cfg( feature = "cipher" ) ]
2222mod chacha20test {
2323 use chacha20:: { ChaCha20 , KeyIvInit } ;
24- use cipher:: { StreamCipher , StreamCipherSeek } ;
24+ use cipher:: StreamCipher ;
2525 use hex_literal:: hex;
2626
2727 //
@@ -94,85 +94,6 @@ mod chacha20test {
9494 cipher. apply_keystream ( & mut buf) ;
9595 assert_eq ! ( & buf[ ..] , & CIPHERTEXT [ ..] ) ;
9696 }
97-
98- macro_rules! impl_chacha20_potential_counter_issue {
99- ( $name: ident, $num_blocks: literal) => {
100- #[ test]
101- fn $name( ) {
102- let mut cipher = ChaCha20 :: new( & KEY . into( ) , & IV . into( ) ) ;
103- let mut first_4_blocks = [ 0u8 ; 256 ] ;
104- assert_eq!( cipher. current_pos:: <u64 >( ) , 0 ) ;
105- cipher. apply_keystream( & mut first_4_blocks) ;
106-
107- let mut buf_1 = [ 0u8 ; $num_blocks * 64 ] ;
108- let mut buf_2 = [ 0u8 ; $num_blocks * 64 + 1 ] ;
109-
110- // seek to end of keystream
111- let pos = ( 1 << 32 ) * 64 - $num_blocks * 64 - 64 ;
112- cipher. try_seek( pos) . unwrap( ) ;
113- assert_eq!( cipher. current_pos:: <u64 >( ) , pos) ;
114-
115- // overshoot keystream length
116- let applied_keystream = cipher. try_apply_keystream( & mut buf_2) ;
117- assert_eq!( applied_keystream. is_err( ) , true ) ;
118-
119- // exhaust keystream
120- cipher. apply_keystream( & mut buf_1) ;
121-
122- // verify that we cannot write another byte
123- assert!( cipher. try_apply_keystream( & mut [ 0u8 ; 1 ] ) . is_err( ) ) ;
124-
125- // seek to beginning and check if the first block is the same as before
126- cipher. seek( 0 ) ;
127- assert_eq!( cipher. current_pos:: <u64 >( ) , 0 ) ;
128- cipher. apply_keystream( & mut first_4_blocks) ;
129-
130- // if this assert fails, exhausting the keystream increments
131- // state[13], resulting in a different keystream when it
132- // should be the same
133- assert_eq!( first_4_blocks, [ 0u8 ; 256 ] ) ;
134- }
135- } ;
136- }
137-
138- impl_chacha20_potential_counter_issue ! ( chacha20_potential_counter_issue_v2, 4 ) ;
139- impl_chacha20_potential_counter_issue ! ( chacha20_potential_counter_issue_v3, 11 ) ;
140- impl_chacha20_potential_counter_issue ! ( chacha20_potential_counter_issue_v4, 10 ) ;
141- impl_chacha20_potential_counter_issue ! ( chacha20_potential_counter_issue_v5, 9 ) ;
142- impl_chacha20_potential_counter_issue ! ( chacha20_potential_counter_issue_v6, 8 ) ;
143- impl_chacha20_potential_counter_issue ! ( chacha20_potential_counter_issue_v7, 7 ) ;
144-
145- #[ test]
146- fn chacha20core_counter_overflow ( ) {
147- use chacha20:: { ChaChaCore , R20 , variants:: Ietf } ;
148- use cipher:: { StreamCipherCore , StreamCipherSeekCore } ;
149- let mut core = ChaChaCore :: < R20 , Ietf > :: new ( & KEY . into ( ) , & IV . into ( ) ) ;
150-
151- // observe the first block two "different" ways
152- let mut first_block_observation_1 = Default :: default ( ) ;
153- core. write_keystream_block ( & mut first_block_observation_1) ;
154- core. set_block_pos ( 0 ) ;
155- let mut first_block_observation_2 = Default :: default ( ) ;
156- core. write_keystream_block ( & mut first_block_observation_2) ;
157- // proof that setting block pos to 0 results in first_block_observation_1
158- assert_eq ! ( first_block_observation_1, first_block_observation_2) ;
159-
160- // try to make the counter overflow/wrap
161- core. set_block_pos ( u32:: MAX ) ;
162- core. write_keystream_block ( & mut Default :: default ( ) ) ;
163-
164- let mut first_block_observation_3 = Default :: default ( ) ;
165- core. write_keystream_block ( & mut first_block_observation_3) ;
166- // fails if the counter doesn't wrap
167- assert_eq ! ( first_block_observation_1, first_block_observation_3) ;
168-
169- core. set_block_pos ( 0 ) ;
170- let mut first_block_observation_4 = Default :: default ( ) ;
171- core. write_keystream_block ( & mut first_block_observation_4) ;
172-
173- // fails when `state[13]` changes
174- assert_eq ! ( first_block_observation_1, first_block_observation_4)
175- }
17697}
17798
17899#[ rustfmt:: skip]
@@ -312,35 +233,4 @@ mod legacy {
312233 }
313234 }
314235 }
315-
316- #[ test]
317- fn chacha20core_counter_overflow ( ) {
318- use cipher:: { StreamCipherCore , StreamCipherSeekCore } ;
319- use chacha20:: { ChaChaCore , R20 , variants:: Legacy } ;
320- let mut core = ChaChaCore :: < R20 , Legacy > :: new ( & KEY_LONG . into ( ) , & IV_LONG . into ( ) ) ;
321-
322- // observe the first block two "different" ways
323- let mut first_block_observation_1 = Default :: default ( ) ;
324- core. write_keystream_block ( & mut first_block_observation_1) ;
325- core. set_block_pos ( 0 ) ;
326- let mut first_block_observation_2 = Default :: default ( ) ;
327- core. write_keystream_block ( & mut first_block_observation_2) ;
328- // proof that setting block pos to 0 results in first_block_observation_1
329- assert_eq ! ( first_block_observation_1, first_block_observation_2) ;
330-
331- // try to make the counter wrap
332- core. set_block_pos ( u64:: MAX ) ;
333- core. write_keystream_block ( & mut Default :: default ( ) ) ;
334-
335- let mut first_block_observation_3 = Default :: default ( ) ;
336- core. write_keystream_block ( & mut first_block_observation_3) ;
337- // fails if the counter doesn't wrap
338- assert_eq ! ( first_block_observation_1, first_block_observation_3) ;
339-
340- core. set_block_pos ( 0 ) ;
341- let mut first_block_observation_4 = Default :: default ( ) ;
342- core. write_keystream_block ( & mut first_block_observation_4) ;
343-
344- assert_eq ! ( first_block_observation_1, first_block_observation_4)
345- }
346236}
0 commit comments