use crate::bs::{
block::{
Encypher,
block_decypher,
block_encypher,
},
nibble::Nibble,
stream::StreamState,
word::{
BLOCKS,
MAX_LANES,
PKT,
Word,
load_block,
load_block_bytes,
store_block_bytes,
},
};
pub struct CsaBatch {
cw: [u8; 8],
kk: [u8; 56],
}
impl CsaBatch {
pub fn new(cw: &[u8; 8]) -> Self {
let mut s = CsaBatch {
cw: [0; 8],
kk: [0; 56],
};
s.set_cw(cw);
s
}
pub fn set_cw(&mut self, cw: &[u8; 8]) {
self.cw = *cw;
crate::key::expand_key(&mut self.kk, cw);
}
pub fn decrypt_in_place(&self, packets: &mut [u8]) {
assert!(
packets.len() % PKT == 0,
"buffer length must be a multiple of 188"
);
self.decrypt_dispatch(packets, None);
}
pub(crate) fn decrypt_payloads_in_place(&self, packets: &mut [u8], blocks: &[u8]) {
assert!(
packets.len() == blocks.len() * PKT,
"one block count per 188-byte slot"
);
debug_assert!(blocks.iter().all(|&n| (1 ..= BLOCKS as u8).contains(&n)));
if blocks.iter().all(|&n| n == BLOCKS as u8) {
self.decrypt_dispatch(packets, None);
} else {
self.decrypt_dispatch(packets, Some(blocks));
}
}
fn decrypt_dispatch(&self, packets: &mut [u8], blocks: Option<&[u8]>) {
#[cfg(target_arch = "x86_64")]
if crate::bs::avx2::available() {
unsafe { self.descramble_avx2(packets, blocks) }
} else {
unsafe { self.descramble_sse2(packets, blocks) }
}
#[cfg(target_arch = "aarch64")]
self.descramble::<crate::bs::neon::W128>(packets, blocks);
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
self.descramble::<u64>(packets, blocks);
}
#[doc(hidden)]
pub fn decrypt_in_place_u64(&self, packets: &mut [u8]) {
assert!(
packets.len() % PKT == 0,
"buffer length must be a multiple of 188"
);
self.descramble::<u64>(packets, None);
}
#[doc(hidden)]
#[cfg(target_arch = "x86_64")]
pub fn decrypt_in_place_avx2(&self, packets: &mut [u8]) {
assert!(
packets.len() % PKT == 0,
"buffer length must be a multiple of 188"
);
assert!(crate::bs::avx2::available(), "AVX2 not available");
unsafe {
self.descramble_avx2(packets, None);
}
}
#[doc(hidden)]
#[cfg(target_arch = "x86_64")]
pub fn decrypt_in_place_sse2(&self, packets: &mut [u8]) {
assert!(
packets.len() % PKT == 0,
"buffer length must be a multiple of 188"
);
unsafe {
self.descramble_sse2(packets, None);
}
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
unsafe fn descramble_avx2(&self, packets: &mut [u8], blocks: Option<&[u8]>) {
self.descramble::<crate::bs::avx2::W256>(packets, blocks);
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "sse2")]
unsafe fn descramble_sse2(&self, packets: &mut [u8], blocks: Option<&[u8]>) {
self.descramble::<crate::bs::sse2::W128>(packets, blocks);
}
#[inline(always)]
fn descramble<W: Word>(&self, packets: &mut [u8], blocks: Option<&[u8]>) {
let ccw = ccw_bits::<W>(&self.cw);
let total = packets.len() / PKT;
let mut done = 0;
while done < total {
let lanes = core::cmp::min(W::LANES, total - done);
let group = &mut packets[done * PKT .. (done + lanes) * PKT];
let group_blocks = blocks.map(|b| &b[done .. done + lanes]);
descramble_group::<W>(&self.kk, &ccw, group, lanes, group_blocks);
done += lanes;
}
}
pub fn encrypt_in_place(&self, packets: &mut [u8]) {
assert!(
packets.len() % PKT == 0,
"buffer length must be a multiple of 188"
);
self.encrypt_dispatch(packets, None);
}
pub(crate) fn encrypt_payloads_in_place(&self, packets: &mut [u8], blocks: &[u8]) {
assert!(
packets.len() == blocks.len() * PKT,
"one block count per 188-byte slot"
);
debug_assert!(blocks.iter().all(|&n| (1 ..= BLOCKS as u8).contains(&n)));
if blocks.iter().all(|&n| n == BLOCKS as u8) {
self.encrypt_dispatch(packets, None);
} else {
self.encrypt_dispatch(packets, Some(blocks));
}
}
fn encrypt_dispatch(&self, packets: &mut [u8], blocks: Option<&[u8]>) {
#[cfg(target_arch = "x86_64")]
if crate::bs::avx2::available() {
unsafe { self.scramble_avx2(packets, blocks) }
} else {
unsafe { self.scramble_sse2(packets, blocks) }
}
#[cfg(target_arch = "aarch64")]
self.scramble::<crate::bs::neon::W128>(packets, blocks);
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
self.scramble::<u64>(packets, blocks);
}
#[doc(hidden)]
pub fn encrypt_in_place_u64(&self, packets: &mut [u8]) {
assert!(
packets.len() % PKT == 0,
"buffer length must be a multiple of 188"
);
self.scramble::<u64>(packets, None);
}
#[doc(hidden)]
#[cfg(target_arch = "x86_64")]
pub fn encrypt_in_place_avx2(&self, packets: &mut [u8]) {
assert!(
packets.len() % PKT == 0,
"buffer length must be a multiple of 188"
);
assert!(crate::bs::avx2::available(), "AVX2 not available");
unsafe {
self.scramble_avx2(packets, None);
}
}
#[doc(hidden)]
#[cfg(target_arch = "x86_64")]
pub fn encrypt_in_place_sse2(&self, packets: &mut [u8]) {
assert!(
packets.len() % PKT == 0,
"buffer length must be a multiple of 188"
);
unsafe {
self.scramble_sse2(packets, None);
}
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
unsafe fn scramble_avx2(&self, packets: &mut [u8], blocks: Option<&[u8]>) {
self.scramble::<crate::bs::avx2::W256>(packets, blocks);
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "sse2")]
unsafe fn scramble_sse2(&self, packets: &mut [u8], blocks: Option<&[u8]>) {
self.scramble::<crate::bs::sse2::W128>(packets, blocks);
}
#[inline(always)]
fn scramble<W: Word>(&self, packets: &mut [u8], blocks: Option<&[u8]>) {
let ccw = ccw_bits::<W>(&self.cw);
let total = packets.len() / PKT;
let mut done = 0;
while done < total {
let lanes = core::cmp::min(W::LANES, total - done);
let group = &mut packets[done * PKT .. (done + lanes) * PKT];
let group_blocks = blocks.map(|b| &b[done .. done + lanes]);
scramble_group::<W>(&self.kk, &ccw, group, lanes, group_blocks);
done += lanes;
}
}
}
#[inline(always)]
fn ccw_bits<W: Word>(cw: &[u8; 8]) -> [Nibble<W>; 16] {
let mut ccw = [Nibble::zero(); 16];
for i in 0 .. 8 {
let hi = cw[i] >> 4;
ccw[2 * i] = Nibble(
W::splat(hi & 1 == 1),
W::splat((hi >> 1) & 1 == 1),
W::splat((hi >> 2) & 1 == 1),
W::splat((hi >> 3) & 1 == 1),
);
let lo = cw[i];
ccw[2 * i + 1] = Nibble(
W::splat(lo & 1 == 1),
W::splat((lo >> 1) & 1 == 1),
W::splat((lo >> 2) & 1 == 1),
W::splat((lo >> 3) & 1 == 1),
);
}
ccw
}
#[inline(always)]
fn descramble_group<W: Word>(
kk: &[u8; 56],
ccw: &[Nibble<W>; 16],
group: &mut [u8],
lanes: usize,
blocks: Option<&[u8]>,
) {
let mut ct = [[[0u8; MAX_LANES]; 8]; BLOCKS];
for k in 0 .. BLOCKS {
load_block_bytes(group, lanes, k, &mut ct[k]);
}
let mut t = [[0u8; MAX_LANES]; 64];
let seed = load_block::<W>(group, lanes, 0);
let mut st = StreamState::<W>::new();
st.stream_init(ccw);
st.stream_cypher_init(&seed);
let mut block = ct[0];
let mut ks_bytes = [[0u8; MAX_LANES]; 8];
let mut pt = [[0u8; MAX_LANES]; 8];
let mut tail = [[0u8; MAX_LANES]; 8];
for i in 0 .. 22 {
block_decypher::<W>(&mut t, &block, kk, lanes); let ks = st.stream_cypher(); for j in 0 .. 8 {
W::scatter_bitplanes(&ks[j], &mut ks_bytes[j][.. W::LANES]); }
for j in 0 .. 8 {
for g in 0 .. lanes {
let nb = ks_bytes[j][g] ^ ct[i + 1][j][g];
block[j][g] = nb; pt[j][g] = nb ^ t[j][g]; }
}
if let Some(blocks) = blocks {
short_lanes(blocks, i, &block, &t, &mut pt, &mut tail);
}
store_block_bytes(&pt, group, lanes, i);
}
block_decypher::<W>(&mut t, &block, kk, lanes);
let mut last = [[0u8; MAX_LANES]; 8];
last.copy_from_slice(&t[0 .. 8]);
if let Some(blocks) = blocks {
short_lanes(blocks, BLOCKS - 1, &block, &t, &mut last, &mut tail);
}
store_block_bytes(&last, group, lanes, BLOCKS - 1);
}
#[inline(always)]
fn short_lanes(
blocks: &[u8],
i: usize,
block: &[[u8; MAX_LANES]; 8],
t: &[[u8; MAX_LANES]; 64],
pt: &mut [[u8; MAX_LANES]; 8],
tail: &mut [[u8; MAX_LANES]; 8],
) {
for (g, &n) in blocks.iter().enumerate() {
let n = n as usize;
if i + 1 == n {
for j in 0 .. 8 {
tail[j][g] = block[j][g];
pt[j][g] = t[j][g];
}
} else if i == n {
for j in 0 .. 8 {
pt[j][g] = tail[j][g];
}
}
}
}
#[inline(always)]
fn scramble_group<W: Word>(
kk: &[u8; 56],
ccw: &[Nibble<W>; 16],
group: &mut [u8],
lanes: usize,
blocks: Option<&[u8]>,
) {
let mut p = [[[0u8; MAX_LANES]; 8]; BLOCKS];
for k in 0 .. BLOCKS {
load_block_bytes(group, lanes, k, &mut p[k]);
}
let mut ib = [[[0u8; MAX_LANES]; 8]; BLOCKS];
let mut e = Encypher::new();
block_encypher::<W>(&mut e, &p[22], kk, &mut ib[22], lanes);
let mut x = [[0u8; MAX_LANES]; 8];
for i in (0 .. 22).rev() {
for j in 0 .. 8 {
for g in 0 .. lanes {
x[j][g] = p[i][j][g] ^ ib[i + 1][j][g];
}
}
if let Some(blocks) = blocks {
for (g, &n) in blocks.iter().enumerate() {
if n as usize == i + 1 {
for j in 0 .. 8 {
x[j][g] = p[i][j][g];
}
}
}
}
block_encypher::<W>(&mut e, &x, kk, &mut ib[i], lanes);
}
if let Some(blocks) = blocks {
for (g, &n) in blocks.iter().enumerate() {
let n = n as usize;
if n < BLOCKS {
for j in 0 .. 8 {
ib[n][j][g] = p[n][j][g];
}
}
}
}
store_block_bytes(&ib[0], group, lanes, 0);
let mut seed = [[W::zero(); 8]; 8];
for j in 0 .. 8 {
seed[j] = W::gather_bitplanes(&ib[0][j][0 .. lanes]);
}
let mut st = StreamState::<W>::new();
st.stream_init(ccw);
st.stream_cypher_init(&seed);
let mut ks_bytes = [[0u8; MAX_LANES]; 8];
let mut sb = [[0u8; MAX_LANES]; 8];
for k in 1 .. BLOCKS {
let ks = st.stream_cypher(); for j in 0 .. 8 {
W::scatter_bitplanes(&ks[j], &mut ks_bytes[j][.. W::LANES]); }
for j in 0 .. 8 {
for g in 0 .. lanes {
sb[j][g] = ib[k][j][g] ^ ks_bytes[j][g];
}
}
store_block_bytes(&sb, group, lanes, k);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
Csa,
bs::word::HDR,
};
include!("../../fixtures/dvb_csa.rs");
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
fn byte(&mut self) -> u8 {
self.next() as u8
}
}
fn scalar(cw: &[u8; 8], pkt: &[u8]) -> Vec<u8> {
let mut c = Csa::default();
c.set_cw(cw);
let mut out = vec![0u8; pkt.len()];
c.decrypt(pkt, &mut out);
out
}
fn scalar_encrypt(cw: &[u8; 8], pkt: &[u8]) -> Vec<u8> {
let mut c = Csa::default();
c.set_cw(cw);
let mut out = vec![0u8; pkt.len()];
c.encrypt(pkt, &mut out);
out
}
#[test]
fn batch_kat_u64() {
let n = 64;
let mut buf = TS_SCRAMBLED.repeat(n);
CsaBatch::new(&CW).decrypt_in_place_u64(&mut buf);
for p in 0 .. n {
assert_eq!(&buf[p * PKT .. (p + 1) * PKT], TS_CLEAR, "lane {p}");
}
}
#[test]
#[cfg(target_arch = "x86_64")]
fn batch_kat_avx2() {
if !crate::bs::avx2::available() {
return;
}
let n = 256;
let mut buf = TS_SCRAMBLED.repeat(n);
CsaBatch::new(&CW).decrypt_in_place_avx2(&mut buf);
for p in 0 .. n {
assert_eq!(&buf[p * PKT .. (p + 1) * PKT], TS_CLEAR, "lane {p}");
}
}
#[test]
#[cfg(target_arch = "x86_64")]
fn batch_kat_sse2() {
let n = 128;
let mut buf = TS_SCRAMBLED.repeat(n);
CsaBatch::new(&CW).decrypt_in_place_sse2(&mut buf);
for p in 0 .. n {
assert_eq!(&buf[p * PKT .. (p + 1) * PKT], TS_CLEAR, "lane {p}");
}
}
#[test]
fn batch_encrypt_kat_u64() {
let n = 64;
let mut buf = TS_CLEAR.repeat(n);
CsaBatch::new(&CW).encrypt_in_place_u64(&mut buf);
for p in 0 .. n {
assert_eq!(&buf[p * PKT .. (p + 1) * PKT], TS_SCRAMBLED, "lane {p}");
}
}
#[test]
#[cfg(target_arch = "x86_64")]
fn batch_encrypt_kat_avx2() {
if !crate::bs::avx2::available() {
return;
}
let n = 256;
let mut buf = TS_CLEAR.repeat(n);
CsaBatch::new(&CW).encrypt_in_place_avx2(&mut buf);
for p in 0 .. n {
assert_eq!(&buf[p * PKT .. (p + 1) * PKT], TS_SCRAMBLED, "lane {p}");
}
}
#[test]
#[cfg(target_arch = "x86_64")]
fn batch_encrypt_kat_sse2() {
let n = 128;
let mut buf = TS_CLEAR.repeat(n);
CsaBatch::new(&CW).encrypt_in_place_sse2(&mut buf);
for p in 0 .. n {
assert_eq!(&buf[p * PKT .. (p + 1) * PKT], TS_SCRAMBLED, "lane {p}");
}
}
fn round_trip(
encrypt: impl Fn(&CsaBatch, &mut [u8]),
decrypt: impl Fn(&CsaBatch, &mut [u8]),
count: usize,
seed: u64,
) {
let mut rng = Rng(seed);
let mut cw = [0u8; 8];
for x in cw.iter_mut() {
*x = rng.byte();
}
let mut buf = vec![0u8; count * PKT];
for x in buf.iter_mut() {
*x = rng.byte();
}
let orig = buf.clone();
let batch = CsaBatch::new(&cw);
encrypt(&batch, &mut buf);
decrypt(&batch, &mut buf);
assert_eq!(buf, orig, "encrypt->decrypt not identity");
decrypt(&batch, &mut buf);
encrypt(&batch, &mut buf);
assert_eq!(buf, orig, "decrypt->encrypt not identity");
}
#[test]
fn round_trip_u64() {
round_trip(
|b, buf| b.encrypt_in_place_u64(buf),
|b, buf| b.decrypt_in_place_u64(buf),
100,
0x1CE0_CEA5_u64,
);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn round_trip_avx2() {
if !crate::bs::avx2::available() {
return;
}
round_trip(
|b, buf| b.encrypt_in_place_avx2(buf),
|b, buf| b.decrypt_in_place_avx2(buf),
300,
0xAB5_1DE_5EED_u64,
);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn round_trip_sse2() {
round_trip(
|b, buf| b.encrypt_in_place_sse2(buf),
|b, buf| b.decrypt_in_place_sse2(buf),
150,
0x5EED_C0DE_u64,
);
}
fn every_lane_count<W: Word>(
dec: impl Fn(&CsaBatch, &mut [u8], Option<&[u8]>),
enc: impl Fn(&CsaBatch, &mut [u8]),
) {
let mut rng = Rng(W::LANES as u64);
let cw: [u8; 8] = core::array::from_fn(|_| rng.byte());
let batch = CsaBatch::new(&cw);
let mut csa = Csa::default();
csa.set_cw(&cw);
let src: Vec<u8> = (0 .. W::LANES * PKT).map(|_| rng.byte()).collect();
let lens: Vec<usize> = (0 .. W::LANES)
.map(|_| 8 + (rng.next() % 177) as usize)
.collect();
let blocks: Vec<u8> = lens.iter().map(|&len| (len / 8) as u8).collect();
let (mut clear, mut scrambled, mut short) = (src.clone(), src.clone(), src.clone());
for (p, &len) in lens.iter().enumerate() {
let at = p * PKT + HDR;
csa.decrypt_payload(&mut clear[at .. at + 184]);
csa.encrypt_payload(&mut scrambled[at .. at + 184]);
csa.decrypt_payload(&mut short[at .. at + len]);
}
for n in 1 ..= W::LANES {
let mut buf = src[.. n * PKT].to_vec();
dec(&batch, &mut buf, None);
assert!(buf == clear[.. n * PKT], "decrypt {n} lanes");
buf.copy_from_slice(&src[.. n * PKT]);
enc(&batch, &mut buf);
assert!(buf == scrambled[.. n * PKT], "encrypt {n} lanes");
buf.copy_from_slice(&src[.. n * PKT]);
dec(&batch, &mut buf, Some(&blocks[.. n]));
for (p, &len) in lens[.. n].iter().enumerate() {
let r = p * PKT + HDR .. p * PKT + HDR + len;
assert!(
buf[r.clone()] == short[r],
"{n} lanes, lane {p}, {len} bytes"
);
}
}
}
#[test]
fn every_lane_count_u64() {
every_lane_count::<u64>(
|b, buf, blocks| b.descramble::<u64>(buf, blocks),
|b, buf| b.encrypt_in_place_u64(buf),
);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn every_lane_count_sse2() {
every_lane_count::<crate::bs::sse2::W128>(
|b, buf, blocks| unsafe { b.descramble_sse2(buf, blocks) },
|b, buf| b.encrypt_in_place_sse2(buf),
);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn every_lane_count_avx2() {
if !crate::bs::avx2::available() {
return;
}
every_lane_count::<crate::bs::avx2::W256>(
|b, buf, blocks| unsafe { b.descramble_avx2(buf, blocks) },
|b, buf| b.encrypt_in_place_avx2(buf),
);
}
#[test]
#[cfg(target_arch = "aarch64")]
fn every_lane_count_neon() {
every_lane_count::<crate::bs::neon::W128>(
|b, buf, blocks| b.descramble::<crate::bs::neon::W128>(buf, blocks),
|b, buf| b.scramble::<crate::bs::neon::W128>(buf),
);
}
fn differential(mut run: impl FnMut(&CsaBatch, &mut [u8]), iters: usize, max: usize) {
let mut rng = Rng(0x0BAD_F00D_1234_5678);
for _ in 0 .. iters {
let mut cw = [0u8; 8];
for x in cw.iter_mut() {
*x = rng.byte();
}
let count = 1 + (rng.next() as usize) % max;
let mut buf = vec![0u8; count * PKT];
for x in buf.iter_mut() {
*x = rng.byte();
}
let mut expect = buf.clone();
for p in 0 .. count {
let out = scalar(&cw, &buf[p * PKT .. (p + 1) * PKT]);
expect[p * PKT .. (p + 1) * PKT].copy_from_slice(&out);
}
let batch = CsaBatch::new(&cw);
run(&batch, &mut buf);
for p in 0 .. count {
assert_eq!(
&buf[p * PKT .. (p + 1) * PKT],
&expect[p * PKT .. (p + 1) * PKT],
"packet {p} of {count} diverged"
);
}
}
}
#[test]
fn differential_vs_scalar_u64() {
differential(|b, buf| b.decrypt_in_place_u64(buf), 100, 200);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn differential_vs_scalar_avx2() {
if !crate::bs::avx2::available() {
return;
}
differential(|b, buf| b.decrypt_in_place_avx2(buf), 20, 600);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn differential_vs_scalar_sse2() {
differential(|b, buf| b.decrypt_in_place_sse2(buf), 20, 400);
}
#[test]
fn partial_batches_u64() {
let mut rng = Rng(0xC0FF_EE00);
let mut cw = [0u8; 8];
for x in cw.iter_mut() {
*x = rng.byte();
}
for &count in &[1usize, 3, 63, 64, 65, 127, 128, 131] {
let mut buf = vec![0u8; count * PKT];
for x in buf.iter_mut() {
*x = rng.byte();
}
let mut expect = buf.clone();
for p in 0 .. count {
let out = scalar(&cw, &buf[p * PKT .. (p + 1) * PKT]);
expect[p * PKT .. (p + 1) * PKT].copy_from_slice(&out);
}
CsaBatch::new(&cw).decrypt_in_place_u64(&mut buf);
assert_eq!(buf, expect, "count {count}");
}
}
#[test]
fn dispatch_auto_all_sizes() {
let mut rng = Rng(0xD15_9A7C_4321);
let mut cw = [0u8; 8];
for x in cw.iter_mut() {
*x = rng.byte();
}
for &count in &[
1usize, 63, 64, 65, 127, 128, 129, 200, 255, 256, 257, 384, 512, 700,
] {
let mut buf = vec![0u8; count * PKT];
for x in buf.iter_mut() {
*x = rng.byte();
}
let mut expect = buf.clone();
for p in 0 .. count {
let out = scalar(&cw, &buf[p * PKT .. (p + 1) * PKT]);
expect[p * PKT .. (p + 1) * PKT].copy_from_slice(&out);
}
CsaBatch::new(&cw).decrypt_in_place(&mut buf);
assert_eq!(buf, expect, "count {count}");
}
}
fn differential_encrypt(mut run: impl FnMut(&CsaBatch, &mut [u8]), iters: usize, max: usize) {
let mut rng = Rng(0xD00D_FEED_8765_4321);
for _ in 0 .. iters {
let mut cw = [0u8; 8];
for x in cw.iter_mut() {
*x = rng.byte();
}
let count = 1 + (rng.next() as usize) % max;
let mut buf = vec![0u8; count * PKT];
for x in buf.iter_mut() {
*x = rng.byte();
}
let mut expect = buf.clone();
for p in 0 .. count {
let out = scalar_encrypt(&cw, &buf[p * PKT .. (p + 1) * PKT]);
expect[p * PKT .. (p + 1) * PKT].copy_from_slice(&out);
}
let batch = CsaBatch::new(&cw);
run(&batch, &mut buf);
for p in 0 .. count {
assert_eq!(
&buf[p * PKT .. (p + 1) * PKT],
&expect[p * PKT .. (p + 1) * PKT],
"packet {p} of {count} diverged"
);
}
}
}
#[test]
fn differential_encrypt_vs_scalar_u64() {
differential_encrypt(|b, buf| b.encrypt_in_place_u64(buf), 100, 200);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn differential_encrypt_vs_scalar_avx2() {
if !crate::bs::avx2::available() {
return;
}
differential_encrypt(|b, buf| b.encrypt_in_place_avx2(buf), 20, 600);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn differential_encrypt_vs_scalar_sse2() {
differential_encrypt(|b, buf| b.encrypt_in_place_sse2(buf), 20, 400);
}
#[test]
fn partial_encrypt_batches_u64() {
let mut rng = Rng(0xFACE_B00C);
let mut cw = [0u8; 8];
for x in cw.iter_mut() {
*x = rng.byte();
}
for &count in &[
1usize, 3, 63, 64, 65, 127, 128, 129, 200, 255, 256, 257, 384, 512, 700,
] {
let mut buf = vec![0u8; count * PKT];
for x in buf.iter_mut() {
*x = rng.byte();
}
let mut expect = buf.clone();
for p in 0 .. count {
let out = scalar_encrypt(&cw, &buf[p * PKT .. (p + 1) * PKT]);
expect[p * PKT .. (p + 1) * PKT].copy_from_slice(&out);
}
CsaBatch::new(&cw).encrypt_in_place_u64(&mut buf);
assert_eq!(buf, expect, "count {count}");
}
}
#[test]
fn encrypt_dispatch_auto_all_sizes() {
let mut rng = Rng(0xE4C_1234_9A7C);
let mut cw = [0u8; 8];
for x in cw.iter_mut() {
*x = rng.byte();
}
for &count in &[
1usize, 3, 63, 64, 65, 127, 128, 129, 200, 255, 256, 257, 384, 512, 700,
] {
let mut buf = vec![0u8; count * PKT];
for x in buf.iter_mut() {
*x = rng.byte();
}
let mut expect = buf.clone();
for p in 0 .. count {
let out = scalar_encrypt(&cw, &buf[p * PKT .. (p + 1) * PKT]);
expect[p * PKT .. (p + 1) * PKT].copy_from_slice(&out);
}
CsaBatch::new(&cw).encrypt_in_place(&mut buf);
assert_eq!(buf, expect, "count {count}");
}
}
#[test]
fn payloads_vs_scalar() {
let mut rng = Rng(7);
for n in [1usize, 63, 64, 130, 300] {
let cw: [u8; 8] = core::array::from_fn(|_| rng.byte());
let lens: Vec<usize> = (0 .. n).map(|_| 8 + (rng.next() % 177) as usize).collect();
let mut buf = vec![0u8; n * PKT];
for (slot, &len) in buf.chunks_exact_mut(PKT).zip(&lens) {
for b in &mut slot[HDR .. HDR + len] {
*b = rng.byte();
}
}
let mut csa = Csa::default();
csa.set_cw(&cw);
let mut expected = buf.clone();
for (slot, &len) in expected.chunks_exact_mut(PKT).zip(&lens) {
csa.decrypt_payload(&mut slot[HDR .. HDR + len]);
}
let blocks: Vec<u8> = lens.iter().map(|&len| (len / 8) as u8).collect();
CsaBatch::new(&cw).decrypt_payloads_in_place(&mut buf, &blocks);
for (p, &len) in lens.iter().enumerate() {
let r = p * PKT + HDR .. p * PKT + HDR + len;
assert_eq!(buf[r.clone()], expected[r], "n {n} lane {p} len {len}");
}
}
}
#[test]
fn encrypt_payloads_vs_scalar() {
let mut rng = Rng(11);
for n in [1usize, 63, 64, 130, 300, 700] {
let cw: [u8; 8] = core::array::from_fn(|_| rng.byte());
let lens: Vec<usize> = (0 .. n)
.map(|p| {
if n >= 300 {
8 + p % 177
} else {
8 + (rng.next() % 177) as usize
}
})
.collect();
let mut buf = vec![0u8; n * PKT];
for (slot, &len) in buf.chunks_exact_mut(PKT).zip(&lens) {
for b in &mut slot[HDR .. HDR + len] {
*b = rng.byte();
}
}
let mut csa = Csa::default();
csa.set_cw(&cw);
let mut expected = buf.clone();
for (slot, &len) in expected.chunks_exact_mut(PKT).zip(&lens) {
csa.encrypt_payload(&mut slot[HDR .. HDR + len]);
}
let blocks: Vec<u8> = lens.iter().map(|&len| (len / 8) as u8).collect();
let batch = CsaBatch::new(&cw);
let mut wide = buf.clone();
batch.encrypt_payloads_in_place(&mut wide, &blocks);
let mut narrow = buf.clone();
batch.scramble::<u64>(&mut narrow, Some(&blocks));
#[cfg(target_arch = "x86_64")]
unsafe {
batch.scramble_sse2(&mut buf, Some(&blocks));
}
#[cfg(not(target_arch = "x86_64"))]
buf.copy_from_slice(&narrow);
for (p, &len) in lens.iter().enumerate() {
let r = p * PKT + HDR .. p * PKT + HDR + len;
assert_eq!(
wide[r.clone()],
expected[r.clone()],
"n {n} lane {p} len {len}"
);
assert_eq!(
narrow[r.clone()],
expected[r.clone()],
"u64 n {n} lane {p} len {len}"
);
assert_eq!(buf[r.clone()], expected[r], "sse2 n {n} lane {p} len {len}");
}
}
}
}