use crate::bs::word::{
MAX_LANES,
Word,
};
#[inline(always)]
fn perm_byte(x: u8) -> u8 {
((x & 0x29) << 1)
| ((x & 0x02) << 6)
| ((x & 0x04) << 3)
| ((x & 0x10) >> 2)
| ((x & 0x40) >> 6)
| ((x & 0x80) >> 4)
}
#[inline(always)]
pub fn block_decypher<W: Word>(
t: &mut [[u8; MAX_LANES]; 64],
ib: &[[u8; MAX_LANES]; 8],
kk: &[u8; 56],
lanes: usize,
) {
if lanes > W::LANES / 4 * 3 {
decypher(t, ib, kk, W::LANES, W::LANES)
} else {
let n = lanes.next_multiple_of(8).min(W::LANES);
decypher(t, ib, kk, n, lanes.next_multiple_of(32).min(W::LANES))
}
}
#[inline(always)]
fn sbox_plane(dst: &mut [u8; MAX_LANES], src: &[u8; MAX_LANES], k: u8, n: usize) {
let k = u64::from(k) * 0x0101_0101_0101_0101;
for g in (0 .. n).step_by(8) {
let x = u64::from_le_bytes(src[g .. g + 8].try_into().unwrap()) ^ k;
let mut y = 0u64;
for b in 0 .. 8 {
y |= u64::from(crate::csa::BLOCK_SBOX[(x >> (8 * b)) as u8 as usize]) << (8 * b);
}
dst[g .. g + 8].copy_from_slice(&y.to_le_bytes());
}
}
#[inline(always)]
fn decypher(
t: &mut [[u8; MAX_LANES]; 64],
ib: &[[u8; MAX_LANES]; 8],
kk: &[u8; 56],
n: usize,
m: usize,
) {
for j in 0 .. 8 {
t[56 + j] = ib[j]; }
let mut raw = [0u8; MAX_LANES];
for i in (0 ..= 55).rev() {
sbox_plane(&mut raw, &t[i + 7], kk[i], n);
for g in 0 .. m {
t[i + 6][g] ^= perm_byte(raw[g]);
}
for g in 0 .. m {
let so = raw[g] ^ t[i + 8][g];
t[i + 4][g] ^= so;
t[i + 3][g] ^= so;
t[i + 2][g] ^= so;
t[i][g] = so;
}
}
}
pub struct Encypher {
t: [[u8; MAX_LANES]; 64],
so: [[u8; MAX_LANES]; 62],
s: [[u8; MAX_LANES]; 62],
}
impl Encypher {
pub fn new() -> Self {
Encypher {
t: [[0; MAX_LANES]; 64],
so: [[0; MAX_LANES]; 62],
s: [[0; MAX_LANES]; 62],
}
}
}
#[inline(always)]
pub fn block_encypher<W: Word>(
e: &mut Encypher,
p: &[[u8; MAX_LANES]; 8],
kk: &[u8; 56],
out_ib: &mut [[u8; MAX_LANES]; 8],
lanes: usize,
) {
if lanes > W::LANES / 4 * 3 {
encypher(e, p, kk, out_ib, W::LANES, W::LANES)
} else {
let n = lanes.next_multiple_of(8).min(W::LANES);
encypher(
e,
p,
kk,
out_ib,
n,
lanes.next_multiple_of(32).min(W::LANES),
)
}
}
#[inline(always)]
fn encypher(
e: &mut Encypher,
p: &[[u8; MAX_LANES]; 8],
kk: &[u8; 56],
out_ib: &mut [[u8; MAX_LANES]; 8],
n: usize,
m: usize,
) {
let Encypher { t, so, s } = e;
for j in 0 .. 8 {
t[j] = p[j]; }
for i in 0 .. 56 {
sbox_plane(&mut s[i + 6], &t[i + 7], kk[i], n);
for g in 0 .. m {
let o = t[i][g] ^ so[i + 4][g] ^ so[i + 3][g] ^ so[i + 2][g] ^ perm_byte(s[i][g]);
so[i + 6][g] = o;
t[i + 8][g] = s[i + 6][g] ^ o;
}
}
for g in 0 .. m {
out_ib[0][g] = t[56][g] ^ so[60][g] ^ so[59][g] ^ so[58][g] ^ perm_byte(s[56][g]);
out_ib[1][g] = t[57][g] ^ so[61][g] ^ so[60][g] ^ so[59][g] ^ perm_byte(s[57][g]);
out_ib[2][g] = t[58][g] ^ so[61][g] ^ so[60][g] ^ perm_byte(s[58][g]);
out_ib[3][g] = t[59][g] ^ so[61][g] ^ perm_byte(s[59][g]);
out_ib[4][g] = t[60][g] ^ perm_byte(s[60][g]);
out_ib[5][g] = t[61][g] ^ perm_byte(s[61][g]);
out_ib[6][g] = t[62][g];
out_ib[7][g] = t[63][g];
}
}
#[cfg(test)]
mod tests {
use super::*;
fn scalar_block(ib: &[u8; 8], kk: &[u8; 56]) -> [u8; 8] {
let mut tt = [0u8; 64];
tt[56 .. 64].copy_from_slice(ib);
let mut t6 = ib[6];
for i in (0 ..= 55).rev() {
t6 ^= kk[i];
let s = crate::csa::BLOCK_SBOX[t6 as usize];
t6 = tt[i + 6] ^ crate::csa::BLOCK_PERM[s as usize];
tt[i + 6] = t6;
let so = s ^ tt[i + 8];
tt[i + 4] ^= so;
tt[i + 3] ^= so;
tt[i + 2] ^= so;
tt[i] = so;
}
let mut out = [0u8; 8];
out.copy_from_slice(&tt[0 .. 8]);
out
}
#[test]
fn shift_perm_matches_block_perm() {
for x in 0 .. 256usize {
assert_eq!(perm_byte(x as u8), crate::csa::BLOCK_PERM[x], "x={x:#04x}");
}
}
#[test]
fn byte_block_matches_scalar() {
let mut s = 0x1234_5678_9ABC_DEF0u64;
let mut nextb = || {
s = s.wrapping_mul(6364136223846793005).wrapping_add(1);
(s >> 33) as u8
};
for _ in 0 .. 500 {
let mut kk = [0u8; 56];
for k in kk.iter_mut() {
*k = nextb();
}
let mut inputs = [[0u8; 8]; 64];
let mut ib = [[0u8; MAX_LANES]; 8];
for g in 0 .. 64 {
for j in 0 .. 8 {
let b = nextb();
inputs[g][j] = b;
ib[j][g] = b;
}
}
let mut t = [[0u8; MAX_LANES]; 64];
block_decypher::<u64>(&mut t, &ib, &kk, 64);
for g in 0 .. 64 {
let exp = scalar_block(&inputs[g], &kk);
for j in 0 .. 8 {
assert_eq!(t[j][g], exp[j], "lane {g} byte {j}");
}
}
}
}
#[test]
fn byte_block_encypher_inverts_scalar() {
let mut s = 0xFEDC_BA98_7654_3210u64;
let mut nextb = || {
s = s.wrapping_mul(6364136223846793005).wrapping_add(1);
(s >> 33) as u8
};
let mut e = Encypher::new();
for _ in 0 .. 500 {
let mut kk = [0u8; 56];
for k in kk.iter_mut() {
*k = nextb();
}
let mut inputs = [[0u8; 8]; 64];
let mut p = [[0u8; MAX_LANES]; 8];
for g in 0 .. 64 {
let mut ib = [0u8; 8];
for b in ib.iter_mut() {
*b = nextb();
}
inputs[g] = ib;
let out = scalar_block(&ib, &kk);
for j in 0 .. 8 {
p[j][g] = out[j];
}
}
let mut rec = [[0u8; MAX_LANES]; 8];
block_encypher::<u64>(&mut e, &p, &kk, &mut rec, 64);
for g in 0 .. 64 {
for j in 0 .. 8 {
assert_eq!(rec[j][g], inputs[g][j], "lane {g} byte {j}");
}
}
}
}
}