-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaead.rs
More file actions
270 lines (236 loc) · 9.11 KB
/
Copy pathaead.rs
File metadata and controls
270 lines (236 loc) · 9.11 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/)
// Copyright 2026 Datadog, Inc.
use rustls::crypto::cipher::NONCE_LEN;
use rustls::Error;
use windows::core::Owned;
use windows::Win32::Security::Cryptography::{
BCryptDecrypt, BCryptEncrypt, BCryptGenerateSymmetricKey, BCRYPT_AES_GCM_ALG_HANDLE,
BCRYPT_ALG_HANDLE, BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO,
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO_VERSION, BCRYPT_CHACHA20_POLY1305_ALG_HANDLE,
BCRYPT_FLAGS, BCRYPT_KEY_HANDLE,
};
/// The tag length is 16 bytes for all supported ciphers.
pub(crate) const TAG_LEN: usize = 16;
#[derive(Debug, Clone, Copy)]
pub(crate) struct Algorithm {
handle: BCRYPT_ALG_HANDLE,
key_size: usize,
is_aes: bool,
}
pub(crate) const AES_128_GCM: Algorithm = Algorithm {
handle: BCRYPT_AES_GCM_ALG_HANDLE,
key_size: 16,
is_aes: true,
};
pub(crate) const AES_256_GCM: Algorithm = Algorithm {
handle: BCRYPT_AES_GCM_ALG_HANDLE,
key_size: 32,
is_aes: true,
};
pub(crate) const CHACHA20_POLY1305: Algorithm = Algorithm {
handle: BCRYPT_CHACHA20_POLY1305_ALG_HANDLE,
key_size: 32,
is_aes: false,
};
unsafe impl Send for Algorithm {}
unsafe impl Sync for Algorithm {}
pub(crate) struct AeadKey {
handle: Owned<BCRYPT_KEY_HANDLE>,
}
unsafe impl Send for AeadKey {}
unsafe impl Sync for AeadKey {}
impl Algorithm {
pub(crate) fn key_size(&self) -> usize {
self.key_size
}
pub(crate) fn is_aes(&self) -> bool {
self.is_aes
}
pub(crate) fn with_key(&self, key: &[u8]) -> Result<AeadKey, Error> {
if key.len() != self.key_size {
return Err(Error::General(format!(
"Invalid key size for AEAD algorithm: {}",
key.len()
)));
}
let mut key_handle = Owned::default();
unsafe {
BCryptGenerateSymmetricKey(self.handle, &mut *key_handle, None, key, 0)
.ok()
.map_err(|e| Error::General(format!("AEAD key import error: {e}")))?;
// if self.is_aes {
// let bcrypt_handle = BCRYPT_HANDLE(&mut *key_handle.0);
// BCryptSetProperty(
// bcrypt_handle,
// BCRYPT_CHAINING_MODE,
// &to_null_terminated_le_bytes(BCRYPT_CHAIN_MODE_GCM),
// 0,
// )
// .ok()
// .map_err(|e| Error::General(format!("AEAD set chaining mode error: {e}")))?;
// }
}
Ok(AeadKey { handle: key_handle })
}
}
impl AeadKey {
/// Encrypts data in place and returns the tag.
pub(crate) fn seal(
&self,
nonce: [u8; NONCE_LEN], // Take ownership of nonce as it is modified in place.
aad: &[u8],
data: &mut [u8],
) -> Result<[u8; TAG_LEN], Error> {
let mut tag = [0u8; TAG_LEN];
// https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/ns-bcrypt-bcrypt_authenticated_cipher_mode_info
let info = BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO {
cbSize: core::mem::size_of::<BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO>() as u32,
dwInfoVersion: BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO_VERSION,
pbNonce: nonce.as_ptr().cast_mut(),
cbNonce: nonce.len() as u32,
pbTag: tag.as_mut_ptr(),
cbTag: tag.len() as u32,
pbAuthData: aad.as_ptr().cast_mut(),
cbAuthData: aad.len() as u32,
..Default::default()
};
unsafe {
// SAFETY: CNG supports in-place encryption, so the input and output buffers can be the same.
let mut size = 0u32;
let input = std::slice::from_raw_parts(data.as_ptr().cast(), data.len());
BCryptEncrypt(
*self.handle,
Some(input),
Some(std::ptr::from_ref(&info) as *mut _),
None,
Some(data),
&mut size,
BCRYPT_FLAGS::default(),
)
.ok()
.map_err(|_| Error::EncryptError)?;
}
Ok(tag)
}
/// Decrypts in place, verifying the tag and returns the length of the plaintext.
pub(crate) fn open(
&self,
nonce: [u8; NONCE_LEN], // Take ownership of nonce as it is modified in place.
aad: &[u8],
data: &mut [u8],
) -> Result<usize, Error> {
let payload_len = data.len();
if payload_len < TAG_LEN {
return Err(Error::DecryptError);
}
let (ciphertext, tag) = data.split_at_mut(payload_len - TAG_LEN);
// https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/ns-bcrypt-bcrypt_authenticated_cipher_mode_info
let info = BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO {
cbSize: core::mem::size_of::<BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO>() as u32,
dwInfoVersion: BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO_VERSION,
pbNonce: nonce.as_ptr().cast_mut(),
cbNonce: nonce.len() as u32,
pbTag: tag.as_mut_ptr(),
cbTag: tag.len() as u32,
pbAuthData: aad.as_ptr().cast_mut(),
cbAuthData: aad.len() as u32,
..Default::default()
};
let mut size = 0u32;
unsafe {
// SAFETY: CNG supports in-place decryption, so the input and output buffers can be the same.
let input = std::slice::from_raw_parts(ciphertext.as_ptr().cast(), ciphertext.len());
BCryptDecrypt(
*self.handle,
Some(input),
Some(std::ptr::from_ref(&info) as *mut _),
None,
Some(ciphertext),
&mut size,
BCRYPT_FLAGS::default(),
)
.ok()
.map_err(|_| Error::DecryptError)?;
}
size.try_into().map_err(|_| Error::DecryptError)
}
}
#[cfg(test)]
mod test {
use crate::aead::{Algorithm, AES_128_GCM, AES_256_GCM, CHACHA20_POLY1305};
use rustls::Error;
use wycheproof::{
aead::{TestFlag, TestName},
TestResult,
};
#[rstest::rstest]
#[case::aes128gcm(AES_128_GCM, wycheproof::aead::TestName::AesGcm)]
#[case::aes256gcm(AES_256_GCM, wycheproof::aead::TestName::AesGcm)]
#[case::chacha20poly1305(CHACHA20_POLY1305, wycheproof::aead::TestName::ChaCha20Poly1305)]
fn roundtrip(#[case] alg: Algorithm, #[case] test_name: TestName) {
let test_set = wycheproof::aead::TestSet::load(test_name).unwrap();
let mut counter = 0;
for group in test_set
.test_groups
.into_iter()
.filter(|group| group.key_size == 8 * alg.key_size)
.filter(|group| group.nonce_size == 96)
{
for test in group.tests {
counter += 1;
let mut iv_bytes = [0u8; 12];
iv_bytes.copy_from_slice(&test.nonce[0..12]);
let mut actual_ciphertext = test.pt.to_vec();
let key = alg.with_key(&test.key).unwrap();
let actual_tag = key
.seal(iv_bytes, &test.aad, &mut actual_ciphertext)
.unwrap();
match &test.result {
TestResult::Invalid => {
if test.flags.contains(&TestFlag::ModifiedTag) {
assert_ne!(
actual_tag[..],
test.tag[..],
"Expected incorrect tag. Id {}: {}",
test.tc_id,
test.comment
);
}
}
TestResult::Valid | TestResult::Acceptable => {
assert_eq!(
actual_ciphertext[..],
test.ct[..],
"Incorrect ciphertext on testcase {}: {}",
test.tc_id,
test.comment
);
assert_eq!(
actual_tag[..],
test.tag[..],
"Incorrect tag on testcase {}: {}",
test.tc_id,
test.comment
);
}
}
let mut data = test.ct.to_vec();
data.extend_from_slice(&test.tag);
let res = key.open(iv_bytes, &test.aad, &mut data);
match &test.result {
TestResult::Invalid => {
assert_eq!(res, Err(Error::DecryptError));
}
TestResult::Valid | TestResult::Acceptable => {
assert_eq!(res, Ok(test.pt.len()));
assert_eq!(&data[..res.unwrap()], &test.pt[..]);
}
}
}
}
// Ensure we ran some tests.
assert!(counter > 50);
}
}