rwebtransport 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/Cargo.lock +153 -226
  2. package/Cargo.toml +5 -5
  3. package/README.md +4 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +1 -1
  6. package/crates/native/src/config.rs +13 -1
  7. package/crates/native/src/driver.rs +249 -91
  8. package/crates/native/src/lib.rs +9 -10
  9. package/crates/native/src/parser_memory.rs +1 -0
  10. package/crates/native/src/server.rs +197 -182
  11. package/crates/native/src/session.rs +329 -177
  12. package/crates/native/src/test_support.rs +2 -2
  13. package/crates/wtcore/src/h3.rs +166 -10
  14. package/dist/index.js +92 -45
  15. package/dist/index.js.map +2 -2
  16. package/dist/index.mjs +92 -45
  17. package/dist/index.mjs.map +2 -2
  18. package/dist/native.d.ts +9 -4
  19. package/package.json +14 -11
  20. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  21. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  23. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  24. package/scripts/build-linux-prebuilt.sh +46 -0
  25. package/scripts/build.js +10 -3
  26. package/vendor/quiche/AGENTS.md +10 -0
  27. package/vendor/quiche/Cargo.lock +239 -248
  28. package/vendor/quiche/Cargo.toml +23 -10
  29. package/vendor/quiche/examples/Makefile +4 -4
  30. package/vendor/quiche/examples/client.rs +10 -28
  31. package/vendor/quiche/examples/http3-client.rs +20 -34
  32. package/vendor/quiche/examples/http3-server.rs +47 -78
  33. package/vendor/quiche/examples/server.rs +19 -49
  34. package/vendor/quiche/include/quiche.h +35 -4
  35. package/vendor/quiche/src/build.rs +17 -0
  36. package/vendor/quiche/src/cid.rs +129 -61
  37. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  38. package/vendor/quiche/src/crypto/mod.rs +64 -77
  39. package/vendor/quiche/src/dgram.rs +2 -3
  40. package/vendor/quiche/src/error.rs +22 -20
  41. package/vendor/quiche/src/ffi.rs +558 -280
  42. package/vendor/quiche/src/flowcontrol.rs +0 -7
  43. package/vendor/quiche/src/frame.rs +134 -135
  44. package/vendor/quiche/src/h3/ffi.rs +73 -60
  45. package/vendor/quiche/src/h3/frame.rs +70 -90
  46. package/vendor/quiche/src/h3/mod.rs +1024 -593
  47. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  48. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  49. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  50. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  51. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  52. package/vendor/quiche/src/h3/stream.rs +1009 -194
  53. package/vendor/quiche/src/lib.rs +1346 -1195
  54. package/vendor/quiche/src/minmax.rs +2 -6
  55. package/vendor/quiche/src/packet.rs +420 -552
  56. package/vendor/quiche/src/path.rs +202 -72
  57. package/vendor/quiche/src/pmtud.rs +18 -16
  58. package/vendor/quiche/src/ranges.rs +184 -82
  59. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  60. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  61. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  62. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  63. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  64. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  65. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  66. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  67. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  68. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  69. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  70. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  71. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  72. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  73. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  74. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  84. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  85. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  86. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  87. package/vendor/quiche/src/recovery/mod.rs +414 -173
  88. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  89. package/vendor/quiche/src/stream/mod.rs +864 -133
  90. package/vendor/quiche/src/stream/recv_buf.rs +471 -172
  91. package/vendor/quiche/src/stream/send_buf.rs +338 -81
  92. package/vendor/quiche/src/test_utils.rs +78 -64
  93. package/vendor/quiche/src/tests.rs +2559 -1335
  94. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  95. package/vendor/quiche/src/tls/mod.rs +146 -193
  96. package/vendor/quiche/src/transport_params.rs +87 -103
@@ -4,19 +4,16 @@ use std::convert::TryFrom;
4
4
 
5
5
  use std::mem::MaybeUninit;
6
6
 
7
+ use std::ptr::NonNull;
8
+
7
9
  use libc::c_int;
8
10
  use libc::c_uint;
9
11
  use libc::c_void;
10
12
 
11
- // NOTE: This structure is copied from <openssl/aead.h> in order to be able to
12
- // statically allocate it. While it is not often modified upstream, it needs to
13
- // be kept in sync.
14
- #[repr(C)]
13
+ #[allow(non_camel_case_types)]
14
+ #[repr(transparent)]
15
15
  struct EVP_AEAD_CTX {
16
- aead: libc::uintptr_t,
17
- opaque: [u8; 580],
18
- alignment: u64,
19
- tag_len: u8,
16
+ _unused: c_void,
20
17
  }
21
18
 
22
19
  #[derive(Clone)]
@@ -31,9 +28,7 @@ impl Algorithm {
31
28
  match self {
32
29
  Algorithm::AES128_GCM => unsafe { EVP_aead_aes_128_gcm_tls13() },
33
30
  Algorithm::AES256_GCM => unsafe { EVP_aead_aes_256_gcm_tls13() },
34
- Algorithm::ChaCha20_Poly1305 => unsafe {
35
- EVP_aead_chacha20_poly1305()
36
- },
31
+ Algorithm::ChaCha20_Poly1305 => unsafe { EVP_aead_chacha20_poly1305() },
37
32
  }
38
33
  }
39
34
  }
@@ -41,15 +36,13 @@ impl Algorithm {
41
36
  pub(crate) struct PacketKey {
42
37
  alg: Algorithm,
43
38
 
44
- ctx: EVP_AEAD_CTX,
39
+ ctx: NonNull<EVP_AEAD_CTX>,
45
40
 
46
41
  nonce: Vec<u8>,
47
42
  }
48
43
 
49
44
  impl PacketKey {
50
- pub fn new(
51
- alg: Algorithm, key: Vec<u8>, iv: Vec<u8>, _enc: u32,
52
- ) -> Result<Self> {
45
+ pub fn new(alg: Algorithm, key: Vec<u8>, iv: Vec<u8>, _enc: u32) -> Result<Self> {
53
46
  Ok(Self {
54
47
  alg,
55
48
  ctx: make_aead_ctx(alg, &key)?,
@@ -80,9 +73,7 @@ impl PacketKey {
80
73
  Ok(pkt_key)
81
74
  }
82
75
 
83
- pub fn open_with_u64_counter(
84
- &self, counter: u64, ad: &[u8], buf: &mut [u8],
85
- ) -> Result<usize> {
76
+ pub fn open_with_u64_counter(&self, counter: u64, ad: &[u8], buf: &mut [u8]) -> Result<usize> {
86
77
  let tag_len = self.alg.tag_len();
87
78
 
88
79
  let mut out_len = match buf.len().checked_sub(tag_len) {
@@ -96,7 +87,7 @@ impl PacketKey {
96
87
 
97
88
  let rc = unsafe {
98
89
  EVP_AEAD_CTX_open(
99
- &self.ctx, // ctx
90
+ self.ctx.as_ptr(), // ctx
100
91
  buf.as_mut_ptr(), // out
101
92
  &mut out_len, // out_len
102
93
  max_out_len, // max_out_len
@@ -117,7 +108,11 @@ impl PacketKey {
117
108
  }
118
109
 
119
110
  pub fn seal_with_u64_counter(
120
- &mut self, counter: u64, ad: &[u8], buf: &mut [u8], in_len: usize,
111
+ &mut self,
112
+ counter: u64,
113
+ ad: &[u8],
114
+ buf: &mut [u8],
115
+ in_len: usize,
121
116
  extra_in: Option<&[u8]>,
122
117
  ) -> Result<usize> {
123
118
  let tag_len = self.alg.tag_len();
@@ -139,7 +134,7 @@ impl PacketKey {
139
134
 
140
135
  let rc = unsafe {
141
136
  EVP_AEAD_CTX_seal_scatter(
142
- &mut self.ctx, // ctx
137
+ self.ctx.as_ptr(), // ctx
143
138
  buf.as_mut_ptr(), // out
144
139
  buf[in_len..].as_mut_ptr(), // out_tag
145
140
  &mut out_tag_len, // out_tag_len
@@ -163,6 +158,17 @@ impl PacketKey {
163
158
  }
164
159
  }
165
160
 
161
+ // PacketKey uniquely owns the context. Its API requires exclusive access for
162
+ // stateful seal operations, while open operations support shared access.
163
+ unsafe impl Send for PacketKey {}
164
+ unsafe impl Sync for PacketKey {}
165
+
166
+ impl Drop for PacketKey {
167
+ fn drop(&mut self) {
168
+ unsafe { EVP_AEAD_CTX_free(self.ctx.as_ptr()) }
169
+ }
170
+ }
171
+
166
172
  #[derive(Clone)]
167
173
  #[allow(clippy::large_enum_variant)]
168
174
  pub(crate) enum HeaderProtectionKey {
@@ -179,11 +185,7 @@ impl HeaderProtectionKey {
179
185
 
180
186
  let mut aes_key = MaybeUninit::<AES_KEY>::uninit();
181
187
 
182
- let rc = AES_set_encrypt_key(
183
- hp_key.as_ptr(),
184
- key_len_bits,
185
- aes_key.as_mut_ptr(),
186
- );
188
+ let rc = AES_set_encrypt_key(hp_key.as_ptr(), key_len_bits, aes_key.as_mut_ptr());
187
189
 
188
190
  if rc != 0 {
189
191
  return Err(Error::CryptoFail);
@@ -202,34 +204,23 @@ impl HeaderProtectionKey {
202
204
  Self::Aes(aes_key) => {
203
205
  let mut block = [0_u8; 16];
204
206
 
205
- unsafe {
206
- AES_ecb_encrypt(
207
- sample.as_ptr(),
208
- block.as_mut_ptr(),
209
- aes_key as _,
210
- 1,
211
- )
212
- };
207
+ unsafe { AES_ecb_encrypt(sample.as_ptr(), block.as_mut_ptr(), aes_key as _, 1) };
213
208
 
214
209
  // Downsize the encrypted block to the size of the header
215
210
  // protection mask.
216
211
  //
217
212
  // The length of the slice will always match the size of
218
213
  // `HeaderProtectionMask` so the `unwrap()` is safe.
219
- let new_mask =
220
- HeaderProtectionMask::try_from(&block[..HP_MASK_LEN])
221
- .unwrap();
214
+ let new_mask = HeaderProtectionMask::try_from(&block[..HP_MASK_LEN]).unwrap();
222
215
  Ok(new_mask)
223
- },
216
+ }
224
217
 
225
218
  Self::ChaCha(key) => {
226
219
  const PLAINTEXT: &[u8; HP_MASK_LEN] = &[0_u8; HP_MASK_LEN];
227
220
 
228
221
  let mut new_mask = HeaderProtectionMask::default();
229
222
 
230
- let counter = u32::from_le_bytes([
231
- sample[0], sample[1], sample[2], sample[3],
232
- ]);
223
+ let counter = u32::from_le_bytes([sample[0], sample[1], sample[2], sample[3]]);
233
224
 
234
225
  unsafe {
235
226
  CRYPTO_chacha_20(
@@ -243,38 +234,26 @@ impl HeaderProtectionKey {
243
234
  };
244
235
 
245
236
  Ok(new_mask)
246
- },
237
+ }
247
238
  }
248
239
  }
249
240
  }
250
241
 
251
- fn make_aead_ctx(alg: Algorithm, key: &[u8]) -> Result<EVP_AEAD_CTX> {
252
- let mut ctx = MaybeUninit::uninit();
253
-
242
+ fn make_aead_ctx(alg: Algorithm, key: &[u8]) -> Result<NonNull<EVP_AEAD_CTX>> {
254
243
  let ctx = unsafe {
255
244
  let aead = alg.get_evp_aead();
256
245
 
257
- let rc = EVP_AEAD_CTX_init(
258
- ctx.as_mut_ptr(),
259
- aead,
260
- key.as_ptr(),
261
- alg.key_len(),
262
- alg.tag_len(),
263
- std::ptr::null_mut(),
264
- );
265
-
266
- if rc != 1 {
267
- return Err(Error::CryptoFail);
268
- }
269
-
270
- ctx.assume_init()
246
+ EVP_AEAD_CTX_new(aead, key.as_ptr(), alg.key_len(), alg.tag_len())
271
247
  };
272
248
 
273
- Ok(ctx)
249
+ NonNull::new(ctx).ok_or(Error::CryptoFail)
274
250
  }
275
251
 
276
252
  pub(crate) fn hkdf_extract(
277
- alg: Algorithm, out: &mut [u8], secret: &[u8], salt: &[u8],
253
+ alg: Algorithm,
254
+ out: &mut [u8],
255
+ secret: &[u8],
256
+ salt: &[u8],
278
257
  ) -> Result<()> {
279
258
  let mut out_len = out.len();
280
259
 
@@ -298,7 +277,10 @@ pub(crate) fn hkdf_extract(
298
277
  }
299
278
 
300
279
  pub(crate) fn hkdf_expand(
301
- alg: Algorithm, out: &mut [u8], secret: &[u8], info: &[u8],
280
+ alg: Algorithm,
281
+ out: &mut [u8],
282
+ secret: &[u8],
283
+ info: &[u8],
302
284
  ) -> Result<()> {
303
285
  let rc = unsafe {
304
286
  HKDF_expand(
@@ -328,46 +310,76 @@ extern "C" {
328
310
 
329
311
  // HKDF
330
312
  fn HKDF_extract(
331
- out_key: *mut u8, out_len: *mut usize, digest: *const EVP_MD,
332
- secret: *const u8, secret_len: usize, salt: *const u8, salt_len: usize,
313
+ out_key: *mut u8,
314
+ out_len: *mut usize,
315
+ digest: *const EVP_MD,
316
+ secret: *const u8,
317
+ secret_len: usize,
318
+ salt: *const u8,
319
+ salt_len: usize,
333
320
  ) -> c_int;
334
321
 
335
322
  fn HKDF_expand(
336
- out_key: *mut u8, out_len: usize, digest: *const EVP_MD, prk: *const u8,
337
- prk_len: usize, info: *const u8, info_len: usize,
323
+ out_key: *mut u8,
324
+ out_len: usize,
325
+ digest: *const EVP_MD,
326
+ prk: *const u8,
327
+ prk_len: usize,
328
+ info: *const u8,
329
+ info_len: usize,
338
330
  ) -> c_int;
339
331
 
340
332
  // EVP_AEAD_CTX
341
- fn EVP_AEAD_CTX_init(
342
- ctx: *mut EVP_AEAD_CTX, aead: *const EVP_AEAD, key: *const u8,
343
- key_len: usize, tag_len: usize, engine: *mut c_void,
344
- ) -> c_int;
333
+ fn EVP_AEAD_CTX_new(
334
+ aead: *const EVP_AEAD,
335
+ key: *const u8,
336
+ key_len: usize,
337
+ tag_len: usize,
338
+ ) -> *mut EVP_AEAD_CTX;
339
+
340
+ fn EVP_AEAD_CTX_free(ctx: *mut EVP_AEAD_CTX);
345
341
 
346
342
  fn EVP_AEAD_CTX_open(
347
- ctx: *const EVP_AEAD_CTX, out: *mut u8, out_len: *mut usize,
348
- max_out_len: usize, nonce: *const u8, nonce_len: usize, inp: *const u8,
349
- in_len: usize, ad: *const u8, ad_len: usize,
343
+ ctx: *const EVP_AEAD_CTX,
344
+ out: *mut u8,
345
+ out_len: *mut usize,
346
+ max_out_len: usize,
347
+ nonce: *const u8,
348
+ nonce_len: usize,
349
+ inp: *const u8,
350
+ in_len: usize,
351
+ ad: *const u8,
352
+ ad_len: usize,
350
353
  ) -> c_int;
351
354
 
352
355
  fn EVP_AEAD_CTX_seal_scatter(
353
- ctx: *mut EVP_AEAD_CTX, out: *mut u8, out_tag: *mut u8,
354
- out_tag_len: *mut usize, max_out_tag_len: usize, nonce: *const u8,
355
- nonce_len: usize, inp: *const u8, in_len: usize, extra_in: *const u8,
356
- extra_in_len: usize, ad: *const u8, ad_len: usize,
356
+ ctx: *mut EVP_AEAD_CTX,
357
+ out: *mut u8,
358
+ out_tag: *mut u8,
359
+ out_tag_len: *mut usize,
360
+ max_out_tag_len: usize,
361
+ nonce: *const u8,
362
+ nonce_len: usize,
363
+ inp: *const u8,
364
+ in_len: usize,
365
+ extra_in: *const u8,
366
+ extra_in_len: usize,
367
+ ad: *const u8,
368
+ ad_len: usize,
357
369
  ) -> c_int;
358
370
 
359
371
  // AES
360
- fn AES_set_encrypt_key(
361
- key: *const u8, bits: c_uint, aeskey: *mut AES_KEY,
362
- ) -> c_int;
372
+ fn AES_set_encrypt_key(key: *const u8, bits: c_uint, aeskey: *mut AES_KEY) -> c_int;
363
373
 
364
- fn AES_ecb_encrypt(
365
- inp: *const u8, out: *mut u8, key: *const AES_KEY, enc: c_int,
366
- ) -> c_void;
374
+ fn AES_ecb_encrypt(inp: *const u8, out: *mut u8, key: *const AES_KEY, enc: c_int);
367
375
 
368
376
  // ChaCha20
369
377
  fn CRYPTO_chacha_20(
370
- out: *mut u8, inp: *const u8, in_len: usize, key: *const u8,
371
- nonce: *const u8, counter: u32,
372
- ) -> c_void;
378
+ out: *mut u8,
379
+ inp: *const u8,
380
+ in_len: usize,
381
+ key: *const u8,
382
+ nonce: *const u8,
383
+ counter: u32,
384
+ );
373
385
  }
@@ -41,10 +41,10 @@ pub const HP_MASK_LEN: usize = 5;
41
41
  #[repr(C)]
42
42
  #[derive(Clone, Copy, Debug, PartialEq, Eq)]
43
43
  pub enum Level {
44
- Initial = 0,
45
- ZeroRTT = 1,
44
+ Initial = 0,
45
+ ZeroRTT = 1,
46
46
  Handshake = 2,
47
- OneRTT = 3,
47
+ OneRTT = 3,
48
48
  }
49
49
 
50
50
  impl Level {
@@ -142,7 +142,10 @@ impl Open {
142
142
  pub const DECRYPT: u32 = 0;
143
143
 
144
144
  pub fn new(
145
- alg: Algorithm, key: Vec<u8>, iv: Vec<u8>, hp_key: Vec<u8>,
145
+ alg: Algorithm,
146
+ key: Vec<u8>,
147
+ iv: Vec<u8>,
148
+ hp_key: Vec<u8>,
146
149
  secret: Vec<u8>,
147
150
  ) -> Result<Open> {
148
151
  Ok(Open {
@@ -183,8 +186,7 @@ impl Open {
183
186
  pub fn derive_next_packet_key(&self) -> Result<Open> {
184
187
  let next_secret = derive_next_secret(self.alg, &self.secret)?;
185
188
 
186
- let next_packet_key =
187
- PacketKey::from_secret(self.alg, &next_secret, Self::DECRYPT)?;
189
+ let next_packet_key = PacketKey::from_secret(self.alg, &next_secret, Self::DECRYPT)?;
188
190
 
189
191
  Ok(Open {
190
192
  alg: self.alg,
@@ -197,9 +199,7 @@ impl Open {
197
199
  })
198
200
  }
199
201
 
200
- pub fn open_with_u64_counter(
201
- &self, counter: u64, ad: &[u8], buf: &mut [u8],
202
- ) -> Result<usize> {
202
+ pub fn open_with_u64_counter(&self, counter: u64, ad: &[u8], buf: &mut [u8]) -> Result<usize> {
203
203
  if cfg!(feature = "fuzzing") {
204
204
  let tag_len = self.alg.tag_len();
205
205
  let out_len = match buf.len().checked_sub(tag_len) {
@@ -233,7 +233,10 @@ impl Seal {
233
233
  pub const ENCRYPT: u32 = 1;
234
234
 
235
235
  pub fn new(
236
- alg: Algorithm, key: Vec<u8>, iv: Vec<u8>, hp_key: Vec<u8>,
236
+ alg: Algorithm,
237
+ key: Vec<u8>,
238
+ iv: Vec<u8>,
239
+ hp_key: Vec<u8>,
237
240
  secret: Vec<u8>,
238
241
  ) -> Result<Seal> {
239
242
  Ok(Seal {
@@ -274,8 +277,7 @@ impl Seal {
274
277
  pub fn derive_next_packet_key(&self) -> Result<Seal> {
275
278
  let next_secret = derive_next_secret(self.alg, &self.secret)?;
276
279
 
277
- let next_packet_key =
278
- PacketKey::from_secret(self.alg, &next_secret, Self::ENCRYPT)?;
280
+ let next_packet_key = PacketKey::from_secret(self.alg, &next_secret, Self::ENCRYPT)?;
279
281
 
280
282
  Ok(Seal {
281
283
  alg: self.alg,
@@ -289,7 +291,11 @@ impl Seal {
289
291
  }
290
292
 
291
293
  pub fn seal_with_u64_counter(
292
- &mut self, counter: u64, ad: &[u8], buf: &mut [u8], in_len: usize,
294
+ &mut self,
295
+ counter: u64,
296
+ ad: &[u8],
297
+ buf: &mut [u8],
298
+ in_len: usize,
293
299
  extra_in: Option<&[u8]>,
294
300
  ) -> Result<usize> {
295
301
  if cfg!(feature = "fuzzing") {
@@ -327,7 +333,10 @@ impl HeaderProtectionKey {
327
333
  }
328
334
 
329
335
  pub fn derive_initial_key_material(
330
- cid: &[u8], version: u32, is_server: bool, did_reset: bool,
336
+ cid: &[u8],
337
+ version: u32,
338
+ is_server: bool,
339
+ did_reset: bool,
331
340
  ) -> Result<(Open, Seal)> {
332
341
  let mut initial_secret = [0; 32];
333
342
  let mut client_secret = vec![0; 32];
@@ -397,12 +406,10 @@ pub fn derive_initial_key_material(
397
406
  Ok((open, seal))
398
407
  }
399
408
 
400
- fn derive_initial_secret(
401
- secret: &[u8], version: u32, out_prk: &mut [u8],
402
- ) -> Result<()> {
409
+ fn derive_initial_secret(secret: &[u8], version: u32, out_prk: &mut [u8]) -> Result<()> {
403
410
  const INITIAL_SALT_V1: [u8; 20] = [
404
- 0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17, 0x9a, 0xe6,
405
- 0xa4, 0xc8, 0x0c, 0xad, 0xcc, 0xbb, 0x7f, 0x0a,
411
+ 0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17, 0x9a, 0xe6, 0xa4, 0xc8, 0x0c,
412
+ 0xad, 0xcc, 0xbb, 0x7f, 0x0a,
406
413
  ];
407
414
 
408
415
  let salt = match version {
@@ -414,16 +421,12 @@ fn derive_initial_secret(
414
421
  hkdf_extract(Algorithm::AES128_GCM, out_prk, secret, salt)
415
422
  }
416
423
 
417
- fn derive_client_initial_secret(
418
- aead: Algorithm, prk: &[u8], out: &mut [u8],
419
- ) -> Result<()> {
424
+ fn derive_client_initial_secret(aead: Algorithm, prk: &[u8], out: &mut [u8]) -> Result<()> {
420
425
  const LABEL: &[u8] = b"client in";
421
426
  hkdf_expand_label(aead, prk, LABEL, out)
422
427
  }
423
428
 
424
- fn derive_server_initial_secret(
425
- aead: Algorithm, prk: &[u8], out: &mut [u8],
426
- ) -> Result<()> {
429
+ fn derive_server_initial_secret(aead: Algorithm, prk: &[u8], out: &mut [u8]) -> Result<()> {
427
430
  const LABEL: &[u8] = b"server in";
428
431
  hkdf_expand_label(aead, prk, LABEL, out)
429
432
  }
@@ -438,9 +441,7 @@ fn derive_next_secret(aead: Algorithm, secret: &[u8]) -> Result<Vec<u8>> {
438
441
  Ok(next_secret)
439
442
  }
440
443
 
441
- pub fn derive_hdr_key(
442
- aead: Algorithm, secret: &[u8], out: &mut [u8],
443
- ) -> Result<()> {
444
+ pub fn derive_hdr_key(aead: Algorithm, secret: &[u8], out: &mut [u8]) -> Result<()> {
444
445
  const LABEL: &[u8] = b"quic hp";
445
446
 
446
447
  let key_len = aead.key_len();
@@ -476,9 +477,7 @@ pub fn derive_pkt_iv(aead: Algorithm, prk: &[u8], out: &mut [u8]) -> Result<()>
476
477
  hkdf_expand_label(aead, prk, LABEL, &mut out[..nonce_len])
477
478
  }
478
479
 
479
- fn hkdf_expand_label(
480
- alg: Algorithm, prk: &[u8], label: &[u8], out: &mut [u8],
481
- ) -> Result<()> {
480
+ fn hkdf_expand_label(alg: Algorithm, prk: &[u8], label: &[u8], out: &mut [u8]) -> Result<()> {
482
481
  const LABEL_PREFIX: &[u8] = b"tls13 ";
483
482
 
484
483
  let out_len = (out.len() as u16).to_be_bytes();
@@ -545,77 +544,66 @@ mod tests {
545
544
 
546
545
  let aead = Algorithm::AES128_GCM;
547
546
 
548
- assert!(derive_initial_secret(
549
- &dcid,
550
- crate::PROTOCOL_VERSION_V1,
551
- &mut initial_secret,
552
- )
553
- .is_ok());
554
-
555
- // Client.
556
547
  assert!(
557
- derive_client_initial_secret(aead, &initial_secret, &mut secret)
558
- .is_ok()
548
+ derive_initial_secret(&dcid, crate::PROTOCOL_VERSION_V1, &mut initial_secret,).is_ok()
559
549
  );
550
+
551
+ // Client.
552
+ assert!(derive_client_initial_secret(aead, &initial_secret, &mut secret).is_ok());
560
553
  let expected_client_initial_secret = [
561
- 0xc0, 0x0c, 0xf1, 0x51, 0xca, 0x5b, 0xe0, 0x75, 0xed, 0x0e, 0xbf,
562
- 0xb5, 0xc8, 0x03, 0x23, 0xc4, 0x2d, 0x6b, 0x7d, 0xb6, 0x78, 0x81,
563
- 0x28, 0x9a, 0xf4, 0x00, 0x8f, 0x1f, 0x6c, 0x35, 0x7a, 0xea,
554
+ 0xc0, 0x0c, 0xf1, 0x51, 0xca, 0x5b, 0xe0, 0x75, 0xed, 0x0e, 0xbf, 0xb5, 0xc8, 0x03,
555
+ 0x23, 0xc4, 0x2d, 0x6b, 0x7d, 0xb6, 0x78, 0x81, 0x28, 0x9a, 0xf4, 0x00, 0x8f, 0x1f,
556
+ 0x6c, 0x35, 0x7a, 0xea,
564
557
  ];
565
558
  assert_eq!(&secret, &expected_client_initial_secret);
566
559
 
567
560
  assert!(derive_pkt_key(aead, &secret, &mut pkt_key).is_ok());
568
561
  let expected_client_pkt_key = [
569
- 0x1f, 0x36, 0x96, 0x13, 0xdd, 0x76, 0xd5, 0x46, 0x77, 0x30, 0xef,
570
- 0xcb, 0xe3, 0xb1, 0xa2, 0x2d,
562
+ 0x1f, 0x36, 0x96, 0x13, 0xdd, 0x76, 0xd5, 0x46, 0x77, 0x30, 0xef, 0xcb, 0xe3, 0xb1,
563
+ 0xa2, 0x2d,
571
564
  ];
572
565
  assert_eq!(&pkt_key, &expected_client_pkt_key);
573
566
 
574
567
  assert!(derive_pkt_iv(aead, &secret, &mut pkt_iv).is_ok());
575
568
  let expected_client_pkt_iv = [
576
- 0xfa, 0x04, 0x4b, 0x2f, 0x42, 0xa3, 0xfd, 0x3b, 0x46, 0xfb, 0x25,
577
- 0x5c,
569
+ 0xfa, 0x04, 0x4b, 0x2f, 0x42, 0xa3, 0xfd, 0x3b, 0x46, 0xfb, 0x25, 0x5c,
578
570
  ];
579
571
  assert_eq!(&pkt_iv, &expected_client_pkt_iv);
580
572
 
581
573
  assert!(derive_hdr_key(aead, &secret, &mut hdr_key).is_ok());
582
574
  let expected_client_hdr_key = [
583
- 0x9f, 0x50, 0x44, 0x9e, 0x04, 0xa0, 0xe8, 0x10, 0x28, 0x3a, 0x1e,
584
- 0x99, 0x33, 0xad, 0xed, 0xd2,
575
+ 0x9f, 0x50, 0x44, 0x9e, 0x04, 0xa0, 0xe8, 0x10, 0x28, 0x3a, 0x1e, 0x99, 0x33, 0xad,
576
+ 0xed, 0xd2,
585
577
  ];
586
578
  assert_eq!(&hdr_key, &expected_client_hdr_key);
587
579
 
588
580
  // Server.
589
- assert!(
590
- derive_server_initial_secret(aead, &initial_secret, &mut secret)
591
- .is_ok()
592
- );
581
+ assert!(derive_server_initial_secret(aead, &initial_secret, &mut secret).is_ok());
593
582
 
594
583
  let expected_server_initial_secret = [
595
- 0x3c, 0x19, 0x98, 0x28, 0xfd, 0x13, 0x9e, 0xfd, 0x21, 0x6c, 0x15,
596
- 0x5a, 0xd8, 0x44, 0xcc, 0x81, 0xfb, 0x82, 0xfa, 0x8d, 0x74, 0x46,
597
- 0xfa, 0x7d, 0x78, 0xbe, 0x80, 0x3a, 0xcd, 0xda, 0x95, 0x1b,
584
+ 0x3c, 0x19, 0x98, 0x28, 0xfd, 0x13, 0x9e, 0xfd, 0x21, 0x6c, 0x15, 0x5a, 0xd8, 0x44,
585
+ 0xcc, 0x81, 0xfb, 0x82, 0xfa, 0x8d, 0x74, 0x46, 0xfa, 0x7d, 0x78, 0xbe, 0x80, 0x3a,
586
+ 0xcd, 0xda, 0x95, 0x1b,
598
587
  ];
599
588
  assert_eq!(&secret, &expected_server_initial_secret);
600
589
 
601
590
  assert!(derive_pkt_key(aead, &secret, &mut pkt_key).is_ok());
602
591
  let expected_server_pkt_key = [
603
- 0xcf, 0x3a, 0x53, 0x31, 0x65, 0x3c, 0x36, 0x4c, 0x88, 0xf0, 0xf3,
604
- 0x79, 0xb6, 0x06, 0x7e, 0x37,
592
+ 0xcf, 0x3a, 0x53, 0x31, 0x65, 0x3c, 0x36, 0x4c, 0x88, 0xf0, 0xf3, 0x79, 0xb6, 0x06,
593
+ 0x7e, 0x37,
605
594
  ];
606
595
  assert_eq!(&pkt_key, &expected_server_pkt_key);
607
596
 
608
597
  assert!(derive_pkt_iv(aead, &secret, &mut pkt_iv).is_ok());
609
598
  let expected_server_pkt_iv = [
610
- 0x0a, 0xc1, 0x49, 0x3c, 0xa1, 0x90, 0x58, 0x53, 0xb0, 0xbb, 0xa0,
611
- 0x3e,
599
+ 0x0a, 0xc1, 0x49, 0x3c, 0xa1, 0x90, 0x58, 0x53, 0xb0, 0xbb, 0xa0, 0x3e,
612
600
  ];
613
601
  assert_eq!(&pkt_iv, &expected_server_pkt_iv);
614
602
 
615
603
  assert!(derive_hdr_key(aead, &secret, &mut hdr_key).is_ok());
616
604
  let expected_server_hdr_key = [
617
- 0xc2, 0x06, 0xb8, 0xd9, 0xb9, 0xf0, 0xf3, 0x76, 0x44, 0x43, 0x0b,
618
- 0x49, 0x0e, 0xea, 0xa3, 0x14,
605
+ 0xc2, 0x06, 0xb8, 0xd9, 0xb9, 0xf0, 0xf3, 0x76, 0x44, 0x43, 0x0b, 0x49, 0x0e, 0xea,
606
+ 0xa3, 0x14,
619
607
  ];
620
608
  assert_eq!(&hdr_key, &expected_server_hdr_key);
621
609
  }
@@ -623,9 +611,9 @@ mod tests {
623
611
  #[test]
624
612
  fn derive_chacha20_secrets() {
625
613
  let secret = [
626
- 0x9a, 0xc3, 0x12, 0xa7, 0xf8, 0x77, 0x46, 0x8e, 0xbe, 0x69, 0x42,
627
- 0x27, 0x48, 0xad, 0x00, 0xa1, 0x54, 0x43, 0xf1, 0x82, 0x03, 0xa0,
628
- 0x7d, 0x60, 0x60, 0xf6, 0x88, 0xf3, 0x0f, 0x21, 0x63, 0x2b,
614
+ 0x9a, 0xc3, 0x12, 0xa7, 0xf8, 0x77, 0x46, 0x8e, 0xbe, 0x69, 0x42, 0x27, 0x48, 0xad,
615
+ 0x00, 0xa1, 0x54, 0x43, 0xf1, 0x82, 0x03, 0xa0, 0x7d, 0x60, 0x60, 0xf6, 0x88, 0xf3,
616
+ 0x0f, 0x21, 0x63, 0x2b,
629
617
  ];
630
618
 
631
619
  let aead = Algorithm::ChaCha20_Poly1305;
@@ -636,32 +624,31 @@ mod tests {
636
624
 
637
625
  assert!(derive_pkt_key(aead, &secret, &mut pkt_key).is_ok());
638
626
  let expected_pkt_key = [
639
- 0xc6, 0xd9, 0x8f, 0xf3, 0x44, 0x1c, 0x3f, 0xe1, 0xb2, 0x18, 0x20,
640
- 0x94, 0xf6, 0x9c, 0xaa, 0x2e, 0xd4, 0xb7, 0x16, 0xb6, 0x54, 0x88,
641
- 0x96, 0x0a, 0x7a, 0x98, 0x49, 0x79, 0xfb, 0x23, 0xe1, 0xc8,
627
+ 0xc6, 0xd9, 0x8f, 0xf3, 0x44, 0x1c, 0x3f, 0xe1, 0xb2, 0x18, 0x20, 0x94, 0xf6, 0x9c,
628
+ 0xaa, 0x2e, 0xd4, 0xb7, 0x16, 0xb6, 0x54, 0x88, 0x96, 0x0a, 0x7a, 0x98, 0x49, 0x79,
629
+ 0xfb, 0x23, 0xe1, 0xc8,
642
630
  ];
643
631
  assert_eq!(&pkt_key, &expected_pkt_key);
644
632
 
645
633
  assert!(derive_pkt_iv(aead, &secret, &mut pkt_iv).is_ok());
646
634
  let expected_pkt_iv = [
647
- 0xe0, 0x45, 0x9b, 0x34, 0x74, 0xbd, 0xd0, 0xe4, 0x4a, 0x41, 0xc1,
648
- 0x44,
635
+ 0xe0, 0x45, 0x9b, 0x34, 0x74, 0xbd, 0xd0, 0xe4, 0x4a, 0x41, 0xc1, 0x44,
649
636
  ];
650
637
  assert_eq!(&pkt_iv, &expected_pkt_iv);
651
638
 
652
639
  assert!(derive_hdr_key(aead, &secret, &mut hdr_key).is_ok());
653
640
  let expected_hdr_key = [
654
- 0x25, 0xa2, 0x82, 0xb9, 0xe8, 0x2f, 0x06, 0xf2, 0x1f, 0x48, 0x89,
655
- 0x17, 0xa4, 0xfc, 0x8f, 0x1b, 0x73, 0x57, 0x36, 0x85, 0x60, 0x85,
656
- 0x97, 0xd0, 0xef, 0xcb, 0x07, 0x6b, 0x0a, 0xb7, 0xa7, 0xa4,
641
+ 0x25, 0xa2, 0x82, 0xb9, 0xe8, 0x2f, 0x06, 0xf2, 0x1f, 0x48, 0x89, 0x17, 0xa4, 0xfc,
642
+ 0x8f, 0x1b, 0x73, 0x57, 0x36, 0x85, 0x60, 0x85, 0x97, 0xd0, 0xef, 0xcb, 0x07, 0x6b,
643
+ 0x0a, 0xb7, 0xa7, 0xa4,
657
644
  ];
658
645
  assert_eq!(&hdr_key, &expected_hdr_key);
659
646
 
660
647
  let next_secret = derive_next_secret(aead, &secret).unwrap();
661
648
  let expected_secret = [
662
- 0x12, 0x23, 0x50, 0x47, 0x55, 0x03, 0x6d, 0x55, 0x63, 0x42, 0xee,
663
- 0x93, 0x61, 0xd2, 0x53, 0x42, 0x1a, 0x82, 0x6c, 0x9e, 0xcd, 0xf3,
664
- 0xc7, 0x14, 0x86, 0x84, 0xb3, 0x6b, 0x71, 0x48, 0x81, 0xf9,
649
+ 0x12, 0x23, 0x50, 0x47, 0x55, 0x03, 0x6d, 0x55, 0x63, 0x42, 0xee, 0x93, 0x61, 0xd2,
650
+ 0x53, 0x42, 0x1a, 0x82, 0x6c, 0x9e, 0xcd, 0xf3, 0xc7, 0x14, 0x86, 0x84, 0xb3, 0x6b,
651
+ 0x71, 0x48, 0x81, 0xf9,
665
652
  ];
666
653
  assert_eq!(&next_secret, &expected_secret);
667
654
  }
@@ -72,7 +72,7 @@ impl<F: BufFactory> DatagramQueue<F> {
72
72
 
73
73
  buf[..len].copy_from_slice(&d.as_ref()[..len]);
74
74
  Ok(len)
75
- },
75
+ }
76
76
 
77
77
  None => Err(Error::Done),
78
78
  }
@@ -80,8 +80,7 @@ impl<F: BufFactory> DatagramQueue<F> {
80
80
 
81
81
  pub fn pop(&mut self) -> Option<F::DgramBuf> {
82
82
  if let Some(d) = self.queue.pop_front() {
83
- self.queue_bytes_size =
84
- self.queue_bytes_size.saturating_sub(d.as_ref().len());
83
+ self.queue_bytes_size = self.queue_bytes_size.saturating_sub(d.as_ref().len());
85
84
  return Some(d);
86
85
  }
87
86