use crate::error::{AnubisError, Result};
use crate::traits::Aead as AeadTrait;
use aes_gcm_siv::aead::{Aead, KeyInit, Payload};
use aes_gcm_siv::{Aes256GcmSiv, Key, Nonce};
pub type Key32 = [u8; 32];
pub type Nonce96 = [u8; 12];
#[derive(Clone, Copy, Debug)]
pub struct Aes256GcmSivProvider;
impl AeadTrait for Aes256GcmSivProvider {
type Key = Key32;
type Nonce = Nonce96;
fn seal(
&self,
key: &Self::Key,
nonce: &Self::Nonce,
aad: &[u8],
plaintext: &[u8],
) -> Result<Vec<u8>> {
let cipher = Aes256GcmSiv::new(Key::<Aes256GcmSiv>::from_slice(key));
let n = Nonce::from_slice(nonce);
cipher
.encrypt(n, Payload { msg: plaintext, aad })
.map_err(|_| AnubisError::DecryptFailed)
}
fn open(
&self,
key: &Self::Key,
nonce: &Self::Nonce,
aad: &[u8],
ciphertext: &[u8],
) -> Result<Vec<u8>> {
let cipher = Aes256GcmSiv::new(Key::<Aes256GcmSiv>::from_slice(key));
let n = Nonce::from_slice(nonce);
cipher
.decrypt(n, Payload { msg: ciphertext, aad })
.map_err(|_| AnubisError::DecryptFailed)
}
}