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
use ring::{aead, hkdf, hmac};
use crate::{
config::ConfigError,
crypto::{self, CryptoError},
packet::{PacketNumber, LONG_HEADER_FORM},
};
impl crypto::HeaderKey for aead::quic::HeaderProtectionKey {
fn decrypt(&self, pn_offset: usize, packet: &mut [u8]) {
let (header, sample) = packet.split_at_mut(pn_offset + 4);
let mask = self.new_mask(&sample[0..self.sample_size()]).unwrap();
if header[0] & LONG_HEADER_FORM == LONG_HEADER_FORM {
header[0] ^= mask[0] & 0x0f;
} else {
header[0] ^= mask[0] & 0x1f;
}
let pn_length = PacketNumber::decode_len(header[0]);
for (out, inp) in header[pn_offset..pn_offset + pn_length]
.iter_mut()
.zip(&mask[1..])
{
*out ^= inp;
}
}
fn encrypt(&self, pn_offset: usize, packet: &mut [u8]) {
let (header, sample) = packet.split_at_mut(pn_offset + 4);
let mask = self.new_mask(&sample[0..self.sample_size()]).unwrap();
let pn_length = PacketNumber::decode_len(header[0]);
if header[0] & LONG_HEADER_FORM == LONG_HEADER_FORM {
header[0] ^= mask[0] & 0x0f;
} else {
header[0] ^= mask[0] & 0x1f;
}
for (out, inp) in header[pn_offset..pn_offset + pn_length]
.iter_mut()
.zip(&mask[1..])
{
*out ^= inp;
}
}
fn sample_size(&self) -> usize {
self.algorithm().sample_len()
}
}
impl crypto::HmacKey for hmac::Key {
const KEY_LEN: usize = 64;
type Signature = hmac::Tag;
fn new(key: &[u8]) -> Result<Self, ConfigError> {
if key.len() == Self::KEY_LEN {
Ok(hmac::Key::new(hmac::HMAC_SHA256, key))
} else {
Err(ConfigError::OutOfBounds)
}
}
fn sign(&self, data: &[u8]) -> Self::Signature {
hmac::sign(self, data)
}
fn verify(&self, data: &[u8], signature: &[u8]) -> Result<(), CryptoError> {
Ok(hmac::verify(self, data, signature)?)
}
}
impl crypto::HandshakeTokenKey for hkdf::Prk {
type AeadKey = ring::aead::LessSafeKey;
fn aead_from_hkdf(&self, random_bytes: &[u8]) -> Self::AeadKey {
let mut key_buffer = [0u8; 32];
let info = [random_bytes];
let okm = self.expand(&info, hkdf::HKDF_SHA256).unwrap();
okm.fill(&mut key_buffer).unwrap();
let key = aead::UnboundKey::new(&aead::AES_256_GCM, &key_buffer).unwrap();
Self::AeadKey::new(key)
}
fn from_secret(bytes: &[u8]) -> Self {
hkdf::Salt::new(hkdf::HKDF_SHA256, &[]).extract(bytes)
}
}
impl crypto::AeadKey for aead::LessSafeKey {
const KEY_LEN: usize = 32;
fn seal(&self, data: &mut Vec<u8>, additional_data: &[u8]) -> Result<(), CryptoError> {
let aad = ring::aead::Aad::from(additional_data);
let zero_nonce = ring::aead::Nonce::assume_unique_for_key([0u8; 12]);
Ok(self.seal_in_place_append_tag(zero_nonce, aad, data)?)
}
fn open<'a>(
&self,
data: &'a mut [u8],
additional_data: &[u8],
) -> Result<&'a mut [u8], CryptoError> {
let aad = ring::aead::Aad::from(additional_data);
let zero_nonce = ring::aead::Nonce::assume_unique_for_key([0u8; 12]);
Ok(self.open_in_place(zero_nonce, aad, data)?)
}
}
impl From<ring::error::Unspecified> for CryptoError {
fn from(_: ring::error::Unspecified) -> Self {
CryptoError
}
}