@@ -61,31 +61,29 @@ mod real_chachapoly {
61
61
}
62
62
63
63
pub fn encrypt ( & mut self , input : & [ u8 ] , output : & mut [ u8 ] , out_tag : & mut [ u8 ] ) {
64
+ assert ! ( input. len( ) == output. len( ) ) ;
65
+ assert ! ( self . finished == false ) ;
64
66
self . cipher . process ( input, output) ;
65
- self . encrypt_inner ( input, Some ( output) , Some ( out_tag) ) ;
66
- }
67
-
68
- pub fn encrypt_in_place ( & mut self , input_output : & mut [ u8 ] , out_tag : Option < & mut [ u8 ] > ) {
69
- self . cipher . process_in_place ( input_output) ;
70
- self . encrypt_inner ( input_output, None , out_tag) ;
67
+ self . data_len += input. len ( ) ;
68
+ self . mac . input ( output) ;
69
+ ChaCha20Poly1305RFC :: pad_mac_16 ( & mut self . mac , self . data_len ) ;
70
+ self . finished = true ;
71
+ self . mac . input ( & self . aad_len . to_le_bytes ( ) ) ;
72
+ self . mac . input ( & ( self . data_len as u64 ) . to_le_bytes ( ) ) ;
73
+ self . mac . raw_result ( out_tag) ;
71
74
}
72
75
73
- // Encrypt in place, and fill in the tag if it's provided. If the tag is not provided, then
74
- // `finish_and_get_tag` may be called to check it later .
75
- fn encrypt_inner ( & mut self , input : & [ u8 ] , output : Option < & mut [ u8 ] > , out_tag : Option < & mut [ u8 ] > ) {
76
+ // ChaCha20Poly1305-encrypt `input_output` in- place. To finish and calculate the tag, use
77
+ // `finish_and_get_tag` below .
78
+ pub fn encrypt_in_place ( & mut self , input_output : & mut [ u8 ] ) {
76
79
assert ! ( self . finished == false ) ;
77
- if let Some ( output) = output {
78
- assert ! ( input. len( ) == output. len( ) ) ;
79
- self . mac . input ( output) ;
80
- } else {
81
- self . mac . input ( input) ;
82
- }
83
- self . data_len += input. len ( ) ;
84
- if let Some ( tag) = out_tag {
85
- self . finish_and_get_tag ( tag) ;
86
- }
80
+ self . data_len += input_output. len ( ) ;
81
+ self . cipher . process_in_place ( input_output) ;
82
+ self . mac . input ( input_output) ;
87
83
}
88
84
85
+ // If we were previously encrypting with `encrypt_in_place`, this method can be used to finish
86
+ // encrypting and calculate the tag.
89
87
pub fn finish_and_get_tag ( & mut self , out_tag : & mut [ u8 ] ) {
90
88
ChaCha20Poly1305RFC :: pad_mac_16 ( & mut self . mac , self . data_len ) ;
91
89
self . finished = true ;
@@ -136,7 +134,7 @@ impl<'a, W: Writer> Writer for ChaChaPolyWriter<'a, W> {
136
134
for i in 0 ..num_writes {
137
135
let mut write_buffer = [ 0 ; 8192 ] ;
138
136
let bytes_written = ( & mut write_buffer[ ..] ) . write ( & src[ i * 8192 ..] ) ?;
139
- self . chacha . encrypt_in_place ( & mut write_buffer[ ..bytes_written] , None ) ;
137
+ self . chacha . encrypt_in_place ( & mut write_buffer[ ..bytes_written] ) ;
140
138
self . write . write_all ( & write_buffer[ ..bytes_written] ) ?;
141
139
}
142
140
Ok ( ( ) )
@@ -204,7 +202,7 @@ mod fuzzy_chachapoly {
204
202
self . finished = true ;
205
203
}
206
204
207
- pub fn encrypt_in_place ( & mut self , _input_output : & mut [ u8 ] , out_tag : Option < & mut [ u8 ] > ) {
205
+ pub fn encrypt_in_place ( & mut self , _input_output : & mut [ u8 ] ) {
208
206
assert ! ( self . finished == false ) ;
209
207
if let Some ( tag) = out_tag {
210
208
tag. copy_from_slice ( & self . tag ) ;
0 commit comments