use super::{DataKeyWithTag, EncryptPayload};
use aes_gcm_siv::{aead::Payload, Nonce};
use recipher::key::{Iv, Key};
use uuid::Uuid;
pub struct EncryptionTarget<'e> {
payload: EncryptPayload<'e>,
key_with_tag: DataKeyWithTag,
aad: Vec<u8>,
keyset_id: Option<Uuid>,
}
impl<'e> EncryptionTarget<'e> {
pub(crate) fn new(
payload: EncryptPayload<'e>,
key: DataKeyWithTag,
keyset_id: Option<Uuid>,
) -> Self {
let descriptor_bytes = payload.descriptor.as_bytes();
let mut aad = Vec::with_capacity(descriptor_bytes.len() + key.tag.len());
aad.extend_from_slice(descriptor_bytes);
aad.extend_from_slice(&key.tag);
Self {
payload,
key_with_tag: key,
aad,
keyset_id,
}
}
pub(super) fn nonce(&self) -> &Nonce {
Nonce::from_slice(&self.key_with_tag.iv[..12])
}
pub(super) fn key(&self) -> &Key {
self.key_with_tag.key()
}
pub(super) fn into_meta(self) -> (Iv, Vec<u8>, String, Option<Uuid>) {
(
self.key_with_tag.iv,
self.key_with_tag.tag,
self.payload.descriptor.to_string(),
self.keyset_id,
)
}
}
impl<'e> From<&'e EncryptionTarget<'e>> for Payload<'e, 'e> {
fn from(target: &'e EncryptionTarget<'e>) -> Self {
Payload {
msg: target.payload.msg,
aad: &target.aad,
}
}
}