@amritk/nish-aarch64-linux 0.49.1 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
package/std/crypto/rsa.ts CHANGED
@@ -207,14 +207,16 @@ const rsaBitLength = (v: u8[]): i32 => {
207
207
  return -1
208
208
  }
209
209
  const top: i32 = toI32(v[0])
210
- let topBits: i32 = 0
210
+ // The leading zero bits of the top byte, eight for a zero byte.
211
+ let missing: i32 = 8
211
212
  for (let b: i32 = 0; b < 8; b++) {
212
213
  if (top >> b !== 0) {
213
- topBits = b + 1
214
+ missing = 7 - b
214
215
  }
215
216
  }
216
- // At most (2^28 - 2) 8 + 8, below 2^31, so the u32 sum is the count.
217
- return toI32(toU32(length - 1) * 8 + toU32(topBits))
217
+ // The masks keep what the checks above already hold (fewer than 2^28 bytes,
218
+ // at most eight missing bits), and let src/bounds.ts prove the count fits.
219
+ return (length & 0x0fffffff) * 8 - (missing & 0xf)
218
220
  }
219
221
 
220
222
  /**
@@ -738,3 +740,23 @@ export const rsaVerifyPss = (key: RsaPublicKey, hash: i32, saltLen: i32, msg: u8
738
740
  }
739
741
  return timingSafeEqual(rsaDigest(hash, prime), h)
740
742
  }
743
+
744
+ /**
745
+ * Verifies an RSASSA-PSS signature under a public key given as its DER, as
746
+ * `rsaVerifyPss` does after `rsaParsePublicKey`: for a caller that holds the
747
+ * key's bytes and no `RsaPublicKey`.
748
+ *
749
+ * @param der The key as `rsaParsePublicKey` reads it, whole.
750
+ * @param hash `RSA_HASH_SHA256`, `RSA_HASH_SHA384` or `RSA_HASH_SHA512`.
751
+ * @param saltLen The salt's length in bytes.
752
+ * @param msg The message signed, whole.
753
+ * @param sig The signature, big-endian.
754
+ * @returns `true` only when `der` parses and `sig` is its signature of `msg`.
755
+ */
756
+ export const rsaVerifyPssDer = (der: u8[], hash: i32, saltLen: i32, msg: u8[], sig: u8[]): boolean => {
757
+ const key: RsaPublicKey | null = rsaParsePublicKey(der)
758
+ if (key === null) {
759
+ return false
760
+ }
761
+ return rsaVerifyPss(key, hash, saltLen, msg, sig)
762
+ }
@@ -460,20 +460,20 @@ const sha256Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
460
460
  * the digest does not depend on where the message was split. `digest` pads the
461
461
  * message (§5.1.1), runs the last block or two, and ends the computation: the
462
462
  * padding is written into this hasher's own state, so a later `update` would
463
- * carry the padding as message, and it panics instead of answering a wrong
464
- * value. A second `digest` answers the same digest again, and a `copy` of a
465
- * finished hasher is finished too. `copy` first when the computation has to go
466
- * on.
463
+ * carry the padding as message, so it answers `false` and absorbs nothing
464
+ * instead of hashing a wrong value. A second `digest` answers
465
+ * the same digest again, and a `copy` of a finished hasher is finished too.
466
+ * `copy` first when the computation has to go on.
467
467
  */
468
468
  export class Sha256 {
469
469
  /**
470
- * The message length absorbed so far, in bytes. An `i64` because §5.1.1
471
- * appends the length in bits as a 64-bit number, and a transcript can pass
472
- * 2^31 bytes where an `i32` would wrap.
470
+ * The message length absorbed so far, in bytes. A `u64` because §5.1.1
471
+ * appends the length in bits modulo 2^64, which a `u64` wraps to by its type,
472
+ * and a transcript can pass 2^31 bytes where an `i32` would wrap.
473
473
  *
474
474
  * @internal Set by `nish/crypto/hmac` and `nish/net/postgres` when they wipe, copy or restart a hasher.
475
475
  */
476
- total: i64 = 0
476
+ total: u64 = 0
477
477
  /** H_0 .. H_7, the eight words of the intermediate hash value (§6.2.2 step 4). */
478
478
  state: u32[]
479
479
  /** The pending bytes of the current block; the first `fill` of them are live. */
@@ -515,61 +515,34 @@ export class Sha256 {
515
515
  }
516
516
 
517
517
  /**
518
- * Absorbs `data[off .. off + len)`. A window outside `data` panics rather
519
- * than hashing a short read, since a digest over the wrong bytes looks
520
- * exactly like a digest over the right ones.
518
+ * Absorbs `data[off .. off + len)`. A window outside `data`, or a call
519
+ * after `digest`, answers `false` and absorbs nothing rather than hashing a
520
+ * short read. Check it, since a digest over the wrong bytes looks exactly
521
+ * like a digest over the right ones.
521
522
  *
522
523
  * @param data The buffer holding the window; it is read, not kept.
523
524
  * @param off Where the window starts in `data`.
524
525
  * @param len How many bytes to absorb; 0 absorbs nothing.
526
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
527
+ * for a window outside `data` or a hasher already digested.
525
528
  */
526
- update(data: u8[], off: i32, len: i32): void {
527
- if (this.finished) {
528
- panic("Sha256: update after digest")
529
- }
530
- const size: i32 = toI32(data.length)
531
- if (off < 0 || len < 0 || off > size || len > size - off) {
532
- panic("Sha256: the window is outside the buffer")
533
- }
534
- // §5.1.1 appends the length modulo 2^64, so the count wraps as a `u64`.
535
- this.total = toI64(toU64(this.total) + toU64(toI64(len)))
536
- const state: u32[] = this.state
537
- const schedule: u32[] = this.schedule
538
- const block: u8[] = this.block
539
- const room: i32 = toI32(block.length)
540
- let at: i32 = off
541
- let left: i32 = len
542
- let fill: i32 = this.fill
543
- // Top up a partly filled block first, so that the loop below only ever
544
- // starts at a block boundary. Each loop's condition beyond its count is
545
- // what proves its indices in range; the window check above makes it hold.
546
- if (fill > 0) {
547
- while (left > 0 && fill >= 0 && fill < SHA256_BLOCK && fill < room && at >= 0 && at < size) {
548
- block[fill] = data[at]
549
- fill += 1
550
- at += 1
551
- left -= 1
552
- }
553
- if (fill < SHA256_BLOCK) {
554
- this.fill = fill
555
- return
556
- }
557
- sha256Compress(state, schedule, block, 0)
558
- }
559
- // Whole blocks are compressed straight out of the caller's buffer, with no
560
- // copy through `block`.
561
- while (left >= 64 && at >= 0 && at <= size - 64) {
562
- sha256Compress(state, schedule, data, at)
563
- at += 64
564
- left -= 64
565
- }
566
- fill = 0
567
- while (fill < left && fill < room && at >= 0 && at < size) {
568
- block[fill] = data[at]
569
- fill += 1
570
- at += 1
571
- }
572
- this.fill = fill
529
+ update(data: u8[], off: i32, len: i32): boolean {
530
+ return !this.finished && sha256Absorb(this, data, off, len)
531
+ }
532
+
533
+ /**
534
+ * The same answer as `update`, kept for callers written against it: a
535
+ * window outside `data`, or an update after `digest`, answers `false` and
536
+ * absorbs nothing.
537
+ *
538
+ * @param data The buffer holding the window; it is read, not kept.
539
+ * @param off Where the window starts in `data`.
540
+ * @param len How many bytes to absorb; 0 absorbs nothing.
541
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
542
+ * for a window outside `data` or a hasher already digested.
543
+ */
544
+ tryUpdate(data: u8[], off: i32, len: i32): boolean {
545
+ return !this.finished && sha256Absorb(this, data, off, len)
573
546
  }
574
547
 
575
548
  /**
@@ -577,39 +550,61 @@ export class Sha256 {
577
550
  * `update`. A longer one (possible under `--number-mode f64`, where `toI32`
578
551
  * of its length would saturate and leave a prefix) is walked with a `number`
579
552
  * offset instead, a block at a time through `block`, so it is absorbed
580
- * whole. After `digest` it panics, as `update` does.
553
+ * whole. After `digest` it answers `false` and absorbs nothing, as
554
+ * `update` does.
581
555
  *
582
556
  * @param data The message, or the rest of it; it is read, not kept.
557
+ * @returns `true` once `data` is absorbed; `false`, absorbing nothing, for a
558
+ * hasher already digested.
583
559
  */
584
- updateAll(data: u8[]): void {
585
- const n: number = data.length
586
- if (this.finished || n <= 2147483647) {
587
- // A finished hasher panics in `update` before it reads the window, so
588
- // the saturated length of a longer array is never used.
589
- const from: i32 = 0
590
- this.update(data, from, toI32(n))
591
- return
560
+ updateAll(data: u8[]): boolean {
561
+ if (this.finished) {
562
+ return false
592
563
  }
593
- this.total = toI64(toU64(this.total) + toU64(toI64(n)))
594
- const state: u32[] = this.state
595
- const schedule: u32[] = this.schedule
596
- const block: u8[] = this.block
597
- const room: i32 = toI32(block.length)
598
- let fill: i32 = this.fill
599
- let off: number = 0
600
- while (true) {
601
- while (fill >= 0 && fill < SHA256_BLOCK && fill < room && off >= 0 && off < n) {
602
- block[fill] = data[off]
603
- fill += 1
604
- off += 1
605
- }
606
- if (fill < SHA256_BLOCK) {
607
- this.fill = fill
608
- return
609
- }
610
- sha256Compress(state, schedule, block, 0)
611
- fill = 0
564
+ sha256AbsorbWhole(this, data)
565
+ return true
566
+ }
567
+
568
+ /**
569
+ * The same answer as `updateAll`, kept for callers written against it:
570
+ * after `digest` it answers `false` and absorbs nothing.
571
+ *
572
+ * @param data The message, or the rest of it; it is read, not kept.
573
+ * @returns `true` once `data` is absorbed; `false`, absorbing nothing, for a
574
+ * hasher already digested.
575
+ */
576
+ tryUpdateAll(data: u8[]): boolean {
577
+ if (this.finished) {
578
+ return false
612
579
  }
580
+ sha256AbsorbWhole(this, data)
581
+ return true
582
+ }
583
+
584
+ /**
585
+ * `updateAll` past its finished test, for a caller holding a hasher that
586
+ * `digest` cannot have ended: one it made itself and has not digested (the
587
+ * HMAC key and pad blocks, a tag's inner digest). The whole array is the
588
+ * window, so there is no window to get wrong.
589
+ *
590
+ * @internal Called by `nish/crypto/hmac`.
591
+ */
592
+ absorbWhole(data: u8[]): void {
593
+ sha256AbsorbWhole(this, data)
594
+ }
595
+
596
+ /**
597
+ * `update` past its finished test, for the same callers as `absorbWhole`
598
+ * with a window (an HMAC tag's inner digest). A window outside `data`
599
+ * absorbs nothing, as `tryUpdate`'s does.
600
+ *
601
+ * @internal Called by `nish/crypto/hmac`.
602
+ * @param data The bytes to read from; they are read, not kept.
603
+ * @param off Where the window starts in `data`.
604
+ * @param len How many bytes the window holds.
605
+ */
606
+ absorbWindow(data: u8[], off: i32, len: i32): void {
607
+ sha256Absorb(this, data, off, len)
613
608
  }
614
609
 
615
610
  /**
@@ -663,7 +658,7 @@ export class Sha256 {
663
658
  * hasher as it was and answers `false`, rather than writing the part of the
664
659
  * digest that fits. Once finished, the hash value is the digest, so a second
665
660
  * `digest` or `digestInto` writes the same 32 bytes again; an `update` after
666
- * it panics.
661
+ * it answers `false` and absorbs nothing.
667
662
  *
668
663
  * @param out The caller's array the digest is written into.
669
664
  * @param at Where the 32 digest bytes start in `out`.
@@ -688,7 +683,10 @@ export class Sha256 {
688
683
  const room: i32 = toI32(block.length)
689
684
  const fill: i32 = this.fill
690
685
  for (let i: i32 = fill; i >= 0 && i < room; i += 1) {
691
- block[i] = i === fill ? 0x80 : 0
686
+ block[i] = 0
687
+ }
688
+ if (fill >= 0 && fill < room) {
689
+ block[fill] = 0x80
692
690
  }
693
691
  if (fill >= 56) {
694
692
  sha256Compress(state, schedule, block, 0)
@@ -696,7 +694,7 @@ export class Sha256 {
696
694
  block[i] = 0
697
695
  }
698
696
  }
699
- const bits: u64 = toU64(this.total) << 3
697
+ const bits: u64 = this.total << 3
700
698
  let shift: u64 = 0
701
699
  for (let i: i32 = 63; i >= 56 && i < room; i -= 1) {
702
700
  block[i] = toU8(bits >>> shift)
@@ -717,6 +715,100 @@ export class Sha256 {
717
715
  }
718
716
  }
719
717
 
718
+ /**
719
+ * The body of `Sha256.update`, after its finished check: absorbs
720
+ * `data[off .. off + len)` into `h`, or absorbs nothing and returns false
721
+ * when that window lies outside `data`. Each loop's own condition still
722
+ * bounds its indices; the window check is what makes them hold.
723
+ */
724
+ const sha256Absorb = (h: Sha256, data: u8[], off: i32, len: i32): boolean => {
725
+ const size: i32 = toI32(data.length)
726
+ if (off < 0 || len < 0 || off > size || len > size - off) {
727
+ return false
728
+ }
729
+ // §5.1.1 appends the length modulo 2^64, so the count wraps as a `u64`.
730
+ h.total = h.total + toU64(len)
731
+ const state: u32[] = h.state
732
+ const schedule: u32[] = h.schedule
733
+ const block: u8[] = h.block
734
+ const room: i32 = toI32(block.length)
735
+ let at: i32 = off
736
+ let left: i32 = len
737
+ let fill: i32 = h.fill
738
+ // Top up a partly filled block first, so that the loop below only ever
739
+ // starts at a block boundary. Each loop's condition beyond its count is
740
+ // what proves its indices in range; the window check above makes it hold.
741
+ if (fill > 0) {
742
+ while (left > 0 && fill >= 0 && fill < SHA256_BLOCK && fill < room && at >= 0 && at < size) {
743
+ block[fill] = data[at]
744
+ fill += 1
745
+ at += 1
746
+ left -= 1
747
+ }
748
+ if (fill < SHA256_BLOCK) {
749
+ h.fill = fill
750
+ return true
751
+ }
752
+ sha256Compress(state, schedule, block, 0)
753
+ }
754
+ // Whole blocks are compressed straight out of the caller's buffer, with no
755
+ // copy through `block`.
756
+ while (left >= 64 && at >= 0 && at <= size - 64) {
757
+ sha256Compress(state, schedule, data, at)
758
+ at += 64
759
+ left -= 64
760
+ }
761
+ fill = 0
762
+ while (fill < left && fill < room && at >= 0 && at < size) {
763
+ block[fill] = data[at]
764
+ fill += 1
765
+ at += 1
766
+ }
767
+ h.fill = fill
768
+ return true
769
+ }
770
+
771
+ /**
772
+ * The body of `Sha256.updateAll` for an array longer than 2^31 - 1 bytes,
773
+ * after its finished check: walks `data` with a `number` offset, a block at a
774
+ * time through `h.block`, so it is absorbed whole.
775
+ */
776
+ const sha256AbsorbLong = (h: Sha256, data: u8[]): void => {
777
+ const n: number = data.length
778
+ h.total = h.total + toU64(n)
779
+ const state: u32[] = h.state
780
+ const schedule: u32[] = h.schedule
781
+ const block: u8[] = h.block
782
+ const room: i32 = toI32(block.length)
783
+ let fill: i32 = h.fill
784
+ let off: number = 0
785
+ while (true) {
786
+ while (fill >= 0 && fill < SHA256_BLOCK && fill < room && off >= 0 && off < n) {
787
+ block[fill] = data[off]
788
+ fill += 1
789
+ off += 1
790
+ }
791
+ if (fill < SHA256_BLOCK) {
792
+ h.fill = fill
793
+ return
794
+ }
795
+ sha256Compress(state, schedule, block, 0)
796
+ fill = 0
797
+ }
798
+ }
799
+
800
+ /** Absorbs all of `data` into `h`, past the finished check (`sha256`). */
801
+ const sha256AbsorbWhole = (h: Sha256, data: u8[]): void => {
802
+ const n: number = data.length
803
+ if (n <= 2147483647) {
804
+ // The window is the whole array, so it always lies inside it.
805
+ const from: i32 = 0
806
+ sha256Absorb(h, data, from, toI32(n))
807
+ } else {
808
+ sha256AbsorbLong(h, data)
809
+ }
810
+ }
811
+
720
812
  /**
721
813
  * The SHA-256 digest of all of `data`, as a fresh 32-byte array.
722
814
  *
@@ -727,7 +819,37 @@ export class Sha256 {
727
819
  * @returns A fresh 32-byte digest.
728
820
  */
729
821
  export const sha256 = (data: u8[]): u8[] => {
822
+ // A fresh hasher is not finished, so the message goes straight to the
823
+ // absorbing bodies, past `update`'s and `updateAll`'s finished checks.
730
824
  const hasher: Sha256 = new Sha256()
731
- hasher.updateAll(data)
825
+ sha256AbsorbWhole(hasher, data)
732
826
  return hasher.digest()
733
827
  }
828
+
829
+ /**
830
+ * A fresh `Sha256` that has absorbed `a || b || c || d || e` and is not yet
831
+ * finished. This is the start of a hash whose message is keyed material,
832
+ * such as an HMAC's padded key block followed by its message: the parts are
833
+ * absorbed in place, never joined into an array that a `push` could outgrow
834
+ * and leave behind, and the caller, which owns the hasher, calls `digest`
835
+ * and then wipes its `state`, `block` and `schedule`. Pass an empty array for
836
+ * a part the message lacks.
837
+ *
838
+ * @param a The first part of the message; it is read, not changed.
839
+ * @param b The second part, read after `a`.
840
+ * @param c The third part, read after `b`.
841
+ * @param d The fourth part, read after `c`.
842
+ * @param e The fifth part, read after `d`.
843
+ * @returns A hasher holding the five parts, ready for `digest`.
844
+ */
845
+ export const sha256Absorbed = (a: u8[], b: u8[], c: u8[], d: u8[], e: u8[]): Sha256 => {
846
+ // A fresh hasher is not finished, so the parts go straight to the
847
+ // absorbing bodies, past `update`'s finished check.
848
+ const hasher: Sha256 = new Sha256()
849
+ sha256AbsorbWhole(hasher, a)
850
+ sha256AbsorbWhole(hasher, b)
851
+ sha256AbsorbWhole(hasher, c)
852
+ sha256AbsorbWhole(hasher, d)
853
+ sha256AbsorbWhole(hasher, e)
854
+ return hasher
855
+ }