@@ -78,7 +78,7 @@ pub use cipher;
7878use cipher:: {
7979 Block , BlockSizeUser , IvSizeUser , KeyIvInit , KeySizeUser , StreamCipherClosure ,
8080 StreamCipherCore , StreamCipherCoreWrapper , StreamCipherSeekCore ,
81- array:: { Array , typenum:: Unsigned } ,
81+ array:: { Array , ArraySize , typenum:: Unsigned } ,
8282 consts:: { U4 , U6 , U8 , U10 , U24 , U32 , U64 } ,
8383} ;
8484use core:: marker:: PhantomData ;
@@ -93,18 +93,18 @@ pub use xsalsa::{XSalsa8, XSalsa12, XSalsa20, XSalsaCore, hsalsa};
9393
9494/// Salsa20/8 stream cipher
9595/// (reduced-round variant of Salsa20 with 8 rounds, *not recommended*)
96- pub type Salsa8 = StreamCipherCoreWrapper < SalsaCore < U4 > > ;
96+ pub type Salsa8 = StreamCipherCoreWrapper < SalsaCore < U4 , U32 > > ;
9797
9898/// Salsa20/12 stream cipher
9999/// (reduced-round variant of Salsa20 with 12 rounds, *not recommended*)
100- pub type Salsa12 = StreamCipherCoreWrapper < SalsaCore < U6 > > ;
100+ pub type Salsa12 = StreamCipherCoreWrapper < SalsaCore < U6 , U32 > > ;
101101
102102/// Salsa20/20 stream cipher
103103/// (20 rounds; **recommended**)
104- pub type Salsa20 = StreamCipherCoreWrapper < SalsaCore < U10 > > ;
104+ pub type Salsa20 = StreamCipherCoreWrapper < SalsaCore < U10 , U32 > > ;
105105
106106/// Key type used by all Salsa variants and [`XSalsa20`].
107- pub type Key = Array < u8 , U32 > ;
107+ pub type Key < KeySize > = Array < u8 , KeySize > ;
108108
109109/// Nonce type used by all Salsa variants.
110110pub type Nonce = Array < u8 , U8 > ;
@@ -119,14 +119,16 @@ const STATE_WORDS: usize = 16;
119119const CONSTANTS : [ u32 ; 4 ] = [ 0x6170_7865 , 0x3320_646e , 0x7962_2d32 , 0x6b20_6574 ] ;
120120
121121/// The Salsa20 core function.
122- pub struct SalsaCore < R : Unsigned > {
122+ pub struct SalsaCore < R : Unsigned , KeySize = U32 > {
123123 /// Internal state of the core function
124124 state : [ u32 ; STATE_WORDS ] ,
125125 /// Number of rounds to perform
126126 rounds : PhantomData < R > ,
127+ /// Key size
128+ key_size : PhantomData < KeySize > ,
127129}
128130
129- impl < R : Unsigned > SalsaCore < R > {
131+ impl < R : Unsigned , KeySize > SalsaCore < R , KeySize > {
130132 /// Create new Salsa core from raw state.
131133 ///
132134 /// This method is mainly intended for the `scrypt` crate.
@@ -135,24 +137,29 @@ impl<R: Unsigned> SalsaCore<R> {
135137 Self {
136138 state,
137139 rounds : PhantomData ,
140+ key_size : PhantomData ,
138141 }
139142 }
140143}
141144
142- impl < R : Unsigned > KeySizeUser for SalsaCore < R > {
143- type KeySize = U32 ;
145+ impl < R : Unsigned , KeySize > KeySizeUser for SalsaCore < R , KeySize >
146+ where
147+ KeySize : ArraySize ,
148+ {
149+ type KeySize = KeySize ;
144150}
145151
146- impl < R : Unsigned > IvSizeUser for SalsaCore < R > {
152+ impl < R : Unsigned , KeySize > IvSizeUser for SalsaCore < R , KeySize > {
147153 type IvSize = U8 ;
148154}
149155
150- impl < R : Unsigned > BlockSizeUser for SalsaCore < R > {
156+ impl < R : Unsigned , KeySize > BlockSizeUser for SalsaCore < R , KeySize > {
151157 type BlockSize = U64 ;
152158}
153159
154- impl < R : Unsigned > KeyIvInit for SalsaCore < R > {
155- fn new ( key : & Key , iv : & Nonce ) -> Self {
160+ impl < R : Unsigned > KeyIvInit for SalsaCore < R , U32 >
161+ {
162+ fn new ( key : & Key < U32 > , iv : & Nonce ) -> Self {
156163 let mut state = [ 0u32 ; STATE_WORDS ] ;
157164 state[ 0 ] = CONSTANTS [ 0 ] ;
158165
@@ -179,11 +186,12 @@ impl<R: Unsigned> KeyIvInit for SalsaCore<R> {
179186 Self {
180187 state,
181188 rounds : PhantomData ,
189+ key_size : PhantomData ,
182190 }
183191 }
184192}
185193
186- impl < R : Unsigned > StreamCipherCore for SalsaCore < R > {
194+ impl < R : Unsigned , KeySize > StreamCipherCore for SalsaCore < R , KeySize > {
187195 #[ inline( always) ]
188196 fn remaining_blocks ( & self ) -> Option < usize > {
189197 let rem = u64:: MAX - self . get_block_pos ( ) ;
@@ -194,7 +202,7 @@ impl<R: Unsigned> StreamCipherCore for SalsaCore<R> {
194202 }
195203}
196204
197- impl < R : Unsigned > StreamCipherSeekCore for SalsaCore < R > {
205+ impl < R : Unsigned , KeySize > StreamCipherSeekCore for SalsaCore < R , KeySize > {
198206 type Counter = u64 ;
199207
200208 #[ inline( always) ]
0 commit comments