@@ -36,6 +36,18 @@ const STATE_WORDS: usize = 16;
36
36
pub struct ChaCha < const ROUNDS : usize > {
37
37
state : [ u32 ; STATE_WORDS ] ,
38
38
counter : u64 ,
39
+ /// ChaCha is a stream cipher that works with 64-byte blocks.
40
+ /// It means that
41
+ /// Thus calling multiple times `xor_keystream`:
42
+ /// xor_keystream(plaintext[0..3]), xor_keystream(plaintext[3..50]), xor_keystream(plaintext[50..150]);
43
+ /// Should be equal to calling it only once:
44
+ /// xor_keystream(plaintext[0..150]);
45
+ /// For that, we keep the last computed keystream block, as well as an index of where in the keystream
46
+ /// we were after completing the last call.
47
+ /// Then, when calling `xor_keystream` again, we first check if there is sone leftover form the last
48
+ /// keystream.
49
+ /// NOTE: the `last_keystream_block` is valid only if the previous call to `xor_keystream` had
50
+ /// an input.len() % 64 != 0
39
51
last_keystream_block : [ u8 ; 64 ] ,
40
52
last_keystream_block_index : usize ,
41
53
}
@@ -404,8 +416,9 @@ Expected: {}",
404
416
// thus:
405
417
// cipher.xor_keystream(plaintext[0..10])
406
418
// cipher.xor_keystream(plaintext[10..30])
419
+ // cipher.xor_keystream(plaintext[30..5])
407
420
// should be equal to:
408
- // cipher.xor_keystream(plaintext[0..30 ])
421
+ // cipher.xor_keystream(plaintext[0..35 ])
409
422
410
423
let mut cipher = ChaCha :: < 20 > :: new ( & test. key , & test. nonce ) ;
411
424
cipher. xor_keystream ( & mut plaintext) ;
0 commit comments