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
use alloc::boxed::Box;
use core::fmt;
use aead::{Aead, AeadCore, Payload};
use chacha20poly1305::{Key, KeyInit, KeySizeUser, XChaCha20Poly1305, XNonce};
use generic_array::{ArrayLength, GenericArray};
use hkdf::Hkdf;
use rand_core::{CryptoRng, RngCore};
use sha2::Sha256;
use typenum::Unsigned;
use zeroize::ZeroizeOnDrop;
use crate::secret_box::SecretBox;
#[derive(Debug, PartialEq, Eq)]
pub enum EncryptionError {
PlaintextTooLarge,
}
impl fmt::Display for EncryptionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PlaintextTooLarge => write!(f, "Plaintext is too large to encrypt"),
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum DecryptionError {
CiphertextTooShort,
AuthenticationFailed,
}
impl fmt::Display for DecryptionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CiphertextTooShort => write!(f, "The ciphertext must include the nonce"),
Self::AuthenticationFailed => write!(
f,
"Decryption of ciphertext failed: \
either someone tampered with the ciphertext or \
you are using an incorrect decryption key."
),
}
}
}
pub(crate) fn kdf<S: ArrayLength<u8>>(
seed: &[u8],
salt: Option<&[u8]>,
info: Option<&[u8]>,
) -> SecretBox<GenericArray<u8, S>> {
let hk = Hkdf::<Sha256>::new(salt, seed);
let mut okm = SecretBox::new(GenericArray::<u8, S>::default());
let def_info = info.unwrap_or(&[]);
hk.expand(def_info, okm.as_mut_secret()).unwrap();
okm
}
type NonceSize = <XChaCha20Poly1305 as AeadCore>::NonceSize;
#[allow(clippy::upper_case_acronyms)]
#[derive(ZeroizeOnDrop)]
pub(crate) struct DEM {
cipher: XChaCha20Poly1305,
}
impl DEM {
pub fn new(key_seed: &[u8]) -> Self {
type KeySize = <XChaCha20Poly1305 as KeySizeUser>::KeySize;
let key_bytes = kdf::<KeySize>(key_seed, None, None);
let key = SecretBox::new(*Key::from_slice(key_bytes.as_secret()));
let cipher = XChaCha20Poly1305::new(key.as_secret());
Self { cipher }
}
pub fn encrypt(
&self,
rng: &mut (impl CryptoRng + RngCore),
data: &[u8],
authenticated_data: &[u8],
) -> Result<Box<[u8]>, EncryptionError> {
let mut nonce = GenericArray::<u8, NonceSize>::default();
rng.fill_bytes(&mut nonce);
let nonce = XNonce::from_slice(&nonce);
let payload = Payload {
msg: data,
aad: authenticated_data,
};
let mut result = nonce.to_vec();
let enc_data = self
.cipher
.encrypt(nonce, payload)
.or(Err(EncryptionError::PlaintextTooLarge))?;
result.extend(enc_data);
Ok(result.into_boxed_slice())
}
pub fn decrypt(
&self,
ciphertext: impl AsRef<[u8]>,
authenticated_data: &[u8],
) -> Result<Box<[u8]>, DecryptionError> {
let nonce_size = <NonceSize as Unsigned>::to_usize();
let buf_size = ciphertext.as_ref().len();
if buf_size < nonce_size {
return Err(DecryptionError::CiphertextTooShort);
}
let nonce = XNonce::from_slice(&ciphertext.as_ref()[..nonce_size]);
let payload = Payload {
msg: &ciphertext.as_ref()[nonce_size..],
aad: authenticated_data,
};
self.cipher
.decrypt(nonce, payload)
.map(|pt| pt.into_boxed_slice())
.or(Err(DecryptionError::AuthenticationFailed))
}
}
#[cfg(test)]
mod tests {
use typenum::U32;
use super::kdf;
use crate::curve::CurvePoint;
use crate::secret_box::SecretBox;
use crate::SerializableToArray;
#[test]
fn test_kdf() {
let p1 = CurvePoint::generator();
let salt = b"abcdefg";
let info = b"sdasdasd";
let seed = SecretBox::new(p1.to_array());
let key = kdf::<U32>(seed.as_secret(), Some(&salt[..]), Some(&info[..]));
let key_same = kdf::<U32>(seed.as_secret(), Some(&salt[..]), Some(&info[..]));
assert_eq!(key.as_secret(), key_same.as_secret());
let key_diff = kdf::<U32>(seed.as_secret(), None, Some(&info[..]));
assert_ne!(key.as_secret(), key_diff.as_secret());
}
}