@@ -56,6 +56,8 @@ mod real_chacha {
56
56
}
57
57
}
58
58
59
+ const BLOCK_SIZE : usize = 64 ;
60
+
59
61
#[ derive( Clone , Copy ) ]
60
62
struct ChaChaState {
61
63
a : u32x4 ,
@@ -67,7 +69,7 @@ mod real_chacha {
67
69
#[ derive( Copy ) ]
68
70
pub struct ChaCha20 {
69
71
state : ChaChaState ,
70
- output : [ u8 ; 64 ] ,
72
+ output : [ u8 ; BLOCK_SIZE ] ,
71
73
offset : usize ,
72
74
}
73
75
@@ -135,7 +137,7 @@ mod real_chacha {
135
137
assert ! ( key. len( ) == 16 || key. len( ) == 32 ) ;
136
138
assert ! ( nonce. len( ) == 8 || nonce. len( ) == 12 ) ;
137
139
138
- ChaCha20 { state : ChaCha20 :: expand ( key, nonce) , output : [ 0u8 ; 64 ] , offset : 64 }
140
+ ChaCha20 { state : ChaCha20 :: expand ( key, nonce) , output : [ 0u8 ; BLOCK_SIZE ] , offset : 64 }
139
141
}
140
142
141
143
fn expand ( key : & [ u8 ] , nonce : & [ u8 ] ) -> ChaChaState {
@@ -197,7 +199,7 @@ mod real_chacha {
197
199
}
198
200
}
199
201
200
- // put the the next 64 keystream bytes into self.output
202
+ // put the the next BLOCK_SIZE keystream bytes into self.output
201
203
fn update ( & mut self ) {
202
204
let mut state = self . state ;
203
205
@@ -234,12 +236,12 @@ mod real_chacha {
234
236
while i < len {
235
237
// If there is no keystream available in the output buffer,
236
238
// generate the next block.
237
- if self . offset == 64 {
239
+ if self . offset == BLOCK_SIZE {
238
240
self . update ( ) ;
239
241
}
240
242
241
243
// Process the min(available keystream, remaining input length).
242
- let count = cmp:: min ( 64 - self . offset , len - i) ;
244
+ let count = cmp:: min ( BLOCK_SIZE - self . offset , len - i) ;
243
245
// explicitly assert lengths to avoid bounds checks:
244
246
assert ! ( output. len( ) >= i + count) ;
245
247
assert ! ( input. len( ) >= i + count) ;
@@ -258,12 +260,12 @@ mod real_chacha {
258
260
while i < len {
259
261
// If there is no keystream available in the output buffer,
260
262
// generate the next block.
261
- if self . offset == 64 {
263
+ if self . offset == BLOCK_SIZE {
262
264
self . update ( ) ;
263
265
}
264
266
265
267
// Process the min(available keystream, remaining input length).
266
- let count = cmp:: min ( 64 - self . offset , len - i) ;
268
+ let count = cmp:: min ( BLOCK_SIZE - self . offset , len - i) ;
267
269
// explicitly assert lengths to avoid bounds checks:
268
270
assert ! ( input_output. len( ) >= i + count) ;
269
271
assert ! ( self . output. len( ) >= self . offset + count) ;
0 commit comments