| 1 |
// Copyright 2021 Brian Smith. |
| 2 |
// Portions Copyright (c) 2014, Google Inc. |
| 3 |
// |
| 4 |
// Permission to use, copy, modify, and/or distribute this software for any |
| 5 |
// purpose with or without fee is hereby granted, provided that the above |
| 6 |
// copyright notice and this permission notice appear in all copies. |
| 7 |
// |
| 8 |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 11 |
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 13 |
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 14 |
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 15 |
// Adapted from the public domain, estream code by D. Bernstein. |
| 16 |
// Adapted from the BoringSSL crypto/chacha/chacha.c. |
| 17 |
|
| 18 |
use super::{super::overlapping::IndexError, Counter, Key, Overlapping, BLOCK_LEN}; |
| 19 |
use crate::{bb, polyfill::sliceutil}; |
| 20 |
use core::mem::size_of; |
| 21 |
|
| 22 |
pub(super) fn ChaCha20_ctr32(key: &Key, counter: Counter, mut in_out: Overlapping<'_>) { |
| 23 |
const SIGMA: [u32; 4] = [ |
| 24 |
u32::from_le_bytes(*b"expa"), |
| 25 |
u32::from_le_bytes(*b"nd 3"), |
| 26 |
u32::from_le_bytes(*b"2-by"), |
| 27 |
u32::from_le_bytes(*b"te k"), |
| 28 |
]; |
| 29 |
|
| 30 |
let key = key.words_less_safe(); |
| 31 |
let counter = counter.into_words_less_safe(); |
| 32 |
|
| 33 |
let mut state = [ |
| 34 |
SIGMA[0], SIGMA[1], SIGMA[2], SIGMA[3], key[0], key[1], key[2], key[3], key[4], key[5], |
| 35 |
key[6], key[7], counter[0], counter[1], counter[2], counter[3], |
| 36 |
]; |
| 37 |
|
| 38 |
let mut in_out_len = in_out.len(); |
| 39 |
|
| 40 |
let mut buf = [0u8; BLOCK_LEN]; |
| 41 |
while in_out_len > 0 { |
| 42 |
chacha_core(&mut buf, &state); |
| 43 |
state[12] += 1; |
| 44 |
|
| 45 |
debug_assert_eq!(in_out_len, in_out.len()); |
| 46 |
|
| 47 |
// Both branches do the same thing, but the duplication helps the |
| 48 |
// compiler optimize (vectorize) the `BLOCK_LEN` case. |
| 49 |
if in_out_len >= BLOCK_LEN { |
| 50 |
in_out = in_out |
| 51 |
.split_first_chunk::<BLOCK_LEN>(|in_out| { |
| 52 |
bb::xor_assign_at_start(&mut buf, in_out.input()); |
| 53 |
sliceutil::overwrite_at_start(in_out.into_unwritten_output(), &buf); |
| 54 |
}) |
| 55 |
.unwrap_or_else(|IndexError { .. }| { |
| 56 |
// Since `in_out_len == in_out.len() && in_out_len >= BLOCK_LEN`. |
| 57 |
unreachable!() |
| 58 |
}); |
| 59 |
} else { |
| 60 |
bb::xor_assign_at_start(&mut buf, in_out.input()); |
| 61 |
sliceutil::overwrite_at_start(in_out.into_unwritten_output(), &buf); |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
in_out_len -= BLOCK_LEN; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Performs 20 rounds of ChaCha on `input`, storing the result in `output`. |
| 70 |
#[inline(always)] |
| 71 |
fn chacha_core(output: &mut [u8; BLOCK_LEN], input: &State) { |
| 72 |
let mut x = *input; |
| 73 |
|
| 74 |
for _ in (0..20).step_by(2) { |
| 75 |
quarterround(&mut x, 0, 4, 8, 12); |
| 76 |
quarterround(&mut x, 1, 5, 9, 13); |
| 77 |
quarterround(&mut x, 2, 6, 10, 14); |
| 78 |
quarterround(&mut x, 3, 7, 11, 15); |
| 79 |
quarterround(&mut x, 0, 5, 10, 15); |
| 80 |
quarterround(&mut x, 1, 6, 11, 12); |
| 81 |
quarterround(&mut x, 2, 7, 8, 13); |
| 82 |
quarterround(&mut x, 3, 4, 9, 14); |
| 83 |
} |
| 84 |
|
| 85 |
for (x, input) in x.iter_mut().zip(input.iter()) { |
| 86 |
*x = x.wrapping_add(*input); |
| 87 |
} |
| 88 |
|
| 89 |
output |
| 90 |
.chunks_exact_mut(size_of::<u32>()) |
| 91 |
.zip(x.iter()) |
| 92 |
.for_each(|(output, &x)| output.copy_from_slice(&x.to_le_bytes())); |
| 93 |
} |
| 94 |
|
| 95 |
#[inline(always)] |
| 96 |
fn quarterround(x: &mut State, a: usize, b: usize, c: usize, d: usize) { |
| 97 |
#[inline(always)] |
| 98 |
fn step(x: &mut State, a: usize, b: usize, c: usize, rotation: u32) { |
| 99 |
x[a] = x[a].wrapping_add(x[b]); |
| 100 |
x[c] = (x[c] ^ x[a]).rotate_left(rotation); |
| 101 |
} |
| 102 |
step(x, a, b, d, 16); |
| 103 |
step(x, c, d, b, 12); |
| 104 |
step(x, a, b, d, 8); |
| 105 |
step(x, c, d, b, 7); |
| 106 |
} |
| 107 |
|
| 108 |
type State = [u32; BLOCK_LEN / 4]; |