use aes_gcm::aead::{Aead, KeyInit};
use aes_gcm::{Aes256Gcm, Key, Nonce};
pub fn decrypt(
key: &[u8; 32],
nonce: &[u8; 12],
ciphertext_and_tag: &[u8],
) -> Result<Vec<u8>, crate::error::CryptoError> {
let cipher = Aes256Gcm::new(&Key::<Aes256Gcm>::from(*key));
let nonce = Nonce::from(*nonce);
cipher
.decrypt(&nonce, ciphertext_and_tag)
.map_err(|_| crate::error::CryptoError::DecryptionFailed)
}
#[cfg(test)]
mod tests {
use super::*;
use rand::{rngs::OsRng, RngCore};
fn encrypt_for_test(key: &[u8; 32], plaintext: &[u8]) -> ([u8; 12], Vec<u8>) {
let cipher = Aes256Gcm::new(&Key::<Aes256Gcm>::from(*key));
let mut nonce_bytes = [0u8; 12];
OsRng.fill_bytes(&mut nonce_bytes);
let nonce = Nonce::from(nonce_bytes);
let ciphertext_and_tag = cipher.encrypt(&nonce, plaintext).unwrap();
(nonce_bytes, ciphertext_and_tag)
}
#[test]
fn round_trips_a_known_plaintext() {
let key = [0x42u8; 32];
let plaintext = b"license payload json";
let (nonce, ciphertext_and_tag) = encrypt_for_test(&key, plaintext);
let decrypted = decrypt(&key, &nonce, &ciphertext_and_tag).unwrap();
assert_eq!(decrypted, plaintext);
}
#[test]
fn rejects_tampered_ciphertext() {
let key = [0x42u8; 32];
let (nonce, mut ciphertext_and_tag) = encrypt_for_test(&key, b"payload");
let last = ciphertext_and_tag.len() - 1;
ciphertext_and_tag[last] ^= 0xff; assert!(decrypt(&key, &nonce, &ciphertext_and_tag).is_err());
}
#[test]
fn rejects_wrong_key() {
let key = [0x42u8; 32];
let wrong_key = [0x43u8; 32];
let (nonce, ciphertext_and_tag) = encrypt_for_test(&key, b"payload");
assert!(decrypt(&wrong_key, &nonce, &ciphertext_and_tag).is_err());
}
}