Skip to content

Commit 1863b0d

Browse files
committed
salsa20: add type param for key length (#432)
1 parent d43c405 commit 1863b0d

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
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/lib.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub use cipher;
7878
use 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
};
8484
use 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.
110110
pub type Nonce = Array<u8, U8>;
@@ -119,14 +119,16 @@ const STATE_WORDS: usize = 16;
119119
const 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)]

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)