@@ -80,7 +80,7 @@ pub use cipher;
8080use cipher:: {
8181 Block , BlockSizeUser , IvSizeUser , KeyIvInit , KeySizeUser , StreamCipherClosure ,
8282 StreamCipherCore , StreamCipherCoreWrapper , StreamCipherSeekCore ,
83- array:: { Array , typenum:: Unsigned } ,
83+ array:: { Array , ArraySize , typenum:: Unsigned } ,
8484 consts:: { U4 , U6 , U8 , U10 , U24 , U32 , U64 } ,
8585} ;
8686use core:: marker:: PhantomData ;
@@ -95,18 +95,18 @@ pub use xsalsa::{XSalsa8, XSalsa12, XSalsa20, XSalsaCore, hsalsa};
9595
9696/// Salsa20/8 stream cipher
9797/// (reduced-round variant of Salsa20 with 8 rounds, *not recommended*)
98- pub type Salsa8 = StreamCipherCoreWrapper < SalsaCore < U4 > > ;
98+ pub type Salsa8 = StreamCipherCoreWrapper < SalsaCore < U4 , U32 > > ;
9999
100100/// Salsa20/12 stream cipher
101101/// (reduced-round variant of Salsa20 with 12 rounds, *not recommended*)
102- pub type Salsa12 = StreamCipherCoreWrapper < SalsaCore < U6 > > ;
102+ pub type Salsa12 = StreamCipherCoreWrapper < SalsaCore < U6 , U32 > > ;
103103
104104/// Salsa20/20 stream cipher
105105/// (20 rounds; **recommended**)
106- pub type Salsa20 = StreamCipherCoreWrapper < SalsaCore < U10 > > ;
106+ pub type Salsa20 = StreamCipherCoreWrapper < SalsaCore < U10 , U32 > > ;
107107
108108/// Key type used by all Salsa variants and [`XSalsa20`].
109- pub type Key = Array < u8 , U32 > ;
109+ pub type Key < KeySize > = Array < u8 , KeySize > ;
110110
111111/// Nonce type used by all Salsa variants.
112112pub type Nonce = Array < u8 , U8 > ;
@@ -121,14 +121,16 @@ const STATE_WORDS: usize = 16;
121121const CONSTANTS : [ u32 ; 4 ] = [ 0x6170_7865 , 0x3320_646e , 0x7962_2d32 , 0x6b20_6574 ] ;
122122
123123/// The Salsa20 core function.
124- pub struct SalsaCore < R : Unsigned > {
124+ pub struct SalsaCore < R : Unsigned , KeySize = U32 > {
125125 /// Internal state of the core function
126126 state : [ u32 ; STATE_WORDS ] ,
127127 /// Number of rounds to perform
128128 rounds : PhantomData < R > ,
129+ /// Key size
130+ key_size : PhantomData < KeySize > ,
129131}
130132
131- impl < R : Unsigned > SalsaCore < R > {
133+ impl < R : Unsigned , KeySize > SalsaCore < R , KeySize > {
132134 /// Create new Salsa core from raw state.
133135 ///
134136 /// This method is mainly intended for the `scrypt` crate.
@@ -137,24 +139,29 @@ impl<R: Unsigned> SalsaCore<R> {
137139 Self {
138140 state,
139141 rounds : PhantomData ,
142+ key_size : PhantomData ,
140143 }
141144 }
142145}
143146
144- impl < R : Unsigned > KeySizeUser for SalsaCore < R > {
145- type KeySize = U32 ;
147+ impl < R : Unsigned , KeySize > KeySizeUser for SalsaCore < R , KeySize >
148+ where
149+ KeySize : ArraySize ,
150+ {
151+ type KeySize = KeySize ;
146152}
147153
148- impl < R : Unsigned > IvSizeUser for SalsaCore < R > {
154+ impl < R : Unsigned , KeySize > IvSizeUser for SalsaCore < R , KeySize > {
149155 type IvSize = U8 ;
150156}
151157
152- impl < R : Unsigned > BlockSizeUser for SalsaCore < R > {
158+ impl < R : Unsigned , KeySize > BlockSizeUser for SalsaCore < R , KeySize > {
153159 type BlockSize = U64 ;
154160}
155161
156- impl < R : Unsigned > KeyIvInit for SalsaCore < R > {
157- fn new ( key : & Key , iv : & Nonce ) -> Self {
162+ impl < R : Unsigned > KeyIvInit for SalsaCore < R , U32 >
163+ {
164+ fn new ( key : & Key < U32 > , iv : & Nonce ) -> Self {
158165 let mut state = [ 0u32 ; STATE_WORDS ] ;
159166 state[ 0 ] = CONSTANTS [ 0 ] ;
160167
@@ -192,11 +199,12 @@ impl<R: Unsigned> KeyIvInit for SalsaCore<R> {
192199 Self {
193200 state,
194201 rounds : PhantomData ,
202+ key_size : PhantomData ,
195203 }
196204 }
197205}
198206
199- impl < R : Unsigned > StreamCipherCore for SalsaCore < R > {
207+ impl < R : Unsigned , KeySize > StreamCipherCore for SalsaCore < R , KeySize > {
200208 #[ inline( always) ]
201209 fn remaining_blocks ( & self ) -> Option < usize > {
202210 let rem = u64:: MAX - self . get_block_pos ( ) ;
@@ -206,7 +214,7 @@ impl<R: Unsigned> StreamCipherCore for SalsaCore<R> {
206214 cfg_if ! {
207215 if #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ] {
208216 unsafe {
209- backends:: sse2:: inner:: <R , _>( & mut self . state, f) ;
217+ backends:: sse2:: inner:: <R , _, KeySize >( & mut self . state, f) ;
210218 }
211219 } else {
212220 f. call( & mut backends:: soft:: Backend ( self ) ) ;
@@ -215,7 +223,7 @@ impl<R: Unsigned> StreamCipherCore for SalsaCore<R> {
215223 }
216224}
217225
218- impl < R : Unsigned > StreamCipherSeekCore for SalsaCore < R > {
226+ impl < R : Unsigned , KeySize > StreamCipherSeekCore for SalsaCore < R , KeySize > {
219227 type Counter = u64 ;
220228
221229 #[ inline( always) ]
0 commit comments