Skip to content

Commit 47111f1

Browse files
committed
wip
1 parent 93c0409 commit 47111f1

2 files changed

Lines changed: 74 additions & 63 deletions

File tree

rust/Cargo.lock.in

Lines changed: 50 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/src/quic/crypto.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@
1515
* 02110-1301, USA.
1616
*/
1717

18-
use aes::cipher::generic_array::GenericArray;
18+
use aes::cipher::{Block, BlockEncrypt, NewBlockCipher};
1919
use aes::Aes128;
20-
use aes::BlockEncrypt;
21-
use aes::NewBlockCipher;
22-
use aes_gcm::AeadInPlace;
23-
use aes_gcm::Aes128Gcm;
24-
use aes_gcm::NewAead;
20+
use aes_gcm::aead::{AeadCore, AeadInPlace, NewAead};
21+
use aes_gcm::{Aes128Gcm, Nonce, Tag};
2522
use hkdf::Hkdf;
2623
use sha2::Sha256;
2724

2825
pub const AES128_KEY_LEN: usize = 16;
2926
pub const AES128_TAG_LEN: usize = 16;
3027
pub const AES128_IV_LEN: usize = 12;
3128

29+
type PacketNonce = Nonce<<Aes128Gcm as AeadCore>::NonceSize>;
30+
3231
pub struct HeaderProtectionKey(Aes128);
3332

3433
impl HeaderProtectionKey {
@@ -41,16 +40,19 @@ impl HeaderProtectionKey {
4140
b"quic hp" as &[u8]
4241
};
4342
hkdf_expand_label(&hk, quichp, &mut secret, AES128_KEY_LEN as u16);
44-
return Self(Aes128::new(GenericArray::from_slice(&secret)));
43+
return Self(
44+
Aes128::new_from_slice(&secret).expect("invalid AES-128 key length"),
45+
);
4546
}
4647

4748
pub fn decrypt_in_place(
4849
&self, sample: &[u8], first: &mut u8, packet_number: &mut [u8],
4950
) -> Result<(), ()> {
50-
let mut mask = GenericArray::clone_from_slice(sample);
51+
let mut mask = Block::<Aes128>::default();
52+
mask.copy_from_slice(sample);
5153
self.0.encrypt_block(&mut mask);
52-
53-
let (first_mask, pn_mask) = mask.split_first().unwrap();
54+
let mask_slice: &[u8] = &mask;
55+
let (first_mask, pn_mask) = mask_slice.split_first().unwrap();
5456

5557
let bits = if (*first & 0x80) != 0 {
5658
0x0f // Long header: 4 bits masked
@@ -84,7 +86,8 @@ impl PacketKey {
8486
b"quic key" as &[u8]
8587
};
8688
hkdf_expand_label(&hk, quickey, &mut secret, AES128_KEY_LEN as u16);
87-
let key = Aes128Gcm::new(GenericArray::from_slice(&secret));
89+
let key =
90+
Aes128Gcm::new_from_slice(&secret).expect("invalid AES-GCM key length");
8891

8992
let mut r = PacketKey {
9093
key,
@@ -112,9 +115,17 @@ impl PacketKey {
112115
}
113116
let tag_pos = payload.len() - AES128_TAG_LEN;
114117
let (buffer, tag) = payload.split_at_mut(tag_pos);
115-
let taga = GenericArray::from_slice(tag);
118+
let mut taga = Tag::default();
119+
taga.copy_from_slice(tag);
120+
let mut nonce_block = PacketNonce::default();
121+
nonce_block.copy_from_slice(&nonce);
116122
self.key
117-
.decrypt_in_place_detached(GenericArray::from_slice(&nonce), header, buffer, taga)
123+
.decrypt_in_place_detached(
124+
&nonce_block,
125+
header,
126+
buffer,
127+
&taga,
128+
)
118129
.map_err(|_| ())?;
119130
Ok(&payload[..tag_pos])
120131
}

0 commit comments

Comments
 (0)