Skip to content

Commit 8b5818c

Browse files
committed
feat(s2n-quic-dc): import 7/18 version
1 parent d55d258 commit 8b5818c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+6322
-2267
lines changed

dc/s2n-quic-dc/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ license = "Apache-2.0"
1111
exclude = ["corpus.tar.gz"]
1212

1313
[features]
14+
default = ["tokio"]
1415
testing = ["bolero-generator", "s2n-quic-core/testing"]
16+
tokio = ["tokio/io-util", "tokio/net", "tokio/rt-multi-thread", "tokio/time"]
1517

1618
[dependencies]
1719
arrayvec = "0.7"
@@ -35,7 +37,7 @@ s2n-quic-core = { version = "=0.42.0", path = "../../quic/s2n-quic-core", defaul
3537
s2n-quic-platform = { version = "=0.42.0", path = "../../quic/s2n-quic-platform" }
3638
slotmap = "1"
3739
thiserror = "1"
38-
tokio = { version = "1", features = ["sync"] }
40+
tokio = { version = "1", default-features = false, features = ["sync"] }
3941
tracing = "0.1"
4042
zerocopy = { version = "0.7", features = ["derive"] }
4143
zeroize = "1"
@@ -46,4 +48,4 @@ bolero-generator = "0.11"
4648
insta = "1"
4749
s2n-codec = { path = "../../common/s2n-codec", features = ["testing"] }
4850
s2n-quic-core = { path = "../../quic/s2n-quic-core", features = ["testing"] }
49-
tokio = { version = "1", features = ["sync"] }
51+
tokio = { version = "1", features = ["full"] }

dc/s2n-quic-dc/src/clock.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use core::{fmt, pin::Pin, task::Poll, time::Duration};
55
use s2n_quic_core::{ensure, time};
66
use tracing::trace;
77

8+
#[cfg(feature = "tokio")]
89
pub mod tokio;
910
pub use time::clock::Cached;
1011

dc/s2n-quic-dc/src/crypto.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub mod encrypt {
1616
pub trait Key {
1717
fn credentials(&self) -> &Credentials;
1818

19+
fn key_phase(&self) -> KeyPhase;
20+
1921
fn tag_len(&self) -> usize;
2022

2123
/// Encrypt a payload
@@ -75,6 +77,7 @@ pub mod decrypt {
7577
/// Decrypt a payload
7678
fn decrypt<N: IntoNonce>(
7779
&self,
80+
key_phase: KeyPhase,
7881
nonce: N,
7982
header: &[u8],
8083
payload_in: &[u8],
@@ -85,13 +88,15 @@ pub mod decrypt {
8588
/// Decrypt a payload
8689
fn decrypt_in_place<N: IntoNonce>(
8790
&self,
91+
key_phase: KeyPhase,
8892
nonce: N,
8993
header: &[u8],
9094
payload_and_tag: &mut [u8],
9195
) -> Result;
9296

9397
fn retransmission_tag(
9498
&self,
99+
key_phase: KeyPhase,
95100
original_packet_number: u64,
96101
retransmission_packet_number: u64,
97102
tag_out: &mut [u8],

dc/s2n-quic-dc/src/crypto/awslc.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use super::IntoNonce;
55
use crate::credentials::Credentials;
66
use aws_lc_rs::aead::{Aad, Algorithm, LessSafeKey, Nonce, UnboundKey, NONCE_LEN};
7-
use s2n_quic_core::assume;
7+
use s2n_quic_core::{assume, packet::KeyPhase};
88

99
pub use aws_lc_rs::aead::{AES_128_GCM, AES_256_GCM};
1010

@@ -41,6 +41,11 @@ impl super::encrypt::Key for EncryptKey {
4141
&self.credentials
4242
}
4343

44+
#[inline]
45+
fn key_phase(&self) -> KeyPhase {
46+
KeyPhase::Zero
47+
}
48+
4449
#[inline(always)]
4550
fn tag_len(&self) -> usize {
4651
debug_assert_eq!(TAG_LEN, self.key.algorithm().tag_len());
@@ -137,6 +142,7 @@ impl super::decrypt::Key for DecryptKey {
137142
#[inline]
138143
fn decrypt<N: IntoNonce>(
139144
&self,
145+
_key_phase: KeyPhase,
140146
nonce: N,
141147
header: &[u8],
142148
payload_in: &[u8],
@@ -163,6 +169,7 @@ impl super::decrypt::Key for DecryptKey {
163169
#[inline]
164170
fn decrypt_in_place<N: IntoNonce>(
165171
&self,
172+
_key_phase: KeyPhase,
166173
nonce: N,
167174
header: &[u8],
168175
payload_and_tag: &mut [u8],
@@ -180,6 +187,7 @@ impl super::decrypt::Key for DecryptKey {
180187
#[inline]
181188
fn retransmission_tag(
182189
&self,
190+
_key_phase: KeyPhase,
183191
original_packet_number: u64,
184192
retransmission_packet_number: u64,
185193
tag_out: &mut [u8],

dc/s2n-quic-dc/src/crypto/testing.rs

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use super::IntoNonce;
55
use crate::credentials::Credentials;
66
use s2n_quic_core::assume;
7+
use s2n_quic_core::packet::KeyPhase;
78

89
#[derive(Clone, Debug)]
910
pub struct Key {
@@ -27,6 +28,11 @@ impl super::encrypt::Key for Key {
2728
&self.credentials
2829
}
2930

31+
#[inline]
32+
fn key_phase(&self) -> KeyPhase {
33+
KeyPhase::Zero
34+
}
35+
3036
#[inline]
3137
fn tag_len(&self) -> usize {
3238
self.tag_len
@@ -77,6 +83,7 @@ impl super::decrypt::Key for Key {
7783
#[inline]
7884
fn decrypt<N: IntoNonce>(
7985
&self,
86+
_key_phase: KeyPhase,
8087
_nonce: N,
8188
_header: &[u8],
8289
payload_in: &[u8],
@@ -90,6 +97,7 @@ impl super::decrypt::Key for Key {
9097
#[inline]
9198
fn decrypt_in_place<N: IntoNonce>(
9299
&self,
100+
_key_phase: KeyPhase,
93101
_nonce: N,
94102
_header: &[u8],
95103
_payload_and_tag: &mut [u8],
@@ -100,6 +108,7 @@ impl super::decrypt::Key for Key {
100108
#[inline]
101109
fn retransmission_tag(
102110
&self,
111+
_key_phase: KeyPhase,
103112
_original_packet_number: u64,
104113
_retransmission_packet_number: u64,
105114
_tag_out: &mut [u8],

dc/s2n-quic-dc/src/datagram/tunneled/recv.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl<K: decrypt::Key> Receiver<K> {
5454
debug_assert_eq!(packet.payload().len(), payload_out.len());
5555

5656
self.key.decrypt(
57+
packet.tag().key_phase(),
5758
packet.crypto_nonce(),
5859
packet.header(),
5960
packet.payload(),

dc/s2n-quic-dc/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ pub mod socket;
1818
pub mod stream;
1919
pub mod task;
2020

21+
#[cfg(any(test, feature = "testing"))]
22+
pub mod testing;
23+
2124
pub use s2n_quic_core::dc::{Version, SUPPORTED_VERSIONS};

dc/s2n-quic-dc/src/packet.rs

+26
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@ pub type PayloadLen = VarInt;
1010

1111
#[macro_use]
1212
pub mod tag;
13+
pub mod wire_version;
1314

1415
pub mod control;
1516
pub mod datagram;
1617
pub mod secret_control;
1718
pub mod stream;
1819

1920
pub use tag::Tag;
21+
pub use wire_version::WireVersion;
22+
23+
#[derive(Clone, Copy, Debug)]
24+
pub enum Kind {
25+
Stream,
26+
Datagram,
27+
Control,
28+
StaleKey,
29+
ReplayDetected,
30+
UnknownPathSecret,
31+
}
2032

2133
#[derive(Debug)]
2234
pub enum Packet<'a> {
@@ -28,6 +40,20 @@ pub enum Packet<'a> {
2840
UnknownPathSecret(secret_control::unknown_path_secret::Packet<'a>),
2941
}
3042

43+
impl<'a> Packet<'a> {
44+
#[inline]
45+
pub fn kind(&self) -> Kind {
46+
match self {
47+
Packet::Stream(_) => Kind::Stream,
48+
Packet::Datagram(_) => Kind::Datagram,
49+
Packet::Control(_) => Kind::Control,
50+
Packet::StaleKey(_) => Kind::StaleKey,
51+
Packet::ReplayDetected(_) => Kind::ReplayDetected,
52+
Packet::UnknownPathSecret(_) => Kind::UnknownPathSecret,
53+
}
54+
}
55+
}
56+
3157
impl<'a> s2n_codec::DecoderParameterizedValueMut<'a> for Packet<'a> {
3258
type Parameter = usize;
3359

dc/s2n-quic-dc/src/packet/control.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use super::tag::Common;
55
use core::fmt;
6+
use s2n_quic_core::packet::KeyPhase;
67
use zerocopy::{AsBytes, FromBytes, FromZeroes, Unaligned};
78

89
const NONCE_MASK: u64 = 1 << 63;
@@ -28,13 +29,15 @@ impl fmt::Debug for Tag {
2829
f.debug_struct("control::Tag")
2930
.field("is_stream", &self.is_stream())
3031
.field("has_application_header", &self.has_application_header())
32+
.field("key_phase", &self.key_phase())
3133
.finish()
3234
}
3335
}
3436

3537
impl Tag {
36-
pub const IS_STREAM_MASK: u8 = 0b0010;
37-
pub const HAS_APPLICATION_HEADER_MASK: u8 = 0b00_0001;
38+
pub const IS_STREAM_MASK: u8 = 0b0100;
39+
pub const HAS_APPLICATION_HEADER_MASK: u8 = 0b0010;
40+
pub const KEY_PHASE_MASK: u8 = 0b0001;
3841

3942
pub const MIN: u8 = 0b0101_0000;
4043
pub const MAX: u8 = 0b0101_1111;
@@ -59,6 +62,24 @@ impl Tag {
5962
self.0.get(Self::HAS_APPLICATION_HEADER_MASK)
6063
}
6164

65+
#[inline]
66+
pub fn set_key_phase(&mut self, key_phase: KeyPhase) {
67+
let v = match key_phase {
68+
KeyPhase::Zero => false,
69+
KeyPhase::One => true,
70+
};
71+
self.0.set(Self::KEY_PHASE_MASK, v)
72+
}
73+
74+
#[inline]
75+
pub fn key_phase(&self) -> KeyPhase {
76+
if self.0.get(Self::KEY_PHASE_MASK) {
77+
KeyPhase::One
78+
} else {
79+
KeyPhase::Zero
80+
}
81+
}
82+
6283
#[inline]
6384
fn validate(&self) -> Result<(), s2n_codec::DecoderError> {
6485
let range = Self::MIN..=Self::MAX;

dc/s2n-quic-dc/src/packet/control/decoder.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
credentials::Credentials,
66
packet::{
77
control::{self, Tag},
8-
stream,
8+
stream, WireVersion,
99
},
1010
};
1111
use s2n_codec::{
@@ -50,6 +50,7 @@ where
5050
#[derive(Debug)]
5151
pub struct Packet<'a> {
5252
tag: Tag,
53+
wire_version: WireVersion,
5354
credentials: Credentials,
5455
source_control_port: u16,
5556
stream_id: Option<stream::Id>,
@@ -66,6 +67,11 @@ impl<'a> Packet<'a> {
6667
self.tag
6768
}
6869

70+
#[inline]
71+
pub fn wire_version(&self) -> WireVersion {
72+
self.wire_version
73+
}
74+
6975
#[inline]
7076
pub fn credentials(&self) -> &Credentials {
7177
&self.credentials
@@ -124,6 +130,7 @@ impl<'a> Packet<'a> {
124130
) -> R<Packet> {
125131
let (
126132
tag,
133+
wire_version,
127134
credentials,
128135
source_control_port,
129136
stream_id,
@@ -147,6 +154,8 @@ impl<'a> Packet<'a> {
147154
let (tag, buffer) = buffer.decode()?;
148155
validator.validate_tag(tag)?;
149156

157+
let (wire_version, buffer) = buffer.decode()?;
158+
150159
let (credentials, buffer) = buffer.decode()?;
151160

152161
let (source_control_port, buffer) = buffer.decode()?;
@@ -181,6 +190,7 @@ impl<'a> Packet<'a> {
181190

182191
(
183192
tag,
193+
wire_version,
184194
credentials,
185195
source_control_port,
186196
stream_id,
@@ -222,6 +232,7 @@ impl<'a> Packet<'a> {
222232

223233
let packet = Packet {
224234
tag,
235+
wire_version,
225236
credentials,
226237
source_control_port,
227238
stream_id,

dc/s2n-quic-dc/src/packet/control/encoder.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
crypto::encrypt,
66
packet::{
77
control::{Tag, NONCE_MASK},
8-
stream,
8+
stream, WireVersion,
99
},
1010
};
1111
use s2n_codec::{Encoder, EncoderBuffer, EncoderValue};
@@ -31,19 +31,16 @@ where
3131
debug_assert_ne!(source_control_port, 0);
3232

3333
let mut tag = Tag::default();
34+
tag.set_key_phase(crypto.key_phase());
35+
tag.set_is_stream(stream_id.is_some());
36+
tag.set_has_application_header(*header_len > 0);
37+
encoder.encode(&tag);
3438

35-
if stream_id.is_some() {
36-
tag.set_is_stream(true);
37-
}
38-
39-
if *header_len > 0 {
40-
tag.set_has_application_header(true);
41-
}
39+
// wire version - we only support `0` currently
40+
encoder.encode(&WireVersion::ZERO);
4241

4342
let nonce = *packet_number | NONCE_MASK;
4443

45-
encoder.encode(&tag);
46-
4744
// encode the credentials being used
4845
encoder.encode(crypto.credentials());
4946
encoder.encode(&source_control_port);

0 commit comments

Comments
 (0)