@amritk/nish-x86_64-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
@@ -58,14 +58,16 @@
58
58
  * Written from RFC 8446, not ported from another implementation.
59
59
  */
60
60
  import {
61
+ HkdfHash,
61
62
  HkdfScratch,
62
- hkdfExpandLabelHeldInto,
63
63
  hkdfExpandLabelInto,
64
- hkdfExpandLabelSha256Held,
65
64
  hkdfExpandLabelSha256Secret,
66
- hkdfExpandLabelSha384Held,
67
65
  hkdfExpandLabelSha384Secret,
68
66
  hkdfExtractInto,
67
+ tryHkdfExpandLabelHeldInto,
68
+ tryHkdfExpandLabelInto,
69
+ tryHkdfExpandLabelSha256Held,
70
+ tryHkdfExpandLabelSha384Held,
69
71
  } from "nish/crypto/hkdf"
70
72
  import {
71
73
  hmacCopySha256,
@@ -137,15 +139,18 @@ export const tlsSuiteKeyLength = (suite: i32): i32 => {
137
139
  /**
138
140
  * HKDF-Expand-Label (RFC 8446 §7.1) over the hash `hashLength` names: `length`
139
141
  * bytes from `prk` (RFC 8446's Secret), the label (without its `"tls13 "`
140
- * prefix, which is added) and `context`, held as a `Secret`. It panics as
141
- * `hkdfExpandLabelSha256Secret` (or its SHA-384 twin) does.
142
+ * prefix, which is added) and `context`, held as a `Secret`; `null` where
143
+ * `hkdfExpandLabelSha256Secret` (or its SHA-384 twin) answers it: a label or
144
+ * context too long for the `HkdfLabel`, or a length below 0 or past 255
145
+ * blocks of the hash.
142
146
  *
143
147
  * @param hashLength 48 for SHA-384; any other value runs SHA-256.
144
148
  * @param prk The secret to expand. It stays the caller's: this function does not wipe it.
145
149
  * @param label The label without its `"tls13 "` prefix, such as `"key"` or `"c hs traffic"`.
146
150
  * @param context The public context bytes, usually a transcript hash; empty for none.
147
151
  * @param length How many bytes to derive.
148
- * @returns A fresh `Secret` holding the `length` derived bytes, which the caller owns and wipes.
152
+ * @returns A fresh `Secret` holding the `length` derived bytes, which the caller owns and wipes; `null`
153
+ * for a label, context or length the `HkdfLabel` cannot carry.
149
154
  */
150
155
  export const tlsExpandLabel = (
151
156
  hashLength: i32,
@@ -153,7 +158,7 @@ export const tlsExpandLabel = (
153
158
  label: string,
154
159
  context: u8[],
155
160
  length: i32
156
- ): Secret<u8[]> => {
161
+ ): Secret<u8[]> | null => {
157
162
  if (hashLength === SHA384_SIZE) {
158
163
  return hkdfExpandLabelSha384Secret(prk, label, context, length)
159
164
  }
@@ -212,6 +217,13 @@ export const tlsEmptyHash = (hashLength: i32): u8[] => {
212
217
  */
213
218
  const tlsHashZeros = (hashLength: i32): u8[] => new Array<u8>(toU16(hashLength === 48 ? 48 : 32))
214
219
 
220
+ /** Whether `hashLength` is one the schedule runs, 32 or 48: an in-place step answers `false` for any other. */
221
+ const tlsHashKnown = (hashLength: i32): boolean => hashLength === SHA256_SIZE || hashLength === SHA384_SIZE
222
+
223
+ /** The `HkdfHash` a hash length names: SHA-384 for 48, SHA-256 for any other, as `tlsHashZeros` reads one. */
224
+ const tlsHkdfHash = (hashLength: i32): HkdfHash =>
225
+ hashLength === SHA384_SIZE ? HkdfHash.Sha384 : HkdfHash.Sha256
226
+
215
227
  /**
216
228
  * Derive-Secret (RFC 8446 §7.1): Expand-Label of `prk` with `label` and a transcript hash, HashLen bytes
217
229
  * held as a `Secret`.
@@ -220,14 +232,15 @@ const tlsHashZeros = (hashLength: i32): u8[] => new Array<u8>(toU16(hashLength =
220
232
  * @param prk The stage of the schedule to derive from. It stays the caller's: this function does not wipe it.
221
233
  * @param label The label without its `"tls13 "` prefix, such as `"s hs traffic"`.
222
234
  * @param transcriptHash The transcript hash the secret is bound to, all of it used as the context.
223
- * @returns A fresh `Secret` holding the HashLen derived bytes, which the caller owns and wipes.
235
+ * @returns A fresh `Secret` holding the HashLen derived bytes, which the caller owns and wipes; `null`
236
+ * where `tlsExpandLabel` answers it, for a label or transcript hash too long.
224
237
  */
225
238
  export const tlsDeriveSecret = (
226
239
  hashLength: i32,
227
240
  prk: Secret<u8[]>,
228
241
  label: string,
229
242
  transcriptHash: u8[]
230
- ): Secret<u8[]> => tlsExpandLabel(hashLength, prk, label, transcriptHash, hashLength)
243
+ ): Secret<u8[]> | null => tlsExpandLabel(hashLength, prk, label, transcriptHash, hashLength)
231
244
 
232
245
  /**
233
246
  * The early secret with no PSK: HKDF-Extract of HashLen zeros under a zero salt (RFC 8446 §7.1). The zero
@@ -266,9 +279,10 @@ export const tlsPskEarlySecret = (hashLength: i32, psk: Secret<u8[]>): Secret<u8
266
279
  *
267
280
  * @param hashLength The hash length, 32 or 48.
268
281
  * @param earlySecret The early secret from `tlsPskEarlySecret`. It stays the caller's: this does not wipe it.
269
- * @returns A fresh `Secret` holding the binder key, which the caller owns and wipes.
282
+ * @returns A fresh `Secret` holding the binder key, which the caller owns and wipes; `null` where
283
+ * `tlsDeriveSecret` answers it.
270
284
  */
271
- export const tlsBinderKey = (hashLength: i32, earlySecret: Secret<u8[]>): Secret<u8[]> =>
285
+ export const tlsBinderKey = (hashLength: i32, earlySecret: Secret<u8[]>): Secret<u8[]> | null =>
272
286
  tlsDeriveSecret(hashLength, earlySecret, "res binder", tlsEmptyHash(hashLength))
273
287
 
274
288
  /**
@@ -278,9 +292,10 @@ export const tlsBinderKey = (hashLength: i32, earlySecret: Secret<u8[]>): Secret
278
292
  *
279
293
  * @param hashLength The hash length, 32 or 48.
280
294
  * @param earlySecret The early secret. It stays the caller's: this function does not wipe it.
281
- * @returns A fresh `Secret` holding the salt, which the caller owns and wipes once the extract is done.
295
+ * @returns A fresh `Secret` holding the salt, which the caller owns and wipes once the extract is done;
296
+ * `null` where `tlsDeriveSecret` answers it.
282
297
  */
283
- export const tlsHandshakeSalt = (hashLength: i32, earlySecret: Secret<u8[]>): Secret<u8[]> =>
298
+ export const tlsHandshakeSalt = (hashLength: i32, earlySecret: Secret<u8[]>): Secret<u8[]> | null =>
284
299
  tlsDeriveSecret(hashLength, earlySecret, "derived", tlsEmptyHash(hashLength))
285
300
 
286
301
  /**
@@ -290,13 +305,15 @@ export const tlsHandshakeSalt = (hashLength: i32, earlySecret: Secret<u8[]>): Se
290
305
  * @param hashLength The hash length, 32 or 48, which is also how many bytes are derived.
291
306
  * @param resumptionMasterSecret The connection's resumption master secret. It stays the caller's to wipe.
292
307
  * @param ticketNonce The `ticket_nonce` of the NewSessionTicket, all of it used as the context. It is public.
293
- * @returns A fresh `Secret` holding the PSK, which the caller owns and wipes.
308
+ * @returns A fresh `Secret` holding the PSK, which the caller owns and wipes; `null` for a nonce longer
309
+ * than the 255 bytes an `HkdfLabel` context carries.
294
310
  */
295
311
  export const tlsResumptionPsk = (
296
312
  hashLength: i32,
297
313
  resumptionMasterSecret: Secret<u8[]>,
298
314
  ticketNonce: u8[]
299
- ): Secret<u8[]> => tlsExpandLabel(hashLength, resumptionMasterSecret, "resumption", ticketNonce, hashLength)
315
+ ): Secret<u8[]> | null =>
316
+ tlsExpandLabel(hashLength, resumptionMasterSecret, "resumption", ticketNonce, hashLength)
300
317
 
301
318
  /**
302
319
  * The handshake secret: HKDF-Extract of the ECDHE secret under
@@ -305,14 +322,23 @@ export const tlsResumptionPsk = (
305
322
  * @param hashLength The hash length, 32 or 48.
306
323
  * @param earlySecret The early secret. It stays the caller's: this function does not wipe it.
307
324
  * @param ecdhe The shared secret of the key exchange. It stays the caller's to wipe as well.
308
- * @returns A fresh `Secret` holding the handshake secret, which the caller owns and wipes.
325
+ * @returns A fresh `Secret` holding the handshake secret, which the caller owns and wipes; `null` where
326
+ * `tlsDeriveSecret` answers it for the salt, with nothing left to wipe.
309
327
  */
310
328
  export const tlsHandshakeSecret = (
311
329
  hashLength: i32,
312
330
  earlySecret: Secret<u8[]>,
313
331
  ecdhe: Secret<u8[]>
314
- ): Secret<u8[]> => {
315
- const derived: Secret<u8[]> = tlsDeriveSecret(hashLength, earlySecret, "derived", tlsEmptyHash(hashLength))
332
+ ): Secret<u8[]> | null => {
333
+ const derived: Secret<u8[]> | null = tlsDeriveSecret(
334
+ hashLength,
335
+ earlySecret,
336
+ "derived",
337
+ tlsEmptyHash(hashLength)
338
+ )
339
+ if (derived === null) {
340
+ return null
341
+ }
316
342
  const handshake: Secret<u8[]> = tlsExtract(hashLength, derived, ecdhe)
317
343
  wipe(derived)
318
344
  return handshake
@@ -325,15 +351,19 @@ export const tlsHandshakeSecret = (
325
351
  *
326
352
  * @param hashLength The hash length, 32 or 48.
327
353
  * @param handshakeSecret The handshake secret. It stays the caller's: this function does not wipe it.
328
- * @returns A fresh `Secret` holding the master secret, which the caller owns and wipes.
354
+ * @returns A fresh `Secret` holding the master secret, which the caller owns and wipes; `null` where
355
+ * `tlsDeriveSecret` answers it for the salt, with nothing left to wipe.
329
356
  */
330
- export const tlsMasterSecret = (hashLength: i32, handshakeSecret: Secret<u8[]>): Secret<u8[]> => {
331
- const derived: Secret<u8[]> = tlsDeriveSecret(
357
+ export const tlsMasterSecret = (hashLength: i32, handshakeSecret: Secret<u8[]>): Secret<u8[]> | null => {
358
+ const derived: Secret<u8[]> | null = tlsDeriveSecret(
332
359
  hashLength,
333
360
  handshakeSecret,
334
361
  "derived",
335
362
  tlsEmptyHash(hashLength)
336
363
  )
364
+ if (derived === null) {
365
+ return null
366
+ }
337
367
  const zeros: Secret<u8[]> = secret(tlsHashZeros(hashLength))
338
368
  const master: Secret<u8[]> = tlsExtract(hashLength, derived, zeros)
339
369
  wipe(derived)
@@ -351,11 +381,19 @@ export const tlsMasterSecret = (hashLength: i32, handshakeSecret: Secret<u8[]>):
351
381
  * @param hashLength The hash length, 32 or 48.
352
382
  * @param baseKey The sender's handshake traffic secret. It stays the caller's: this does not wipe it.
353
383
  * @param transcriptHash The transcript hash up to, and not including, the Finished message.
354
- * @returns The HashLen bytes of `verify_data`, plain, in a fresh array.
384
+ * @returns The HashLen bytes of `verify_data`, plain, in a fresh array; `null` where `tlsExpandLabel`
385
+ * answers it for the finished key, with nothing left to wipe.
355
386
  */
356
- export const tlsFinishedVerifyData = (hashLength: i32, baseKey: Secret<u8[]>, transcriptHash: u8[]): u8[] => {
387
+ export const tlsFinishedVerifyData = (
388
+ hashLength: i32,
389
+ baseKey: Secret<u8[]>,
390
+ transcriptHash: u8[]
391
+ ): u8[] | null => {
357
392
  const none: u8[] = []
358
- const finishedKey: Secret<u8[]> = tlsExpandLabel(hashLength, baseKey, "finished", none, hashLength)
393
+ const finishedKey: Secret<u8[]> | null = tlsExpandLabel(hashLength, baseKey, "finished", none, hashLength)
394
+ if (finishedKey === null) {
395
+ return null
396
+ }
359
397
  const verifyData: u8[] =
360
398
  hashLength === SHA384_SIZE
361
399
  ? hmacSha384Secret(finishedKey, transcriptHash)
@@ -371,9 +409,10 @@ export const tlsFinishedVerifyData = (hashLength: i32, baseKey: Secret<u8[]>, tr
371
409
  * @param suite The negotiated cipher suite, such as `TLS_AES_128_GCM_SHA256`.
372
410
  * @param trafficSecret The traffic secret the key comes from. It stays the caller's to wipe.
373
411
  * @returns A fresh `Secret` holding the suite's key (16 or 32 bytes), or an empty one for an unknown suite.
374
- * The caller owns and wipes it.
412
+ * The caller owns and wipes it. `null` is kept for `tlsExpandLabel`'s refusal, which a known suite's
413
+ * fixed label and length never draw.
375
414
  */
376
- export const tlsTrafficKey = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> => {
415
+ export const tlsTrafficKey = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> | null => {
377
416
  const none: u8[] = []
378
417
  const hashLength: i32 = tlsSuiteHashLength(suite)
379
418
  if (hashLength === 0) {
@@ -389,9 +428,9 @@ export const tlsTrafficKey = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u
389
428
  * @param suite The negotiated cipher suite, such as `TLS_AES_128_GCM_SHA256`.
390
429
  * @param trafficSecret The traffic secret the IV comes from. It stays the caller's to wipe.
391
430
  * @returns A fresh `Secret` holding the 12-byte IV, or an empty one for an unknown suite. The caller owns
392
- * and wipes it.
431
+ * and wipes it. `null` is kept for `tlsExpandLabel`'s refusal, which a known suite never draws.
393
432
  */
394
- export const tlsTrafficIv = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> => {
433
+ export const tlsTrafficIv = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> | null => {
395
434
  const none: u8[] = []
396
435
  const hashLength: i32 = tlsSuiteHashLength(suite)
397
436
  if (hashLength === 0) {
@@ -417,8 +456,6 @@ export const tlsTrafficIv = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u8
417
456
  * connection with `reset` allocates nothing that outlives a call.
418
457
  */
419
458
  export class TlsTranscript {
420
- /** 32 for SHA-256, 48 for SHA-384. */
421
- private hashLength: i32
422
459
  private sha256: Sha256
423
460
  private sha384: Sha384
424
461
  /** Never fed: the initial hash values `reset` copies from. */
@@ -427,6 +464,10 @@ export class TlsTranscript {
427
464
  /** Where `hashInto` finishes a copy of the running hash, so that the transcript goes on. */
428
465
  private twin256: Sha256
429
466
  private twin384: Sha384
467
+ /** 32 for SHA-256, 48 for SHA-384. */
468
+ private hashLength: i32
469
+ /** Set when an `update` was refused a window; `reset` clears it. */
470
+ private wasRefused: boolean
430
471
 
431
472
  /**
432
473
  * Makes an empty transcript and all six of its hashers, once, so that `reset` can reuse them.
@@ -442,6 +483,7 @@ export class TlsTranscript {
442
483
  this.fresh384 = new Sha384()
443
484
  this.twin256 = new Sha256()
444
485
  this.twin384 = new Sha384()
486
+ this.wasRefused = false
445
487
  }
446
488
 
447
489
  /**
@@ -451,25 +493,68 @@ export class TlsTranscript {
451
493
  */
452
494
  reset(hashLength: i32): void {
453
495
  this.hashLength = hashLength
496
+ this.wasRefused = false
454
497
  hmacCopySha256(this.fresh256, this.sha256)
455
498
  hmacCopySha384(this.fresh384, this.sha384)
456
499
  }
457
500
 
458
501
  /**
459
502
  * Absorbs `data[off .. off + len)`, a whole handshake message or part of one.
503
+ * A window outside `data` absorbs nothing and marks the transcript
504
+ * `refused`, so that the handshake fails closed at its Finished check.
460
505
  *
461
506
  * @param data The array that holds the message bytes.
462
507
  * @param off Where in `data` the bytes to absorb start.
463
508
  * @param len How many bytes, from `off`, to absorb.
464
509
  */
465
510
  update(data: u8[], off: i32, len: i32): void {
466
- if (this.hashLength === SHA384_SIZE) {
467
- this.sha384.update(data, off, len)
468
- } else {
469
- this.sha256.update(data, off, len)
511
+ const fed: boolean =
512
+ this.hashLength === SHA384_SIZE
513
+ ? this.sha384.update(data, off, len)
514
+ : this.sha256.update(data, off, len)
515
+ if (!fed) {
516
+ this.wasRefused = true
470
517
  }
471
518
  }
472
519
 
520
+ /**
521
+ * Marks the transcript refused, as a refused `update` does: for a caller
522
+ * whose own hashing of the transcript hash was refused a window.
523
+ *
524
+ * @internal Read by `net/tls.ts`.
525
+ */
526
+ refuse(): void {
527
+ this.wasRefused = true
528
+ }
529
+
530
+ /**
531
+ * Whether an `update` since the last `reset` was refused a window, so the
532
+ * hash no longer covers the whole transcript.
533
+ *
534
+ * @internal Read by `net/tls.ts` and `net/tls/client.ts`.
535
+ * @returns `true` once a window was refused; `false` otherwise.
536
+ */
537
+ refused(): boolean {
538
+ return this.wasRefused
539
+ }
540
+
541
+ /**
542
+ * `update` for a caller that handles a bad window: a window outside `data`
543
+ * absorbs nothing and answers `false`, as `Sha256.tryUpdate` does.
544
+ *
545
+ * @internal Read by `net/tls/client.ts`.
546
+ * @param data The array that holds the message bytes.
547
+ * @param off Where in `data` the bytes to absorb start.
548
+ * @param len How many bytes, from `off`, to absorb.
549
+ * @returns `true` once the bytes are absorbed; `false`, with nothing
550
+ * absorbed, for a window outside `data`.
551
+ */
552
+ tryUpdate(data: u8[], off: i32, len: i32): boolean {
553
+ return this.hashLength === SHA384_SIZE
554
+ ? this.sha384.tryUpdate(data, off, len)
555
+ : this.sha256.tryUpdate(data, off, len)
556
+ }
557
+
473
558
  /**
474
559
  * Writes the transcript hash of everything absorbed so far, `hashLength`
475
560
  * bytes, at `out[at]`; the transcript goes on. What the digest allocates
@@ -515,17 +600,43 @@ export class TlsTranscript {
515
600
  * Replaces the transcript so far, which must be exactly the first
516
601
  * ClientHello, by `message_hash` (type 254, a 24-bit length of HashLen,
517
602
  * then Hash(ClientHello1)), as RFC 8446 §4.4.1 has a server do before it
518
- * absorbs its HelloRetryRequest.
603
+ * absorbs its HelloRetryRequest. A refusal marked while ClientHello1 was
604
+ * absorbed stays marked, so the Finished check still fails closed.
519
605
  */
520
606
  restartWithMessageHash(): void {
521
607
  const first: u8[] = new Array<u8>(SHA384_SIZE)
522
608
  this.hashInto(first, TLS_SCHEDULE_FROM)
523
609
  const header: u8[] = [toU8(254), toU8(0), toU8(0), toU8(this.hashLength)]
524
610
  const headerLength: i32 = 4
611
+ const wasRefused: boolean = this.wasRefused
525
612
  this.reset(this.hashLength)
613
+ this.wasRefused = wasRefused
526
614
  this.update(header, TLS_SCHEDULE_FROM, headerLength)
527
615
  this.update(first, TLS_SCHEDULE_FROM, this.hashLength)
528
616
  }
617
+
618
+ /**
619
+ * `restartWithMessageHash` for a caller that handles a failure: a
620
+ * transcript whose `hashLength` names neither hash answers `false`.
621
+ *
622
+ * @internal Read by `net/tls/client.ts`.
623
+ * @returns `true` once the transcript holds `message_hash`; `false` when
624
+ * `hashLength` is neither 32 nor 48, with the transcript started again empty.
625
+ * A refusal marked before the restart stays marked either way.
626
+ */
627
+ tryRestartWithMessageHash(): boolean {
628
+ const first: u8[] = new Array<u8>(SHA384_SIZE)
629
+ this.hashInto(first, TLS_SCHEDULE_FROM)
630
+ const header: u8[] = [toU8(254), toU8(0), toU8(0), toU8(this.hashLength)]
631
+ const headerLength: i32 = 4
632
+ const wasRefused: boolean = this.wasRefused
633
+ this.reset(this.hashLength)
634
+ this.wasRefused = wasRefused
635
+ if (!this.tryUpdate(header, TLS_SCHEDULE_FROM, headerLength)) {
636
+ return false
637
+ }
638
+ return this.tryUpdate(first, TLS_SCHEDULE_FROM, this.hashLength)
639
+ }
529
640
  }
530
641
 
531
642
  // ---- The schedule in place -------------------------------------------------------
@@ -549,8 +660,9 @@ const tlsEmptyHashInto = (hashLength: i32, out: u8[]): void => {
549
660
  * `tlsDeriveSecret` in place: Derive-Secret (RFC 8446 §7.1) of `prk` with
550
661
  * `label` and the transcript hash `transcriptHash[hashOff .. hashOff +
551
662
  * hashLength)`, its HashLen bytes written at `out[0]`. The scratch is wiped
552
- * before it returns. It panics as `hkdfExpandLabelInto` does, a window outside
553
- * its array included.
663
+ * before it returns. `false` for a hash length that is not 32 or 48, and
664
+ * where `hkdfExpandLabelInto` answers it: a label too long, or a window
665
+ * outside its array.
554
666
  *
555
667
  * @param kdf The caller's HKDF scratch, which this computes in and wipes.
556
668
  * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
@@ -559,6 +671,7 @@ const tlsEmptyHashInto = (hashLength: i32, out: u8[]): void => {
559
671
  * @param transcriptHash The array that holds the transcript hash.
560
672
  * @param hashOff Where in `transcriptHash` the `hashLength` bytes of the hash start.
561
673
  * @param out Where the derived secret is written, from index 0; the caller owns and wipes it.
674
+ * @returns `true` once the secret is written; `false` for a bad hash length, label or window.
562
675
  */
563
676
  export const tlsDeriveSecretInto = (
564
677
  kdf: HkdfScratch,
@@ -568,10 +681,13 @@ export const tlsDeriveSecretInto = (
568
681
  transcriptHash: u8[],
569
682
  hashOff: i32,
570
683
  out: u8[]
571
- ): void => {
572
- hkdfExpandLabelInto(
684
+ ): boolean => {
685
+ if (!tlsHashKnown(hashLength)) {
686
+ return false
687
+ }
688
+ return hkdfExpandLabelInto(
573
689
  kdf,
574
- hashLength,
690
+ tlsHkdfHash(hashLength),
575
691
  prk,
576
692
  label,
577
693
  transcriptHash,
@@ -592,19 +708,23 @@ export const tlsDeriveSecretInto = (
592
708
  * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
593
709
  * @param handshakeSecret The handshake secret, plain. It stays the caller's to wipe.
594
710
  * @param out Where the master secret is written, from index 0; the caller owns and wipes it.
711
+ * @returns `true` once the master secret is written; `false` for a hash length that is not 32 or 48, or an
712
+ * `out` shorter than HashLen, with the salt wiped all the same.
595
713
  */
596
714
  export const tlsMasterSecretInto = (
597
715
  kdf: HkdfScratch,
598
716
  hashLength: i32,
599
717
  handshakeSecret: u8[],
600
718
  out: u8[]
601
- ): void => {
719
+ ): boolean => {
602
720
  const empty: u8[] = tlsHashZeros(hashLength)
603
721
  tlsEmptyHashInto(hashLength, empty)
604
722
  const salt: u8[] = tlsHashZeros(hashLength)
605
- tlsDeriveSecretInto(kdf, hashLength, handshakeSecret, "derived", empty, TLS_SCHEDULE_FROM, salt)
606
- hkdfExtractInto(kdf, hashLength, salt, tlsHashZeros(hashLength), out, TLS_SCHEDULE_FROM)
723
+ const extracted: boolean =
724
+ tlsDeriveSecretInto(kdf, hashLength, handshakeSecret, "derived", empty, TLS_SCHEDULE_FROM, salt) &&
725
+ hkdfExtractInto(kdf, tlsHkdfHash(hashLength), salt, tlsHashZeros(hashLength), out, TLS_SCHEDULE_FROM)
607
726
  secureZero(salt)
727
+ return extracted
608
728
  }
609
729
 
610
730
  /**
@@ -620,6 +740,10 @@ export const tlsMasterSecretInto = (
620
740
  * @param hashOff Where in `transcriptHash` the `hashLength` bytes of the hash start.
621
741
  * @param out Where `verify_data` is written. It goes on the wire, so it is plain.
622
742
  * @param at Where in `out` the HashLen bytes of `verify_data` start.
743
+ * @returns `true` once `verify_data` is written; `false` for a hash length
744
+ * that is not 32 or 48, or when the `transcriptHash` window or the `out`
745
+ * window lies outside its array, and what `out` holds is not a
746
+ * `verify_data` to send or accept.
623
747
  */
624
748
  export const tlsFinishedVerifyDataInto = (
625
749
  kdf: HkdfScratch,
@@ -629,12 +753,15 @@ export const tlsFinishedVerifyDataInto = (
629
753
  hashOff: i32,
630
754
  out: u8[],
631
755
  at: i32
632
- ): void => {
756
+ ): boolean => {
757
+ if (!tlsHashKnown(hashLength)) {
758
+ return false
759
+ }
633
760
  const none: u8[] = []
634
761
  const finishedKey: u8[] = tlsHashZeros(hashLength)
635
- hkdfExpandLabelInto(
762
+ const keyed: boolean = hkdfExpandLabelInto(
636
763
  kdf,
637
- hashLength,
764
+ tlsHkdfHash(hashLength),
638
765
  baseKey,
639
766
  "finished",
640
767
  none,
@@ -644,17 +771,21 @@ export const tlsFinishedVerifyDataInto = (
644
771
  TLS_SCHEDULE_FROM,
645
772
  hashLength
646
773
  )
647
- if (hashLength === SHA384_SIZE) {
648
- kdf.sha384.begin(finishedKey, TLS_SCHEDULE_FROM, hashLength)
649
- kdf.sha384.update(transcriptHash, hashOff, hashLength)
650
- kdf.sha384.finishInto(out, at)
651
- } else {
652
- kdf.sha256.begin(finishedKey, TLS_SCHEDULE_FROM, hashLength)
653
- kdf.sha256.update(transcriptHash, hashOff, hashLength)
654
- kdf.sha256.finishInto(out, at)
774
+ let fed: boolean = false
775
+ if (keyed && hashLength === SHA384_SIZE) {
776
+ fed =
777
+ kdf.sha384.begin(finishedKey, TLS_SCHEDULE_FROM, hashLength) &&
778
+ kdf.sha384.update(transcriptHash, hashOff, hashLength) &&
779
+ kdf.sha384.finishInto(out, at)
780
+ } else if (keyed) {
781
+ fed =
782
+ kdf.sha256.begin(finishedKey, TLS_SCHEDULE_FROM, hashLength) &&
783
+ kdf.sha256.update(transcriptHash, hashOff, hashLength) &&
784
+ kdf.sha256.finishInto(out, at)
655
785
  }
656
786
  secureZero(finishedKey)
657
787
  kdf.wipe()
788
+ return fed
658
789
  }
659
790
 
660
791
  /**
@@ -666,10 +797,12 @@ export const tlsFinishedVerifyDataInto = (
666
797
  * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
667
798
  * @param psk The pre-shared key, plain. It stays the caller's to wipe.
668
799
  * @param out Where the early secret is written, from index 0; the caller owns and wipes it.
800
+ * @returns `true` once the early secret is written; `false` for a hash length that is not 32 or 48, or an
801
+ * `out` shorter than HashLen.
669
802
  */
670
- export const tlsPskEarlySecretInto = (kdf: HkdfScratch, hashLength: i32, psk: u8[], out: u8[]): void => {
671
- hkdfExtractInto(kdf, hashLength, tlsHashZeros(hashLength), psk, out, TLS_SCHEDULE_FROM)
672
- }
803
+ export const tlsPskEarlySecretInto = (kdf: HkdfScratch, hashLength: i32, psk: u8[], out: u8[]): boolean =>
804
+ tlsHashKnown(hashLength) &&
805
+ hkdfExtractInto(kdf, tlsHkdfHash(hashLength), tlsHashZeros(hashLength), psk, out, TLS_SCHEDULE_FROM)
673
806
 
674
807
  /**
675
808
  * `tlsBinderKey` in place: `Derive-Secret(early, "res binder", "")`, written at `out[0]`. The scratch is
@@ -679,11 +812,17 @@ export const tlsPskEarlySecretInto = (kdf: HkdfScratch, hashLength: i32, psk: u8
679
812
  * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
680
813
  * @param earlySecret The early secret, plain. It stays the caller's to wipe.
681
814
  * @param out Where the binder key is written, from index 0; the caller owns and wipes it.
815
+ * @returns `true` once the binder key is written; `false` where `tlsDeriveSecretInto` answers it.
682
816
  */
683
- export const tlsBinderKeyInto = (kdf: HkdfScratch, hashLength: i32, earlySecret: u8[], out: u8[]): void => {
817
+ export const tlsBinderKeyInto = (
818
+ kdf: HkdfScratch,
819
+ hashLength: i32,
820
+ earlySecret: u8[],
821
+ out: u8[]
822
+ ): boolean => {
684
823
  const empty: u8[] = tlsHashZeros(hashLength)
685
824
  tlsEmptyHashInto(hashLength, empty)
686
- tlsDeriveSecretInto(kdf, hashLength, earlySecret, "res binder", empty, TLS_SCHEDULE_FROM, out)
825
+ return tlsDeriveSecretInto(kdf, hashLength, earlySecret, "res binder", empty, TLS_SCHEDULE_FROM, out)
687
826
  }
688
827
 
689
828
  /**
@@ -696,24 +835,26 @@ export const tlsBinderKeyInto = (kdf: HkdfScratch, hashLength: i32, earlySecret:
696
835
  * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
697
836
  * @param earlySecret The early secret, plain. It stays the caller's to wipe.
698
837
  * @param out Where the salt is written, from index 0.
838
+ * @returns `true` once the salt is written; `false` where `tlsDeriveSecretInto` answers it.
699
839
  */
700
840
  export const tlsHandshakeSaltInto = (
701
841
  kdf: HkdfScratch,
702
842
  hashLength: i32,
703
843
  earlySecret: u8[],
704
844
  out: u8[]
705
- ): void => {
845
+ ): boolean => {
706
846
  const empty: u8[] = tlsHashZeros(hashLength)
707
847
  tlsEmptyHashInto(hashLength, empty)
708
- tlsDeriveSecretInto(kdf, hashLength, earlySecret, "derived", empty, TLS_SCHEDULE_FROM, out)
848
+ return tlsDeriveSecretInto(kdf, hashLength, earlySecret, "derived", empty, TLS_SCHEDULE_FROM, out)
709
849
  }
710
850
 
711
851
  /**
712
852
  * `tlsResumptionPsk` in place: `HKDF-Expand-Label(resumption_master_secret,
713
853
  * "resumption", ticketNonce[nonceOff .. nonceOff + nonceLength), HashLen)`
714
854
  * (RFC 8446 §4.6.1), written at `out[0]`. The scratch is wiped before it
715
- * returns. A nonce window outside `ticketNonce`, or longer than 255 bytes,
716
- * panics, as `hkdfExpandLabelInto` does.
855
+ * returns. A hash length that is not 32 or 48, a nonce window outside
856
+ * `ticketNonce`, or one longer than 255 bytes answers `false`, as
857
+ * `hkdfExpandLabelInto` does.
717
858
  *
718
859
  * @param kdf The caller's HKDF scratch, which this computes in and wipes.
719
860
  * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
@@ -722,6 +863,7 @@ export const tlsHandshakeSaltInto = (
722
863
  * @param nonceOff Where in `ticketNonce` the nonce starts.
723
864
  * @param nonceLength How many bytes the nonce is.
724
865
  * @param out Where the PSK is written, from index 0; the caller owns and wipes it.
866
+ * @returns `true` once the PSK is written; `false` for a bad hash length or nonce window.
725
867
  */
726
868
  export const tlsResumptionPskInto = (
727
869
  kdf: HkdfScratch,
@@ -731,10 +873,13 @@ export const tlsResumptionPskInto = (
731
873
  nonceOff: i32,
732
874
  nonceLength: i32,
733
875
  out: u8[]
734
- ): void => {
735
- hkdfExpandLabelInto(
876
+ ): boolean => {
877
+ if (!tlsHashKnown(hashLength)) {
878
+ return false
879
+ }
880
+ return hkdfExpandLabelInto(
736
881
  kdf,
737
- hashLength,
882
+ tlsHkdfHash(hashLength),
738
883
  resumptionMasterSecret,
739
884
  "resumption",
740
885
  ticketNonce,
@@ -782,8 +927,17 @@ export const tlsBinderVerify = (
782
927
  return false
783
928
  }
784
929
  const expected: u8[] = tlsHashZeros(hashLength)
785
- tlsFinishedVerifyDataInto(kdf, hashLength, binderKey, truncatedHash, hashOff, expected, TLS_SCHEDULE_FROM)
786
- const verified: boolean = timingSafeEqualAt(binder, binderOff, expected, TLS_SCHEDULE_FROM, hashLength)
930
+ const computed: boolean = tlsFinishedVerifyDataInto(
931
+ kdf,
932
+ hashLength,
933
+ binderKey,
934
+ truncatedHash,
935
+ hashOff,
936
+ expected,
937
+ TLS_SCHEDULE_FROM
938
+ )
939
+ const verified: boolean =
940
+ timingSafeEqualAt(binder, binderOff, expected, TLS_SCHEDULE_FROM, hashLength) && computed
787
941
  secureZero(expected)
788
942
  return verified
789
943
  }
@@ -801,7 +955,7 @@ export const tlsBinderVerify = (
801
955
  * @param trafficSecret The traffic secret, plain, of the suite's hash length. It stays the caller's to wipe.
802
956
  * @param key Where the AEAD key is written: an array of exactly the suite's key length (16 or 32 bytes).
803
957
  * @param iv Where the IV is written: an array of exactly 12 bytes.
804
- * @returns `true` when the key and IV were written; `false`, with nothing written, for a bad suite or length.
958
+ * @returns `true` when the key and IV were written; `false`, with nothing written, for a bad suite or length, or, the key possibly written, if the IV's derive refuses (none in range does).
805
959
  */
806
960
  export const tlsTrafficKeysInto = (
807
961
  kdf: HkdfScratch,
@@ -821,31 +975,33 @@ export const tlsTrafficKeysInto = (
821
975
  ) {
822
976
  return false
823
977
  }
824
- hkdfExpandLabelInto(
825
- kdf,
826
- hashLength,
827
- trafficSecret,
828
- "key",
829
- none,
830
- TLS_SCHEDULE_FROM,
831
- TLS_SCHEDULE_FROM,
832
- key,
833
- TLS_SCHEDULE_FROM,
834
- keyLength
978
+ // The tests above keep both expansions in range, so neither answers `false`.
979
+ return (
980
+ tryHkdfExpandLabelInto(
981
+ kdf,
982
+ tlsHkdfHash(hashLength),
983
+ trafficSecret,
984
+ "key",
985
+ none,
986
+ TLS_SCHEDULE_FROM,
987
+ TLS_SCHEDULE_FROM,
988
+ key,
989
+ TLS_SCHEDULE_FROM,
990
+ keyLength
991
+ ) &&
992
+ tryHkdfExpandLabelInto(
993
+ kdf,
994
+ tlsHkdfHash(hashLength),
995
+ trafficSecret,
996
+ "iv",
997
+ none,
998
+ TLS_SCHEDULE_FROM,
999
+ TLS_SCHEDULE_FROM,
1000
+ iv,
1001
+ TLS_SCHEDULE_FROM,
1002
+ TLS_IV_SIZE
1003
+ )
835
1004
  )
836
- hkdfExpandLabelInto(
837
- kdf,
838
- hashLength,
839
- trafficSecret,
840
- "iv",
841
- none,
842
- TLS_SCHEDULE_FROM,
843
- TLS_SCHEDULE_FROM,
844
- iv,
845
- TLS_SCHEDULE_FROM,
846
- TLS_IV_SIZE
847
- )
848
- return true
849
1005
  }
850
1006
 
851
1007
  // ---- The schedule into held `Secret`s --------------------------------------
@@ -862,31 +1018,21 @@ const tlsHeldCopy = (held: u8[], from: u8[]): void => {
862
1018
 
863
1019
  /**
864
1020
  * `transcriptHash[hashOff .. hashOff + hashLength)` in a fresh array: the
865
- * context a held form hands HKDF. Panics naming `caller` when the window is
866
- * not inside `transcriptHash`, as `tlsHeldCheck` does for a wrong hash length:
867
- * a shorter context would derive another secret without a word.
1021
+ * context a held form hands HKDF, or `null` when the window is not inside
1022
+ * `transcriptHash`: a shorter context would derive another secret without a
1023
+ * word.
868
1024
  */
869
- const tlsHeldWindow = (caller: string, hashLength: i32, transcriptHash: u8[], hashOff: i32): u8[] => {
1025
+ const tlsHeldWindow = (hashLength: i32, transcriptHash: u8[], hashOff: i32): u8[] | null => {
870
1026
  const window: u8[] = tlsCopyWindow(transcriptHash, hashOff, hashLength)
871
- if (toI32(window.length) !== hashLength) {
872
- panic(`${caller}: a transcript hash at ${hashOff} outside its ${transcriptHash.length} bytes`)
873
- }
874
- return window
1027
+ return toI32(window.length) === hashLength ? window : null
875
1028
  }
876
1029
 
877
1030
  /**
878
- * Panics naming `caller` unless `hashLength` is 32 or 48 and `held` holds
879
- * `hashLength` bytes, as every secret of the schedule does.
1031
+ * Whether `hashLength` is 32 or 48 and `held` holds `hashLength` bytes, as
1032
+ * every secret of the schedule does.
880
1033
  */
881
- const tlsHeldCheck = (caller: string, hashLength: i32, held: Secret<u8[]>): void => {
882
- if (hashLength !== SHA256_SIZE && hashLength !== SHA384_SIZE) {
883
- panic(`${caller}: a hash length of ${hashLength}, not 32 or 48`)
884
- }
885
- const length: i32 = expose(held, tlsHeldLength)
886
- if (length !== hashLength) {
887
- panic(`${caller}: a secret of ${length} bytes, not ${hashLength}`)
888
- }
889
- }
1034
+ const tlsHeldCheck = (hashLength: i32, held: Secret<u8[]>): boolean =>
1035
+ (hashLength === SHA256_SIZE || hashLength === SHA384_SIZE) && expose(held, tlsHeldLength) === hashLength
890
1036
 
891
1037
  /** HKDF-Extract of `ikm` under `salt`, both held, written over `dst`'s bytes; the plain PRK is wiped. */
892
1038
  const tlsExtractHeld = (hashLength: i32, salt: Secret<u8[]>, ikm: Secret<u8[]>, dst: Secret<u8[]>): void => {
@@ -902,8 +1048,9 @@ const tlsExtractHeld = (hashLength: i32, salt: Secret<u8[]>, ikm: Secret<u8[]>,
902
1048
  * `tlsDeriveSecret` into a held `Secret`: Derive-Secret (RFC 8446 §7.1) of
903
1049
  * `prk` with `label` and the transcript hash `transcriptHash[hashOff ..
904
1050
  * hashOff + hashLength)`, written over the HashLen bytes `dst` holds. `dst`
905
- * may be `prk`. Panics unless `hashLength` is 32 or 48, both hold HashLen
906
- * bytes and the transcript hash's window is inside `transcriptHash`.
1051
+ * may be `prk`. `false`, with `dst` untouched, unless `hashLength` is 32 or
1052
+ * 48, both hold HashLen bytes and the transcript hash's window is inside
1053
+ * `transcriptHash`.
907
1054
  *
908
1055
  * @param hashLength The hash length, 32 or 48.
909
1056
  * @param prk The held stage of the schedule to derive from. It is only overwritten when it is also `dst`.
@@ -911,6 +1058,7 @@ const tlsExtractHeld = (hashLength: i32, salt: Secret<u8[]>, ikm: Secret<u8[]>,
911
1058
  * @param transcriptHash The array that holds the transcript hash, which is public.
912
1059
  * @param hashOff Where in `transcriptHash` the `hashLength` bytes of the hash start.
913
1060
  * @param dst The held secret whose bytes the result overwrites. The caller still owns and wipes it.
1061
+ * @returns `true` once `dst` holds the secret; `false`, with `dst` untouched, for a bad length or window.
914
1062
  */
915
1063
  export const tlsDeriveSecretHeld = (
916
1064
  hashLength: i32,
@@ -919,64 +1067,79 @@ export const tlsDeriveSecretHeld = (
919
1067
  transcriptHash: u8[],
920
1068
  hashOff: i32,
921
1069
  dst: Secret<u8[]>
922
- ): void => {
923
- tlsHeldCheck("tlsDeriveSecretHeld", hashLength, prk)
924
- tlsHeldCheck("tlsDeriveSecretHeld", hashLength, dst)
925
- const context: u8[] = tlsHeldWindow("tlsDeriveSecretHeld", hashLength, transcriptHash, hashOff)
926
- if (hashLength === SHA384_SIZE) {
927
- hkdfExpandLabelSha384Held(dst, prk, label, context)
928
- } else {
929
- hkdfExpandLabelSha256Held(dst, prk, label, context)
1070
+ ): boolean => {
1071
+ const context: u8[] | null = tlsHeldWindow(hashLength, transcriptHash, hashOff)
1072
+ if (context === null || !tlsHeldCheck(hashLength, prk) || !tlsHeldCheck(hashLength, dst)) {
1073
+ return false
930
1074
  }
1075
+ return hashLength === SHA384_SIZE
1076
+ ? tryHkdfExpandLabelSha384Held(dst, prk, label, context)
1077
+ : tryHkdfExpandLabelSha256Held(dst, prk, label, context)
931
1078
  }
932
1079
 
933
1080
  /**
934
1081
  * `tlsHandshakeSecret` into a held `Secret`: HKDF-Extract of `ecdhe` under
935
1082
  * `Derive-Secret(early, "derived", "")`, written over `dst`'s HashLen bytes.
936
- * The salt is wiped before it returns; `dst` may be `earlySecret`. Panics
937
- * unless `hashLength` is 32 or 48 and `earlySecret` and `dst` hold HashLen
938
- * bytes.
1083
+ * The salt is wiped before it returns; `dst` may be `earlySecret`. `false`,
1084
+ * with `dst` untouched, unless `hashLength` is 32 or 48 and `earlySecret` and
1085
+ * `dst` hold HashLen bytes.
939
1086
  *
940
1087
  * @param hashLength The hash length, 32 or 48.
941
1088
  * @param earlySecret The held early secret. It is only overwritten when it is also `dst`.
942
1089
  * @param ecdhe The held shared secret of the key exchange. It stays the caller's to wipe.
943
1090
  * @param dst The held secret whose bytes the handshake secret overwrites. The caller still owns and wipes it.
1091
+ * @returns `true` once `dst` holds the secret; `false`, with `dst` untouched, for a bad length.
944
1092
  */
945
1093
  export const tlsHandshakeSecretHeld = (
946
1094
  hashLength: i32,
947
1095
  earlySecret: Secret<u8[]>,
948
1096
  ecdhe: Secret<u8[]>,
949
1097
  dst: Secret<u8[]>
950
- ): void => {
951
- tlsHeldCheck("tlsHandshakeSecretHeld", hashLength, dst)
1098
+ ): boolean => {
1099
+ if (!tlsHeldCheck(hashLength, dst)) {
1100
+ return false
1101
+ }
952
1102
  // Sized by the empty hash, not by a second test of `hashLength`, which `-O2`
953
1103
  // would thread into a copy of the derivation (and its wipes) for each size.
954
1104
  const empty: u8[] = tlsEmptyHash(hashLength)
955
1105
  const derived: Secret<u8[]> = secret(new Array<u8>(toU16(empty.length)))
956
- tlsDeriveSecretHeld(hashLength, earlySecret, "derived", empty, TLS_SCHEDULE_FROM, derived)
957
- tlsExtractHeld(hashLength, derived, ecdhe, dst)
1106
+ const salted: boolean = tlsDeriveSecretHeld(
1107
+ hashLength,
1108
+ earlySecret,
1109
+ "derived",
1110
+ empty,
1111
+ TLS_SCHEDULE_FROM,
1112
+ derived
1113
+ )
1114
+ if (salted) {
1115
+ tlsExtractHeld(hashLength, derived, ecdhe, dst)
1116
+ }
958
1117
  wipe(derived)
1118
+ return salted
959
1119
  }
960
1120
 
961
1121
  /**
962
1122
  * `tlsMasterSecret` into a held `Secret`: HKDF-Extract of HashLen zeros under
963
1123
  * `Derive-Secret(handshake, "derived", "")`, written over `dst`'s HashLen
964
1124
  * bytes. The salt and the zeros are wiped before it returns; `dst` may be
965
- * `handshakeSecret`. Panics unless `hashLength` is 32 or 48 and both hold
966
- * HashLen bytes.
1125
+ * `handshakeSecret`. `false`, with `dst` untouched, unless `hashLength` is 32
1126
+ * or 48 and both hold HashLen bytes.
967
1127
  *
968
1128
  * @param hashLength The hash length, 32 or 48.
969
1129
  * @param handshakeSecret The held handshake secret. It is only overwritten when it is also `dst`.
970
1130
  * @param dst The held secret whose bytes the master secret overwrites. The caller still owns and wipes it.
1131
+ * @returns `true` once `dst` holds the secret; `false`, with `dst` untouched, for a bad length.
971
1132
  */
972
1133
  export const tlsMasterSecretHeld = (
973
1134
  hashLength: i32,
974
1135
  handshakeSecret: Secret<u8[]>,
975
1136
  dst: Secret<u8[]>
976
- ): void => {
977
- tlsHeldCheck("tlsMasterSecretHeld", hashLength, dst)
1137
+ ): boolean => {
1138
+ if (!tlsHeldCheck(hashLength, dst)) {
1139
+ return false
1140
+ }
978
1141
  const derived: Secret<u8[]> = secret(tlsHashZeros(hashLength))
979
- tlsDeriveSecretHeld(
1142
+ const salted: boolean = tlsDeriveSecretHeld(
980
1143
  hashLength,
981
1144
  handshakeSecret,
982
1145
  "derived",
@@ -985,9 +1148,12 @@ export const tlsMasterSecretHeld = (
985
1148
  derived
986
1149
  )
987
1150
  const zeros: Secret<u8[]> = secret(tlsHashZeros(hashLength))
988
- tlsExtractHeld(hashLength, derived, zeros, dst)
1151
+ if (salted) {
1152
+ tlsExtractHeld(hashLength, derived, zeros, dst)
1153
+ }
989
1154
  wipe(derived)
990
1155
  wipe(zeros)
1156
+ return salted
991
1157
  }
992
1158
 
993
1159
  /**
@@ -995,7 +1161,7 @@ export const tlsMasterSecretHeld = (
995
1161
  * the HMAC of `transcriptHash[hashOff .. hashOff + hashLength)` under
996
1162
  * `finished_key = Expand-Label(baseKey, "finished", "", HashLen)` (RFC 8446
997
1163
  * §4.4.4). The finished key is held and wiped before it returns;
998
- * `verify_data` goes on the wire, so it is plain. Panics unless `hashLength`
1164
+ * `verify_data` goes on the wire, so it is plain. `null` unless `hashLength`
999
1165
  * is 32 or 48, `baseKey` holds HashLen bytes and the transcript hash's window
1000
1166
  * is inside `transcriptHash`.
1001
1167
  *
@@ -1003,23 +1169,29 @@ export const tlsMasterSecretHeld = (
1003
1169
  * @param baseKey The sender's held handshake traffic secret. It stays the caller's: this does not wipe it.
1004
1170
  * @param transcriptHash The array that holds the transcript hash up to the Finished message.
1005
1171
  * @param hashOff Where in `transcriptHash` the `hashLength` bytes of the hash start.
1006
- * @returns The HashLen bytes of `verify_data`, plain, in a fresh array.
1172
+ * @returns The HashLen bytes of `verify_data`, plain, in a fresh array; `null` for a bad length or window.
1007
1173
  */
1008
1174
  export const tlsFinishedVerifyDataHeld = (
1009
1175
  hashLength: i32,
1010
1176
  baseKey: Secret<u8[]>,
1011
1177
  transcriptHash: u8[],
1012
1178
  hashOff: i32
1013
- ): u8[] => {
1014
- tlsHeldCheck("tlsFinishedVerifyDataHeld", hashLength, baseKey)
1015
- // Refused before the finished key exists, so a panic leaves no key unwiped.
1016
- const transcript: u8[] = tlsHeldWindow("tlsFinishedVerifyDataHeld", hashLength, transcriptHash, hashOff)
1179
+ ): u8[] | null => {
1180
+ // Refused before the finished key exists, so a refusal leaves no key unwiped.
1181
+ const transcript: u8[] | null = tlsHeldWindow(hashLength, transcriptHash, hashOff)
1182
+ if (transcript === null || !tlsHeldCheck(hashLength, baseKey)) {
1183
+ return null
1184
+ }
1017
1185
  const none: u8[] = []
1018
1186
  const finishedKey: Secret<u8[]> = secret(tlsHashZeros(hashLength))
1019
- if (hashLength === SHA384_SIZE) {
1020
- hkdfExpandLabelSha384Held(finishedKey, baseKey, "finished", none)
1021
- } else {
1022
- hkdfExpandLabelSha256Held(finishedKey, baseKey, "finished", none)
1187
+ // `tlsHeldCheck` above keeps the expansion in range, so it does not answer `false`.
1188
+ const keyed: boolean =
1189
+ hashLength === SHA384_SIZE
1190
+ ? tryHkdfExpandLabelSha384Held(finishedKey, baseKey, "finished", none)
1191
+ : tryHkdfExpandLabelSha256Held(finishedKey, baseKey, "finished", none)
1192
+ if (!keyed) {
1193
+ wipe(finishedKey)
1194
+ return null
1023
1195
  }
1024
1196
  const verifyData: u8[] =
1025
1197
  hashLength === SHA384_SIZE
@@ -1041,7 +1213,7 @@ export const tlsFinishedVerifyDataHeld = (
1041
1213
  * @param trafficSecret The held traffic secret, of the suite's hash length. It stays the caller's to wipe.
1042
1214
  * @param key Where the AEAD key is written: an array of exactly the suite's key length (16 or 32 bytes).
1043
1215
  * @param iv Where the IV is written: an array of exactly 12 bytes.
1044
- * @returns `true` when the key and IV were written; `false`, with nothing written, for a bad suite or length.
1216
+ * @returns `true` when the key and IV were written; `false`, with nothing written, for a bad suite or length, or, the key possibly written, if the IV's derive refuses (none in range does).
1045
1217
  */
1046
1218
  export const tlsTrafficKeysFromHeld = (
1047
1219
  suite: i32,
@@ -1060,7 +1232,25 @@ export const tlsTrafficKeysFromHeld = (
1060
1232
  ) {
1061
1233
  return false
1062
1234
  }
1063
- hkdfExpandLabelHeldInto(hashLength, trafficSecret, "key", none, key, TLS_SCHEDULE_FROM, keyLength)
1064
- hkdfExpandLabelHeldInto(hashLength, trafficSecret, "iv", none, iv, TLS_SCHEDULE_FROM, TLS_IV_SIZE)
1065
- return true
1235
+ // The tests above keep both expansions in range, so neither answers `false`.
1236
+ return (
1237
+ tryHkdfExpandLabelHeldInto(
1238
+ tlsHkdfHash(hashLength),
1239
+ trafficSecret,
1240
+ "key",
1241
+ none,
1242
+ key,
1243
+ TLS_SCHEDULE_FROM,
1244
+ keyLength
1245
+ ) &&
1246
+ tryHkdfExpandLabelHeldInto(
1247
+ tlsHkdfHash(hashLength),
1248
+ trafficSecret,
1249
+ "iv",
1250
+ none,
1251
+ iv,
1252
+ TLS_SCHEDULE_FROM,
1253
+ TLS_IV_SIZE
1254
+ )
1255
+ )
1066
1256
  }