@amritk/nish-aarch64-linux 0.49.1 → 0.50.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 (91) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +12 -6
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +16 -9
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/yaml-json.ts +238 -106
  89. package/std/yaml-lines.ts +47 -27
  90. package/std/yaml-write.ts +58 -24
  91. 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
+ }
@@ -467,13 +467,13 @@ const sha256Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
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. */
@@ -527,49 +527,9 @@ export class Sha256 {
527
527
  if (this.finished) {
528
528
  panic("Sha256: update after digest")
529
529
  }
530
- const size: i32 = toI32(data.length)
531
- if (off < 0 || len < 0 || off > size || len > size - off) {
530
+ if (!sha256Absorb(this, data, off, len)) {
532
531
  panic("Sha256: the window is outside the buffer")
533
532
  }
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
573
533
  }
574
534
 
575
535
  /**
@@ -590,26 +550,7 @@ export class Sha256 {
590
550
  this.update(data, from, toI32(n))
591
551
  return
592
552
  }
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
612
- }
553
+ sha256AbsorbLong(this, data)
613
554
  }
614
555
 
615
556
  /**
@@ -688,7 +629,10 @@ export class Sha256 {
688
629
  const room: i32 = toI32(block.length)
689
630
  const fill: i32 = this.fill
690
631
  for (let i: i32 = fill; i >= 0 && i < room; i += 1) {
691
- block[i] = i === fill ? 0x80 : 0
632
+ block[i] = 0
633
+ }
634
+ if (fill >= 0 && fill < room) {
635
+ block[fill] = 0x80
692
636
  }
693
637
  if (fill >= 56) {
694
638
  sha256Compress(state, schedule, block, 0)
@@ -696,7 +640,7 @@ export class Sha256 {
696
640
  block[i] = 0
697
641
  }
698
642
  }
699
- const bits: u64 = toU64(this.total) << 3
643
+ const bits: u64 = this.total << 3
700
644
  let shift: u64 = 0
701
645
  for (let i: i32 = 63; i >= 56 && i < room; i -= 1) {
702
646
  block[i] = toU8(bits >>> shift)
@@ -717,6 +661,99 @@ export class Sha256 {
717
661
  }
718
662
  }
719
663
 
664
+ /**
665
+ * The body of `Sha256.update`, after its finished check: absorbs
666
+ * `data[off .. off + len)` into `h`, or absorbs nothing and returns false
667
+ * when that window lies outside `data`. Each loop's own condition still
668
+ * bounds its indices; the window check is what makes them hold.
669
+ */
670
+ const sha256Absorb = (h: Sha256, data: u8[], off: i32, len: i32): boolean => {
671
+ const size: i32 = toI32(data.length)
672
+ if (off < 0 || len < 0 || off > size || len > size - off) {
673
+ return false
674
+ }
675
+ // §5.1.1 appends the length modulo 2^64, so the count wraps as a `u64`.
676
+ h.total = h.total + toU64(len)
677
+ const state: u32[] = h.state
678
+ const schedule: u32[] = h.schedule
679
+ const block: u8[] = h.block
680
+ const room: i32 = toI32(block.length)
681
+ let at: i32 = off
682
+ let left: i32 = len
683
+ let fill: i32 = h.fill
684
+ // Top up a partly filled block first, so that the loop below only ever
685
+ // starts at a block boundary. Each loop's condition beyond its count is
686
+ // what proves its indices in range; the window check above makes it hold.
687
+ if (fill > 0) {
688
+ while (left > 0 && fill >= 0 && fill < SHA256_BLOCK && fill < room && at >= 0 && at < size) {
689
+ block[fill] = data[at]
690
+ fill += 1
691
+ at += 1
692
+ left -= 1
693
+ }
694
+ if (fill < SHA256_BLOCK) {
695
+ h.fill = fill
696
+ return true
697
+ }
698
+ sha256Compress(state, schedule, block, 0)
699
+ }
700
+ // Whole blocks are compressed straight out of the caller's buffer, with no
701
+ // copy through `block`.
702
+ while (left >= 64 && at >= 0 && at <= size - 64) {
703
+ sha256Compress(state, schedule, data, at)
704
+ at += 64
705
+ left -= 64
706
+ }
707
+ fill = 0
708
+ while (fill < left && fill < room && at >= 0 && at < size) {
709
+ block[fill] = data[at]
710
+ fill += 1
711
+ at += 1
712
+ }
713
+ h.fill = fill
714
+ return true
715
+ }
716
+
717
+ /**
718
+ * The body of `Sha256.updateAll` for an array longer than 2^31 - 1 bytes,
719
+ * after its finished check: walks `data` with a `number` offset, a block at a
720
+ * time through `h.block`, so it is absorbed whole.
721
+ */
722
+ const sha256AbsorbLong = (h: Sha256, data: u8[]): void => {
723
+ const n: number = data.length
724
+ h.total = h.total + toU64(n)
725
+ const state: u32[] = h.state
726
+ const schedule: u32[] = h.schedule
727
+ const block: u8[] = h.block
728
+ const room: i32 = toI32(block.length)
729
+ let fill: i32 = h.fill
730
+ let off: number = 0
731
+ while (true) {
732
+ while (fill >= 0 && fill < SHA256_BLOCK && fill < room && off >= 0 && off < n) {
733
+ block[fill] = data[off]
734
+ fill += 1
735
+ off += 1
736
+ }
737
+ if (fill < SHA256_BLOCK) {
738
+ h.fill = fill
739
+ return
740
+ }
741
+ sha256Compress(state, schedule, block, 0)
742
+ fill = 0
743
+ }
744
+ }
745
+
746
+ /** Absorbs all of `data` into `h`, past the finished check (`sha256`). */
747
+ const sha256AbsorbWhole = (h: Sha256, data: u8[]): void => {
748
+ const n: number = data.length
749
+ if (n <= 2147483647) {
750
+ // The window is the whole array, so it always lies inside it.
751
+ sha256Absorb(h, data, 0, toI32(n))
752
+ } else {
753
+ sha256AbsorbLong(h, data)
754
+ }
755
+ }
756
+
720
757
  /**
721
758
  * The SHA-256 digest of all of `data`, as a fresh 32-byte array.
722
759
  *
@@ -727,7 +764,37 @@ export class Sha256 {
727
764
  * @returns A fresh 32-byte digest.
728
765
  */
729
766
  export const sha256 = (data: u8[]): u8[] => {
767
+ // A fresh hasher is not finished, so the message goes straight to the
768
+ // absorbing bodies, past `update`'s and `updateAll`'s finished checks.
730
769
  const hasher: Sha256 = new Sha256()
731
- hasher.updateAll(data)
770
+ sha256AbsorbWhole(hasher, data)
732
771
  return hasher.digest()
733
772
  }
773
+
774
+ /**
775
+ * A fresh `Sha256` that has absorbed `a || b || c || d || e` and is not yet
776
+ * finished. This is the start of a hash whose message is keyed material,
777
+ * such as an HMAC's padded key block followed by its message: the parts are
778
+ * absorbed in place, never joined into an array that a `push` could outgrow
779
+ * and leave behind, and the caller, which owns the hasher, calls `digest`
780
+ * and then wipes its `state`, `block` and `schedule`. Pass an empty array for
781
+ * a part the message lacks.
782
+ *
783
+ * @param a The first part of the message; it is read, not changed.
784
+ * @param b The second part, read after `a`.
785
+ * @param c The third part, read after `b`.
786
+ * @param d The fourth part, read after `c`.
787
+ * @param e The fifth part, read after `d`.
788
+ * @returns A hasher holding the five parts, ready for `digest`.
789
+ */
790
+ export const sha256Absorbed = (a: u8[], b: u8[], c: u8[], d: u8[], e: u8[]): Sha256 => {
791
+ // A fresh hasher is not finished, so the parts go straight to the
792
+ // absorbing bodies, past `update`'s finished check.
793
+ const hasher: Sha256 = new Sha256()
794
+ sha256AbsorbWhole(hasher, a)
795
+ sha256AbsorbWhole(hasher, b)
796
+ sha256AbsorbWhole(hasher, c)
797
+ sha256AbsorbWhole(hasher, d)
798
+ sha256AbsorbWhole(hasher, e)
799
+ return hasher
800
+ }
@@ -97,6 +97,7 @@
97
97
  */
98
98
 
99
99
  import { Secret, exposeWith, secret, wipe } from "nish:secret"
100
+ import { wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
100
101
 
101
102
  /** The length in bytes of a scalar, a u-coordinate and an X25519 output. */
102
103
  export const X25519_SIZE: i32 = 32
@@ -105,12 +106,12 @@ export const X25519_SIZE: i32 = 32
105
106
  const X25519_A24: i64 = 121665
106
107
 
107
108
  /**
108
- * `a + b` on limbs: the `u64` sum, read back as `i64`; `f25519Sub64` and
109
- * `f25519Mul64` are its difference and product. Every bound this module states
110
- * keeps the true result inside `i64`, so the answer is the signed one bit for
111
- * bit; the arithmetic is unsigned because signed arithmetic is checked for an
112
- * overflow, and a check is a branch on a secret limb. Each is one
113
- * instruction once LLVM inlines it.
109
+ * `a + b` on limbs, through `wrappingAdd`; `f25519Sub64` and `f25519Mul64` are
110
+ * its difference and product, through `wrappingSub` and `wrappingMul`. Every
111
+ * bound this module states keeps the true result inside `i64`, so the wrap
112
+ * never happens and the answer is the signed one; the wrapping call is there
113
+ * because plain signed arithmetic is checked for an overflow, and a check is a
114
+ * branch on a secret limb. Each is one instruction once LLVM inlines it.
114
115
  *
115
116
  * @param a The first limb.
116
117
  * @param b The second limb.
@@ -118,9 +119,9 @@ const X25519_A24: i64 = 121665
118
119
  *
119
120
  * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
120
121
  */
121
- export const f25519Add64 = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
122
+ export const f25519Add64 = (a: i64, b: i64): i64 => wrappingAdd(a, b)
122
123
  /**
123
- * `a - b` on limbs, unsigned and read back as `i64`, as `f25519Add64` explains.
124
+ * `a - b` on limbs, through `wrappingSub`, as `f25519Add64` explains.
124
125
  *
125
126
  * @param a The limb subtracted from.
126
127
  * @param b The limb subtracted.
@@ -128,9 +129,9 @@ export const f25519Add64 = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
128
129
  *
129
130
  * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
130
131
  */
131
- export const f25519Sub64 = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
132
+ export const f25519Sub64 = (a: i64, b: i64): i64 => wrappingSub(a, b)
132
133
  /**
133
- * `a * b` on limbs, unsigned and read back as `i64`, as `f25519Add64` explains.
134
+ * `a * b` on limbs, through `wrappingMul`, as `f25519Add64` explains.
134
135
  *
135
136
  * @param a The first limb.
136
137
  * @param b The second limb.
@@ -138,7 +139,7 @@ export const f25519Sub64 = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
138
139
  *
139
140
  * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
140
141
  */
141
- export const f25519Mul64 = (a: i64, b: i64): i64 => toI64(toU64(a) * toU64(b))
142
+ export const f25519Mul64 = (a: i64, b: i64): i64 => wrappingMul(a, b)
142
143
 
143
144
  /** The width of limb `i`: 26 bits for an even limb, 25 for an odd one. */
144
145
  const f25519Width = (i: i32): i64 => {