vector_core/crypto/chachapoly/
mod.rs1mod chacha;
9mod poly1305;
10
11use chacha::{Backend, State, BUF};
12use poly1305::Poly1305;
13use zeroize::Zeroize;
14
15pub const KEY_LEN: usize = 32;
16pub const NONCE_LEN: usize = 12;
17pub const TAG_LEN: usize = 16;
18
19const MAX_LEN: u64 = (u32::MAX as u64 - 1) * 64;
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct Error;
24
25impl std::fmt::Display for Error {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 f.write_str("aead error")
28 }
29}
30
31impl std::error::Error for Error {}
32
33pub struct ChaCha20Poly1305 {
35 key: [u32; 8],
36 backend: Backend,
37 poly_min: usize,
39}
40
41impl Drop for ChaCha20Poly1305 {
42 fn drop(&mut self) {
43 self.key.zeroize();
44 }
45}
46
47impl ChaCha20Poly1305 {
48 pub fn new(key: &[u8; KEY_LEN]) -> Self {
49 let mut k = [0u32; 8];
50 for (w, c) in k.iter_mut().zip(key.chunks_exact(4)) {
51 *w = u32::from_le_bytes(c.try_into().unwrap());
52 }
53 Self { key: k, backend: Backend::detect(), poly_min: poly1305::simd_min() }
54 }
55
56 #[cfg(test)]
57 fn with_backend(key: &[u8; KEY_LEN], backend: Backend) -> Self {
58 let mut s = Self::new(key);
59 s.backend = backend;
60 s
61 }
62
63 fn state(&self, nonce: &[u8; NONCE_LEN]) -> State {
64 let mut n = [0u32; 3];
65 for (w, c) in n.iter_mut().zip(nonce.chunks_exact(4)) {
66 *w = u32::from_le_bytes(c.try_into().unwrap());
67 }
68 State { key: self.key, nonce: n }
69 }
70
71 pub fn seal_in_place(&self, nonce: &[u8; NONCE_LEN], aad: &[u8], buf: &mut [u8]) -> Result<[u8; TAG_LEN], Error> {
73 if buf.len() as u64 > MAX_LEN {
74 return Err(Error);
75 }
76 let st = self.state(nonce);
77 let mut ks = [0u8; BUF];
78 let (mut mac, first) = self.first_pass(&st, buf.len(), &mut ks);
79 mac.padded(aad, self.poly_min);
80 let used = self.xor_stream(&st, buf, &mut ks, first);
81 mac.padded(buf, self.poly_min);
82 let tag = finish(mac, aad.len(), buf.len());
83 wipe(&mut ks[..used]);
84 Ok(tag)
85 }
86
87 pub fn open_in_place(
89 &self,
90 nonce: &[u8; NONCE_LEN],
91 aad: &[u8],
92 buf: &mut [u8],
93 tag: &[u8; TAG_LEN],
94 ) -> Result<(), Error> {
95 if buf.len() as u64 > MAX_LEN {
96 return Err(Error);
97 }
98 let st = self.state(nonce);
99 let mut ks = [0u8; BUF];
100 let (mut mac, first) = self.first_pass(&st, buf.len(), &mut ks);
101 mac.padded(aad, self.poly_min);
102 mac.padded(buf, self.poly_min);
103 let expected = finish(mac, aad.len(), buf.len());
104 if !ct_eq(&expected, tag) {
105 wipe(&mut ks[..64 * first]);
106 return Err(Error);
107 }
108 let used = self.xor_stream(&st, buf, &mut ks, first);
109 wipe(&mut ks[..used]);
110 Ok(())
111 }
112
113 pub fn open_framed_in_place<'a>(&self, data: &'a mut [u8]) -> Result<&'a mut [u8], Error> {
116 if data.len() < NONCE_LEN + TAG_LEN {
117 return Err(Error);
118 }
119 let (nonce, rest) = data.split_at_mut(NONCE_LEN);
120 let nonce: [u8; NONCE_LEN] = (&*nonce).try_into().unwrap();
121 let (body, tag) = rest.split_at_mut(rest.len() - TAG_LEN);
122 let tag: [u8; TAG_LEN] = (&*tag).try_into().unwrap();
123 self.open_in_place(&nonce, &[], body, &tag)?;
124 Ok(body)
125 }
126
127 #[inline]
131 fn first_pass(&self, st: &State, len: usize, ks: &mut [u8; BUF]) -> (Poly1305, usize) {
132 let want = 1 + len.div_ceil(64);
133 let n = self.backend.keystream(st, 0, want, ks);
134 let pk: &[u8; 32] = ks[..32].try_into().unwrap();
135 (Poly1305::new(pk), n)
136 }
137
138 #[inline]
140 fn xor_stream(&self, st: &State, buf: &mut [u8], ks: &mut [u8; BUF], first: usize) -> usize {
141 let mut used = 64 * first;
142 let avail = 64 * (first - 1);
143 let head = avail.min(buf.len());
144 xor(&mut buf[..head], &ks[64..64 + head]);
145 let mut counter = first as u32;
146 let mut rest = &mut buf[head..];
147 while !rest.is_empty() {
148 let want = rest.len().div_ceil(64);
149 let n = self.backend.keystream(st, counter, want, ks);
150 used = used.max(64 * n);
151 let take = (64 * n).min(rest.len());
152 let (now, later) = rest.split_at_mut(take);
153 xor(now, &ks[..take]);
154 rest = later;
155 counter = counter.wrapping_add(n as u32);
156 }
157 used
158 }
159}
160
161pub fn chacha20_xor(key: &[u8; KEY_LEN], nonce: &[u8; NONCE_LEN], counter: u32, buf: &mut [u8]) {
164 xor_with(&ChaCha20Poly1305::new(key), nonce, counter, buf)
165}
166
167fn xor_with(cipher: &ChaCha20Poly1305, nonce: &[u8; NONCE_LEN], counter: u32, buf: &mut [u8]) {
168 let st = cipher.state(nonce);
169 let (mut counter, mut rest) = (counter, buf);
170 #[cfg(target_arch = "aarch64")]
173 if cipher.backend == Backend::Neon {
174 while rest.len() >= 640 {
175 let (now, later) = rest.split_at_mut(640);
176 chacha::xor10(&st, counter, now.try_into().expect("640 bytes"));
177 rest = later;
178 counter = counter.wrapping_add(10);
179 }
180 }
181 let mut ks = [0u8; BUF];
182 let mut used = 0;
183 while !rest.is_empty() {
184 let n = cipher.backend.keystream(&st, counter, rest.len().div_ceil(64), &mut ks);
185 used = used.max(64 * n);
186 let take = (64 * n).min(rest.len());
187 let (now, later) = rest.split_at_mut(take);
188 xor(now, &ks[..take]);
189 rest = later;
190 counter = counter.wrapping_add(n as u32);
191 }
192 wipe(&mut ks[..used]);
193}
194
195#[inline]
196fn finish(mut mac: Poly1305, aad_len: usize, ct_len: usize) -> [u8; TAG_LEN] {
197 let mut lens = [0u8; 16];
198 lens[..8].copy_from_slice(&(aad_len as u64).to_le_bytes());
199 lens[8..].copy_from_slice(&(ct_len as u64).to_le_bytes());
200 mac.blocks(&lens);
201 mac.finish()
202}
203
204#[inline(always)]
205fn xor(dst: &mut [u8], ks: &[u8]) {
206 for (d, k) in dst.iter_mut().zip(ks) {
207 *d ^= *k;
208 }
209}
210
211#[inline]
214pub fn wipe(buf: &mut [u8]) {
215 unsafe { wipe_raw(buf.as_mut_ptr(), buf.len()) }
217}
218
219#[inline]
221pub fn wipe_vec(v: &mut Vec<u8>) {
222 unsafe {
224 wipe_raw(v.as_mut_ptr(), v.capacity());
225 v.set_len(0);
226 }
227}
228
229#[inline]
231unsafe fn wipe_raw(p: *mut u8, len: usize) {
232 use core::ptr::write_volatile;
233 let head = p.align_offset(8).min(len);
234 let words = (len - head) / 8;
235 for i in 0..head {
236 write_volatile(p.add(i), 0);
237 }
238 let w = p.add(head) as *mut u64;
239 for i in 0..words {
240 write_volatile(w.add(i), 0);
241 }
242 for i in head + 8 * words..len {
243 write_volatile(p.add(i), 0);
244 }
245 core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
246}
247
248#[inline]
249fn ct_eq(a: &[u8; 16], b: &[u8; 16]) -> bool {
250 let d = u128::from_ne_bytes(*a) ^ u128::from_ne_bytes(*b);
251 let folded = (d as u64) | ((d >> 64) as u64);
252 core::hint::black_box(folded) == 0
253}
254
255#[doc(hidden)]
257#[cfg(target_pointer_width = "64")]
258pub mod bench {
259 use super::chacha::Backend;
260 use super::poly1305::r64::{self, Poly1305};
261 use super::ChaCha20Poly1305;
262
263 pub fn chacha_backends() -> Vec<&'static str> {
265 Backend::available().into_iter().map(|(n, _)| n).collect()
266 }
267
268 pub fn default_chacha_backend() -> &'static str {
270 let chosen = Backend::detect();
271 Backend::available().into_iter().find(|(_, b)| *b == chosen).map_or("portable", |(n, _)| n)
272 }
273
274 pub fn chacha20_xor_on(chacha: &str, key: &[u8; 32], nonce: &[u8; 12], buf: &mut [u8]) -> bool {
276 let Some((_, backend)) = Backend::available().into_iter().find(|(n, _)| *n == chacha) else { return false };
277 let mut c = ChaCha20Poly1305::new(key);
278 c.backend = backend;
279 super::xor_with(&c, nonce, 0, buf);
280 true
281 }
282
283 pub fn poly_simd_available() -> bool {
285 r64::simd_available()
286 }
287
288 pub fn poly_simd_min() -> usize {
290 super::poly1305::simd_min()
291 }
292
293 pub fn cipher(key: &[u8; 32], chacha: &str, poly_simd_min: usize) -> Option<ChaCha20Poly1305> {
296 let (_, backend) = Backend::available().into_iter().find(|(n, _)| *n == chacha)?;
297 let mut c = ChaCha20Poly1305::new(key);
298 c.backend = backend;
299 c.poly_min = poly_simd_min;
300 Some(c)
301 }
302
303 pub fn poly1305_scalar(key: &[u8; 32], data: &[u8]) -> [u8; 16] {
305 let mut p = Poly1305::new(key);
306 p.blocks(data);
307 p.finish()
308 }
309
310 pub fn poly1305_simd(key: &[u8; 32], data: &[u8]) -> Option<[u8; 16]> {
313 #[cfg(target_arch = "x86_64")]
314 if r64::simd_available() && !data.is_empty() && data.len().is_multiple_of(64) {
315 let mut p = Poly1305::new(key);
316 unsafe { p.blocks_simd(data) };
318 return Some(p.finish());
319 }
320 let _ = (key, data);
321 None
322 }
323}
324
325#[cfg(test)]
326mod tests;
327