s2n-quic-dc 0.83.0

Internal crate used by s2n-quic
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use crate::{
    credentials::Id,
    crypto::awslc::{open, seal},
};
use aws_lc_rs::{
    aead::{self, NONCE_LEN},
    hkdf::{self, Prk},
    hmac,
};
use s2n_quic_core::{dc, varint::VarInt};
use zeroize::Zeroizing;

pub use s2n_quic_core::endpoint;

pub const MAX_KEY_LEN: usize = 32;
const MAX_HMAC_KEY_LEN: usize = 1024 / 8;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
#[cfg_attr(test, derive(bolero_generator::TypeGenerator))]
#[allow(non_camel_case_types)]
pub enum Ciphersuite {
    AES_GCM_128_SHA256,
    AES_GCM_256_SHA384,
}

impl Ciphersuite {
    #[inline]
    pub fn aead(&self) -> &'static aead::Algorithm {
        match self {
            Self::AES_GCM_128_SHA256 => &aead::AES_128_GCM,
            Self::AES_GCM_256_SHA384 => &aead::AES_256_GCM,
        }
    }

    #[inline]
    pub fn hkdf(&self) -> hkdf::Algorithm {
        match self {
            Self::AES_GCM_128_SHA256 => hkdf::HKDF_SHA256,
            Self::AES_GCM_256_SHA384 => hkdf::HKDF_SHA384,
        }
    }

    #[inline]
    pub fn hmac(&self) -> &'static hmac::Algorithm {
        match self {
            Self::AES_GCM_128_SHA256 => &hmac::HMAC_SHA256,
            Self::AES_GCM_256_SHA384 => &hmac::HMAC_SHA384,
        }
    }
}

impl hkdf::KeyType for Ciphersuite {
    #[inline]
    fn len(&self) -> usize {
        match self {
            Self::AES_GCM_128_SHA256 => 16,
            Self::AES_GCM_256_SHA384 => 32,
        }
    }
}

impl From<Ciphersuite> for u8 {
    fn from(c: Ciphersuite) -> u8 {
        c as u8
    }
}

impl TryFrom<u8> for Ciphersuite {
    type Error = &'static str;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Ciphersuite::AES_GCM_128_SHA256),
            1 => Ok(Ciphersuite::AES_GCM_256_SHA384),
            _ => Err("Invalid Ciphersuite value"),
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum Initiator {
    Local,
    Remote,
}

impl Initiator {
    #[inline]
    fn label(self, endpoint: endpoint::Type) -> &'static [u8] {
        use endpoint::Type::*;
        use Initiator::*;

        match (endpoint, self) {
            (Client, Local) | (Server, Remote) => b" client",
            (Server, Local) | (Client, Remote) => b" server",
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum Direction {
    Send,
    Receive,
}

impl Direction {
    #[inline]
    fn label(self, endpoint: endpoint::Type) -> &'static [u8] {
        use endpoint::Type::*;
        use Direction::*;

        match (endpoint, self) {
            (Client, Send) | (Server, Receive) => b" client",
            (Server, Send) | (Client, Receive) => b" server",
        }
    }
}

pub const EXPORT_SECRET_LEN: usize = 32;
pub type ExportSecret = [u8; 32];

#[derive(Debug)]
pub struct Secret {
    id: Id,
    export_secret: Zeroizing<ExportSecret>,
    endpoint: endpoint::Type,
    ciphersuite: Ciphersuite,
}

impl super::map::SizeOf for Id {}
impl super::map::SizeOf for endpoint::Type {}
impl super::map::SizeOf for Ciphersuite {}
impl super::map::SizeOf for Zeroizing<ExportSecret> {
    fn size(&self) -> usize {
        // Zeroizing uses Drop just for zeroing, but that doesn't add any space.
        std::mem::size_of::<Self>()
    }
}

impl super::map::SizeOf for Secret {
    fn size(&self) -> usize {
        let Secret {
            id,
            export_secret,
            endpoint,
            ciphersuite,
        } = self;
        id.size() + export_secret.size() + endpoint.size() + ciphersuite.size()
    }
}

impl Secret {
    #[inline]
    pub fn new(
        ciphersuite: Ciphersuite,
        _version: dc::Version,
        endpoint: endpoint::Type,
        export_secret: &ExportSecret,
    ) -> Self {
        let mut v = Self {
            id: Default::default(),
            export_secret: Zeroizing::new(*export_secret),
            endpoint,
            ciphersuite,
        };

        let mut id = Id::default();
        v.prk().expand_into(&[&[16], b" pid"], &mut *id);
        v.id = id;

        v
    }

    // Note that Prk doesn't allocate when constructed with new_less_safe (or even traverse to C),
    // but we can store it in far less space (104 -> 32 bytes) if we store just the secret
    // directly.
    fn prk(&self) -> Prk {
        Prk::new_less_safe(self.ciphersuite.hkdf(), &*self.export_secret)
    }

    #[inline]
    pub fn id(&self) -> &Id {
        &self.id
    }

    #[inline]
    pub fn export_secret(&self) -> &ExportSecret {
        &self.export_secret
    }

    #[inline]
    pub fn ciphersuite(&self) -> &Ciphersuite {
        &self.ciphersuite
    }

    #[inline]
    pub fn application_pair(
        &self,
        key_id: VarInt,
        initiator: Initiator,
    ) -> (seal::Application, SealUpdate, open::Application, OpenUpdate) {
        let ciphersuite = &self.ciphersuite;
        let mut out = [0u8; (NONCE_LEN + MAX_KEY_LEN) * 2 + MAX_KEY_LEN * 2];
        let key_len = hkdf::KeyType::len(ciphersuite);
        let out_len = (NONCE_LEN + key_len) * 2 + key_len * 2;

        debug_assert!(out_len <= u16::MAX as usize);

        let (out, _) = out.split_at_mut(out_len);
        self.prk().expand_into(
            &[
                &(out_len as u16).to_be_bytes(),
                b" bidi",
                initiator.label(self.endpoint),
                b" app",
                &key_id.to_be_bytes(),
            ],
            out,
        );

        // if the hash is ever broken, it's better to put the "more secret" data at the beginning
        //
        // here we derive:
        //
        // (client_ku, server_ku, client_key, server_key, client_iv, server_iv)
        let (client_ku, out) = out.split_at(key_len);
        let (server_ku, out) = out.split_at(key_len);
        let (client_key, out) = out.split_at(key_len);
        let (server_key, out) = out.split_at(key_len);
        let (client_iv, server_iv) = out.split_at(NONCE_LEN);
        #[expect(
            clippy::unwrap_used,
            reason = "the slice was split off at NONCE_LEN, so it is provably the right length for the array conversion"
        )]
        let client_iv = client_iv.try_into().unwrap();
        #[expect(
            clippy::unwrap_used,
            reason = "the slice was split off at NONCE_LEN, so it is provably the right length for the array conversion"
        )]
        let server_iv = server_iv.try_into().unwrap();
        let aead = ciphersuite.aead();

        let (sealer_ku, opener_ku, sealer_key, opener_key, sealer_iv, opener_iv) =
            match self.endpoint {
                endpoint::Type::Client => (
                    client_ku, server_ku, client_key, server_key, client_iv, server_iv,
                ),
                endpoint::Type::Server => (
                    server_ku, client_ku, server_key, client_key, server_iv, client_iv,
                ),
            };

        let sealer = seal::Application::new(sealer_key, sealer_iv, aead);
        let sealer_ku = SealUpdate::new(sealer_ku, ciphersuite);
        let opener = open::Application::new(opener_key, opener_iv, aead);
        let opener_ku = OpenUpdate::new(opener_ku, ciphersuite);
        (sealer, sealer_ku, opener, opener_ku)
    }

    #[inline]
    pub fn control_pair(
        &self,
        key_id: VarInt,
        initiator: Initiator,
    ) -> (seal::control::Stream, open::control::Stream) {
        let ciphersuite = &self.ciphersuite;
        let mut out = [0u8; MAX_HMAC_KEY_LEN * 2];
        let key_len = {
            // Use the block length for the key, instead of output length for stronger security and to
            // avoid padding.

            //= https://www.rfc-editor.org/rfc/rfc2104.html#section-2
            //# The authentication key K can be of any length up to B, the
            //# block length of the hash function.  Applications that use keys longer
            //# than B bytes will first hash the key using H and then use the
            //# resultant L byte string as the actual key to HMAC. In any case the
            //# minimal recommended length for K is L bytes (as the hash output
            //# length).
            ciphersuite.hmac().digest_algorithm().block_len()
        };
        let out_len = key_len * 2;

        debug_assert!(out_len <= u16::MAX as usize);

        let (out, _) = out.split_at_mut(out_len);
        self.prk().expand_into(
            &[
                &(out_len as u16).to_be_bytes(),
                b" bidi",
                initiator.label(self.endpoint),
                b" ctl",
                &key_id.to_be_bytes(),
            ],
            out,
        );

        let (client_key, server_key) = out.split_at(key_len);
        let hmac = ciphersuite.hmac();

        let (sealer_key, opener_key) = match self.endpoint {
            endpoint::Type::Client => (client_key, server_key),
            endpoint::Type::Server => (server_key, client_key),
        };

        let sealer = seal::control::Stream::new(sealer_key, hmac);
        let opener = open::control::Stream::new(opener_key, hmac);
        (sealer, opener)
    }

    #[inline]
    pub fn application_sealer(&self, key_id: VarInt) -> seal::Application {
        self.derive_application_key(Direction::Send, key_id, |alg, key, iv| {
            seal::Application::new(key, iv, alg)
        })
    }

    #[inline]
    pub fn application_opener(&self, key_id: VarInt) -> open::Application {
        self.derive_application_key(Direction::Receive, key_id, |alg, key, iv| {
            open::Application::new(key, iv, alg)
        })
    }

    #[inline]
    fn derive_application_key<F, R>(&self, direction: Direction, key_id: VarInt, f: F) -> R
    where
        F: FnOnce(&'static aead::Algorithm, &[u8], [u8; NONCE_LEN]) -> R,
    {
        let mut out = [0u8; NONCE_LEN + MAX_KEY_LEN];
        let key_len = hkdf::KeyType::len(&self.ciphersuite);
        let out_len = NONCE_LEN + key_len;
        debug_assert!(out_len <= u16::MAX as usize);

        let (out, _) = out.split_at_mut(out_len);
        self.prk().expand_into(
            &[
                &(out_len as u16).to_be_bytes(),
                b" uni",
                direction.label(self.endpoint),
                &key_id.to_be_bytes(),
            ],
            out,
        );
        // if the hash is ever broken, it's better to put the "more secret" data at the beginning
        let (key, iv) = out.split_at(key_len);
        #[expect(
            clippy::unwrap_used,
            reason = "the remaining slice after splitting off key_len is NONCE_LEN long, matching the array conversion"
        )]
        let iv = iv.try_into().unwrap();
        f(self.ciphersuite.aead(), key, iv)
    }

    pub fn control_sealer(&self) -> seal::control::Secret {
        self.derive_control_key(Direction::Send, seal::control::Secret::new)
    }

    pub fn control_opener(&self) -> open::control::Secret {
        self.derive_control_key(Direction::Receive, open::control::Secret::new)
    }

    #[inline]
    fn derive_control_key<F, R>(&self, direction: Direction, f: F) -> R
    where
        F: FnOnce(&[u8], &'static hmac::Algorithm) -> R,
    {
        let mut out = [0u8; MAX_HMAC_KEY_LEN];
        let key_len = {
            // Use the block length for the key, instead of output length for stronger security and to
            // avoid padding.

            //= https://www.rfc-editor.org/rfc/rfc2104.html#section-2
            //# The authentication key K can be of any length up to B, the
            //# block length of the hash function.  Applications that use keys longer
            //# than B bytes will first hash the key using H and then use the
            //# resultant L byte string as the actual key to HMAC. In any case the
            //# minimal recommended length for K is L bytes (as the hash output
            //# length).
            self.ciphersuite.hmac().digest_algorithm().block_len()
        };

        let out_len = key_len;
        debug_assert!(out_len <= u16::MAX as usize);

        let (out, _) = out.split_at_mut(out_len);
        self.prk().expand_into(
            &[
                &(out_len as u16).to_be_bytes(),
                b" ctl",
                direction.label(self.endpoint),
            ],
            out,
        );
        f(out, self.ciphersuite.hmac())
    }
}

trait PrkExt {
    fn expand_into(&self, label: &[&[u8]], out: &mut [u8]);
}

impl PrkExt for Prk {
    #[inline]
    fn expand_into(&self, label: &[&[u8]], out: &mut [u8]) {
        #[expect(
            clippy::unwrap_used,
            reason = "expand and fill only fail on invalid output lengths; OutLen is derived from the output slice so the length is always valid"
        )]
        self.expand(label, OutLen(out.len()))
            .unwrap()
            .fill(out)
            .unwrap();
    }
}

#[derive(Clone, Copy)]
pub struct OutLen(pub usize);

impl hkdf::KeyType for OutLen {
    #[inline]
    fn len(&self) -> usize {
        self.0
    }
}

#[derive(Debug)]
pub struct SealUpdate(Updater);

impl SealUpdate {
    #[inline]
    pub fn new(secret: &[u8], ciphersuite: &Ciphersuite) -> Self {
        Self(Updater::new(secret, ciphersuite))
    }

    #[inline]
    pub fn next(&self) -> (seal::Application, SealUpdate) {
        self.0.next(|key, iv, updater| {
            let key = seal::Application::new(key, iv, updater.ciphersuite.aead());
            (key, Self(updater))
        })
    }
}

#[derive(Debug)]
pub struct OpenUpdate(Updater);

impl OpenUpdate {
    #[inline]
    pub fn new(secret: &[u8], ciphersuite: &Ciphersuite) -> Self {
        Self(Updater::new(secret, ciphersuite))
    }

    #[inline]
    pub fn next(&self) -> (open::Application, OpenUpdate) {
        self.0.next(|key, iv, updater| {
            let key = open::Application::new(key, iv, updater.ciphersuite.aead());
            (key, Self(updater))
        })
    }
}

#[derive(Debug)]
struct Updater {
    prk: Prk,
    ciphersuite: Ciphersuite,
}

impl Updater {
    #[inline]
    fn new(secret: &[u8], ciphersuite: &Ciphersuite) -> Self {
        let prk = Prk::new_less_safe(ciphersuite.hkdf(), secret);
        let ciphersuite = *ciphersuite;
        Self { prk, ciphersuite }
    }

    #[inline]
    fn next<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&[u8], [u8; NONCE_LEN], Updater) -> R,
    {
        let ciphersuite = &self.ciphersuite;

        let mut out = [0u8; NONCE_LEN + MAX_KEY_LEN * 2];
        let key_len = hkdf::KeyType::len(ciphersuite);
        let out_len = NONCE_LEN + key_len * 2;
        let (out, _) = out.split_at_mut(out_len);
        self.prk
            .expand_into(&[&(out_len as u16).to_be_bytes(), b" ku"], out);

        // if the hash is ever broken, it's better to put the "more secret" data at the beginning
        //
        // here we derive:
        //
        // (key_update, key, iv)
        let (ku, out) = out.split_at(key_len);
        let (key, iv) = out.split_at(key_len);
        #[expect(
            clippy::unwrap_used,
            reason = "the remaining slice after splitting off key_len is NONCE_LEN long, matching the array conversion"
        )]
        let iv = iv.try_into().unwrap();

        let ku = Self::new(ku, ciphersuite);

        f(key, iv, ku)
    }
}

#[cfg(test)]
#[allow(
    clippy::panic_in_result_fn,
    reason = "test code may panic to surface failures"
)]
mod tests {
    use super::*;
    use crate::{
        event,
        path::secret::{map::Dedup, open::Application as Opener, seal::Application as Sealer},
        stream,
    };
    use bolero::*;
    use s2n_quic_core::time::testing;

    #[derive(Clone, Copy, Debug, TypeGenerator)]
    struct Pair {
        ciphersuite: Ciphersuite,
        key_id: VarInt,
        initiator_is_client: bool,
    }

    impl Pair {
        fn initiator(&self) -> endpoint::Type {
            if self.initiator_is_client {
                endpoint::Type::Client
            } else {
                endpoint::Type::Server
            }
        }

        fn endpoints(&self) -> (Secret, Secret) {
            let secret = &[42; 32];
            let client = Secret::new(self.ciphersuite, 0, endpoint::Type::Client, secret);
            let server = Secret::new(self.ciphersuite, 0, endpoint::Type::Server, secret);
            (client, server)
        }

        fn check_app(self) {
            let (client, server) = self.endpoints();
            let (client_i, server_i) = match self.initiator() {
                endpoint::Type::Client => (Initiator::Local, Initiator::Remote),
                endpoint::Type::Server => (Initiator::Remote, Initiator::Local),
            };
            let mut client_app = Application::new(client.application_pair(self.key_id, client_i));
            let mut server_app = Application::new(server.application_pair(self.key_id, server_i));
            let subscriber = stream::shared::Subscriber {
                subscriber: event::testing::Subscriber::no_snapshot(),
                context: (),
            };
            let clock = testing::Clock::default();

            for i in 0..8 {
                client_app.send(&server_app).unwrap();
                server_app.send(&client_app).unwrap();

                // invalid sender/recipient should fail
                client_app.send(&client_app).unwrap_err();
                server_app.send(&server_app).unwrap_err();

                let (sender, receiver) = if i % 2 == 0 {
                    dbg!("client ku");
                    (&mut client_app, &mut server_app)
                } else {
                    dbg!("server ku");
                    (&mut server_app, &mut client_app)
                };

                sender.sealer.update(&clock, &subscriber);
                sender.send(receiver).unwrap();

                assert!(receiver.opener.needs_update());
                receiver.opener.update(&clock, &subscriber);
            }
        }

        fn check_control(self) {
            let (client, server) = self.endpoints();
            let (client_i, server_i) = match self.initiator() {
                endpoint::Type::Client => (Initiator::Local, Initiator::Remote),
                endpoint::Type::Server => (Initiator::Remote, Initiator::Local),
            };
            let client_app = Control::new(client.control_pair(self.key_id, client_i));
            let server_app = Control::new(server.control_pair(self.key_id, server_i));

            client_app.send(&server_app).unwrap();
            server_app.send(&client_app).unwrap();

            // invalid sender/recipient should fail
            client_app.send(&client_app).unwrap_err();
            server_app.send(&server_app).unwrap_err();
        }
    }

    struct Application {
        sealer: Sealer,
        opener: Opener,
    }

    impl Application {
        fn new(
            (sealer, sealer_ku, opener, opener_ku): (
                seal::Application,
                SealUpdate,
                open::Application,
                OpenUpdate,
            ),
        ) -> Self {
            let sealer = Sealer::new(sealer, sealer_ku);
            let opener = Opener::new(opener, opener_ku, Dedup::disabled());
            Self { sealer, opener }
        }

        fn send(&self, other: &Self) -> crate::crypto::open::Result {
            use crate::crypto::{open::Application as _, seal::Application as _};

            let msg = b"hello";
            let mut buf = [0u8; 5 + 16];

            let packet_number = 0u64;
            let header = &[];

            let key_phase = self.sealer.key_phase();
            self.sealer
                .encrypt(packet_number, header, Some(msg), &mut buf);

            assert_ne!(msg, &buf[..5]);

            let (payload, tag) = buf.split_at_mut(5);
            other
                .opener
                .decrypt_in_place(key_phase, packet_number, header, payload, tag)?;

            assert_eq!(msg, payload);

            Ok(())
        }
    }

    struct Control {
        sealer: seal::control::Stream,
        opener: open::control::Stream,
    }

    impl Control {
        fn new((sealer, opener): (seal::control::Stream, open::control::Stream)) -> Self {
            Self { sealer, opener }
        }

        fn send(&self, other: &Self) -> crate::crypto::open::Result {
            use crate::crypto::{open::Control as _, seal::Control as _};

            let msg = b"hello";
            let mut tag = [0u8; crate::packet::secret_control::TAG_LEN];

            self.sealer.sign(msg, &mut tag);

            other.opener.verify(msg, &tag)?;

            Ok(())
        }
    }

    #[test]
    fn application_pair() {
        bolero::check!()
            .with_type::<Pair>()
            .for_each(|input| input.check_app())
    }

    #[test]
    fn control_pair() {
        bolero::check!()
            .with_type::<Pair>()
            .for_each(|input| input.check_control())
    }
}