nish 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/README.md +27 -21
  2. package/docs/AI.md +237 -160
  3. package/docs/INSTALL.md +1 -1
  4. package/package.json +12 -12
  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
@@ -863,8 +863,7 @@ export class TlsClient {
863
863
  if (length < 0 || length > 65536) {
864
864
  return false
865
865
  }
866
- this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
867
- return true
866
+ return this.transcript.tryUpdate(this.input, TLS_CLIENT_FROM, 4 + length)
868
867
  }
869
868
 
870
869
  /** A ServerHello or HelloRetryRequest, `input[0 .. 4 + length)`. */
@@ -892,8 +891,8 @@ export class TlsClient {
892
891
  if (view.shareLength !== shareSize) {
893
892
  return TLS_ALERT_ILLEGAL_PARAMETER
894
893
  }
895
- if (!this.retried) {
896
- this.startTranscript(view.suite)
894
+ if (!this.retried && !this.startTranscript(view.suite)) {
895
+ return TLS_ALERT_INTERNAL_ERROR
897
896
  }
898
897
  if (!this.hashMessage(length)) {
899
898
  return TLS_ALERT_INTERNAL_ERROR
@@ -905,8 +904,26 @@ export class TlsClient {
905
904
  const pool: TlsSecretPool = this.secrets()
906
905
  const h: i32 = this.hashLength
907
906
  this.transcript.hashInto(this.hashes, TLS_CLIENT_FROM)
908
- tlsDeriveSecretHeld(h, pool.handshake, "c hs traffic", this.hashes, TLS_CLIENT_FROM, pool.clientHandshake)
909
- tlsDeriveSecretHeld(h, pool.handshake, "s hs traffic", this.hashes, TLS_CLIENT_FROM, pool.serverHandshake)
907
+ if (
908
+ !tlsDeriveSecretHeld(
909
+ h,
910
+ pool.handshake,
911
+ "c hs traffic",
912
+ this.hashes,
913
+ TLS_CLIENT_FROM,
914
+ pool.clientHandshake
915
+ ) ||
916
+ !tlsDeriveSecretHeld(
917
+ h,
918
+ pool.handshake,
919
+ "s hs traffic",
920
+ this.hashes,
921
+ TLS_CLIENT_FROM,
922
+ pool.serverHandshake
923
+ )
924
+ ) {
925
+ return TLS_ALERT_INTERNAL_ERROR
926
+ }
910
927
  this.held =
911
928
  this.held |
912
929
  (1 << TLS_SECRET_HANDSHAKE) |
@@ -916,12 +933,16 @@ export class TlsClient {
916
933
  return 0
917
934
  }
918
935
 
919
- /** Starts the transcript over `suite`'s hash, from the first ClientHello. */
920
- private startTranscript(suite: i32): void {
936
+ /**
937
+ * Starts the transcript over `suite`'s hash, from the first ClientHello;
938
+ * false when the hash refuses the hello's window, so the caller fails the
939
+ * handshake.
940
+ */
941
+ private startTranscript(suite: i32): boolean {
921
942
  this.suite = suite
922
943
  this.hashLength = tlsSuiteHashLength(suite)
923
944
  this.transcript.reset(this.hashLength)
924
- this.transcript.update(this.firstHello, TLS_CLIENT_FROM, toI32(this.firstHello.length))
945
+ return this.transcript.tryUpdate(this.firstHello, TLS_CLIENT_FROM, toI32(this.firstHello.length))
925
946
  }
926
947
 
927
948
  /**
@@ -944,8 +965,12 @@ export class TlsClient {
944
965
  return TLS_ALERT_ILLEGAL_PARAMETER
945
966
  }
946
967
  this.retried = true
947
- this.startTranscript(view.suite)
948
- this.transcript.restartWithMessageHash()
968
+ if (!this.startTranscript(view.suite)) {
969
+ return TLS_ALERT_INTERNAL_ERROR
970
+ }
971
+ if (!this.transcript.tryRestartWithMessageHash()) {
972
+ return TLS_ALERT_INTERNAL_ERROR
973
+ }
949
974
  if (!this.hashMessage(length)) {
950
975
  return TLS_ALERT_INTERNAL_ERROR
951
976
  }
@@ -1002,10 +1027,10 @@ export class TlsClient {
1002
1027
  }
1003
1028
  const h: i32 = this.hashLength
1004
1029
  const early: Secret<u8[]> = tlsEarlySecret(h)
1005
- tlsHandshakeSecretHeld(h, early, shared, this.secrets().handshake)
1030
+ const derived: boolean = tlsHandshakeSecretHeld(h, early, shared, this.secrets().handshake)
1006
1031
  wipe(early)
1007
1032
  wipe(shared)
1008
- return 0
1033
+ return derived ? 0 : TLS_ALERT_INTERNAL_ERROR
1009
1034
  }
1010
1035
 
1011
1036
  /** EncryptedExtensions (§4.3.1): ALPN, if any, must be one the client offered, and `server_name` acknowledged only if sent. */
@@ -1095,7 +1120,15 @@ export class TlsClient {
1095
1120
  }
1096
1121
  const pool: TlsSecretPool = this.secrets()
1097
1122
  this.transcript.hashInto(this.hashes, TLS_CLIENT_FROM)
1098
- const expected: u8[] = tlsFinishedVerifyDataHeld(h, pool.serverHandshake, this.hashes, TLS_CLIENT_FROM)
1123
+ const expected: u8[] | null = tlsFinishedVerifyDataHeld(
1124
+ h,
1125
+ pool.serverHandshake,
1126
+ this.hashes,
1127
+ TLS_CLIENT_FROM
1128
+ )
1129
+ if (expected === null) {
1130
+ return TLS_ALERT_INTERNAL_ERROR
1131
+ }
1099
1132
  const matches: boolean = timingSafeEqualAt(this.input, 4, expected, TLS_CLIENT_FROM, h)
1100
1133
  secureZero(expected)
1101
1134
  if (!matches) {
@@ -1107,26 +1140,38 @@ export class TlsClient {
1107
1140
  this.transcript.hashInto(this.hashes, TLS_CLIENT_FROM)
1108
1141
  // The master secret is derived over the handshake secret, which has no
1109
1142
  // other use left.
1110
- tlsMasterSecretHeld(h, pool.handshake, pool.handshake)
1111
- tlsDeriveSecretHeld(
1112
- h,
1113
- pool.handshake,
1114
- "c ap traffic",
1115
- this.hashes,
1116
- TLS_CLIENT_FROM,
1117
- pool.clientApplication
1118
- )
1119
- tlsDeriveSecretHeld(
1143
+ if (
1144
+ !tlsMasterSecretHeld(h, pool.handshake, pool.handshake) ||
1145
+ !tlsDeriveSecretHeld(
1146
+ h,
1147
+ pool.handshake,
1148
+ "c ap traffic",
1149
+ this.hashes,
1150
+ TLS_CLIENT_FROM,
1151
+ pool.clientApplication
1152
+ ) ||
1153
+ !tlsDeriveSecretHeld(
1154
+ h,
1155
+ pool.handshake,
1156
+ "s ap traffic",
1157
+ this.hashes,
1158
+ TLS_CLIENT_FROM,
1159
+ pool.serverApplication
1160
+ ) ||
1161
+ !tlsDeriveSecretHeld(h, pool.handshake, "exp master", this.hashes, TLS_CLIENT_FROM, pool.exporter)
1162
+ ) {
1163
+ return TLS_ALERT_INTERNAL_ERROR
1164
+ }
1165
+ pool.zero(TLS_SECRET_HANDSHAKE)
1166
+ const verifyData: u8[] | null = tlsFinishedVerifyDataHeld(
1120
1167
  h,
1121
- pool.handshake,
1122
- "s ap traffic",
1168
+ pool.clientHandshake,
1123
1169
  this.hashes,
1124
- TLS_CLIENT_FROM,
1125
- pool.serverApplication
1170
+ TLS_CLIENT_FROM
1126
1171
  )
1127
- tlsDeriveSecretHeld(h, pool.handshake, "exp master", this.hashes, TLS_CLIENT_FROM, pool.exporter)
1128
- pool.zero(TLS_SECRET_HANDSHAKE)
1129
- const verifyData: u8[] = tlsFinishedVerifyDataHeld(h, pool.clientHandshake, this.hashes, TLS_CLIENT_FROM)
1172
+ if (verifyData === null) {
1173
+ return TLS_ALERT_INTERNAL_ERROR
1174
+ }
1130
1175
  const at: i32 = this.outputHandshakeLength
1131
1176
  const end: i32 = tlsWriteFinished(this.outputHandshake, at, verifyData, TLS_CLIENT_FROM, h)
1132
1177
  secureZero(verifyData)
@@ -1255,12 +1300,14 @@ export class TlsClient {
1255
1300
  return false
1256
1301
  }
1257
1302
  if (level === TLS_LEVEL_INITIAL) {
1258
- if (this.hashLength !== 0) {
1259
- this.transcript.update(this.outputInitial, at, end - at)
1303
+ if (this.hashLength !== 0 && !this.transcript.tryUpdate(this.outputInitial, at, end - at)) {
1304
+ return false
1260
1305
  }
1261
1306
  this.outputInitialLength = end
1262
1307
  } else {
1263
- this.transcript.update(this.outputHandshake, at, end - at)
1308
+ if (!this.transcript.tryUpdate(this.outputHandshake, at, end - at)) {
1309
+ return false
1310
+ }
1264
1311
  this.outputHandshakeLength = end
1265
1312
  }
1266
1313
  return true
@@ -1847,11 +1847,10 @@ export const tlsWriteCertificateVerifyContent = (
1847
1847
  * bytes or with a vector its length field cannot hold, gives an empty one, which the writer then leaves empty.
1848
1848
  */
1849
1849
  const tlsZeros = (n: i32): u8[] => {
1850
- const out: u8[] = []
1851
- for (let k: i32 = 0; k < n; k++) {
1852
- out.push(0)
1850
+ if (n <= 0) {
1851
+ return []
1853
1852
  }
1854
- return out
1853
+ return new Array<u8>(n)
1855
1854
  }
1856
1855
 
1857
1856
  /** The bytes of `name` as a string of one character a byte, which `tlsWriteEncryptedExtensions` writes back as they were. */
@@ -71,10 +71,11 @@ import {
71
71
  TLS_LEGACY_VERSION,
72
72
  } from "nish/net/tls/codec"
73
73
  import {
74
+ HkdfHash,
74
75
  HkdfScratch,
75
- hkdfExpandLabelInto,
76
- hkdfExpandLabelSha256Held,
77
- hkdfExpandLabelSha384Held,
76
+ tryHkdfExpandLabelInto,
77
+ tryHkdfExpandLabelSha256Held,
78
+ tryHkdfExpandLabelSha384Held,
78
79
  } from "nish/crypto/hkdf"
79
80
  import { SHA384_SIZE } from "nish/crypto/sha512"
80
81
  import {
@@ -144,6 +145,23 @@ export const TLS_KEY_UPDATE_RECORDS: i64 = 16777216
144
145
  */
145
146
  export const TLS_LAST_SEQUENCE: i64 = 9007199254740991
146
147
 
148
+ /**
149
+ * The sequence number after `sequence`, or -1 when no record may use
150
+ * `sequence`: below 0, or at `TLS_LAST_SEQUENCE`, where §5.3's counter would
151
+ * have to wrap. Counted down from `TLS_LAST_SEQUENCE`, so every step is a sum
152
+ * `src/bounds.ts` proves, never a wrap.
153
+ */
154
+ const tlsNextSequence = (sequence: i64): i64 => {
155
+ if (sequence < 0) {
156
+ return -1
157
+ }
158
+ const left: i64 = TLS_LAST_SEQUENCE - sequence
159
+ if (left < 1) {
160
+ return -1
161
+ }
162
+ return TLS_LAST_SEQUENCE - (left - 1)
163
+ }
164
+
147
165
  /** A typed zero for the offsets below: a bare literal is an `f64` under `--number-mode f64`. */
148
166
  const TLS_RECORD_FROM: i32 = 0
149
167
 
@@ -155,9 +173,9 @@ const TLS_RECORD_FROM: i32 = 0
155
173
  *
156
174
  * @param hashLength The hash length of the connection's suite, 32 or 48, and so how many bytes are derived.
157
175
  * @param trafficSecret The direction's current traffic secret. This function does not wipe it.
158
- * @returns A fresh `Secret` holding the next traffic secret, which the caller owns and wipes.
176
+ * @returns A fresh `Secret` holding the next traffic secret, which the caller owns and wipes, or `null` when `hashLength` is outside 0 to 255, so no `HkdfLabel` holds it, or the secret is shorter than the hash it names (SHA-384 for 48, SHA-256 for any other).
159
177
  */
160
- export const tlsNextTrafficSecret = (hashLength: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> => {
178
+ export const tlsNextTrafficSecret = (hashLength: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> | null => {
161
179
  const none: u8[] = []
162
180
  return tlsExpandLabel(hashLength, trafficSecret, "traffic upd", none, hashLength)
163
181
  }
@@ -308,13 +326,15 @@ export class TlsRecordReader {
308
326
  /**
309
327
  * Copies as many of `data[off .. off + len)` as fit and answers how many,
310
328
  * after moving what is held to the front. A window outside `data` is
311
- * `-TLS_ALERT_INTERNAL_ERROR`, and nothing is copied.
329
+ * `-TLS_ALERT_INTERNAL_ERROR`, and nothing is copied; so is a first byte
330
+ * that finds a shared pool (`borrowFrom`) with every buffer lent.
312
331
  *
313
332
  * @param data The array that holds the bytes read from the stream.
314
333
  * @param off Where in `data` the new bytes start.
315
334
  * @param len How many bytes, from `off`, to offer.
316
335
  * @returns How many of the bytes were taken, from 0 (the buffer is full) up to `len`; the caller offers the
317
- * rest again once a record is consumed. `-TLS_ALERT_INTERNAL_ERROR` for a window outside `data`.
336
+ * rest again once a record is consumed. `-TLS_ALERT_INTERNAL_ERROR` for a window outside `data`
337
+ * or a spent shared pool.
318
338
  */
319
339
  push(data: u8[], off: i32, len: i32): i32 {
320
340
  if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
@@ -324,7 +344,12 @@ export class TlsRecordReader {
324
344
  return 0
325
345
  }
326
346
  if (this.buffer.length === 0) {
327
- this.buffer = this.pool.take()
347
+ // A shared pool with every buffer lent leaves this reader nothing to read into.
348
+ const taken: u8[] | null = this.pool.tryTake()
349
+ if (taken === null) {
350
+ return -TLS_ALERT_INTERNAL_ERROR
351
+ }
352
+ this.buffer = taken
328
353
  }
329
354
  const buffer: u8[] = this.buffer
330
355
  let start: i32 = this.start
@@ -528,7 +553,9 @@ export class TlsRecordProtection {
528
553
  return false
529
554
  }
530
555
  this.reserve(suite)
531
- this.derive(suite, secret)
556
+ if (!this.derive(suite, secret)) {
557
+ return false
558
+ }
532
559
  this.suite = suite
533
560
  return true
534
561
  }
@@ -547,15 +574,21 @@ export class TlsRecordProtection {
547
574
  * direction's own HKDF scratch, which is wiped after, and the schedule
548
575
  * expanded into this direction's `AesKey` by `aesKeyInto`. All of it runs
549
576
  * inside an arena block, so a key install leaves the arena where it was.
550
- * ChaCha20 has no schedule.
577
+ * ChaCha20 has no schedule. False, with no schedule expanded, when
578
+ * `tlsTrafficKeysInto` refuses the secret or the arrays; whatever it wrote
579
+ * of the key and IV is zeroed by `clear` before it answers.
551
580
  */
552
- private derive(suite: i32, secret: u8[]): void {
581
+ private derive(suite: i32, secret: u8[]): boolean {
553
582
  using _scope = arena()
554
- tlsTrafficKeysInto(this.kdf, suite, secret, this.key, this.iv)
583
+ if (!tlsTrafficKeysInto(this.kdf, suite, secret, this.key, this.iv)) {
584
+ this.clear()
585
+ return false
586
+ }
555
587
  const aes: AesKey | null = this.aes
556
588
  if (aes !== null && suite !== TLS_CHACHA20_POLY1305_SHA256) {
557
589
  aesKeyInto(this.key, aes)
558
590
  }
591
+ return true
559
592
  }
560
593
 
561
594
  /**
@@ -575,7 +608,7 @@ export class TlsRecordProtection {
575
608
  * keys are. False, as `install` answers, when the keys do not install.
576
609
  *
577
610
  * @param secret The direction's current traffic secret, plain, which is overwritten with the next one. It
578
- * stays the caller's to wipe. One shorter than the suite's hash length panics in HKDF.
611
+ * stays the caller's to wipe. One not of the suite's hash length is left as it was, and answers `false`.
579
612
  * @returns `true` when the next keys are installed; `false` for a cleartext direction, or when the install
580
613
  * fails and the direction is left cleartext.
581
614
  */
@@ -586,12 +619,13 @@ export class TlsRecordProtection {
586
619
  return false
587
620
  }
588
621
  const next: u8[] = this.next
622
+ let stepped: boolean = false
589
623
  {
590
624
  using _scope = arena()
591
625
  const none: u8[] = []
592
- hkdfExpandLabelInto(
626
+ stepped = tryHkdfExpandLabelInto(
593
627
  this.kdf,
594
- hashLength,
628
+ hashLength === SHA384_SIZE ? HkdfHash.Sha384 : HkdfHash.Sha256,
595
629
  secret,
596
630
  "traffic upd",
597
631
  none,
@@ -602,10 +636,18 @@ export class TlsRecordProtection {
602
636
  hashLength
603
637
  )
604
638
  }
605
- for (let k: i32 = 0; k < toI32(secret.length) && k < hashLength && k < toI32(next.length); k++) {
639
+ for (
640
+ let k: i32 = 0;
641
+ stepped && k < toI32(secret.length) && k < hashLength && k < toI32(next.length);
642
+ k++
643
+ ) {
606
644
  secret[k] = next[k]
607
645
  }
608
646
  secureZero(next)
647
+ if (!stepped) {
648
+ this.clear()
649
+ return false
650
+ }
609
651
  return this.install(suite, secret)
610
652
  }
611
653
 
@@ -627,7 +669,10 @@ export class TlsRecordProtection {
627
669
  return false
628
670
  }
629
671
  this.reserve(suite)
630
- tlsTrafficKeysFromHeld(suite, secret, this.key, this.iv)
672
+ if (!tlsTrafficKeysFromHeld(suite, secret, this.key, this.iv)) {
673
+ this.clear()
674
+ return false
675
+ }
631
676
  const aes: AesKey | null = this.aes
632
677
  if (aes !== null && suite !== TLS_CHACHA20_POLY1305_SHA256) {
633
678
  using _scope = arena()
@@ -655,13 +700,17 @@ export class TlsRecordProtection {
655
700
  if (hashLength === 0) {
656
701
  return false
657
702
  }
703
+ let stepped: boolean = true
658
704
  if (expose(secret, (bytes: u8[]): i32 => toI32(bytes.length)) === hashLength) {
659
705
  const none: u8[] = []
660
- if (hashLength === SHA384_SIZE) {
661
- hkdfExpandLabelSha384Held(secret, secret, "traffic upd", none)
662
- } else {
663
- hkdfExpandLabelSha256Held(secret, secret, "traffic upd", none)
664
- }
706
+ stepped =
707
+ hashLength === SHA384_SIZE
708
+ ? tryHkdfExpandLabelSha384Held(secret, secret, "traffic upd", none)
709
+ : tryHkdfExpandLabelSha256Held(secret, secret, "traffic upd", none)
710
+ }
711
+ if (!stepped) {
712
+ this.clear()
713
+ return false
665
714
  }
666
715
  return this.installHeld(suite, secret)
667
716
  }
@@ -742,10 +791,9 @@ export class TlsRecordProtection {
742
791
  }
743
792
  return TLS_RECORD_HEADER_SIZE + len
744
793
  }
745
- // The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule, and it
746
- // keeps the step at the end from overflowing.
747
- const sequence: i64 = this.sequence
748
- if (padding > TLS_MAX_INNER_PLAINTEXT - 1 - len || sequence < 0 || sequence >= 9007199254740991) {
794
+ // The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule.
795
+ const next: i64 = tlsNextSequence(this.sequence)
796
+ if (padding > TLS_MAX_INNER_PLAINTEXT - 1 - len || next < 0) {
749
797
  return -TLS_ALERT_INTERNAL_ERROR
750
798
  }
751
799
  const inner: i32 = len + 1 + padding
@@ -786,8 +834,7 @@ export class TlsRecordProtection {
786
834
  out[k] = ciphertext[i]
787
835
  k++
788
836
  }
789
- // Unsigned, the step cannot panic; it cannot wrap either, since `sequence` is below 2^53 - 1.
790
- this.sequence = toI64(toU64(sequence) + toU64(1))
837
+ this.sequence = next
791
838
  return TLS_RECORD_HEADER_SIZE + body
792
839
  }
793
840
 
@@ -874,10 +921,9 @@ export class TlsRecordProtection {
874
921
  if (body < TLS_RECORD_TAG_SIZE + 1) {
875
922
  return -TLS_ALERT_BAD_RECORD_MAC
876
923
  }
877
- // The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule, and it
878
- // keeps the step below from wrapping.
879
- const sequence: i64 = this.sequence
880
- if (sequence < 0 || sequence >= 9007199254740991) {
924
+ // The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule.
925
+ const next: i64 = tlsNextSequence(this.sequence)
926
+ if (next < 0) {
881
927
  return -TLS_ALERT_INTERNAL_ERROR
882
928
  }
883
929
  // `body` is at most 2^14 + 256, so it is a u16 as it is.
@@ -908,9 +954,8 @@ export class TlsRecordProtection {
908
954
  }
909
955
  const inner: u8[] = opened
910
956
  // The record authenticated, so it is the peer's, and its number is spent
911
- // whatever its content turns out to be. Unsigned, the step cannot panic, and it cannot wrap from below
912
- // 2^53 - 1.
913
- this.sequence = toI64(toU64(sequence) + toU64(1))
957
+ // whatever its content turns out to be.
958
+ this.sequence = next
914
959
  const innerLength: i32 = toI32(inner.length)
915
960
  if (innerLength > TLS_MAX_INNER_PLAINTEXT) {
916
961
  return -TLS_ALERT_RECORD_OVERFLOW