quinn-boring 0.1.0

BoringSSL crypto provider for quinn
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
use crate::error::{map_result, map_result_zero_is_success, Result};
use crate::macros::bounded_array;
use crate::secret::Secret;
use crate::suite::{CipherSuite, ID};
use crate::{Error, QuicVersion};
use boring_sys as bffi;
use bytes::BytesMut;
use quinn_proto::crypto;
use std::ffi::c_uint;
use std::fmt::{Debug, Formatter};
use std::mem;
use std::mem::MaybeUninit;
use std::result::Result as StdResult;

const SAMPLE_LEN: usize = 16; // 128-bits.

/// The maximum key size used by Quic algorithms.
const MAX_KEY_LEN: usize = 32;

/// The maximum nonce size used by Quic algorithms.
const MAX_NONCE_LEN: usize = 12;

/// The maximum tag size used by Quic algorithms.
const MAX_TAG_LEN: usize = 16;

bounded_array! {
    /// A buffer that can fit the largest key supported by Quic.
    pub(crate) struct Key(MAX_KEY_LEN),

    /// A buffer that can fit the largest nonce supported by Quic.
    pub(crate) struct Nonce(MAX_NONCE_LEN),

    /// A buffer that can fit the largest tag supported by Quic.
    pub(crate) struct Tag(MAX_TAG_LEN)
}

/// A pair of keys for bidirectional communication
#[derive(Copy, Clone, Debug)]
pub(crate) struct KeyPair<T> {
    /// The key for this side, used for encrypting data.
    pub(crate) local: T,

    /// The key for the other side, used for decrypting data.
    pub(crate) remote: T,
}

impl KeyPair<HeaderKey> {
    #[inline]
    pub(crate) fn as_crypto(&self) -> Result<crypto::KeyPair<Box<dyn crypto::HeaderKey>>> {
        Ok(crypto::KeyPair {
            local: self.local.as_crypto()?,
            remote: self.remote.as_crypto()?,
        })
    }
}

impl KeyPair<PacketKey> {
    #[inline]
    pub(crate) fn as_crypto(&self) -> Result<crypto::KeyPair<Box<dyn crypto::PacketKey>>> {
        Ok(crypto::KeyPair {
            local: Box::new(self.local),
            remote: Box::new(self.remote),
        })
    }
}

/// A complete set of keys for a certain encryption level.
#[derive(Clone, Debug)]
pub(crate) struct Keys {
    /// Header protection keys
    pub(crate) header: KeyPair<HeaderKey>,
    /// Packet protection keys
    pub(crate) packet: KeyPair<PacketKey>,
}

impl Keys {
    pub(crate) fn as_crypto(&self) -> Result<crypto::Keys> {
        Ok(crypto::Keys {
            header: self.header.as_crypto()?,
            packet: self.packet.as_crypto()?,
        })
    }
}

/// Internal header key representation. Supports conversion to [crypto::HeaderKey]
#[derive(Copy, Clone, Debug)]
pub(crate) struct HeaderKey {
    suite: &'static CipherSuite,
    key: Key,
}

impl HeaderKey {
    pub(crate) fn new(
        version: QuicVersion,
        suite: &'static CipherSuite,
        secret: &Secret,
    ) -> Result<Self> {
        let mut key = suite.aead.zero_key();
        suite
            .hkdf
            .expand_label(secret.slice(), version.header_key_label(), key.slice_mut())?;

        Ok(Self { suite, key })
    }

    #[inline]
    pub(crate) fn key(&self) -> &Key {
        &self.key
    }

    /// Converts to a crypto HeaderKey.
    #[inline]
    pub(crate) fn as_crypto(&self) -> Result<Box<dyn crypto::HeaderKey>> {
        match self.suite.id {
            ID::Aes128GcmSha256 | ID::Aes256GcmSha384 => {
                Ok(Box::new(AesHeaderKey::new(self.key())?))
            }
            ID::Chacha20Poly1305Sha256 => Ok(Box::new(ChaChaHeaderKey::new(self.key())?)),
        }
    }
}

/// Base trait for a crypto header protection keys. Implementation copied from rustls.
trait CryptoHeaderKey: crypto::HeaderKey {
    fn new_mask(&self, sample: &[u8]) -> Result<[u8; 5]>;

    #[inline]
    fn sample_len(&self) -> usize {
        SAMPLE_LEN
    }

    #[inline]
    fn decrypt_in_place(&self, pn_offset: usize, packet: &mut [u8]) {
        let (header, sample) = packet.split_at_mut(pn_offset + 4);
        let (first, rest) = header.split_at_mut(1);
        let pn_end = Ord::min(pn_offset + 3, rest.len());
        self.xor_in_place(
            &sample[..self.sample_len()],
            &mut first[0],
            &mut rest[pn_offset - 1..pn_end],
            true,
        )
        .unwrap();
    }

    #[inline]
    fn encrypt_in_place(&self, pn_offset: usize, packet: &mut [u8]) {
        let (header, sample) = packet.split_at_mut(pn_offset + 4);
        let (first, rest) = header.split_at_mut(1);
        let pn_end = Ord::min(pn_offset + 3, rest.len());
        self.xor_in_place(
            &sample[..self.sample_size()],
            &mut first[0],
            &mut rest[pn_offset - 1..pn_end],
            false,
        )
        .unwrap();
    }

    #[inline]
    fn xor_in_place(
        &self,
        sample: &[u8],
        first: &mut u8,
        packet_number: &mut [u8],
        masked: bool,
    ) -> Result<()> {
        // This implements [Header Protection Application] almost verbatim.

        let mask = self.new_mask(sample).unwrap();

        // The `unwrap()` will not panic because `new_mask` returns a
        // non-empty result.
        let (first_mask, pn_mask) = mask.split_first().unwrap();

        // It is OK for the `mask` to be longer than `packet_number`,
        // but a valid `packet_number` will never be longer than `mask`.
        if packet_number.len() > pn_mask.len() {
            return Err(Error::other(format!(
                "packet number too long: {}",
                packet_number.len()
            )));
        }

        // Infallible from this point on. Before this point, `first` and
        // `packet_number` are unchanged.

        const LONG_HEADER_FORM: u8 = 0x80;
        let bits = match *first & LONG_HEADER_FORM == LONG_HEADER_FORM {
            true => 0x0f,  // Long header: 4 bits masked
            false => 0x1f, // Short header: 5 bits masked
        };

        let first_plain = match masked {
            // When unmasking, use the packet length bits after unmasking
            true => *first ^ (first_mask & bits),
            // When masking, use the packet length bits before masking
            false => *first,
        };
        let pn_len = (first_plain & 0x03) as usize + 1;

        *first ^= first_mask & bits;
        for (dst, m) in packet_number.iter_mut().zip(pn_mask).take(pn_len) {
            *dst ^= m;
        }

        Ok(())
    }
}

/// A [CryptoHeaderKey] for AES ciphers.
struct AesHeaderKey(bffi::AES_KEY);

impl AesHeaderKey {
    fn new(key: &Key) -> Result<AesHeaderKey> {
        let hpk = unsafe {
            let mut hpk = MaybeUninit::uninit();

            // NOTE: this function breaks the usual return value convention.
            map_result_zero_is_success(bffi::AES_set_encrypt_key(
                key.as_ptr(),
                (key.len() * 8) as c_uint,
                hpk.as_mut_ptr(),
            ))?;

            hpk.assume_init()
        };
        Ok(Self(hpk))
    }
}

impl CryptoHeaderKey for AesHeaderKey {
    #[inline]
    fn new_mask(&self, sample: &[u8]) -> Result<[u8; 5]> {
        if sample.len() != SAMPLE_LEN {
            return Err(Error::invalid_input(format!(
                "invalid sample length: {}",
                sample.len()
            )));
        }

        let mut encrypted: [u8; SAMPLE_LEN] = [0; SAMPLE_LEN];
        unsafe {
            bffi::AES_encrypt(sample.as_ptr(), encrypted.as_mut_ptr(), &self.0);
        }

        let mut out: [u8; 5] = [0; 5];
        out.copy_from_slice(&encrypted[..5]);
        Ok(out)
    }
}

impl crypto::HeaderKey for AesHeaderKey {
    #[inline]
    fn decrypt(&self, pn_offset: usize, packet: &mut [u8]) {
        self.decrypt_in_place(pn_offset, packet)
    }

    #[inline]
    fn encrypt(&self, pn_offset: usize, packet: &mut [u8]) {
        self.encrypt_in_place(pn_offset, packet)
    }

    #[inline]
    fn sample_size(&self) -> usize {
        self.sample_len()
    }
}

/// A [CryptoHeaderKey] for ChaCha ciphers.
struct ChaChaHeaderKey(Key);

impl ChaChaHeaderKey {
    const ZEROS: [u8; 5] = [0; 5];

    fn new(key: &Key) -> Result<ChaChaHeaderKey> {
        Ok(Self(*key))
    }
}

impl CryptoHeaderKey for ChaChaHeaderKey {
    #[inline]
    fn new_mask(&self, sample: &[u8]) -> Result<[u8; 5]> {
        if sample.len() != SAMPLE_LEN {
            return Err(Error::invalid_input(format!(
                "sample len invalid: {}",
                sample.len()
            )));
        }

        // Extract the counter and the nonce from the sample.
        let (counter, nonce) = sample.split_at(mem::size_of::<u32>());
        let counter = u32::from_ne_bytes(counter.try_into().unwrap());

        let mut out: [u8; 5] = [0; 5];
        unsafe {
            bffi::CRYPTO_chacha_20(
                out.as_mut_ptr(),
                Self::ZEROS.as_ptr(),
                Self::ZEROS.len(),
                self.0.as_ptr(),
                nonce.as_ptr(),
                counter,
            );
        }

        Ok(out)
    }
}

impl crypto::HeaderKey for ChaChaHeaderKey {
    #[inline]
    fn decrypt(&self, pn_offset: usize, packet: &mut [u8]) {
        self.decrypt_in_place(pn_offset, packet)
    }

    #[inline]
    fn encrypt(&self, pn_offset: usize, packet: &mut [u8]) {
        self.encrypt_in_place(pn_offset, packet)
    }

    #[inline]
    fn sample_size(&self) -> usize {
        self.sample_len()
    }
}

/// Internal key representation.
#[derive(Copy, Clone, Debug)]
pub(crate) struct PacketKey {
    aead_key: AeadKey,
    iv: Nonce,
}

impl PacketKey {
    #[inline]
    pub(crate) fn new(
        version: QuicVersion,
        suite: &'static CipherSuite,
        secret: &Secret,
    ) -> Result<Self> {
        let mut key = suite.aead.zero_key();
        suite
            .hkdf
            .expand_label(secret.slice(), version.key_label(), key.slice_mut())?;

        let mut iv = suite.aead.zero_nonce();
        suite
            .hkdf
            .expand_label(secret.slice(), version.iv_label(), iv.slice_mut())?;

        let aead_key = AeadKey::new(suite, key)?;

        Ok(Self { aead_key, iv })
    }

    #[cfg(test)]
    pub(crate) fn key(&self) -> &Key {
        &self.aead_key.key
    }

    #[cfg(test)]
    pub(crate) fn iv(&self) -> &Nonce {
        &self.iv
    }

    #[inline]
    fn nonce_for_packet(&self, packet_number: u64) -> Nonce {
        let mut nonce = self.aead_key.suite.aead.zero_nonce();
        let slice = nonce.slice_mut();
        slice[4..].copy_from_slice(&packet_number.to_be_bytes());
        for (out, inp) in slice.iter_mut().zip(self.iv.slice().iter()) {
            *out ^= inp;
        }
        nonce
    }
}

impl crypto::PacketKey for PacketKey {
    /// Encrypt a QUIC packet in-place.
    fn encrypt(&self, packet_number: u64, buf: &mut [u8], header_len: usize) {
        let (header, payload_tag) = buf.split_at_mut(header_len);

        let nonce = self.nonce_for_packet(packet_number);

        self.aead_key
            .seal_in_place(&nonce, payload_tag, header)
            .unwrap();
    }

    /// Decrypt a QUIC packet in-place.
    fn decrypt(
        &self,
        packet_number: u64,
        header: &[u8],
        payload: &mut BytesMut,
    ) -> StdResult<(), crypto::CryptoError> {
        let nonce = self.nonce_for_packet(packet_number);

        let plain_len = self
            .aead_key
            .open_in_place(&nonce, payload.as_mut(), header)?;
        payload.truncate(plain_len);
        Ok(())
    }

    #[inline]
    fn tag_len(&self) -> usize {
        self.aead_key.suite.aead.tag_len
    }

    #[inline]
    fn confidentiality_limit(&self) -> u64 {
        self.aead_key.suite.confidentiality_limit
    }

    #[inline]
    fn integrity_limit(&self) -> u64 {
        self.aead_key.suite.integrity_limit
    }
}

/// A [crypto::PacketKey] that is based on a BoringSSL [bffi::EVP_AEAD_CTX].
#[derive(Copy, Clone)]
pub(crate) struct AeadKey {
    suite: &'static CipherSuite,
    key: Key,
    ctx: bffi::EVP_AEAD_CTX,
}

impl Debug for AeadKey {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AeadKey")
            .field("suite", self.suite)
            .field("key", &self.key)
            .finish()
    }
}

unsafe impl Send for AeadKey {}

impl AeadKey {
    #[inline]
    pub(crate) fn new(suite: &'static CipherSuite, key: Key) -> Result<Self> {
        let ctx = suite.aead.new_aead_ctx(&key)?;
        Ok(Self { suite, key, ctx })
    }

    #[inline]
    pub(crate) fn seal_in_place(
        &self,
        nonce: &Nonce,
        data: &mut [u8],
        additional_data: &[u8],
    ) -> Result<()> {
        let mut out_len = data.len() - self.suite.aead.tag_len;
        unsafe {
            map_result(bffi::EVP_AEAD_CTX_seal(
                &self.ctx,
                data.as_mut_ptr(),
                &mut out_len,
                data.len(),
                nonce.as_ptr(),
                nonce.len(),
                data.as_ptr(),
                out_len,
                additional_data.as_ptr(),
                additional_data.len(),
            ))?;
        }
        Ok(())
    }

    #[inline]
    pub(crate) fn open_in_place(
        &self,
        nonce: &Nonce,
        data: &mut [u8],
        additional_data: &[u8],
    ) -> StdResult<usize, crypto::CryptoError> {
        let mut out_len = match data.len().checked_sub(self.suite.aead.tag_len) {
            Some(n) => n,
            None => return Err(crypto::CryptoError {}),
        };

        unsafe {
            map_result(bffi::EVP_AEAD_CTX_open(
                &self.ctx,
                data.as_mut_ptr(),
                &mut out_len,
                out_len,
                nonce.as_ptr(),
                nonce.len(),
                data.as_ptr(),
                data.len(),
                additional_data.as_ptr(),
                additional_data.len(),
            ))?;
        }
        Ok(out_len)
    }
}

impl crypto::AeadKey for AeadKey {
    #[inline]
    fn seal(
        &self,
        data: &mut Vec<u8>,
        additional_data: &[u8],
    ) -> StdResult<(), crypto::CryptoError> {
        data.extend_from_slice(self.suite.aead.zero_tag().slice());
        self.seal_in_place(&self.suite.aead.zero_nonce(), data, additional_data)?;
        Ok(())
    }

    #[inline]
    fn open<'a>(
        &self,
        data: &'a mut [u8],
        additional_data: &[u8],
    ) -> StdResult<&'a mut [u8], crypto::CryptoError> {
        let plain_len = self.open_in_place(&self.suite.aead.zero_nonce(), data, additional_data)?;
        Ok(&mut data[..plain_len])
    }
}