1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::{EncryptedMessage, Nonce, tags, Digest};
use bc_crypto::{aead_chacha20_poly1305_encrypt_with_aad, aead_chacha20_poly1305_decrypt_with_aad};
use bc_ur::prelude::*;
use anyhow::bail;
use bytes::Bytes;

/// A symmetric encryption key.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SymmetricKey([u8; Self::SYMMETRIC_KEY_SIZE]);

impl SymmetricKey {
    pub const SYMMETRIC_KEY_SIZE: usize = 32;

    /// Create a new random symmetric key.
    pub fn new() -> Self {
        let mut rng = bc_rand::SecureRandomNumberGenerator;
        Self::new_using(&mut rng)
    }

    /// Create a new random symmetric key using the given random number generator.
    pub fn new_using(rng: &mut impl bc_rand::RandomNumberGenerator) -> Self {
        let mut key = [0u8; Self::SYMMETRIC_KEY_SIZE];
        rng.fill_random_data(&mut key);
        Self::from_data(key)
    }

    /// Create a new symmetric key from data.
    pub const fn from_data(data: [u8; Self::SYMMETRIC_KEY_SIZE]) -> Self {
        Self(data)
    }

    /// Create a new symmetric key from data.
    pub fn from_data_ref(data: impl AsRef<[u8]>) -> anyhow::Result<Self> {
        let data = data.as_ref();
        if data.len() != Self::SYMMETRIC_KEY_SIZE {
            bail!("Invalid symmetric key size");
        }
        let mut arr = [0u8; Self::SYMMETRIC_KEY_SIZE];
        arr.copy_from_slice(data);
        Ok(Self::from_data(arr))
    }

    /// Get the data of the symmetric key.
    pub fn data(&self) -> &[u8; Self::SYMMETRIC_KEY_SIZE] {
        self.into()
    }

    /// Create a new symmetric key from the given hexadecimal string.
    ///
    /// # Panics
    /// Panics if the string is not exactly 24 hexadecimal digits.
    pub fn from_hex(hex: impl AsRef<str>) -> anyhow::Result<Self> {
        Self::from_data_ref(hex::decode(hex.as_ref()).unwrap())
    }

    /// The data as a hexadecimal string.
    pub fn hex(&self) -> String {
        hex::encode(self.data())
    }

    /// Encrypt the given plaintext with this key, and the given additional authenticated data and nonce.
    pub fn encrypt(&self, plaintext: impl Into<Bytes>, aad: Option<impl Into<Bytes>>, nonce: Option<impl AsRef<Nonce>>) -> EncryptedMessage
    {
        let aad: Bytes = aad.map(|a| a.into()).unwrap_or_default();
        let nonce: Nonce = nonce.map(|n| n.as_ref().clone()).unwrap_or_default();
        let plaintext = plaintext.into();
        let (ciphertext, auth) = aead_chacha20_poly1305_encrypt_with_aad(plaintext, self.into(), (&nonce).into(), &aad);
        EncryptedMessage::new(Bytes::from(ciphertext), aad, nonce, auth.into())
    }

    /// Encrypt the given plaintext with this key, and the given digest of the plaintext, and nonce.
    pub fn encrypt_with_digest(&self, plaintext: impl Into<Bytes>, digest: impl AsRef<Digest>, nonce: Option<impl AsRef<Nonce>>) -> EncryptedMessage
    {
        self.encrypt(plaintext, Some(Bytes::from(digest.as_ref().cbor_data())), nonce)
    }

    /// Decrypt the given encrypted message with this key.
    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 AsRef<SymmetricKey> for SymmetricKey {
    fn as_ref(&self) -> &SymmetricKey {
        self
    }
}

impl<'a> From<&'a SymmetricKey> for &'a [u8; SymmetricKey::SYMMETRIC_KEY_SIZE] {
    fn from(digest: &'a SymmetricKey) -> Self {
        &digest.0
    }
}

// Convert from a reference to a byte vector to an instance.
impl From<&SymmetricKey> for SymmetricKey {
    fn from(digest: &SymmetricKey) -> Self {
        digest.clone()
    }
}

// Convert from a byte vector to an instance.
impl From<SymmetricKey> for Vec<u8> {
    fn from(digest: SymmetricKey) -> Self {
        digest.0.to_vec()
    }
}

// Convert a reference to an instance to a byte vector.
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 From<SymmetricKey> for CBOR {
    fn from(value: SymmetricKey) -> Self {
        value.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) -> anyhow::Result<Self> {
        Self::from_untagged_cbor(cbor)
    }
}

impl TryFrom<CBOR> for SymmetricKey {
    type Error = anyhow::Error;

    fn try_from(cbor: CBOR) -> Result<Self, Self::Error> {
        Self::from_cbor(&cbor)
    }
}

impl TryFrom<&CBOR> for SymmetricKey {
    type Error = anyhow::Error;

    fn try_from(cbor: &CBOR) -> Result<Self, Self::Error> {
        Self::from_cbor(cbor)
    }
}

impl CBORTaggedDecodable for SymmetricKey {
    fn from_untagged_cbor(cbor: &CBOR) -> anyhow::Result<Self> {
        let bytes = CBOR::expect_byte_string(cbor)?;
        let instance = Self::from_data_ref(&bytes)?;
        Ok(instance)
    }
}

impl URDecodable for SymmetricKey { }

impl URCodable for SymmetricKey { }