Skip to content

Commit dfb0728

Browse files
Implement QUIC header and packet protection key
1 parent 4fae7b4 commit dfb0728

File tree

13 files changed

+522
-24
lines changed

13 files changed

+522
-24
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ aead = { version = "0.6.0-rc.2", default-features = false, optional = true }
2323
aes = { version = "0.9.0-rc.1", default-features = false, optional = true }
2424
aes-gcm = { version = "0.11.0-rc.1", default-features = false, optional = true }
2525
ccm = { version = "0.6.0-pre", default-features = false, optional = true, git = "https://github.com/RustCrypto/AEADs/" }
26+
chacha20 = { version = "0.10.0-rc.2", default-features = false, optional = true }
2627
chacha20poly1305 = { version = "0.11.0-rc.1", default-features = false, optional = true }
2728
cipher = "0.5.0-rc.1"
2829
crypto-common = { version = "0.2.0-rc.4", default-features = false }
@@ -53,6 +54,8 @@ rand_core = { version = "0.9.3", default-features = false, features = [
5354
], optional = true }
5455
rustls = { version = "0.23.31", default-features = false }
5556
webpki = { package = "rustls-webpki", version = "0.103.4", default-features = false, optional = true }
57+
enum_dispatch = "0.3.13"
58+
tinyvec = { version = "1.10.0", default-features = false, optional = true }
5659

5760
[dev-dependencies]
5861
bytes = { version = "1.10.1", default-features = false }
@@ -82,7 +85,7 @@ tls12 = ["rustls/tls12"]
8285

8386
# RustCrypto is preparing to migrate to core::error::Error
8487
# and in before most of the use case for std is just std::error::Error
85-
std = ["alloc", "rustls/std", "ed448-goldilocks?/std"]
88+
std = ["alloc", "rustls/std", "ed448-goldilocks?/std", "tinyvec?/std"]
8689
alloc = [
8790
"ecdsa?/alloc",
8891
"ed448-goldilocks?/alloc",
@@ -218,6 +221,8 @@ hash-sha384 = ["hash"]
218221
hash-sha512 = ["hash"]
219222
hash-full = ["hash-sha224", "hash-sha256", "hash-sha384", "hash-sha512"]
220223

224+
quic = ["aead", "chacha20?/cipher", "tinyvec"]
225+
221226
# Formats
222227
der = ["dep:der", "sec1?/der"]
223228
sec1 = ["dep:sec1", "elliptic-curve?/sec1"]
@@ -238,7 +243,8 @@ aes = ["dep:aes"]
238243
aes-ccm = ["aes", "ccm"]
239244
aes-gcm = ["dep:aes-gcm", "aes", "gcm"]
240245
ccm = ["dep:ccm"]
241-
chacha20poly1305 = ["dep:chacha20poly1305"]
246+
chacha20 = ["dep:chacha20"]
247+
chacha20poly1305 = ["dep:chacha20poly1305", "chacha20"]
242248
elliptic-curve = [
243249
"dep:elliptic-curve",
244250
"elliptic-curve/ecdh",
@@ -248,3 +254,4 @@ gcm = []
248254
rand = ["dep:rand_core", "signature?/rand_core", "x25519-dalek?/os_rng"]
249255
signature = ["dep:signature"]
250256
x448 = ["dep:x448"]
257+
tinyvec = ["dep:tinyvec"]

src/aead.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use aead::Buffer;
22
use rustls::crypto::cipher::{BorrowedPayload, PrefixedPayload};
33

4+
#[cfg(feature = "tinyvec")]
5+
use tinyvec::SliceVec;
6+
47
#[cfg(feature = "gcm")]
58
pub mod gcm;
69

@@ -9,42 +12,75 @@ pub mod ccm;
912

1013
#[macro_use]
1114
pub(crate) mod common;
12-
pub(crate) struct EncryptBufferAdapter<'a>(pub(crate) &'a mut PrefixedPayload);
15+
16+
pub(crate) enum EncryptBufferAdapter<'a> {
17+
PrefixedPayload(&'a mut PrefixedPayload),
18+
#[cfg(feature = "tinyvec")]
19+
Slice(SliceVec<'a, u8>),
20+
}
1321

1422
impl AsRef<[u8]> for EncryptBufferAdapter<'_> {
1523
fn as_ref(&self) -> &[u8] {
16-
self.0.as_ref()
24+
match self {
25+
EncryptBufferAdapter::PrefixedPayload(payload) => payload.as_ref(),
26+
#[cfg(feature = "tinyvec")]
27+
EncryptBufferAdapter::Slice(payload) => payload.as_ref(),
28+
}
1729
}
1830
}
1931

2032
impl AsMut<[u8]> for EncryptBufferAdapter<'_> {
2133
fn as_mut(&mut self) -> &mut [u8] {
22-
self.0.as_mut()
34+
match self {
35+
EncryptBufferAdapter::PrefixedPayload(payload) => payload.as_mut(),
36+
#[cfg(feature = "tinyvec")]
37+
EncryptBufferAdapter::Slice(payload) => payload.as_mut(),
38+
}
2339
}
2440
}
2541

2642
impl Buffer for EncryptBufferAdapter<'_> {
2743
fn extend_from_slice(&mut self, other: &[u8]) -> aead::Result<()> {
28-
self.0.extend_from_slice(other);
44+
match self {
45+
EncryptBufferAdapter::PrefixedPayload(payload) => payload.extend_from_slice(other),
46+
#[cfg(feature = "tinyvec")]
47+
EncryptBufferAdapter::Slice(payload) => payload.extend_from_slice(other),
48+
}
2949
Ok(())
3050
}
3151

3252
fn truncate(&mut self, len: usize) {
33-
self.0.truncate(len)
53+
match self {
54+
EncryptBufferAdapter::PrefixedPayload(payload) => payload.truncate(len),
55+
#[cfg(feature = "tinyvec")]
56+
EncryptBufferAdapter::Slice(payload) => payload.truncate(len),
57+
}
3458
}
3559
}
3660

37-
pub(crate) struct DecryptBufferAdapter<'a, 'p>(pub(crate) &'a mut BorrowedPayload<'p>);
61+
pub(crate) enum DecryptBufferAdapter<'a, 'p> {
62+
BorrowedPayload(&'a mut BorrowedPayload<'p>),
63+
#[cfg(feature = "tinyvec")]
64+
Slice(SliceVec<'a, u8>),
65+
}
3866

3967
impl AsRef<[u8]> for DecryptBufferAdapter<'_, '_> {
4068
fn as_ref(&self) -> &[u8] {
41-
self.0
69+
match self {
70+
DecryptBufferAdapter::BorrowedPayload(payload) => payload,
71+
#[cfg(feature = "tinyvec")]
72+
DecryptBufferAdapter::Slice(slice) => slice,
73+
}
4274
}
4375
}
4476

4577
impl AsMut<[u8]> for DecryptBufferAdapter<'_, '_> {
4678
fn as_mut(&mut self) -> &mut [u8] {
47-
self.0
79+
match self {
80+
DecryptBufferAdapter::BorrowedPayload(payload) => payload,
81+
#[cfg(feature = "tinyvec")]
82+
DecryptBufferAdapter::Slice(slice) => slice,
83+
}
4884
}
4985
}
5086

@@ -54,7 +90,11 @@ impl Buffer for DecryptBufferAdapter<'_, '_> {
5490
}
5591

5692
fn truncate(&mut self, len: usize) {
57-
self.0.truncate(len)
93+
match self {
94+
DecryptBufferAdapter::BorrowedPayload(payload) => payload.truncate(len),
95+
#[cfg(feature = "tinyvec")]
96+
DecryptBufferAdapter::Slice(payload) => payload.truncate(len),
97+
}
5898
}
5999
}
60100

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ pub mod tls13;
105105
#[cfg(feature = "verify")]
106106
pub mod verify;
107107

108+
#[cfg(feature = "quic")]
109+
pub mod quic;
110+
108111
const _: () = assert!(
109112
!ALL_CIPHER_SUITES.is_empty(),
110113
"At least one cipher suite should be enabled"

src/misc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ macro_rules! feature_slice {
5454

5555
#[macro_export]
5656
macro_rules! tls13_cipher_suite {
57-
($name:ident, $suite:expr, $hash:expr, $hkdf:expr, $aead:expr) => {
57+
($name:ident, $suite:expr, $hash:expr, $hkdf:expr, $aead:expr, $quic:expr) => {
5858
pub const $name: Tls13CipherSuite = Tls13CipherSuite {
5959
common: CipherSuiteCommon {
6060
suite: $suite,
@@ -63,7 +63,7 @@ macro_rules! tls13_cipher_suite {
6363
},
6464
hkdf_provider: &$hkdf,
6565
aead_alg: $aead,
66-
quic: None,
66+
quic: $quic,
6767
};
6868
};
6969
}

0 commit comments

Comments
 (0)