use super::*;
use crate::error::Result;
#[test]
fn test_empty_inputs() -> Result<()> {
let h = [
0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b,
0x2e,
];
let empty: [u8; 0] = [];
let result = process_ghash(&h, &empty, &empty)?;
let expected = [0u8; 16];
assert_eq!(result, expected);
Ok(())
}
#[test]
fn test_gf_multiply_commutative() {
let x = [
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
0x10,
];
let y = [
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
0x00,
];
let result1 = GHash::gf_multiply(&x, &y);
let result2 = GHash::gf_multiply(&y, &x);
assert_eq!(result1, result2);
}
#[test]
fn test_gf_multiply_zero() {
let x = [
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
0x10,
];
let zero = [0u8; 16];
let result = GHash::gf_multiply(&x, &zero);
assert_eq!(result, zero);
}
#[test]
fn test_ghash_internal_consistency() -> Result<()> {
let h = [
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
0x10,
];
let data1 = [0xaa; 32];
let data2 = [0xbb; 16];
let result1 = process_ghash(&h, &data1, &data2)?;
let result2 = process_ghash(&h, &data1, &data2)?;
assert_eq!(result1, result2);
Ok(())
}
#[test]
fn test_ghash_length_block() -> Result<()> {
let h = [
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
0x10,
];
let data_a = [0xaa; 32]; let data_b = [0xbb; 16];
let result1 = process_ghash(&h, &data_a, &data_b)?;
let result2 = process_ghash(&h, &data_b, &data_a)?;
assert_ne!(result1, result2);
Ok(())
}
#[test]
fn test_ghash_unaligned() -> Result<()> {
let h = [
0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b,
0x2e,
];
let aad = [
0x4d, 0xcf, 0x79, 0x36, 0x36, 0xf7, 0xd2, 0xc4, 0x50, 0xfa, 0x37,
];
let ct = [0x48, 0xaf, 0x2e, 0x8c, 0x4a, 0x89, 0x3d, 0xda, 0x59, 0x8];
let aad_len = aad.len();
let ct_len = ct.len();
let mut ghash_instance = GHash::new(&h);
ghash_instance.update_block(&aad, aad_len)?;
ghash_instance.update_block(&ct, ct_len)?;
ghash_instance.update_lengths(aad_len as u64, ct_len as u64)?;
let manual_result = ghash_instance.finalize();
let helper_result = process_ghash(&h, &aad, &ct)?;
assert_eq!(manual_result, helper_result);
Ok(())
}