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
#![no_std]
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
#![deny(missing_docs)]
#[cfg(feature = "stream-cipher")]
pub use stream_cipher;
mod block;
#[cfg(feature = "stream-cipher")]
pub(crate) mod cipher;
#[cfg(feature = "legacy")]
mod legacy;
#[cfg(feature = "xchacha20")]
mod xchacha20;
#[cfg(feature = "rng")]
mod rng;
#[cfg(feature = "legacy")]
pub use self::legacy::ChaCha20Legacy;
#[cfg(feature = "stream-cipher")]
use self::{block::Block, cipher::Cipher};
#[cfg(feature = "stream-cipher")]
use core::convert::TryInto;
#[cfg(feature = "stream-cipher")]
use stream_cipher::generic_array::{
typenum::{U12, U32},
GenericArray,
};
#[cfg(feature = "stream-cipher")]
use stream_cipher::{LoopError, NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek};
#[cfg(feature = "xchacha20")]
pub use self::xchacha20::XChaCha20;
#[cfg(feature = "rng")]
pub use rng::{
ChaCha12Rng, ChaCha12RngCore, ChaCha20Rng, ChaCha20RngCore, ChaCha8Rng, ChaCha8RngCore,
};
pub const BLOCK_SIZE: usize = 64;
pub const KEY_SIZE: usize = 32;
pub const MAX_BLOCKS: usize = core::u32::MAX as usize;
const IV_SIZE: usize = 8;
const STATE_WORDS: usize = 16;
const CONSTANTS: [u32; 4] = [0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574];
macro_rules! impl_chacha {
($name:ident, $rounds:expr, $doc:expr) => {
#[cfg(feature = "stream-cipher")]
#[doc = $doc]
pub struct $name(Cipher);
#[cfg(feature = "stream-cipher")]
impl NewStreamCipher for $name {
type KeySize = U32;
type NonceSize = U12;
fn new(key: &GenericArray<u8, U32>, iv: &GenericArray<u8, U12>) -> Self {
let block = Block::new(
key.as_ref().try_into().unwrap(),
iv[4..12].try_into().unwrap(),
$rounds,
);
let counter = initial_counter(iv[..4].try_into().unwrap());
$name(Cipher::new(block, counter))
}
}
#[cfg(feature = "stream-cipher")]
impl SyncStreamCipher for $name {
fn try_apply_keystream(&mut self, data: &mut [u8]) -> Result<(), LoopError> {
self.0.try_apply_keystream(data)
}
}
#[cfg(feature = "stream-cipher")]
impl SyncStreamCipherSeek for $name {
fn current_pos(&self) -> u64 {
self.0.current_pos()
}
fn seek(&mut self, pos: u64) {
self.0.seek(pos);
}
}
}
}
impl_chacha!(
ChaCha8,
8,
"The ChaCha8 stream cipher (8-round variant of ChaCha20)"
);
impl_chacha!(
ChaCha12,
12,
"The ChaCha20 stream cipher (12-round variant of ChaCha20)"
);
impl_chacha!(
ChaCha20,
20,
"The ChaCha20 stream cipher (RFC 8439 version with 96-bit nonce)"
);
#[cfg(feature = "stream-cipher")]
fn initial_counter(exp_iv: [u8; 4]) -> u64 {
(u64::from(exp_iv[0]) & 0xff) << 32
| (u64::from(exp_iv[1]) & 0xff) << 40
| (u64::from(exp_iv[2]) & 0xff) << 48
| (u64::from(exp_iv[3]) & 0xff) << 56
}