13
13
#[ cfg( not( fuzzing) ) ]
14
14
mod real_chachapoly {
15
15
use super :: super :: chacha20:: ChaCha20 ;
16
- use super :: super :: poly1305:: Poly1305 ;
17
16
use super :: super :: fixed_time_eq;
17
+ use super :: super :: poly1305:: Poly1305 ;
18
18
19
19
#[ derive( Clone , Copy ) ]
20
20
pub struct ChaCha20Poly1305RFC {
@@ -70,7 +70,9 @@ mod real_chachapoly {
70
70
self . mac . raw_result ( out_tag) ;
71
71
}
72
72
73
- pub fn encrypt_full_message_in_place ( & mut self , input_output : & mut [ u8 ] , out_tag : & mut [ u8 ] ) {
73
+ pub fn encrypt_full_message_in_place (
74
+ & mut self , input_output : & mut [ u8 ] , out_tag : & mut [ u8 ] ,
75
+ ) {
74
76
self . encrypt_in_place ( input_output) ;
75
77
self . finish_and_get_tag ( out_tag) ;
76
78
}
@@ -98,7 +100,9 @@ mod real_chachapoly {
98
100
/// Decrypt the `input`, checking the given `tag` prior to writing the decrypted contents
99
101
/// into `output`. Note that, because `output` is not touched until the `tag` is checked,
100
102
/// this decryption is *variable time*.
101
- pub fn variable_time_decrypt ( & mut self , input : & [ u8 ] , output : & mut [ u8 ] , tag : & [ u8 ] ) -> Result < ( ) , ( ) > {
103
+ pub fn variable_time_decrypt (
104
+ & mut self , input : & [ u8 ] , output : & mut [ u8 ] , tag : & [ u8 ] ,
105
+ ) -> Result < ( ) , ( ) > {
102
106
assert ! ( input. len( ) == output. len( ) ) ;
103
107
assert ! ( !self . finished) ;
104
108
@@ -111,7 +115,7 @@ mod real_chachapoly {
111
115
self . mac . input ( & self . aad_len . to_le_bytes ( ) ) ;
112
116
self . mac . input ( & ( self . data_len as u64 ) . to_le_bytes ( ) ) ;
113
117
114
- let mut calc_tag = [ 0u8 ; 16 ] ;
118
+ let mut calc_tag = [ 0u8 ; 16 ] ;
115
119
self . mac . raw_result ( & mut calc_tag) ;
116
120
if fixed_time_eq ( & calc_tag, tag) {
117
121
self . cipher . process ( input, output) ;
@@ -121,9 +125,15 @@ mod real_chachapoly {
121
125
}
122
126
}
123
127
124
- pub fn check_decrypt_in_place ( & mut self , input_output : & mut [ u8 ] , tag : & [ u8 ] ) -> Result < ( ) , ( ) > {
128
+ pub fn check_decrypt_in_place (
129
+ & mut self , input_output : & mut [ u8 ] , tag : & [ u8 ] ,
130
+ ) -> Result < ( ) , ( ) > {
125
131
self . decrypt_in_place ( input_output) ;
126
- if self . finish_and_check_tag ( tag) { Ok ( ( ) ) } else { Err ( ( ) ) }
132
+ if self . finish_and_check_tag ( tag) {
133
+ Ok ( ( ) )
134
+ } else {
135
+ Err ( ( ) )
136
+ }
127
137
}
128
138
129
139
/// Decrypt in place, without checking the tag. Use `finish_and_check_tag` to check it
@@ -146,7 +156,7 @@ mod real_chachapoly {
146
156
self . mac . input ( & self . aad_len . to_le_bytes ( ) ) ;
147
157
self . mac . input ( & ( self . data_len as u64 ) . to_le_bytes ( ) ) ;
148
158
149
- let mut calc_tag = [ 0u8 ; 16 ] ;
159
+ let mut calc_tag = [ 0u8 ; 16 ] ;
150
160
self . mac . raw_result ( & mut calc_tag) ;
151
161
if fixed_time_eq ( & calc_tag, tag) {
152
162
true
@@ -177,10 +187,7 @@ mod fuzzy_chachapoly {
177
187
let mut tag = [ 0 ; 16 ] ;
178
188
tag. copy_from_slice ( & key[ 0 ..16 ] ) ;
179
189
180
- ChaCha20Poly1305RFC {
181
- tag,
182
- finished : false ,
183
- }
190
+ ChaCha20Poly1305RFC { tag, finished : false }
184
191
}
185
192
186
193
pub fn encrypt ( & mut self , input : & [ u8 ] , output : & mut [ u8 ] , out_tag : & mut [ u8 ] ) {
@@ -192,7 +199,9 @@ mod fuzzy_chachapoly {
192
199
self . finished = true ;
193
200
}
194
201
195
- pub fn encrypt_full_message_in_place ( & mut self , input_output : & mut [ u8 ] , out_tag : & mut [ u8 ] ) {
202
+ pub fn encrypt_full_message_in_place (
203
+ & mut self , input_output : & mut [ u8 ] , out_tag : & mut [ u8 ] ,
204
+ ) {
196
205
self . encrypt_in_place ( input_output) ;
197
206
self . finish_and_get_tag ( out_tag) ;
198
207
}
@@ -207,27 +216,39 @@ mod fuzzy_chachapoly {
207
216
self . finished = true ;
208
217
}
209
218
210
- pub fn variable_time_decrypt ( & mut self , input : & [ u8 ] , output : & mut [ u8 ] , tag : & [ u8 ] ) -> Result < ( ) , ( ) > {
219
+ pub fn variable_time_decrypt (
220
+ & mut self , input : & [ u8 ] , output : & mut [ u8 ] , tag : & [ u8 ] ,
221
+ ) -> Result < ( ) , ( ) > {
211
222
assert ! ( input. len( ) == output. len( ) ) ;
212
223
assert ! ( self . finished == false ) ;
213
224
214
- if tag[ ..] != self . tag [ ..] { return Err ( ( ) ) ; }
225
+ if tag[ ..] != self . tag [ ..] {
226
+ return Err ( ( ) ) ;
227
+ }
215
228
output. copy_from_slice ( input) ;
216
229
self . finished = true ;
217
230
Ok ( ( ) )
218
231
}
219
232
220
- pub fn check_decrypt_in_place ( & mut self , input_output : & mut [ u8 ] , tag : & [ u8 ] ) -> Result < ( ) , ( ) > {
233
+ pub fn check_decrypt_in_place (
234
+ & mut self , input_output : & mut [ u8 ] , tag : & [ u8 ] ,
235
+ ) -> Result < ( ) , ( ) > {
221
236
self . decrypt_in_place ( input_output) ;
222
- if self . finish_and_check_tag ( tag) { Ok ( ( ) ) } else { Err ( ( ) ) }
237
+ if self . finish_and_check_tag ( tag) {
238
+ Ok ( ( ) )
239
+ } else {
240
+ Err ( ( ) )
241
+ }
223
242
}
224
243
225
244
pub ( in super :: super ) fn decrypt_in_place ( & mut self , _input : & mut [ u8 ] ) {
226
245
assert ! ( self . finished == false ) ;
227
246
}
228
247
229
248
pub ( in super :: super ) fn finish_and_check_tag ( & mut self , tag : & [ u8 ] ) -> bool {
230
- if tag[ ..] != self . tag [ ..] { return false ; }
249
+ if tag[ ..] != self . tag [ ..] {
250
+ return false ;
251
+ }
231
252
self . finished = true ;
232
253
true
233
254
}
0 commit comments