Skip to content

Commit 908f98c

Browse files
committed
salsa20: add type param for key length (#432)
1 parent 07ee501 commit 908f98c

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

salsa20/src/backends/soft.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ use cipher::{
77
consts::{U1, U64},
88
};
99

10-
pub(crate) struct Backend<'a, R: Unsigned>(pub(crate) &'a mut SalsaCore<R>);
10+
pub(crate) struct Backend<'a, R: Unsigned, KeySize>(pub(crate) &'a mut SalsaCore<R, KeySize>);
1111

12-
impl<R: Unsigned> BlockSizeUser for Backend<'_, R> {
12+
impl<R: Unsigned, KeySize> BlockSizeUser for Backend<'_, R, KeySize> {
1313
type BlockSize = U64;
1414
}
1515

16-
impl<R: Unsigned> ParBlocksSizeUser for Backend<'_, R> {
16+
impl<R: Unsigned, KeySize> ParBlocksSizeUser for Backend<'_, R, KeySize> {
1717
type ParBlocksSize = U1;
1818
}
1919

20-
impl<R: Unsigned> StreamCipherBackend for Backend<'_, R> {
20+
impl<R: Unsigned, KeySize> StreamCipherBackend for Backend<'_, R, KeySize> {
2121
#[inline(always)]
2222
fn gen_ks_block(&mut self, block: &mut Block<Self>) {
2323
let res = run_rounds::<R>(&self.0.state);

salsa20/src/backends/sse2.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use core::arch::x86_64::*;
1616

1717
#[inline]
1818
#[target_feature(enable = "sse2")]
19-
pub(crate) unsafe fn inner<R, F>(state: &mut [u32; STATE_WORDS], f: F)
19+
pub(crate) unsafe fn inner<R, F, KeySize>(state: &mut [u32; STATE_WORDS], f: F)
2020
where
2121
R: Unsigned,
2222
F: StreamCipherClosure<BlockSize = U64>,
@@ -37,9 +37,10 @@ where
3737
f.call(&mut backend);
3838
state[8] = _mm_cvtsi128_si32(backend.v[2]) as u32;
3939
} else {
40-
f.call(&mut SoftBackend(&mut SalsaCore::<R> {
40+
f.call(&mut SoftBackend(&mut SalsaCore::<R, KeySize> {
4141
state: *state,
4242
rounds: PhantomData,
43+
key_size: PhantomData,
4344
}));
4445
}
4546
}

salsa20/src/lib.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub use cipher;
8080
use 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
};
8686
use 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.
112112
pub type Nonce = Array<u8, U8>;
@@ -121,14 +121,16 @@ const STATE_WORDS: usize = 16;
121121
const 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)]

salsa20/src/xsalsa.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub type XSalsa12 = StreamCipherCoreWrapper<XSalsaCore<U6>>;
2525
pub type XSalsa8 = StreamCipherCoreWrapper<XSalsaCore<U4>>;
2626

2727
/// The XSalsa core function.
28-
pub struct XSalsaCore<R: Unsigned>(SalsaCore<R>);
28+
pub struct XSalsaCore<R: Unsigned>(SalsaCore<R, U32>);
2929

3030
impl<R: Unsigned> KeySizeUser for XSalsaCore<R> {
3131
type KeySize = U32;
@@ -41,7 +41,7 @@ impl<R: Unsigned> BlockSizeUser for XSalsaCore<R> {
4141

4242
impl<R: Unsigned> KeyIvInit for XSalsaCore<R> {
4343
#[inline]
44-
fn new(key: &Key, iv: &XNonce) -> Self {
44+
fn new(key: &Key<U32>, iv: &XNonce) -> Self {
4545
let subkey = hsalsa::<R>(key, iv[..16].try_into().unwrap());
4646
let mut padded_iv = Nonce::default();
4747
padded_iv.copy_from_slice(&iv[16..]);
@@ -89,7 +89,7 @@ impl<R: Unsigned> ZeroizeOnDrop for XSalsaCore<R> {}
8989
/// - Nonce (`u32` x 4)
9090
///
9191
/// It produces 256-bits of output suitable for use as a Salsa20 key
92-
pub fn hsalsa<R: Unsigned>(key: &Key, input: &Array<u8, U16>) -> Array<u8, U32> {
92+
pub fn hsalsa<R: Unsigned>(key: &Key<U32>, input: &Array<u8, U16>) -> Array<u8, U32> {
9393
#[inline(always)]
9494
fn to_u32(chunk: &[u8]) -> u32 {
9595
u32::from_le_bytes(chunk.try_into().unwrap())

0 commit comments

Comments
 (0)