@amritk/nish-x86_64-linux 0.35.0 → 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 (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
package/std/crypto/ct.ts CHANGED
@@ -1,21 +1,27 @@
1
1
  /**
2
2
  * `nish/crypto/ct` — comparing secrets without telling the clock where they differ.
3
3
  *
4
- * An `===` loop over two MACs stops at the first differing byte, so the time it
5
- * takes says how many leading bytes an attacker already guessed right, and a
6
- * tag can be recovered one byte at a time. These functions read every byte of
7
- * the window whatever it holds: each pair is XORed, the differences are ORed
8
- * into one accumulator, and that accumulator is tested once, after the loop.
4
+ * import { timingSafeEqual } from "nish/crypto/ct";
9
5
  *
10
- * What is *not* secret is checked first and may branch: the two lengths, and
11
- * whether a window lies inside its array. A caller comparing a received tag
12
- * against an expected one already knows both lengths, so refusing a mismatch
13
- * early leaks nothing the attacker did not send.
6
+ * const ok: boolean = timingSafeEqual(expectedTag, receivedTag);
14
7
  *
15
- * The names are `timingSafeEqual*`, after Node's `crypto.timingSafeEqual`,
16
- * rather than `ctEq`: that one is reserved for the builtin WP34 N6 will add,
17
- * together with the disassembly check that proves the loop stays branch-free
18
- * after LLVM has seen it. Until then this is the discipline and not a proof.
8
+ * **Every byte is read.** An `===` loop over two MACs stops at the first
9
+ * differing byte, so the time it takes says how many leading bytes an attacker
10
+ * already guessed right, and a tag can be recovered one byte at a time. These
11
+ * functions read every byte of the window whatever it holds: each pair is
12
+ * XORed, the differences are ORed into one accumulator, and that accumulator is
13
+ * tested once, after the loop.
14
+ *
15
+ * **Lengths may branch.** What is *not* secret is checked first and may branch:
16
+ * the two lengths, and whether a window lies inside its array. A caller
17
+ * comparing a received tag against an expected one already knows both lengths,
18
+ * so refusing a mismatch early leaks nothing the attacker did not send.
19
+ *
20
+ * **A discipline, not yet a proof.** The names are `timingSafeEqual*`, after
21
+ * Node's `crypto.timingSafeEqual`, rather than `ctEq`: that one is reserved for
22
+ * the builtin WP34 N6 will add, together with the disassembly check that proves
23
+ * the loop stays branch-free after LLVM has seen it. Until then this is the
24
+ * discipline and not a proof.
19
25
  */
20
26
 
21
27
  /**
@@ -29,6 +35,11 @@
29
35
  * it would have looked the same length and been compared only that far. An
30
36
  * array longer than that answers `false` too — no `i32` index reaches its end,
31
37
  * and a compare that cannot read every byte must not say "equal".
38
+ *
39
+ * @param a One byte string, such as the tag that was received.
40
+ * @param b The other, such as the tag computed for comparison.
41
+ * @returns `true` only when both lengths match and every byte does; `false` for
42
+ * different lengths or a length past 2^31 - 1.
32
43
  */
33
44
  export const timingSafeEqual = (a: u8[], b: u8[]): boolean => {
34
45
  if (a.length !== b.length || a.length > 2147483647) {
@@ -53,6 +64,14 @@ export const timingSafeEqual = (a: u8[], b: u8[]): boolean => {
53
64
  * first out-of-range read. That is a decision about the caller's position: a
54
65
  * verifier handed a truncated record should say "not equal", not take the
55
66
  * process down, and the bounds are public, so testing them first is safe.
67
+ *
68
+ * @param a The array holding the first window.
69
+ * @param aOff Where the first window starts in `a`.
70
+ * @param b The array holding the second window.
71
+ * @param bOff Where the second window starts in `b`.
72
+ * @param len How many bytes each window covers.
73
+ * @returns `true` only when both windows lie inside their arrays and every byte
74
+ * matches; `false` for a window out of range.
56
75
  */
57
76
  export const timingSafeEqualAt = (a: u8[], aOff: i32, b: u8[], bOff: i32, len: i32): boolean => {
58
77
  const aLen: i32 = toI32(a.length)
@@ -1,6 +1,20 @@
1
1
  /**
2
2
  * `nish/crypto/ed25519` — the Ed25519 signature scheme of RFC 8032 (§5.1).
3
3
  *
4
+ * import { ED25519_SEED_SIZE, ed25519PublicKey, ed25519Sign, ed25519Verify } from "nish/crypto/ed25519";
5
+ * import { Secret, secret, wipe } from "nish:secret";
6
+ *
7
+ * const raw: u8[] = new Array<u8>(ED25519_SEED_SIZE)
8
+ * crypto.getRandomValues(raw) // any 32 bytes are a seed
9
+ * const seed: Secret<u8[]> = secret(raw)
10
+ * const message: u8[] = [104, 105]
11
+ * const pub: u8[] | null = ed25519PublicKey(seed) // 32 bytes
12
+ * const sig: u8[] | null = ed25519Sign(seed, message) // 64 bytes, R || S
13
+ * wipe(seed)
14
+ * if (pub !== null && sig !== null) {
15
+ * ed25519Verify(pub, message, sig) // true
16
+ * }
17
+ *
4
18
  * Written from the specification (RFC 8032 §5.1.1 to §5.1.7), in this
5
19
  * module's own structure: nothing here is ported from another implementation.
6
20
  * The field is `nish/crypto/x25519`'s GF(2^255 - 19), imported rather than
@@ -152,9 +166,13 @@ const ed25519Order = (): u64[] => [
152
166
 
153
167
  /** A point (X : Y : Z : T) in extended coordinates. */
154
168
  class Ed25519Point {
169
+ /** @internal Read and written by this module's point functions; the class is not exported. */
155
170
  x: i64[]
171
+ /** @internal Read and written by this module's point functions; the class is not exported. */
156
172
  y: i64[]
173
+ /** @internal Read and written by this module's point functions; the class is not exported. */
157
174
  z: i64[]
175
+ /** @internal Read and written by this module's point functions; the class is not exported. */
158
176
  t: i64[]
159
177
 
160
178
  /** The identity, (0 : 1 : 1 : 0). */
@@ -168,9 +186,13 @@ class Ed25519Point {
168
186
 
169
187
  /** A point in the form the addition reads: (Y + X, Y - X, 2 d T, 2 Z), each carried. */
170
188
  class Ed25519Cached {
189
+ /** @internal Read and written by this module's point functions; the class is not exported. */
171
190
  ypx: i64[]
191
+ /** @internal Read and written by this module's point functions; the class is not exported. */
172
192
  ymx: i64[]
193
+ /** @internal Read and written by this module's point functions; the class is not exported. */
173
194
  t2d: i64[]
195
+ /** @internal Read and written by this module's point functions; the class is not exported. */
174
196
  z2: i64[]
175
197
 
176
198
  /** The identity, (1, 1, 0, 2). */
@@ -188,16 +210,27 @@ class Ed25519Cached {
188
210
  * is built in; and 2 d, which the cached form multiplies by.
189
211
  */
190
212
  class Ed25519Scratch {
213
+ /** @internal Read and written by this module's point functions; the class is not exported. */
191
214
  a: i64[]
215
+ /** @internal Read and written by this module's point functions; the class is not exported. */
192
216
  b: i64[]
217
+ /** @internal Read and written by this module's point functions; the class is not exported. */
193
218
  c: i64[]
219
+ /** @internal Read and written by this module's point functions; the class is not exported. */
194
220
  d: i64[]
221
+ /** @internal Read and written by this module's point functions; the class is not exported. */
195
222
  e: i64[]
223
+ /** @internal Read and written by this module's point functions; the class is not exported. */
196
224
  f: i64[]
225
+ /** @internal Read and written by this module's point functions; the class is not exported. */
197
226
  g: i64[]
227
+ /** @internal Read and written by this module's point functions; the class is not exported. */
198
228
  h: i64[]
229
+ /** @internal Read and written by this module's point functions; the class is not exported. */
199
230
  entry: Ed25519Cached
231
+ /** @internal Read and written by this module's point functions; the class is not exported. */
200
232
  sum: Ed25519Point
233
+ /** @internal Read and written by this module's point functions; the class is not exported. */
201
234
  d2: i64[]
202
235
 
203
236
  constructor() {
@@ -716,6 +749,10 @@ const ed25519Challenge = (r: u8[], pub: u8[], msg: u8[]): u8[] => {
716
749
  /**
717
750
  * The public key of `seed` (RFC 8032 §5.1.5): the encoding of `a * B`, as 32
718
751
  * plain bytes. Answers `null` unless `seed` is `ED25519_SEED_SIZE` bytes.
752
+ *
753
+ * @param seed The private key, 32 bytes.
754
+ * @returns The 32-byte encoded public key, or `null` for a seed of the wrong
755
+ * length.
719
756
  */
720
757
  export const ed25519PublicKey = (seed: Secret<u8[]>): u8[] | null => expose(seed, ed25519PublicKeyExposed)
721
758
 
@@ -742,6 +779,11 @@ const ed25519PublicKeyExposed = (seed: u8[]): u8[] | null => {
742
779
  * bytes. The public key is derived from the seed again on every call. A
743
780
  * message longer than 2^31 - 1 bytes panics, as in `sha512`: its length does
744
781
  * not survive `toI32`, and the signature would be of a prefix.
782
+ *
783
+ * @param seed The private key, 32 bytes.
784
+ * @param msg The whole message signed.
785
+ * @returns The 64-byte signature `R || S`, or `null` for a seed of the wrong
786
+ * length.
745
787
  */
746
788
  export const ed25519Sign = (seed: Secret<u8[]>, msg: u8[]): u8[] | null => {
747
789
  if (msg.length > 2147483647) {
@@ -805,6 +847,11 @@ const ed25519SignExposed = (seed: u8[], msg: u8[]): u8[] | null => {
805
847
  * bytes panics, as in `ed25519Sign`: a check of a prefix would accept the
806
848
  * signature for any longer message that shares it. Every value here is
807
849
  * public, so the answer may be reached by branches.
850
+ *
851
+ * @param pub The signer's 32-byte encoded public key.
852
+ * @param msg The whole message.
853
+ * @param sig The 64-byte signature `R || S`.
854
+ * @returns `true` when the signature is valid, `false` for anything else.
808
855
  */
809
856
  export const ed25519Verify = (pub: u8[], msg: u8[], sig: u8[]): boolean => {
810
857
  if (msg.length > 2147483647) {
@@ -136,10 +136,24 @@ const hkdfAppend = (out: u8[], at: i32, block: u8[]): i32 => {
136
136
  return at + k
137
137
  }
138
138
 
139
- /** HKDF-Extract with HMAC-SHA-256 (RFC 5869 §2.2): the 32-byte PRK. */
139
+ /**
140
+ * HKDF-Extract with HMAC-SHA-256 (RFC 5869 §2.2): the 32-byte PRK.
141
+ *
142
+ * @param salt The public salt; an empty one stands for HashLen zero bytes
143
+ * (§2.2).
144
+ * @param ikm The input keying material; it is read, not kept.
145
+ * @returns A fresh 32-byte PRK, which the caller owns and wipes.
146
+ */
140
147
  export const hkdfExtractSha256 = (salt: u8[], ikm: u8[]): u8[] => hmacSha256(hkdfSalt(salt, SHA256_SIZE), ikm)
141
148
 
142
- /** HKDF-Extract with HMAC-SHA-384 (RFC 5869 §2.2): the 48-byte PRK. */
149
+ /**
150
+ * HKDF-Extract with HMAC-SHA-384 (RFC 5869 §2.2): the 48-byte PRK.
151
+ *
152
+ * @param salt The public salt; an empty one stands for HashLen zero bytes
153
+ * (§2.2).
154
+ * @param ikm The input keying material; it is read, not kept.
155
+ * @returns A fresh 48-byte PRK, which the caller owns and wipes.
156
+ */
143
157
  export const hkdfExtractSha384 = (salt: u8[], ikm: u8[]): u8[] => hmacSha384(hkdfSalt(salt, SHA384_SIZE), ikm)
144
158
 
145
159
  /**
@@ -147,6 +161,12 @@ export const hkdfExtractSha384 = (salt: u8[], ikm: u8[]): u8[] => hmacSha384(hkd
147
161
  * keying material from `prk` and `info`, or `null` when `length` is negative
148
162
  * or past 255 × 32, when `info` is longer than 2^31 - 1 bytes, or when `prk`
149
163
  * is shorter than 32 bytes.
164
+ *
165
+ * @param prk The pseudorandom key, at least 32 bytes; it is read, not kept.
166
+ * @param info The public context the output is bound to; may be empty.
167
+ * @param length How many bytes to derive, 0 to 8160.
168
+ * @returns A fresh array of `length` bytes, which the caller owns and wipes, or
169
+ * `null` for bad input.
150
170
  */
151
171
  export const hkdfExpandSha256 = (prk: u8[], info: u8[], length: i32): u8[] | null => {
152
172
  if (
@@ -184,6 +204,12 @@ export const hkdfExpandSha256 = (prk: u8[], info: u8[], length: i32): u8[] | nul
184
204
  * keying material from `prk` and `info`, or `null` when `length` is negative
185
205
  * or past 255 × 48, when `info` is longer than 2^31 - 1 bytes, or when `prk`
186
206
  * is shorter than 48 bytes.
207
+ *
208
+ * @param prk The pseudorandom key, at least 48 bytes; it is read, not kept.
209
+ * @param info The public context the output is bound to; may be empty.
210
+ * @param length How many bytes to derive, 0 to 12240.
211
+ * @returns A fresh array of `length` bytes, which the caller owns and wipes, or
212
+ * `null` for bad input.
187
213
  */
188
214
  export const hkdfExpandSha384 = (prk: u8[], info: u8[], length: i32): u8[] | null => {
189
215
  if (
@@ -247,21 +273,43 @@ const hkdfExpandSha256Exposed = (prk: u8[], request: HkdfExpandRequest): u8[] |
247
273
  const hkdfExpandSha384Exposed = (prk: u8[], request: HkdfExpandRequest): u8[] | null =>
248
274
  hkdfExpandSha384(prk, request.info, request.length)
249
275
 
250
- /** `hkdfExtractSha256` on input keying material held as a `Secret`: the 32-byte PRK, held as one. */
276
+ /**
277
+ * `hkdfExtractSha256` on input keying material held as a `Secret`.
278
+ *
279
+ * @param salt The public salt; an empty one stands for HashLen zero bytes
280
+ * (§2.2).
281
+ * @param ikm The input keying material, read only inside `exposeWith`; the
282
+ * caller still owns and wipes it.
283
+ * @returns The 32-byte PRK, held as a `Secret` the caller wipes.
284
+ */
251
285
  export const hkdfExtractSha256Secret = (salt: u8[], ikm: Secret<u8[]>): Secret<u8[]> => {
252
286
  const prk: u8[] = exposeWith(ikm, salt, hkdfExtractSha256Exposed)
253
287
  return secret(prk)
254
288
  }
255
289
 
256
- /** `hkdfExtractSha384` on input keying material held as a `Secret`: the 48-byte PRK, held as one. */
290
+ /**
291
+ * `hkdfExtractSha384` on input keying material held as a `Secret`.
292
+ *
293
+ * @param salt The public salt; an empty one stands for HashLen zero bytes
294
+ * (§2.2).
295
+ * @param ikm The input keying material, read only inside `exposeWith`; the
296
+ * caller still owns and wipes it.
297
+ * @returns The 48-byte PRK, held as a `Secret` the caller wipes.
298
+ */
257
299
  export const hkdfExtractSha384Secret = (salt: u8[], ikm: Secret<u8[]>): Secret<u8[]> => {
258
300
  const prk: u8[] = exposeWith(ikm, salt, hkdfExtractSha384Exposed)
259
301
  return secret(prk)
260
302
  }
261
303
 
262
304
  /**
263
- * `hkdfExpandSha256` on a PRK held as a `Secret`: the output keying material,
264
- * held as one, or `null` where `hkdfExpandSha256` answers `null`.
305
+ * `hkdfExpandSha256` on a PRK held as a `Secret`.
306
+ *
307
+ * @param prk The pseudorandom key, at least 32 bytes, read only inside
308
+ * `exposeWith`.
309
+ * @param info The public context the output is bound to; may be empty.
310
+ * @param length How many bytes to derive, 0 to 8160.
311
+ * @returns The `length` bytes, held as a `Secret` the caller wipes, or `null`
312
+ * for bad input.
265
313
  */
266
314
  export const hkdfExpandSha256Secret = (prk: Secret<u8[]>, info: u8[], length: i32): Secret<u8[]> | null => {
267
315
  const request: HkdfExpandRequest = { info: info, length: length }
@@ -273,8 +321,14 @@ export const hkdfExpandSha256Secret = (prk: Secret<u8[]>, info: u8[], length: i3
273
321
  }
274
322
 
275
323
  /**
276
- * `hkdfExpandSha384` on a PRK held as a `Secret`: the output keying material,
277
- * held as one, or `null` where `hkdfExpandSha384` answers `null`.
324
+ * `hkdfExpandSha384` on a PRK held as a `Secret`.
325
+ *
326
+ * @param prk The pseudorandom key, at least 48 bytes, read only inside
327
+ * `exposeWith`.
328
+ * @param info The public context the output is bound to; may be empty.
329
+ * @param length How many bytes to derive, 0 to 12240.
330
+ * @returns The `length` bytes, held as a `Secret` the caller wipes, or `null`
331
+ * for bad input.
278
332
  */
279
333
  export const hkdfExpandSha384Secret = (prk: Secret<u8[]>, info: u8[], length: i32): Secret<u8[]> | null => {
280
334
  const request: HkdfExpandRequest = { info: info, length: length }
@@ -289,6 +343,15 @@ export const hkdfExpandSha384Secret = (prk: Secret<u8[]>, info: u8[], length: i3
289
343
  * HKDF with HMAC-SHA-256, both steps (RFC 5869 §2): `length` bytes from `salt`,
290
344
  * the `Secret` input keying material and `info`, held as a `Secret`, or `null`
291
345
  * where `hkdfExpandSha256` answers `null`. The PRK is wiped before it returns.
346
+ *
347
+ * @param salt The public salt; an empty one stands for HashLen zero bytes
348
+ * (§2.2).
349
+ * @param ikm The input keying material, read only inside `exposeWith`; the
350
+ * caller still owns and wipes it.
351
+ * @param info The public context the output is bound to; may be empty.
352
+ * @param length How many bytes to derive, 0 to 8160.
353
+ * @returns The `length` bytes, held as a `Secret` the caller wipes, or `null`
354
+ * for bad input.
292
355
  */
293
356
  export const hkdfSha256Secret = (
294
357
  salt: u8[],
@@ -302,7 +365,19 @@ export const hkdfSha256Secret = (
302
365
  return okm
303
366
  }
304
367
 
305
- /** `hkdfSha256Secret` with HMAC-SHA-384: the PRK is 48 bytes, and wiped before it returns. */
368
+ /**
369
+ * `hkdfSha256Secret` with HMAC-SHA-384: the PRK is 48 bytes, and wiped before
370
+ * it returns.
371
+ *
372
+ * @param salt The public salt; an empty one stands for HashLen zero bytes
373
+ * (§2.2).
374
+ * @param ikm The input keying material, read only inside `exposeWith`; the
375
+ * caller still owns and wipes it.
376
+ * @param info The public context the output is bound to; may be empty.
377
+ * @param length How many bytes to derive, 0 to 12240.
378
+ * @returns The `length` bytes, held as a `Secret` the caller wipes, or `null`
379
+ * for bad input.
380
+ */
306
381
  export const hkdfSha384Secret = (
307
382
  salt: u8[],
308
383
  ikm: Secret<u8[]>,
@@ -359,6 +434,13 @@ const hkdfLabel = (caller: string, label: string, context: u8[], length: i32): u
359
434
  * HKDF-Expand-Label with HMAC-SHA-256 (RFC 8446 §7.1): `length` bytes from
360
435
  * `prk` (RFC 8446's Secret), `"tls13 " + label` and `context`. Panics on an argument out of the
361
436
  * range the module docs give, a secret shorter than 32 bytes among them.
437
+ *
438
+ * @param prk RFC 8446's Secret, at least 32 bytes; it is read, not kept.
439
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
440
+ * @param context The public context, often a transcript hash; at most 255
441
+ * bytes.
442
+ * @param length How many bytes to derive, 0 to 255.
443
+ * @returns A fresh array of `length` bytes, which the caller owns.
362
444
  */
363
445
  export const hkdfExpandLabelSha256 = (prk: u8[], label: string, context: u8[], length: i32): u8[] => {
364
446
  const okm: u8[] | null = hkdfExpandSha256(
@@ -376,6 +458,13 @@ export const hkdfExpandLabelSha256 = (prk: u8[], label: string, context: u8[], l
376
458
  * HKDF-Expand-Label with HMAC-SHA-384 (RFC 8446 §7.1): `length` bytes from
377
459
  * `prk` (RFC 8446's Secret), `"tls13 " + label` and `context`. Panics on an argument out of the
378
460
  * range the module docs give, a secret shorter than 48 bytes among them.
461
+ *
462
+ * @param prk RFC 8446's Secret, at least 48 bytes; it is read, not kept.
463
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
464
+ * @param context The public context, often a transcript hash; at most 255
465
+ * bytes.
466
+ * @param length How many bytes to derive, 0 to 255.
467
+ * @returns A fresh array of `length` bytes, which the caller owns.
379
468
  */
380
469
  export const hkdfExpandLabelSha384 = (prk: u8[], label: string, context: u8[], length: i32): u8[] => {
381
470
  const okm: u8[] | null = hkdfExpandSha384(
@@ -394,6 +483,14 @@ export const hkdfExpandLabelSha384 = (prk: u8[], label: string, context: u8[], l
394
483
  * held as one. The label is built outside `expose`, since the label and the
395
484
  * context are not secret, and the PRK is read only inside it. Panics as
396
485
  * `hkdfExpandLabelSha256` does.
486
+ *
487
+ * @param prk RFC 8446's Secret, at least 32 bytes, read only inside
488
+ * `exposeWith`.
489
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
490
+ * @param context The public context, often a transcript hash; at most 255
491
+ * bytes.
492
+ * @param length How many bytes to derive, 0 to 255.
493
+ * @returns The `length` bytes, held as a `Secret` the caller wipes.
397
494
  */
398
495
  export const hkdfExpandLabelSha256Secret = (
399
496
  prk: Secret<u8[]>,
@@ -412,7 +509,18 @@ export const hkdfExpandLabelSha256Secret = (
412
509
  return okm
413
510
  }
414
511
 
415
- /** `hkdfExpandLabelSha256Secret` with HMAC-SHA-384: panics as `hkdfExpandLabelSha384` does. */
512
+ /**
513
+ * `hkdfExpandLabelSha256Secret` with HMAC-SHA-384: panics as
514
+ * `hkdfExpandLabelSha384` does.
515
+ *
516
+ * @param prk RFC 8446's Secret, at least 48 bytes, read only inside
517
+ * `exposeWith`.
518
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
519
+ * @param context The public context, often a transcript hash; at most 255
520
+ * bytes.
521
+ * @param length How many bytes to derive, 0 to 255.
522
+ * @returns The `length` bytes, held as a `Secret` the caller wipes.
523
+ */
416
524
  export const hkdfExpandLabelSha384Secret = (
417
525
  prk: Secret<u8[]>,
418
526
  label: string,
@@ -442,15 +550,35 @@ const HKDF_LABEL_MAX: i32 = 514
442
550
  * hand it to every derivation; it carries nothing from one call to the next.
443
551
  */
444
552
  export class HkdfScratch {
553
+ /**
554
+ * The HMAC the `*Into` functions run when `hashLength` is 32; wiped before
555
+ * a call that ran it returns.
556
+ */
445
557
  sha256: HmacSha256Scratch
558
+ /**
559
+ * The HMAC the `*Into` functions run when `hashLength` is 48; wiped before
560
+ * a call that ran it returns.
561
+ */
446
562
  sha384: HmacSha384Scratch
447
563
  /** T(i - 1), HashLen bytes of it; wiped before every function returns. */
448
564
  previous: u8[]
449
- /** The one-byte block counter `i`. */
565
+ /**
566
+ * The one-byte block counter `i`.
567
+ *
568
+ * @internal Written by this module's `*Into` functions.
569
+ */
450
570
  counter: u8[]
451
- /** HKDF-Expand-Label's `info`, built here: public, so it is not wiped. */
571
+ /**
572
+ * HKDF-Expand-Label's `info`, built here: public, so it is not wiped.
573
+ *
574
+ * @internal Written by this module's `*Into` functions.
575
+ */
452
576
  label: u8[]
453
577
 
578
+ /**
579
+ * Allocates the HMAC scratch for both hashes and the buffers every derivation
580
+ * reuses.
581
+ */
454
582
  constructor() {
455
583
  this.sha256 = new HmacSha256Scratch()
456
584
  this.sha384 = new HmacSha384Scratch()
@@ -524,6 +652,14 @@ const hkdfMacFinish = (s: HkdfScratch, hashLength: i32, out: u8[], at: i32): voi
524
652
  * which HMAC pads to the block with zeros, exactly as the HashLen zeros §2.2
525
653
  * asks for would be. An `ikm` longer than 2^31 - 1 bytes panics, as
526
654
  * `hmacSha256` does.
655
+ *
656
+ * @param s The scratch to compute in; what the call leaves in it is wiped.
657
+ * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
658
+ * @param salt The public salt; an empty one stands for HashLen zero bytes
659
+ * (§2.2).
660
+ * @param ikm The input keying material; it is read, not kept.
661
+ * @param out The caller's array the PRK is written into.
662
+ * @param at Where the `hashLength` PRK bytes start in `out`.
527
663
  */
528
664
  export const hkdfExtractInto = (
529
665
  s: HkdfScratch,
@@ -586,6 +722,18 @@ const hkdfExpandWindow = (
586
722
  * where `hkdfExpandSha256` answers `null`: a `length` negative or past
587
723
  * 255 × HashLen, an `info` longer than 2^31 - 1 bytes, or a `prk` shorter than
588
724
  * HashLen.
725
+ *
726
+ * @param s The scratch to compute in; what the call leaves in it is wiped.
727
+ * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
728
+ * @param prk The pseudorandom key, at least `hashLength` bytes; it is read, not
729
+ * kept.
730
+ * @param info The public context the output is bound to; may be empty.
731
+ * @param out The caller's array the output is written into.
732
+ * @param at Where the output starts in `out`.
733
+ * @param length How many bytes to derive, 0 to 255 × `hashLength`.
734
+ * @returns `true` once the bytes are written; `false`, with `out` untouched,
735
+ * for bad input. A `hashLength` other than 32 or 48, or an output window
736
+ * outside `out`, panics instead, before anything is written.
589
737
  */
590
738
  export const hkdfExpandInto = (
591
739
  s: HkdfScratch,
@@ -617,6 +765,18 @@ export const hkdfExpandInto = (
617
765
  * panics where `hkdfExpandLabelSha256` does — a length outside 0 to 255, a
618
766
  * label empty or past 249 bytes, a context past 255, a secret shorter than
619
767
  * HashLen — and on a context window outside its array.
768
+ *
769
+ * @param s The scratch to compute in; what the call leaves in it is wiped.
770
+ * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
771
+ * @param prk RFC 8446's Secret, at least `hashLength` bytes; it is read, not
772
+ * kept.
773
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
774
+ * @param context The buffer holding the context.
775
+ * @param contextOff Where the context starts in `context`.
776
+ * @param contextLen The context's length, at most 255 bytes.
777
+ * @param out The caller's array the output is written into.
778
+ * @param at Where the output starts in `out`.
779
+ * @param length How many bytes to derive, 0 to 255.
620
780
  */
621
781
  export const hkdfExpandLabelInto = (
622
782
  s: HkdfScratch,
@@ -743,6 +903,13 @@ const hkdfHeld = (
743
903
  * derived from `prk`, the label and `context` and written over `dst`'s in
744
904
  * place. `dst` may be `prk` itself, since the output is whole before it is
745
905
  * copied: that is a ratchet. Panics as `hkdfExpandLabelSha256` does.
906
+ *
907
+ * @param dst The `Secret` to overwrite; its length is the length derived. It
908
+ * may be `prk`.
909
+ * @param prk RFC 8446's Secret, read only inside `exposeWith`.
910
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
911
+ * @param context The public context, often a transcript hash; at most 255
912
+ * bytes.
746
913
  */
747
914
  export const hkdfExpandLabelSha256Held = (
748
915
  dst: Secret<u8[]>,
@@ -753,7 +920,17 @@ export const hkdfExpandLabelSha256Held = (
753
920
  hkdfHeld("hkdfExpandLabelSha256Held", SHA256_SIZE, dst, prk, label, context)
754
921
  }
755
922
 
756
- /** `hkdfExpandLabelSha256Held` with HMAC-SHA-384: panics as `hkdfExpandLabelSha384` does. */
923
+ /**
924
+ * `hkdfExpandLabelSha256Held` with HMAC-SHA-384: panics as
925
+ * `hkdfExpandLabelSha384` does.
926
+ *
927
+ * @param dst The `Secret` to overwrite; its length is the length derived. It
928
+ * may be `prk`.
929
+ * @param prk RFC 8446's Secret, read only inside `exposeWith`.
930
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
931
+ * @param context The public context, often a transcript hash; at most 255
932
+ * bytes.
933
+ */
757
934
  export const hkdfExpandLabelSha384Held = (
758
935
  dst: Secret<u8[]>,
759
936
  prk: Secret<u8[]>,
@@ -768,6 +945,14 @@ export const hkdfExpandLabelSha384Held = (
768
945
  * plain array: `length` bytes written at `out[at]`. This is for output that is
769
946
  * kept plain by design, a record layer's AEAD key and IV, say, which live in
770
947
  * the arrays the cipher reads. Panics as `hkdfExpandLabelInto` does.
948
+ *
949
+ * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
950
+ * @param prk RFC 8446's Secret, read only inside `exposeWith`.
951
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
952
+ * @param context The public context, at most 255 bytes.
953
+ * @param out The caller's plain array the output is written into.
954
+ * @param at Where the output starts in `out`.
955
+ * @param length How many bytes to derive, 0 to 255.
771
956
  */
772
957
  export const hkdfExpandLabelHeldInto = (
773
958
  hashLength: i32,