nish 0.44.0 → 0.46.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 (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -116,7 +116,7 @@ import {
116
116
  } from "nish/crypto/aes"
117
117
  import { chacha20HeaderMask, chacha20Poly1305Open, chacha20Poly1305Seal } from "nish/crypto/chacha20poly1305"
118
118
  import { timingSafeEqual } from "nish/crypto/ct"
119
- import { copyBytes } from "nish/bytes"
119
+ import { tryCopyBytes } from "nish/bytes"
120
120
  import {
121
121
  HkdfScratch,
122
122
  hkdfExpandLabelHeldInto,
@@ -377,23 +377,43 @@ const quicPacketExpandLabel = (aead: i32, secret: u8[], label: string, length: i
377
377
  ? hkdfExpandLabelSha384(secret, label, [], length)
378
378
  : hkdfExpandLabelSha256(secret, label, [], length)
379
379
 
380
+ /**
381
+ * A zeroed array of `size` bytes, `size` at least 0. A size that fits a `u16`,
382
+ * every packet and connection ID, is one allocation whose length needs no
383
+ * check; a larger one is grown a byte at a time.
384
+ */
385
+ const quicPacketZeroed = (size: i32): u8[] => {
386
+ if (size >= 0 && size <= 65535) {
387
+ return new Array<u8>(toU16(size))
388
+ }
389
+ const out: u8[] = []
390
+ for (let k: i32 = 0; k < size; k += 1) {
391
+ out.push(toU8(0))
392
+ }
393
+ return out
394
+ }
395
+
396
+ /**
397
+ * Byte `k` of a header-protection mask. Every caller has checked the mask is
398
+ * 5 bytes and `k` at most 4; any other `k` answers 0, which leaves the byte
399
+ * it masks unchanged.
400
+ */
401
+ const quicPacketMaskByte = (mask: u8[], k: i32): u8 => (k >= 0 && k < toI32(mask.length) ? mask[k] : toU8(0))
402
+
380
403
  /**
381
404
  * The bytes `bytes[from .. to)` as a fresh array of exactly that size, so a
382
- * packet-sized copy is one allocation rather than a push-grown one; the
383
- * window is clamped to `bytes`, though every caller has checked it.
405
+ * packet-sized copy is one allocation rather than a push-grown one. Every
406
+ * caller has checked the window; one that is not inside `bytes` answers an
407
+ * empty array.
384
408
  */
385
409
  const quicPacketSlice = (bytes: u8[], from: i32, to: i32): u8[] => {
386
- const length: i32 = toI32(bytes.length)
387
- const start: i32 = from < 0 ? 0 : from
388
- const stop: i32 = to > length ? length : to
389
- const size: i32 = stop > start ? stop - start : 0
390
- const out: u8[] = new Array<u8>(size)
391
- const outLength: i32 = toI32(out.length)
392
- for (let k: i32 = 0; k < outLength; k += 1) {
393
- if (start + k < toI32(bytes.length)) {
394
- out[k] = bytes[start + k]
395
- }
410
+ if (from < 0 || to < from || to > toI32(bytes.length)) {
411
+ return []
396
412
  }
413
+ // `from` is 0 or more and `to` at least `from`, so the size is 0 to `to`.
414
+ const size: i32 = toI32(toI64(to) - toI64(from))
415
+ const out: u8[] = quicPacketZeroed(size)
416
+ tryCopyBytes(out, 0, bytes, from, size)
397
417
  return out
398
418
  }
399
419
 
@@ -442,7 +462,7 @@ export const quicVarintSize = (value: i64): i32 => {
442
462
  */
443
463
  export const quicVarintPut = (buf: u8[], at: i32, value: i64, size: i32): i32 => {
444
464
  const needed: i32 = quicVarintSize(value)
445
- if (needed === 0 || size < needed || at < 0 || at > toI32(buf.length) - size) {
465
+ if (needed === 0 || size < needed || size > 8 || at < 0 || toI64(at) + toI64(size) > toI64(buf.length)) {
446
466
  return -1
447
467
  }
448
468
  // The two-bit prefix is log2 of the size: 0, 1, 2 or 3 for 1, 2, 4 or 8 bytes.
@@ -456,17 +476,20 @@ export const quicVarintPut = (buf: u8[], at: i32, value: i64, size: i32): i32 =>
456
476
  } else if (size !== 1) {
457
477
  return -1
458
478
  }
479
+ // The first check put `at + size` inside `buf`, so it fits an `i32`, and
480
+ // every `j` below is inside `buf`: the bounds guard never skips a byte.
481
+ const end: i32 = toI32(toI64(at) + toI64(size))
459
482
  for (let k: i32 = 0; k < size; k += 1) {
460
- const shift: i64 = toI64(size - 1 - k) * 8
461
- let b: i32 = toI32((value >> shift) & 255)
483
+ let b: i32 = toI32((value >> (toI64(size - 1 - k) * 8)) & 255)
462
484
  if (k === 0) {
463
485
  b = b | (prefix << 6)
464
486
  }
465
- if (at + k < toI32(buf.length)) {
466
- buf[at + k] = toU8(b)
487
+ const j: i32 = toI32(toI64(at) + toI64(k))
488
+ if (j >= 0 && j < toI32(buf.length)) {
489
+ buf[j] = toU8(b)
467
490
  }
468
491
  }
469
- return at + size
492
+ return end
470
493
  }
471
494
 
472
495
  /**
@@ -542,13 +565,18 @@ export const quicVarintLength = (bytes: u8[], at: i32): i32 => {
542
565
  * @returns The integer, 0 to 2^62 - 1, or -1 when it cannot be read.
543
566
  */
544
567
  export const quicVarintRead = (bytes: u8[], at: i32, end: i32): i64 => {
545
- const size: i32 = quicVarintLength(bytes, at)
546
- if (size === 0 || end > toI32(bytes.length) || at + size > end) {
568
+ if (at < 0 || at >= toI32(bytes.length) || end > toI32(bytes.length)) {
569
+ return -1
570
+ }
571
+ const size: i32 = 1 << toI32(bytes[at] >> 6)
572
+ if (toI64(at) + toI64(size) > toI64(end)) {
547
573
  return -1
548
574
  }
575
+ // `at + size` is at most `end`, inside `bytes`.
576
+ const stop: i32 = toI32(toI64(at) + toI64(size))
549
577
  let value: i64 = toI64(bytes[at] & 0x3f)
550
- for (let k: i32 = 1; k < size; k += 1) {
551
- value = (value << 8) | toI64(bytes[at + k])
578
+ for (let j: i32 = at + 1; j < stop && j < toI32(bytes.length); j += 1) {
579
+ value = (value << 8) | toI64(bytes[j])
552
580
  }
553
581
  return value
554
582
  }
@@ -571,9 +599,11 @@ export const quicPacketNumberLength = (fullPn: i64, largestAcked: i64): i32 => {
571
599
  // `fullPn - largestAcked` packets are in flight with the new one, and the
572
600
  // receiver's window is centred on what it expects, so the encoding has to
573
601
  // span twice that: `unacked <= 2^(8n - 1)` for n bytes.
574
- const unacked: i64 = fullPn - largestAcked
602
+ // Both are -1 to 2^62 - 1 and `fullPn` is the larger, so the difference
603
+ // is 1 to 2^62; for -1 the `u64` subtraction wraps to `fullPn + 1`, as wanted.
604
+ const unacked: u64 = toU64(fullPn) - toU64(largestAcked)
575
605
  for (let n: i32 = 1; n <= 4; n += 1) {
576
- const half: i64 = toI64(1) << (toI64(n) * 8 - 1)
606
+ const half: u64 = toU64(1) << toU64(n * 8 - 1)
577
607
  if (unacked <= half) {
578
608
  return n
579
609
  }
@@ -597,13 +627,16 @@ export const quicPacketNumberDecode = (largestPn: i64, truncatedPn: i64, pnLengt
597
627
  if (pnLength < 1 || pnLength > 4 || largestPn < -1 || largestPn > QUIC_MAX_VARINT) {
598
628
  return -1
599
629
  }
600
- const window: i64 = toI64(1) << (toI64(pnLength) * 8)
601
- if (truncatedPn < 0 || truncatedPn >= window) {
630
+ const window: u64 = toU64(1) << toU64(pnLength * 8)
631
+ if (truncatedPn < 0 || toU64(truncatedPn) >= window) {
602
632
  return -1
603
633
  }
604
- const expected: i64 = largestPn + 1
605
- const halfWindow: i64 = window >> 1
606
- const candidate: i64 = (expected & ~(window - 1)) | truncatedPn
634
+ // In `u64`, where nothing below can wrap: `largestPn` is -1 to 2^62 - 1,
635
+ // so `expected` is 0 to 2^62, and every sum stays under 2^63.
636
+ const expected: u64 = toU64(largestPn) + toU64(1)
637
+ const halfWindow: u64 = window >> toU64(1)
638
+ const candidate: u64 = (expected & ~(window - toU64(1))) | toU64(truncatedPn)
639
+ const limit: u64 = toU64(QUIC_MAX_VARINT)
607
640
  // The candidate shares `expected`'s high bits; one window up or down is
608
641
  // nearer when it falls outside (expected - half, expected + half]. The
609
642
  // bound checks keep the answer inside [0, 2^62). One case is left that
@@ -611,13 +644,13 @@ export const quicPacketNumberDecode = (largestPn: i64, truncatedPn: i64, pnLengt
611
644
  // `expected` is 2^62 itself, and a candidate equal to it falls through
612
645
  // both tests. Every packet number below 2^62 with these low bytes is a
613
646
  // window down, and the nearest of them is the answer.
614
- if (candidate <= expected - halfWindow && candidate < QUIC_MAX_VARINT + 1 - window) {
615
- return candidate + window
647
+ if (candidate + halfWindow <= expected && candidate + window <= limit) {
648
+ return toI64(candidate + window)
616
649
  }
617
- if ((candidate > expected + halfWindow || candidate > QUIC_MAX_VARINT) && candidate >= window) {
618
- return candidate - window
650
+ if ((candidate > expected + halfWindow || candidate > limit) && candidate >= window) {
651
+ return toI64(candidate - window)
619
652
  }
620
- return candidate
653
+ return toI64(candidate)
621
654
  }
622
655
 
623
656
  /** Whether `cid` is a connection ID version 1 allows: 0 to 20 bytes. */
@@ -627,9 +660,18 @@ const quicPacketCidFits = (cid: u8[]): boolean => toI32(cid.length) <= QUIC_MAX_
627
660
  const quicPacketCidWindowFits = (cid: u8[], length: i32): boolean =>
628
661
  length >= 0 && length <= QUIC_MAX_CID_LENGTH && length <= toI32(cid.length)
629
662
 
663
+ /** A copy of `length` bytes of `bytes` from `from`, or an empty array for a window not inside it. */
664
+ const quicPacketTake = (bytes: u8[], from: i32, length: i32): u8[] => {
665
+ if (from < 0 || length < 0 || toI64(from) + toI64(length) > toI64(bytes.length)) {
666
+ return []
667
+ }
668
+ // The window is inside `bytes`, so its end fits an `i32`.
669
+ return quicPacketSlice(bytes, from, toI32(toI64(from) + toI64(length)))
670
+ }
671
+
630
672
  /**
631
673
  * Writes `bytes[from .. from + length)` into `buf` at `at`, as one slice copy; the caller has checked both windows.
632
- * The part of the run outside either window is skipped rather than written, so a wrong window cannot panic.
674
+ * A run that is not inside both arrays writes nothing, so a wrong window cannot panic.
633
675
  *
634
676
  * @param buf The buffer to write into; it is not grown.
635
677
  * @param at The byte offset in `buf` of the first byte written.
@@ -638,23 +680,7 @@ const quicPacketCidWindowFits = (cid: u8[], length: i32): boolean =>
638
680
  * @param length How many bytes to copy.
639
681
  */
640
682
  export const quicPacketCopy = (buf: u8[], at: i32, bytes: u8[], from: i32, length: i32): void => {
641
- let low: i32 = 0
642
- if (low < -at) {
643
- low = -at
644
- }
645
- if (low < -from) {
646
- low = -from
647
- }
648
- let high: i32 = length
649
- if (high > toI32(buf.length) - at) {
650
- high = toI32(buf.length) - at
651
- }
652
- if (high > toI32(bytes.length) - from) {
653
- high = toI32(bytes.length) - from
654
- }
655
- if (low < high) {
656
- copyBytes(buf, at + low, bytes, from + low, high - low)
657
- }
683
+ tryCopyBytes(buf, at, bytes, from, length)
658
684
  }
659
685
 
660
686
  /** Writes one byte at `at` when it is inside `buf`. */
@@ -668,7 +694,8 @@ const quicPacketPutByte = (buf: u8[], at: i32, value: i32): void => {
668
694
  * Writes what every version 1 long header starts with (RFC 9000 §17.2) into
669
695
  * `buf` at `at`: the first byte, the version, and each connection ID (the
670
696
  * first `dcidLength` and `scidLength` bytes of `dcid` and `scid`) after its
671
- * length. Answers the offset past it; the caller has checked that it fits.
697
+ * length. Answers the offset past it, or -1, writing nothing, for an ID
698
+ * length outside 0 to 20 or a prefix that does not fit in `buf` from `at`.
672
699
  */
673
700
  const quicPacketPutLongPrefix = (
674
701
  buf: u8[],
@@ -679,28 +706,46 @@ const quicPacketPutLongPrefix = (
679
706
  scid: u8[],
680
707
  scidLength: i32
681
708
  ): i32 => {
709
+ // 20 is `QUIC_MAX_CID_LENGTH`, written out so the bounds prover narrows on it.
710
+ if (at < 0 || dcidLength < 0 || dcidLength > 20 || scidLength < 0 || scidLength > 20) {
711
+ return -1
712
+ }
713
+ if (toI64(at) + toI64(7 + dcidLength + scidLength) > toI64(buf.length)) {
714
+ return -1
715
+ }
716
+ // The check above put the whole prefix inside `buf`, so each offset in it fits an `i32`.
717
+ const scidAt: i32 = toI32(toI64(at) + toI64(6 + dcidLength))
682
718
  const version: i32 = toI32(QUIC_VERSION_1)
683
719
  quicPacketPutByte(buf, at, first)
684
- quicPacketPutByte(buf, at + 1, (version >> 24) & 255)
685
- quicPacketPutByte(buf, at + 2, (version >> 16) & 255)
686
- quicPacketPutByte(buf, at + 3, (version >> 8) & 255)
687
- quicPacketPutByte(buf, at + 4, version & 255)
688
- quicPacketPutByte(buf, at + 5, dcidLength)
689
- quicPacketCopy(buf, at + 6, dcid, 0, dcidLength)
690
- const scidAt: i32 = at + 6 + dcidLength
720
+ quicPacketPutByte(buf, toI32(toI64(at) + 1), (version >> 24) & 255)
721
+ quicPacketPutByte(buf, toI32(toI64(at) + 2), (version >> 16) & 255)
722
+ quicPacketPutByte(buf, toI32(toI64(at) + 3), (version >> 8) & 255)
723
+ quicPacketPutByte(buf, toI32(toI64(at) + 4), version & 255)
724
+ quicPacketPutByte(buf, toI32(toI64(at) + 5), dcidLength)
725
+ quicPacketCopy(buf, toI32(toI64(at) + 6), dcid, 0, dcidLength)
691
726
  quicPacketPutByte(buf, scidAt, scidLength)
692
- quicPacketCopy(buf, scidAt + 1, scid, 0, scidLength)
693
- return scidAt + 1 + scidLength
727
+ quicPacketCopy(buf, toI32(toI64(scidAt) + 1), scid, 0, scidLength)
728
+ return toI32(toI64(scidAt) + toI64(1 + scidLength))
694
729
  }
695
730
 
696
- /** Writes the low `pnLength` bytes of `packetNumber` into `buf` at `at`, big-endian, and answers the offset past them. */
731
+ /**
732
+ * Writes the low `pnLength` bytes of `packetNumber` into `buf` at `at`,
733
+ * big-endian, and answers the offset past them, or -1, writing nothing, for a
734
+ * `pnLength` outside 1 to 4 or bytes that do not fit in `buf` from `at`.
735
+ */
697
736
  const quicPacketPutNumber = (buf: u8[], at: i32, packetNumber: i64, pnLength: i32): i32 => {
737
+ if (at < 0 || pnLength < 1 || pnLength > 4 || toI64(at) + toI64(pnLength) > toI64(buf.length)) {
738
+ return -1
739
+ }
698
740
  for (let k: i32 = 0; k < pnLength; k += 1) {
699
- if (at + k >= 0 && at + k < toI32(buf.length)) {
700
- buf[at + k] = toU8(toI32((packetNumber >> (toI64(pnLength - 1 - k) * 8)) & 255))
741
+ // `at` and `k` are 0 or more and the first check put their sum inside
742
+ // `buf`, so it fits an `i32` and the bounds guard never skips a byte.
743
+ const j: i32 = toI32(toI64(at) + toI64(k))
744
+ if (j >= 0 && j < toI32(buf.length)) {
745
+ buf[j] = toU8(toI32((packetNumber >> (toI64(pnLength - 1 - k) * 8)) & 255))
701
746
  }
702
747
  }
703
- return at + pnLength
748
+ return toI32(toI64(at) + toI64(pnLength))
704
749
  }
705
750
 
706
751
  /**
@@ -743,8 +788,14 @@ export const quicLongHeaderSize = (
743
788
  ) {
744
789
  return 0
745
790
  }
746
- const token: i32 = type === QUIC_PACKET_INITIAL ? quicVarintSize(toI64(tokenLength)) + tokenLength : 0
747
- return 7 + dcidLength + scidLength + token + 2 + pnLength
791
+ // Each term is an `i32` of 0 or more, so the unsigned sum is exact.
792
+ const token: u64 =
793
+ type === QUIC_PACKET_INITIAL ? toU64(quicVarintSize(toI64(tokenLength))) + toU64(tokenLength) : toU64(0)
794
+ const size: u64 = toU64(dcidLength) + toU64(scidLength) + toU64(9 + pnLength) + token
795
+ if (size > toU64(2147483647)) {
796
+ return 0
797
+ }
798
+ return toI32(size)
748
799
  }
749
800
 
750
801
  /**
@@ -795,8 +846,10 @@ export const quicPutLongHeader = (
795
846
  size === 0 ||
796
847
  !quicPacketCidWindowFits(dcid, dcidLength) ||
797
848
  !quicPacketCidWindowFits(scid, scidLength) ||
849
+ pnLength < 1 ||
850
+ pnLength > 4 ||
798
851
  at < 0 ||
799
- at > toI32(buf.length) - size
852
+ toI64(at) + toI64(size) > toI64(buf.length)
800
853
  ) {
801
854
  return -1
802
855
  }
@@ -809,13 +862,17 @@ export const quicPutLongHeader = (
809
862
  scid,
810
863
  scidLength
811
864
  )
812
- if (type === QUIC_PACKET_INITIAL) {
865
+ if (type === QUIC_PACKET_INITIAL && cursor >= 0) {
813
866
  cursor = quicVarintPut(buf, cursor, toI64(tokenLength), quicVarintSize(toI64(tokenLength)))
814
867
  quicPacketCopy(buf, cursor, token, 0, tokenLength)
815
- cursor = cursor + tokenLength
868
+ // The size check above put the token inside `buf`, so the sum fits an `i32`.
869
+ cursor = cursor < 0 ? -1 : toI32(toI64(cursor) + toI64(tokenLength))
870
+ }
871
+ if (cursor < 0) {
872
+ return -1
816
873
  }
817
- cursor = quicVarintPut(buf, cursor, toI64(pnLength + payloadLength + QUIC_AEAD_TAG_SIZE), 2)
818
- return quicPacketPutNumber(buf, cursor, packetNumber, pnLength)
874
+ cursor = quicVarintPut(buf, cursor, toI64(pnLength) + toI64(payloadLength) + toI64(QUIC_AEAD_TAG_SIZE), 2)
875
+ return cursor < 0 ? -1 : quicPacketPutNumber(buf, cursor, packetNumber, pnLength)
819
876
  }
820
877
 
821
878
  /**
@@ -865,7 +922,7 @@ export const quicLongHeader = (
865
922
  if (size === 0) {
866
923
  return null
867
924
  }
868
- const out: u8[] = new Array<u8>(size)
925
+ const out: u8[] = quicPacketZeroed(size)
869
926
  if (
870
927
  quicPutLongHeader(
871
928
  out,
@@ -914,23 +971,29 @@ export const quicPutShortHeader = (
914
971
  packetNumber: i64,
915
972
  pnLength: i32
916
973
  ): i32 => {
917
- const size: i32 = 1 + dcidLength + pnLength
974
+ // 20 is `QUIC_MAX_CID_LENGTH`, written out so the bounds prover narrows on it.
918
975
  if (
919
- !quicPacketCidWindowFits(dcid, dcidLength) ||
976
+ dcidLength < 0 ||
977
+ dcidLength > 20 ||
978
+ dcidLength > toI32(dcid.length) ||
920
979
  pnLength < 1 ||
921
980
  pnLength > 4 ||
922
981
  packetNumber < 0 ||
923
982
  packetNumber > QUIC_MAX_VARINT ||
924
983
  at < 0 ||
925
- at > toI32(buf.length) - size
984
+ at >= toI32(buf.length)
926
985
  ) {
927
986
  return -1
928
987
  }
988
+ if (toI64(at) + toI64(1 + dcidLength + pnLength) > toI64(buf.length)) {
989
+ return -1
990
+ }
929
991
  const spinBit: i32 = spin ? 0x20 : 0
930
992
  const keyPhaseBit: i32 = keyPhase ? 0x04 : 0
931
993
  buf[at] = toU8((pnLength - 1) | spinBit | keyPhaseBit | 0x40)
932
- quicPacketCopy(buf, at + 1, dcid, 0, dcidLength)
933
- return quicPacketPutNumber(buf, at + 1 + dcidLength, packetNumber, pnLength)
994
+ // The check above put the header inside `buf`, so its offsets fit an `i32`.
995
+ quicPacketCopy(buf, toI32(toI64(at) + 1), dcid, 0, dcidLength)
996
+ return quicPacketPutNumber(buf, toI32(toI64(at) + toI64(1 + dcidLength)), packetNumber, pnLength)
934
997
  }
935
998
 
936
999
  /**
@@ -956,10 +1019,11 @@ export const quicShortHeader = (
956
1019
  pnLength: i32
957
1020
  ): u8[] | null => {
958
1021
  const dcidLength: i32 = toI32(dcid.length)
959
- if (!quicPacketCidFits(dcid) || pnLength < 1 || pnLength > 4) {
1022
+ // 20 is `QUIC_MAX_CID_LENGTH`, written out so the bounds prover narrows on it.
1023
+ if (dcidLength > 20 || pnLength < 1 || pnLength > 4) {
960
1024
  return null
961
1025
  }
962
- const out: u8[] = new Array<u8>(1 + dcidLength + pnLength)
1026
+ const out: u8[] = new Array<u8>(toU8(1 + dcidLength + pnLength))
963
1027
  if (quicPutShortHeader(out, 0, dcid, dcidLength, spin, keyPhase, packetNumber, pnLength) < 0) {
964
1028
  return null
965
1029
  }
@@ -981,7 +1045,19 @@ const quicPacketCidLength = (bytes: u8[], at: i32, end: i32): i32 => {
981
1045
  return -1
982
1046
  }
983
1047
  const length: i32 = toI32(bytes[at])
984
- return at + 1 + length > end ? -1 : length
1048
+ return toI64(at) + 1 + toI64(length) > toI64(end) ? -1 : length
1049
+ }
1050
+
1051
+ /**
1052
+ * `at + count` when both are 0 or more and the sum is at most `end`, else
1053
+ * -1: a parser's step past `count` bytes that must stay inside the packet.
1054
+ */
1055
+ const quicPacketSkip = (at: i32, count: i32, end: i32): i32 => {
1056
+ if (at < 0 || count < 0 || toI64(at) + toI64(count) > toI64(end)) {
1057
+ return -1
1058
+ }
1059
+ // The sum is at most `end`, so it fits an `i32`.
1060
+ return toI32(toI64(at) + toI64(count))
985
1061
  }
986
1062
 
987
1063
  /**
@@ -1022,47 +1098,55 @@ export const quicParseHeaderInto = (
1022
1098
  header.scidLength = 0
1023
1099
  header.tokenStart = 0
1024
1100
  header.tokenLength = 0
1025
- if (at < 0 || at >= end) {
1101
+ if (at < 0 || at >= end || at >= toI32(datagram.length)) {
1026
1102
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1027
1103
  }
1028
1104
  const first: i32 = toI32(datagram[at])
1105
+ // `at` is below `end`, so the byte after it is at most `end` and fits an `i32`.
1106
+ const second: i32 = toI32(toI64(at) + 1)
1029
1107
  if ((first & 0x80) === 0) {
1030
1108
  header.type = QUIC_PACKET_SHORT
1031
1109
  if ((first & 0x40) === 0) {
1032
1110
  return quicPacketRefuse(header, QUIC_ERR_FIXED_BIT)
1033
1111
  }
1034
- if (shortDcidLength < 0 || shortDcidLength > QUIC_MAX_CID_LENGTH || at + 1 + shortDcidLength > end) {
1112
+ const pnOffset: i32 = quicPacketSkip(second, shortDcidLength, end)
1113
+ if (shortDcidLength > QUIC_MAX_CID_LENGTH || pnOffset < 0) {
1035
1114
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1036
1115
  }
1037
- header.dcidStart = at + 1
1116
+ header.dcidStart = second
1038
1117
  header.dcidLength = shortDcidLength
1039
- header.pnOffset = at + 1 + shortDcidLength
1118
+ header.pnOffset = pnOffset
1040
1119
  return QUIC_PACKET_OK
1041
1120
  }
1042
- if (at + 5 > end) {
1121
+ const dcidLengthAt: i32 = quicPacketSkip(at, 5, end)
1122
+ if (dcidLengthAt < 0) {
1043
1123
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1044
1124
  }
1125
+ // `quicPacketSkip` put `at + 5` inside the datagram, so the guard below
1126
+ // never skips a version byte.
1045
1127
  let version: i64 = 0
1046
- for (let k: i32 = at + 1; k < at + 5; k += 1) {
1047
- if (k < toI32(datagram.length)) {
1048
- version = (version << 8) | toI64(datagram[k])
1128
+ for (let k: i32 = 1; k < 5; k += 1) {
1129
+ const j: i32 = toI32(toI64(at) + toI64(k))
1130
+ if (j >= 0 && j < toI32(datagram.length)) {
1131
+ version = (version << 8) | toI64(datagram[j])
1049
1132
  }
1050
1133
  }
1051
1134
  header.version = version
1052
- const dcidLength: i32 = quicPacketCidLength(datagram, at + 5, end)
1135
+ const dcidLength: i32 = quicPacketCidLength(datagram, dcidLengthAt, end)
1053
1136
  if (dcidLength < 0) {
1054
1137
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1055
1138
  }
1056
- header.dcidStart = at + 6
1139
+ // `quicPacketCidLength` put the ID inside `[dcidLengthAt, end)`, so these offsets fit an `i32`.
1140
+ header.dcidStart = toI32(toI64(dcidLengthAt) + 1)
1057
1141
  header.dcidLength = dcidLength
1058
- const afterDcid: i32 = at + 6 + dcidLength
1142
+ const afterDcid: i32 = toI32(toI64(dcidLengthAt) + 1 + toI64(dcidLength))
1059
1143
  const scidLength: i32 = quicPacketCidLength(datagram, afterDcid, end)
1060
1144
  if (scidLength < 0) {
1061
1145
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1062
1146
  }
1063
- header.scidStart = afterDcid + 1
1147
+ header.scidStart = toI32(toI64(afterDcid) + 1)
1064
1148
  header.scidLength = scidLength
1065
- let cursor: i32 = afterDcid + 1 + scidLength
1149
+ let cursor: i32 = toI32(toI64(afterDcid) + 1 + toI64(scidLength))
1066
1150
  if (version !== QUIC_VERSION_1) {
1067
1151
  return quicPacketRefuse(header, QUIC_ERR_VERSION)
1068
1152
  }
@@ -1077,12 +1161,13 @@ export const quicParseHeaderInto = (
1077
1161
  if (type === QUIC_PACKET_RETRY) {
1078
1162
  // A Retry has no Length and no packet number: the token runs to the
1079
1163
  // integrity tag, and the tag to the end of the datagram (§17.2.5).
1080
- if (cursor + QUIC_RETRY_TAG_SIZE > end) {
1164
+ if (toI64(cursor) + toI64(QUIC_RETRY_TAG_SIZE) > toI64(end)) {
1081
1165
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1082
1166
  }
1167
+ // `cursor` is 0 or more and the tag fits before `end`, so both fit an `i32`.
1083
1168
  header.tokenStart = cursor
1084
- header.tokenLength = end - QUIC_RETRY_TAG_SIZE - cursor
1085
- header.pnOffset = end - QUIC_RETRY_TAG_SIZE
1169
+ header.tokenLength = toI32(toI64(end) - toI64(QUIC_RETRY_TAG_SIZE) - toI64(cursor))
1170
+ header.pnOffset = toI32(toI64(end) - toI64(QUIC_RETRY_TAG_SIZE))
1086
1171
  return QUIC_PACKET_OK
1087
1172
  }
1088
1173
  if (type === QUIC_PACKET_INITIAL) {
@@ -1090,24 +1175,29 @@ export const quicParseHeaderInto = (
1090
1175
  if (tokenLength < 0) {
1091
1176
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1092
1177
  }
1093
- cursor += quicVarintLength(datagram, cursor)
1094
- if (tokenLength > toI64(end - cursor)) {
1178
+ cursor = quicPacketSkip(cursor, quicVarintLength(datagram, cursor), end)
1179
+ if (cursor < 0 || tokenLength > toI64(end) - toI64(cursor)) {
1095
1180
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1096
1181
  }
1097
1182
  header.tokenStart = cursor
1183
+ // The token fits before `end`, so its length fits an `i32`.
1098
1184
  header.tokenLength = toI32(tokenLength)
1099
- cursor = cursor + toI32(tokenLength)
1185
+ cursor = quicPacketSkip(cursor, toI32(tokenLength), end)
1100
1186
  }
1101
1187
  const length: i64 = quicVarintRead(datagram, cursor, end)
1102
1188
  if (length < 0) {
1103
1189
  return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1104
1190
  }
1105
- cursor += quicVarintLength(datagram, cursor)
1106
- if (length > toI64(end - cursor)) {
1191
+ cursor = quicPacketSkip(cursor, quicVarintLength(datagram, cursor), end)
1192
+ if (cursor < 0) {
1193
+ return quicPacketRefuse(header, QUIC_ERR_TRUNCATED)
1194
+ }
1195
+ if (length > toI64(end) - toI64(cursor)) {
1107
1196
  return quicPacketRefuse(header, QUIC_ERR_LENGTH)
1108
1197
  }
1109
1198
  header.pnOffset = cursor
1110
- header.end = cursor + toI32(length)
1199
+ // The packet fits before `end`, so its length fits an `i32`.
1200
+ header.end = quicPacketSkip(cursor, toI32(length), end)
1111
1201
  return QUIC_PACKET_OK
1112
1202
  }
1113
1203
 
@@ -1131,11 +1221,12 @@ export const quicParseHeaderInto = (
1131
1221
  export const quicParseHeader = (datagram: u8[], at: i32, shortDcidLength: i32): QuicHeader => {
1132
1222
  const header: QuicHeader = new QuicHeader()
1133
1223
  quicParseHeaderInto(header, datagram, at, toI32(datagram.length), shortDcidLength)
1134
- header.dcid = quicPacketSlice(datagram, header.dcidStart, header.dcidStart + header.dcidLength)
1135
- header.scid = quicPacketSlice(datagram, header.scidStart, header.scidStart + header.scidLength)
1136
- header.token = quicPacketSlice(datagram, header.tokenStart, header.tokenStart + header.tokenLength)
1224
+ header.dcid = quicPacketTake(datagram, header.dcidStart, header.dcidLength)
1225
+ header.scid = quicPacketTake(datagram, header.scidStart, header.scidLength)
1226
+ header.token = quicPacketTake(datagram, header.tokenStart, header.tokenLength)
1137
1227
  if (header.type === QUIC_PACKET_RETRY && header.error === QUIC_PACKET_OK) {
1138
- header.retryTag = quicPacketSlice(datagram, header.end - QUIC_RETRY_TAG_SIZE, header.end)
1228
+ // A Retry's `pnOffset` is where its integrity tag starts.
1229
+ header.retryTag = quicPacketTake(datagram, header.pnOffset, QUIC_RETRY_TAG_SIZE)
1139
1230
  }
1140
1231
  return header
1141
1232
  }
@@ -1304,7 +1395,11 @@ export const quicKeysUpdate = (
1304
1395
  }
1305
1396
  quicPacketPoint(slot, keys.aead)
1306
1397
  const next: QuicKeys = slot.keys
1307
- next.hp.set(keys.hp)
1398
+ // `quicPacketPoint` sized the slot's key for `keys.aead`, and `quicPacketKeysUsable`
1399
+ // fixed `keys.hp` at that size, so this copy never refuses: nothing has been derived.
1400
+ if (!tryCopyBytes(next.hp, 0, keys.hp, 0, toI32(keys.hp.length))) {
1401
+ return null
1402
+ }
1308
1403
  const scratch: HkdfScratch = new HkdfScratch()
1309
1404
  {
1310
1405
  using _scope = arena()
@@ -1647,7 +1742,7 @@ export const quicInitialSecretsInto = (
1647
1742
  using _scope = arena()
1648
1743
  const none: u8[] = []
1649
1744
  const initial: u8[] = new Array<u8>(32)
1650
- hkdfExtractInto(kdf, 32, quicPacketInitialSalt(), quicPacketSlice(buf, at, at + length), initial, 0)
1745
+ hkdfExtractInto(kdf, 32, quicPacketInitialSalt(), quicPacketTake(buf, at, length), initial, 0)
1651
1746
  hkdfExpandLabelInto(kdf, 32, initial, "client in", none, 0, 0, client, 0, 32)
1652
1747
  hkdfExpandLabelInto(kdf, 32, initial, "server in", none, 0, 0, server, 0, 32)
1653
1748
  secureZero(initial)
@@ -1665,7 +1760,10 @@ const quicPacketNonce = (keys: QuicKeys, packetNumber: i64): u8[] => {
1665
1760
  const nonce: u8[] = new Array<u8>(QUIC_IV_SIZE)
1666
1761
  const nonceLength: i32 = toI32(nonce.length)
1667
1762
  for (let k: i32 = 0; k < nonceLength; k += 1) {
1668
- // Byte `k` of the nonce takes byte `k - 4` of the number's eight.
1763
+ // Byte `k` of the nonce takes byte `k - 4` of the number's eight. Every
1764
+ // caller holds keys `quicPacketKeysUsable` passed, which fixes `keys.iv`
1765
+ // at `QUIC_IV_SIZE` bytes, so the `: 0` never runs; without that check a
1766
+ // short IV would give a wrong nonce here, not a refusal.
1669
1767
  const pn: i64 = k < 4 ? 0 : (packetNumber >> (toI64(11 - k) * 8)) & 255
1670
1768
  const iv: i32 = k < toI32(keys.iv.length) ? toI32(keys.iv[k]) : 0
1671
1769
  nonce[k] = toU8(iv ^ toI32(pn))
@@ -1737,35 +1835,48 @@ export const quicSealInPlace = (
1737
1835
  payloadLength: i32,
1738
1836
  packetNumber: i64
1739
1837
  ): i32 => {
1740
- const end: i32 = start + headerLength + payloadLength + QUIC_AEAD_TAG_SIZE
1741
- if (start < 0 || headerLength < 2 || payloadLength < 0 || end > toI32(buf.length) || end < start) {
1838
+ if (
1839
+ start < 0 ||
1840
+ start >= toI32(buf.length) ||
1841
+ headerLength < 2 ||
1842
+ payloadLength < 0 ||
1843
+ toI64(start) + toI64(headerLength) + toI64(payloadLength) + toI64(QUIC_AEAD_TAG_SIZE) > toI64(buf.length)
1844
+ ) {
1742
1845
  return -1
1743
1846
  }
1847
+ // Every offset below is at most `end`, which the check above put inside
1848
+ // `buf`, so each fits an `i32`.
1849
+ const end: i32 = toI32(
1850
+ toI64(start) + toI64(headerLength) + toI64(payloadLength) + toI64(QUIC_AEAD_TAG_SIZE)
1851
+ )
1744
1852
  const first: i32 = toI32(buf[start])
1745
1853
  const pnLength: i32 = (first & 3) + 1
1746
- const pnOffset: i32 = start + headerLength - pnLength
1854
+ const pnOffset: i32 = toI32(toI64(start) + toI64(headerLength) - toI64(pnLength))
1747
1855
  if (
1748
1856
  !quicPacketKeysUsable(keys) ||
1749
- pnOffset < start + 1 ||
1750
- pnLength + payloadLength < 4 ||
1857
+ toI64(pnOffset) < toI64(start) + 1 ||
1858
+ toI64(pnLength) + toI64(payloadLength) < 4 ||
1751
1859
  packetNumber < 0 ||
1752
1860
  packetNumber > QUIC_MAX_VARINT
1753
1861
  ) {
1754
1862
  return -1
1755
1863
  }
1756
1864
  for (let k: i32 = 0; k < pnLength; k += 1) {
1757
- const want: i32 = toI32((packetNumber >> (toI64(pnLength - 1 - k) * 8)) & 255)
1758
- if (pnOffset + k < toI32(buf.length) && toI32(buf[pnOffset + k]) !== want) {
1865
+ const want: i32 = toI32((packetNumber >> ((toI64(pnLength) - 1 - toI64(k)) * 8)) & 255)
1866
+ const j: i32 = toI32(toI64(pnOffset) + toI64(k))
1867
+ if (j >= 0 && j < toI32(buf.length) && toI32(buf[j]) !== want) {
1759
1868
  return -1
1760
1869
  }
1761
1870
  }
1871
+ // `pnOffset` is past `start`, so 2 before it is -1 or more.
1762
1872
  if (
1763
1873
  (first & 0x80) !== 0 &&
1764
- quicVarintRead(buf, pnOffset - 2, pnOffset) !== toI64(pnLength + payloadLength + QUIC_AEAD_TAG_SIZE)
1874
+ quicVarintRead(buf, toI32(toI64(pnOffset) - 2), pnOffset) !==
1875
+ toI64(pnLength) + toI64(payloadLength) + toI64(QUIC_AEAD_TAG_SIZE)
1765
1876
  ) {
1766
1877
  return -1
1767
1878
  }
1768
- const payloadAt: i32 = start + headerLength
1879
+ const payloadAt: i32 = toI32(toI64(start) + toI64(headerLength))
1769
1880
  {
1770
1881
  using _scope = arena()
1771
1882
  // The nonce is the IV with a public number XORed in, so it gives the IV
@@ -1776,27 +1887,30 @@ export const quicSealInPlace = (
1776
1887
  keys,
1777
1888
  nonce,
1778
1889
  quicPacketSlice(buf, start, payloadAt),
1779
- quicPacketSlice(buf, payloadAt, payloadAt + payloadLength)
1890
+ quicPacketTake(buf, payloadAt, payloadLength)
1780
1891
  )
1781
1892
  wipe(nonce)
1782
- if (sealed === null || payloadAt + toI32(sealed.length) > toI32(buf.length)) {
1893
+ if (sealed === null || !tryCopyBytes(buf, payloadAt, sealed, 0, toI32(sealed.length))) {
1783
1894
  return -1
1784
1895
  }
1785
- buf.set(sealed, payloadAt)
1786
- const sampleAt: i32 = pnOffset + 4
1896
+ // The packet number and payload are at least 4 bytes and the tag's 16
1897
+ // follow them, so the sample starts before `end` and fits an `i32`.
1787
1898
  const mask: u8[] | null = quicPacketMask(
1788
1899
  keys,
1789
- quicPacketSlice(buf, sampleAt, sampleAt + QUIC_SAMPLE_SIZE)
1900
+ quicPacketTake(buf, toI32(toI64(pnOffset) + 4), QUIC_SAMPLE_SIZE)
1790
1901
  )
1791
1902
  if (mask === null) {
1792
1903
  return -1
1793
1904
  }
1794
- const masked: boolean = toI32(mask.length) >= 5
1795
- if (masked) {
1796
- buf[start] = toU8(first ^ (toI32(mask[0]) & quicPacketProtectedBits(first)))
1797
- for (let k: i32 = 0; k < pnLength && k + 1 < toI32(mask.length); k += 1) {
1798
- if (pnOffset + k >= 0 && pnOffset + k < toI32(buf.length)) {
1799
- buf[pnOffset + k] = buf[pnOffset + k] ^ mask[k + 1]
1905
+ // `buf` is not grown in between, so `start` is still inside it; the test
1906
+ // restates that for the bounds proof, and a packet left unmasked is refused.
1907
+ const masked: boolean = toI32(mask.length) >= 5 && start >= 0 && start < toI32(buf.length)
1908
+ if (masked && start >= 0 && start < toI32(buf.length)) {
1909
+ buf[start] = toU8(first ^ (toI32(quicPacketMaskByte(mask, 0)) & quicPacketProtectedBits(first)))
1910
+ for (let k: i32 = 0; k < pnLength; k += 1) {
1911
+ const j: i32 = toI32(toI64(pnOffset) + toI64(k))
1912
+ if (j >= 0 && j < toI32(buf.length)) {
1913
+ buf[j] = buf[j] ^ quicPacketMaskByte(mask, toI32(toI64(k) + 1))
1800
1914
  }
1801
1915
  }
1802
1916
  }
@@ -1839,10 +1953,16 @@ export const quicSealPacket = (
1839
1953
  ): u8[] | null => {
1840
1954
  const headerLength: i32 = toI32(header.length)
1841
1955
  const payloadLength: i32 = toI32(payload.length)
1842
- const packet: u8[] = new Array<u8>(headerLength + payloadLength + QUIC_AEAD_TAG_SIZE)
1843
- packet.set(header)
1844
- packet.set(payload, headerLength)
1845
- if (quicSealInPlace(keys, packet, 0, headerLength, payloadLength, packetNumber) < 0) {
1956
+ const size: u64 = toU64(headerLength) + toU64(payloadLength) + toU64(QUIC_AEAD_TAG_SIZE)
1957
+ if (size > toU64(2147483647)) {
1958
+ return null
1959
+ }
1960
+ const packet: u8[] = quicPacketZeroed(toI32(size))
1961
+ if (
1962
+ !tryCopyBytes(packet, 0, header, 0, headerLength) ||
1963
+ !tryCopyBytes(packet, headerLength, payload, 0, payloadLength) ||
1964
+ quicSealInPlace(keys, packet, 0, headerLength, payloadLength, packetNumber) < 0
1965
+ ) {
1846
1966
  return null
1847
1967
  }
1848
1968
  return packet
@@ -1890,42 +2010,49 @@ export const quicUnprotectHeader = (
1890
2010
  return []
1891
2011
  }
1892
2012
  const pnOffset: i32 = header.pnOffset
1893
- const sampleAt: i32 = pnOffset + 4
2013
+ const start: i32 = header.start
1894
2014
  if (
1895
- header.start < 0 ||
1896
- header.start >= pnOffset ||
2015
+ start < 0 ||
2016
+ start >= datagramLength ||
2017
+ start >= pnOffset ||
1897
2018
  header.end > datagramLength ||
1898
- sampleAt + QUIC_SAMPLE_SIZE > header.end
2019
+ toI64(pnOffset) + 4 + toI64(QUIC_SAMPLE_SIZE) > toI64(header.end)
1899
2020
  ) {
1900
2021
  packet.error = QUIC_ERR_SAMPLE
1901
2022
  return []
1902
2023
  }
2024
+ const protectedFirst: i32 = toI32(datagram[start])
2025
+ // The check above put the sample inside the datagram, so its offset fits an `i32`.
1903
2026
  const mask: u8[] | null = quicPacketMask(
1904
2027
  keys,
1905
- quicPacketSlice(datagram, sampleAt, sampleAt + QUIC_SAMPLE_SIZE)
2028
+ quicPacketTake(datagram, toI32(toI64(pnOffset) + 4), QUIC_SAMPLE_SIZE)
1906
2029
  )
1907
2030
  if (mask === null) {
1908
2031
  packet.error = QUIC_ERR_KEYS
1909
2032
  return []
1910
2033
  }
1911
2034
  const usable: boolean = toI32(mask.length) >= 5
1912
- const protectedFirst: i32 = toI32(datagram[header.start])
1913
2035
  const long: boolean = (protectedFirst & 0x80) !== 0
1914
2036
  const first: i32 = usable
1915
- ? protectedFirst ^ (toI32(mask[0]) & quicPacketProtectedBits(protectedFirst))
2037
+ ? protectedFirst ^ (toI32(quicPacketMaskByte(mask, 0)) & quicPacketProtectedBits(protectedFirst))
1916
2038
  : protectedFirst
1917
2039
  const pnLength: i32 = (first & 3) + 1
1918
2040
  // The sample check above put 20 bytes after `pnOffset` inside the packet,
1919
- // so the packet number's at most 4 are there too.
1920
- const clear: u8[] = quicPacketSlice(datagram, header.start, pnOffset + pnLength)
1921
- clear[0] = toU8(first)
2041
+ // so the packet number's at most 4 are there too, and every offset below
2042
+ // fits an `i32`.
2043
+ const clear: u8[] = quicPacketTake(datagram, start, toI32(toI64(pnOffset) + toI64(pnLength) - toI64(start)))
2044
+ // The same sample check rules out an empty `clear`, so the guard below
2045
+ // always holds and `truncated` is the real packet number.
1922
2046
  let truncated: i64 = 0
1923
- for (let k: i32 = 0; k < pnLength; k += 1) {
1924
- const at: i32 = pnOffset - header.start + k
1925
- if (at >= 0 && at < toI32(clear.length) && k + 1 < toI32(mask.length)) {
1926
- const b: u8 = clear[at] ^ mask[k + 1]
1927
- clear[at] = b
1928
- truncated = (truncated << 8) | toI64(b)
2047
+ if (toI32(clear.length) > 0) {
2048
+ clear[0] = toU8(first)
2049
+ for (let k: i32 = 0; k < pnLength; k += 1) {
2050
+ const at: i32 = toI32(toI64(pnOffset) - toI64(start) + toI64(k))
2051
+ if (at >= 0 && at < toI32(clear.length)) {
2052
+ const b: u8 = clear[at] ^ quicPacketMaskByte(mask, toI32(toI64(k) + 1))
2053
+ clear[at] = b
2054
+ truncated = (truncated << 8) | toI64(b)
2055
+ }
1929
2056
  }
1930
2057
  }
1931
2058
  // The mask is the header-protection key's keystream: wiped once used, on
@@ -2001,7 +2128,11 @@ export const quicDecryptPayload = (
2001
2128
  packet.error = QUIC_ERR_KEYS
2002
2129
  return null
2003
2130
  }
2004
- const payloadStart: i32 = header.start + toI32(clear.length)
2131
+ // A start past `header.end` leaves an empty window, which does not open.
2132
+ const payloadStart: i32 =
2133
+ toI64(header.start) + toI64(clear.length) > toI64(header.end)
2134
+ ? header.end
2135
+ : toI32(toI64(header.start) + toI64(clear.length))
2005
2136
  // The nonce gives the IV away (see `quicSealInPlace`): wiped once used.
2006
2137
  const nonce: u8[] = quicPacketNonce(keys, packet.packetNumber)
2007
2138
  const plain: u8[] | null = quicPacketOpen(
@@ -2122,18 +2253,13 @@ export const quicRetryPacket = (
2122
2253
  odcid: u8[],
2123
2254
  unused: i32
2124
2255
  ): u8[] | null => {
2125
- if (
2126
- !quicPacketCidFits(dcid) ||
2127
- !quicPacketCidFits(scid) ||
2128
- toI32(token.length) === 0 ||
2129
- unused < 0 ||
2130
- unused > 15
2131
- ) {
2132
- return null
2133
- }
2134
2256
  const dcidLength: i32 = toI32(dcid.length)
2135
2257
  const scidLength: i32 = toI32(scid.length)
2136
- const out: u8[] = new Array<u8>(7 + dcidLength + scidLength)
2258
+ // 20 is `QUIC_MAX_CID_LENGTH`, written out so the bounds prover narrows on it.
2259
+ if (dcidLength > 20 || scidLength > 20 || toI32(token.length) === 0 || unused < 0 || unused > 15) {
2260
+ return null
2261
+ }
2262
+ const out: u8[] = new Array<u8>(toU8(7 + dcidLength + scidLength))
2137
2263
  quicPacketPutLongPrefix(out, 0, unused | 0xf0, dcid, dcidLength, scid, scidLength)
2138
2264
  quicPacketAppend(out, token)
2139
2265
  const tag: u8[] | null = quicRetryIntegrityTag(odcid, out)