use chacha20poly1305::aead::{Aead, Payload};
use chacha20poly1305::{KeyInit, XChaCha20Poly1305, XNonce};
use nibkeep_base::cbor::{self, FromCbor, ToCbor, Value};
use rand_core::CryptoRng;
use zeroize::Zeroizing;
use crate::format::{self, TAG_LEN};
use crate::{CryptoError, Key};
const NONCE_LEN: usize = 24;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Envelope {
nonce: [u8; NONCE_LEN],
ciphertext: Vec<u8>,
}
impl Envelope {
#[must_use]
pub fn seal(
key: &Key,
plaintext: &[u8],
associated_data: &[u8],
rng: &mut impl CryptoRng,
) -> Self {
let mut nonce = [0; NONCE_LEN];
rng.fill_bytes(&mut nonce);
let payload = Payload {
msg: plaintext,
aad: associated_data,
};
let ciphertext = cipher(key)
.encrypt(&XNonce::from(nonce), payload)
.expect("the plaintext fits in an envelope");
Self { nonce, ciphertext }
}
pub fn open(
&self,
key: &Key,
associated_data: &[u8],
) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
let payload = Payload {
msg: &self.ciphertext,
aad: associated_data,
};
cipher(key)
.decrypt(&XNonce::from(self.nonce), payload)
.map(Zeroizing::new)
.map_err(|_| CryptoError::Decryption)
}
#[must_use]
pub fn seal_key(
wrapping_key: &Key,
key: &Key,
associated_data: &[u8],
rng: &mut impl CryptoRng,
) -> Self {
Self::seal(wrapping_key, key.expose_bytes(), associated_data, rng)
}
pub fn open_key(&self, wrapping_key: &Key, associated_data: &[u8]) -> Result<Key, CryptoError> {
key_from_plaintext(&self.open(wrapping_key, associated_data)?)
}
}
impl Envelope {
#[must_use]
pub fn byte_len(&self) -> usize {
NONCE_LEN + self.ciphertext.len()
}
}
impl ToCbor for Envelope {
fn to_cbor(&self) -> Value {
format::write_bytes(&[&self.nonce, &self.ciphertext])
}
}
impl FromCbor for Envelope {
fn from_cbor(value: &Value) -> Result<Self, cbor::Error> {
const EXPECTED: &str = "an envelope: [1, nonce, ciphertext]";
let [nonce, ciphertext] = format::read_bytes(value, EXPECTED)?;
let nonce = nonce
.try_into()
.map_err(|_| cbor::Error::Expected(EXPECTED))?;
if ciphertext.len() < TAG_LEN {
return Err(cbor::Error::Expected(EXPECTED));
}
Ok(Self {
nonce,
ciphertext: ciphertext.to_vec(),
})
}
}
pub(crate) fn key_from_plaintext(plaintext: &[u8]) -> Result<Key, CryptoError> {
let plaintext: &[u8; 32] = plaintext.try_into().map_err(|_| CryptoError::Decryption)?;
Key::fill_with(|bytes| {
bytes.copy_from_slice(plaintext);
Ok(())
})
}
fn cipher(key: &Key) -> XChaCha20Poly1305 {
XChaCha20Poly1305::new(key.expose_bytes().into())
}