@@ -6,7 +6,7 @@ extern crate stream_cipher;
6
6
#[ cfg( cargo_feature = "zeroize" ) ]
7
7
extern crate zeroize;
8
8
9
- use block_cipher_trait:: generic_array:: typenum:: { U32 , U8 } ;
9
+ use block_cipher_trait:: generic_array:: typenum:: { U12 , U32 , U8 } ;
10
10
use block_cipher_trait:: generic_array:: { ArrayLength , GenericArray } ;
11
11
use stream_cipher:: { NewStreamCipher , StreamCipher , SyncStreamCipherSeek } ;
12
12
@@ -148,6 +148,30 @@ impl NewStreamCipher for ChaChaState<U8> {
148
148
}
149
149
}
150
150
151
+ impl NewStreamCipher for ChaChaState < U12 > {
152
+ /// Key size in bytes
153
+ type KeySize = U32 ;
154
+ /// Nonce size in bytes
155
+ type NonceSize = U12 ;
156
+
157
+ fn new ( key : & GenericArray < u8 , Self :: KeySize > , iv : & GenericArray < u8 , Self :: NonceSize > ) -> Self {
158
+ let exp_iv = & iv[ 0 ..4 ] ;
159
+ let base_iv = & iv[ 4 ..12 ] ;
160
+
161
+ let mut ccs = ChaChaState {
162
+ state : SalsaFamilyState :: new ( key, GenericArray :: from_slice ( base_iv) ) ,
163
+ phantom : core:: marker:: PhantomData ,
164
+ } ;
165
+
166
+ ccs. state . block_idx = ( exp_iv[ 0 ] as u64 & 0xff ) << 32
167
+ | ( exp_iv[ 1 ] as u64 & 0xff ) << 40
168
+ | ( exp_iv[ 2 ] as u64 & 0xff ) << 48
169
+ | ( exp_iv[ 3 ] as u64 & 0xff ) << 56 ;
170
+
171
+ ccs
172
+ }
173
+ }
174
+
151
175
impl < N : ArrayLength < u8 > > SyncStreamCipherSeek for ChaChaState < N > {
152
176
fn current_pos ( & self ) -> u64 {
153
177
self . state . current_pos ( )
@@ -205,6 +229,23 @@ impl NewStreamCipher for ChaCha20<U8> {
205
229
}
206
230
}
207
231
232
+ impl NewStreamCipher for ChaCha20 < U12 > {
233
+ /// Key size in bytes
234
+ type KeySize = U32 ;
235
+ /// Nonce size in bytes
236
+ type NonceSize = U12 ;
237
+
238
+ fn new ( key : & GenericArray < u8 , Self :: KeySize > , iv : & GenericArray < u8 , Self :: NonceSize > ) -> Self {
239
+ let mut out = ChaCha20 {
240
+ state : ChaChaState :: new ( key, iv) ,
241
+ } ;
242
+
243
+ out. gen_block ( ) ;
244
+
245
+ out
246
+ }
247
+ }
248
+
208
249
impl < N : ArrayLength < u8 > > SyncStreamCipherSeek for ChaCha20 < N > {
209
250
fn current_pos ( & self ) -> u64 {
210
251
self . state . current_pos ( )
0 commit comments