use crate::{EncryptedMessage, Nonce, tags, Digest};
use bc_crypto::{aead_chacha20_poly1305_encrypt_with_aad, aead_chacha20_poly1305_decrypt_with_aad};
use bc_ur::{UREncodable, URDecodable, URCodable};
use dcbor::{CBORTagged, Tag, CBORTaggedEncodable, CBOR, CBOREncodable, CBORDecodable, CBORTaggedDecodable};
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SymmetricKey([u8; Self::SYMMETRIC_KEY_SIZE]);
impl SymmetricKey {
pub const SYMMETRIC_KEY_SIZE: usize = 32;
pub fn new() -> Self {
let mut rng = bc_crypto::SecureRandomNumberGenerator;
Self::new_using(&mut rng)
}
pub fn new_using(rng: &mut impl bc_crypto::RandomNumberGenerator) -> Self {
let mut key = [0u8; Self::SYMMETRIC_KEY_SIZE];
rng.fill_random_data(&mut key);
Self::from_data(key)
}
pub const fn from_data(data: [u8; Self::SYMMETRIC_KEY_SIZE]) -> Self {
Self(data)
}
pub fn from_data_ref<T>(data: &T) -> Option<Self> where T: AsRef<[u8]> {
let data = data.as_ref();
if data.len() != Self::SYMMETRIC_KEY_SIZE {
return None;
}
let mut arr = [0u8; Self::SYMMETRIC_KEY_SIZE];
arr.copy_from_slice(data);
Some(Self::from_data(arr))
}
pub fn data(&self) -> &[u8; Self::SYMMETRIC_KEY_SIZE] {
self.into()
}
pub fn from_hex<T>(hex: T) -> Self where T: AsRef<str> {
Self::from_data_ref(&hex::decode(hex.as_ref()).unwrap()).unwrap()
}
pub fn hex(&self) -> String {
hex::encode(self.data())
}
pub fn encrypt<D, N>(&self, plaintext: D, aad: Option<&[u8]>, nonce: Option<N>) -> EncryptedMessage
where
D: AsRef<[u8]>,
N: AsRef<Nonce>
{
let aad = aad.unwrap_or(&[]).into();
let nonce: Nonce = nonce.map(|n| n.as_ref().clone()).unwrap_or_else(Nonce::new);
let (ciphertext, auth) = aead_chacha20_poly1305_encrypt_with_aad(plaintext, self.into(), (&nonce).into(), &aad);
EncryptedMessage::new(ciphertext, aad, nonce, auth.into())
}
pub fn encrypt_with_digest<D, N>(&self, plaintext: D, digest: &Digest, nonce: Option<N>) -> EncryptedMessage
where
D: AsRef<[u8]>,
N: AsRef<Nonce>,
{
self.encrypt(plaintext, Some(&digest.cbor_data()), nonce)
}
pub fn decrypt(&self, message: &EncryptedMessage) -> Result<Vec<u8>, bc_crypto::Error> {
aead_chacha20_poly1305_decrypt_with_aad(message.ciphertext(), self.into(), message.nonce().into(), message.aad(), message.authentication_tag().into())
}
}
impl Default for SymmetricKey {
fn default() -> Self {
Self::new()
}
}
impl<'a> From<&'a SymmetricKey> for &'a [u8; SymmetricKey::SYMMETRIC_KEY_SIZE] {
fn from(digest: &'a SymmetricKey) -> Self {
&digest.0
}
}
impl From<&SymmetricKey> for SymmetricKey {
fn from(digest: &SymmetricKey) -> Self {
digest.clone()
}
}
impl From<SymmetricKey> for Vec<u8> {
fn from(digest: SymmetricKey) -> Self {
digest.0.to_vec()
}
}
impl From<&SymmetricKey> for Vec<u8> {
fn from(digest: &SymmetricKey) -> Self {
digest.0.to_vec()
}
}
impl std::fmt::Debug for SymmetricKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SymmetricKey({})", self.hex())
}
}
impl CBORTagged for SymmetricKey {
const CBOR_TAG: Tag = tags::SYMMETRIC_KEY;
}
impl CBOREncodable for SymmetricKey {
fn cbor(&self) -> CBOR {
self.tagged_cbor()
}
}
impl CBORTaggedEncodable for SymmetricKey {
fn untagged_cbor(&self) -> CBOR {
CBOR::byte_string(self.0)
}
}
impl UREncodable for SymmetricKey { }
impl CBORDecodable for SymmetricKey {
fn from_cbor(cbor: &CBOR) -> Result<Self, dcbor::Error> {
Self::from_untagged_cbor(cbor)
}
}
impl CBORTaggedDecodable for SymmetricKey {
fn from_untagged_cbor(cbor: &CBOR) -> Result<Self, dcbor::Error> {
let bytes = CBOR::expect_byte_string(cbor)?;
let instance = Self::from_data_ref(&bytes).ok_or(dcbor::Error::InvalidFormat)?;
Ok(instance)
}
}
impl URDecodable for SymmetricKey { }
impl URCodable for SymmetricKey { }