-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathlib.rs
More file actions
427 lines (387 loc) · 14.5 KB
/
lib.rs
File metadata and controls
427 lines (387 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
)]
//! # Usage
//!
//! Cipher functionality is accessed using traits from re-exported [`cipher`] crate, or as a set
//! of random number generator types ending in `*Rng` which implement traits from the [`rand_core`]
//! crate.
//!
//! This crate contains the following variants of the ChaCha20 core algorithm:
//!
//! - [`ChaCha20`]: standard IETF variant with 96-bit nonce
//! - [`ChaCha8`] / [`ChaCha12`]: reduced round variants of ChaCha20
//! - [`XChaCha20`]: 192-bit extended nonce variant
//! - [`XChaCha8`] / [`XChaCha12`]: reduced round variants of XChaCha20
//! - [`ChaCha20Legacy`]: "djb" variant with 64-bit nonce.
//! **WARNING:** This implementation internally uses 32-bit counter,
//! while the original implementation uses 64-bit counter. In other words,
//! it does not allow encryption of more than 256 GiB of data.
//!
//! ## Example
#![cfg_attr(feature = "cipher", doc = " ```")]
#![cfg_attr(not(feature = "cipher"), doc = " ```ignore")]
//! use chacha20::ChaCha20;
//! // Import relevant traits
//! use chacha20::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};
//! use hex_literal::hex;
//!
//! let key = [0x42; 32];
//! let nonce = [0x24; 12];
//! let plaintext = hex!("00010203 04050607 08090A0B 0C0D0E0F");
//! let ciphertext = hex!("e405626e 4f1236b3 670ee428 332ea20e");
//!
//! // Key and IV must be references to the `Array` type.
//! // Here we use the `Into` trait to convert arrays into it.
//! let mut cipher = ChaCha20::new(&key.into(), &nonce.into());
//!
//! let mut buffer = plaintext;
//!
//! // apply keystream (encrypt)
//! cipher.apply_keystream(&mut buffer);
//! assert_eq!(buffer, ciphertext);
//!
//! let ciphertext = buffer;
//!
//! // ChaCha ciphers support seeking
//! cipher.seek(0u32);
//!
//! // decrypt ciphertext by applying keystream again
//! cipher.apply_keystream(&mut buffer);
//! assert_eq!(buffer, plaintext);
//!
//! // stream ciphers can be used with streaming messages
//! cipher.seek(0u32);
//! for chunk in buffer.chunks_mut(3) {
//! cipher.apply_keystream(chunk);
//! }
//! assert_eq!(buffer, ciphertext);
//! ```
//!
//! # Configuration Flags
//!
//! You can modify crate using the following configuration flags:
//!
//! - `chacha20_backend="avx2"`: force AVX2 backend on x86/x86_64 targets.
//! Requires enabled AVX2 target feature. Ignored on non-x86(_64) targets.
//! - `chacha20_backend="avx512": force AVX-512 backend on x86/x86_64 targets.
//! Requires enabled AVX-512 target feature (MSRV 1.89). Ignored on non-x86(_64) targets.
//! - `chacha20_backend="soft"`: force software backend.
//! - `chacha20_backend="sse2"`: force SSE2 backend on x86/x86_64 targets.
//! Requires enabled SSE2 target feature. Ignored on non-x86(-64) targets.
//!
//! The flags can be enabled using `RUSTFLAGS` environmental variable
//! (e.g. `RUSTFLAGS='--cfg chacha20_backend="avx2"'`) or by modifying `.cargo/config.toml`:
//!
//! ```toml
//! # In .cargo/config.toml
//! [build]
//! rustflags = ['--cfg', 'chacha20_backend="avx2"']
//! ```
//!
//! ## AVX-512 support
//!
//! To use the MSRV 1.89 AVX-512 support, you must enable it using: `--cfg chacha20_avx512`.
//!
//! [ChaCha]: https://tools.ietf.org/html/rfc8439
//! [Salsa]: https://en.wikipedia.org/wiki/Salsa20
//! [`chacha20poly1305`]: https://docs.rs/chacha20poly1305
pub mod variants;
mod backends;
#[cfg(feature = "cipher")]
mod chacha;
#[cfg(feature = "legacy")]
mod legacy;
#[cfg(feature = "rng")]
mod rng;
#[cfg(feature = "xchacha")]
mod xchacha;
#[cfg(feature = "cipher")]
pub use chacha::{ChaCha8, ChaCha12, ChaCha20, Key, KeyIvInit};
#[cfg(feature = "cipher")]
pub use cipher;
#[cfg(feature = "legacy")]
pub use legacy::{ChaCha20Legacy, LegacyNonce};
#[cfg(feature = "rng")]
pub use rand_core;
#[cfg(feature = "rng")]
pub use rng::{ChaCha8Rng, ChaCha12Rng, ChaCha20Rng, Seed, SerializedRngState};
#[cfg(feature = "xchacha")]
pub use xchacha::{XChaCha8, XChaCha12, XChaCha20, XNonce, hchacha};
use cfg_if::cfg_if;
use core::{fmt, marker::PhantomData};
use variants::Variant;
#[cfg(feature = "cipher")]
use cipher::{BlockSizeUser, StreamCipherCore, StreamCipherSeekCore, consts::U64};
#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};
/// State initialization constant ("expand 32-byte k")
#[cfg(any(feature = "cipher", feature = "rng"))]
const CONSTANTS: [u32; 4] = [0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574];
/// Number of 32-bit words in the ChaCha state
const STATE_WORDS: usize = 16;
/// Marker type for a number of ChaCha rounds to perform.
pub trait Rounds: Copy {
/// The amount of rounds to perform
const COUNT: usize;
}
/// 8-rounds
#[derive(Copy, Clone, Debug)]
pub struct R8;
impl Rounds for R8 {
const COUNT: usize = 4;
}
/// 12-rounds
#[derive(Copy, Clone, Debug)]
pub struct R12;
impl Rounds for R12 {
const COUNT: usize = 6;
}
/// 20-rounds
#[derive(Copy, Clone, Debug)]
pub struct R20;
impl Rounds for R20 {
const COUNT: usize = 10;
}
cfg_if! {
if #[cfg(chacha20_backend = "soft")] {
type Tokens = ();
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
if #[cfg(all(chacha20_avx512, chacha20_backend = "avx512"))] {
#[cfg(not(all(target_feature = "avx512f", target_feature = "avx512vl")))]
compile_error!("You must enable `avx512f` and `avx512vl` target features with \
`chacha20_backend = "avx512"` configuration option");
type Tokens = ();
} else if #[cfg(chacha20_backend = "avx2")] {
#[cfg(not(target_feature = "avx2"))]
compile_error!("You must enable `avx2` target feature with \
`chacha20_backend = "avx2"` configuration option");
type Tokens = ();
} else if #[cfg(chacha20_backend = "sse2")] {
#[cfg(not(target_feature = "sse2"))]
compile_error!("You must enable `sse2` target feature with \
`chacha20_backend = "sse2"` configuration option");
type Tokens = ();
} else {
#[cfg(chacha20_avx512)]
cpufeatures::new!(avx512_cpuid, "avx512f", "avx512vl");
cpufeatures::new!(avx2_cpuid, "avx2");
cpufeatures::new!(sse2_cpuid, "sse2");
#[cfg(chacha20_avx512)]
type Tokens = (avx512_cpuid::InitToken, avx2_cpuid::InitToken, sse2_cpuid::InitToken);
#[cfg(not(chacha20_avx512))]
type Tokens = (avx2_cpuid::InitToken, sse2_cpuid::InitToken);
}
}
} else {
type Tokens = ();
}
}
/// The ChaCha core function.
pub struct ChaChaCore<R: Rounds, V: Variant> {
/// Internal state of the core function
state: [u32; STATE_WORDS],
/// CPU target feature tokens
#[allow(dead_code)]
tokens: Tokens,
/// Number of rounds to perform and the cipher variant
_pd: PhantomData<(R, V)>,
}
impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
/// Constructs a ChaChaCore with the specified `key` and `iv`.
///
/// You must ensure that the iv is of the correct size when using this method
/// directly.
///
/// # Panics
/// If `iv.len()` is not equal to 4, 8, or 12.
#[must_use]
#[cfg(any(feature = "cipher", feature = "rng"))]
fn new_internal(key: &[u8; 32], iv: &[u8]) -> Self {
assert!(matches!(iv.len(), 4 | 8 | 12));
let mut state = [0u32; STATE_WORDS];
let ctr_size = size_of::<V::Counter>() / size_of::<u32>();
let (const_dst, state_rem) = state.split_at_mut(4);
let (key_dst, state_rem) = state_rem.split_at_mut(8);
let (_ctr_dst, iv_dst) = state_rem.split_at_mut(ctr_size);
const_dst.copy_from_slice(&CONSTANTS);
// TODO(tarcieri): when MSRV 1.88, use `[T]::as_chunks` to avoid panic
#[allow(clippy::unwrap_used, reason = "MSRV TODO")]
{
for (src, dst) in key.chunks_exact(4).zip(key_dst) {
*dst = u32::from_le_bytes(src.try_into().unwrap());
}
assert_eq!(size_of_val(iv_dst), size_of_val(iv));
for (src, dst) in iv.chunks_exact(4).zip(iv_dst) {
*dst = u32::from_le_bytes(src.try_into().unwrap());
}
}
cfg_if! {
if #[cfg(chacha20_backend = "soft")] {
let tokens = ();
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
if #[cfg(chacha20_backend = "avx512")] {
let tokens = ();
} else if #[cfg(chacha20_backend = "avx2")] {
let tokens = ();
} else if #[cfg(chacha20_backend = "sse2")] {
let tokens = ();
} else if #[cfg(chacha20_avx512)] {
let tokens = (avx512_cpuid::init(), avx2_cpuid::init(), sse2_cpuid::init());
} else {
let tokens = (avx2_cpuid::init(), sse2_cpuid::init());
}
}
} else {
let tokens = ();
}
}
Self {
state,
tokens,
_pd: PhantomData,
}
}
/// Get the current block position.
#[inline(always)]
#[must_use]
pub fn get_block_pos(&self) -> V::Counter {
V::get_block_pos(&self.state[12..])
}
/// Set the block position.
#[inline(always)]
pub fn set_block_pos(&mut self, pos: V::Counter) {
V::set_block_pos(&mut self.state[12..], pos);
}
}
impl<R: Rounds, V: Variant> fmt::Debug for ChaChaCore<R, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"ChaChaCore<R: {}, V: {}-bit)> {{ ... }}",
R::COUNT,
size_of::<V::Counter>() * 8
)
}
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherSeekCore for ChaChaCore<R, V> {
type Counter = V::Counter;
#[inline(always)]
fn get_block_pos(&self) -> Self::Counter {
self.get_block_pos()
}
#[inline(always)]
fn set_block_pos(&mut self, pos: Self::Counter) {
self.set_block_pos(pos);
}
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> StreamCipherCore for ChaChaCore<R, V> {
#[inline(always)]
fn remaining_blocks(&self) -> Option<usize> {
V::remaining_blocks(self.get_block_pos())
}
fn process_with_backend(
&mut self,
f: impl cipher::StreamCipherClosure<BlockSize = Self::BlockSize>,
) {
cfg_if! {
if #[cfg(chacha20_backend = "soft")] {
f.call(&mut backends::soft::Backend(self));
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
cfg_if! {
if #[cfg(all(chacha20_avx512, chacha20_backend = "avx512"))] {
unsafe {
backends::avx512::inner::<R, _, V>(&mut self.state, f);
}
} else if #[cfg(chacha20_backend = "avx2")] {
unsafe {
backends::avx2::inner::<R, _, V>(&mut self.state, f);
}
} else if #[cfg(chacha20_backend = "sse2")] {
unsafe {
backends::sse2::inner::<R, _, V>(&mut self.state, f);
}
} else {
#[cfg(chacha20_avx512)]
let (avx512_token, avx2_token, sse2_token) = self.tokens;
#[cfg(not(chacha20_avx512))]
let (avx2_token, sse2_token) = self.tokens;
#[cfg(chacha20_avx512)]
if avx512_token.get() {
// SAFETY: runtime CPU feature detection above ensures this is valid
unsafe {
backends::avx512::inner::<R, _, V>(&mut self.state, f);
}
return;
}
if avx2_token.get() {
// SAFETY: runtime CPU feature detection above ensures this is valid
unsafe {
backends::avx2::inner::<R, _, V>(&mut self.state, f);
}
} else if sse2_token.get() {
// SAFETY: runtime CPU feature detection above ensures this is valid
unsafe {
backends::sse2::inner::<R, _, V>(&mut self.state, f);
}
} else {
f.call(&mut backends::soft::Backend(self));
}
}
}
} else if #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] {
// SAFETY: we have used conditional compilation to ensure NEON is available
unsafe {
backends::neon::inner::<R, _, V>(&mut self.state, f);
}
} else {
f.call(&mut backends::soft::Backend(self));
}
}
}
}
#[cfg(feature = "cipher")]
impl<R: Rounds, V: Variant> BlockSizeUser for ChaChaCore<R, V> {
type BlockSize = U64;
}
#[cfg(feature = "zeroize")]
impl<R: Rounds, V: Variant> Drop for ChaChaCore<R, V> {
fn drop(&mut self) {
self.state.zeroize();
}
}
#[cfg(feature = "zeroize")]
impl<R: Rounds, V: Variant> ZeroizeOnDrop for ChaChaCore<R, V> {}
/// The ChaCha20 quarter round function
///
/// We located this function in the root of the crate as we want it to be available
/// for the soft backend and for xchacha.
#[allow(dead_code)]
pub(crate) fn quarter_round(
a: usize,
b: usize,
c: usize,
d: usize,
state: &mut [u32; STATE_WORDS],
) {
state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(16);
state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(12);
state[a] = state[a].wrapping_add(state[b]);
state[d] ^= state[a];
state[d] = state[d].rotate_left(8);
state[c] = state[c].wrapping_add(state[d]);
state[b] ^= state[c];
state[b] = state[b].rotate_left(7);
}