@amritk/nish-x86_64-linux 0.34.1 → 0.36.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 (126) hide show
  1. package/INSTALL.md +22 -13
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +86 -16
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +49 -0
  6. package/runtime/async.mjs +27 -0
  7. package/runtime/nish.d.ts +4681 -427
  8. package/runtime/nish.h +6 -2
  9. package/runtime/nish.mjs +4 -0
  10. package/runtime/runtime-net.c +22 -0
  11. package/runtime/runtime-shm.c +93 -2
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +108 -13
  15. package/std/README.md +131 -131
  16. package/std/base64.ts +34 -5
  17. package/std/bytes.ts +258 -42
  18. package/std/cli-args.ts +296 -70
  19. package/std/cli-flags.ts +184 -19
  20. package/std/cli.ts +475 -54
  21. package/std/collections.ts +121 -23
  22. package/std/compress/checksum.ts +44 -14
  23. package/std/compress/deflate.ts +224 -111
  24. package/std/compress/gzip.ts +217 -53
  25. package/std/compress/zlib.ts +63 -7
  26. package/std/crypto/aes.ts +80 -9
  27. package/std/crypto/base64url.ts +22 -0
  28. package/std/crypto/bcrypt-batch.ts +88 -21
  29. package/std/crypto/bcrypt.ts +60 -0
  30. package/std/crypto/chacha20poly1305.ts +53 -0
  31. package/std/crypto/ct.ts +32 -13
  32. package/std/crypto/ed25519.ts +47 -0
  33. package/std/crypto/hkdf.ts +198 -13
  34. package/std/crypto/hmac.ts +235 -31
  35. package/std/crypto/jwt.ts +192 -63
  36. package/std/crypto/p256.ts +159 -16
  37. package/std/crypto/p384.ts +28 -3
  38. package/std/crypto/pki-roots.ts +32 -2
  39. package/std/crypto/pki.ts +81 -15
  40. package/std/crypto/rsa.ts +33 -3
  41. package/std/crypto/sha1.ts +3 -0
  42. package/std/crypto/sha256.ts +24 -1
  43. package/std/crypto/sha512.ts +99 -14
  44. package/std/crypto/x25519.ts +152 -14
  45. package/std/crypto/x509-random.ts +10 -0
  46. package/std/crypto/x509.ts +67 -7
  47. package/std/json-equal.ts +19 -0
  48. package/std/json-format.ts +112 -13
  49. package/std/json-number.ts +64 -14
  50. package/std/json-pointer.ts +23 -1
  51. package/std/json-tokenizer.ts +100 -29
  52. package/std/json-typed.ts +713 -66
  53. package/std/json-value.ts +400 -124
  54. package/std/json.ts +138 -10
  55. package/std/map.ts +25 -7
  56. package/std/math.ts +36 -4
  57. package/std/net/buffer-pool.ts +42 -11
  58. package/std/net/dns-config.ts +109 -23
  59. package/std/net/dns-message.ts +62 -7
  60. package/std/net/dns-resolver.ts +247 -53
  61. package/std/net/dns.ts +203 -35
  62. package/std/net/fetch.ts +151 -61
  63. package/std/net/handler.ts +278 -80
  64. package/std/net/hpack.ts +262 -52
  65. package/std/net/http-compress.ts +125 -15
  66. package/std/net/http-fields.ts +85 -9
  67. package/std/net/http1-client-codec.ts +100 -19
  68. package/std/net/http1-codec.ts +297 -77
  69. package/std/net/http1-server.ts +430 -94
  70. package/std/net/http2-frame.ts +272 -35
  71. package/std/net/http2-tls.ts +135 -34
  72. package/std/net/http2.ts +247 -106
  73. package/std/net/http3-frame.ts +154 -17
  74. package/std/net/http3-server.ts +275 -101
  75. package/std/net/http3.ts +307 -115
  76. package/std/net/outbound.ts +37 -3
  77. package/std/net/postgres-carrier.ts +144 -8
  78. package/std/net/postgres.ts +801 -112
  79. package/std/net/prefork.ts +164 -31
  80. package/std/net/qpack.ts +105 -18
  81. package/std/net/quic-conn-ack.ts +59 -12
  82. package/std/net/quic-conn-cid.ts +124 -27
  83. package/std/net/quic-conn-params.ts +70 -10
  84. package/std/net/quic-datagram.ts +43 -3
  85. package/std/net/quic-frame.ts +286 -12
  86. package/std/net/quic-listener.ts +101 -32
  87. package/std/net/quic-packet.ts +302 -21
  88. package/std/net/quic-recovery.ts +264 -52
  89. package/std/net/quic-stream.ts +353 -91
  90. package/std/net/quic.ts +1124 -201
  91. package/std/net/router.ts +87 -40
  92. package/std/net/tls/client.ts +166 -51
  93. package/std/net/tls/codec.ts +413 -56
  94. package/std/net/tls/record-client.ts +110 -55
  95. package/std/net/tls/record-server.ts +159 -59
  96. package/std/net/tls/record.ts +109 -12
  97. package/std/net/tls/schedule.ts +235 -26
  98. package/std/net/tls/ticket.ts +159 -40
  99. package/std/net/tls/verify.ts +49 -3
  100. package/std/net/tls-tcp.ts +250 -45
  101. package/std/net/tls.ts +221 -59
  102. package/std/net/upstream.ts +81 -45
  103. package/std/net/url.ts +97 -13
  104. package/std/net/websocket.ts +122 -12
  105. package/std/net/webtransport.ts +220 -89
  106. package/std/pair.ts +10 -2
  107. package/std/random.ts +34 -9
  108. package/std/regex.ts +352 -198
  109. package/std/region.ts +70 -15
  110. package/std/secret.ts +52 -11
  111. package/std/sort.ts +17 -2
  112. package/std/sqlite.ts +256 -35
  113. package/std/sync/ring.ts +430 -40
  114. package/std/sync/shm.ts +222 -15
  115. package/std/term-io.ts +137 -64
  116. package/std/term-pixels.ts +144 -19
  117. package/std/term-probe.ts +66 -25
  118. package/std/term-rate.ts +128 -57
  119. package/std/term.ts +332 -121
  120. package/std/testing.ts +130 -32
  121. package/std/text.ts +73 -7
  122. package/std/threads.ts +229 -41
  123. package/std/yaml-json.ts +94 -82
  124. package/std/yaml-lines.ts +47 -15
  125. package/std/yaml-write.ts +24 -16
  126. package/std/yaml.ts +764 -384
@@ -98,6 +98,9 @@ const TLS_SCHEDULE_FROM: i32 = 0
98
98
  * The hash length of `suite`: 32 for the two SHA-256 suites, 48 for
99
99
  * `TLS_AES_256_GCM_SHA384`, and 0 for any suite this module does not
100
100
  * negotiate, which is how a caller tells the two apart.
101
+ *
102
+ * @param suite A cipher suite code point, such as `TLS_AES_128_GCM_SHA256`.
103
+ * @returns The suite's hash length in bytes (32 or 48), or 0 for a suite this module does not negotiate.
101
104
  */
102
105
  export const tlsSuiteHashLength = (suite: i32): i32 => {
103
106
  switch (suite) {
@@ -112,7 +115,12 @@ export const tlsSuiteHashLength = (suite: i32): i32 => {
112
115
  }
113
116
  }
114
117
 
115
- /** The AEAD key length of `suite`: 16 for AES-128-GCM, 32 for the other two, 0 for an unknown suite. */
118
+ /**
119
+ * The AEAD key length of `suite`: 16 for AES-128-GCM, 32 for the other two, 0 for an unknown suite.
120
+ *
121
+ * @param suite A cipher suite code point, such as `TLS_AES_128_GCM_SHA256`.
122
+ * @returns The suite's AEAD key length in bytes (16 or 32), or 0 for a suite this module does not negotiate.
123
+ */
116
124
  export const tlsSuiteKeyLength = (suite: i32): i32 => {
117
125
  switch (suite) {
118
126
  case TLS_AES_128_GCM_SHA256:
@@ -129,7 +137,15 @@ export const tlsSuiteKeyLength = (suite: i32): i32 => {
129
137
  /**
130
138
  * HKDF-Expand-Label (RFC 8446 §7.1) over the hash `hashLength` names: `length`
131
139
  * bytes from `prk` (RFC 8446's Secret), the label (without its `"tls13 "`
132
- * prefix, which is added) and `context`, held as a `Secret`.
140
+ * prefix, which is added) and `context`, held as a `Secret`. It panics as
141
+ * `hkdfExpandLabelSha256Secret` (or its SHA-384 twin) does.
142
+ *
143
+ * @param hashLength 48 for SHA-384; any other value runs SHA-256.
144
+ * @param prk The secret to expand. It stays the caller's: this function does not wipe it.
145
+ * @param label The label without its `"tls13 "` prefix, such as `"key"` or `"c hs traffic"`.
146
+ * @param context The public context bytes, usually a transcript hash; empty for none.
147
+ * @param length How many bytes to derive.
148
+ * @returns A fresh `Secret` holding the `length` derived bytes, which the caller owns and wipes.
133
149
  */
134
150
  export const tlsExpandLabel = (
135
151
  hashLength: i32,
@@ -163,6 +179,11 @@ const tlsExtractSha384Salted = (salt: u8[], ikm: Secret<u8[]>): u8[] =>
163
179
  * `Secret`. Both inputs are `Secret`s, since the schedule salts every extract
164
180
  * after the first with a derived secret; an empty salt keys HMAC as HashLen
165
181
  * zeros do.
182
+ *
183
+ * @param hashLength 48 for SHA-384; any other value runs SHA-256.
184
+ * @param salt The HMAC key. It stays the caller's: this function does not wipe it.
185
+ * @param ikm The input keying material. It stays the caller's as well.
186
+ * @returns A fresh `Secret` holding the HashLen-byte PRK, which the caller owns and wipes.
166
187
  */
167
188
  export const tlsExtract = (hashLength: i32, salt: Secret<u8[]>, ikm: Secret<u8[]>): Secret<u8[]> => {
168
189
  if (hashLength === SHA384_SIZE) {
@@ -173,13 +194,27 @@ export const tlsExtract = (hashLength: i32, salt: Secret<u8[]>, ikm: Secret<u8[]
173
194
  return secret(prk)
174
195
  }
175
196
 
176
- /** The hash of the empty string, the transcript `Derive-Secret(., "derived", "")` hashes. */
197
+ /**
198
+ * The hash of the empty string, the transcript `Derive-Secret(., "derived", "")` hashes.
199
+ *
200
+ * @param hashLength 48 for SHA-384; any other value runs SHA-256.
201
+ * @returns The digest of no bytes, in a fresh array. It is public, so it is plain.
202
+ */
177
203
  export const tlsEmptyHash = (hashLength: i32): u8[] => {
178
204
  const none: u8[] = []
179
205
  return hashLength === SHA384_SIZE ? sha384(none) : sha256(none)
180
206
  }
181
207
 
182
- /** Derive-Secret (RFC 8446 §7.1): Expand-Label of `prk` with `label` and a transcript hash, HashLen bytes held as a `Secret`. */
208
+ /**
209
+ * Derive-Secret (RFC 8446 §7.1): Expand-Label of `prk` with `label` and a transcript hash, HashLen bytes
210
+ * held as a `Secret`.
211
+ *
212
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are derived.
213
+ * @param prk The stage of the schedule to derive from. It stays the caller's: this function does not wipe it.
214
+ * @param label The label without its `"tls13 "` prefix, such as `"s hs traffic"`.
215
+ * @param transcriptHash The transcript hash the secret is bound to, all of it used as the context.
216
+ * @returns A fresh `Secret` holding the HashLen derived bytes, which the caller owns and wipes.
217
+ */
183
218
  export const tlsDeriveSecret = (
184
219
  hashLength: i32,
185
220
  prk: Secret<u8[]>,
@@ -187,7 +222,13 @@ export const tlsDeriveSecret = (
187
222
  transcriptHash: u8[]
188
223
  ): Secret<u8[]> => tlsExpandLabel(hashLength, prk, label, transcriptHash, hashLength)
189
224
 
190
- /** The early secret with no PSK: HKDF-Extract of HashLen zeros under a zero salt (RFC 8446 §7.1). */
225
+ /**
226
+ * The early secret with no PSK: HKDF-Extract of HashLen zeros under a zero salt (RFC 8446 §7.1). The zero
227
+ * salt and the zero input are wiped before it returns.
228
+ *
229
+ * @param hashLength The hash length, 32 or 48.
230
+ * @returns A fresh `Secret` holding the early secret, which the caller owns and wipes.
231
+ */
191
232
  export const tlsEarlySecret = (hashLength: i32): Secret<u8[]> => {
192
233
  const salt: Secret<u8[]> = secret(new Array<u8>(hashLength))
193
234
  const ikm: Secret<u8[]> = secret(new Array<u8>(hashLength))
@@ -201,6 +242,10 @@ export const tlsEarlySecret = (hashLength: i32): Secret<u8[]> => {
201
242
  * The early secret of a resumed handshake: HKDF-Extract of the PSK under a
202
243
  * zero salt (RFC 8446 §7.1). The zero salt is wiped before it returns; the
203
244
  * PSK is the caller's.
245
+ *
246
+ * @param hashLength The hash length, 32 or 48.
247
+ * @param psk The pre-shared key the ticket stands for. It stays the caller's to wipe.
248
+ * @returns A fresh `Secret` holding the early secret, which the caller owns and wipes.
204
249
  */
205
250
  export const tlsPskEarlySecret = (hashLength: i32, psk: Secret<u8[]>): Secret<u8[]> => {
206
251
  const salt: Secret<u8[]> = secret(new Array<u8>(hashLength))
@@ -209,7 +254,13 @@ export const tlsPskEarlySecret = (hashLength: i32, psk: Secret<u8[]>): Secret<u8
209
254
  return early
210
255
  }
211
256
 
212
- /** The binder key of a resumption PSK: `Derive-Secret(early, "res binder", "")` (RFC 8446 §7.1). */
257
+ /**
258
+ * The binder key of a resumption PSK: `Derive-Secret(early, "res binder", "")` (RFC 8446 §7.1).
259
+ *
260
+ * @param hashLength The hash length, 32 or 48.
261
+ * @param earlySecret The early secret from `tlsPskEarlySecret`. It stays the caller's: this does not wipe it.
262
+ * @returns A fresh `Secret` holding the binder key, which the caller owns and wipes.
263
+ */
213
264
  export const tlsBinderKey = (hashLength: i32, earlySecret: Secret<u8[]>): Secret<u8[]> =>
214
265
  tlsDeriveSecret(hashLength, earlySecret, "res binder", tlsEmptyHash(hashLength))
215
266
 
@@ -217,6 +268,10 @@ export const tlsBinderKey = (hashLength: i32, earlySecret: Secret<u8[]>): Secret
217
268
  * The salt the handshake secret is extracted under, `Derive-Secret(early,
218
269
  * "derived", "")` (RFC 8446 §7.1): what `tlsHandshakeSecret` derives and
219
270
  * wipes itself, answered for a caller that extracts in place.
271
+ *
272
+ * @param hashLength The hash length, 32 or 48.
273
+ * @param earlySecret The early secret. It stays the caller's: this function does not wipe it.
274
+ * @returns A fresh `Secret` holding the salt, which the caller owns and wipes once the extract is done.
220
275
  */
221
276
  export const tlsHandshakeSalt = (hashLength: i32, earlySecret: Secret<u8[]>): Secret<u8[]> =>
222
277
  tlsDeriveSecret(hashLength, earlySecret, "derived", tlsEmptyHash(hashLength))
@@ -224,6 +279,11 @@ export const tlsHandshakeSalt = (hashLength: i32, earlySecret: Secret<u8[]>): Se
224
279
  /**
225
280
  * The PSK a ticket stands for: `HKDF-Expand-Label(resumption_master_secret,
226
281
  * "resumption", ticket_nonce, HashLen)` (RFC 8446 §4.6.1).
282
+ *
283
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are derived.
284
+ * @param resumptionMasterSecret The connection's resumption master secret. It stays the caller's to wipe.
285
+ * @param ticketNonce The `ticket_nonce` of the NewSessionTicket, all of it used as the context. It is public.
286
+ * @returns A fresh `Secret` holding the PSK, which the caller owns and wipes.
227
287
  */
228
288
  export const tlsResumptionPsk = (
229
289
  hashLength: i32,
@@ -234,6 +294,11 @@ export const tlsResumptionPsk = (
234
294
  /**
235
295
  * The handshake secret: HKDF-Extract of the ECDHE secret under
236
296
  * `Derive-Secret(early, "derived", "")`, which is wiped before it returns.
297
+ *
298
+ * @param hashLength The hash length, 32 or 48.
299
+ * @param earlySecret The early secret. It stays the caller's: this function does not wipe it.
300
+ * @param ecdhe The shared secret of the key exchange. It stays the caller's to wipe as well.
301
+ * @returns A fresh `Secret` holding the handshake secret, which the caller owns and wipes.
237
302
  */
238
303
  export const tlsHandshakeSecret = (
239
304
  hashLength: i32,
@@ -248,7 +313,12 @@ export const tlsHandshakeSecret = (
248
313
 
249
314
  /**
250
315
  * The master secret: HKDF-Extract of HashLen zeros under
251
- * `Derive-Secret(handshake, "derived", "")`, which is wiped before it returns.
316
+ * `Derive-Secret(handshake, "derived", "")`, which is wiped before it returns,
317
+ * as the zeros are.
318
+ *
319
+ * @param hashLength The hash length, 32 or 48.
320
+ * @param handshakeSecret The handshake secret. It stays the caller's: this function does not wipe it.
321
+ * @returns A fresh `Secret` holding the master secret, which the caller owns and wipes.
252
322
  */
253
323
  export const tlsMasterSecret = (hashLength: i32, handshakeSecret: Secret<u8[]>): Secret<u8[]> => {
254
324
  const derived: Secret<u8[]> = tlsDeriveSecret(
@@ -270,6 +340,11 @@ export const tlsMasterSecret = (hashLength: i32, handshakeSecret: Secret<u8[]>):
270
340
  * HashLen)`, where `baseKey` is the sender's handshake traffic secret. The
271
341
  * `finished_key` is wiped before it returns; `verify_data` goes on the wire,
272
342
  * so it is plain.
343
+ *
344
+ * @param hashLength The hash length, 32 or 48.
345
+ * @param baseKey The sender's handshake traffic secret. It stays the caller's: this does not wipe it.
346
+ * @param transcriptHash The transcript hash up to, and not including, the Finished message.
347
+ * @returns The HashLen bytes of `verify_data`, plain, in a fresh array.
273
348
  */
274
349
  export const tlsFinishedVerifyData = (hashLength: i32, baseKey: Secret<u8[]>, transcriptHash: u8[]): u8[] => {
275
350
  const none: u8[] = []
@@ -285,6 +360,11 @@ export const tlsFinishedVerifyData = (hashLength: i32, baseKey: Secret<u8[]>, tr
285
360
  /**
286
361
  * The AEAD key a traffic secret gives `suite`'s record protection (RFC 8446
287
362
  * §7.3), held as a `Secret`; empty for a suite this module does not negotiate.
363
+ *
364
+ * @param suite The negotiated cipher suite, such as `TLS_AES_128_GCM_SHA256`.
365
+ * @param trafficSecret The traffic secret the key comes from. It stays the caller's to wipe.
366
+ * @returns A fresh `Secret` holding the suite's key (16 or 32 bytes), or an empty one for an unknown suite.
367
+ * The caller owns and wipes it.
288
368
  */
289
369
  export const tlsTrafficKey = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> => {
290
370
  const none: u8[] = []
@@ -298,6 +378,11 @@ export const tlsTrafficKey = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u
298
378
  /**
299
379
  * The 12-byte IV a traffic secret gives `suite`'s record protection (RFC 8446
300
380
  * §7.3), held as a `Secret`; empty for a suite this module does not negotiate.
381
+ *
382
+ * @param suite The negotiated cipher suite, such as `TLS_AES_128_GCM_SHA256`.
383
+ * @param trafficSecret The traffic secret the IV comes from. It stays the caller's to wipe.
384
+ * @returns A fresh `Secret` holding the 12-byte IV, or an empty one for an unknown suite. The caller owns
385
+ * and wipes it.
301
386
  */
302
387
  export const tlsTrafficIv = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> => {
303
388
  const none: u8[] = []
@@ -326,16 +411,22 @@ export const tlsTrafficIv = (suite: i32, trafficSecret: Secret<u8[]>): Secret<u8
326
411
  */
327
412
  export class TlsTranscript {
328
413
  /** 32 for SHA-256, 48 for SHA-384. */
329
- hashLength: i32
330
- sha256: Sha256
331
- sha384: Sha384
414
+ private hashLength: i32
415
+ private sha256: Sha256
416
+ private sha384: Sha384
332
417
  /** Never fed: the initial hash values `reset` copies from. */
333
- fresh256: Sha256
334
- fresh384: Sha384
418
+ private fresh256: Sha256
419
+ private fresh384: Sha384
335
420
  /** Where `hashInto` finishes a copy of the running hash, so that the transcript goes on. */
336
- twin256: Sha256
337
- twin384: Sha384
421
+ private twin256: Sha256
422
+ private twin384: Sha384
338
423
 
424
+ /**
425
+ * Makes an empty transcript and all six of its hashers, once, so that `reset` can reuse them.
426
+ *
427
+ * @param hashLength 32 to hash with SHA-256, 48 to hash with SHA-384, or 0 for a
428
+ * transcript whose hash `reset` names later.
429
+ */
339
430
  constructor(hashLength: i32) {
340
431
  this.hashLength = hashLength
341
432
  this.sha256 = new Sha256()
@@ -346,14 +437,24 @@ export class TlsTranscript {
346
437
  this.twin384 = new Sha384()
347
438
  }
348
439
 
349
- /** Starts an empty transcript over the hash `hashLength` names, in the hashers it already has. */
440
+ /**
441
+ * Starts an empty transcript over the hash `hashLength` names, in the hashers it already has.
442
+ *
443
+ * @internal Read by `net/tls.ts` and `net/tls/client.ts`.
444
+ */
350
445
  reset(hashLength: i32): void {
351
446
  this.hashLength = hashLength
352
447
  hmacCopySha256(this.fresh256, this.sha256)
353
448
  hmacCopySha384(this.fresh384, this.sha384)
354
449
  }
355
450
 
356
- /** Absorbs `data[off .. off + len)`, a whole handshake message or part of one. */
451
+ /**
452
+ * Absorbs `data[off .. off + len)`, a whole handshake message or part of one.
453
+ *
454
+ * @param data The array that holds the message bytes.
455
+ * @param off Where in `data` the bytes to absorb start.
456
+ * @param len How many bytes, from `off`, to absorb.
457
+ */
357
458
  update(data: u8[], off: i32, len: i32): void {
358
459
  if (this.hashLength === SHA384_SIZE) {
359
460
  this.sha384.update(data, off, len)
@@ -366,6 +467,8 @@ export class TlsTranscript {
366
467
  * Writes the transcript hash of everything absorbed so far, `hashLength`
367
468
  * bytes, at `out[at]`; the transcript goes on. What the digest allocates
368
469
  * dies with the call. A window outside `out` writes what fits.
470
+ *
471
+ * @internal Read by `net/tls.ts` and `net/tls/client.ts`.
369
472
  */
370
473
  hashInto(out: u8[], at: i32): void {
371
474
  let digest: u8[] = []
@@ -381,7 +484,11 @@ export class TlsTranscript {
381
484
  }
382
485
  }
383
486
 
384
- /** The transcript hash of everything absorbed so far, in a fresh array; the transcript goes on. */
487
+ /**
488
+ * The transcript hash of everything absorbed so far, in a fresh array; the transcript goes on.
489
+ *
490
+ * @returns The `hashLength` bytes of the hash so far. It is public, so it is plain.
491
+ */
385
492
  hash(): u8[] {
386
493
  const out: u8[] = new Array<u8>(this.hashLength)
387
494
  this.hashInto(out, TLS_SCHEDULE_FROM)
@@ -425,7 +532,17 @@ const tlsEmptyHashInto = (hashLength: i32, out: u8[]): void => {
425
532
  /**
426
533
  * `tlsDeriveSecret` in place: Derive-Secret (RFC 8446 §7.1) of `prk` with
427
534
  * `label` and the transcript hash `transcriptHash[hashOff .. hashOff +
428
- * hashLength)`, its HashLen bytes written at `out[0]`.
535
+ * hashLength)`, its HashLen bytes written at `out[0]`. The scratch is wiped
536
+ * before it returns. It panics as `hkdfExpandLabelInto` does, a window outside
537
+ * its array included.
538
+ *
539
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
540
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
541
+ * @param prk The stage of the schedule to derive from, plain. It stays the caller's to wipe.
542
+ * @param label The label without its `"tls13 "` prefix, such as `"s hs traffic"`.
543
+ * @param transcriptHash The array that holds the transcript hash.
544
+ * @param hashOff Where in `transcriptHash` the `hashLength` bytes of the hash start.
545
+ * @param out Where the derived secret is written, from index 0; the caller owns and wipes it.
429
546
  */
430
547
  export const tlsDeriveSecretInto = (
431
548
  kdf: HkdfScratch,
@@ -453,7 +570,12 @@ export const tlsDeriveSecretInto = (
453
570
  /**
454
571
  * `tlsMasterSecret` in place: HKDF-Extract of HashLen zeros under
455
572
  * `Derive-Secret(handshake, "derived", "")`, written at `out[0]`. The salt is
456
- * wiped before it returns.
573
+ * wiped before it returns, and so is the scratch.
574
+ *
575
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
576
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
577
+ * @param handshakeSecret The handshake secret, plain. It stays the caller's to wipe.
578
+ * @param out Where the master secret is written, from index 0; the caller owns and wipes it.
457
579
  */
458
580
  export const tlsMasterSecretInto = (
459
581
  kdf: HkdfScratch,
@@ -474,6 +596,14 @@ export const tlsMasterSecretInto = (
474
596
  * hashOff + hashLength)` under `finished_key = Expand-Label(baseKey,
475
597
  * "finished", "", HashLen)` (RFC 8446 §4.4.4), written at `out[at]`. The
476
598
  * finished key is wiped before it returns, and so is the scratch.
599
+ *
600
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
601
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
602
+ * @param baseKey The sender's handshake traffic secret, plain. It stays the caller's to wipe.
603
+ * @param transcriptHash The array that holds the transcript hash up to the Finished message.
604
+ * @param hashOff Where in `transcriptHash` the `hashLength` bytes of the hash start.
605
+ * @param out Where `verify_data` is written. It goes on the wire, so it is plain.
606
+ * @param at Where in `out` the HashLen bytes of `verify_data` start.
477
607
  */
478
608
  export const tlsFinishedVerifyDataInto = (
479
609
  kdf: HkdfScratch,
@@ -515,12 +645,25 @@ export const tlsFinishedVerifyDataInto = (
515
645
  * `tlsPskEarlySecret` in place: HKDF-Extract of `psk` under a zero salt
516
646
  * (RFC 8446 §7.1), written at `out[0]`. The scratch is wiped; the zero salt
517
647
  * holds nothing to wipe.
648
+ *
649
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
650
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
651
+ * @param psk The pre-shared key, plain. It stays the caller's to wipe.
652
+ * @param out Where the early secret is written, from index 0; the caller owns and wipes it.
518
653
  */
519
654
  export const tlsPskEarlySecretInto = (kdf: HkdfScratch, hashLength: i32, psk: u8[], out: u8[]): void => {
520
655
  hkdfExtractInto(kdf, hashLength, new Array<u8>(hashLength), psk, out, TLS_SCHEDULE_FROM)
521
656
  }
522
657
 
523
- /** `tlsBinderKey` in place: `Derive-Secret(early, "res binder", "")`, written at `out[0]`. */
658
+ /**
659
+ * `tlsBinderKey` in place: `Derive-Secret(early, "res binder", "")`, written at `out[0]`. The scratch is
660
+ * wiped before it returns.
661
+ *
662
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
663
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
664
+ * @param earlySecret The early secret, plain. It stays the caller's to wipe.
665
+ * @param out Where the binder key is written, from index 0; the caller owns and wipes it.
666
+ */
524
667
  export const tlsBinderKeyInto = (kdf: HkdfScratch, hashLength: i32, earlySecret: u8[], out: u8[]): void => {
525
668
  const empty: u8[] = new Array<u8>(hashLength)
526
669
  tlsEmptyHashInto(hashLength, empty)
@@ -530,7 +673,13 @@ export const tlsBinderKeyInto = (kdf: HkdfScratch, hashLength: i32, earlySecret:
530
673
  /**
531
674
  * `tlsHandshakeSalt` in place: `Derive-Secret(early, "derived", "")`, the
532
675
  * salt a resumed handshake extracts its handshake secret under, written at
533
- * `out[0]`. The caller wipes it once the extract is done.
676
+ * `out[0]`. The caller wipes it once the extract is done. The scratch is wiped
677
+ * before it returns.
678
+ *
679
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
680
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
681
+ * @param earlySecret The early secret, plain. It stays the caller's to wipe.
682
+ * @param out Where the salt is written, from index 0.
534
683
  */
535
684
  export const tlsHandshakeSaltInto = (
536
685
  kdf: HkdfScratch,
@@ -546,7 +695,17 @@ export const tlsHandshakeSaltInto = (
546
695
  /**
547
696
  * `tlsResumptionPsk` in place: `HKDF-Expand-Label(resumption_master_secret,
548
697
  * "resumption", ticketNonce[nonceOff .. nonceOff + nonceLength), HashLen)`
549
- * (RFC 8446 §4.6.1), written at `out[0]`.
698
+ * (RFC 8446 §4.6.1), written at `out[0]`. The scratch is wiped before it
699
+ * returns. A nonce window outside `ticketNonce`, or longer than 255 bytes,
700
+ * panics, as `hkdfExpandLabelInto` does.
701
+ *
702
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
703
+ * @param hashLength The hash length, 32 or 48, which is also how many bytes are written.
704
+ * @param resumptionMasterSecret The connection's resumption master secret, plain. It stays the caller's.
705
+ * @param ticketNonce The array that holds the ticket's `ticket_nonce`, which is public.
706
+ * @param nonceOff Where in `ticketNonce` the nonce starts.
707
+ * @param nonceLength How many bytes the nonce is.
708
+ * @param out Where the PSK is written, from index 0; the caller owns and wipes it.
550
709
  */
551
710
  export const tlsResumptionPskInto = (
552
711
  kdf: HkdfScratch,
@@ -582,6 +741,16 @@ export const tlsResumptionPskInto = (
582
741
  * whatever it finds; the binder the server computed is wiped before it
583
742
  * returns, as `tlsFinishedVerifyDataInto` wipes the finished key and the
584
743
  * scratch.
744
+ *
745
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
746
+ * @param hashLength The hash length, 32 or 48.
747
+ * @param binderKey The binder key from `tlsBinderKeyInto`, plain. It stays the caller's to wipe.
748
+ * @param truncatedHash The array that holds the hash of the ClientHello up to its binders list.
749
+ * @param hashOff Where in `truncatedHash` the `hashLength` bytes of that hash start.
750
+ * @param binder The array that holds the binder the client sent, usually the received ClientHello.
751
+ * @param binderOff Where in `binder` the client's binder starts.
752
+ * @param binderLength How many bytes the client's binder is.
753
+ * @returns `true` when the client's binder matches the one the server computed, so the client holds the PSK.
585
754
  */
586
755
  export const tlsBinderVerify = (
587
756
  kdf: HkdfScratch,
@@ -608,7 +777,15 @@ export const tlsBinderVerify = (
608
777
  * gives `suite` (RFC 8446 §7.3) written into `key`, which must be the suite's
609
778
  * key length, and its 12-byte IV into `iv`. `false`, writing nothing, for a
610
779
  * suite this module does not negotiate, a secret that is not its hash's
611
- * length, or a key or IV array of the wrong length.
780
+ * length, or a key or IV array of the wrong length. When it derives, the
781
+ * scratch is wiped before it returns.
782
+ *
783
+ * @param kdf The caller's HKDF scratch, which this computes in and wipes.
784
+ * @param suite The negotiated cipher suite, such as `TLS_AES_128_GCM_SHA256`.
785
+ * @param trafficSecret The traffic secret, plain, of the suite's hash length. It stays the caller's to wipe.
786
+ * @param key Where the AEAD key is written: an array of exactly the suite's key length (16 or 32 bytes).
787
+ * @param iv Where the IV is written: an array of exactly 12 bytes.
788
+ * @returns `true` when the key and IV were written; `false`, with nothing written, for a bad suite or length.
612
789
  */
613
790
  export const tlsTrafficKeysInto = (
614
791
  kdf: HkdfScratch,
@@ -706,6 +883,13 @@ const tlsExtractHeld = (hashLength: i32, salt: Secret<u8[]>, ikm: Secret<u8[]>,
706
883
  * hashOff + hashLength)`, written over the HashLen bytes `dst` holds. `dst`
707
884
  * may be `prk`. Panics unless `hashLength` is 32 or 48 and both hold
708
885
  * HashLen bytes.
886
+ *
887
+ * @param hashLength The hash length, 32 or 48.
888
+ * @param prk The held stage of the schedule to derive from. It is only overwritten when it is also `dst`.
889
+ * @param label The label without its `"tls13 "` prefix, such as `"c ap traffic"`.
890
+ * @param transcriptHash The array that holds the transcript hash, which is public.
891
+ * @param hashOff Where in `transcriptHash` the `hashLength` bytes of the hash start.
892
+ * @param dst The held secret whose bytes the result overwrites. The caller still owns and wipes it.
709
893
  */
710
894
  export const tlsDeriveSecretHeld = (
711
895
  hashLength: i32,
@@ -728,7 +912,14 @@ export const tlsDeriveSecretHeld = (
728
912
  /**
729
913
  * `tlsHandshakeSecret` into a held `Secret`: HKDF-Extract of `ecdhe` under
730
914
  * `Derive-Secret(early, "derived", "")`, written over `dst`'s HashLen bytes.
731
- * The salt is wiped before it returns; `dst` may be `earlySecret`.
915
+ * The salt is wiped before it returns; `dst` may be `earlySecret`. Panics
916
+ * unless `hashLength` is 32 or 48 and `earlySecret` and `dst` hold HashLen
917
+ * bytes.
918
+ *
919
+ * @param hashLength The hash length, 32 or 48.
920
+ * @param earlySecret The held early secret. It is only overwritten when it is also `dst`.
921
+ * @param ecdhe The held shared secret of the key exchange. It stays the caller's to wipe.
922
+ * @param dst The held secret whose bytes the handshake secret overwrites. The caller still owns and wipes it.
732
923
  */
733
924
  export const tlsHandshakeSecretHeld = (
734
925
  hashLength: i32,
@@ -754,7 +945,12 @@ export const tlsHandshakeSecretHeld = (
754
945
  * `tlsMasterSecret` into a held `Secret`: HKDF-Extract of HashLen zeros under
755
946
  * `Derive-Secret(handshake, "derived", "")`, written over `dst`'s HashLen
756
947
  * bytes. The salt and the zeros are wiped before it returns; `dst` may be
757
- * `handshakeSecret`.
948
+ * `handshakeSecret`. Panics unless `hashLength` is 32 or 48 and both hold
949
+ * HashLen bytes.
950
+ *
951
+ * @param hashLength The hash length, 32 or 48.
952
+ * @param handshakeSecret The held handshake secret. It is only overwritten when it is also `dst`.
953
+ * @param dst The held secret whose bytes the master secret overwrites. The caller still owns and wipes it.
758
954
  */
759
955
  export const tlsMasterSecretHeld = (
760
956
  hashLength: i32,
@@ -782,7 +978,14 @@ export const tlsMasterSecretHeld = (
782
978
  * the HMAC of `transcriptHash[hashOff .. hashOff + hashLength)` under
783
979
  * `finished_key = Expand-Label(baseKey, "finished", "", HashLen)` (RFC 8446
784
980
  * §4.4.4). The finished key is held and wiped before it returns;
785
- * `verify_data` goes on the wire, so it is plain.
981
+ * `verify_data` goes on the wire, so it is plain. Panics unless `hashLength`
982
+ * is 32 or 48 and `baseKey` holds HashLen bytes.
983
+ *
984
+ * @param hashLength The hash length, 32 or 48.
985
+ * @param baseKey The sender's held handshake traffic secret. It stays the caller's: this does not wipe it.
986
+ * @param transcriptHash The array that holds the transcript hash up to the Finished message.
987
+ * @param hashOff Where in `transcriptHash` the `hashLength` bytes of the hash start.
988
+ * @returns The HashLen bytes of `verify_data`, plain, in a fresh array.
786
989
  */
787
990
  export const tlsFinishedVerifyDataHeld = (
788
991
  hashLength: i32,
@@ -814,6 +1017,12 @@ export const tlsFinishedVerifyDataHeld = (
814
1017
  * reads them. `false`, writing nothing, for a suite this module does not
815
1018
  * negotiate, a secret that is not its hash's length, or a key or IV array of
816
1019
  * the wrong length.
1020
+ *
1021
+ * @param suite The negotiated cipher suite, such as `TLS_AES_128_GCM_SHA256`.
1022
+ * @param trafficSecret The held traffic secret, of the suite's hash length. It stays the caller's to wipe.
1023
+ * @param key Where the AEAD key is written: an array of exactly the suite's key length (16 or 32 bytes).
1024
+ * @param iv Where the IV is written: an array of exactly 12 bytes.
1025
+ * @returns `true` when the key and IV were written; `false`, with nothing written, for a bad suite or length.
817
1026
  */
818
1027
  export const tlsTrafficKeysFromHeld = (
819
1028
  suite: i32,