nish 0.44.0 → 0.46.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/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -73,11 +73,10 @@ const scramClientProofExposed = (salted: u8[], authMessage: u8[]): u8[] => {
73
73
  const clientKey: u8[] = hmacSha256(salted, scramAscii("Client Key"))
74
74
  const storedKey: u8[] = scramSha256Wiped(clientKey)
75
75
  const clientSignature: u8[] = hmacSha256(storedKey, authMessage)
76
- if (clientKey.length < 32 || clientSignature.length < 32) {
77
- panic("nish/crypto/scram: an HMAC-SHA-256 shorter than 32 bytes")
78
- }
76
+ // Both are HMAC-SHA-256 tags, 32 bytes each; the bounds below only let the
77
+ // prover see that, and never stop the loop early.
79
78
  const proof: u8[] = new Array<u8>(32)
80
- for (let i: i32 = 0; i < 32; i++) {
79
+ for (let i: i32 = 0; i < 32 && i < toI32(clientKey.length) && i < toI32(clientSignature.length); i++) {
81
80
  proof[i] = clientKey[i] ^ clientSignature[i]
82
81
  }
83
82
  wipe(clientKey)
@@ -35,22 +35,21 @@ export const SHA1_BLOCK: i32 = 64
35
35
  /** ROTL^n(x) of FIPS 180-4 §3.2: a left rotation of a 32-bit word, `0 < n < 32`. */
36
36
  const sha1Rotl = (x: u32, n: u32): u32 => (x << n) | (x >>> (32 - n))
37
37
 
38
- /** The big-endian word at `buf[at .. at + 4)`, as FIPS 180-4 §3.1 orders bytes in a word. */
39
- const sha1LoadWord = (buf: u8[], at: i32): u32 =>
40
- (toU32(buf[at]) << 24) | (toU32(buf[at + 1]) << 16) | (toU32(buf[at + 2]) << 8) | toU32(buf[at + 3])
41
-
42
38
  /**
43
39
  * Folds the 64-byte block at `src[at .. at + 64)` into `h`: FIPS 180-4
44
40
  * §6.1.2, steps 1 to 4. `w` is the 80-word schedule, held by the `Sha1`
45
- * hasher rather than allocated once per block.
41
+ * hasher rather than allocated once per block. Every caller passes a block
42
+ * inside `src` and the hasher's own `h` and `w`; the conditions below are the
43
+ * shapes that prove each index in range, not checks a caller can fail.
46
44
  */
47
45
  const sha1Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
48
- // Written as the condition the body runs under, the shape that proves the
49
- // plain `w[t]` and `h[i]` indices in range (as `sha256Compress` does).
50
- if (toI32(h.length) >= 5 && toI32(w.length) >= 80 && at >= 0 && at <= toI32(src.length) - SHA1_BLOCK) {
46
+ if (toI32(h.length) >= 5 && toI32(w.length) >= 80) {
51
47
  // Step 1: the message schedule, big-endian words (§3.1), then (6.1.2).
52
- for (let t: i32 = 0; t < 16; t += 1) {
53
- w[t] = sha1LoadWord(src, at + 4 * t)
48
+ const n: i32 = toI32(src.length)
49
+ let p: i32 = at
50
+ for (let t: i32 = 0; t < 16 && p >= 0 && p < n - 3; t += 1) {
51
+ w[t] = (toU32(src[p]) << 24) | (toU32(src[p + 1]) << 16) | (toU32(src[p + 2]) << 8) | toU32(src[p + 3])
52
+ p += 4
54
53
  }
55
54
  for (let t: i32 = 16; t < 80; t += 1) {
56
55
  w[t] = sha1Rotl(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1)
@@ -91,8 +90,56 @@ const sha1Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
91
90
  h[2] += c
92
91
  h[3] += d
93
92
  h[4] += e
94
- } else {
95
- panic("sha1: a block outside its buffer")
93
+ }
94
+ }
95
+
96
+ /** Sets `h` to H^(0) of FIPS 180-4 §5.3.1. */
97
+ const sha1Begin = (h: u32[]): void => {
98
+ if (toI32(h.length) >= 5) {
99
+ h[0] = 0x67452301
100
+ h[1] = 0xefcdab89
101
+ h[2] = 0x98badcfe
102
+ h[3] = 0x10325476
103
+ h[4] = 0xc3d2e1f0
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Finishes a digest whose last `tail` bytes (`0 <= tail < 64`) the caller has
109
+ * copied to `last[0 .. tail)`: pads them (FIPS 180-4 §5.1.1) with the message
110
+ * length of `bits` bits, folds the one or two padded blocks into `h`, and
111
+ * writes the digest to `out[at .. at + 20)`, which the caller has checked.
112
+ */
113
+ const sha1Finish = (h: u32[], w: u32[], last: u8[], tail: i32, bits: u64, out: u8[], at: i32): void => {
114
+ let lastLength: i32 = 64
115
+ if (tail >= 56) {
116
+ lastLength = 128
117
+ }
118
+ const room: i32 = toI32(last.length)
119
+ // The 0x80 that ends the message, then zeros, then the length big-endian.
120
+ let z: i32 = tail
121
+ while (z >= 0 && z < lastLength && z < room) {
122
+ last[z] = z === tail ? 0x80 : 0
123
+ z += 1
124
+ }
125
+ let k: i32 = lastLength - 8
126
+ let shift: u64 = 56
127
+ for (let i: i32 = 0; i < 8 && k >= 0 && k < room; i += 1) {
128
+ last[k] = toU8(bits >>> shift)
129
+ shift -= 8
130
+ k += 1
131
+ }
132
+ for (let off: i32 = 0; off < lastLength; off += 64) {
133
+ sha1Compress(h, w, last, off)
134
+ }
135
+ let p: i32 = at
136
+ for (let i: i32 = 0; i < 5 && i < toI32(h.length) && p >= 0 && p < toI32(out.length) - 3; i += 1) {
137
+ const word: u32 = h[i]
138
+ out[p] = toU8(word >>> 24)
139
+ out[p + 1] = toU8(word >>> 16)
140
+ out[p + 2] = toU8(word >>> 8)
141
+ out[p + 3] = toU8(word)
142
+ p += 4
96
143
  }
97
144
  }
98
145
 
@@ -117,92 +164,87 @@ export class Sha1 {
117
164
  constructor() {
118
165
  this.h = new Array<u32>(5)
119
166
  this.w = new Array<u32>(80)
120
- this.last = new Array<u8>(2 * SHA1_BLOCK)
167
+ this.last = new Array<u8>(128)
121
168
  }
122
169
 
123
170
  /**
124
171
  * Writes the SHA-1 digest of `data[start .. start + length)` into
125
172
  * `out[at .. at + 20)`, allocating nothing. A range outside `data` or `out`
126
- * panics, as does a message longer than 2^31 - 1 bytes.
173
+ * writes nothing and answers `false`.
127
174
  *
128
175
  * @param data The buffer holding the message; it is read, not changed.
129
176
  * @param start Where in `data` the message starts.
130
177
  * @param length How many bytes of `data` the message is.
131
178
  * @param out The buffer the 20-byte digest is written into.
132
179
  * @param at Where in `out` the digest starts.
180
+ * @returns `true` once the digest is written; `false`, with `out` untouched, when either range lies outside its buffer.
133
181
  */
134
- digestInto(data: u8[], start: i32, length: i32, out: u8[], at: i32): void {
135
- if (data.length > 2147483647 || out.length > 2147483647) {
136
- panic("sha1: a buffer longer than 2^31 - 1 bytes")
137
- }
182
+ digestInto(data: u8[], start: i32, length: i32, out: u8[], at: i32): boolean {
183
+ // `toI32` of a length past 2^31 - 1 (under `--number-mode f64`) saturates,
184
+ // which only narrows the ranges accepted here: an `i32` range ends below it.
138
185
  if (start < 0 || length < 0 || start > toI32(data.length) - length) {
139
- panic("sha1: a message outside its buffer")
186
+ return false
140
187
  }
141
188
  if (at < 0 || at > toI32(out.length) - SHA1_SIZE) {
142
- panic("sha1: a digest outside its buffer")
189
+ return false
143
190
  }
144
191
  const h: u32[] = this.h
145
192
  const w: u32[] = this.w
146
193
  const last: u8[] = this.last
147
- if (toI32(h.length) < 5) {
148
- panic("sha1: a hasher without its state")
149
- }
150
- // H^(0) of FIPS 180-4 §5.3.1.
151
- h[0] = 0x67452301
152
- h[1] = 0xefcdab89
153
- h[2] = 0x98badcfe
154
- h[3] = 0x10325476
155
- h[4] = 0xc3d2e1f0
194
+ sha1Begin(h)
156
195
  // Whole blocks straight from `data`, then the tail and its padding (§5.1.1)
157
196
  // in one or two blocks of `last`.
158
- const whole: i32 = length - (length % SHA1_BLOCK)
159
- for (let off: i32 = 0; off < whole; off += SHA1_BLOCK) {
160
- sha1Compress(h, w, data, start + off)
197
+ const size: i32 = toI32(data.length)
198
+ let off: i32 = start
199
+ let left: i32 = length
200
+ while (left >= 64 && off >= 0 && off <= size - 64) {
201
+ sha1Compress(h, w, data, off)
202
+ off += 64
203
+ left -= 64
161
204
  }
162
- const tail: i32 = length - whole
163
- const lastLength: i32 = tail < SHA1_BLOCK - 8 ? SHA1_BLOCK : 2 * SHA1_BLOCK
205
+ let k: i32 = 0
164
206
  const room: i32 = toI32(last.length)
165
- if (lastLength > room) {
166
- panic("sha1: a hasher without its state")
167
- }
168
- for (let k: i32 = 0; k < lastLength && k < room; k += 1) {
169
- last[k] = k < tail ? data[start + whole + k] : 0
170
- }
171
- last[tail] = 0x80
172
- const bits: u64 = toU64(toI64(length)) << 3
173
- for (let i: i32 = 0; i < 8; i += 1) {
174
- last[lastLength - 1 - i] = toU8(bits >>> toU64(8 * i))
175
- }
176
- for (let off: i32 = 0; off < lastLength; off += SHA1_BLOCK) {
177
- sha1Compress(h, w, last, off)
178
- }
179
- for (let i: i32 = 0; i < 5; i += 1) {
180
- const word: u32 = h[i]
181
- out[at + 4 * i] = toU8(word >>> 24)
182
- out[at + 4 * i + 1] = toU8(word >>> 16)
183
- out[at + 4 * i + 2] = toU8(word >>> 8)
184
- out[at + 4 * i + 3] = toU8(word)
207
+ while (k < left && k < room && off >= 0 && off < size) {
208
+ last[k] = data[off]
209
+ k += 1
210
+ off += 1
185
211
  }
212
+ sha1Finish(h, w, last, k, toU64(toI64(length)) << 3, out, at)
213
+ return true
186
214
  }
187
215
  }
188
216
 
189
217
  /**
190
- * The SHA-1 digest of all of `data`, as a fresh 20-byte array: `Sha1`'s
191
- * `digestInto` over the whole of it, with a hasher of its own.
218
+ * The SHA-1 digest of all of `data`, as a fresh 20-byte array.
192
219
  *
193
- * An array longer than 2^31 - 1 bytes panics, for the reason `sha256` gives:
194
- * under `--number-mode f64`, `toI32` of a longer length saturates, and the
195
- * answer would be the digest of a prefix.
220
+ * It walks `data` with a `number` offset and copies each block into the
221
+ * hasher's own buffer, so an array longer than 2^31 - 1 bytes (possible under
222
+ * `--number-mode f64`) is hashed whole, never cut to a prefix.
196
223
  *
197
224
  * @param data The whole message; it is read, not changed.
198
225
  * @returns A fresh 20-byte digest.
199
226
  */
200
227
  export const sha1 = (data: u8[]): u8[] => {
201
- if (data.length > 2147483647) {
202
- panic("sha1: a message longer than 2^31 - 1 bytes")
203
- }
204
228
  const out: u8[] = new Array<u8>(SHA1_SIZE)
205
- const zero: i32 = 0
206
- new Sha1().digestInto(data, zero, toI32(data.length), out, zero)
207
- return out
229
+ const hasher: Sha1 = new Sha1()
230
+ const h: u32[] = hasher.h
231
+ const w: u32[] = hasher.w
232
+ const last: u8[] = hasher.last
233
+ const room: i32 = toI32(last.length)
234
+ sha1Begin(h)
235
+ const n: number = data.length
236
+ let off: number = 0
237
+ while (true) {
238
+ let k: i32 = 0
239
+ while (k < SHA1_BLOCK && k < room && off >= 0 && off < n) {
240
+ last[k] = data[off]
241
+ k += 1
242
+ off += 1
243
+ }
244
+ if (k < SHA1_BLOCK) {
245
+ sha1Finish(h, w, last, k, toU64(toI64(n)) << 3, out, 0)
246
+ return out
247
+ }
248
+ sha1Compress(h, w, last, 0)
249
+ }
208
250
  }