@amritk/nish-aarch64-linux 0.49.1 → 0.51.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 (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
@@ -167,6 +167,7 @@ import {
167
167
  quicWorkerByte,
168
168
  quicWorkerCheck,
169
169
  } from "nish/net/quic"
170
+ import { wrappingAdd } from "nish:unsafe"
170
171
 
171
172
  // ---- What `handle` answers ----------------------------------------------------
172
173
 
@@ -525,6 +526,8 @@ export class QuicListener {
525
526
  const mac: HmacSha256Scratch = this.kdf.sha256
526
527
  {
527
528
  using _scope = arena()
529
+ // The seed and the block are their sizes long and the pool is 32
530
+ // bytes, so none of the three answers `false`.
528
531
  mac.begin(this.seed, QUIC_LISTENER_FROM, QUIC_LISTENER_ENTROPY_SIZE)
529
532
  mac.update(this.block, QUIC_LISTENER_FROM, QUIC_LISTENER_BLOCK)
530
533
  mac.finishInto(this.pool, QUIC_LISTENER_FROM)
@@ -541,7 +544,7 @@ export class QuicListener {
541
544
  * whose arrays are refilled in their own room. Answers `answer.kind`. See
542
545
  * the module header for the rules; nothing a peer sends makes it panic,
543
546
  * and nothing it does keeps arena memory. A window outside `datagram` is
544
- * the caller's mistake, and panics.
547
+ * dropped unread, with no reply.
545
548
  *
546
549
  * @param datagram The array the datagram lies in; read, not kept.
547
550
  * @param at The index in `datagram` of its first byte.
@@ -554,17 +557,16 @@ export class QuicListener {
554
557
  * `QUIC_LISTEN_RETRY`, `QUIC_LISTEN_INVALID_TOKEN` or `QUIC_LISTEN_STATELESS_RESET`.
555
558
  */
556
559
  handleWindow(datagram: u8[], at: i32, len: i32, address: u8[], now: i64, answer: QuicListenerAnswer): i32 {
557
- // `[at, at + len)` inside `datagram`: `p256WindowFits`' test.
558
- if (at < 0 || len < 0 || at > toI32(datagram.length) - len) {
559
- panic(
560
- `QuicListener.handleWindow: the window [${at}, ${at} + ${len}) is outside a datagram of ${toI32(datagram.length)} bytes`
561
- )
562
- }
563
560
  answer.kind = QUIC_LISTEN_DROP
564
561
  answer.retried = false
565
562
  quicListenerResize(answer.reply, 0)
566
563
  quicListenerResize(answer.originalDcid, 0)
567
564
  quicListenerResize(answer.retryScid, 0)
565
+ // `[at, at + len)` inside `datagram`: `p256WindowFits`' test. Outside it,
566
+ // nothing is read and nothing is answered.
567
+ if (at < 0 || len < 0 || at > toI32(datagram.length) - len) {
568
+ return QUIC_LISTEN_DROP
569
+ }
568
570
  if (now > this.now) {
569
571
  this.now = now
570
572
  }
@@ -706,7 +708,8 @@ export class QuicListener {
706
708
  * (`scid[scidAt .. scidAt + scidLength)`) and its address, into `tag`:
707
709
  * what makes a token good for one address and one connection only. Its
708
710
  * first `QUIC_LISTENER_TOKEN_MAC_SIZE` bytes are the token's MAC; the
709
- * caller wipes `tag` once it has used them.
711
+ * caller wipes `tag` once it has used them. Answers `false`, writing
712
+ * nothing to `tag`, when a window lies outside its array.
710
713
  */
711
714
  private tokenMac(
712
715
  head: u8[],
@@ -716,24 +719,27 @@ export class QuicListener {
716
719
  scidAt: i32,
717
720
  scidLength: i32,
718
721
  address: u8[]
719
- ): void {
722
+ ): boolean {
720
723
  const mac: HmacSha256Scratch = this.kdf.sha256
721
724
  const lengths: u8[] = this.lengths
722
725
  const one: i32 = 1
723
726
  const four: i32 = 4
724
727
  quicListenerPutNumber(lengths, QUIC_LISTENER_FROM, toI64(scidLength), one)
725
728
  quicListenerPutNumber(lengths, one, toI64(toI32(address.length)), four)
729
+ let fed: boolean = false
726
730
  {
727
731
  using _scope = arena()
728
- mac.begin(this.config.retryTokenKey, QUIC_LISTENER_FROM, toI32(this.config.retryTokenKey.length))
729
- mac.update(head, headAt, headLength)
730
- mac.update(lengths, QUIC_LISTENER_FROM, one)
731
- mac.update(scid, scidAt, scidLength)
732
- mac.update(lengths, one, four)
733
- mac.update(address, QUIC_LISTENER_FROM, toI32(address.length))
734
- mac.finishInto(this.tag, QUIC_LISTENER_FROM)
732
+ fed =
733
+ mac.begin(this.config.retryTokenKey, QUIC_LISTENER_FROM, toI32(this.config.retryTokenKey.length)) &&
734
+ mac.update(head, headAt, headLength) &&
735
+ mac.update(lengths, QUIC_LISTENER_FROM, one) &&
736
+ mac.update(scid, scidAt, scidLength) &&
737
+ mac.update(lengths, one, four) &&
738
+ mac.update(address, QUIC_LISTENER_FROM, toI32(address.length))
739
+ fed = fed && mac.finishInto(this.tag, QUIC_LISTENER_FROM)
735
740
  }
736
741
  mac.wipe()
742
+ return fed
737
743
  }
738
744
 
739
745
  /**
@@ -765,7 +771,9 @@ export class QuicListener {
765
771
  if (head + QUIC_LISTENER_TOKEN_MAC_SIZE !== length) {
766
772
  return -1
767
773
  }
768
- this.tokenMac(datagram, tokenAt, head, datagram, header.dcidStart, header.dcidLength, address)
774
+ if (!this.tokenMac(datagram, tokenAt, head, datagram, header.dcidStart, header.dcidLength, address)) {
775
+ return -1
776
+ }
769
777
  const same: boolean = timingSafeEqualAt(
770
778
  this.tag,
771
779
  0,
@@ -822,7 +830,10 @@ export class QuicListener {
822
830
  quicListenerPutNumber(reply, tokenAt + 1, this.now, 8)
823
831
  quicListenerPutNumber(reply, tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1, toI64(odcidLength), 1)
824
832
  quicPacketCopy(reply, tokenAt + QUIC_LISTENER_TOKEN_HEAD, datagram, header.dcidStart, odcidLength)
825
- this.tokenMac(reply, tokenAt, macAt - tokenAt, reply, retryScidAt, QUIC_CONN_CID_LENGTH, address)
833
+ if (!this.tokenMac(reply, tokenAt, macAt - tokenAt, reply, retryScidAt, QUIC_CONN_CID_LENGTH, address)) {
834
+ quicListenerResize(reply, 0)
835
+ return
836
+ }
826
837
  quicPacketCopy(reply, macAt, this.tag, 0, QUIC_LISTENER_TOKEN_MAC_SIZE)
827
838
  secureZero(this.tag)
828
839
  quicListenerPutNumber(reply, 0, toI64((this.randomByte() & 15) | 0xf0), 1)
@@ -961,7 +972,8 @@ export class QuicListener {
961
972
  /** Takes one stateless reset from the rate limit's budget, topping it up first. Answers whether one was there. */
962
973
  private takeReset(): boolean {
963
974
  // `now` never falls below 0, where it starts, and `refilled` is never
964
- // after it, so the u64 sums below are the i64 ones.
975
+ // after it, so the gap between them is a difference of two values at or
976
+ // above 0, which cannot overflow.
965
977
  const now: i64 = this.now
966
978
  let refilled: i64 = this.resetRefilled
967
979
  let budget: i32 = this.resetBudget
@@ -969,15 +981,18 @@ export class QuicListener {
969
981
  refilled = now
970
982
  budget = QUIC_LISTENER_RESET_BURST
971
983
  }
972
- if (now > refilled) {
984
+ if (now >= 0 && refilled >= 0 && now > refilled) {
985
+ const gap: i64 = now - refilled
973
986
  // 100 is `QUIC_LISTENER_RESET_INTERVAL`, as a literal the prover reads.
974
- const earned: u64 = (toU64(now) - toU64(refilled)) / toU64(100)
975
- let k: u64 = 0
976
- while (k < earned && budget >= 0 && budget < QUIC_LISTENER_RESET_BURST) {
987
+ let earned: i64 = gap / 100
988
+ while (earned > 0 && budget >= 0 && budget < QUIC_LISTENER_RESET_BURST) {
977
989
  budget += 1
978
- k += toU64(1)
990
+ earned -= 1
979
991
  }
980
- refilled = toI64(toU64(refilled) + earned * toU64(QUIC_LISTENER_RESET_INTERVAL))
992
+ // Advanced by every whole interval in the gap: `now` less what is left
993
+ // over, which is at or above 0 because the gap is.
994
+ const rest: i64 = gap % 100
995
+ refilled = rest >= 0 ? now - rest : now
981
996
  }
982
997
  this.resetRefilled = refilled
983
998
  if (budget <= 0) {
@@ -1035,15 +1050,20 @@ export class QuicListener {
1035
1050
  if (reply.length > 0) {
1036
1051
  reply[0] = toU8((toI32(reply[0]) & 0x3f) | 0x40)
1037
1052
  }
1038
- quicConnResetTokenInto(
1039
- this.kdf.sha256,
1040
- this.config.statelessResetKey,
1041
- datagram,
1042
- dcidAt,
1043
- dcidLength,
1044
- reply,
1045
- tokenAt
1046
- )
1053
+ if (
1054
+ !quicConnResetTokenInto(
1055
+ this.kdf.sha256,
1056
+ this.config.statelessResetKey,
1057
+ datagram,
1058
+ dcidAt,
1059
+ dcidLength,
1060
+ reply,
1061
+ tokenAt
1062
+ )
1063
+ ) {
1064
+ quicListenerResize(reply, 0)
1065
+ return
1066
+ }
1047
1067
  answer.kind = QUIC_LISTEN_STATELESS_RESET
1048
1068
  }
1049
1069
  }
@@ -1070,16 +1090,18 @@ export const quicListenerTakePaced = (conn: QuicConnection, now: i64): u8[] | nu
1070
1090
  }
1071
1091
 
1072
1092
  /**
1073
- * `now + delay` for a pacer's delay, which is never below 0: summed in `u64`,
1074
- * where it cannot overflow, and held at the largest `i64` when a `now` near it
1075
- * would carry past; no caller's clock comes near.
1093
+ * `now + delay` for a pacer's delay, which is never below 0, held at the
1094
+ * largest `i64` when a `now` near it would carry past; no caller's clock comes
1095
+ * near. The sum is a `wrappingAdd` because the wrap is how that carry is seen:
1096
+ * a `now` at or above 0 plus a positive delay wraps below 0 only on overflow,
1097
+ * and then the answer is the cap, never the wrapped value.
1076
1098
  */
1077
1099
  const quicListenerLater = (now: i64, delay: i64): i64 => {
1078
1100
  if (delay <= 0) {
1079
1101
  return now
1080
1102
  }
1081
- const sum: i64 = toI64(toU64(now) + toU64(delay))
1082
- return now >= 0 && sum < 0 ? toI64((toU64(0) - toU64(1)) >> toU64(1)) : sum
1103
+ const sum: i64 = wrappingAdd(now, delay)
1104
+ return now >= 0 && sum < 0 ? toI64(-1) >>> toI64(1) : sum
1083
1105
  }
1084
1106
 
1085
1107
  /**
@@ -118,6 +118,7 @@ import { chacha20HeaderMask, chacha20Poly1305Open, chacha20Poly1305Seal } from "
118
118
  import { timingSafeEqual } from "nish/crypto/ct"
119
119
  import { tryCopyBytes } from "nish/bytes"
120
120
  import {
121
+ HkdfHash,
121
122
  HkdfScratch,
122
123
  hkdfExpandLabelHeldInto,
123
124
  hkdfExpandLabelInto,
@@ -370,9 +371,10 @@ export class QuicPacket {
370
371
  /**
371
372
  * HKDF-Expand-Label (RFC 8446 §7.1, RFC 9001 §5.1) with an empty context,
372
373
  * under the hash `aead` pairs with: SHA-384 for AES-256-GCM, SHA-256 for the
373
- * others. Every label and length here is a fixed one the expand accepts.
374
+ * others. Answers `null` where the expand does, which no fixed label and
375
+ * length here reaches.
374
376
  */
375
- const quicPacketExpandLabel = (aead: i32, secret: u8[], label: string, length: i32): u8[] =>
377
+ const quicPacketExpandLabel = (aead: i32, secret: u8[], label: string, length: i32): u8[] | null =>
376
378
  aead === QUIC_AEAD_AES_256_GCM
377
379
  ? hkdfExpandLabelSha384(secret, label, [], length)
378
380
  : hkdfExpandLabelSha256(secret, label, [], length)
@@ -1245,10 +1247,12 @@ export const quicInitialSecrets = (dcid: u8[]): QuicInitialSecrets | null => {
1245
1247
  return null
1246
1248
  }
1247
1249
  const initial: u8[] = hkdfExtractSha256(quicPacketInitialSalt(), dcid)
1248
- return new QuicInitialSecrets(
1249
- quicPacketExpandLabel(QUIC_AEAD_AES_128_GCM, initial, "client in", 32),
1250
- quicPacketExpandLabel(QUIC_AEAD_AES_128_GCM, initial, "server in", 32)
1251
- )
1250
+ const client: u8[] | null = quicPacketExpandLabel(QUIC_AEAD_AES_128_GCM, initial, "client in", 32)
1251
+ const server: u8[] | null = quicPacketExpandLabel(QUIC_AEAD_AES_128_GCM, initial, "server in", 32)
1252
+ if (client === null || server === null) {
1253
+ return null
1254
+ }
1255
+ return new QuicInitialSecrets(client, server)
1252
1256
  }
1253
1257
 
1254
1258
  /** The key length of `aead` in bytes, or 0 for no AEAD this module knows. */
@@ -1262,6 +1266,10 @@ const quicPacketKeyLength = (aead: i32): i32 => {
1262
1266
  return 0
1263
1267
  }
1264
1268
 
1269
+ /** The HKDF hash `aead` pairs with: SHA-384 for AES-256-GCM, SHA-256 for the others. */
1270
+ const quicPacketHkdfHash = (aead: i32): HkdfHash =>
1271
+ aead === QUIC_AEAD_AES_256_GCM ? HkdfHash.Sha384 : HkdfHash.Sha256
1272
+
1265
1273
  /** The length of `aead`'s hash, which is its traffic secret's length: 48 for SHA-384, else 32. */
1266
1274
  const quicPacketSecretLength = (aead: i32): i32 => (aead === QUIC_AEAD_AES_256_GCM ? 48 : 32)
1267
1275
 
@@ -1282,9 +1290,15 @@ const quicPacketDup = (bytes: u8[]): u8[] => quicPacketSlice(bytes, 0, toI32(byt
1282
1290
 
1283
1291
  /**
1284
1292
  * `quicPacketExpandLabel` of a held secret, answered held: the
1285
- * `hkdfExpandLabelSha*Secret` form, which wipes its own intermediates.
1293
+ * `hkdfExpandLabelSha*Secret` form, which wipes its own intermediates, and
1294
+ * `null` where it answers `null`.
1286
1295
  */
1287
- const quicPacketExpandHeld = (aead: i32, prk: Secret<u8[]>, label: string, length: i32): Secret<u8[]> => {
1296
+ const quicPacketExpandHeld = (
1297
+ aead: i32,
1298
+ prk: Secret<u8[]>,
1299
+ label: string,
1300
+ length: i32
1301
+ ): Secret<u8[]> | null => {
1288
1302
  const none: u8[] = []
1289
1303
  return aead === QUIC_AEAD_AES_256_GCM
1290
1304
  ? hkdfExpandLabelSha384Secret(prk, label, none, length)
@@ -1340,13 +1354,14 @@ export const quicKeys = (slot: QuicKeysSlot, aead: i32, trafficSecret: Secret<u8
1340
1354
  return null
1341
1355
  }
1342
1356
  const scratch: HkdfScratch = new HkdfScratch()
1357
+ let derived: boolean = false
1343
1358
  {
1344
1359
  using _scope = arena()
1345
1360
  const plain: u8[] = expose(trafficSecret, quicPacketDup)
1346
- quicKeysInto(scratch, slot, aead, plain)
1361
+ derived = quicKeysInto(scratch, slot, aead, plain) !== null
1347
1362
  secureZero(plain)
1348
1363
  }
1349
- return slot.keys
1364
+ return derived ? slot.keys : null
1350
1365
  }
1351
1366
 
1352
1367
  /**
@@ -1401,6 +1416,7 @@ export const quicKeysUpdate = (
1401
1416
  return null
1402
1417
  }
1403
1418
  const scratch: HkdfScratch = new HkdfScratch()
1419
+ let derived: boolean = false
1404
1420
  {
1405
1421
  using _scope = arena()
1406
1422
  const hpAes: AesKey | null = next.hpAes
@@ -1408,10 +1424,10 @@ export const quicKeysUpdate = (
1408
1424
  aesKeyInto(next.hp, hpAes)
1409
1425
  }
1410
1426
  const plain: u8[] = expose(nextSecret, quicPacketDup)
1411
- quicPacketDeriveInto(scratch, next, plain)
1427
+ derived = quicPacketDeriveInto(scratch, next, plain)
1412
1428
  secureZero(plain)
1413
1429
  }
1414
- return next
1430
+ return derived ? next : null
1415
1431
  }
1416
1432
 
1417
1433
  /**
@@ -1524,19 +1540,28 @@ const quicPacketPoint = (slot: QuicKeysSlot, aead: i32): void => {
1524
1540
  * `quicPacketDerive` in place: the packet key and IV from `secret` into
1525
1541
  * `keys`' own arrays, and for the AES suites the key's schedule expanded
1526
1542
  * into its own `AesKey`. HKDF runs in `kdf`, which wipes its secret-bearing fields, and
1527
- * the expansion's temporaries go with the arena block.
1543
+ * the expansion's temporaries go with the arena block. Answers whether both
1544
+ * expands derived; on `false` the key and IV are zeroed and the schedule is
1545
+ * not expanded.
1528
1546
  */
1529
- const quicPacketDeriveInto = (kdf: HkdfScratch, keys: QuicKeys, secret: u8[]): void => {
1547
+ const quicPacketDeriveInto = (kdf: HkdfScratch, keys: QuicKeys, secret: u8[]): boolean => {
1530
1548
  const aead: i32 = keys.aead
1531
- const hashLength: i32 = quicPacketSecretLength(aead)
1549
+ const hash: HkdfHash = quicPacketHkdfHash(aead)
1532
1550
  using _scope = arena()
1533
1551
  const none: u8[] = []
1534
- hkdfExpandLabelInto(kdf, hashLength, secret, "quic key", none, 0, 0, keys.key, 0, quicPacketKeyLength(aead))
1535
- hkdfExpandLabelInto(kdf, hashLength, secret, "quic iv", none, 0, 0, keys.iv, 0, QUIC_IV_SIZE)
1552
+ const derived: boolean =
1553
+ hkdfExpandLabelInto(kdf, hash, secret, "quic key", none, 0, 0, keys.key, 0, quicPacketKeyLength(aead)) &&
1554
+ hkdfExpandLabelInto(kdf, hash, secret, "quic iv", none, 0, 0, keys.iv, 0, QUIC_IV_SIZE)
1555
+ if (!derived) {
1556
+ secureZero(keys.key)
1557
+ secureZero(keys.iv)
1558
+ return false
1559
+ }
1536
1560
  const packetAes: AesKey | null = keys.packetAes
1537
1561
  if (packetAes !== null) {
1538
1562
  aesKeyInto(keys.key, packetAes)
1539
1563
  }
1564
+ return true
1540
1565
  }
1541
1566
 
1542
1567
  /**
@@ -1565,22 +1590,29 @@ export const quicKeysInto = (
1565
1590
  }
1566
1591
  quicPacketPoint(slot, aead)
1567
1592
  const keys: QuicKeys = slot.keys
1568
- quicPacketDeriveInto(kdf, keys, secret)
1593
+ if (!quicPacketDeriveInto(kdf, keys, secret)) {
1594
+ return null
1595
+ }
1569
1596
  {
1570
1597
  using _scope = arena()
1571
1598
  const none: u8[] = []
1572
- hkdfExpandLabelInto(
1573
- kdf,
1574
- quicPacketSecretLength(aead),
1575
- secret,
1576
- "quic hp",
1577
- none,
1578
- 0,
1579
- 0,
1580
- keys.hp,
1581
- 0,
1582
- quicPacketKeyLength(aead)
1583
- )
1599
+ if (
1600
+ !hkdfExpandLabelInto(
1601
+ kdf,
1602
+ quicPacketHkdfHash(aead),
1603
+ secret,
1604
+ "quic hp",
1605
+ none,
1606
+ 0,
1607
+ 0,
1608
+ keys.hp,
1609
+ 0,
1610
+ quicPacketKeyLength(aead)
1611
+ )
1612
+ ) {
1613
+ quicKeysWipe(keys)
1614
+ return null
1615
+ }
1584
1616
  const hpAes: AesKey | null = keys.hpAes
1585
1617
  if (hpAes !== null) {
1586
1618
  aesKeyInto(keys.hp, hpAes)
@@ -1618,8 +1650,7 @@ export const quicKeysUpdateInto = (
1618
1650
  const next: QuicKeys = slot.keys
1619
1651
  next.hp = keys.hp
1620
1652
  next.hpAes = keys.hpAes
1621
- quicPacketDeriveInto(kdf, next, nextSecret)
1622
- return next
1653
+ return quicPacketDeriveInto(kdf, next, nextSecret) ? next : null
1623
1654
  }
1624
1655
 
1625
1656
  /**
@@ -1632,7 +1663,8 @@ export const quicKeysUpdateInto = (
1632
1663
  * @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
1633
1664
  * @param secret The current traffic secret as a plain array; only read, and not wiped.
1634
1665
  * @param out The array the next secret is written over, 32 bytes (48 for AES-256-GCM).
1635
- * @returns True when `out` holds the next secret; false, `out` untouched, for a wrong length or `aead`.
1666
+ * @returns True when `out` holds the next secret; false, `out` untouched, for a wrong length or `aead`, or
1667
+ * false, `out` zeroed, where the expand refuses an HMAC step, which a checked length never makes it do.
1636
1668
  */
1637
1669
  export const quicKeyUpdateSecretInto = (kdf: HkdfScratch, aead: i32, secret: u8[], out: u8[]): boolean => {
1638
1670
  const length: i32 = quicPacketSecretLength(aead)
@@ -1641,8 +1673,7 @@ export const quicKeyUpdateSecretInto = (kdf: HkdfScratch, aead: i32, secret: u8[
1641
1673
  }
1642
1674
  using _scope = arena()
1643
1675
  const none: u8[] = []
1644
- hkdfExpandLabelInto(kdf, length, secret, "quic ku", none, 0, 0, out, 0, length)
1645
- return true
1676
+ return hkdfExpandLabelInto(kdf, quicPacketHkdfHash(aead), secret, "quic ku", none, 0, 0, out, 0, length)
1646
1677
  }
1647
1678
 
1648
1679
  /**
@@ -1663,12 +1694,17 @@ export const quicKeysHeldInto = (slot: QuicKeysSlot, aead: i32, secret: Secret<u
1663
1694
  }
1664
1695
  quicPacketPoint(slot, aead)
1665
1696
  const keys: QuicKeys = slot.keys
1666
- const hashLength: i32 = quicPacketSecretLength(aead)
1697
+ const hash: HkdfHash = quicPacketHkdfHash(aead)
1667
1698
  const keyLength: i32 = quicPacketKeyLength(aead)
1668
1699
  const none: u8[] = []
1669
- hkdfExpandLabelHeldInto(hashLength, secret, "quic key", none, keys.key, 0, keyLength)
1670
- hkdfExpandLabelHeldInto(hashLength, secret, "quic iv", none, keys.iv, 0, QUIC_IV_SIZE)
1671
- hkdfExpandLabelHeldInto(hashLength, secret, "quic hp", none, keys.hp, 0, keyLength)
1700
+ if (
1701
+ !hkdfExpandLabelHeldInto(hash, secret, "quic key", none, keys.key, 0, keyLength) ||
1702
+ !hkdfExpandLabelHeldInto(hash, secret, "quic iv", none, keys.iv, 0, QUIC_IV_SIZE) ||
1703
+ !hkdfExpandLabelHeldInto(hash, secret, "quic hp", none, keys.hp, 0, keyLength)
1704
+ ) {
1705
+ quicKeysWipe(keys)
1706
+ return null
1707
+ }
1672
1708
  using _scope = arena()
1673
1709
  const packetAes: AesKey | null = keys.packetAes
1674
1710
  if (packetAes !== null) {
@@ -1698,12 +1734,9 @@ export const quicKeyUpdateSecretHeldInto = (dst: Secret<u8[]>, src: Secret<u8[]>
1698
1734
  return false
1699
1735
  }
1700
1736
  const none: u8[] = []
1701
- if (length === 48) {
1702
- hkdfExpandLabelSha384Held(dst, src, "quic ku", none)
1703
- } else {
1704
- hkdfExpandLabelSha256Held(dst, src, "quic ku", none)
1705
- }
1706
- return true
1737
+ return length === 48
1738
+ ? hkdfExpandLabelSha384Held(dst, src, "quic ku", none)
1739
+ : hkdfExpandLabelSha256Held(dst, src, "quic ku", none)
1707
1740
  }
1708
1741
 
1709
1742
  /**
@@ -1719,7 +1752,7 @@ export const quicKeyUpdateSecretHeldInto = (dst: Secret<u8[]>, src: Secret<u8[]>
1719
1752
  * @param length The ID's length in bytes, 0 to 20.
1720
1753
  * @param client The 32-byte array `client_initial_secret` is written over.
1721
1754
  * @param server The 32-byte array `server_initial_secret` is written over.
1722
- * @returns True when both secrets were written; false, both untouched, otherwise.
1755
+ * @returns True when both secrets were written; false, both untouched, for a refused length or window, or false, `client` possibly still written, if a derive refuses (none in range does).
1723
1756
  */
1724
1757
  export const quicInitialSecretsInto = (
1725
1758
  kdf: HkdfScratch,
@@ -1742,11 +1775,19 @@ export const quicInitialSecretsInto = (
1742
1775
  using _scope = arena()
1743
1776
  const none: u8[] = []
1744
1777
  const initial: u8[] = new Array<u8>(32)
1745
- hkdfExtractInto(kdf, 32, quicPacketInitialSalt(), quicPacketTake(buf, at, length), initial, 0)
1746
- hkdfExpandLabelInto(kdf, 32, initial, "client in", none, 0, 0, client, 0, 32)
1747
- hkdfExpandLabelInto(kdf, 32, initial, "server in", none, 0, 0, server, 0, 32)
1778
+ const derived: boolean =
1779
+ hkdfExtractInto(
1780
+ kdf,
1781
+ HkdfHash.Sha256,
1782
+ quicPacketInitialSalt(),
1783
+ quicPacketTake(buf, at, length),
1784
+ initial,
1785
+ 0
1786
+ ) &&
1787
+ hkdfExpandLabelInto(kdf, HkdfHash.Sha256, initial, "client in", none, 0, 0, client, 0, 32) &&
1788
+ hkdfExpandLabelInto(kdf, HkdfHash.Sha256, initial, "server in", none, 0, 0, server, 0, 32)
1748
1789
  secureZero(initial)
1749
- return true
1790
+ return derived
1750
1791
  }
1751
1792
 
1752
1793
  /** The bits of the first byte header protection covers: four in a long header, five in a short one (RFC 9001 §5.4.1). */
@@ -95,6 +95,7 @@
95
95
  * (`docs/wp26-stdlib.md` §3e).
96
96
  */
97
97
  import { QUIC_MAX_VARINT } from "nish/net/quic-packet"
98
+ import { wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
98
99
 
99
100
  /** The Initial space's index, which is `TLS_LEVEL_INITIAL`. */
100
101
  export const QUIC_RECOVERY_INITIAL: i32 = 0
@@ -225,18 +226,18 @@ const QUIC_RECOVERY_CUBIC_SCALE: i64 = 5000000000
225
226
  const quicRecoveryMax = (a: i64, b: i64): i64 => (a > b ? a : b)
226
227
 
227
228
  /** 2^63 − 1, the largest `i64`, which a sum or product past it is held at. */
228
- const quicRecoveryTop = (): i64 => toI64((toU64(1) << toU64(63)) - toU64(1))
229
+ const quicRecoveryTop = (): i64 => toI64(-1) >>> toI64(1)
229
230
 
230
231
  /**
231
232
  * `a + b`, held at 2^63 − 1 when the true sum is past it and at −2^63 when
232
233
  * it is below. Recovery's times, durations and byte counts are far inside
233
234
  * an `i64`, so a held sum is one no clock or window reaches: a deadline that
234
- * never comes, a window nothing fills. Taken in `u64`, whose `+` wraps, and
235
- * read back as the `i64` with the same bits, which is the true sum whenever
236
- * that fits.
235
+ * never comes, a window nothing fills. The wrap is how the overflow is
236
+ * seen, not an answer: `wrappingAdd` is the true sum whenever that fits, and
237
+ * its sign crosses only when it does not, which is held instead.
237
238
  */
238
239
  const quicRecoverySum = (a: i64, b: i64): i64 => {
239
- const sum: i64 = toI64(toU64(a) + toU64(b))
240
+ const sum: i64 = wrappingAdd(a, b)
240
241
  if (a >= 0 && b >= 0 && sum < 0) {
241
242
  return quicRecoveryTop()
242
243
  }
@@ -248,7 +249,7 @@ const quicRecoverySum = (a: i64, b: i64): i64 => {
248
249
 
249
250
  /** `a − b`, held at 2^63 − 1 / −2^63 where the true difference passes either. */
250
251
  const quicRecoveryLess = (a: i64, b: i64): i64 => {
251
- const diff: i64 = toI64(toU64(a) - toU64(b))
252
+ const diff: i64 = wrappingSub(a, b)
252
253
  if (a >= 0 && b < 0 && diff < 0) {
253
254
  return quicRecoveryTop()
254
255
  }
@@ -264,8 +265,9 @@ const quicRecoveryStep = (n: i32): i32 => (n >= 2147483647 ? n : n + 1)
264
265
  /**
265
266
  * `a × b` for two non-negative times, durations or byte counts, held at
266
267
  * 2^63 − 1 when the true product is past it, as `quicRecoverySum` holds a
267
- * sum. Below that it is taken in `u64` and is exact. A negative operand
268
- * counts as 0: every caller passes values it has kept at 0 or above.
268
+ * sum. Below that, with `b` at most 2^63 − 1 over `a`, the product fits, so
269
+ * `wrappingMul` is exact and never wraps. A negative operand counts as 0:
270
+ * every caller passes values it has kept at 0 or above.
269
271
  */
270
272
  const quicRecoveryProduct = (a: i64, b: i64): i64 => {
271
273
  if (a <= 0 || b <= 0) {
@@ -274,7 +276,7 @@ const quicRecoveryProduct = (a: i64, b: i64): i64 => {
274
276
  if (b > quicRecoveryTop() / a) {
275
277
  return quicRecoveryTop()
276
278
  }
277
- return toI64(toU64(a) * toU64(b))
279
+ return wrappingMul(a, b)
278
280
  }
279
281
 
280
282
  /** `x` clamped to −`QUIC_RECOVERY_CUBIC_SPAN` .. `QUIC_RECOVERY_CUBIC_SPAN`. */