@@ -159,6 +159,20 @@ mod real_chacha {
159159 chacha_bytes
160160 }
161161
162+ /// Encrypts `src` into `dest` using a single block from a ChaCha stream. Passing `dest` as
163+ /// `src` in a second call will decrypt it.
164+ pub fn encrypt_single_block (
165+ key : & [ u8 ; 32 ] , nonce : & [ u8 ; 16 ] , dest : & mut [ u8 ] , src : & [ u8 ]
166+ ) {
167+ debug_assert_eq ! ( dest. len( ) , src. len( ) ) ;
168+ debug_assert ! ( dest. len( ) <= 32 ) ;
169+
170+ let block = ChaCha20 :: get_single_block ( key, nonce) ;
171+ for i in 0 ..dest. len ( ) {
172+ dest[ i] = block[ i] ^ src[ i] ;
173+ }
174+ }
175+
162176 fn expand ( key : & [ u8 ] , nonce : & [ u8 ] ) -> ChaChaState {
163177 let constant = match key. len ( ) {
164178 16 => b"expand 16-byte k" ,
@@ -290,6 +304,10 @@ mod fuzzy_chacha {
290304 [ 0 ; 32 ]
291305 }
292306
307+ pub fn encrypt_single_block (
308+ _key : & [ u8 ; 32 ] , _nonce : & [ u8 ; 16 ] , _dest : & mut [ u8 ] , _src : & [ u8 ]
309+ ) { }
310+
293311 pub fn process ( & mut self , input : & [ u8 ] , output : & mut [ u8 ] ) {
294312 output. copy_from_slice ( input) ;
295313 }
@@ -618,4 +636,27 @@ mod test {
618636
619637 assert_eq ! ( ChaCha20 :: get_single_block( & key, & nonce_16bytes) , block_bytes) ;
620638 }
639+
640+ #[ test]
641+ fn encrypt_single_block ( ) {
642+ let key = [
643+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
644+ 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f ,
645+ 0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 ,
646+ 0x18 , 0x19 , 0x1a , 0x1b , 0x1c , 0x1d , 0x1e , 0x1f ,
647+ ] ;
648+ let nonce = [
649+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
650+ 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f ,
651+ ] ;
652+ let bytes = [ 1 ; 32 ] ;
653+
654+ let mut encrypted_bytes = [ 0 ; 32 ] ;
655+ ChaCha20 :: encrypt_single_block ( & key, & nonce, & mut encrypted_bytes, & bytes) ;
656+
657+ let mut decrypted_bytes = [ 0 ; 32 ] ;
658+ ChaCha20 :: encrypt_single_block ( & key, & nonce, & mut decrypted_bytes, & encrypted_bytes) ;
659+
660+ assert_eq ! ( bytes, decrypted_bytes) ;
661+ }
621662}
0 commit comments