sse2neon 0.1.0

x86 SSE-to-ARM NEON intrinsic compatibility shim
Documentation
//! AES-NI intrinsics and carry-less multiply.
//!
//! Implements the x86 AES round instructions in software over the standard
//! Rijndael S-box. Each function reproduces the exact x86 AES-NI transform:
//! ShiftRows, SubBytes, MixColumns, and AddRoundKey in the order the hardware
//! applies them. Results match AES-NI bit for bit.

use crate::sse2::_mm_set_epi32;
use crate::types::*;

/// Rijndael forward S-box, 256 entries.
#[rustfmt::skip]
const SBOX: [u8; 256] = [
    0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
    0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
    0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
    0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
    0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
    0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
    0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
    0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
    0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
    0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
    0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
    0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
    0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
    0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
    0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
    0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
];

/// Rijndael inverse S-box, 256 entries.
#[rustfmt::skip]
const RSBOX: [u8; 256] = [
    0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
    0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
    0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
    0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
    0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
    0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
    0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
    0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
    0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
    0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
    0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
    0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
    0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
    0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
    0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
    0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
];

/// ShiftRows byte permutation. Output byte `i` comes from input byte `SHIFT[i]`.
const SHIFT_ROWS: [usize; 16] = [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11];

/// InvShiftRows byte permutation for decryption.
const INV_SHIFT_ROWS: [usize; 16] = [0, 13, 10, 7, 4, 1, 14, 11, 8, 5, 2, 15, 12, 9, 6, 3];

/// Multiply a byte by `{02}` in GF(2^8) with reduction polynomial `0x11b`.
#[inline]
fn xtime(v: u8) -> u8 {
    let hi = (v >> 7) & 1;
    (v << 1) ^ (hi * 0x1b)
}

/// Multiply `x` by `y` in GF(2^8).
#[inline]
fn gmul(mut x: u8, mut y: u8) -> u8 {
    let mut p = 0u8;
    for _ in 0..8 {
        if y & 1 == 1 {
            p ^= x;
        }
        y >>= 1;
        x = xtime(x);
    }
    p
}

fn bytes(a: __m128i) -> [u8; 16] {
    crate::sse2::to_u8_array(a)
}

fn from_bytes(b: [u8; 16]) -> __m128i {
    __m128i::from_u8(unsafe { core::arch::aarch64::vld1q_u8(b.as_ptr()) })
}

fn sub_bytes(s: &mut [u8; 16]) {
    for b in s.iter_mut() {
        *b = SBOX[*b as usize];
    }
}

fn inv_sub_bytes(s: &mut [u8; 16]) {
    for b in s.iter_mut() {
        *b = RSBOX[*b as usize];
    }
}

fn shift_rows(s: [u8; 16]) -> [u8; 16] {
    let mut out = [0u8; 16];
    for (i, o) in out.iter_mut().enumerate() {
        *o = s[SHIFT_ROWS[i]];
    }
    out
}

fn inv_shift_rows(s: [u8; 16]) -> [u8; 16] {
    let mut out = [0u8; 16];
    for (i, o) in out.iter_mut().enumerate() {
        *o = s[INV_SHIFT_ROWS[i]];
    }
    out
}

fn mix_columns(s: [u8; 16]) -> [u8; 16] {
    let mut out = [0u8; 16];
    for c in 0..4 {
        let i = c * 4;
        let a0 = s[i];
        let a1 = s[i + 1];
        let a2 = s[i + 2];
        let a3 = s[i + 3];
        out[i] = xtime(a0) ^ (xtime(a1) ^ a1) ^ a2 ^ a3;
        out[i + 1] = a0 ^ xtime(a1) ^ (xtime(a2) ^ a2) ^ a3;
        out[i + 2] = a0 ^ a1 ^ xtime(a2) ^ (xtime(a3) ^ a3);
        out[i + 3] = (xtime(a0) ^ a0) ^ a1 ^ a2 ^ xtime(a3);
    }
    out
}

fn inv_mix_columns(s: [u8; 16]) -> [u8; 16] {
    let mut out = [0u8; 16];
    for c in 0..4 {
        let i = c * 4;
        let a0 = s[i];
        let a1 = s[i + 1];
        let a2 = s[i + 2];
        let a3 = s[i + 3];
        out[i] = gmul(a0, 0x0e) ^ gmul(a1, 0x0b) ^ gmul(a2, 0x0d) ^ gmul(a3, 0x09);
        out[i + 1] = gmul(a0, 0x09) ^ gmul(a1, 0x0e) ^ gmul(a2, 0x0b) ^ gmul(a3, 0x0d);
        out[i + 2] = gmul(a0, 0x0d) ^ gmul(a1, 0x09) ^ gmul(a2, 0x0e) ^ gmul(a3, 0x0b);
        out[i + 3] = gmul(a0, 0x0b) ^ gmul(a1, 0x0d) ^ gmul(a2, 0x09) ^ gmul(a3, 0x0e);
    }
    out
}

fn xor16(a: [u8; 16], b: [u8; 16]) -> [u8; 16] {
    let mut out = [0u8; 16];
    for i in 0..16 {
        out[i] = a[i] ^ b[i];
    }
    out
}

/// One AES encryption round: ShiftRows, SubBytes, MixColumns, then XOR the key.
/// Matches `_mm_aesenc_si128`.
#[inline]
pub fn _mm_aesenc_si128(a: __m128i, round_key: __m128i) -> __m128i {
    let mut s = shift_rows(bytes(a));
    sub_bytes(&mut s);
    let s = mix_columns(s);
    from_bytes(xor16(s, bytes(round_key)))
}

/// Last AES encryption round: ShiftRows, SubBytes, then XOR the key.
/// Matches `_mm_aesenclast_si128`.
#[inline]
pub fn _mm_aesenclast_si128(a: __m128i, round_key: __m128i) -> __m128i {
    let mut s = shift_rows(bytes(a));
    sub_bytes(&mut s);
    from_bytes(xor16(s, bytes(round_key)))
}

/// One AES decryption round: InvShiftRows, InvSubBytes, InvMixColumns, XOR key.
/// Matches `_mm_aesdec_si128`.
#[inline]
pub fn _mm_aesdec_si128(a: __m128i, round_key: __m128i) -> __m128i {
    let mut s = inv_shift_rows(bytes(a));
    inv_sub_bytes(&mut s);
    let s = inv_mix_columns(s);
    from_bytes(xor16(s, bytes(round_key)))
}

/// Last AES decryption round: InvShiftRows, InvSubBytes, then XOR the key.
/// Matches `_mm_aesdeclast_si128`.
#[inline]
pub fn _mm_aesdeclast_si128(a: __m128i, round_key: __m128i) -> __m128i {
    let mut s = inv_shift_rows(bytes(a));
    inv_sub_bytes(&mut s);
    from_bytes(xor16(s, bytes(round_key)))
}

/// Apply InvMixColumns to `a`. Matches `_mm_aesimc_si128`.
#[inline]
pub fn _mm_aesimc_si128(a: __m128i) -> __m128i {
    from_bytes(inv_mix_columns(bytes(a)))
}

/// Assist AES key expansion. Matches `_mm_aeskeygenassist_si128`.
///
/// Applies SubWord to words 1 and 3, rotates them one byte, and XORs `RCON`.
/// The output is `{SubWord(X1), RotWord(SubWord(X1)) ^ rcon, SubWord(X3),
/// RotWord(SubWord(X3)) ^ rcon}`.
#[inline]
pub fn _mm_aeskeygenassist_si128<const RCON: i32>(a: __m128i) -> __m128i {
    const { assert!(RCON >= 0 && RCON < 256, "RCON must be in 0..256") };
    let b = bytes(a);
    let x1 = u32::from_le_bytes([b[4], b[5], b[6], b[7]]);
    let x3 = u32::from_le_bytes([b[12], b[13], b[14], b[15]]);
    let s1 = sub_word(x1);
    let s3 = sub_word(x3);
    let rcon = RCON as u32;
    let w1 = s1.rotate_right(8) ^ rcon;
    let w3 = s3.rotate_right(8) ^ rcon;
    _mm_set_epi32(w3 as i32, s3 as i32, w1 as i32, s1 as i32)
}

fn sub_word(w: u32) -> u32 {
    let b = w.to_le_bytes();
    u32::from_le_bytes([
        SBOX[b[0] as usize],
        SBOX[b[1] as usize],
        SBOX[b[2] as usize],
        SBOX[b[3] as usize],
    ])
}

/// Carry-less multiply of selected 64-bit halves. Matches `_mm_clmulepi64_si128`.
///
/// `IMM & 0x01` picks the half of `a`, `IMM & 0x10` picks the half of `b`.
/// The 128-bit product has no carries between bit positions.
#[inline]
pub fn _mm_clmulepi64_si128<const IMM: i32>(a: __m128i, b: __m128i) -> __m128i {
    const { assert!(IMM >= 0 && IMM < 256, "IMM must be in 0..256") };
    let av = crate::sse2::_mm_cvtsi128_si64(if IMM & 0x01 == 1 { high_i64(a) } else { a }) as u64;
    let bv =
        crate::sse2::_mm_cvtsi128_si64(if IMM & 0x10 == 0x10 { high_i64(b) } else { b }) as u64;
    let product = clmul64(av, bv);
    crate::sse2::_mm_set_epi64x(product.1 as i64, product.0 as i64)
}

fn high_i64(a: __m128i) -> __m128i {
    let hi = unsafe { core::arch::aarch64::vgetq_lane_s64(a.s64(), 1) };
    crate::sse2::_mm_cvtsi64_si128(hi)
}

/// Carry-less multiply of two 64-bit values into a 128-bit result `(low, high)`.
fn clmul64(a: u64, b: u64) -> (u64, u64) {
    let mut lo = 0u64;
    let mut hi = 0u64;
    for i in 0..64 {
        if (b >> i) & 1 == 1 {
            lo ^= a << i;
            if i > 0 {
                hi ^= a >> (64 - i);
            }
        }
    }
    (lo, hi)
}