nish 0.35.0 → 0.37.0

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 (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. package/std/yaml.ts +764 -384
@@ -5,25 +5,35 @@
5
5
  * Initial secrets, AEAD packet protection, header protection and the Retry
6
6
  * integrity tag.
7
7
  *
8
- * import { quicInitialSecrets, quicKeys, quicParseHeader, quicOpenPacket, QuicKeysSlot,
9
- * QUIC_AEAD_AES_128_GCM, QUIC_PACKET_OK } from "nish/net/quic-packet";
10
- * import { Secret, secret, wipe } from "nish:secret";
8
+ * import { QuicHeader, QuicInitialSecrets, QuicKeys, QuicKeysSlot, QuicPacket, quicInitialSecrets,
9
+ * quicKeys, quicLongHeader, quicOpenPacket, quicParseHeader, quicSealPacket,
10
+ * QUIC_AEAD_AES_128_GCM, QUIC_PACKET_INITIAL, QUIC_PACKET_OK } from "nish/net/quic-packet"
11
+ * import { Secret, secret, wipe } from "nish:secret"
11
12
  *
12
- * const secrets = quicInitialSecrets(clientDcid); // RFC 9001 §5.2
13
- * if (secrets === null) { … a DCID over 20 bytes … }
14
- * const client: u8[] = new Array<u8>(secrets.client.length); // public, so a copy
15
- * client.set(secrets.client);
16
- * const held: Secret<u8[]> = secret(client);
17
- * const slot = new QuicKeysSlot(); // ours, so ours to wipe
18
- * const keys = quicKeys(slot, QUIC_AEAD_AES_128_GCM, held);
19
- * wipe(held);
20
- * if (keys === null) { … }
21
- * const header = quicParseHeader(datagram, 0, 8); // 8: our short-header DCID length
22
- * const packet = quicOpenPacket(keys, datagram, header, largestReceived);
23
- * if (packet.error === QUIC_PACKET_OK) { … packet.payload holds the frames … }
24
- * slot.wipe(); // once the level is done
13
+ * const dcid: u8[] = [0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08]
14
+ * const secrets: QuicInitialSecrets | null = quicInitialSecrets(dcid) // RFC 9001 §5.2
15
+ * if (secrets !== null) {
16
+ * const client: u8[] = new Array<u8>(32) // public, so a copy
17
+ * client.set(secrets.client)
18
+ * const held: Secret<u8[]> = secret(client)
19
+ * const slot: QuicKeysSlot = new QuicKeysSlot() // ours, so ours to wipe
20
+ * const keys: QuicKeys | null = quicKeys(slot, QUIC_AEAD_AES_128_GCM, held)
21
+ * wipe(held)
22
+ * const header: u8[] | null = quicLongHeader(QUIC_PACKET_INITIAL, dcid, [], [], 0, 4, 20)
23
+ * if (keys !== null && header !== null) {
24
+ * const datagram: u8[] | null = quicSealPacket(keys, header, 0, new Array<u8>(20))
25
+ * if (datagram !== null) {
26
+ * const parsed: QuicHeader = quicParseHeader(datagram, 0, 8) // 8: our short DCID length
27
+ * const packet: QuicPacket = quicOpenPacket(keys, datagram, parsed, -1)
28
+ * if (packet.error === QUIC_PACKET_OK) {
29
+ * // packet.payload holds the 20 bytes of frames sealed above
30
+ * }
31
+ * }
32
+ * }
33
+ * slot.wipe() // once the level is done
34
+ * }
25
35
  *
26
- * The module is sans-IO: bytes in, bytes out, no socket and no clock. It
36
+ * **Sans-IO.** The module takes bytes in and gives bytes out, with no socket and no clock. It
27
37
  * frames and protects packets and stops there, so it knows nothing of frames,
28
38
  * of a connection's state or of which keys a level is on; that is
29
39
  * `nish/net/quic` (WP34 Q2). Only version 1 (`0x00000001`) is spoken. A long
@@ -31,18 +41,19 @@
31
41
  * which is what a server needs to send Version Negotiation, and reported as
32
42
  * `QUIC_ERR_VERSION`.
33
43
  *
34
- * **What a parse refuses.** Every read is bounds-checked against the
35
- * datagram, so nothing a peer sends can make this module panic. A header
36
- * that runs past the datagram is `QUIC_ERR_TRUNCATED`; a clear fixed bit
37
- * `QUIC_ERR_FIXED_BIT`; a connection ID over 20 bytes in a version 1 header
38
- * `QUIC_ERR_CID_LENGTH`; a Length field reaching past the datagram
39
- * `QUIC_ERR_LENGTH`; a packet too short to sample for header protection
40
- * `QUIC_ERR_SAMPLE`; a payload that does not authenticate `QUIC_ERR_DECRYPT`;
41
- * and nonzero reserved bits once both protections are off
42
- * `QUIC_ERR_RESERVED_BITS`, which RFC 9000 §17.2 makes a connection error
43
- * rather than a drop, so the payload is still handed back with it. The
44
- * builders, whose inputs come from this side rather than from a peer, answer
45
- * `null` for an argument out of range.
44
+ * **What a parse refuses.** Every read is bounds-checked against the datagram,
45
+ * so nothing a peer sends can make this module panic.
46
+ *
47
+ * A header that runs past the datagram is `QUIC_ERR_TRUNCATED`; a clear fixed
48
+ * bit `QUIC_ERR_FIXED_BIT`; a connection ID over 20 bytes in a version 1 header
49
+ * `QUIC_ERR_CID_LENGTH`; a Length field reaching past the datagram
50
+ * `QUIC_ERR_LENGTH`; a packet too short to sample for header protection
51
+ * `QUIC_ERR_SAMPLE`; a payload that does not authenticate `QUIC_ERR_DECRYPT`;
52
+ * and nonzero reserved bits once both protections are off
53
+ * `QUIC_ERR_RESERVED_BITS`, which RFC 9000 §17.2 makes a connection error
54
+ * rather than a drop, so the payload is still handed back with it. The
55
+ * builders, whose inputs come from this side rather than from a peer, answer
56
+ * `null` for an argument out of range.
46
57
  *
47
58
  * **In place.** For a connection that allocates nothing per packet,
48
59
  * `quicParseHeaderInto` reads a header as windows into the datagram, the
@@ -58,24 +69,24 @@
58
69
  * datagram starts. A short header has none, so it runs to the datagram's end.
59
70
  *
60
71
  * **Secrets.** `QuicKeys` holds a packet key, an IV and a header-protection
61
- * key, pointing into a `QuicKeysSlot`'s arrays. `quicKeys` and
62
- * `quicKeysUpdate` take a traffic secret as a `Secret` from `nish:secret`
63
- * and a slot the caller owns, and derive into the slot; a slot the caller
64
- * owns is the caller's to wipe, and `QuicKeysSlot.wipe` zeroes it with
65
- * stores no optimiser removes, AES schedules included (QUIC-1).
66
- * `quicKeyUpdateSecret` answers the next secret as a `Secret`, and every
67
- * derivation wipes its HKDF intermediates before it returns. `quicKeysWipe`
68
- * zeroes the arrays one `QuicKeys` points at. The Initial keys are not
69
- * secret, because anyone who reads the client's first DCID can derive them
70
- * (RFC 9001 §5.2), so `quicInitialSecrets` answers plain arrays, and
71
- * `quicKeysInto`, `quicKeysUpdateInto`, `quicKeyUpdateSecretInto` and
72
- * `quicInitialSecretsInto` derive from plain secrets over a caller's
73
- * `HkdfScratch`; the Handshake and 1-RTT keys are secret. Each derivation's
74
- * own temporaries go with its arena block. A slot whose traffic secret is
75
- * held as a `Secret` may also derive with `quicKeysHeldInto`, and ratchet
76
- * the secret in place with `quicKeyUpdateSecretHeldInto`. Sealing and
77
- * opening a packet wipe the nonce, which gives the IV away, and the
78
- * header-protection mask once each is used (QUIC-2).
72
+ * key, pointing into a `QuicKeysSlot`'s arrays. `quicKeys` and `quicKeysUpdate`
73
+ * take a traffic secret as a `Secret` from `nish:secret` and a slot the caller
74
+ * owns, and derive into the slot; a slot the caller owns is the caller's to
75
+ * wipe, and `QuicKeysSlot.wipe` zeroes it with stores no optimiser removes, AES
76
+ * schedules included (QUIC-1). `quicKeyUpdateSecret` answers the next secret as
77
+ * a `Secret`, and every derivation wipes its HKDF intermediates before it
78
+ * returns. `quicKeysWipe` zeroes the arrays one `QuicKeys` points at.
79
+ *
80
+ * The Initial keys are not secret, because anyone who reads the client's first
81
+ * DCID can derive them (RFC 9001 §5.2), so `quicInitialSecrets` answers plain
82
+ * arrays, and `quicKeysInto`, `quicKeysUpdateInto`, `quicKeyUpdateSecretInto`
83
+ * and `quicInitialSecretsInto` derive from plain secrets over a caller's
84
+ * `HkdfScratch`; the Handshake and 1-RTT keys are secret. Each derivation's own
85
+ * temporaries go with its arena block. A slot whose traffic secret is held as a
86
+ * `Secret` may also derive with `quicKeysHeldInto`, and ratchet the secret in
87
+ * place with `quicKeyUpdateSecretHeldInto`. Sealing and opening a packet wipe
88
+ * the nonce, which gives the IV away, and the header-protection mask once each
89
+ * is used (QUIC-2).
79
90
  *
80
91
  * **Constant time.** The AEADs and the header-protection masks are
81
92
  * `nish/crypto/aes` and `nish/crypto/chacha20poly1305`, with their guarantees.
@@ -84,12 +95,12 @@
84
95
  * who holds the header-protection key, and RFC 9001 §5.4.1 protects it against
85
96
  * observers, not timing), and whether a payload authenticated.
86
97
  *
87
- * Written from RFC 9000 and RFC 9001, in this module's own structure: nothing
98
+ * **Provenance.** Written from RFC 9000 and RFC 9001, in this module's own structure: nothing
88
99
  * here is ported from another implementation. The packet-number decoding
89
100
  * follows the arithmetic of RFC 9000 §A.3 and the length choice the rule of
90
101
  * §A.2, neither line by line.
91
102
  *
92
- * Private names carry the `quicPacket` prefix because a `std/` module's
103
+ * **Private names.** They carry the `quicPacket` prefix because a `std/` module's
93
104
  * private functions share the importing program's flat symbol namespace
94
105
  * (`docs/wp26-stdlib.md` §3e).
95
106
  */
@@ -240,12 +251,24 @@ export class QuicHeader {
240
251
  * windows out into the arrays above.
241
252
  */
242
253
  dcidStart: i32 = 0
254
+ /**
255
+ * The Destination Connection ID's length in bytes, 0 to 20; a short header's is the length the
256
+ * reader passed.
257
+ */
243
258
  dcidLength: i32 = 0
259
+ /** Where the Source Connection ID starts in the datagram; 0 for a short header. */
244
260
  scidStart: i32 = 0
261
+ /** The Source Connection ID's length in bytes, 0 to 20; 0 for a short header. */
245
262
  scidLength: i32 = 0
263
+ /** Where an Initial's or a Retry's token starts in the datagram; 0 for every other type. */
246
264
  tokenStart: i32 = 0
265
+ /** An Initial's or a Retry's token length in bytes; 0 for every other type. */
247
266
  tokenLength: i32 = 0
248
267
 
268
+ /**
269
+ * Makes an empty header, every array empty and every window at 0, for
270
+ * `quicParseHeader` or `quicParseHeaderInto` to fill.
271
+ */
249
272
  constructor() {
250
273
  this.dcid = []
251
274
  this.scid = []
@@ -274,6 +297,17 @@ export class QuicKeys {
274
297
  /** `hp` expanded, for the AES suites; `null` for ChaCha20-Poly1305. */
275
298
  hpAes: AesKey | null = null
276
299
 
300
+ /**
301
+ * Wraps keys already derived; the arrays are kept, not copied, so they stay the owner's to wipe.
302
+ * `packetAes` and `hpAes` start `null`; the `quicKeys*` functions fill them for the AES suites. A
303
+ * `QuicKeys` built by hand for an AES suite stays unusable: opening refuses it with `QUIC_ERR_KEYS`, and
304
+ * sealing answers `null`.
305
+ *
306
+ * @param aead The suite, one of the `QUIC_AEAD_*` constants.
307
+ * @param key The packet-protection key: 16 bytes for AES-128-GCM, 32 for the others.
308
+ * @param iv The 12-byte packet IV.
309
+ * @param hp The header-protection key, the same length as `key`.
310
+ */
277
311
  constructor(aead: i32, key: u8[], iv: u8[], hp: u8[]) {
278
312
  this.aead = aead
279
313
  this.key = key
@@ -289,6 +323,12 @@ export class QuicInitialSecrets {
289
323
  /** `server_initial_secret`: what protects the server's. */
290
324
  server: u8[]
291
325
 
326
+ /**
327
+ * Pairs two secrets already derived, keeping the arrays rather than copying them.
328
+ *
329
+ * @param client The 32-byte `client_initial_secret`.
330
+ * @param server The 32-byte `server_initial_secret`.
331
+ */
292
332
  constructor(client: u8[], server: u8[]) {
293
333
  this.client = client
294
334
  this.server = server
@@ -316,6 +356,10 @@ export class QuicPacket {
316
356
  /** A short header's Key Phase bit (RFC 9001 §6); `false` for a long header. */
317
357
  keyPhase: boolean = false
318
358
 
359
+ /**
360
+ * Makes an empty packet, its header and payload empty, for `quicOpenPacket`
361
+ * to fill.
362
+ */
319
363
  constructor() {
320
364
  this.header = []
321
365
  this.payload = []
@@ -362,6 +406,9 @@ const quicPacketAppend = (out: u8[], bytes: u8[]): void => {
362
406
  /**
363
407
  * The bytes a variable-length integer needs (RFC 9000 §16): 1, 2, 4 or 8, or
364
408
  * 0 for a value that has no encoding, below zero or past 2^62 - 1.
409
+ *
410
+ * @param value The integer to encode.
411
+ * @returns The encoded length in bytes, or 0 when `value` has no encoding.
365
412
  */
366
413
  export const quicVarintSize = (value: i64): i32 => {
367
414
  if (value < 0 || value > QUIC_MAX_VARINT) {
@@ -385,6 +432,12 @@ export const quicVarintSize = (value: i64): i32 => {
385
432
  * allows that, and a Length field written before its packet is complete uses
386
433
  * it). Answers `at + size`, or -1, writing nothing, when `size` is not 1, 2,
387
434
  * 4 or 8, is too short for `value`, or does not fit in `buf` from `at`.
435
+ *
436
+ * @param buf The buffer to write into; it is not grown.
437
+ * @param at The byte offset of the integer's first byte.
438
+ * @param value The integer, 0 to 2^62 - 1.
439
+ * @param size The encoded length in bytes: 1, 2, 4 or 8, at least `quicVarintSize(value)`.
440
+ * @returns The byte offset one past the integer, or -1 when nothing was written.
388
441
  */
389
442
  export const quicVarintPut = (buf: u8[], at: i32, value: i64, size: i32): i32 => {
390
443
  const needed: i32 = quicVarintSize(value)
@@ -418,6 +471,9 @@ export const quicVarintPut = (buf: u8[], at: i32, value: i64, size: i32): i32 =>
418
471
  /**
419
472
  * Appends `count` zero bytes to `out`, which a writer then fills in place
420
473
  * with the `put` form of what it appends: one encoder serves both.
474
+ *
475
+ * @param out The array to lengthen.
476
+ * @param count How many zero bytes to append; 0 or less appends none.
421
477
  */
422
478
  export const quicPacketGrow = (out: u8[], count: i32): void => {
423
479
  for (let k: i32 = 0; k < count; k += 1) {
@@ -429,6 +485,11 @@ export const quicPacketGrow = (out: u8[], count: i32): void => {
429
485
  * Appends `value` to `out` as a variable-length integer of exactly `size`
430
486
  * bytes, as `quicVarintPut` writes it. Answers `false`, appending nothing,
431
487
  * when `size` is not 1, 2, 4 or 8 or is too short for `value`.
488
+ *
489
+ * @param out The array to append to.
490
+ * @param value The integer, 0 to 2^62 - 1.
491
+ * @param size The encoded length in bytes: 1, 2, 4 or 8, at least `quicVarintSize(value)`.
492
+ * @returns True when the integer was appended.
432
493
  */
433
494
  export const quicVarintPushSized = (out: u8[], value: i64, size: i32): boolean => {
434
495
  const needed: i32 = quicVarintSize(value)
@@ -444,6 +505,10 @@ export const quicVarintPushSized = (out: u8[], value: i64, size: i32): boolean =
444
505
  * Appends `value` to `out` as a variable-length integer in the fewest bytes
445
506
  * (RFC 9000 §16). Answers `false`, appending nothing, for a value below zero
446
507
  * or past 2^62 - 1.
508
+ *
509
+ * @param out The array to append to.
510
+ * @param value The integer, 0 to 2^62 - 1.
511
+ * @returns True when the integer was appended.
447
512
  */
448
513
  export const quicVarintPush = (out: u8[], value: i64): boolean =>
449
514
  quicVarintPushSized(out, value, quicVarintSize(value))
@@ -452,6 +517,10 @@ export const quicVarintPush = (out: u8[], value: i64): boolean =>
452
517
  * The length of the variable-length integer whose first byte is at
453
518
  * `bytes[at]`, read from its two-bit prefix: 1, 2, 4 or 8. Answers 0 when
454
519
  * `at` is outside `bytes`.
520
+ *
521
+ * @param bytes The bytes holding the integer.
522
+ * @param at The byte offset of the integer's first byte.
523
+ * @returns The integer's length in bytes, which may run past the end of `bytes`; or 0.
455
524
  */
456
525
  export const quicVarintLength = (bytes: u8[], at: i32): i32 => {
457
526
  if (at < 0 || at >= toI32(bytes.length)) {
@@ -465,6 +534,11 @@ export const quicVarintLength = (bytes: u8[], at: i32): i32 => {
465
534
  * from `bytes[at .. end)`. Answers -1, which no varint holds, when it does not
466
535
  * fit in that window or the window is not inside `bytes`. Advance past it by
467
536
  * `quicVarintLength(bytes, at)`.
537
+ *
538
+ * @param bytes The bytes holding the integer.
539
+ * @param at The byte offset of the integer's first byte.
540
+ * @param end The byte offset the integer must end at or before, at most `bytes.length`.
541
+ * @returns The integer, 0 to 2^62 - 1, or -1 when it cannot be read.
468
542
  */
469
543
  export const quicVarintRead = (bytes: u8[], at: i32, end: i32): i64 => {
470
544
  const size: i32 = quicVarintLength(bytes, at)
@@ -484,6 +558,10 @@ export const quicVarintRead = (bytes: u8[], at: i32, end: i32): i64 => {
484
558
  * or -1 if none: enough that twice the unacknowledged range fits, 1 to 4.
485
559
  * Answers 0 when no length will do — `fullPn` not above `largestAcked`, not a
486
560
  * packet number, or more than 2^31 packets unacknowledged.
561
+ *
562
+ * @param fullPn The packet number about to be sent, 0 to 2^62 - 1.
563
+ * @param largestAcked The largest packet number the peer has acknowledged in this space, or -1 for none.
564
+ * @returns The packet-number length in bytes, 1 to 4, or 0 when none will do.
487
565
  */
488
566
  export const quicPacketNumberLength = (fullPn: i64, largestAcked: i64): i32 => {
489
567
  if (fullPn < 0 || fullPn > QUIC_MAX_VARINT || largestAcked < -1 || fullPn <= largestAcked) {
@@ -508,6 +586,11 @@ export const quicPacketNumberLength = (fullPn: i64, largestAcked: i64): i32 => {
508
586
  * that space, or -1 for none (RFC 9000 §17.1, §A.3): the one closest to
509
587
  * `largestPn + 1` whose low bytes are `truncatedPn`. Answers -1 when
510
588
  * `pnLength` is not 1 to 4 or `truncatedPn` does not fit in it.
589
+ *
590
+ * @param largestPn The largest packet number processed in this space, or -1 for none.
591
+ * @param truncatedPn The packet number's low bytes as the header carried them, below 2^(8 × `pnLength`).
592
+ * @param pnLength The packet number's length on the wire, 1 to 4 bytes.
593
+ * @returns The full packet number, 0 to 2^62 - 1, or -1 for an argument out of range.
511
594
  */
512
595
  export const quicPacketNumberDecode = (largestPn: i64, truncatedPn: i64, pnLength: i32): i64 => {
513
596
  if (pnLength < 1 || pnLength > 4 || largestPn < -1 || largestPn > QUIC_MAX_VARINT) {
@@ -543,7 +626,16 @@ const quicPacketCidFits = (cid: u8[]): boolean => toI32(cid.length) <= QUIC_MAX_
543
626
  const quicPacketCidWindowFits = (cid: u8[], length: i32): boolean =>
544
627
  length >= 0 && length <= QUIC_MAX_CID_LENGTH && length <= toI32(cid.length)
545
628
 
546
- /** Writes `bytes[from .. from + length)` into `buf` at `at`; the caller has checked both windows. */
629
+ /**
630
+ * Writes `bytes[from .. from + length)` into `buf` at `at`; the caller has checked both windows.
631
+ * A byte outside either window is skipped rather than written, so a wrong window cannot panic.
632
+ *
633
+ * @param buf The buffer to write into; it is not grown.
634
+ * @param at The byte offset in `buf` of the first byte written.
635
+ * @param bytes The bytes to copy from.
636
+ * @param from The byte offset in `bytes` of the first byte copied.
637
+ * @param length How many bytes to copy.
638
+ */
547
639
  export const quicPacketCopy = (buf: u8[], at: i32, bytes: u8[], from: i32, length: i32): void => {
548
640
  for (let k: i32 = 0; k < length; k += 1) {
549
641
  if (at + k >= 0 && at + k < toI32(buf.length) && from + k >= 0 && from + k < toI32(bytes.length)) {
@@ -601,6 +693,16 @@ const quicPacketPutNumber = (buf: u8[], at: i32, packetNumber: i64, pnLength: i3
601
693
  /**
602
694
  * The size of the long header `quicPutLongHeader` writes for these
603
695
  * arguments, or 0 when it would refuse them (see `quicLongHeader`).
696
+ *
697
+ * @param type `QUIC_PACKET_INITIAL`, `QUIC_PACKET_ZERO_RTT` or `QUIC_PACKET_HANDSHAKE`.
698
+ * @param dcidLength The Destination Connection ID's length in bytes, 0 to 20.
699
+ * @param scidLength The Source Connection ID's length in bytes, 0 to 20.
700
+ * @param tokenLength The token's length in bytes; 0 for anything but an Initial.
701
+ * @param packetNumber The full packet number, 0 to 2^62 - 1; only range-checked, as the size does not
702
+ * depend on it.
703
+ * @param pnLength The packet number's length on the wire, 1 to 4 bytes.
704
+ * @param payloadLength The plaintext payload's length in bytes, without the AEAD tag.
705
+ * @returns The header's length in bytes, packet number included, or 0 for an argument out of range.
604
706
  */
605
707
  export const quicLongHeaderSize = (
606
708
  type: i32,
@@ -638,6 +740,20 @@ export const quicLongHeaderSize = (
638
740
  * bytes of `dcid` and `scid`, and answers the offset past it: where the
639
741
  * payload goes. Answers -1, writing nothing, where `quicLongHeader` answers
640
742
  * `null`, and for a header that does not fit in `buf` from `at`.
743
+ *
744
+ * @param buf The buffer to write into; it is not grown.
745
+ * @param at The byte offset in `buf` where the packet starts.
746
+ * @param type `QUIC_PACKET_INITIAL`, `QUIC_PACKET_ZERO_RTT` or `QUIC_PACKET_HANDSHAKE`.
747
+ * @param dcid The bytes holding the Destination Connection ID at their start.
748
+ * @param dcidLength How many bytes of `dcid` are the ID, 0 to 20.
749
+ * @param scid The bytes holding the Source Connection ID at their start.
750
+ * @param scidLength How many bytes of `scid` are the ID, 0 to 20.
751
+ * @param token The whole of an Initial's token; empty for the other types.
752
+ * @param packetNumber The full packet number, 0 to 2^62 - 1; its low `pnLength` bytes are written.
753
+ * @param pnLength The packet number's length on the wire, 1 to 4 bytes.
754
+ * @param payloadLength The plaintext payload's length in bytes, without the AEAD tag.
755
+ * @returns The byte offset one past the packet number, where the payload goes, or -1 when nothing was
756
+ * written.
641
757
  */
642
758
  export const quicPutLongHeader = (
643
759
  buf: u8[],
@@ -703,6 +819,15 @@ export const quicPutLongHeader = (
703
819
  * on a non-Initial packet, a `pnLength` outside 1 to 4, a packet number that
704
820
  * is not one, a negative `payloadLength`, or a Length past 16383, the most two
705
821
  * bytes hold and far more than a datagram carries.
822
+ *
823
+ * @param type `QUIC_PACKET_INITIAL`, `QUIC_PACKET_ZERO_RTT` or `QUIC_PACKET_HANDSHAKE`.
824
+ * @param dcid The Destination Connection ID, 0 to 20 bytes.
825
+ * @param scid The Source Connection ID, 0 to 20 bytes.
826
+ * @param token An Initial's token, or empty.
827
+ * @param packetNumber The full packet number, 0 to 2^62 - 1; its low `pnLength` bytes are written.
828
+ * @param pnLength The packet number's length on the wire, 1 to 4 bytes.
829
+ * @param payloadLength The plaintext payload's length in bytes, without the AEAD tag.
830
+ * @returns A new array holding the header, or `null` for an argument out of range.
706
831
  */
707
832
  export const quicLongHeader = (
708
833
  type: i32,
@@ -753,6 +878,18 @@ export const quicLongHeader = (
753
878
  * answers, with the Destination Connection ID the first `dcidLength` bytes of
754
879
  * `dcid`, and answers the offset past it. Answers -1, writing nothing, where
755
880
  * `quicShortHeader` answers `null`, and for a header that does not fit.
881
+ *
882
+ * @param buf The buffer to write into; it is not grown.
883
+ * @param at The byte offset in `buf` where the packet starts.
884
+ * @param dcid The bytes holding the Destination Connection ID at their start.
885
+ * @param dcidLength How many bytes of `dcid` are the ID, 0 to 20.
886
+ * @param spin The latency spin bit (RFC 9000 §17.4).
887
+ * @param keyPhase The Key Phase bit, which says which generation of 1-RTT keys protects the packet
888
+ * (RFC 9001 §6).
889
+ * @param packetNumber The full packet number, 0 to 2^62 - 1; its low `pnLength` bytes are written.
890
+ * @param pnLength The packet number's length on the wire, 1 to 4 bytes.
891
+ * @returns The byte offset one past the packet number, where the payload goes, or -1 when nothing was
892
+ * written.
756
893
  */
757
894
  export const quicPutShortHeader = (
758
895
  buf: u8[],
@@ -789,6 +926,14 @@ export const quicPutShortHeader = (
789
926
  * and the packet number in `pnLength` bytes. Answers `null` for a connection
790
927
  * ID over 20 bytes, a `pnLength` outside 1 to 4 or a packet number that is
791
928
  * not one.
929
+ *
930
+ * @param dcid The Destination Connection ID, 0 to 20 bytes.
931
+ * @param spin The latency spin bit (RFC 9000 §17.4).
932
+ * @param keyPhase The Key Phase bit, which says which generation of 1-RTT keys protects the packet
933
+ * (RFC 9001 §6).
934
+ * @param packetNumber The full packet number, 0 to 2^62 - 1; its low `pnLength` bytes are written.
935
+ * @param pnLength The packet number's length on the wire, 1 to 4 bytes.
936
+ * @returns A new array holding the header, or `null` for an argument out of range.
792
937
  */
793
938
  export const quicShortHeader = (
794
939
  dcid: u8[],
@@ -836,6 +981,13 @@ const quicPacketCidLength = (bytes: u8[], at: i32, end: i32): i32 => {
836
981
  * as they were. The datagram is `datagram[at .. limit)`, so one buffer can
837
982
  * hold several (a GRO receive); a `limit` past the array reads as its length.
838
983
  * See `quicParseHeader` for what is read and refused.
984
+ *
985
+ * @param header The header to overwrite; its windows index into `datagram`.
986
+ * @param datagram The received bytes, only read.
987
+ * @param at The byte offset where the packet starts.
988
+ * @param limit The byte offset one past the datagram's end.
989
+ * @param shortDcidLength The length in bytes of the connection IDs this endpoint hands out, 0 to 20.
990
+ * @returns `QUIC_PACKET_OK`, or the `QUIC_ERR_*` that stopped the parse.
839
991
  */
840
992
  export const quicParseHeaderInto = (
841
993
  header: QuicHeader,
@@ -957,6 +1109,11 @@ export const quicParseHeaderInto = (
957
1109
  * answer's arrays; `quicParseHeaderInto` reads the same without a copy.
958
1110
  *
959
1111
  * The answer's `error` says what stopped it; nothing a peer sends panics.
1112
+ *
1113
+ * @param datagram The received bytes, only read; the datagram runs to the array's end.
1114
+ * @param at The byte offset where the packet starts.
1115
+ * @param shortDcidLength The length in bytes of the connection IDs this endpoint hands out, 0 to 20.
1116
+ * @returns A new `QuicHeader`; read its `error` before any other field.
960
1117
  */
961
1118
  export const quicParseHeader = (datagram: u8[], at: i32, shortDcidLength: i32): QuicHeader => {
962
1119
  const header: QuicHeader = new QuicHeader()
@@ -975,6 +1132,9 @@ export const quicParseHeader = (datagram: u8[], at: i32, shortDcidLength: i32):
975
1132
  * ID, `dcid`: `initial_secret` is HKDF-Extract under version 1's salt, and the
976
1133
  * client's and the server's are expanded from it with the labels `client in`
977
1134
  * and `server in`. Answers `null` for a `dcid` over 20 bytes.
1135
+ *
1136
+ * @param dcid The Destination Connection ID of the client's first Initial, 0 to 20 bytes.
1137
+ * @returns The two 32-byte secrets as plain arrays, which are not secret, or `null`.
978
1138
  */
979
1139
  export const quicInitialSecrets = (dcid: u8[]): QuicInitialSecrets | null => {
980
1140
  if (!quicPacketCidFits(dcid)) {
@@ -1048,21 +1208,28 @@ const quicPacketKeysUsable = (keys: QuicKeys): boolean => {
1048
1208
  }
1049
1209
 
1050
1210
  /**
1051
- * The packet-protection keys of RFC 9001 §5.1 from a traffic secret, held as
1052
- * a `Secret` the caller still owns and wipes, derived into `slot`, which the
1211
+ * The packet-protection keys of RFC 9001 §5.1 from a traffic secret, held as a
1212
+ * `Secret` the caller still owns and wipes, derived into `slot`, which the
1053
1213
  * caller owns too: the AEAD key (`quic key`), the IV (`quic iv`) and the
1054
1214
  * header-protection key (`quic hp`), each HKDF-Expand-Label with an empty
1055
- * context under the hash `aead` pairs with, written into the slot's arrays
1056
- * and the AES schedules expanded there. The secret is read through a plain
1057
- * copy inside an arena block, which is zeroed, and the derivation's
1058
- * temporaries go with the block, so the call keeps in the arena only the
1059
- * `HkdfScratch` it makes for the derivation, which wipes itself before the
1060
- * derivation returns.
1061
- * Answers the slot's keys, which the next derivation into it overwrites and
1062
- * `QuicKeysSlot.wipe` zeroes, or `null`, deriving nothing, for an `aead`
1063
- * this module does not know or a secret that is not that hash's length. The
1064
- * Initial secrets are public (RFC 9001 §5.2), so a caller deriving Initial
1065
- * keys wraps a copy of one in `secret`, or uses `quicKeysInto`.
1215
+ * context under the hash `aead` pairs with, written into the slot's arrays and
1216
+ * the AES schedules expanded there.
1217
+ *
1218
+ * The secret is read through a plain copy inside an arena block, which is
1219
+ * zeroed, and the derivation's temporaries go with the block, so the call keeps
1220
+ * in the arena only the `HkdfScratch` it makes for the derivation, which wipes
1221
+ * itself before the derivation returns. Answers the slot's keys, which the next
1222
+ * derivation into it overwrites and `QuicKeysSlot.wipe` zeroes, or `null`,
1223
+ * deriving nothing, for an `aead` this module does not know or a secret that is
1224
+ * not that hash's length. The Initial secrets are public (RFC 9001 §5.2), so a
1225
+ * caller deriving Initial keys wraps a copy of one in `secret`, or uses
1226
+ * `quicKeysInto`.
1227
+ *
1228
+ * @param slot The caller's slot to derive into; its earlier keys are overwritten.
1229
+ * @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
1230
+ * @param trafficSecret The level's traffic secret, 32 bytes (48 for AES-256-GCM); borrowed: it is
1231
+ * not wiped here, and the caller wipes it once no more keys come from it.
1232
+ * @returns `slot.keys`, valid until the slot is derived into again or wiped, or `null`.
1066
1233
  */
1067
1234
  export const quicKeys = (slot: QuicKeysSlot, aead: i32, trafficSecret: Secret<u8[]>): QuicKeys | null => {
1068
1235
  if (!quicPacketHeldFits(aead, trafficSecret)) {
@@ -1084,6 +1251,10 @@ export const quicKeys = (slot: QuicKeysSlot, aead: i32, trafficSecret: Secret<u8
1084
1251
  * answered as a `Secret` the caller wipes. The header-protection key does
1085
1252
  * not change with it (§6), so the new keys are `quicKeysUpdate` of the old
1086
1253
  * keys and this. Answers `null` where `quicKeys` would.
1254
+ *
1255
+ * @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
1256
+ * @param trafficSecret The current generation's traffic secret; borrowed, and left for the caller to wipe.
1257
+ * @returns The next generation's secret, a new `Secret` the caller owns and wipes, or `null`.
1087
1258
  */
1088
1259
  export const quicKeyUpdateSecret = (aead: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> | null => {
1089
1260
  if (!quicPacketHeldFits(aead, trafficSecret)) {
@@ -1094,15 +1265,21 @@ export const quicKeyUpdateSecret = (aead: i32, trafficSecret: Secret<u8[]>): Sec
1094
1265
 
1095
1266
  /**
1096
1267
  * The next generation's keys in a key update (RFC 9001 §6), derived into
1097
- * `slot`, which the caller owns and which must not be the slot `keys` live
1098
- * in: the packet key and IV from `nextSecret`, which `quicKeyUpdateSecret`
1099
- * made from the current secret and the caller still owns, read through a
1100
- * plain copy inside an arena block that is zeroed, and `keys`'
1101
- * header-protection key kept, since header protection never changes with
1102
- * the phase. The new generation holds its own copy of that key and its own
1103
- * schedule, in the slot, so wiping one generation leaves the other usable.
1104
- * Answers the slot's keys, or `null`, deriving nothing, for keys `quicKeys`
1105
- * did not make, keys that live in `slot`, or a secret of the wrong length.
1268
+ * `slot`, which the caller owns and which must not be the slot `keys` live in:
1269
+ * the packet key and IV from `nextSecret`, which `quicKeyUpdateSecret` made
1270
+ * from the current secret and the caller still owns, read through a plain copy
1271
+ * inside an arena block that is zeroed, and `keys`' header-protection key kept,
1272
+ * since header protection never changes with the phase.
1273
+ *
1274
+ * The new generation holds its own copy of that key and its own schedule, in
1275
+ * the slot, so wiping one generation leaves the other usable. Answers the
1276
+ * slot's keys, or `null`, deriving nothing, for keys `quicKeys` did not make,
1277
+ * keys that live in `slot`, or a secret of the wrong length.
1278
+ *
1279
+ * @param slot The caller's slot for the next generation; a different slot from the one `keys` live in.
1280
+ * @param keys The current generation's keys, whose header-protection key is copied; left unchanged.
1281
+ * @param nextSecret The next generation's traffic secret; borrowed, and left for the caller to wipe.
1282
+ * @returns `slot.keys`, valid until the slot is derived into again or wiped, or `null`.
1106
1283
  */
1107
1284
  export const quicKeysUpdate = (
1108
1285
  slot: QuicKeysSlot,
@@ -1136,6 +1313,9 @@ export const quicKeysUpdate = (
1136
1313
  * kernels' powers of H included) and H itself. It zeroes the arrays `keys`
1137
1314
  * points at now; a slot's arrays of the other lengths are
1138
1315
  * `QuicKeysSlot.wipe`'s, which zeroes them all.
1316
+ *
1317
+ * @param keys The keys to zero. Nothing marks them wiped and nothing refuses them afterwards, so derive
1318
+ * into them again before sealing or opening with them.
1139
1319
  */
1140
1320
  export const quicKeysWipe = (keys: QuicKeys): void => {
1141
1321
  secureZero(keys.key)
@@ -1166,15 +1346,27 @@ export const quicKeysWipe = (keys: QuicKeys): void => {
1166
1346
  export class QuicKeysSlot {
1167
1347
  /** The keys, pointing into the arrays below; `aead` is 0 until something is derived. */
1168
1348
  keys: QuicKeys
1349
+ /** The 16-byte packet key an AES-128-GCM suite uses. */
1169
1350
  key16: u8[]
1351
+ /** The 32-byte packet key an AES-256-GCM or ChaCha20-Poly1305 suite uses. */
1170
1352
  key32: u8[]
1353
+ /** The 16-byte header-protection key an AES-128-GCM suite uses. */
1171
1354
  hp16: u8[]
1355
+ /** The 32-byte header-protection key an AES-256-GCM or ChaCha20-Poly1305 suite uses. */
1172
1356
  hp32: u8[]
1357
+ /** The AES-128 key schedule for packet protection, expanded from `key16`. */
1173
1358
  packet128: AesKey
1359
+ /** The AES-256 key schedule for packet protection, expanded from `key32`. */
1174
1360
  packet256: AesKey
1361
+ /** The AES-128 key schedule for header protection, expanded from `hp16`. */
1175
1362
  hp128: AesKey
1363
+ /** The AES-256 key schedule for header protection, expanded from `hp32`. */
1176
1364
  hp256: AesKey
1177
1365
 
1366
+ /**
1367
+ * Makes the slot's arrays and key schedules once, with no keys derived yet
1368
+ * (`keys.aead` is 0).
1369
+ */
1178
1370
  constructor() {
1179
1371
  this.key16 = new Array<u8>(16)
1180
1372
  this.key32 = new Array<u8>(32)
@@ -1223,7 +1415,7 @@ const quicPacketPoint = (slot: QuicKeysSlot, aead: i32): void => {
1223
1415
  /**
1224
1416
  * `quicPacketDerive` in place: the packet key and IV from `secret` into
1225
1417
  * `keys`' own arrays, and for the AES suites the key's schedule expanded
1226
- * into its own `AesKey`. HKDF runs in `kdf`, which wipes what it held, and
1418
+ * into its own `AesKey`. HKDF runs in `kdf`, which wipes its secret-bearing fields, and
1227
1419
  * the expansion's temporaries go with the arena block.
1228
1420
  */
1229
1421
  const quicPacketDeriveInto = (kdf: HkdfScratch, keys: QuicKeys, secret: u8[]): void => {
@@ -1245,6 +1437,14 @@ const quicPacketDeriveInto = (kdf: HkdfScratch, keys: QuicKeys, secret: u8[]): v
1245
1437
  * `kdf`, with the AES schedules expanded in place. Answers the slot's keys,
1246
1438
  * or `null`, deriving nothing, where `quicKeys` answers `null`. The keys
1247
1439
  * are the slot's: the next derivation into it overwrites them.
1440
+ *
1441
+ * @param kdf The caller's HKDF scratch, reused across derivations; its secret-bearing fields are wiped
1442
+ * before each returns (its `info` label is not).
1443
+ * @param slot The caller's slot to derive into; its earlier keys are overwritten.
1444
+ * @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
1445
+ * @param secret The traffic secret as a plain array, 32 bytes (48 for AES-256-GCM); only read, and not
1446
+ * wiped, so a caller passing a non-Initial secret wipes it.
1447
+ * @returns `slot.keys`, valid until the slot is derived into again or wiped, or `null`.
1248
1448
  */
1249
1449
  export const quicKeysInto = (
1250
1450
  kdf: HkdfScratch,
@@ -1286,6 +1486,16 @@ export const quicKeysInto = (
1286
1486
  * the next generation's packet key and IV from `nextSecret`, and `keys`'
1287
1487
  * header-protection key shared, as every generation shares it (RFC 9001
1288
1488
  * §6). Answers the slot's keys, or `null` where `quicKeysUpdate` does.
1489
+ * Unlike `quicKeysUpdate` the header-protection key and its schedule are the
1490
+ * same arrays as `keys`', not a copy, so wiping either generation zeroes the
1491
+ * header-protection key both use.
1492
+ *
1493
+ * @param kdf The caller's HKDF scratch, reused across derivations; its secret-bearing fields are wiped
1494
+ * before each returns (its `info` label is not).
1495
+ * @param slot The caller's slot for the next generation; a different slot from the one `keys` live in.
1496
+ * @param keys The current generation's keys, whose header-protection arrays are shared; left unchanged.
1497
+ * @param nextSecret The next generation's traffic secret as a plain array; only read, and not wiped.
1498
+ * @returns `slot.keys`, valid until the slot is derived into again or wiped, or `null`.
1289
1499
  */
1290
1500
  export const quicKeysUpdateInto = (
1291
1501
  kdf: HkdfScratch,
@@ -1308,6 +1518,13 @@ export const quicKeysUpdateInto = (
1308
1518
  * `quicKeyUpdateSecret` into `out`, which must be exactly the secret's
1309
1519
  * length and not `secret` itself. Answers whether it derived, which it does
1310
1520
  * where `quicKeyUpdateSecret` answers a secret.
1521
+ *
1522
+ * @param kdf The caller's HKDF scratch, reused across derivations; its secret-bearing fields are wiped
1523
+ * before each returns (its `info` label is not).
1524
+ * @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
1525
+ * @param secret The current traffic secret as a plain array; only read, and not wiped.
1526
+ * @param out The array the next secret is written over, 32 bytes (48 for AES-256-GCM).
1527
+ * @returns True when `out` holds the next secret; false, `out` untouched, for a wrong length or `aead`.
1311
1528
  */
1312
1529
  export const quicKeyUpdateSecretInto = (kdf: HkdfScratch, aead: i32, secret: u8[], out: u8[]): boolean => {
1313
1530
  const length: i32 = quicPacketSecretLength(aead)
@@ -1326,6 +1543,11 @@ export const quicKeyUpdateSecretInto = (kdf: HkdfScratch, aead: i32, secret: u8[
1326
1543
  * the plain output of each derivation wiped, and the AES schedules expanded
1327
1544
  * in place inside an arena block. Answers the slot's keys, or `null`,
1328
1545
  * deriving nothing, where `quicKeys` answers `null`.
1546
+ *
1547
+ * @param slot The caller's slot to derive into; its earlier keys are overwritten.
1548
+ * @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
1549
+ * @param secret The traffic secret, 32 bytes (48 for AES-256-GCM); borrowed, and left for the caller to wipe.
1550
+ * @returns `slot.keys`, valid until the slot is derived into again or wiped, or `null`.
1329
1551
  */
1330
1552
  export const quicKeysHeldInto = (slot: QuicKeysSlot, aead: i32, secret: Secret<u8[]>): QuicKeys | null => {
1331
1553
  if (!quicPacketHeldFits(aead, secret)) {
@@ -1357,6 +1579,10 @@ export const quicKeysHeldInto = (slot: QuicKeysSlot, aead: i32, secret: Secret<u
1357
1579
  * bytes in place, under SHA-384 for a 48-byte secret and SHA-256 for a
1358
1580
  * 32-byte one. `dst` may be `src`, which ratchets it. Answers whether it
1359
1581
  * derived: not unless both hold 32 bytes or both 48.
1582
+ *
1583
+ * @param dst The secret overwritten with the next generation's; the caller's, and the caller wipes it.
1584
+ * @param src The current generation's secret; borrowed, and not wiped here unless it is `dst`.
1585
+ * @returns True when `dst` holds the next secret; false, both untouched, for mismatched or wrong lengths.
1360
1586
  */
1361
1587
  export const quicKeyUpdateSecretHeldInto = (dst: Secret<u8[]>, src: Secret<u8[]>): boolean => {
1362
1588
  const length: i32 = expose(src, quicPacketBytesLength)
@@ -1377,6 +1603,15 @@ export const quicKeyUpdateSecretHeldInto = (dst: Secret<u8[]>, src: Secret<u8[]>
1377
1603
  * length)`, read in place, into `client` and `server`, 32 bytes each.
1378
1604
  * Answers whether it derived: not for an ID over 20 bytes, a window outside
1379
1605
  * `buf`, or an output of another length.
1606
+ *
1607
+ * @param kdf The caller's HKDF scratch, reused across derivations; its secret-bearing fields are wiped
1608
+ * before each returns (its `info` label is not).
1609
+ * @param buf The bytes holding the client's first Destination Connection ID, such as the datagram.
1610
+ * @param at The byte offset in `buf` where the ID starts.
1611
+ * @param length The ID's length in bytes, 0 to 20.
1612
+ * @param client The 32-byte array `client_initial_secret` is written over.
1613
+ * @param server The 32-byte array `server_initial_secret` is written over.
1614
+ * @returns True when both secrets were written; false, both untouched, otherwise.
1380
1615
  */
1381
1616
  export const quicInitialSecretsInto = (
1382
1617
  kdf: HkdfScratch,
@@ -1472,6 +1707,14 @@ const quicPacketOpen = (keys: QuicKeys, nonce: u8[], aad: u8[], sealed: u8[]): u
1472
1707
  * `buf` not to be sent, for anything `quicSealPacket` refuses and for a tag
1473
1708
  * that does not fit in `buf`. The AEAD's temporaries are released before it
1474
1709
  * answers, so sealing allocates nothing that outlives the call.
1710
+ *
1711
+ * @param keys The level's keys, as `quicKeys` or one of its `Into` forms made them.
1712
+ * @param buf The buffer holding the packet; it is not grown, so it needs 16 bytes of room past the payload.
1713
+ * @param start The byte offset in `buf` where the packet starts.
1714
+ * @param headerLength The header's length in bytes, packet number included.
1715
+ * @param payloadLength The plaintext payload's length in bytes; with the packet number, at least 4.
1716
+ * @param packetNumber The full packet number the header carries, 0 to 2^62 - 1.
1717
+ * @returns The byte offset one past the AEAD tag, or -1 when the packet is not to be sent.
1475
1718
  */
1476
1719
  export const quicSealInPlace = (
1477
1720
  keys: QuicKeys,
@@ -1568,6 +1811,12 @@ export const quicSealInPlace = (
1568
1811
  * must be at least 4 bytes; a shorter payload has to be padded first (RFC 9001
1569
1812
  * §5.4.2). Answers `null` for any of those, for a header too short for its
1570
1813
  * packet-number length, and for keys `quicKeys` did not make.
1814
+ *
1815
+ * @param keys The level's keys, as `quicKeys` or one of its `Into` forms made them.
1816
+ * @param header The unprotected header, only read.
1817
+ * @param packetNumber The full packet number the header carries, 0 to 2^62 - 1.
1818
+ * @param payload The plaintext frames, only read.
1819
+ * @returns A new array holding the protected packet, 16 bytes longer than header and payload, or `null`.
1571
1820
  */
1572
1821
  export const quicSealPacket = (
1573
1822
  keys: QuicKeys,
@@ -1594,6 +1843,13 @@ export const quicSealPacket = (
1594
1843
  * associated data: empty when `error` is not `QUIC_PACKET_OK`. Only numbers
1595
1844
  * are stored into `packet`, so a connection reuses one for every packet and
1596
1845
  * the answer is its own to let go. See `quicRemoveHeaderProtection`.
1846
+ *
1847
+ * @param keys The level's keys, as `quicKeys` or one of its `Into` forms made them.
1848
+ * @param datagram The received bytes `header` was parsed from, only read.
1849
+ * @param header The packet's parsed header.
1850
+ * @param largestPn The largest packet number processed in the packet's space, or -1 for none.
1851
+ * @param packet The result to overwrite; its `header` and `payload` arrays are not touched.
1852
+ * @returns A new array holding the header in the clear, or an empty one when `packet.error` is set.
1597
1853
  */
1598
1854
  export const quicUnprotectHeader = (
1599
1855
  keys: QuicKeys,
@@ -1686,6 +1942,12 @@ export const quicUnprotectHeader = (
1686
1942
  * `error` is the header's own for a header that did not parse,
1687
1943
  * `QUIC_ERR_NOT_PROTECTED` for a Retry, `QUIC_ERR_KEYS` for keys `quicKeys`
1688
1944
  * did not make, and `QUIC_ERR_SAMPLE` for a packet too short to sample.
1945
+ *
1946
+ * @param keys The level's keys; the whole set must be usable, though only the header-protection key is used.
1947
+ * @param datagram The received bytes `header` was parsed from, only read.
1948
+ * @param header The packet's parsed header.
1949
+ * @param largestPn The largest packet number processed in the packet's space, or -1 for none.
1950
+ * @returns A new `QuicPacket` with no payload yet; read its `error` before any other field.
1689
1951
  */
1690
1952
  export const quicRemoveHeaderProtection = (
1691
1953
  keys: QuicKeys,
@@ -1704,6 +1966,13 @@ export const quicRemoveHeaderProtection = (
1704
1966
  * changing `datagram`. Sets `packet.error` as `quicDecryptPacket` does and
1705
1967
  * answers the plaintext frames, or `null` when they did not authenticate
1706
1968
  * (or `packet` was already in error). Only numbers are stored into `packet`.
1969
+ *
1970
+ * @param keys The level's keys, as `quicKeys` or one of its `Into` forms made them.
1971
+ * @param datagram The received bytes `header` was parsed from, only read.
1972
+ * @param header The packet's parsed header.
1973
+ * @param clear What `quicUnprotectHeader` answered for this packet: the AEAD's associated data.
1974
+ * @param packet The result `quicUnprotectHeader` filled for this packet; only its `error` is changed.
1975
+ * @returns A new array holding the frames, also for `QUIC_ERR_RESERVED_BITS`, or `null`.
1707
1976
  */
1708
1977
  export const quicDecryptPayload = (
1709
1978
  keys: QuicKeys,
@@ -1748,6 +2017,13 @@ export const quicDecryptPayload = (
1748
2017
  * does but the header's reserved bits are not zero (RFC 9000 §17.2, §17.3.1),
1749
2018
  * which the caller is to close the connection over. Answers whether the
1750
2019
  * payload authenticated. A packet already in error is left as it is.
2020
+ *
2021
+ * @param keys The level's keys, as `quicKeys` or one of its `Into` forms made them.
2022
+ * @param datagram The received bytes `header` was parsed from, only read.
2023
+ * @param header The packet's parsed header.
2024
+ * @param packet What `quicRemoveHeaderProtection` answered for this packet.
2025
+ * @returns True when the payload authenticated, `QUIC_ERR_RESERVED_BITS` included, so read
2026
+ * `packet.error` too.
1751
2027
  */
1752
2028
  export const quicDecryptPacket = (
1753
2029
  keys: QuicKeys,
@@ -1769,6 +2045,12 @@ export const quicDecryptPacket = (
1769
2045
  * payload decrypted. The answer's `error` is `QUIC_PACKET_OK` with `payload`
1770
2046
  * holding the frames, or the `QUIC_ERR_*` of the step that failed — see
1771
2047
  * `quicRemoveHeaderProtection` and `quicDecryptPacket`.
2048
+ *
2049
+ * @param keys The level's keys, as `quicKeys` or one of its `Into` forms made them.
2050
+ * @param datagram The received bytes `header` was parsed from, only read.
2051
+ * @param header The packet's parsed header.
2052
+ * @param largestPn The largest packet number processed in the packet's space, or -1 for none.
2053
+ * @returns A new `QuicPacket`; read its `error` before any other field.
1772
2054
  */
1773
2055
  export const quicOpenPacket = (
1774
2056
  keys: QuicKeys,
@@ -1787,6 +2069,10 @@ export const quicOpenPacket = (
1787
2069
  * `odcid`'s length and bytes, then the Retry packet up to its tag — as the
1788
2070
  * associated data. `odcid` is the Destination Connection ID of the client's
1789
2071
  * Initial the Retry answers. Answers `null` for an `odcid` over 20 bytes.
2072
+ *
2073
+ * @param odcid The Destination Connection ID of the client's first Initial, 0 to 20 bytes.
2074
+ * @param retryWithoutTag The Retry packet from its first byte to just before the tag, only read.
2075
+ * @returns The 16-byte tag as a new array, or `null`.
1790
2076
  */
1791
2077
  export const quicRetryIntegrityTag = (odcid: u8[], retryWithoutTag: u8[]): u8[] | null => {
1792
2078
  const key: AesKey | null = aesKey(quicPacketRetryKey())
@@ -1808,6 +2094,13 @@ export const quicRetryIntegrityTag = (odcid: u8[], retryWithoutTag: u8[]): u8[]
1808
2094
  * DCID the client's Initial was sent to. Answers `null` for a connection ID
1809
2095
  * over 20 bytes, an empty token (a client discards that Retry, §17.2.5.2) or
1810
2096
  * `unused` outside 0 to 15.
2097
+ *
2098
+ * @param dcid The client's Source Connection ID, which the Retry is sent to; 0 to 20 bytes.
2099
+ * @param scid The server's new Source Connection ID, 0 to 20 bytes.
2100
+ * @param token The address-validation token the client is to echo; at least one byte.
2101
+ * @param odcid The Destination Connection ID of the client's first Initial, 0 to 20 bytes.
2102
+ * @param unused The first byte's four low bits, 0 to 15.
2103
+ * @returns A new array holding the whole Retry, tag included, or `null`.
1811
2104
  */
1812
2105
  export const quicRetryPacket = (
1813
2106
  dcid: u8[],
@@ -1843,6 +2136,13 @@ export const quicRetryPacket = (
1843
2136
  * `odcid`, the DCID of the Initial this client sent (RFC 9001 §5.8). The tag
1844
2137
  * is recomputed over the datagram's bytes and compared in constant time.
1845
2138
  * Answers `false` for a header that is not a Retry that parsed.
2139
+ *
2140
+ * @param odcid The Destination Connection ID of the Initial this client sent.
2141
+ * @param datagram The received bytes `header` was parsed from, only read.
2142
+ * @param header The Retry's header from `quicParseHeader`, which copies out `retryTag`;
2143
+ * `quicParseHeaderInto` leaves that array as it was.
2144
+ * @returns True when the integrity tag is valid for `odcid`: the packet was not altered and answers the
2145
+ * Initial that carried that DCID. It authenticates nothing more (RFC 9001 §5.8).
1846
2146
  */
1847
2147
  export const quicRetryVerify = (odcid: u8[], datagram: u8[], header: QuicHeader): boolean => {
1848
2148
  if (header.error !== QUIC_PACKET_OK || header.type !== QUIC_PACKET_RETRY) {