use futures::{AsyncWrite, io::{self, AsyncRead, AsyncReadExt, AsyncWriteExt}};
use std::{pin::Pin, task::{Context, Poll}};
fn cha_cha_quarter_round(
state: &mut [u32; 16],
a_index: usize,
b_index: usize,
c_index: usize,
d_index: usize,
) {
let mut a = state[a_index];
let mut b = state[b_index];
let mut c = state[c_index];
let mut d = state[d_index];
a = a.wrapping_add(b);
d = d ^ a;
d = d.rotate_left(16);
c = c.wrapping_add(d);
b = b ^ c;
b = b.rotate_left(12);
a = a.wrapping_add(b);
d = d ^ a;
d = d.rotate_left(8);
c = c.wrapping_add(d);
b = b ^ c;
b = b.rotate_left(7);
state[a_index] = a;
state[b_index] = b;
state[c_index] = c;
state[d_index] = d;
}
fn cha_cha_20_block(key: &[u8; 32], counter: u32, nonce: &[u8; 12]) -> [u8; 64] {
let mut key_words = [0u32; 8];
for i in 0..8 {
key_words[i] = u32::from_le_bytes([
key[4*i],
key[4*i + 1],
key[4*i + 2],
key[4*i + 3],
]);
}
let mut nonce_words = [0u32; 3];
for i in 0..3 {
nonce_words[i] = u32::from_le_bytes([
nonce[4*i],
nonce[4*i + 1],
nonce[4*i + 2],
nonce[4*i + 3],
]);
}
let [k0,k1,k2,k3,k4,k5,k6,k7] = key_words;
let [n0,n1,n2] = nonce_words;
let mut state: [u32; 16] = [
0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574,
k0, k1, k2, k3, k4, k5, k6, k7,
counter,
n0, n1, n2,
];
let mut working_state = state;
for _ in 0..10 {
cha_cha_quarter_round(&mut working_state, 0, 4, 8, 12);
cha_cha_quarter_round(&mut working_state, 1, 5, 9, 13);
cha_cha_quarter_round(&mut working_state, 2, 6, 10, 14);
cha_cha_quarter_round(&mut working_state, 3, 7, 11, 15);
cha_cha_quarter_round(&mut working_state, 0, 5, 10, 15);
cha_cha_quarter_round(&mut working_state, 1, 6, 11, 12);
cha_cha_quarter_round(&mut working_state, 2, 7, 8, 13);
cha_cha_quarter_round(&mut working_state, 3, 4, 9, 14);
}
for i in 0..16 {
state[i] = state[i].wrapping_add(working_state[i]);
}
let mut out = [0u8; 64];
for (i, word) in state.iter().enumerate() {
out[4*i..4*i+4].copy_from_slice(&word.to_le_bytes());
}
out
}
pub async fn cha_cha_20_encrypt<R, W>(
key: &[u8; 32],
mut counter: u32,
nonce: &[u8; 12],
mut reader: R,
mut writer: W,
) -> io::Result<()>
where
R: AsyncRead + Unpin,
W: AsyncWrite + Unpin
{
let mut buf = [0u8; 4096];
let mut block_off = 0usize;
let mut keystream = [0u8; 64];
loop {
let n = reader.read(&mut buf).await?;
if n == 0 { break; }
let mut i = 0;
while i < n {
if block_off == 0 {
keystream = cha_cha_20_block(key, counter, nonce);
}
let take = core::cmp::min(64 - block_off, n - i);
for j in 0..take {
buf[i + j] ^= keystream[block_off + j];
}
i += take;
block_off += take;
if block_off == 64 {
block_off = 0;
counter = counter.wrapping_add(1);
}
}
writer.write_all(&buf[..n]).await?;
}
writer.flush().await?;
Ok(())
}
#[derive(Clone, Copy)]
struct Poly1305 {
accumulator: [u32; 5],
r: [u32; 4],
s: [u32; 4],
buffer: [u8; 16],
buffer_len: usize,
}
impl Poly1305 {
pub fn new(key: &[u8; 32]) -> Self {
let mut r = [0u32; 4];
let mut s = [0u32; 4];
for i in 0..4 {
let j = 4 * i;
r[i] = u32::from_le_bytes([key[j], key[j + 1], key[j + 2], key[j + 3]]);
s[i] = u32::from_le_bytes([
key[16 + j],
key[16 + j + 1],
key[16 + j + 2],
key[16 + j + 3],
]);
}
r[0] &= 0x0fffffff;
r[1] &= 0x0ffffffc;
r[2] &= 0x0ffffffc;
r[3] &= 0x0ffffffc;
Self {
accumulator: [0; 5],
r,
s,
buffer: [0; 16],
buffer_len: 0,
}
}
pub fn update(&mut self, mut data: &[u8]) {
while !data.is_empty() {
let to_copy = std::cmp::min(16 - self.buffer_len, data.len());
self.buffer[self.buffer_len..self.buffer_len + to_copy]
.copy_from_slice(&data[0..to_copy]);
self.buffer_len += to_copy;
data = &data[to_copy..];
if self.buffer_len == 16 {
let mut block = [0u32; 4];
for i in 0..4 {
let j = 4 * i;
block[i] = u32::from_le_bytes([
self.buffer[j],
self.buffer[j + 1],
self.buffer[j + 2],
self.buffer[j + 3],
]);
}
let extended_block = [block[0], block[1], block[2], block[3], 1];
self.accumulator = poly1305_add(self.accumulator, extended_block);
poly1305_mul(&mut self.accumulator, self.r);
self.buffer_len = 0;
}
}
}
pub fn finalize(mut self) -> [u8; 16] {
if self.buffer_len > 0 {
self.buffer[self.buffer_len] = 1;
for i in self.buffer_len + 1..16 {
self.buffer[i] = 0;
}
let mut block = [0u32; 4];
for i in 0..4 {
let j = 4 * i;
block[i] = u32::from_le_bytes([
self.buffer[j],
self.buffer[j + 1],
self.buffer[j + 2],
self.buffer[j + 3],
]);
}
let extended_block = [block[0], block[1], block[2], block[3], 0];
self.accumulator = poly1305_add(self.accumulator, extended_block);
poly1305_mul(&mut self.accumulator, self.r);
}
self.accumulator = poly1305_mod(self.accumulator);
let extended_s = [self.s[0], self.s[1], self.s[2], self.s[3], 0];
self.accumulator = poly1305_add(self.accumulator, extended_s);
let mut out = [0u8; 16];
for (i, limb) in self.accumulator[..4].iter().enumerate() {
out[4 * i..4 * i + 4].copy_from_slice(&limb.to_le_bytes());
}
out
}
}
pub async fn poly1305_mac_async<R: AsyncRead + Unpin>(
key: &[u8; 32],
mut reader: R,
) -> io::Result<[u8; 16]> {
let mut state = Poly1305::new(key);
let mut buf = [0u8; 1024];
loop {
let n = reader.read(&mut buf).await?;
if n == 0 {
break;
}
state.update(&buf[0..n]);
}
Ok(state.finalize())
}
fn poly1305_mul(h: &mut [u32; 5], r: [u32; 4]) {
let r0: u64 = r[0] as u64;
let r1: u64 = r[1] as u64;
let r2: u64 = r[2] as u64;
let r3: u64 = r[3] as u64;
let h0: u64 = h[0] as u64;
let h1: u64 = h[1] as u64;
let h2: u64 = h[2] as u64;
let h3: u64 = h[3] as u64;
let h4: u64 = h[4] as u64;
let rr0 = (r0 >> 2) * 5; let rr1 = (r1 >> 2) * 5; let rr2 = (r2 >> 2) * 5; let rr3 = (r3 >> 2) * 5;
let x0: u64 = h0 * r0 + h1 * rr3 + h2 * rr2 + h3 * rr1 + h4 * rr0;
let x1 = h0 * r1 + h1 * r0 + h2 * rr3 + h3 * rr2 + h4 * rr1;
let x2 = h0 * r2 + h1 * r1 + h2 * r0 + h3 * rr3 + h4 * rr2;
let x3 = h0 * r3 + h1 * r2 + h2 * r1 + h3 * r0 + h4 * rr3;
let x4 = h4 * (r0 & 3);
let msb: u64 = x4 + (x3 >> 32);
let mut u = (msb >> 2) * 5; u += x0 & 0xffffffff;
h[0] = (u & 0xffffffff) as u32;
u >>= 32;
u += (x1 & 0xffffffff) + (x0 >> 32);
h[1] = (u & 0xffffffff) as u32;
u >>= 32;
u += (x2 & 0xffffffff) + (x1 >> 32);
h[2] = (u & 0xffffffff) as u32;
u >>= 32;
u += (x3 & 0xffffffff) + (x2 >> 32);
h[3] = (u & 0xffffffff) as u32;
u >>= 32;
u += msb & 3 ;
h[4] = u as u32;
}
fn poly1305_add(a: [u32; 5], b: [u32; 5]) -> [u32; 5] {
let mut temp: u64 = 0;
let mut result: [u32; 5] = Default::default();
for i in 0..5 {
temp += a[i] as u64 + b[i] as u64;
result[i] = temp as u32;
temp >>= 32;
}
result
}
fn poly1305_mod(a: [u32; 5]) -> [u32; 5] {
if a[4] < 3 {
return a;
}
let low_p = 0xfffffffffffffffffffffffffffffffbu128;
let low_a: u128 =
a[0] as u128
| ((a[1] as u128) << 32)
| ((a[2] as u128) << 64)
| ((a[3] as u128) << 96);
if a[4] == 3 && low_p > low_a {
return a;
}
if a[4] == 3 && low_p == low_a {
return [0, 0, 0, 0, 0];
}
let mut hi_a = a[4];
let low_result = if low_a >= low_p {
low_a - low_p
} else {
hi_a -= 1;
!low_p + 1 + low_a
};
let bytes = low_result.to_le_bytes();
let low_result = [
u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]),
u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]),
];
[
low_result[0],
low_result[1],
low_result[2],
low_result[3],
hi_a - 3,
]
}
pub fn poly1305_key_gen(key: &[u8; 32], nonce: &[u8; 12]) -> [u8; 32] {
let counter = 0;
let block = &cha_cha_20_block(key, counter, nonce)[..32];
let mut result: [u8; 32] = [0; 32];
result.copy_from_slice(block);
result
}
struct TeeReader<'a, R: AsyncRead + Unpin> {
reader: R,
poly: &'a mut Poly1305,
cipher_len: u64,
}
impl<'a, R: AsyncRead + Unpin> TeeReader<'a, R> {
fn new(reader: R, poly: &'a mut Poly1305) -> Self {
Self {
reader,
poly,
cipher_len: 0,
}
}
}
impl<'a, R: AsyncRead + Unpin> AsyncRead for TeeReader<'a, R> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let n = futures::ready!(Pin::new(&mut self.reader).poll_read(cx, buf))?;
if n > 0 {
self.poly.update(&buf[..n]);
self.cipher_len += n as u64;
}
Poll::Ready(Ok(n))
}
}
struct TeeWriter<'a, W: AsyncWrite + Unpin> {
writer: W,
poly: &'a mut Poly1305,
cipher_len: u64,
}
impl<'a, W: AsyncWrite + Unpin> TeeWriter<'a, W> {
fn new(writer: W, poly: &'a mut Poly1305) -> Self {
Self {
writer,
poly,
cipher_len: 0,
}
}
}
impl<'a, W: AsyncWrite + Unpin> AsyncWrite for TeeWriter<'a, W> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
let n = futures::ready!(Pin::new(&mut self.writer).poll_write(cx, buf))?;
self.poly.update(&buf[..n]);
self.cipher_len += n as u64;
Poll::Ready(Ok(n))
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.writer).poll_flush(cx)
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.writer).poll_close(cx)
}
}
pub async fn cha_cha_20_aead_encrypt<R: AsyncRead + Unpin, W: AsyncWrite + Unpin>(
aad: &[u8],
key: &[u8; 32],
nonce: [u8; 12],
reader: R,
mut writer: W,
) -> io::Result<[u8; 16]> {
let otk = poly1305_key_gen(key, &nonce);
let mut poly = Poly1305::new(&otk);
poly.update(aad);
let aad_pad = (16 - aad.len() % 16) % 16;
if aad_pad > 0 {
let zero_pad = [0u8; 16];
poly.update(&zero_pad[..aad_pad]);
}
let mut tee_writer = TeeWriter::new(&mut writer, &mut poly);
cha_cha_20_encrypt(key, 1u32, &nonce, reader, &mut tee_writer).await?;
let cipher_len = tee_writer.cipher_len;
let cipher_pad = (16 - (cipher_len as usize % 16)) % 16;
tee_writer.flush().await?;
if cipher_pad > 0 {
let zero_pad = [0u8; 16];
poly.update(&zero_pad[..cipher_pad]);
}
let aad_len_bytes: [u8; 8] = (aad.len() as u64).to_le_bytes();
let cipher_len_bytes: [u8; 8] = cipher_len.to_le_bytes();
poly.update(&aad_len_bytes);
poly.update(&cipher_len_bytes);
let tag = poly.finalize();
Ok(tag)
}
pub async fn cha_cha_20_aead_decrypt_verify<R, W>(
aad: &[u8],
key: &[u8; 32],
nonce: [u8; 12],
reader: R,
writer: W,
expected_tag: [u8; 16],
) -> io::Result<()>
where
R: AsyncRead + Unpin,
W: AsyncWrite + Unpin
{
let otk = poly1305_key_gen(key, &nonce);
let mut poly = Poly1305::new(&otk);
poly.update(aad);
let aad_pad = (16 - aad.len() % 16) % 16;
if aad_pad != 0 {
let zero_pad = [0u8; 16];
poly.update(&zero_pad[..aad_pad]);
}
let mut tee_reader = TeeReader::new(reader, &mut poly);
cha_cha_20_encrypt(key, 1u32, &nonce, &mut tee_reader, writer).await?;
let cipher_len = tee_reader.cipher_len;
let c_pad = (16 - (cipher_len as usize % 16)) % 16;
if c_pad != 0 {
let zero_pad = [0u8; 16];
poly.update(&zero_pad[..c_pad]);
}
let aad_len_le = (aad.len() as u64).to_le_bytes();
let c_len_le = (cipher_len as u64).to_le_bytes();
poly.update(&aad_len_le);
poly.update(&c_len_le);
let tag = poly.finalize();
let mut diff: u8 = 0;
for i in 0..16 {
diff |= tag[i] ^ expected_tag[i];
}
if diff != 0 {
return Err(io::Error::new(io::ErrorKind::InvalidData, "AEAD tag mismatch"));
}
Ok(())
}
#[cfg(test)]
mod tests {
use futures::executor::block_on;
use super::*;
const PLAIN_TEXT_1:&[u8] = &[
0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e,
0x74, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20,
0x63, 0x6c, 0x61, 0x73, 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20,
0x49, 0x66, 0x20, 0x49, 0x20, 0x63, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e,
0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20,
0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, 0x63, 0x72,
0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
0x74, 0x2e
];
const CIPHER_TEXT1: &[u8] = &[
0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb, 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef,
0x7e, 0xc2, 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe, 0xa9, 0xe2, 0xb5, 0xa7,
0x36, 0xee, 0x62, 0xd6, 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12, 0x82, 0xfa,
0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b, 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36, 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77,
0x8b, 0x8c, 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58, 0xfa, 0xb3, 0x24, 0xe4,
0xfa, 0xd6, 0x75, 0x94, 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc, 0x3f, 0xf4,
0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d, 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
0x61, 0x16,
];
const AAD1: [u8; 12] = [
0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
];
const KEY1: [u8; 32] = [
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d,
0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
0x9c, 0x9d, 0x9e, 0x9f,
];
const IV1: [u8; 8] = [0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47];
const TAG1: [u8; 16] = [
0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a, 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60,
0x06, 0x91,
];
#[test]
fn test_cha_cha_quarter_round() {
let mut state: [u32; 16] = [
0x879531e0, 0xc5ecf37d, 0x516461b1, 0xc9a62f8a, 0x44c20ef3, 0x3390af7f, 0xd9fc690b,
0x2a5f714c, 0x53372767, 0xb00a5631, 0x974c541a, 0x359e9963, 0x5c971061, 0x3d631689,
0x2098d9d6, 0x91dbd320,
];
cha_cha_quarter_round(&mut state, 2, 7, 8, 13);
assert_eq!(
state,
[
0x879531e0, 0xc5ecf37d, 0xbdb886dc, 0xc9a62f8a, 0x44c20ef3, 0x3390af7f, 0xd9fc690b,
0xcfacafd2, 0xe46bea80, 0xb00a5631, 0x974c541a, 0x359e9963, 0x5c971061, 0xccc07c79,
0x2098d9d6, 0x91dbd320
]
);
}
#[test]
fn test_cha_cha_20_block() {
let key: [u8; 32] = [
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
0x1f,
];
let nonce: [u8; 12] = [0x0, 0x0, 0x0, 0x9, 0x0, 0x0, 0x0, 0x4a, 0x0, 0x0, 0x0, 0x0];
let state: &[u8] = &cha_cha_20_block(&key, 1u32, &nonce);
let expected: &[u8] = &[
0x10u8, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15, 0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20,
0x71, 0xc4, 0xc7u8, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03, 0x04, 0x22, 0xaa, 0x9a,
0xc3, 0xd4, 0x6c, 0x4e, 0xd2u8, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09, 0x14, 0xc2,
0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2, 0xb5u8, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9,
0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e,
];
assert_eq!(state, expected);
}
#[test]
fn test_cha_cha_20_encrypt() {
let key: [u8; 32] = [
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
0x1f,
];
let nonce: [u8; 12] = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4a, 0x0, 0x0, 0x0, 0x0];
let mut cipher_text = vec![];
block_on(
cha_cha_20_encrypt(&key, 1, &nonce, PLAIN_TEXT_1, &mut cipher_text)
).unwrap();
let expected: &[u8] = &[
0x6e, 0x2e, 0x35, 0x9a, 0x25, 0x68, 0xf9, 0x80, 0x41, 0xba, 0x07, 0x28, 0xdd, 0x0d,
0x69, 0x81, 0xe9, 0x7e, 0x7a, 0xec, 0x1d, 0x43, 0x60, 0xc2, 0x0a, 0x27, 0xaf, 0xcc,
0xfd, 0x9f, 0xae, 0x0b, 0xf9, 0x1b, 0x65, 0xc5, 0x52, 0x47, 0x33, 0xab, 0x8f, 0x59,
0x3d, 0xab, 0xcd, 0x62, 0xb3, 0x57, 0x16, 0x39, 0xd6, 0x24, 0xe6, 0x51, 0x52, 0xab,
0x8f, 0x53, 0x0c, 0x35, 0x9f, 0x08, 0x61, 0xd8, 0x07, 0xca, 0x0d, 0xbf, 0x50, 0x0d,
0x6a, 0x61, 0x56, 0xa3, 0x8e, 0x08, 0x8a, 0x22, 0xb6, 0x5e, 0x52, 0xbc, 0x51, 0x4d,
0x16, 0xcc, 0xf8, 0x06, 0x81, 0x8c, 0xe9, 0x1a, 0xb7, 0x79, 0x37, 0x36, 0x5a, 0xf9,
0x0b, 0xbf, 0x74, 0xa3, 0x5b, 0xe6, 0xb4, 0x0b, 0x8e, 0xed, 0xf2, 0x78, 0x5e, 0x42,
0x87, 0x4d,
];
assert_eq!(&cipher_text, expected);
}
#[test]
fn test_poly1305_mac() {
let key_material: [u8; 32] = [
0x85, 0xd6, 0xbe, 0x78, 0x57, 0x55, 0x6d, 0x33, 0x7f, 0x44, 0x52, 0xfe, 0x42, 0xd5,
0x06, 0xa8, 0x01, 0x03, 0x80, 0x8a, 0xfb, 0x0d, 0xb2, 0xfd, 0x4a, 0xbf, 0xf6, 0xaf,
0x41, 0x49, 0xf5, 0x1b,
];
let message:&[u8] = &[
0x43u8, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x20,
0x46, 0x6f, 0x72, 0x75, 0x6d, 0x20, 0x52, 0x65, 0x73, 0x65, 0x61, 0x72, 0x63, 0x68,
0x20, 0x47, 0x72, 0x6f, 0x75, 0x70,
];
let tag = block_on(
poly1305_mac_async(&key_material, message)
).unwrap();
let expected: &[u8] = &[
0xa8, 0x06, 0x1d, 0xc1, 0x30, 0x51, 0x36, 0xc6, 0xc2, 0x2b, 0x8b, 0xaf, 0x0c, 0x01,
0x27, 0xa9,
];
assert_eq!(tag, expected);
}
#[test]
fn test_poly1305_key_generation() {
let nonce: [u8; 12] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
];
let result: [u8; 32] = poly1305_key_gen(&KEY1, &nonce);
let expected: [u8; 32] = [
0x8a, 0xd5, 0xa0, 0x8b, 0x90, 0x5f, 0x81, 0xcc, 0x81, 0x50, 0x40, 0x27, 0x4a, 0xb2,
0x94, 0x71, 0xa8, 0x33, 0xb6, 0x37, 0xe3, 0xfd, 0x0d, 0xa5, 0x08, 0xdb, 0xb8, 0xe2,
0xfd, 0xd1, 0xa6, 0x46,
];
assert_eq!(&result, &expected);
}
#[test]
fn test_cha_cha_20_aead_encrypt() {
let constant: [u8; 4] = [0x07, 0x00, 0x00, 0x00];
let mut constant_and_iv: [u8; 12] = [0; 12];
constant_and_iv[..4].copy_from_slice(&constant);
constant_and_iv[4..].copy_from_slice(&IV1);
let mut cipher_text = vec![];
let tag = block_on(
cha_cha_20_aead_encrypt(
&AAD1,
&KEY1,
constant_and_iv,
PLAIN_TEXT_1,
&mut cipher_text
)
).unwrap();
assert_eq!(&cipher_text, CIPHER_TEXT1);
assert_eq!(tag, TAG1);
}
#[test]
fn test_cha_cha_20_aead_decrypt() {
let constant: [u8; 4] = [0x07, 0x00, 0x00, 0x00];
let mut constant_and_iv: [u8; 12] = [0; 12];
constant_and_iv[..4].copy_from_slice(&constant);
constant_and_iv[4..].copy_from_slice(&IV1);
let mut plain_text = vec![];
block_on(cha_cha_20_aead_decrypt_verify(
&AAD1,
&KEY1,
constant_and_iv,
CIPHER_TEXT1,
&mut plain_text,
TAG1
))
.unwrap();
assert_eq!(&plain_text, PLAIN_TEXT_1);
}
#[test]
fn test_poly1305_mac_2() {
let otk = [
0xfd, 0x72, 0xb9, 0xc9, 0x9a, 0xa1, 0x50, 0x9c, 0xd4, 0x7e, 0x72, 0x27, 0x0a, 0xc2,
0xbf, 0x07, 0xc9, 0x21, 0x98, 0xb5, 0x77, 0xfc, 0x73, 0xaa, 0x0f, 0x36, 0x3c, 0xf6,
0x38, 0xd3, 0xa6, 0xd7,
];
let message: &[u8] = &[
0x17, 0x03, 0x03, 0x03, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x6e, 0xc9, 0x75, 0x08, 0x7a, 0x1a, 0x86, 0x19, 0x66, 0x0f, 0x02, 0x13,
0x67, 0x92, 0x64, 0x11, 0xe8, 0xbb, 0x85, 0x8f, 0xab, 0xd2, 0xad, 0xf5, 0xfe, 0xa9,
0xfe, 0xb5, 0x35, 0x4f, 0x81, 0xc1, 0x21, 0xe3, 0xf3, 0x02, 0xe1, 0x2a, 0xbc, 0x55,
0xa8, 0xd5, 0xa7, 0x08, 0xcd, 0x0e, 0xa9, 0x7d, 0x27, 0x78, 0x8b, 0xe8, 0xf3, 0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 0xad, 0xb8, 0xc5,
0xd3, 0xd7, 0xa6, 0xf7, 0xc4, 0x7c, 0xd3, 0xf2, 0x3b, 0x55, 0x34, 0x63, 0x1b, 0x64,
0xb5, 0xf1, 0xf2, 0x73, 0x68, 0x6e, 0x99, 0x08, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe9, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];
let tag = block_on(poly1305_mac_async( &otk, message)).unwrap();
let expected_tag = &[
0x45, 0x6b, 0x78, 0xfc, 0x85, 0x68, 0xbd, 0x62, 0x07, 0x1e, 0x6b, 0x32, 0x9c, 0x23,
0xfb, 0x50,
];
assert_eq!(&tag as &[u8], expected_tag);
}
#[test]
fn test_poly1305_mac_3() {
let otk = [
0x50, 0xd7, 0x07, 0xbc, 0xa8, 0xe5, 0x35, 0x3d, 0x3d, 0xb1, 0x01, 0xf6, 0x78, 0x10,
0xb7, 0x3f, 0x7d, 0x92, 0x02, 0xd8, 0xe4, 0xbd, 0x11, 0x8f, 0xd7, 0xca, 0x44, 0x68,
0x54, 0x56, 0xe6, 0x6f,
];
let message: &[u8] = &[
0x17, 0x03, 0x03, 0x05, 0xed, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xde, 0x6c, 0xea, 0x98, 0x72, 0x1c, 0xb8, 0xfc, 0x60, 0xec, 0x26, 0xec,
0x15, 0x50, 0x27, 0x0c, 0x43, 0x29, 0x6b, 0x4c, 0x9a, 0x67, 0x9e, 0xe1, 0x7b, 0x88,
0xfd, 0x73, 0xae, 0xbf, 0xc8, 0x43, 0xd9, 0xcd, 0x74, 0x8c, 0x4e, 0xd2, 0xd9, 0x8d,
0x01, 0x18, 0x63, 0xd9, 0x36, 0xef, 0x81, 0xf6, 0x7a, 0x7e, 0xf0, 0x3a, 0x8f, 0x27,
0xac, 0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x64, 0x45, 0x95, 0xdb, 0x01, 0x87, 0x21, 0x1d,
0xa8, 0x44, 0xcb, 0x26, 0x14, 0x1d, 0xd5, 0x1b, 0xd3, 0x7f, 0xf6, 0x11, 0x4e, 0x01,
0xa8, 0xa3, 0xb8, 0x90, 0xb3, 0x05, 0xf6, 0xc1, 0x00, 0x0d, 0xd5, 0x5a, 0x48, 0xe9,
0x7f, 0x19, 0x43, 0xc4, 0x8b, 0xbb, 0x0b, 0xb2, 0xd0, 0x3c, 0x7f, 0x0d, 0x6c, 0xbd,
0xbe, 0xba, 0xee, 0x40, 0x04, 0x93, 0xf7, 0x1f, 0x97, 0xa9, 0x9b, 0xef, 0x0e, 0x18,
0x0b, 0x82, 0xfe, 0xe5, 0x7b, 0x0a, 0xc5, 0x1b, 0x02, 0x89, 0x4e, 0x6a, 0x62, 0x69,
0x1f, 0x95, 0xa3, 0xb0, 0xeb, 0x87, 0x11, 0xa3, 0x5f, 0xa4, 0x33, 0xbd, 0xeb, 0xd1,
0xc2, 0x91, 0x0c, 0xf8, 0x7f, 0x73, 0xa7, 0x48, 0xdd, 0x1c, 0x32, 0x69, 0x1b, 0xbc,
0xb8, 0x6d, 0x91, 0xc3, 0xb9, 0x59, 0x1b, 0xf1, 0x0d, 0xbe, 0x20, 0x1f, 0xc3, 0x37,
0x91, 0x8c, 0x15, 0xeb, 0xbb, 0xc4, 0xaa, 0xc5, 0x8a, 0xa3, 0xf3, 0x30, 0xad, 0xab,
0x48, 0x5a, 0xb9, 0x4d, 0x08, 0xcc, 0x36, 0x7e, 0x8b, 0x01, 0x0b, 0x01, 0xed, 0xfd,
0xaa, 0x17, 0x50, 0x4f, 0x28, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xdd, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let tag = block_on(
poly1305_mac_async(&otk, message)
).unwrap();
let expected_tag = &[
0x78, 0xfb, 0xe0, 0x05, 0xdb, 0xff, 0x31, 0xf6, 0xdf, 0xdf, 0xe4, 0x58, 0x7b, 0x4d,
0xbe, 0x8e,
];
assert_eq!(&tag as &[u8], expected_tag);
}
#[test]
fn test_poly1305_mod() {
let a: [u32; 5] = [0x2dde68f6, 0x672041f6, 0xf0a01508, 0x1ed7f726, 0x04];
assert_eq!(
poly1305_mod(a),
[0x2dde68fb, 0x672041f6, 0xf0a01508, 0x1ed7f726, 0x0]
);
let a: [u32; 5] = [0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x03];
assert_eq!(poly1305_mod(a), [0x4, 0, 0, 0, 0]);
let a: [u32; 5] = [0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x04];
assert_eq!(poly1305_mod(a), [0x4, 0, 0, 0, 0x1]);
let a: [u32; 5] = [0xfffffffa, 0xffffffff, 0xffffffff, 0xffffffff, 0x03];
assert_eq!(
poly1305_mod(a),
[0xfffffffa, 0xffffffff, 0xffffffff, 0xffffffff, 0x3]
);
let a: [u32; 5] = [0xfffffffb, 0xffffffff, 0xffffffff, 0xffffffff, 0x03];
assert_eq!(poly1305_mod(a), [0, 0, 0, 0, 0]);
let a: [u32; 5] = [0xfffffffc, 0xffffffff, 0xffffffff, 0xffffffff, 0x03];
assert_eq!(poly1305_mod(a), [1, 0, 0, 0, 0]);
}
#[test]
fn test_cha_cha_20_encrypt_empty() {
let key: [u8; 32] = [0; 32];
let nonce: [u8; 12] = [0; 12];
let plaintext: &[u8] = &[];
let mut result = vec![];
block_on(
cha_cha_20_encrypt(&key, 1, &nonce, plaintext, &mut result)
).unwrap();
assert_eq!(result, &[]);
}
#[test]
fn test_cha_cha_20_encrypt_large() {
let key: [u8; 32] = [0; 32];
let nonce: [u8; 12] = [0; 12];
let plaintext: Vec<u8> = vec![0; 1024]; let mut result = vec![];
block_on(
cha_cha_20_encrypt(&key, 1, &nonce, plaintext.as_slice(), &mut result)
).unwrap();
assert_eq!(result.len(), 1024);
}
#[test]
fn test_poly1305_mac_async_small_chunks() {
let key: [u8; 32] = [0; 32];
let mut message: &[u8] = &[0; 32];
let tag = block_on(
poly1305_mac_async(&key, &mut message)
).unwrap();
let expected:&[u8] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
assert_eq!(&tag, expected);
}
#[test]
fn test_cha_cha_20_aead_decrypt_tampered() {
let key: [u8; 32] = [0; 32];
let nonce: [u8; 12] = [0; 12];
let plaintext: &[u8] = b"test";
let aad: &[u8] = b"aad";
let mut ciphertext = vec![];
let expected_tag = block_on(
cha_cha_20_aead_encrypt(aad, &key, nonce, plaintext, &mut ciphertext)
).unwrap();
ciphertext[0] ^= 1; let mut plain_text = vec![];
let result = block_on(
cha_cha_20_aead_decrypt_verify(aad, &key, nonce, ciphertext.as_slice(), &mut plain_text, expected_tag)
);
assert!(matches!(result, Err(err) if err.kind() == io::ErrorKind::InvalidData));
}
#[test]
fn test_cha_cha_20_aead_decrypt_wrong_tag() {
let key: [u8; 32] = [0; 32];
let nonce: [u8; 12] = [0; 12];
let plaintext: &[u8] = b"test";
let aad: &[u8] = b"aad";
let mut ciphertext = vec![];
let tag = block_on(
cha_cha_20_aead_encrypt(aad, &key, nonce, plaintext, &mut ciphertext)
).unwrap();
let mut wrong_tag = tag;
wrong_tag[0] ^= 1;
let mut plain_text = vec![];
let result = block_on(
cha_cha_20_aead_decrypt_verify(aad, &key, nonce,
ciphertext.as_slice(), &mut plain_text, wrong_tag)
);
assert!(matches!(result, Err(err) if err.kind() == io::ErrorKind::InvalidData));
}
#[test]
fn test_cha_cha_20_encrypt_reader_error() {
struct FailingReader;
impl AsyncRead for FailingReader {
fn poll_read(self: std::pin::Pin<&mut Self>, _cx: &mut std::task::Context<'_>, _buf: &mut [u8]) -> std::task::Poll<io::Result<usize>> {
std::task::Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, "read error")))
}
}
let key: [u8; 32] = [0; 32];
let nonce: [u8; 12] = [0; 12];
let mut cipher1 = vec![];
let result = block_on(
cha_cha_20_encrypt(&key, 1, &nonce, FailingReader, &mut cipher1),
);
assert!(result.is_err());
}
#[test]
fn test_cha_cha_20_key_sensitivity() {
let key1: [u8; 32] = [0; 32];
let key2: [u8; 32] = [1; 32];
let nonce: [u8; 12] = [0; 12];
let plaintext: &[u8] = b"test";
let mut cipher1 = vec![];
block_on(
cha_cha_20_encrypt(&key1, 1, &nonce, plaintext, &mut cipher1)
).unwrap();
let mut cipher2 = vec![];
block_on(
cha_cha_20_encrypt(&key2, 1, &nonce, plaintext, &mut cipher2)
).unwrap();
assert_ne!(cipher1, cipher2);
}
#[test]
fn test_cha_cha_20_nonce_reuse() {
let key: [u8; 32] = [0; 32];
let nonce1: [u8; 12] = [0; 12];
let nonce2: [u8; 12] = [1; 12];
let block1 = cha_cha_20_block(&key, 1, &nonce1);
let block2 = cha_cha_20_block(&key, 1, &nonce2);
assert_ne!(block1, block2);
}
#[test]
fn test_cha_cha_20_block_counter_zero() {
let key: [u8; 32] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
];
let nonce: [u8; 12] = [0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00];
let block = cha_cha_20_block(&key, 0, &nonce);
let expected: [u8; 64] = [
138, 220, 145, 253, 159, 244, 240, 245, 27, 15, 173, 80, 255, 21, 214, 55,
228, 14, 253, 162, 6, 204, 82, 199, 131, 167, 66, 0, 80, 60, 21, 130,
205, 152, 51, 54, 125, 10, 84, 213, 125, 60, 158, 153, 143, 73, 14, 230,
156, 163, 76, 31, 249, 233, 57, 167, 85, 132, 197, 45, 105, 10, 53, 212
];
assert_eq!(block, expected);
}
#[test]
fn test_cha_cha_20_encrypt_counter_max() {
let key: [u8; 32] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
];
let nonce: [u8; 12] = [0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00];
let plaintext: [u8; 64] = [0; 64];
let mut result = vec![];
block_on(
cha_cha_20_encrypt(&key, u32::MAX, &nonce, &plaintext[..], &mut result)
).unwrap();
let expected: [u8; 64] = [
255, 41, 65, 184, 215, 64, 246, 203, 181, 9, 54, 191, 153, 126, 189, 82,
24, 203, 16, 141, 197, 63, 65, 198, 72, 65, 208, 33, 129, 103, 67, 12,
160, 59, 119, 12, 167, 76, 203, 100, 42, 40, 25, 77, 29, 237, 210, 237,
19, 21, 30, 37, 236, 93, 127, 174, 182, 208, 96, 191, 183, 230, 177, 70
];
assert_eq!(result, expected);
}
#[test]
fn test_poly1305_mac_empty() {
let key: [u8; 32] = [
0x85, 0xd6, 0xbe, 0x78, 0x57, 0x55, 0x6d, 0x33,
0x7f, 0x44, 0x52, 0xfe, 0x42, 0xd5, 0x06, 0xa8,
0x01, 0x03, 0x80, 0x8a, 0xfb, 0x0d, 0xb2, 0xfd,
0x4a, 0xbf, 0xf6, 0xaf, 0x41, 0x49, 0xf5, 0x1b,
];
let message:&[u8] = &[];
let tag = block_on(
poly1305_mac_async(&key, message)
).unwrap();
let expected: [u8; 16] = [1, 3, 128, 138, 251, 13, 178, 253, 74, 191, 246, 175, 65, 73, 245, 27];
assert_eq!(tag, expected);
}
#[test]
fn test_poly1305_mac_16_bytes() {
let key: [u8; 32] = [
0x85, 0xd6, 0xbe, 0x78, 0x57, 0x55, 0x6d, 0x33,
0x7f, 0x44, 0x52, 0xfe, 0x42, 0xd5, 0x06, 0xa8,
0x01, 0x03, 0x80, 0x8a, 0xfb, 0x0d, 0xb2, 0xfd,
0x4a, 0xbf, 0xf6, 0xaf, 0x41, 0x49, 0xf5, 0x1b,
];
let message:&[u8] = &[
0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72,
0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x46, 0x6f,
];
let tag = block_on(
poly1305_mac_async(&key, message)
).unwrap();
let expected: [u8; 16] = [253, 134, 28, 113, 132, 249, 143, 69, 220, 109, 91, 77, 198, 192, 129, 228];
assert_eq!(tag, expected);
}
#[test]
fn test_cha_cha_20_aead_encrypt_empty() {
let key: [u8; 32] = [
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
];
let nonce: [u8; 12] = [0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47];
let plaintext: [u8; 0] = [];
let aad: [u8; 0] = [];
let mut ciphertext = vec![];
let tag = block_on(
cha_cha_20_aead_encrypt(&aad, &key, nonce, &plaintext[..], &mut ciphertext)
).unwrap();
let expected_tag: [u8; 16] = [
160, 120, 77, 122, 71, 22, 243, 254, 180, 246, 78, 127, 75, 57, 191, 4
];
assert_eq!(ciphertext, &[]);
assert_eq!(tag, expected_tag);
}
#[test]
fn test_cha_cha_20_aead_encrypt_aad_16_bytes() {
let key: [u8; 32] = [
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
];
let nonce: [u8; 12] = [0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47];
let plaintext: [u8; 4] = [0x74, 0x65, 0x73, 0x74]; let aad: [u8; 16] = [
0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0x00, 0x00, 0x00, 0x00,
];
let mut ciphertext = vec![];
let tag = block_on(
cha_cha_20_aead_encrypt(&aad, &key, nonce, &plaintext[..], &mut ciphertext)
).unwrap();
let expected_ciphertext: [u8; 4] = [0xeb, 0x1e, 0x9a, 0x29];
let expected_tag: [u8; 16] = [105, 124, 51, 142, 40, 75, 123, 86, 47, 48, 217, 240, 118, 71, 195, 224];
assert_eq!(ciphertext, expected_ciphertext);
assert_eq!(tag, expected_tag);
}
#[test]
fn test_cha_cha_20_encrypt_small_chunks() {
let key: [u8; 32] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
];
let nonce: [u8; 12] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00];
let plaintext: &[u8] = b"test message";
let mut result = vec![];
block_on(
cha_cha_20_encrypt(&key, 1, &nonce, plaintext, &mut result)
).unwrap();
let expected: [u8; 12] = [86, 42, 34, 135, 96, 118, 188, 146, 92, 191, 64, 10];
assert_eq!(&result, &expected[..]);
}
}