Skip to main content

vector_core/crypto/chachapoly/
mod.rs

1//! ChaCha20-Poly1305 (RFC 8439), tuned for many short messages.
2//!
3//! Byte-for-byte the same construction as the `chacha20poly1305` crate; what
4//! differs is the cost shape. The Poly1305 key block is produced in the same SIMD
5//! pass as the first data blocks, and Poly1305 is scalar, so a chat message costs
6//! one keystream pass and no per-message vector setup.
7
8mod 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
19/// RFC 8439's 32-bit block counter starts at 1 for data.
20const 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
33/// A keyed ChaCha20-Poly1305 instance. The expanded key is zeroized on drop.
34pub struct ChaCha20Poly1305 {
35    key: [u32; 8],
36    backend: Backend,
37    /// Poly1305 input length from which the SIMD lanes take over.
38    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    /// Encrypt `buf` in place and return the tag.
72    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    /// Verify and decrypt `buf` in place. On a bad tag `buf` is left as ciphertext.
88    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    /// Opens `nonce(12) || ciphertext || tag(16)` in place and returns the plaintext
114    /// range within `data`. The storage format every at-rest blob uses.
115    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    /// Generates the first keystream batch (counter 0 onward) into `ks` and keys
128    /// Poly1305 from block 0. Returns the MAC and the batch's block count; blocks
129    /// 1.. of that batch are data keystream that `xor_stream` consumes first.
130    #[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    /// Returns how many leading bytes of `ks` hold keystream and need wiping.
139    #[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
161/// Raw ChaCha20 (RFC 8439, 96-bit nonce): XOR `buf` with the keystream from block `counter`
162/// on. NIP-44's cipher; the AEAD above starts its data at block 1.
163pub 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    // The bulk XORed from the registers ten blocks at a time; the rest, and the partial block
171    // a NIP-44 length prefix leaves, in one buffered pass sized to what remains.
172    #[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/// Zeroize with word-wide volatile stores. `zeroize` on a byte slice issues one
212/// volatile store per byte, which costs more than the cipher on short messages.
213#[inline]
214pub fn wipe(buf: &mut [u8]) {
215    // SAFETY: the slice is `len` writable bytes.
216    unsafe { wipe_raw(buf.as_mut_ptr(), buf.len()) }
217}
218
219/// [`wipe`] over a vector's whole allocation, spare capacity included, then empty it.
220#[inline]
221pub fn wipe_vec(v: &mut Vec<u8>) {
222    // SAFETY: the allocation is `capacity` writable bytes; zeros are a valid u8 anywhere.
223    unsafe {
224        wipe_raw(v.as_mut_ptr(), v.capacity());
225        v.set_len(0);
226    }
227}
228
229/// SAFETY: `p` must be valid for `len` byte writes.
230#[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/// The AEAD and Poly1305 with their paths forced, for measuring crossovers on a device.
256#[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    /// ChaCha20 backends this CPU can run, by name.
264    pub fn chacha_backends() -> Vec<&'static str> {
265        Backend::available().into_iter().map(|(n, _)| n).collect()
266    }
267
268    /// The ChaCha20 backend production picks on this CPU.
269    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    /// [`super::chacha20_xor`] on the named backend, or false if this CPU can't run it.
275    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    /// Whether this CPU has the Poly1305 lanes.
284    pub fn poly_simd_available() -> bool {
285        r64::simd_available()
286    }
287
288    /// The production Poly1305 lanes threshold on this CPU (`usize::MAX`: lanes off).
289    pub fn poly_simd_min() -> usize {
290        super::poly1305::simd_min()
291    }
292
293    /// An AEAD on the named ChaCha20 backend with the given Poly1305 lanes threshold, or
294    /// None if this CPU can't run that backend.
295    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    /// Poly1305 over `data` (a multiple of 64 bytes) with the scalar loop.
304    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    /// Poly1305 over `data` (a nonzero multiple of 64 bytes) with the lanes, or None
311    /// where this CPU has none.
312    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            // SAFETY: lanes available; the length is a nonzero multiple of 64.
317            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