#![allow(dead_code)]
use sha2::{Digest, Sha256};
static 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,
];
static INV_SBOX: [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, 0xa0, 0x7d, 0x02, 0x12, 0x32, 0xf3, 0x52, 0x2b, 0x04, 0x2a, 0x0c,
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,
];
#[inline(always)]
fn xtime(a: u8) -> u8 {
let shifted = (a as u16) << 1;
if shifted & 0x100 != 0 {
(shifted ^ 0x11b) as u8
} else {
shifted as u8
}
}
#[inline(always)]
fn gf_mul(mut a: u8, mut b: u8) -> u8 {
let mut result = 0u8;
while b > 0 {
if b & 1 != 0 {
result ^= a;
}
a = xtime(a);
b >>= 1;
}
result
}
static RCON: [u8; 11] = [
0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
];
fn aes_key_expansion(key: &[u8]) -> Result<(Vec<u8>, usize), String> {
let nk = key.len() / 4; let nr = nk + 6; let total_words = (nr + 1) * 4;
let mut w = vec![0u32; total_words];
for (i, word) in w[..nk].iter_mut().enumerate() {
let base = i * 4;
*word = u32::from_be_bytes([key[base], key[base + 1], key[base + 2], key[base + 3]]);
}
for i in nk..total_words {
let mut temp = w[i - 1];
if i % nk == 0 {
temp = temp.rotate_left(8);
temp = sub_word(temp);
temp ^= (RCON[i / nk] as u32) << 24;
} else if nk > 6 && i % nk == 4 {
temp = sub_word(temp);
}
w[i] = w[i - nk] ^ temp;
}
let mut round_keys = vec![0u8; total_words * 4];
for (i, &word) in w.iter().enumerate() {
round_keys[i * 4..i * 4 + 4].copy_from_slice(&word.to_be_bytes());
}
Ok((round_keys, nr))
}
#[inline(always)]
fn sub_word(w: u32) -> u32 {
let b = w.to_be_bytes();
u32::from_be_bytes([
SBOX[b[0] as usize],
SBOX[b[1] as usize],
SBOX[b[2] as usize],
SBOX[b[3] as usize],
])
}
type AesState = [[u8; 4]; 4];
fn block_to_state(block: &[u8; 16]) -> AesState {
let mut s = [[0u8; 4]; 4];
for col in 0..4 {
for row in 0..4 {
s[col][row] = block[col * 4 + row];
}
}
s
}
fn state_to_block(s: &AesState) -> [u8; 16] {
let mut out = [0u8; 16];
for col in 0..4 {
for row in 0..4 {
out[col * 4 + row] = s[col][row];
}
}
out
}
fn sub_bytes(s: &mut AesState) {
for col in s.iter_mut() {
for b in col.iter_mut() {
*b = SBOX[*b as usize];
}
}
}
fn inv_sub_bytes(s: &mut AesState) {
for col in s.iter_mut() {
for b in col.iter_mut() {
*b = INV_SBOX[*b as usize];
}
}
}
fn shift_rows(s: &mut AesState) {
let tmp = s[0][1];
s[0][1] = s[1][1];
s[1][1] = s[2][1];
s[2][1] = s[3][1];
s[3][1] = tmp;
let (a, b) = (s[0][2], s[1][2]);
s[0][2] = s[2][2];
s[1][2] = s[3][2];
s[2][2] = a;
s[3][2] = b;
let tmp = s[3][3];
s[3][3] = s[2][3];
s[2][3] = s[1][3];
s[1][3] = s[0][3];
s[0][3] = tmp;
}
fn inv_shift_rows(s: &mut AesState) {
let tmp = s[3][1];
s[3][1] = s[2][1];
s[2][1] = s[1][1];
s[1][1] = s[0][1];
s[0][1] = tmp;
let (a, b) = (s[0][2], s[1][2]);
s[0][2] = s[2][2];
s[1][2] = s[3][2];
s[2][2] = a;
s[3][2] = b;
let tmp = s[0][3];
s[0][3] = s[1][3];
s[1][3] = s[2][3];
s[2][3] = s[3][3];
s[3][3] = tmp;
}
fn mix_columns(s: &mut AesState) {
for col in s.iter_mut() {
let [s0, s1, s2, s3] = *col;
col[0] = gf_mul(2, s0) ^ gf_mul(3, s1) ^ s2 ^ s3;
col[1] = s0 ^ gf_mul(2, s1) ^ gf_mul(3, s2) ^ s3;
col[2] = s0 ^ s1 ^ gf_mul(2, s2) ^ gf_mul(3, s3);
col[3] = gf_mul(3, s0) ^ s1 ^ s2 ^ gf_mul(2, s3);
}
}
fn inv_mix_columns(s: &mut AesState) {
for col in s.iter_mut() {
let [s0, s1, s2, s3] = *col;
col[0] = gf_mul(0x0e, s0) ^ gf_mul(0x0b, s1) ^ gf_mul(0x0d, s2) ^ gf_mul(0x09, s3);
col[1] = gf_mul(0x09, s0) ^ gf_mul(0x0e, s1) ^ gf_mul(0x0b, s2) ^ gf_mul(0x0d, s3);
col[2] = gf_mul(0x0d, s0) ^ gf_mul(0x09, s1) ^ gf_mul(0x0e, s2) ^ gf_mul(0x0b, s3);
col[3] = gf_mul(0x0b, s0) ^ gf_mul(0x0d, s1) ^ gf_mul(0x09, s2) ^ gf_mul(0x0e, s3);
}
}
fn add_round_key(s: &mut AesState, round_key: &[u8]) {
for col in 0..4 {
for row in 0..4 {
s[col][row] ^= round_key[col * 4 + row];
}
}
}
fn aes_encrypt_block(block: &[u8; 16], round_keys: &[u8], nr: usize) -> [u8; 16] {
let mut state = block_to_state(block);
add_round_key(&mut state, &round_keys[0..16]);
for round in 1..nr {
sub_bytes(&mut state);
shift_rows(&mut state);
mix_columns(&mut state);
add_round_key(&mut state, &round_keys[round * 16..(round + 1) * 16]);
}
sub_bytes(&mut state);
shift_rows(&mut state);
add_round_key(&mut state, &round_keys[nr * 16..(nr + 1) * 16]);
state_to_block(&state)
}
fn aes_decrypt_block(block: &[u8; 16], round_keys: &[u8], nr: usize) -> [u8; 16] {
let mut state = block_to_state(block);
add_round_key(&mut state, &round_keys[nr * 16..(nr + 1) * 16]);
for round in (1..nr).rev() {
inv_shift_rows(&mut state);
inv_sub_bytes(&mut state);
add_round_key(&mut state, &round_keys[round * 16..(round + 1) * 16]);
inv_mix_columns(&mut state);
}
inv_shift_rows(&mut state);
inv_sub_bytes(&mut state);
add_round_key(&mut state, &round_keys[0..16]);
state_to_block(&state)
}
fn gcm_inc32(counter: &mut [u8; 16]) {
let count = u32::from_be_bytes([counter[12], counter[13], counter[14], counter[15]]);
let incremented = count.wrapping_add(1).to_be_bytes();
counter[12] = incremented[0];
counter[13] = incremented[1];
counter[14] = incremented[2];
counter[15] = incremented[3];
}
fn gcm_ctr_crypt(plaintext: &[u8], round_keys: &[u8], nr: usize, j0: &[u8; 16]) -> Vec<u8> {
let mut counter = *j0;
gcm_inc32(&mut counter);
let mut output = Vec::with_capacity(plaintext.len());
let mut offset = 0usize;
while offset < plaintext.len() {
let keystream_block = aes_encrypt_block(&counter, round_keys, nr);
let remaining = plaintext.len() - offset;
let chunk_len = remaining.min(16);
for i in 0..chunk_len {
output.push(plaintext[offset + i] ^ keystream_block[i]);
}
gcm_inc32(&mut counter);
offset += chunk_len;
}
output
}
fn gf128_mul(x: &[u8; 16], y: &[u8; 16]) -> [u8; 16] {
let mut z = [0u8; 16];
let mut v = *x;
for &y_byte in y.iter() {
for bit in (0..8).rev() {
if (y_byte >> bit) & 1 == 1 {
for j in 0..16 {
z[j] ^= v[j];
}
}
let lsb = v[15] & 1;
for j in (1..16).rev() {
v[j] = (v[j] >> 1) | (v[j - 1] << 7);
}
v[0] >>= 1;
if lsb == 1 {
v[0] ^= 0xe1;
}
}
}
z
}
fn ghash(h: &[u8; 16], data: &[u8]) -> [u8; 16] {
let mut y = [0u8; 16];
for chunk in data.chunks(16) {
let mut block = [0u8; 16];
block[..chunk.len()].copy_from_slice(chunk);
for i in 0..16 {
y[i] ^= block[i];
}
y = gf128_mul(&y, h);
}
y
}
fn gcm_ghash_input(aad: &[u8], ciphertext: &[u8]) -> Vec<u8> {
let mut data = Vec::new();
data.extend_from_slice(aad);
let aad_pad = (16 - aad.len() % 16) % 16;
data.extend_from_slice(&vec![0u8; aad_pad]);
data.extend_from_slice(ciphertext);
let ct_pad = (16 - ciphertext.len() % 16) % 16;
data.extend_from_slice(&vec![0u8; ct_pad]);
data.extend_from_slice(&((aad.len() as u64 * 8).to_be_bytes()));
data.extend_from_slice(&((ciphertext.len() as u64 * 8).to_be_bytes()));
data
}
fn aes_gcm_seal(
key: &[u8],
nonce: &[u8; 12],
plaintext: &[u8],
aad: &[u8],
) -> Result<Vec<u8>, String> {
let (round_keys, nr) = aes_key_expansion(key)?;
let h_block: [u8; 16] = aes_encrypt_block(&[0u8; 16], &round_keys, nr);
let mut j0 = [0u8; 16];
j0[..12].copy_from_slice(nonce);
j0[12..16].copy_from_slice(&1u32.to_be_bytes());
let ciphertext = gcm_ctr_crypt(plaintext, &round_keys, nr, &j0);
let ghash_input = gcm_ghash_input(aad, &ciphertext);
let s = ghash(&h_block, &ghash_input);
let ej0 = aes_encrypt_block(&j0, &round_keys, nr);
let mut tag = [0u8; 16];
for i in 0..16 {
tag[i] = s[i] ^ ej0[i];
}
let mut output = ciphertext;
output.extend_from_slice(&tag);
Ok(output)
}
fn aes_gcm_open(
key: &[u8],
nonce: &[u8; 12],
ciphertext_with_tag: &[u8],
aad: &[u8],
) -> Result<Vec<u8>, String> {
if ciphertext_with_tag.len() < 16 {
return Err("aes-gcm: ciphertext_with_tag too short".to_string());
}
let (round_keys, nr) = aes_key_expansion(key)?;
let ct_len = ciphertext_with_tag.len() - 16;
let ciphertext = &ciphertext_with_tag[..ct_len];
let received_tag = &ciphertext_with_tag[ct_len..];
let h_block: [u8; 16] = aes_encrypt_block(&[0u8; 16], &round_keys, nr);
let mut j0 = [0u8; 16];
j0[..12].copy_from_slice(nonce);
j0[12..16].copy_from_slice(&1u32.to_be_bytes());
let ghash_input = gcm_ghash_input(aad, ciphertext);
let s = ghash(&h_block, &ghash_input);
let ej0 = aes_encrypt_block(&j0, &round_keys, nr);
let mut expected_tag = [0u8; 16];
for i in 0..16 {
expected_tag[i] = s[i] ^ ej0[i];
}
let mut diff = 0u8;
for i in 0..16 {
diff |= received_tag[i] ^ expected_tag[i];
}
if diff != 0 {
return Err("aes-gcm: authentication tag mismatch".to_string());
}
Ok(gcm_ctr_crypt(ciphertext, &round_keys, nr, &j0))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AesKeyLen {
Aes128,
Aes256,
}
#[derive(Debug, Clone)]
pub struct AesGcmConfig {
pub key_len: AesKeyLen,
pub tag_len: usize,
}
impl Default for AesGcmConfig {
fn default() -> Self {
Self {
key_len: AesKeyLen::Aes256,
tag_len: 16,
}
}
}
#[derive(Debug, Clone)]
pub struct AesGcmCipher {
pub config: AesGcmConfig,
key: Vec<u8>,
}
impl AesGcmCipher {
pub fn new(key: &[u8], config: AesGcmConfig) -> Result<Self, String> {
if key.len() != 16 && key.len() != 32 {
return Err("aes-gcm: key must be 16 or 32 bytes".to_string());
}
Ok(Self {
config,
key: key.to_vec(),
})
}
pub fn key_len_bytes(&self) -> usize {
self.key.len()
}
}
pub fn aes_gcm_encrypt(key: &[u8], nonce: &[u8; 12], plaintext: &[u8]) -> Result<Vec<u8>, String> {
if !aes_key_len_valid(key.len()) {
return Err("aes-gcm: invalid key length".to_string());
}
let ct_and_tag = aes_gcm_seal(key, nonce, plaintext, &[])?;
let mut out = Vec::with_capacity(12 + ct_and_tag.len());
out.extend_from_slice(nonce);
out.extend_from_slice(&ct_and_tag);
Ok(out)
}
pub fn aes_gcm_decrypt(key: &[u8], packet: &[u8]) -> Result<Vec<u8>, String> {
if packet.len() < 28 {
return Err("aes-gcm: ciphertext too short".to_string());
}
if !aes_key_len_valid(key.len()) {
return Err("aes-gcm: invalid key length".to_string());
}
let nonce: [u8; 12] = [
packet[0], packet[1], packet[2], packet[3], packet[4], packet[5], packet[6], packet[7],
packet[8], packet[9], packet[10], packet[11],
];
let ct_with_tag = &packet[12..];
aes_gcm_open(key, &nonce, ct_with_tag, &[])
}
pub fn aes_gcm_encrypt_aad(
key: &[u8],
nonce: &[u8; 12],
plaintext: &[u8],
aad: &[u8],
) -> Result<Vec<u8>, String> {
if !aes_key_len_valid(key.len()) {
return Err("aes-gcm: invalid key length".to_string());
}
aes_gcm_seal(key, nonce, plaintext, aad)
}
pub fn aes_gcm_decrypt_aad(
key: &[u8],
nonce: &[u8; 12],
ciphertext_with_tag: &[u8],
aad: &[u8],
) -> Result<Vec<u8>, String> {
if !aes_key_len_valid(key.len()) {
return Err("aes-gcm: invalid key length".to_string());
}
aes_gcm_open(key, nonce, ciphertext_with_tag, aad)
}
pub fn aes_derive_key_stub(passphrase: &str) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(passphrase.as_bytes());
hasher.finalize().into()
}
pub fn aes_key_len_valid(len: usize) -> bool {
len == 16 || len == 32
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kat_aes_128_gcm() {
let key = [0u8; 16];
let nonce = [0u8; 12];
let plaintext = &[][..];
let expected_tag = hex::decode("58e2fccefa7e3061367f1d57a4e7455a").expect("tag hex");
let result = aes_gcm_encrypt_aad(&key, &nonce, plaintext, &[]).expect("encrypt failed");
assert_eq!(result.len(), 16, "expected only the 16-byte tag");
assert_eq!(result, expected_tag, "AES-128-GCM tag mismatch (empty PT)");
}
#[test]
fn kat_aes_128_gcm_16byte_pt() {
let key = [0u8; 16];
let nonce = [0u8; 12];
let plaintext = [0u8; 16];
let expected_ct = hex::decode("0388dace60b6a392f328c2b971b2fe78").expect("ct hex");
let expected_tag = hex::decode("ab6e47d42cec13bdf53a67b21257bddf").expect("tag hex");
let result = aes_gcm_encrypt_aad(&key, &nonce, &plaintext, &[]).expect("encrypt failed");
let ct = &result[..result.len() - 16];
let tag = &result[result.len() - 16..];
assert_eq!(ct, expected_ct.as_slice(), "AES-128-GCM CT mismatch");
assert_eq!(
tag,
expected_tag.as_slice(),
"AES-128-GCM tag mismatch (16-byte PT)"
);
}
#[test]
fn kat_aes_256_gcm() {
let key = [0u8; 32];
let nonce = [0u8; 12];
let expected_tag = hex::decode("530f8afbc74536b9a963b4f1c4cb738b").expect("tag hex");
let result = aes_gcm_encrypt_aad(&key, &nonce, &[], &[]).expect("encrypt failed");
assert_eq!(result.len(), 16, "expected only the 16-byte tag");
assert_eq!(result, expected_tag, "AES-256-GCM tag mismatch (empty PT)");
}
#[test]
fn kat_aes_128_block() {
let key = hex::decode("2b7e151628aed2a6abf7158809cf4f3c").expect("key hex");
let pt = hex::decode("3243f6a8885a308d313198a2e0370734").expect("pt hex");
let expected_ct = hex::decode("3925841d02dc09fbdc118597196a0b32").expect("ct hex");
let (round_keys, nr) = aes_key_expansion(&key).expect("key expansion");
let pt_block: [u8; 16] = pt.try_into().expect("pt len");
let ct = aes_encrypt_block(&pt_block, &round_keys, nr);
assert_eq!(
ct.as_slice(),
expected_ct.as_slice(),
"FIPS-197 block cipher mismatch"
);
}
#[test]
fn test_default_key_len() {
assert_eq!(AesGcmConfig::default().key_len, AesKeyLen::Aes256);
}
#[test]
fn test_key_len_valid() {
assert!(aes_key_len_valid(16));
assert!(aes_key_len_valid(32));
assert!(!aes_key_len_valid(24));
}
#[test]
fn test_cipher_bad_key() {
let result = AesGcmCipher::new(&[0u8; 24], AesGcmConfig::default());
assert!(result.is_err());
}
#[test]
fn test_cipher_good_key() {
let c = AesGcmCipher::new(&[0u8; 32], AesGcmConfig::default()).expect("should succeed");
assert_eq!(c.key_len_bytes(), 32);
}
#[test]
fn test_encrypt_then_decrypt_128() {
let key = [0x42u8; 16];
let nonce = [0x01u8; 12];
let plain = b"hello aes-gcm";
let enc = aes_gcm_encrypt(&key, &nonce, plain).expect("encrypt");
let dec = aes_gcm_decrypt(&key, &enc).expect("decrypt");
assert_eq!(dec, plain);
}
#[test]
fn test_encrypt_then_decrypt_256() {
let key = [0x42u8; 32];
let nonce = [0x01u8; 12];
let plain = b"hello aes-256-gcm";
let enc = aes_gcm_encrypt(&key, &nonce, plain).expect("encrypt");
let dec = aes_gcm_decrypt(&key, &enc).expect("decrypt");
assert_eq!(dec, plain);
}
#[test]
fn test_encrypt_bad_key() {
assert!(aes_gcm_encrypt(&[0u8; 24], &[0u8; 12], b"data").is_err());
}
#[test]
fn test_decrypt_short() {
assert!(aes_gcm_decrypt(&[0u8; 32], &[0u8; 5]).is_err());
}
#[test]
fn test_derive_key_stub() {
let key = aes_derive_key_stub("my-passphrase");
assert_eq!(key.len(), 32);
}
#[test]
fn test_encrypted_output_len() {
let key = [0u8; 32];
let nonce = [0u8; 12];
let enc = aes_gcm_encrypt(&key, &nonce, b"hello").expect("encrypt");
assert_eq!(enc.len(), 12 + 16 + 5);
}
#[test]
fn test_wrong_key_decrypt_fails() {
let k_enc = [0u8; 32];
let k_dec = [1u8; 32];
let nonce = [0u8; 12];
let enc = aes_gcm_encrypt(&k_enc, &nonce, b"secret").expect("enc");
assert!(aes_gcm_decrypt(&k_dec, &enc).is_err());
}
#[test]
fn test_aad_roundtrip() {
let key = [0xABu8; 32];
let nonce = [0x01u8; 12];
let plaintext = b"authenticate me";
let aad = b"header";
let enc = aes_gcm_encrypt_aad(&key, &nonce, plaintext, aad).expect("enc");
let dec = aes_gcm_decrypt_aad(&key, &nonce, &enc, aad).expect("dec");
assert_eq!(dec.as_slice(), plaintext.as_slice());
}
#[test]
fn test_aad_tamper_fails() {
let key = [0xABu8; 32];
let nonce = [0x01u8; 12];
let plaintext = b"authenticate me";
let aad = b"header";
let enc = aes_gcm_encrypt_aad(&key, &nonce, plaintext, aad).expect("enc");
assert!(aes_gcm_decrypt_aad(&key, &nonce, &enc, b"WRONG").is_err());
}
#[test]
fn test_ciphertext_tamper_fails() {
let key = [0xCDu8; 32];
let nonce = [0x07u8; 12];
let mut enc = aes_gcm_encrypt(&key, &nonce, b"tamper test").expect("enc");
let last = enc.len() - 1;
enc[last] ^= 0xff;
assert!(aes_gcm_decrypt(&key, &enc).is_err());
}
#[test]
fn test_roundtrip_empty_plaintext() {
let key = [0u8; 32];
let nonce = [0u8; 12];
let enc = aes_gcm_encrypt(&key, &nonce, &[]).expect("enc");
let dec = aes_gcm_decrypt(&key, &enc).expect("dec");
assert_eq!(dec, Vec::<u8>::new());
}
#[test]
fn test_gf128_mul_identity() {
let zero = [0u8; 16];
let x = [0xffu8; 16];
let result = gf128_mul(&x, &zero);
assert_eq!(result, zero, "x * 0 should be 0");
}
}