use aes_gcm::{
Aes256Gcm, Nonce,
aead::{Aead, KeyInit},
};
use zeroize::{Zeroize, ZeroizeOnDrop};
use crate::random;
#[derive(Debug, thiserror::Error)]
pub enum AesError {
#[error("encryption failed: {0}")]
Encrypt(String),
#[error("decryption failed: {0}")]
Decrypt(String),
#[error("invalid key length: expected 32, got {0}")]
InvalidKeyLength(usize),
#[error("invalid nonce length: expected 12, got {0}")]
InvalidNonceLength(usize),
}
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct AesGcmEncryptor {
key: [u8; 32],
}
impl AesGcmEncryptor {
pub fn new(key: [u8; 32]) -> Result<Self, AesError> {
Ok(Self { key })
}
pub fn generate() -> Result<Self, AesError> {
let mut key = [0u8; 32];
random::secure_random_bytes(&mut key);
Ok(Self { key })
}
pub fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>, AesError> {
let cipher =
Aes256Gcm::new_from_slice(&self.key).map_err(|e| AesError::Encrypt(e.to_string()))?;
let mut nonce_bytes = [0u8; 12];
random::secure_random_bytes(&mut nonce_bytes);
let nonce = Nonce::from_slice(&nonce_bytes);
let ciphertext = cipher
.encrypt(nonce, plaintext)
.map_err(|e| AesError::Encrypt(e.to_string()))?;
let mut output = Vec::with_capacity(12 + ciphertext.len());
output.extend_from_slice(&nonce_bytes);
output.extend_from_slice(&ciphertext);
Ok(output)
}
pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>, AesError> {
if data.len() < 12 {
return Err(AesError::Decrypt("data too short".into()));
}
let (nonce_bytes, ciphertext) = data.split_at(12);
let nonce = Nonce::from_slice(nonce_bytes);
let cipher =
Aes256Gcm::new_from_slice(&self.key).map_err(|e| AesError::Decrypt(e.to_string()))?;
cipher
.decrypt(nonce, ciphertext)
.map_err(|e| AesError::Decrypt(e.to_string()))
}
pub fn key_bytes(&self) -> &[u8; 32] {
&self.key
}
}
#[allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic
)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip() {
let enc = AesGcmEncryptor::generate().unwrap();
let plaintext = b"hello world, this is a test message!";
let ciphertext = enc.encrypt(plaintext).unwrap();
let decrypted = enc.decrypt(&ciphertext).unwrap();
assert_eq!(decrypted, plaintext);
}
#[test]
fn wrong_key_fails() {
let enc1 = AesGcmEncryptor::generate().unwrap();
let enc2 = AesGcmEncryptor::generate().unwrap();
let ciphertext = enc1.encrypt(b"secret").unwrap();
assert!(enc2.decrypt(&ciphertext).is_err());
}
#[test]
fn tampered_ciphertext_fails() {
let enc = AesGcmEncryptor::generate().unwrap();
let mut ciphertext = enc.encrypt(b"secret").unwrap();
if let Some(last) = ciphertext.last_mut() {
*last ^= 0xff;
}
assert!(enc.decrypt(&ciphertext).is_err());
}
#[test]
fn empty_plaintext() {
let enc = AesGcmEncryptor::generate().unwrap();
let ciphertext = enc.encrypt(b"").unwrap();
let decrypted = enc.decrypt(&ciphertext).unwrap();
assert_eq!(decrypted, b"");
}
#[test]
fn encryptor_is_zeroize_on_drop() {
fn assert_zeroize_on_drop<T: zeroize::ZeroizeOnDrop>() {}
assert_zeroize_on_drop::<AesGcmEncryptor>();
}
#[test]
fn decrypt_rejects_truncated_input() {
let enc = AesGcmEncryptor::generate().unwrap();
for n in 0..12 {
let data = vec![0u8; n];
assert!(enc.decrypt(&data).is_err(), "length {n} must be Err");
}
}
#[test]
fn key_bytes_round_trip() {
let mut key = [0u8; 32];
random::secure_random_bytes(&mut key);
let enc = AesGcmEncryptor::new(key).unwrap();
assert_eq!(enc.key_bytes(), &key);
}
#[test]
fn shared_handle_concurrent_roundtrip() {
use std::sync::Arc;
use std::thread;
let enc = Arc::new(AesGcmEncryptor::generate().unwrap());
let mut handles = Vec::new();
for i in 0..8 {
let enc = Arc::clone(&enc);
handles.push(thread::spawn(move || {
let plaintext = format!("thread-{i}-payload");
let ct = enc.encrypt(plaintext.as_bytes()).unwrap();
let pt = enc.decrypt(&ct).unwrap();
assert_eq!(pt, plaintext.as_bytes());
}));
}
for h in handles {
h.join().unwrap();
}
}
}