1#![no_std]
67#![cfg_attr(docsrs, feature(doc_cfg))]
68#![doc(
69 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
70 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
71 html_root_url = "https://docs.rs/salsa20/0.10.2"
72)]
73#![forbid(unsafe_code)]
74#![warn(missing_docs, rust_2018_idioms, trivial_casts, unused_qualifications)]
75
76pub use cipher;
77
78use cipher::{
79 consts::{U1, U10, U24, U32, U4, U6, U64, U8},
80 generic_array::{typenum::Unsigned, GenericArray},
81 Block, BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, ParBlocksSizeUser, StreamBackend,
82 StreamCipherCore, StreamCipherCoreWrapper, StreamCipherSeekCore, StreamClosure,
83};
84use core::marker::PhantomData;
85
86#[cfg(feature = "zeroize")]
87use cipher::zeroize::{Zeroize, ZeroizeOnDrop};
88
89mod xsalsa;
90
91pub use xsalsa::{hsalsa, XSalsa12, XSalsa20, XSalsa8, XSalsaCore};
92
93pub type Salsa8 = StreamCipherCoreWrapper<SalsaCore<U4>>;
96
97pub type Salsa12 = StreamCipherCoreWrapper<SalsaCore<U6>>;
100
101pub type Salsa20 = StreamCipherCoreWrapper<SalsaCore<U10>>;
104
105pub type Key = GenericArray<u8, U32>;
107
108pub type Nonce = GenericArray<u8, U8>;
110
111pub type XNonce = GenericArray<u8, U24>;
113
114const STATE_WORDS: usize = 16;
116
117const CONSTANTS: [u32; 4] = [0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574];
119
120pub struct SalsaCore<R: Unsigned> {
122 state: [u32; STATE_WORDS],
124 rounds: PhantomData<R>,
126}
127
128impl<R: Unsigned> SalsaCore<R> {
129 pub fn from_raw_state(state: [u32; STATE_WORDS]) -> Self {
134 Self {
135 state,
136 rounds: PhantomData,
137 }
138 }
139}
140
141impl<R: Unsigned> KeySizeUser for SalsaCore<R> {
142 type KeySize = U32;
143}
144
145impl<R: Unsigned> IvSizeUser for SalsaCore<R> {
146 type IvSize = U8;
147}
148
149impl<R: Unsigned> BlockSizeUser for SalsaCore<R> {
150 type BlockSize = U64;
151}
152
153impl<R: Unsigned> KeyIvInit for SalsaCore<R> {
154 fn new(key: &Key, iv: &Nonce) -> Self {
155 let mut state = [0u32; STATE_WORDS];
156 state[0] = CONSTANTS[0];
157
158 for (i, chunk) in key[..16].chunks(4).enumerate() {
159 state[1 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
160 }
161
162 state[5] = CONSTANTS[1];
163
164 for (i, chunk) in iv.chunks(4).enumerate() {
165 state[6 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
166 }
167
168 state[8] = 0;
169 state[9] = 0;
170 state[10] = CONSTANTS[2];
171
172 for (i, chunk) in key[16..].chunks(4).enumerate() {
173 state[11 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
174 }
175
176 state[15] = CONSTANTS[3];
177
178 Self {
179 state,
180 rounds: PhantomData,
181 }
182 }
183}
184
185impl<R: Unsigned> StreamCipherCore for SalsaCore<R> {
186 #[inline(always)]
187 fn remaining_blocks(&self) -> Option<usize> {
188 let rem = u64::MAX - self.get_block_pos();
189 rem.try_into().ok()
190 }
191 fn process_with_backend(&mut self, f: impl StreamClosure<BlockSize = Self::BlockSize>) {
192 f.call(&mut Backend(self));
193 }
194}
195
196impl<R: Unsigned> StreamCipherSeekCore for SalsaCore<R> {
197 type Counter = u64;
198
199 #[inline(always)]
200 fn get_block_pos(&self) -> u64 {
201 (self.state[8] as u64) + ((self.state[9] as u64) << 32)
202 }
203
204 #[inline(always)]
205 fn set_block_pos(&mut self, pos: u64) {
206 self.state[8] = (pos & 0xffff_ffff) as u32;
207 self.state[9] = ((pos >> 32) & 0xffff_ffff) as u32;
208 }
209}
210
211#[cfg(feature = "zeroize")]
212#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
213impl<R: Unsigned> Drop for SalsaCore<R> {
214 fn drop(&mut self) {
215 self.state.zeroize();
216 }
217}
218
219#[cfg(feature = "zeroize")]
220#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
221impl<R: Unsigned> ZeroizeOnDrop for SalsaCore<R> {}
222
223struct Backend<'a, R: Unsigned>(&'a mut SalsaCore<R>);
224
225impl<'a, R: Unsigned> BlockSizeUser for Backend<'a, R> {
226 type BlockSize = U64;
227}
228
229impl<'a, R: Unsigned> ParBlocksSizeUser for Backend<'a, R> {
230 type ParBlocksSize = U1;
231}
232
233impl<'a, R: Unsigned> StreamBackend for Backend<'a, R> {
234 #[inline(always)]
235 fn gen_ks_block(&mut self, block: &mut Block<Self>) {
236 let res = run_rounds::<R>(&self.0.state);
237 self.0.set_block_pos(self.0.get_block_pos() + 1);
238
239 for (chunk, val) in block.chunks_exact_mut(4).zip(res.iter()) {
240 chunk.copy_from_slice(&val.to_le_bytes());
241 }
242 }
243}
244
245#[inline]
246#[allow(clippy::many_single_char_names)]
247pub(crate) fn quarter_round(
248 a: usize,
249 b: usize,
250 c: usize,
251 d: usize,
252 state: &mut [u32; STATE_WORDS],
253) {
254 state[b] ^= state[a].wrapping_add(state[d]).rotate_left(7);
255 state[c] ^= state[b].wrapping_add(state[a]).rotate_left(9);
256 state[d] ^= state[c].wrapping_add(state[b]).rotate_left(13);
257 state[a] ^= state[d].wrapping_add(state[c]).rotate_left(18);
258}
259
260#[inline(always)]
261fn run_rounds<R: Unsigned>(state: &[u32; STATE_WORDS]) -> [u32; STATE_WORDS] {
262 let mut res = *state;
263
264 for _ in 0..R::USIZE {
265 quarter_round(0, 4, 8, 12, &mut res);
267 quarter_round(5, 9, 13, 1, &mut res);
268 quarter_round(10, 14, 2, 6, &mut res);
269 quarter_round(15, 3, 7, 11, &mut res);
270
271 quarter_round(0, 1, 2, 3, &mut res);
273 quarter_round(5, 6, 7, 4, &mut res);
274 quarter_round(10, 11, 8, 9, &mut res);
275 quarter_round(15, 12, 13, 14, &mut res);
276 }
277
278 for (s1, s0) in res.iter_mut().zip(state.iter()) {
279 *s1 = s1.wrapping_add(*s0);
280 }
281 res
282}