@amritk/nish-aarch64-linux 0.50.0 → 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 (54) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +4 -1
  3. package/package.json +1 -1
  4. package/runtime/nish.d.ts +82 -2
  5. package/runtime/nish.h +5 -0
  6. package/runtime/runtime-host.c +21 -5
  7. package/std/README.md +7 -7
  8. package/std/base64.ts +61 -95
  9. package/std/bytes.ts +6 -4
  10. package/std/cli-args.ts +150 -107
  11. package/std/compress/checksum.ts +68 -10
  12. package/std/compress/deflate.ts +193 -100
  13. package/std/compress/gzip.ts +124 -49
  14. package/std/compress/zlib.ts +57 -15
  15. package/std/crypto/aes.ts +51 -9
  16. package/std/crypto/ed25519.ts +12 -10
  17. package/std/crypto/hkdf.ts +405 -283
  18. package/std/crypto/hmac.ts +483 -148
  19. package/std/crypto/jwt.ts +19 -21
  20. package/std/crypto/p256.ts +5 -7
  21. package/std/crypto/pki-roots.ts +28 -8
  22. package/std/crypto/rsa-sign.ts +5 -2
  23. package/std/crypto/sha256.ts +81 -26
  24. package/std/crypto/sha512.ts +135 -49
  25. package/std/json-value.ts +14 -15
  26. package/std/net/buffer-pool.ts +25 -14
  27. package/std/net/fetch.ts +108 -27
  28. package/std/net/handler.ts +11 -22
  29. package/std/net/hpack.ts +38 -19
  30. package/std/net/http-compress.ts +21 -12
  31. package/std/net/http-server.ts +9 -4
  32. package/std/net/http1-client-codec.ts +22 -15
  33. package/std/net/http1-codec.ts +37 -36
  34. package/std/net/http3-server.ts +127 -45
  35. package/std/net/http3.ts +99 -50
  36. package/std/net/postgres-async.ts +5 -10
  37. package/std/net/qpack.ts +83 -17
  38. package/std/net/quic-listener.ts +42 -27
  39. package/std/net/quic-packet.ts +91 -50
  40. package/std/net/quic-stream.ts +29 -22
  41. package/std/net/quic.ts +48 -23
  42. package/std/net/static.ts +5 -3
  43. package/std/net/tls/client.ts +81 -34
  44. package/std/net/tls/codec.ts +3 -4
  45. package/std/net/tls/record.ts +54 -22
  46. package/std/net/tls/schedule.ts +339 -149
  47. package/std/net/tls.ts +42 -12
  48. package/std/net/upstream.ts +18 -19
  49. package/std/net/url.ts +78 -21
  50. package/std/net/websocket.ts +3 -11
  51. package/std/net/webtransport.ts +95 -21
  52. package/std/raster.ts +12 -2
  53. package/std/term-pixels.ts +18 -2
  54. package/std/text.ts +5 -5
@@ -36,34 +36,31 @@
36
36
  *
37
37
  * const key: u8[] = hkdfExpandLabelSha256(secret, "quic key", [], 16);
38
38
  *
39
- * `label` is given without the `"tls13 "` prefix, which the function adds. Its
40
- * arguments come from the program rather than from the peer — a label is a
41
- * literal, a length a key or hash size, a context a transcript hash — so one
42
- * out of range is a bug in the caller, and it panics rather than answering
43
- * `null`: a length below 0 or above 255, a label empty or longer than 249
44
- * bytes (the struct's 7 to 255 less the prefix), a context longer than 255
45
- * bytes, and a secret shorter than HashLen.
39
+ * `label` is given without the `"tls13 "` prefix, which the function adds. An
40
+ * argument out of range answers `null`: a length below 0 or above 255, a
41
+ * label empty or longer than 249 bytes (the struct's 7 to 255 less the
42
+ * prefix), a context longer than 255 bytes, and a secret shorter than HashLen.
46
43
  *
47
44
  * **Caller-owned scratch.** The functions above make their HMAC objects and
48
45
  * their output fresh on every call, which stores allocations no arena scope
49
46
  * may release around them. `hkdfExtractInto`, `hkdfExpandInto` and
50
47
  * `hkdfExpandLabelInto` compute the same bytes in an `HkdfScratch` the caller
51
- * allocates once, over the hash `hashLength` names (32 for SHA-256, 48 for
52
- * SHA-384), and write them into the caller's array, so that a loop of key
53
- * derivations inside one `using a = arena()` block leaves the arena where it
54
- * was. Each wipes the scratch before it returns, its last block and the
55
- * keyed hashers of the hash it ran over, so no key material outlives the
56
- * call there:
57
- *
58
- * const kdf = new HkdfScratch(); // once
59
- * hkdfExtractInto(kdf, 32, salt, ikm, prk, 0); // 32 bytes at prk[0]
60
- * hkdfExpandLabelInto(kdf, 32, prk, "key", context, 0, 0, key, 0, 16);
61
- *
62
- * They refuse what the functions above refuse, the same way: `hkdfExpandInto`
63
- * answers `false` where `hkdfExpandSha256` answers `null`, and
64
- * `hkdfExpandLabelInto` panics where `hkdfExpandLabelSha256` does. A hash
65
- * length other than 32 or 48, or an output window outside its array, is the
66
- * caller's bug and panics.
48
+ * allocates once, over the hash an `HkdfHash` names, and write them into the
49
+ * caller's array, so that a loop of key derivations inside one
50
+ * `using a = arena()` block leaves the arena where it was. Each wipes the
51
+ * scratch before it returns, its last block and the keyed hashers of the hash
52
+ * it ran over, so no key material outlives the call there:
53
+ *
54
+ * const kdf = new HkdfScratch(); // once
55
+ * hkdfExtractInto(kdf, HkdfHash.Sha256, salt, ikm, prk, 0); // 32 bytes at prk[0]
56
+ * hkdfExpandLabelInto(kdf, HkdfHash.Sha256, prk, "key", context, 0, 0, key, 0, 16);
57
+ *
58
+ * Each answers `true` once its bytes are written, and `false`, writing
59
+ * nothing, where the functions above answer `null`, or for an output window
60
+ * outside its array. The hash is an enum, so a hash HKDF does not run over
61
+ * here does not compile. The `try` names (`tryHkdfExtractInto`,
62
+ * `tryHkdfExpandLabelInto`) are the same functions, kept from when the plain
63
+ * names panicked.
67
64
  *
68
65
  * **What the key reaches is wiped** (CLAUDE.md, "Security"). Every HMAC on
69
66
  * the way wipes its key blocks and hashers (`nish/crypto/hmac`), and `expand`
@@ -83,10 +80,13 @@
83
80
  * it with the `*Held` forms, which fill as many bytes as the `Secret` holds,
84
81
  * in place, and wipe the plain output they copied from. The destination may
85
82
  * be the PRK itself; `hkdfExpandLabelHeldInto` writes a plain window instead,
86
- * for output that is plain by design, a record layer's AEAD key and IV:
83
+ * for output that is plain by design, a record layer's AEAD key and IV. They
84
+ * answer `false`, writing nothing, where the forms above answer `null` or
85
+ * `false`; their `try` names (`tryHkdfExpandLabelSha256Held`, …) are the same
86
+ * functions:
87
87
  *
88
88
  * hkdfExpandLabelSha256Held(secret, secret, "traffic upd", none);
89
- * hkdfExpandLabelHeldInto(32, secret, "key", none, key, 0, 16);
89
+ * hkdfExpandLabelHeldInto(HkdfHash.Sha256, secret, "key", none, key, 0, 16);
90
90
  *
91
91
  * Written from RFC 5869 and RFC 8446 §7.1, not ported from another
92
92
  * implementation.
@@ -190,23 +190,26 @@ export const hkdfExpandSha256 = (prk: u8[], info: u8[], length: i32): u8[] | nul
190
190
  ) {
191
191
  return null
192
192
  }
193
- // At most 255 blocks of 48 bytes, so `toU16` keeps every length exactly.
193
+ // At most 255 blocks of 32 bytes, so `toU16` keeps every length exactly.
194
194
  const out: u8[] = new Array<u8>(toU16(length))
195
195
  const counter: u8[] = new Array<u8>(1)
196
- const counterLength: i32 = 1
197
196
  let previous: u8[] = []
198
197
  let at: i32 = 0
199
198
  // At most 255, so the byte below is the counter itself.
200
199
  let i: u32 = 1
201
200
  while (at < length) {
202
201
  const mac = new HmacSha256(prk)
203
- mac.update(previous, HKDF_FROM, toI32(previous.length))
204
- mac.update(info, HKDF_FROM, toI32(info.length))
202
+ // A fresh `mac` each block, its tag not yet taken, and each array whole:
203
+ // `update` past its tests.
204
+ mac.absorbWhole(previous)
205
+ mac.absorbWhole(info)
205
206
  if (toI32(counter.length) >= 1) {
206
207
  counter[0] = toU8(i)
207
208
  }
208
- mac.update(counter, HKDF_FROM, counterLength)
209
- const block: u8[] = mac.digest()
209
+ mac.absorbWhole(counter)
210
+ // A fresh `mac` each block, its tag not yet taken: `digest` past its test.
211
+ const block: u8[] = new Array<u8>(SHA256_SIZE)
212
+ mac.tagInto(block, HKDF_FROM)
210
213
  wipe(previous)
211
214
  previous = block
212
215
  at = hkdfAppend(out, at, previous)
@@ -240,20 +243,22 @@ export const hkdfExpandSha384 = (prk: u8[], info: u8[], length: i32): u8[] | nul
240
243
  // At most 255 blocks of 48 bytes, so `toU16` keeps every length exactly.
241
244
  const out: u8[] = new Array<u8>(toU16(length))
242
245
  const counter: u8[] = new Array<u8>(1)
243
- const counterLength: i32 = 1
244
246
  let previous: u8[] = []
245
247
  let at: i32 = 0
246
248
  // At most 255, so the byte below is the counter itself.
247
249
  let i: u32 = 1
248
250
  while (at < length) {
249
251
  const mac = new HmacSha384(prk)
250
- mac.update(previous, HKDF_FROM, toI32(previous.length))
251
- mac.update(info, HKDF_FROM, toI32(info.length))
252
+ // A fresh `mac` each block, its tag not yet taken, and each array whole:
253
+ // `update` past its tests.
254
+ mac.absorbWhole(previous)
255
+ mac.absorbWhole(info)
252
256
  if (toI32(counter.length) >= 1) {
253
257
  counter[0] = toU8(i)
254
258
  }
255
- mac.update(counter, HKDF_FROM, counterLength)
256
- const block: u8[] = mac.digest()
259
+ mac.absorbWhole(counter)
260
+ // A fresh `mac` each block, its tag not yet taken: `digest` past its test.
261
+ const block: u8[] = mac.tag()
257
262
  wipe(previous)
258
263
  previous = block
259
264
  at = hkdfAppend(out, at, previous)
@@ -413,21 +418,15 @@ export const hkdfSha384Secret = (
413
418
 
414
419
  /**
415
420
  * The `HkdfLabel` struct of RFC 8446 §7.1, the `info` HKDF-Expand-Label hands
416
- * to `expand`, or a panic naming `caller` when `length`, `label` or `context`
417
- * is outside the range the module docs give: 249 is `label<7..255>`'s 255 less
418
- * the prefix's 6 bytes. The lengths of `label` and `context` are compared as
419
- * the `number`s they are, before any `toI32`, which would saturate them under
421
+ * to `expand`, or `null` when `length`, `label` or `context` is outside the
422
+ * range the module docs give: 249 is `label<7..255>`'s 255 less the prefix's 6
423
+ * bytes. The lengths of `label` and `context` are compared as the `number`s
424
+ * they are, before any `toI32`, which would saturate them under
420
425
  * `--number-mode f64`; so the bounds are literals rather than `i32` constants.
421
426
  */
422
- const hkdfLabel = (caller: string, label: string, context: u8[], length: i32): u8[] => {
423
- if (length < 0 || length > 255) {
424
- panic(`${caller}: a length of ${length} bytes, outside 0 to 255`)
425
- }
426
- if (label.length === 0 || label.length > 249) {
427
- panic(`${caller}: a label of ${toI32(label.length)} bytes, outside 1 to 249`)
428
- }
429
- if (context.length > 255) {
430
- panic(`${caller}: a context of more than 255 bytes`)
427
+ const hkdfLabelOrNull = (label: string, context: u8[], length: i32): u8[] | null => {
428
+ if (length < 0 || length > 255 || label.length === 0 || label.length > 249 || context.length > 255) {
429
+ return null
431
430
  }
432
431
  const full: string = `${HKDF_LABEL_PREFIX}${label}`
433
432
  const fullLength: i32 = toI32(full.length)
@@ -449,57 +448,47 @@ const hkdfLabel = (caller: string, label: string, context: u8[], length: i32): u
449
448
 
450
449
  /**
451
450
  * HKDF-Expand-Label with HMAC-SHA-256 (RFC 8446 §7.1): `length` bytes from
452
- * `prk` (RFC 8446's Secret), `"tls13 " + label` and `context`. Panics on an argument out of the
453
- * range the module docs give, a secret shorter than 32 bytes among them.
451
+ * `prk` (RFC 8446's Secret), `"tls13 " + label` and `context`, or `null` for
452
+ * an argument out of the range the module docs give, a secret shorter than
453
+ * 32 bytes among them.
454
454
  *
455
455
  * @param prk RFC 8446's Secret, at least 32 bytes; it is read, not kept.
456
456
  * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
457
457
  * @param context The public context, often a transcript hash; at most 255
458
458
  * bytes.
459
459
  * @param length How many bytes to derive, 0 to 255.
460
- * @returns A fresh array of `length` bytes, which the caller owns.
461
- */
462
- export const hkdfExpandLabelSha256 = (prk: u8[], label: string, context: u8[], length: i32): u8[] => {
463
- const okm: u8[] | null = hkdfExpandSha256(
464
- prk,
465
- hkdfLabel("hkdfExpandLabelSha256", label, context, length),
466
- length
467
- )
468
- if (okm === null) {
469
- panic("hkdfExpandLabelSha256: a secret shorter than 32 bytes")
470
- }
471
- return okm
460
+ * @returns A fresh array of `length` bytes, which the caller owns, or `null`
461
+ * for an argument out of range.
462
+ */
463
+ export const hkdfExpandLabelSha256 = (prk: u8[], label: string, context: u8[], length: i32): u8[] | null => {
464
+ const info: u8[] | null = hkdfLabelOrNull(label, context, length)
465
+ return info === null ? null : hkdfExpandSha256(prk, info, length)
472
466
  }
473
467
 
474
468
  /**
475
469
  * HKDF-Expand-Label with HMAC-SHA-384 (RFC 8446 §7.1): `length` bytes from
476
- * `prk` (RFC 8446's Secret), `"tls13 " + label` and `context`. Panics on an argument out of the
477
- * range the module docs give, a secret shorter than 48 bytes among them.
470
+ * `prk` (RFC 8446's Secret), `"tls13 " + label` and `context`, or `null` for
471
+ * an argument out of the range the module docs give, a secret shorter than
472
+ * 48 bytes among them.
478
473
  *
479
474
  * @param prk RFC 8446's Secret, at least 48 bytes; it is read, not kept.
480
475
  * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
481
476
  * @param context The public context, often a transcript hash; at most 255
482
477
  * bytes.
483
478
  * @param length How many bytes to derive, 0 to 255.
484
- * @returns A fresh array of `length` bytes, which the caller owns.
485
- */
486
- export const hkdfExpandLabelSha384 = (prk: u8[], label: string, context: u8[], length: i32): u8[] => {
487
- const okm: u8[] | null = hkdfExpandSha384(
488
- prk,
489
- hkdfLabel("hkdfExpandLabelSha384", label, context, length),
490
- length
491
- )
492
- if (okm === null) {
493
- panic("hkdfExpandLabelSha384: a secret shorter than 48 bytes")
494
- }
495
- return okm
479
+ * @returns A fresh array of `length` bytes, which the caller owns, or `null`
480
+ * for an argument out of range.
481
+ */
482
+ export const hkdfExpandLabelSha384 = (prk: u8[], label: string, context: u8[], length: i32): u8[] | null => {
483
+ const info: u8[] | null = hkdfLabelOrNull(label, context, length)
484
+ return info === null ? null : hkdfExpandSha384(prk, info, length)
496
485
  }
497
486
 
498
487
  /**
499
488
  * `hkdfExpandLabelSha256` on a secret held as a `Secret`: the `length` bytes,
500
489
  * held as one. The label is built outside `expose`, since the label and the
501
- * context are not secret, and the PRK is read only inside it. Panics as
502
- * `hkdfExpandLabelSha256` does.
490
+ * context are not secret, and the PRK is read only inside it. `null` where
491
+ * `hkdfExpandLabelSha256` answers `null`.
503
492
  *
504
493
  * @param prk RFC 8446's Secret, at least 32 bytes, read only inside
505
494
  * `exposeWith`.
@@ -507,28 +496,22 @@ export const hkdfExpandLabelSha384 = (prk: u8[], label: string, context: u8[], l
507
496
  * @param context The public context, often a transcript hash; at most 255
508
497
  * bytes.
509
498
  * @param length How many bytes to derive, 0 to 255.
510
- * @returns The `length` bytes, held as a `Secret` the caller wipes.
499
+ * @returns The `length` bytes, held as a `Secret` the caller wipes, or
500
+ * `null` for an argument out of range.
511
501
  */
512
502
  export const hkdfExpandLabelSha256Secret = (
513
503
  prk: Secret<u8[]>,
514
504
  label: string,
515
505
  context: u8[],
516
506
  length: i32
517
- ): Secret<u8[]> => {
518
- const okm: Secret<u8[]> | null = hkdfExpandSha256Secret(
519
- prk,
520
- hkdfLabel("hkdfExpandLabelSha256Secret", label, context, length),
521
- length
522
- )
523
- if (okm === null) {
524
- panic("hkdfExpandLabelSha256Secret: a secret shorter than 32 bytes")
525
- }
526
- return okm
507
+ ): Secret<u8[]> | null => {
508
+ const info: u8[] | null = hkdfLabelOrNull(label, context, length)
509
+ return info === null ? null : hkdfExpandSha256Secret(prk, info, length)
527
510
  }
528
511
 
529
512
  /**
530
- * `hkdfExpandLabelSha256Secret` with HMAC-SHA-384: panics as
531
- * `hkdfExpandLabelSha384` does.
513
+ * `hkdfExpandLabelSha256Secret` with HMAC-SHA-384: `null` where
514
+ * `hkdfExpandLabelSha384` answers `null`.
532
515
  *
533
516
  * @param prk RFC 8446's Secret, at least 48 bytes, read only inside
534
517
  * `exposeWith`.
@@ -536,27 +519,42 @@ export const hkdfExpandLabelSha256Secret = (
536
519
  * @param context The public context, often a transcript hash; at most 255
537
520
  * bytes.
538
521
  * @param length How many bytes to derive, 0 to 255.
539
- * @returns The `length` bytes, held as a `Secret` the caller wipes.
522
+ * @returns The `length` bytes, held as a `Secret` the caller wipes, or
523
+ * `null` for an argument out of range.
540
524
  */
541
525
  export const hkdfExpandLabelSha384Secret = (
542
526
  prk: Secret<u8[]>,
543
527
  label: string,
544
528
  context: u8[],
545
529
  length: i32
546
- ): Secret<u8[]> => {
547
- const okm: Secret<u8[]> | null = hkdfExpandSha384Secret(
548
- prk,
549
- hkdfLabel("hkdfExpandLabelSha384Secret", label, context, length),
550
- length
551
- )
552
- if (okm === null) {
553
- panic("hkdfExpandLabelSha384Secret: a secret shorter than 48 bytes")
554
- }
555
- return okm
530
+ ): Secret<u8[]> | null => {
531
+ const info: u8[] | null = hkdfLabelOrNull(label, context, length)
532
+ return info === null ? null : hkdfExpandSha384Secret(prk, info, length)
556
533
  }
557
534
 
558
535
  // ---- Caller-owned scratch ----------------------------------------------------
559
536
 
537
+ /**
538
+ * The hash a scratch or held derivation runs over. An enum, not an `i32`, so
539
+ * a hash HKDF does not run over here is a compile error rather than a value
540
+ * the `*Into` and `*Held` functions must refuse.
541
+ */
542
+ export enum HkdfHash {
543
+ /** HKDF over HMAC-SHA-256: HashLen is 32 bytes. */
544
+ Sha256 = 1,
545
+ /** HKDF over HMAC-SHA-384: HashLen is 48 bytes. */
546
+ Sha384 = 2,
547
+ }
548
+
549
+ /**
550
+ * HashLen of `hash` in bytes: what an extract writes, and the most one block
551
+ * of expand gives.
552
+ *
553
+ * @param hash The hash to measure.
554
+ * @returns 32 for `HkdfHash.Sha256`, 48 for `HkdfHash.Sha384`.
555
+ */
556
+ export const hkdfHashLength = (hash: HkdfHash): i32 => (hash === HkdfHash.Sha384 ? SHA384_SIZE : SHA256_SIZE)
557
+
560
558
  /** The longest `HkdfLabel`: two length bytes, `label<7..255>` and `context<0..255>`, each with its length byte. */
561
559
  const HKDF_LABEL_MAX: i32 = 514
562
560
 
@@ -568,12 +566,12 @@ const HKDF_LABEL_MAX: i32 = 514
568
566
  */
569
567
  export class HkdfScratch {
570
568
  /**
571
- * The HMAC the `*Into` functions run when `hashLength` is 32; wiped before
569
+ * The HMAC the `*Into` functions run for `HkdfHash.Sha256`; wiped before
572
570
  * a call that ran it returns.
573
571
  */
574
572
  sha256: HmacSha256Scratch
575
573
  /**
576
- * The HMAC the `*Into` functions run when `hashLength` is 48; wiped before
574
+ * The HMAC the `*Into` functions run for `HkdfHash.Sha384`; wiped before
577
575
  * a call that ran it returns.
578
576
  */
579
577
  sha384: HmacSha384Scratch
@@ -612,112 +610,113 @@ export class HkdfScratch {
612
610
  }
613
611
  }
614
612
 
615
- /** Panics unless `hashLength` is 32 or 48, the two hashes HKDF runs over here. */
616
- const hkdfCheckHash = (caller: string, hashLength: i32): void => {
617
- if (hashLength !== SHA256_SIZE && hashLength !== SHA384_SIZE) {
618
- panic(`${caller}: a hash length of ${hashLength}, not 32 or 48`)
619
- }
620
- }
621
-
622
- /** Panics unless `out[at .. at + length)` is a window inside `out`. */
623
- const hkdfCheckOutput = (caller: string, out: u8[], at: i32, length: i32): void => {
624
- if (at < 0 || length < 0 || at > toI32(out.length) - length) {
625
- panic(`${caller}: the output window is outside its array`)
626
- }
627
- }
628
-
629
- /** Zeroes what the last derivation over `hashLength` left in `s`: T(i - 1) and that hash's keyed hashers. */
630
- const hkdfWipe = (s: HkdfScratch, hashLength: i32): void => {
613
+ /** Zeroes what the last derivation over `hash` left in `s`: T(i - 1) and that hash's keyed hashers. */
614
+ const hkdfWipe = (s: HkdfScratch, hash: HkdfHash): void => {
631
615
  secureZero(s.previous)
632
- if (hashLength === SHA384_SIZE) {
616
+ if (hash === HkdfHash.Sha384) {
633
617
  s.sha384.wipe()
634
618
  } else {
635
619
  s.sha256.wipe()
636
620
  }
637
621
  }
638
622
 
639
- /** Keys the scratch's HMAC for `hashLength` with `key[off .. off + len)`. */
640
- const hkdfMacBegin = (s: HkdfScratch, hashLength: i32, key: u8[], off: i32, len: i32): void => {
641
- if (hashLength === SHA384_SIZE) {
642
- s.sha384.begin(key, off, len)
643
- } else {
644
- s.sha256.begin(key, off, len)
645
- }
646
- }
623
+ /**
624
+ * Keys the scratch's HMAC for `hash` with `key[off .. off + len)`, or answers
625
+ * `false` for a key window outside `key`. This and the three helpers below
626
+ * answer what the HMAC scratch answers, and every caller passes the `false`
627
+ * on.
628
+ */
629
+ const hkdfMacBegin = (s: HkdfScratch, hash: HkdfHash, key: u8[], off: i32, len: i32): boolean =>
630
+ hash === HkdfHash.Sha384 ? s.sha384.begin(key, off, len) : s.sha256.begin(key, off, len)
647
631
 
648
- /** Feeds `data[off .. off + len)` to the scratch's HMAC for `hashLength`. */
649
- const hkdfMacUpdate = (s: HkdfScratch, hashLength: i32, data: u8[], off: i32, len: i32): void => {
650
- if (hashLength === SHA384_SIZE) {
651
- s.sha384.update(data, off, len)
652
- } else {
653
- s.sha256.update(data, off, len)
654
- }
655
- }
632
+ /** Feeds `data[off .. off + len)` to the scratch's HMAC for `hash`. */
633
+ const hkdfMacUpdate = (s: HkdfScratch, hash: HkdfHash, data: u8[], off: i32, len: i32): boolean =>
634
+ hash === HkdfHash.Sha384 ? s.sha384.update(data, off, len) : s.sha256.update(data, off, len)
656
635
 
657
- /** Feeds the whole of `data`, at any length, to the scratch's HMAC for `hashLength`. */
658
- const hkdfMacUpdateAll = (s: HkdfScratch, hashLength: i32, data: u8[]): void => {
659
- if (hashLength === SHA384_SIZE) {
660
- s.sha384.inner.engine.absorbAll(data)
661
- } else {
662
- s.sha256.inner.updateAll(data)
663
- }
664
- }
636
+ /** Feeds the whole of `data`, at any length, to the scratch's HMAC for `hash`. */
637
+ const hkdfMacUpdateAll = (s: HkdfScratch, hash: HkdfHash, data: u8[]): boolean =>
638
+ hash === HkdfHash.Sha384 ? s.sha384.inner.engine.absorbAll(data) : s.sha256.inner.updateAll(data)
665
639
 
666
- /** Writes the HMAC for `hashLength`'s tag at `out[at]`. */
667
- const hkdfMacFinish = (s: HkdfScratch, hashLength: i32, out: u8[], at: i32): void => {
668
- if (hashLength === SHA384_SIZE) {
669
- s.sha384.finishInto(out, at)
670
- } else {
671
- s.sha256.finishInto(out, at)
672
- }
673
- }
640
+ /** Writes the HMAC for `hash`'s tag at `out[at]`. */
641
+ const hkdfMacFinish = (s: HkdfScratch, hash: HkdfHash, out: u8[], at: i32): boolean =>
642
+ hash === HkdfHash.Sha384 ? s.sha384.finishInto(out, at) : s.sha256.finishInto(out, at)
674
643
 
675
644
  /**
676
- * HKDF-Extract (RFC 5869 §2.2) over the hash `hashLength` names, the PRK's
677
- * HashLen bytes written at `out[at]`. An empty `salt` keys HMAC with no bytes,
678
- * which HMAC pads to the block with zeros, exactly as the HashLen zeros §2.2
679
- * asks for would be. The whole `ikm` is hashed, at any length, as
680
- * `hmacSha256` hashes its message.
645
+ * HKDF-Extract (RFC 5869 §2.2) over `hash`, the PRK's HashLen bytes written
646
+ * at `out[at]`. An empty `salt` keys HMAC with no bytes, which HMAC pads to
647
+ * the block with zeros, exactly as the HashLen zeros §2.2 asks for would be.
648
+ * The whole `ikm` is hashed, at any length, as `hmacSha256` hashes its
649
+ * message.
681
650
  *
682
651
  * @param s The scratch to compute in; what the call leaves in it is wiped.
683
- * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
652
+ * @param hash The hash to run over.
684
653
  * @param salt The public salt; an empty one stands for HashLen zero bytes
685
654
  * (§2.2).
686
655
  * @param ikm The input keying material; it is read, not kept.
687
656
  * @param out The caller's array the PRK is written into.
688
- * @param at Where the `hashLength` PRK bytes start in `out`.
657
+ * @param at Where the HashLen PRK bytes start in `out`.
658
+ * @returns `true` once the PRK is written; `false`, with `out` untouched, for
659
+ * an output window outside `out`, or if the HMAC refuses a step, which a
660
+ * whole salt and IKM and a checked window never make it do.
689
661
  */
690
662
  export const hkdfExtractInto = (
691
663
  s: HkdfScratch,
692
- hashLength: i32,
664
+ hash: HkdfHash,
693
665
  salt: u8[],
694
666
  ikm: u8[],
695
667
  out: u8[],
696
668
  at: i32
697
- ): void => {
698
- hkdfCheckHash("hkdfExtractInto", hashLength)
699
- hkdfCheckOutput("hkdfExtractInto", out, at, hashLength)
700
- hkdfMacBegin(s, hashLength, salt, HKDF_FROM, toI32(salt.length))
701
- hkdfMacUpdateAll(s, hashLength, ikm)
702
- hkdfMacFinish(s, hashLength, out, at)
703
- hkdfWipe(s, hashLength)
669
+ ): boolean => {
670
+ if (at < 0 || at > toI32(out.length) || toI32(out.length) - at < hkdfHashLength(hash)) {
671
+ return false
672
+ }
673
+ const done: boolean =
674
+ hkdfMacBegin(s, hash, salt, HKDF_FROM, toI32(salt.length)) &&
675
+ hkdfMacUpdateAll(s, hash, ikm) &&
676
+ hkdfMacFinish(s, hash, out, at)
677
+ hkdfWipe(s, hash)
678
+ return done
704
679
  }
705
680
 
681
+ /**
682
+ * `hkdfExtractInto` under its name from when that form panicked, kept so its
683
+ * callers still compile: the same answer for the same arguments.
684
+ *
685
+ * @param s The scratch to compute in; what the call leaves in it is wiped.
686
+ * @param hash The hash to run over.
687
+ * @param salt The public salt; an empty one stands for HashLen zero bytes
688
+ * (§2.2).
689
+ * @param ikm The input keying material; it is read, not kept.
690
+ * @param out The caller's array the PRK is written into.
691
+ * @param at Where the HashLen PRK bytes start in `out`.
692
+ * @returns What `hkdfExtractInto` answers.
693
+ */
694
+ export const tryHkdfExtractInto = (
695
+ s: HkdfScratch,
696
+ hash: HkdfHash,
697
+ salt: u8[],
698
+ ikm: u8[],
699
+ out: u8[],
700
+ at: i32
701
+ ): boolean => hkdfExtractInto(s, hash, salt, ikm, out, at)
702
+
706
703
  /**
707
704
  * The T(1) | T(2) | … loop of RFC 5869 §2.3, `length` bytes of it at
708
705
  * `out[at]`, with `info` the first `infoLen` bytes of `info`.
709
- * The callers have checked every bound.
706
+ * The callers have checked every bound. `false` if the HMAC refuses a step,
707
+ * which those bounds never make it do, with the bytes written so far zeroed.
710
708
  */
711
709
  const hkdfExpandWindow = (
712
710
  s: HkdfScratch,
713
- hashLength: i32,
711
+ hash: HkdfHash,
714
712
  prk: u8[],
715
713
  info: u8[],
716
714
  infoLen: i32,
717
715
  out: u8[],
718
716
  at: i32,
719
717
  length: i32
720
- ): void => {
718
+ ): boolean => {
719
+ const hashLength: i32 = hkdfHashLength(hash)
721
720
  const counterLength: i32 = 1
722
721
  // `p` runs through the window `out[at .. at + length)`; the bounds on it in
723
722
  // the loops only let the prover see what the callers checked. The counter is
@@ -726,16 +725,22 @@ const hkdfExpandWindow = (
726
725
  let done: i32 = 0
727
726
  let i: u32 = 1
728
727
  while (done < length && p >= 0 && p < toI32(out.length)) {
729
- hkdfMacBegin(s, hashLength, prk, HKDF_FROM, toI32(prk.length))
730
- if (i > 1) {
731
- hkdfMacUpdate(s, hashLength, s.previous, HKDF_FROM, hashLength)
728
+ let fed: boolean = hkdfMacBegin(s, hash, prk, HKDF_FROM, toI32(prk.length))
729
+ if (fed && i > 1) {
730
+ fed = hkdfMacUpdate(s, hash, s.previous, HKDF_FROM, hashLength)
732
731
  }
733
- hkdfMacUpdate(s, hashLength, info, HKDF_FROM, infoLen)
732
+ fed = fed && hkdfMacUpdate(s, hash, info, HKDF_FROM, infoLen)
734
733
  if (toI32(s.counter.length) >= 1) {
735
734
  s.counter[0] = toU8(i)
736
735
  }
737
- hkdfMacUpdate(s, hashLength, s.counter, HKDF_FROM, counterLength)
738
- hkdfMacFinish(s, hashLength, s.previous, HKDF_FROM)
736
+ fed = fed && hkdfMacUpdate(s, hash, s.counter, HKDF_FROM, counterLength)
737
+ if (!fed || !hkdfMacFinish(s, hash, s.previous, HKDF_FROM)) {
738
+ for (let z: i32 = at; z < p && z >= 0 && z < toI32(out.length); z += 1) {
739
+ out[z] = 0
740
+ }
741
+ hkdfWipe(s, hash)
742
+ return false
743
+ }
739
744
  for (
740
745
  let k: i32 = 0;
741
746
  k < hashLength && done < length && p >= 0 && p < toI32(out.length) && k < toI32(s.previous.length);
@@ -747,104 +752,65 @@ const hkdfExpandWindow = (
747
752
  }
748
753
  i += 1
749
754
  }
750
- hkdfWipe(s, hashLength)
755
+ hkdfWipe(s, hash)
756
+ return true
751
757
  }
752
758
 
753
759
  /**
754
- * HKDF-Expand (RFC 5869 §2.3) over the hash `hashLength` names: `length`
755
- * bytes from `prk` and `info`, written at `out[at]`. `false`, writing nothing,
756
- * where `hkdfExpandSha256` answers `null`: a `length` negative or past
757
- * 255 × HashLen, an `info` longer than 2^31 - 1 bytes, or a `prk` shorter than
758
- * HashLen.
760
+ * HKDF-Expand (RFC 5869 §2.3) over `hash`: `length` bytes from `prk` and
761
+ * `info`, written at `out[at]`. `false`, writing nothing, where
762
+ * `hkdfExpandSha256` answers `null` — a `length` negative or past 255 ×
763
+ * HashLen, an `info` longer than 2^31 - 1 bytes, or a `prk` shorter than
764
+ * HashLen — and for an output window outside `out`.
759
765
  *
760
766
  * @param s The scratch to compute in; what the call leaves in it is wiped.
761
- * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
762
- * @param prk The pseudorandom key, at least `hashLength` bytes; it is read, not
767
+ * @param hash The hash to run over.
768
+ * @param prk The pseudorandom key, at least HashLen bytes; it is read, not
763
769
  * kept.
764
770
  * @param info The public context the output is bound to; may be empty.
765
771
  * @param out The caller's array the output is written into.
766
772
  * @param at Where the output starts in `out`.
767
- * @param length How many bytes to derive, 0 to 255 × `hashLength`.
768
- * @returns `true` once the bytes are written; `false`, with `out` untouched,
769
- * for bad input. A `hashLength` other than 32 or 48, or an output window
770
- * outside `out`, panics instead, before anything is written.
773
+ * @param length How many bytes to derive, 0 to 255 × HashLen.
774
+ * @returns `true` once the bytes are written; `false`, with `out` untouched, for an argument out of range,
775
+ * or, with the window zeroed, if the HMAC refuses a step, which no argument in range makes it do.
771
776
  */
772
777
  export const hkdfExpandInto = (
773
778
  s: HkdfScratch,
774
- hashLength: i32,
779
+ hash: HkdfHash,
775
780
  prk: u8[],
776
781
  info: u8[],
777
782
  out: u8[],
778
783
  at: i32,
779
784
  length: i32
780
785
  ): boolean => {
781
- hkdfCheckHash("hkdfExpandInto", hashLength)
782
786
  if (
783
787
  length < 0 ||
784
- length > (hashLength === SHA384_SIZE ? HKDF_MAX_BLOCKS * SHA384_SIZE : HKDF_MAX_BLOCKS * SHA256_SIZE) ||
788
+ length > (hash === HkdfHash.Sha384 ? HKDF_MAX_BLOCKS * SHA384_SIZE : HKDF_MAX_BLOCKS * SHA256_SIZE) ||
785
789
  info.length > 2147483647 ||
786
- toI32(prk.length) < hashLength
790
+ toI32(prk.length) < hkdfHashLength(hash) ||
791
+ at < 0 ||
792
+ at > toI32(out.length) - length
787
793
  ) {
788
794
  return false
789
795
  }
790
- hkdfCheckOutput("hkdfExpandInto", out, at, length)
791
- hkdfExpandWindow(s, hashLength, prk, info, toI32(info.length), out, at, length)
792
- return true
796
+ return hkdfExpandWindow(s, hash, prk, info, toI32(info.length), out, at, length)
793
797
  }
794
798
 
795
799
  /**
796
- * HKDF-Expand-Label (RFC 8446 §7.1) over the hash `hashLength` names: `length`
797
- * bytes from `prk` (RFC 8446's Secret), `"tls13 " + label` and the context
798
- * `context[contextOff .. contextOff + contextLen)`, written at `out[at]`. It
799
- * panics where `hkdfExpandLabelSha256` does — a length outside 0 to 255, a
800
- * label empty or past 249 bytes, a context past 255, a secret shorter than
801
- * HashLen — and on a context window outside its array.
802
- *
803
- * @param s The scratch to compute in; what the call leaves in it is wiped.
804
- * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
805
- * @param prk RFC 8446's Secret, at least `hashLength` bytes; it is read, not
806
- * kept.
807
- * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
808
- * @param context The buffer holding the context.
809
- * @param contextOff Where the context starts in `context`.
810
- * @param contextLen The context's length, at most 255 bytes.
811
- * @param out The caller's array the output is written into.
812
- * @param at Where the output starts in `out`.
813
- * @param length How many bytes to derive, 0 to 255.
800
+ * Writes RFC 8446's `HkdfLabel` struct for `label`, `context[contextOff ..
801
+ * contextOff + contextLen)` and `length`, as `hkdfLabelOrNull` builds it, into
802
+ * the scratch's label buffer, and answers how many bytes it wrote. The callers
803
+ * have checked every bound, and the buffer's HKDF_LABEL_MAX bytes hold the
804
+ * longest struct those checks let through.
814
805
  */
815
- export const hkdfExpandLabelInto = (
806
+ const hkdfLabelInto = (
816
807
  s: HkdfScratch,
817
- hashLength: i32,
818
- prk: u8[],
819
808
  label: string,
820
809
  context: u8[],
821
810
  contextOff: i32,
822
811
  contextLen: i32,
823
- out: u8[],
824
- at: i32,
825
812
  length: i32
826
- ): void => {
827
- hkdfCheckHash("hkdfExpandLabelInto", hashLength)
828
- if (length < 0 || length > 255) {
829
- panic(`hkdfExpandLabelInto: a length of ${length} bytes, outside 0 to 255`)
830
- }
831
- if (label.length === 0 || label.length > 249) {
832
- panic(`hkdfExpandLabelInto: a label of ${toI32(label.length)} bytes, outside 1 to 249`)
833
- }
834
- if (
835
- contextOff < 0 ||
836
- contextLen < 0 ||
837
- contextLen > 255 ||
838
- contextOff > toI32(context.length) - contextLen
839
- ) {
840
- panic("hkdfExpandLabelInto: the context window is outside its array or longer than 255 bytes")
841
- }
842
- if (toI32(prk.length) < hashLength) {
843
- panic("hkdfExpandLabelInto: a secret shorter than HashLen")
844
- }
845
- hkdfCheckOutput("hkdfExpandLabelInto", out, at, length)
846
- // The `HkdfLabel` struct, as `hkdfLabel` builds it, into the scratch, whose
847
- // HKDF_LABEL_MAX bytes hold the longest one the tests above let through.
813
+ ): i32 => {
848
814
  const prefix: string = HKDF_LABEL_PREFIX
849
815
  const prefixLength: i32 = toI32(prefix.length)
850
816
  const labelLength: i32 = toI32(label.length)
@@ -872,9 +838,93 @@ export const hkdfExpandLabelInto = (
872
838
  p += 1
873
839
  q += 1
874
840
  }
875
- hkdfExpandWindow(s, hashLength, prk, info, p, out, at, length)
841
+ return p
876
842
  }
877
843
 
844
+ /**
845
+ * HKDF-Expand-Label (RFC 8446 §7.1) over `hash`: `length` bytes from `prk`
846
+ * (RFC 8446's Secret), `"tls13 " + label` and the context
847
+ * `context[contextOff .. contextOff + contextLen)`, written at `out[at]`.
848
+ * `false`, writing nothing, where `hkdfExpandLabelSha256` answers `null` — a
849
+ * length outside 0 to 255, a label empty or past 249 bytes, a context past
850
+ * 255, a secret shorter than HashLen — and for a context window outside its
851
+ * array or an output window outside `out`.
852
+ *
853
+ * @param s The scratch to compute in; what the call leaves in it is wiped.
854
+ * @param hash The hash to run over.
855
+ * @param prk RFC 8446's Secret, at least HashLen bytes; it is read, not
856
+ * kept.
857
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
858
+ * @param context The buffer holding the context.
859
+ * @param contextOff Where the context starts in `context`.
860
+ * @param contextLen The context's length, at most 255 bytes.
861
+ * @param out The caller's array the output is written into.
862
+ * @param at Where the output starts in `out`.
863
+ * @param length How many bytes to derive, 0 to 255.
864
+ * @returns `true` once the bytes are written; `false`, with `out` untouched, for an argument out of range,
865
+ * or, with the window zeroed, if the HMAC refuses a step, which no argument in range makes it do.
866
+ */
867
+ export const hkdfExpandLabelInto = (
868
+ s: HkdfScratch,
869
+ hash: HkdfHash,
870
+ prk: u8[],
871
+ label: string,
872
+ context: u8[],
873
+ contextOff: i32,
874
+ contextLen: i32,
875
+ out: u8[],
876
+ at: i32,
877
+ length: i32
878
+ ): boolean => {
879
+ if (
880
+ length < 0 ||
881
+ length > 255 ||
882
+ label.length === 0 ||
883
+ label.length > 249 ||
884
+ contextOff < 0 ||
885
+ contextLen < 0 ||
886
+ contextLen > 255 ||
887
+ contextOff > toI32(context.length) - contextLen ||
888
+ toI32(prk.length) < hkdfHashLength(hash) ||
889
+ at < 0 ||
890
+ at > toI32(out.length) - length
891
+ ) {
892
+ return false
893
+ }
894
+ const p: i32 = hkdfLabelInto(s, label, context, contextOff, contextLen, length)
895
+ return hkdfExpandWindow(s, hash, prk, s.label, p, out, at, length)
896
+ }
897
+
898
+ /**
899
+ * `hkdfExpandLabelInto` under its name from when that form panicked, kept so
900
+ * its callers still compile: the same answer for the same arguments.
901
+ *
902
+ * @param s The scratch to compute in; what the call leaves in it is wiped.
903
+ * @param hash The hash to run over.
904
+ * @param prk RFC 8446's Secret, at least HashLen bytes; it is read, not
905
+ * kept.
906
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
907
+ * @param context The buffer holding the context.
908
+ * @param contextOff Where the context starts in `context`.
909
+ * @param contextLen The context's length, at most 255 bytes.
910
+ * @param out The caller's array the output is written into.
911
+ * @param at Where the output starts in `out`.
912
+ * @param length How many bytes to derive, 0 to 255.
913
+ * @returns What `hkdfExpandLabelInto` answers.
914
+ */
915
+ export const tryHkdfExpandLabelInto = (
916
+ s: HkdfScratch,
917
+ hash: HkdfHash,
918
+ prk: u8[],
919
+ label: string,
920
+ context: u8[],
921
+ contextOff: i32,
922
+ contextLen: i32,
923
+ out: u8[],
924
+ at: i32,
925
+ length: i32
926
+ ): boolean => hkdfExpandLabelInto(s, hash, prk, label, context, contextOff, contextLen, out, at, length)
927
+
878
928
  // ---- From and into a held `Secret` -----------------------------------------
879
929
  //
880
930
  // The `*Held` forms derive from a `Secret<u8[]>` the caller already holds, and
@@ -892,30 +942,26 @@ export const hkdfExpandLabelInto = (
892
942
  const hkdfHeldLength = (bytes: u8[]): i32 => toI32(bytes.length)
893
943
 
894
944
  /**
895
- * HKDF-Expand-Label of the held `prk` over the hash `hashLength` names: the
896
- * `length` bytes, plain, for the caller to copy and wipe. Panics naming
897
- * `caller` where `hkdfExpandLabelSha256` does.
945
+ * HKDF-Expand-Label of the held `prk` over `hash`: the `length` bytes, plain,
946
+ * for the caller to copy and wipe, or `null` where `hkdfExpandLabelSha256`
947
+ * answers `null`. No plain intermediate is made before the label is checked,
948
+ * so the `null` leaves nothing to wipe.
898
949
  */
899
950
  const hkdfHeldDerive = (
900
- caller: string,
901
- hashLength: i32,
951
+ hash: HkdfHash,
902
952
  prk: Secret<u8[]>,
903
953
  label: string,
904
954
  context: u8[],
905
955
  length: i32
906
- ): u8[] => {
907
- hkdfCheckHash(caller, hashLength)
908
- const request: HkdfExpandRequest = { info: hkdfLabel(caller, label, context, length), length: length }
909
- const okm: u8[] | null =
910
- hashLength === SHA256_SIZE
911
- ? exposeWith(prk, request, hkdfExpandSha256Exposed)
912
- : exposeWith(prk, request, hkdfExpandSha384Exposed)
913
- if (okm === null) {
914
- panic(
915
- `${caller}: a secret shorter than ${hashLength} bytes, or more than ${toU32(hashLength) * 255} bytes asked`
916
- )
956
+ ): u8[] | null => {
957
+ const info: u8[] | null = hkdfLabelOrNull(label, context, length)
958
+ if (info === null) {
959
+ return null
917
960
  }
918
- return okm
961
+ const request: HkdfExpandRequest = { info: info, length: length }
962
+ return hash === HkdfHash.Sha384
963
+ ? exposeWith(prk, request, hkdfExpandSha384Exposed)
964
+ : exposeWith(prk, request, hkdfExpandSha256Exposed)
919
965
  }
920
966
 
921
967
  /**
@@ -928,25 +974,31 @@ const hkdfHeldCopy = (held: u8[], okm: u8[]): void => {
928
974
  }
929
975
  }
930
976
 
931
- /** Derives into `dst`, as many bytes as it holds, for the two exported forms. */
977
+ /**
978
+ * Derives into `dst`, as many bytes as it holds, for the two exported forms:
979
+ * `false`, with `dst` untouched, where `hkdfHeldDerive` answers `null`.
980
+ */
932
981
  const hkdfHeld = (
933
- caller: string,
934
- hashLength: i32,
982
+ hash: HkdfHash,
935
983
  dst: Secret<u8[]>,
936
984
  prk: Secret<u8[]>,
937
985
  label: string,
938
986
  context: u8[]
939
- ): void => {
940
- const okm: u8[] = hkdfHeldDerive(caller, hashLength, prk, label, context, expose(dst, hkdfHeldLength))
987
+ ): boolean => {
988
+ const okm: u8[] | null = hkdfHeldDerive(hash, prk, label, context, expose(dst, hkdfHeldLength))
989
+ if (okm === null) {
990
+ return false
991
+ }
941
992
  exposeWith(dst, okm, hkdfHeldCopy)
942
993
  wipe(okm)
994
+ return true
943
995
  }
944
996
 
945
997
  /**
946
998
  * `hkdfExpandLabelSha256Secret` into `dst`: as many bytes as `dst` holds,
947
999
  * derived from `prk`, the label and `context` and written over `dst`'s in
948
1000
  * place. `dst` may be `prk` itself, since the output is whole before it is
949
- * copied: that is a ratchet. Panics as `hkdfExpandLabelSha256` does.
1001
+ * copied: that is a ratchet.
950
1002
  *
951
1003
  * @param dst The `Secret` to overwrite; its length is the length derived. It
952
1004
  * may be `prk`.
@@ -954,19 +1006,20 @@ const hkdfHeld = (
954
1006
  * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
955
1007
  * @param context The public context, often a transcript hash; at most 255
956
1008
  * bytes.
1009
+ * @returns `true` once `dst` holds the output; `false`, with `dst` untouched,
1010
+ * where `hkdfExpandLabelSha256` answers `null`: a `dst` past 255 bytes, a
1011
+ * label empty or past 249, a context past 255 or a secret shorter than 32
1012
+ * bytes.
957
1013
  */
958
1014
  export const hkdfExpandLabelSha256Held = (
959
1015
  dst: Secret<u8[]>,
960
1016
  prk: Secret<u8[]>,
961
1017
  label: string,
962
1018
  context: u8[]
963
- ): void => {
964
- hkdfHeld("hkdfExpandLabelSha256Held", SHA256_SIZE, dst, prk, label, context)
965
- }
1019
+ ): boolean => hkdfHeld(HkdfHash.Sha256, dst, prk, label, context)
966
1020
 
967
1021
  /**
968
- * `hkdfExpandLabelSha256Held` with HMAC-SHA-384: panics as
969
- * `hkdfExpandLabelSha384` does.
1022
+ * `hkdfExpandLabelSha256Held` with HMAC-SHA-384.
970
1023
  *
971
1024
  * @param dst The `Secret` to overwrite; its length is the length derived. It
972
1025
  * may be `prk`.
@@ -974,41 +1027,110 @@ export const hkdfExpandLabelSha256Held = (
974
1027
  * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
975
1028
  * @param context The public context, often a transcript hash; at most 255
976
1029
  * bytes.
1030
+ * @returns `true` once `dst` holds the output; `false`, with `dst` untouched,
1031
+ * where `hkdfExpandLabelSha384` answers `null`.
977
1032
  */
978
1033
  export const hkdfExpandLabelSha384Held = (
979
1034
  dst: Secret<u8[]>,
980
1035
  prk: Secret<u8[]>,
981
1036
  label: string,
982
1037
  context: u8[]
983
- ): void => {
984
- hkdfHeld("hkdfExpandLabelSha384Held", SHA384_SIZE, dst, prk, label, context)
985
- }
1038
+ ): boolean => hkdfHeld(HkdfHash.Sha384, dst, prk, label, context)
986
1039
 
987
1040
  /**
988
- * HKDF-Expand-Label of the held `prk` over the hash `hashLength` names into a
989
- * plain array: `length` bytes written at `out[at]`. This is for output that is
990
- * kept plain by design, a record layer's AEAD key and IV, say, which live in
991
- * the arrays the cipher reads. Panics as `hkdfExpandLabelInto` does.
1041
+ * HKDF-Expand-Label of the held `prk` over `hash` into a plain array:
1042
+ * `length` bytes written at `out[at]`. This is for output that is kept plain
1043
+ * by design, a record layer's AEAD key and IV, say, which live in the arrays
1044
+ * the cipher reads.
992
1045
  *
993
- * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
1046
+ * @param hash The hash to run over.
994
1047
  * @param prk RFC 8446's Secret, read only inside `exposeWith`.
995
1048
  * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
996
1049
  * @param context The public context, at most 255 bytes.
997
1050
  * @param out The caller's plain array the output is written into.
998
1051
  * @param at Where the output starts in `out`.
999
1052
  * @param length How many bytes to derive, 0 to 255.
1053
+ * @returns `true` once the bytes are written; `false`, with `out` untouched,
1054
+ * where `hkdfExpandLabelInto` answers `false`.
1000
1055
  */
1001
1056
  export const hkdfExpandLabelHeldInto = (
1002
- hashLength: i32,
1057
+ hash: HkdfHash,
1003
1058
  prk: Secret<u8[]>,
1004
1059
  label: string,
1005
1060
  context: u8[],
1006
1061
  out: u8[],
1007
1062
  at: i32,
1008
1063
  length: i32
1009
- ): void => {
1010
- hkdfCheckOutput("hkdfExpandLabelHeldInto", out, at, length)
1011
- const okm: u8[] = hkdfHeldDerive("hkdfExpandLabelHeldInto", hashLength, prk, label, context, length)
1064
+ ): boolean => {
1065
+ if (at < 0 || length < 0 || at > toI32(out.length) - length) {
1066
+ return false
1067
+ }
1068
+ const okm: u8[] | null = hkdfHeldDerive(hash, prk, label, context, length)
1069
+ if (okm === null) {
1070
+ return false
1071
+ }
1012
1072
  hkdfAppend(out, at, okm)
1013
1073
  wipe(okm)
1074
+ return true
1014
1075
  }
1076
+
1077
+ /**
1078
+ * `hkdfExpandLabelSha256Held` under its name from when that form panicked,
1079
+ * kept so its callers still compile: the same answer for the same arguments.
1080
+ *
1081
+ * @param dst The `Secret` to overwrite; its length is the length derived. It
1082
+ * may be `prk`.
1083
+ * @param prk RFC 8446's Secret, read only inside `exposeWith`.
1084
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
1085
+ * @param context The public context, often a transcript hash; at most 255
1086
+ * bytes.
1087
+ * @returns What `hkdfExpandLabelSha256Held` answers.
1088
+ */
1089
+ export const tryHkdfExpandLabelSha256Held = (
1090
+ dst: Secret<u8[]>,
1091
+ prk: Secret<u8[]>,
1092
+ label: string,
1093
+ context: u8[]
1094
+ ): boolean => hkdfExpandLabelSha256Held(dst, prk, label, context)
1095
+
1096
+ /**
1097
+ * `hkdfExpandLabelSha384Held` under its name from when that form panicked,
1098
+ * kept so its callers still compile: the same answer for the same arguments.
1099
+ *
1100
+ * @param dst The `Secret` to overwrite; its length is the length derived. It
1101
+ * may be `prk`.
1102
+ * @param prk RFC 8446's Secret, read only inside `exposeWith`.
1103
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
1104
+ * @param context The public context, often a transcript hash; at most 255
1105
+ * bytes.
1106
+ * @returns What `hkdfExpandLabelSha384Held` answers.
1107
+ */
1108
+ export const tryHkdfExpandLabelSha384Held = (
1109
+ dst: Secret<u8[]>,
1110
+ prk: Secret<u8[]>,
1111
+ label: string,
1112
+ context: u8[]
1113
+ ): boolean => hkdfExpandLabelSha384Held(dst, prk, label, context)
1114
+
1115
+ /**
1116
+ * `hkdfExpandLabelHeldInto` under its name from when that form panicked,
1117
+ * kept so its callers still compile: the same answer for the same arguments.
1118
+ *
1119
+ * @param hash The hash to run over.
1120
+ * @param prk RFC 8446's Secret, read only inside `exposeWith`.
1121
+ * @param label The label without the `"tls13 "` prefix, 1 to 249 bytes.
1122
+ * @param context The public context, at most 255 bytes.
1123
+ * @param out The caller's plain array the output is written into.
1124
+ * @param at Where the output starts in `out`.
1125
+ * @param length How many bytes to derive, 0 to 255.
1126
+ * @returns What `hkdfExpandLabelHeldInto` answers.
1127
+ */
1128
+ export const tryHkdfExpandLabelHeldInto = (
1129
+ hash: HkdfHash,
1130
+ prk: Secret<u8[]>,
1131
+ label: string,
1132
+ context: u8[],
1133
+ out: u8[],
1134
+ at: i32,
1135
+ length: i32
1136
+ ): boolean => hkdfExpandLabelHeldInto(hash, prk, label, context, out, at, length)