rwebtransport 0.1.0 → 0.1.2

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 (113) hide show
  1. package/Cargo.lock +666 -666
  2. package/Cargo.toml +31 -31
  3. package/LICENSE +202 -202
  4. package/NOTICE +41 -41
  5. package/README.md +550 -550
  6. package/crates/echo-server/Cargo.toml +20 -20
  7. package/crates/echo-server/src/main.rs +666 -666
  8. package/crates/native/Cargo.toml +23 -23
  9. package/crates/native/src/config.rs +129 -129
  10. package/crates/native/src/driver.rs +463 -463
  11. package/crates/native/src/lib.rs +821 -821
  12. package/crates/native/src/server.rs +718 -718
  13. package/crates/native/src/session.rs +1191 -1191
  14. package/crates/native/src/tls.rs +96 -96
  15. package/crates/wtcore/Cargo.toml +15 -15
  16. package/crates/wtcore/src/h3.rs +226 -226
  17. package/crates/wtcore/src/lib.rs +9 -9
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs.map +1 -1
  20. package/package.json +80 -79
  21. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  24. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  25. package/scripts/bench.mjs +180 -180
  26. package/scripts/build.js +67 -67
  27. package/scripts/postinstall.js +67 -67
  28. package/scripts/setup-unix.sh +129 -129
  29. package/scripts/setup-windows.ps1 +229 -229
  30. package/vendor/quiche/AGENTS.md +75 -75
  31. package/vendor/quiche/COPYING +23 -23
  32. package/vendor/quiche/Cargo.lock +1212 -1212
  33. package/vendor/quiche/Cargo.toml +215 -215
  34. package/vendor/quiche/README.md +466 -466
  35. package/vendor/quiche/examples/Makefile +46 -46
  36. package/vendor/quiche/examples/README.md +20 -20
  37. package/vendor/quiche/examples/cert-big.crt +90 -90
  38. package/vendor/quiche/examples/cert.crt +18 -18
  39. package/vendor/quiche/examples/cert.key +28 -28
  40. package/vendor/quiche/examples/client.c +343 -343
  41. package/vendor/quiche/examples/client.rs +281 -281
  42. package/vendor/quiche/examples/gen-certs.sh +15 -15
  43. package/vendor/quiche/examples/http3-client.c +473 -473
  44. package/vendor/quiche/examples/http3-client.rs +349 -349
  45. package/vendor/quiche/examples/http3-server.c +614 -614
  46. package/vendor/quiche/examples/http3-server.rs +680 -680
  47. package/vendor/quiche/examples/qpack-decode.rs +87 -87
  48. package/vendor/quiche/examples/qpack-encode.rs +88 -88
  49. package/vendor/quiche/examples/rootca.crt +21 -21
  50. package/vendor/quiche/examples/server.c +529 -529
  51. package/vendor/quiche/examples/server.rs +529 -529
  52. package/vendor/quiche/include/quiche.h +1282 -1282
  53. package/vendor/quiche/src/buffers.rs +105 -105
  54. package/vendor/quiche/src/build.rs +66 -66
  55. package/vendor/quiche/src/cid.rs +1079 -1079
  56. package/vendor/quiche/src/crypto/boringssl.rs +373 -373
  57. package/vendor/quiche/src/crypto/mod.rs +671 -671
  58. package/vendor/quiche/src/dgram.rs +118 -118
  59. package/vendor/quiche/src/error.rs +263 -263
  60. package/vendor/quiche/src/ffi.rs +2290 -2290
  61. package/vendor/quiche/src/flowcontrol.rs +246 -246
  62. package/vendor/quiche/src/frame.rs +2172 -2172
  63. package/vendor/quiche/src/h3/AGENTS.md +54 -54
  64. package/vendor/quiche/src/h3/ffi.rs +442 -442
  65. package/vendor/quiche/src/h3/frame.rs +1404 -1404
  66. package/vendor/quiche/src/h3/mod.rs +8223 -8223
  67. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
  68. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
  69. package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
  70. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
  71. package/vendor/quiche/src/h3/stream.rs +1595 -1595
  72. package/vendor/quiche/src/lib.rs +9556 -9556
  73. package/vendor/quiche/src/minmax.rs +446 -446
  74. package/vendor/quiche/src/packet.rs +2334 -2334
  75. package/vendor/quiche/src/path.rs +1374 -1374
  76. package/vendor/quiche/src/pmtud.rs +652 -652
  77. package/vendor/quiche/src/rand.rs +64 -64
  78. package/vendor/quiche/src/range_buf.rs +186 -186
  79. package/vendor/quiche/src/ranges.rs +755 -755
  80. package/vendor/quiche/src/recovery/AGENTS.md +80 -80
  81. package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
  82. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
  83. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
  84. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
  85. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
  86. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
  87. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
  88. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
  89. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
  90. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
  91. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
  92. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
  93. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
  94. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
  95. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
  96. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
  97. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
  98. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
  99. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
  100. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
  101. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
  102. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
  103. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
  104. package/vendor/quiche/src/recovery/mod.rs +3086 -3086
  105. package/vendor/quiche/src/recovery/rtt.rs +152 -152
  106. package/vendor/quiche/src/stream/mod.rs +2210 -2210
  107. package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
  108. package/vendor/quiche/src/stream/send_buf.rs +849 -849
  109. package/vendor/quiche/src/test_utils.rs +681 -681
  110. package/vendor/quiche/src/tests.rs +12922 -12922
  111. package/vendor/quiche/src/tls/boringssl.rs +375 -375
  112. package/vendor/quiche/src/tls/mod.rs +1245 -1245
  113. package/vendor/quiche/src/transport_params.rs +628 -628
@@ -1,373 +1,373 @@
1
- use super::*;
2
-
3
- use std::convert::TryFrom;
4
-
5
- use std::mem::MaybeUninit;
6
-
7
- use libc::c_int;
8
- use libc::c_uint;
9
- use libc::c_void;
10
-
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)]
15
- struct EVP_AEAD_CTX {
16
- aead: libc::uintptr_t,
17
- opaque: [u8; 580],
18
- alignment: u64,
19
- tag_len: u8,
20
- }
21
-
22
- #[derive(Clone)]
23
- #[repr(C)]
24
- pub(crate) struct AES_KEY {
25
- rd_key: [u32; 4 * (14 + 1)],
26
- rounds: c_int,
27
- }
28
-
29
- impl Algorithm {
30
- fn get_evp_aead(self) -> *const EVP_AEAD {
31
- match self {
32
- Algorithm::AES128_GCM => unsafe { EVP_aead_aes_128_gcm_tls13() },
33
- Algorithm::AES256_GCM => unsafe { EVP_aead_aes_256_gcm_tls13() },
34
- Algorithm::ChaCha20_Poly1305 => unsafe {
35
- EVP_aead_chacha20_poly1305()
36
- },
37
- }
38
- }
39
- }
40
-
41
- pub(crate) struct PacketKey {
42
- alg: Algorithm,
43
-
44
- ctx: EVP_AEAD_CTX,
45
-
46
- nonce: Vec<u8>,
47
- }
48
-
49
- impl PacketKey {
50
- pub fn new(
51
- alg: Algorithm, key: Vec<u8>, iv: Vec<u8>, _enc: u32,
52
- ) -> Result<Self> {
53
- Ok(Self {
54
- alg,
55
- ctx: make_aead_ctx(alg, &key)?,
56
- nonce: iv,
57
- })
58
- }
59
-
60
- pub fn from_secret(aead: Algorithm, secret: &[u8], enc: u32) -> Result<Self> {
61
- let key_len = aead.key_len();
62
- let nonce_len = aead.nonce_len();
63
-
64
- let mut key = vec![0; key_len];
65
- let mut iv = vec![0; nonce_len];
66
-
67
- derive_pkt_key(aead, secret, &mut key)?;
68
- derive_pkt_iv(aead, secret, &mut iv)?;
69
-
70
- let mut pkt_key = Self::new(aead, key, iv, enc)?;
71
-
72
- // Dummy seal operation to prime the AEAD context with the nonce mask.
73
- //
74
- // This is needed because BoringCrypto requires the first counter (i.e.
75
- // packet number) to be zero, which would not be the case for packet
76
- // number spaces after Initial as the same packet number sequence is
77
- // shared.
78
- let _ = pkt_key.seal_with_u64_counter(0, b"", &mut [0_u8; 16], 0, None);
79
-
80
- Ok(pkt_key)
81
- }
82
-
83
- pub fn open_with_u64_counter(
84
- &self, counter: u64, ad: &[u8], buf: &mut [u8],
85
- ) -> Result<usize> {
86
- let tag_len = self.alg.tag_len();
87
-
88
- let mut out_len = match buf.len().checked_sub(tag_len) {
89
- Some(n) => n,
90
- None => return Err(Error::CryptoFail),
91
- };
92
-
93
- let max_out_len = out_len;
94
-
95
- let nonce = make_nonce(&self.nonce, counter);
96
-
97
- let rc = unsafe {
98
- EVP_AEAD_CTX_open(
99
- &self.ctx, // ctx
100
- buf.as_mut_ptr(), // out
101
- &mut out_len, // out_len
102
- max_out_len, // max_out_len
103
- nonce[..].as_ptr(), // nonce
104
- nonce.len(), // nonce_len
105
- buf.as_ptr(), // inp
106
- buf.len(), // in_len
107
- ad.as_ptr(), // ad
108
- ad.len(), // ad_len
109
- )
110
- };
111
-
112
- if rc != 1 {
113
- return Err(Error::CryptoFail);
114
- }
115
-
116
- Ok(out_len)
117
- }
118
-
119
- pub fn seal_with_u64_counter(
120
- &mut self, counter: u64, ad: &[u8], buf: &mut [u8], in_len: usize,
121
- extra_in: Option<&[u8]>,
122
- ) -> Result<usize> {
123
- let tag_len = self.alg.tag_len();
124
-
125
- let mut out_tag_len = tag_len;
126
-
127
- let (extra_in_ptr, extra_in_len) = match extra_in {
128
- Some(v) => (v.as_ptr(), v.len()),
129
-
130
- None => (std::ptr::null(), 0),
131
- };
132
-
133
- // Make sure all the outputs combined fit in the buffer.
134
- if in_len + tag_len + extra_in_len > buf.len() {
135
- return Err(Error::CryptoFail);
136
- }
137
-
138
- let nonce = make_nonce(&self.nonce, counter);
139
-
140
- let rc = unsafe {
141
- EVP_AEAD_CTX_seal_scatter(
142
- &mut self.ctx, // ctx
143
- buf.as_mut_ptr(), // out
144
- buf[in_len..].as_mut_ptr(), // out_tag
145
- &mut out_tag_len, // out_tag_len
146
- tag_len + extra_in_len, // max_out_tag_len
147
- nonce[..].as_ptr(), // nonce
148
- nonce.len(), // nonce_len
149
- buf.as_ptr(), // inp
150
- in_len, // in_len
151
- extra_in_ptr, // extra_in
152
- extra_in_len, // extra_in_len
153
- ad.as_ptr(), // ad
154
- ad.len(), // ad_len
155
- )
156
- };
157
-
158
- if rc != 1 {
159
- return Err(Error::CryptoFail);
160
- }
161
-
162
- Ok(in_len + out_tag_len)
163
- }
164
- }
165
-
166
- #[derive(Clone)]
167
- #[allow(clippy::large_enum_variant)]
168
- pub(crate) enum HeaderProtectionKey {
169
- Aes(AES_KEY),
170
-
171
- ChaCha(Vec<u8>),
172
- }
173
-
174
- impl HeaderProtectionKey {
175
- pub fn new(alg: Algorithm, hp_key: Vec<u8>) -> Result<Self> {
176
- match alg {
177
- Algorithm::AES128_GCM | Algorithm::AES256_GCM => unsafe {
178
- let key_len_bits = alg.key_len() as u32 * 8;
179
-
180
- let mut aes_key = MaybeUninit::<AES_KEY>::uninit();
181
-
182
- let rc = AES_set_encrypt_key(
183
- hp_key.as_ptr(),
184
- key_len_bits,
185
- aes_key.as_mut_ptr(),
186
- );
187
-
188
- if rc != 0 {
189
- return Err(Error::CryptoFail);
190
- }
191
-
192
- let aes_key = aes_key.assume_init();
193
- Ok(Self::Aes(aes_key))
194
- },
195
-
196
- Algorithm::ChaCha20_Poly1305 => Ok(Self::ChaCha(hp_key)),
197
- }
198
- }
199
-
200
- pub fn new_mask(&self, sample: &[u8]) -> Result<HeaderProtectionMask> {
201
- match self {
202
- Self::Aes(aes_key) => {
203
- let mut block = [0_u8; 16];
204
-
205
- unsafe {
206
- AES_ecb_encrypt(
207
- sample.as_ptr(),
208
- block.as_mut_ptr(),
209
- aes_key as _,
210
- 1,
211
- )
212
- };
213
-
214
- // Downsize the encrypted block to the size of the header
215
- // protection mask.
216
- //
217
- // The length of the slice will always match the size of
218
- // `HeaderProtectionMask` so the `unwrap()` is safe.
219
- let new_mask =
220
- HeaderProtectionMask::try_from(&block[..HP_MASK_LEN])
221
- .unwrap();
222
- Ok(new_mask)
223
- },
224
-
225
- Self::ChaCha(key) => {
226
- const PLAINTEXT: &[u8; HP_MASK_LEN] = &[0_u8; HP_MASK_LEN];
227
-
228
- let mut new_mask = HeaderProtectionMask::default();
229
-
230
- let counter = u32::from_le_bytes([
231
- sample[0], sample[1], sample[2], sample[3],
232
- ]);
233
-
234
- unsafe {
235
- CRYPTO_chacha_20(
236
- new_mask.as_mut_ptr(),
237
- PLAINTEXT.as_ptr(),
238
- PLAINTEXT.len(),
239
- key.as_ptr(),
240
- sample[size_of::<u32>()..].as_ptr(),
241
- counter,
242
- );
243
- };
244
-
245
- Ok(new_mask)
246
- },
247
- }
248
- }
249
- }
250
-
251
- fn make_aead_ctx(alg: Algorithm, key: &[u8]) -> Result<EVP_AEAD_CTX> {
252
- let mut ctx = MaybeUninit::uninit();
253
-
254
- let ctx = unsafe {
255
- let aead = alg.get_evp_aead();
256
-
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()
271
- };
272
-
273
- Ok(ctx)
274
- }
275
-
276
- pub(crate) fn hkdf_extract(
277
- alg: Algorithm, out: &mut [u8], secret: &[u8], salt: &[u8],
278
- ) -> Result<()> {
279
- let mut out_len = out.len();
280
-
281
- let rc = unsafe {
282
- HKDF_extract(
283
- out.as_mut_ptr(),
284
- &mut out_len,
285
- alg.get_evp_digest(),
286
- secret.as_ptr(),
287
- secret.len(),
288
- salt.as_ptr(),
289
- salt.len(),
290
- )
291
- };
292
-
293
- if rc != 1 {
294
- return Err(Error::CryptoFail);
295
- }
296
-
297
- Ok(())
298
- }
299
-
300
- pub(crate) fn hkdf_expand(
301
- alg: Algorithm, out: &mut [u8], secret: &[u8], info: &[u8],
302
- ) -> Result<()> {
303
- let rc = unsafe {
304
- HKDF_expand(
305
- out.as_mut_ptr(),
306
- out.len(),
307
- alg.get_evp_digest(),
308
- secret.as_ptr(),
309
- secret.len(),
310
- info.as_ptr(),
311
- info.len(),
312
- )
313
- };
314
-
315
- if rc != 1 {
316
- return Err(Error::CryptoFail);
317
- }
318
-
319
- Ok(())
320
- }
321
-
322
- extern "C" {
323
- fn EVP_aead_aes_128_gcm_tls13() -> *const EVP_AEAD;
324
-
325
- fn EVP_aead_aes_256_gcm_tls13() -> *const EVP_AEAD;
326
-
327
- fn EVP_aead_chacha20_poly1305() -> *const EVP_AEAD;
328
-
329
- // HKDF
330
- 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,
333
- ) -> c_int;
334
-
335
- 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,
338
- ) -> c_int;
339
-
340
- // 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;
345
-
346
- 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,
350
- ) -> c_int;
351
-
352
- 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,
357
- ) -> c_int;
358
-
359
- // AES
360
- fn AES_set_encrypt_key(
361
- key: *const u8, bits: c_uint, aeskey: *mut AES_KEY,
362
- ) -> c_int;
363
-
364
- fn AES_ecb_encrypt(
365
- inp: *const u8, out: *mut u8, key: *const AES_KEY, enc: c_int,
366
- ) -> c_void;
367
-
368
- // ChaCha20
369
- 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;
373
- }
1
+ use super::*;
2
+
3
+ use std::convert::TryFrom;
4
+
5
+ use std::mem::MaybeUninit;
6
+
7
+ use libc::c_int;
8
+ use libc::c_uint;
9
+ use libc::c_void;
10
+
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)]
15
+ struct EVP_AEAD_CTX {
16
+ aead: libc::uintptr_t,
17
+ opaque: [u8; 580],
18
+ alignment: u64,
19
+ tag_len: u8,
20
+ }
21
+
22
+ #[derive(Clone)]
23
+ #[repr(C)]
24
+ pub(crate) struct AES_KEY {
25
+ rd_key: [u32; 4 * (14 + 1)],
26
+ rounds: c_int,
27
+ }
28
+
29
+ impl Algorithm {
30
+ fn get_evp_aead(self) -> *const EVP_AEAD {
31
+ match self {
32
+ Algorithm::AES128_GCM => unsafe { EVP_aead_aes_128_gcm_tls13() },
33
+ Algorithm::AES256_GCM => unsafe { EVP_aead_aes_256_gcm_tls13() },
34
+ Algorithm::ChaCha20_Poly1305 => unsafe {
35
+ EVP_aead_chacha20_poly1305()
36
+ },
37
+ }
38
+ }
39
+ }
40
+
41
+ pub(crate) struct PacketKey {
42
+ alg: Algorithm,
43
+
44
+ ctx: EVP_AEAD_CTX,
45
+
46
+ nonce: Vec<u8>,
47
+ }
48
+
49
+ impl PacketKey {
50
+ pub fn new(
51
+ alg: Algorithm, key: Vec<u8>, iv: Vec<u8>, _enc: u32,
52
+ ) -> Result<Self> {
53
+ Ok(Self {
54
+ alg,
55
+ ctx: make_aead_ctx(alg, &key)?,
56
+ nonce: iv,
57
+ })
58
+ }
59
+
60
+ pub fn from_secret(aead: Algorithm, secret: &[u8], enc: u32) -> Result<Self> {
61
+ let key_len = aead.key_len();
62
+ let nonce_len = aead.nonce_len();
63
+
64
+ let mut key = vec![0; key_len];
65
+ let mut iv = vec![0; nonce_len];
66
+
67
+ derive_pkt_key(aead, secret, &mut key)?;
68
+ derive_pkt_iv(aead, secret, &mut iv)?;
69
+
70
+ let mut pkt_key = Self::new(aead, key, iv, enc)?;
71
+
72
+ // Dummy seal operation to prime the AEAD context with the nonce mask.
73
+ //
74
+ // This is needed because BoringCrypto requires the first counter (i.e.
75
+ // packet number) to be zero, which would not be the case for packet
76
+ // number spaces after Initial as the same packet number sequence is
77
+ // shared.
78
+ let _ = pkt_key.seal_with_u64_counter(0, b"", &mut [0_u8; 16], 0, None);
79
+
80
+ Ok(pkt_key)
81
+ }
82
+
83
+ pub fn open_with_u64_counter(
84
+ &self, counter: u64, ad: &[u8], buf: &mut [u8],
85
+ ) -> Result<usize> {
86
+ let tag_len = self.alg.tag_len();
87
+
88
+ let mut out_len = match buf.len().checked_sub(tag_len) {
89
+ Some(n) => n,
90
+ None => return Err(Error::CryptoFail),
91
+ };
92
+
93
+ let max_out_len = out_len;
94
+
95
+ let nonce = make_nonce(&self.nonce, counter);
96
+
97
+ let rc = unsafe {
98
+ EVP_AEAD_CTX_open(
99
+ &self.ctx, // ctx
100
+ buf.as_mut_ptr(), // out
101
+ &mut out_len, // out_len
102
+ max_out_len, // max_out_len
103
+ nonce[..].as_ptr(), // nonce
104
+ nonce.len(), // nonce_len
105
+ buf.as_ptr(), // inp
106
+ buf.len(), // in_len
107
+ ad.as_ptr(), // ad
108
+ ad.len(), // ad_len
109
+ )
110
+ };
111
+
112
+ if rc != 1 {
113
+ return Err(Error::CryptoFail);
114
+ }
115
+
116
+ Ok(out_len)
117
+ }
118
+
119
+ pub fn seal_with_u64_counter(
120
+ &mut self, counter: u64, ad: &[u8], buf: &mut [u8], in_len: usize,
121
+ extra_in: Option<&[u8]>,
122
+ ) -> Result<usize> {
123
+ let tag_len = self.alg.tag_len();
124
+
125
+ let mut out_tag_len = tag_len;
126
+
127
+ let (extra_in_ptr, extra_in_len) = match extra_in {
128
+ Some(v) => (v.as_ptr(), v.len()),
129
+
130
+ None => (std::ptr::null(), 0),
131
+ };
132
+
133
+ // Make sure all the outputs combined fit in the buffer.
134
+ if in_len + tag_len + extra_in_len > buf.len() {
135
+ return Err(Error::CryptoFail);
136
+ }
137
+
138
+ let nonce = make_nonce(&self.nonce, counter);
139
+
140
+ let rc = unsafe {
141
+ EVP_AEAD_CTX_seal_scatter(
142
+ &mut self.ctx, // ctx
143
+ buf.as_mut_ptr(), // out
144
+ buf[in_len..].as_mut_ptr(), // out_tag
145
+ &mut out_tag_len, // out_tag_len
146
+ tag_len + extra_in_len, // max_out_tag_len
147
+ nonce[..].as_ptr(), // nonce
148
+ nonce.len(), // nonce_len
149
+ buf.as_ptr(), // inp
150
+ in_len, // in_len
151
+ extra_in_ptr, // extra_in
152
+ extra_in_len, // extra_in_len
153
+ ad.as_ptr(), // ad
154
+ ad.len(), // ad_len
155
+ )
156
+ };
157
+
158
+ if rc != 1 {
159
+ return Err(Error::CryptoFail);
160
+ }
161
+
162
+ Ok(in_len + out_tag_len)
163
+ }
164
+ }
165
+
166
+ #[derive(Clone)]
167
+ #[allow(clippy::large_enum_variant)]
168
+ pub(crate) enum HeaderProtectionKey {
169
+ Aes(AES_KEY),
170
+
171
+ ChaCha(Vec<u8>),
172
+ }
173
+
174
+ impl HeaderProtectionKey {
175
+ pub fn new(alg: Algorithm, hp_key: Vec<u8>) -> Result<Self> {
176
+ match alg {
177
+ Algorithm::AES128_GCM | Algorithm::AES256_GCM => unsafe {
178
+ let key_len_bits = alg.key_len() as u32 * 8;
179
+
180
+ let mut aes_key = MaybeUninit::<AES_KEY>::uninit();
181
+
182
+ let rc = AES_set_encrypt_key(
183
+ hp_key.as_ptr(),
184
+ key_len_bits,
185
+ aes_key.as_mut_ptr(),
186
+ );
187
+
188
+ if rc != 0 {
189
+ return Err(Error::CryptoFail);
190
+ }
191
+
192
+ let aes_key = aes_key.assume_init();
193
+ Ok(Self::Aes(aes_key))
194
+ },
195
+
196
+ Algorithm::ChaCha20_Poly1305 => Ok(Self::ChaCha(hp_key)),
197
+ }
198
+ }
199
+
200
+ pub fn new_mask(&self, sample: &[u8]) -> Result<HeaderProtectionMask> {
201
+ match self {
202
+ Self::Aes(aes_key) => {
203
+ let mut block = [0_u8; 16];
204
+
205
+ unsafe {
206
+ AES_ecb_encrypt(
207
+ sample.as_ptr(),
208
+ block.as_mut_ptr(),
209
+ aes_key as _,
210
+ 1,
211
+ )
212
+ };
213
+
214
+ // Downsize the encrypted block to the size of the header
215
+ // protection mask.
216
+ //
217
+ // The length of the slice will always match the size of
218
+ // `HeaderProtectionMask` so the `unwrap()` is safe.
219
+ let new_mask =
220
+ HeaderProtectionMask::try_from(&block[..HP_MASK_LEN])
221
+ .unwrap();
222
+ Ok(new_mask)
223
+ },
224
+
225
+ Self::ChaCha(key) => {
226
+ const PLAINTEXT: &[u8; HP_MASK_LEN] = &[0_u8; HP_MASK_LEN];
227
+
228
+ let mut new_mask = HeaderProtectionMask::default();
229
+
230
+ let counter = u32::from_le_bytes([
231
+ sample[0], sample[1], sample[2], sample[3],
232
+ ]);
233
+
234
+ unsafe {
235
+ CRYPTO_chacha_20(
236
+ new_mask.as_mut_ptr(),
237
+ PLAINTEXT.as_ptr(),
238
+ PLAINTEXT.len(),
239
+ key.as_ptr(),
240
+ sample[size_of::<u32>()..].as_ptr(),
241
+ counter,
242
+ );
243
+ };
244
+
245
+ Ok(new_mask)
246
+ },
247
+ }
248
+ }
249
+ }
250
+
251
+ fn make_aead_ctx(alg: Algorithm, key: &[u8]) -> Result<EVP_AEAD_CTX> {
252
+ let mut ctx = MaybeUninit::uninit();
253
+
254
+ let ctx = unsafe {
255
+ let aead = alg.get_evp_aead();
256
+
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()
271
+ };
272
+
273
+ Ok(ctx)
274
+ }
275
+
276
+ pub(crate) fn hkdf_extract(
277
+ alg: Algorithm, out: &mut [u8], secret: &[u8], salt: &[u8],
278
+ ) -> Result<()> {
279
+ let mut out_len = out.len();
280
+
281
+ let rc = unsafe {
282
+ HKDF_extract(
283
+ out.as_mut_ptr(),
284
+ &mut out_len,
285
+ alg.get_evp_digest(),
286
+ secret.as_ptr(),
287
+ secret.len(),
288
+ salt.as_ptr(),
289
+ salt.len(),
290
+ )
291
+ };
292
+
293
+ if rc != 1 {
294
+ return Err(Error::CryptoFail);
295
+ }
296
+
297
+ Ok(())
298
+ }
299
+
300
+ pub(crate) fn hkdf_expand(
301
+ alg: Algorithm, out: &mut [u8], secret: &[u8], info: &[u8],
302
+ ) -> Result<()> {
303
+ let rc = unsafe {
304
+ HKDF_expand(
305
+ out.as_mut_ptr(),
306
+ out.len(),
307
+ alg.get_evp_digest(),
308
+ secret.as_ptr(),
309
+ secret.len(),
310
+ info.as_ptr(),
311
+ info.len(),
312
+ )
313
+ };
314
+
315
+ if rc != 1 {
316
+ return Err(Error::CryptoFail);
317
+ }
318
+
319
+ Ok(())
320
+ }
321
+
322
+ extern "C" {
323
+ fn EVP_aead_aes_128_gcm_tls13() -> *const EVP_AEAD;
324
+
325
+ fn EVP_aead_aes_256_gcm_tls13() -> *const EVP_AEAD;
326
+
327
+ fn EVP_aead_chacha20_poly1305() -> *const EVP_AEAD;
328
+
329
+ // HKDF
330
+ 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,
333
+ ) -> c_int;
334
+
335
+ 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,
338
+ ) -> c_int;
339
+
340
+ // 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;
345
+
346
+ 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,
350
+ ) -> c_int;
351
+
352
+ 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,
357
+ ) -> c_int;
358
+
359
+ // AES
360
+ fn AES_set_encrypt_key(
361
+ key: *const u8, bits: c_uint, aeskey: *mut AES_KEY,
362
+ ) -> c_int;
363
+
364
+ fn AES_ecb_encrypt(
365
+ inp: *const u8, out: *mut u8, key: *const AES_KEY, enc: c_int,
366
+ ) -> c_void;
367
+
368
+ // ChaCha20
369
+ 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;
373
+ }