@amritk/nish-x86_64-linux 0.23.1 → 0.25.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 (51) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +50 -23
  3. package/package.json +1 -1
  4. package/runtime/browser.mjs +4 -3
  5. package/runtime/core/arrays.d.ts +0 -2
  6. package/runtime/core/arrays.mjs +0 -2
  7. package/runtime/core/host.d.ts +8 -0
  8. package/runtime/core/host.mjs +50 -2
  9. package/runtime/core/numbers.mjs +2 -3
  10. package/runtime/nish.d.ts +100 -4
  11. package/runtime/nish.h +34 -0
  12. package/runtime/nish.mjs +23 -5
  13. package/runtime/node.mjs +57 -6
  14. package/runtime/runtime-net.c +45 -4
  15. package/runtime/runtime-os.c +115 -12
  16. package/runtime/runtime-term.c +2 -2
  17. package/runtime/runtime.c +1 -1
  18. package/scripts/build.sh +13 -3
  19. package/std/README.md +44 -7
  20. package/std/cli-args.ts +1216 -0
  21. package/std/cli-flags.ts +585 -0
  22. package/std/cli.ts +308 -19
  23. package/std/compress/checksum.ts +290 -0
  24. package/std/compress/deflate.ts +2790 -0
  25. package/std/compress/gzip.ts +761 -0
  26. package/std/compress/zlib.ts +168 -0
  27. package/std/crypto/LICENSE-fiat-crypto-p384 +21 -0
  28. package/std/crypto/p256.ts +63 -1
  29. package/std/crypto/p384.ts +6044 -0
  30. package/std/crypto/pki-roots.ts +322 -0
  31. package/std/crypto/pki.ts +1207 -0
  32. package/std/crypto/rsa.ts +663 -0
  33. package/std/crypto/sha256.ts +2 -3
  34. package/std/crypto/x25519.ts +3 -4
  35. package/std/crypto/x509.ts +1054 -58
  36. package/std/net/dns-config.ts +734 -0
  37. package/std/net/dns-message.ts +517 -0
  38. package/std/net/dns-resolver.ts +1015 -0
  39. package/std/net/dns.ts +478 -0
  40. package/std/net/http-compress.ts +393 -0
  41. package/std/net/http1-client.ts +1227 -0
  42. package/std/net/http1-server.ts +101 -1
  43. package/std/net/http1.ts +9 -9
  44. package/std/net/tls/client.ts +1094 -0
  45. package/std/net/tls/codec.ts +585 -3
  46. package/std/net/tls/record-client.ts +1192 -0
  47. package/std/net/tls/verify.ts +221 -0
  48. package/std/net/tls-tcp.ts +642 -11
  49. package/std/net/tls.ts +2 -2
  50. package/std/net/url.ts +559 -4
  51. package/std/yaml-write.ts +415 -0
@@ -0,0 +1,168 @@
1
+ /**
2
+ * `nish/compress/zlib` — the zlib format (RFC 1950) around
3
+ * `nish/compress/deflate`: a two-byte header, the DEFLATE stream and the
4
+ * Adler-32 of the data, big-endian. What HTTP's `Content-Encoding: deflate`
5
+ * means (RFC 9110 §8.4.1.2), and what PNG and many protocols carry.
6
+ *
7
+ * import { DEFLATE_DEFAULT } from "nish/compress/deflate";
8
+ * import { ZlibCompressor, ZlibDecompressor } from "nish/compress/zlib";
9
+ *
10
+ * const z = new ZlibCompressor(DEFLATE_DEFAULT);
11
+ * const end: i32 = z.compress(src, 0, n, out, 0); // -1: no room
12
+ * const got: i32 = new ZlibDecompressor().decompress(out, 0, end, back, 0, n); // < 0: an error
13
+ *
14
+ * The objects allocate their tables once and nothing per call; the free
15
+ * functions build one for a single call. The header written is `0x78` and
16
+ * the `FLEVEL` of the level, with no preset dictionary. Reading refuses a
17
+ * method other than 8, a window over 32 KB, a header whose check fails
18
+ * (`ZLIB_ERR_HEADER`) and a preset dictionary (`ZLIB_ERR_DICTIONARY`, since
19
+ * none can be supplied); a wrong Adler-32 is `ZLIB_ERR_ADLER`, and bytes
20
+ * after the trailer are `DEFLATE_ERR_TRAILING`.
21
+ *
22
+ * Written from RFC 1950.
23
+ */
24
+ import { adler32 } from "nish/compress/checksum"
25
+ import {
26
+ DEFLATE_ERR_OUTPUT_LIMIT,
27
+ DEFLATE_ERR_TRAILING,
28
+ DEFLATE_ERR_TRUNCATED,
29
+ DEFLATE_NO_ROOM,
30
+ Deflater,
31
+ INFLATE_DONE,
32
+ INFLATE_NEED_OUTPUT,
33
+ Inflater,
34
+ } from "nish/compress/deflate"
35
+
36
+ /** A zlib header that is not one: the wrong method, a window over 32 KB, or a failed check. */
37
+ export const ZLIB_ERR_HEADER: i32 = -12
38
+
39
+ /** The trailer's Adler-32 is not the Adler-32 of the data. */
40
+ export const ZLIB_ERR_ADLER: i32 = -13
41
+
42
+ /** The stream needs a preset dictionary (`FDICT`), which this reader has no way to be given. */
43
+ export const ZLIB_ERR_DICTIONARY: i32 = -14
44
+
45
+ /** Panics unless `buf[off .. off + len)` lies inside `buf`. */
46
+ const zlibWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
47
+ if (off < 0 || len < 0 || off > toI32(buf.length) - len) {
48
+ panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${toI32(buf.length)} bytes`)
49
+ }
50
+ }
51
+
52
+ /** A zlib compressor at one level, allocated once. Not shared between threads. */
53
+ export class ZlibCompressor {
54
+ readonly deflater: Deflater
55
+
56
+ /** A compressor at `level`, 0 to 9; another level panics. */
57
+ constructor(level: i32) {
58
+ this.deflater = new Deflater(level)
59
+ }
60
+
61
+ /**
62
+ * Compresses `src[off .. off + len)` into `out` from `at` as one zlib
63
+ * stream and answers where it ends, or `DEFLATE_NO_ROOM` (-1) when `out`
64
+ * cannot hold it; `compressBound(len)` bytes always can. A window outside
65
+ * its array panics. Allocates nothing.
66
+ */
67
+ compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
68
+ zlibWindow("ZlibCompressor.compress", src, off, len)
69
+ zlibWindow("ZlibCompressor.compress", out, at, 0)
70
+ if (toI32(out.length) - at < 6) {
71
+ return DEFLATE_NO_ROOM
72
+ }
73
+ const level: i32 = this.deflater.level
74
+ // FLEVEL (§2.2): fastest, fast, default, slowest.
75
+ let flevel: i32 = 3
76
+ if (level < 2) {
77
+ flevel = 0
78
+ } else if (level < 6) {
79
+ flevel = 1
80
+ } else if (level === 6) {
81
+ flevel = 2
82
+ }
83
+ const cmf: i32 = 0x78
84
+ let flg: i32 = flevel << 6
85
+ flg += (toI32(31) - ((cmf * 256 + flg) % 31)) % 31
86
+ out[at] = toU8(cmf)
87
+ out[at + 1] = toU8(flg)
88
+ const end: i32 = this.deflater.compress(src, off, len, out, at + 2)
89
+ if (end < 0 || toI32(out.length) - end < 4) {
90
+ return DEFLATE_NO_ROOM
91
+ }
92
+ const a: u32 = adler32(src, off, len)
93
+ out[end] = toU8(a >>> 24)
94
+ out[end + 1] = toU8((a >>> 16) & 255)
95
+ out[end + 2] = toU8((a >>> 8) & 255)
96
+ out[end + 3] = toU8(a & 255)
97
+ return end + 4
98
+ }
99
+ }
100
+
101
+ /** A zlib decompressor, allocated once. Not shared between threads. */
102
+ export class ZlibDecompressor {
103
+ readonly inflater: Inflater
104
+
105
+ /** Builds the tables. */
106
+ constructor() {
107
+ this.inflater = new Inflater()
108
+ }
109
+
110
+ /**
111
+ * Decompresses the zlib stream `src[off .. off + len)` into `out` from
112
+ * `at`, writing at most `limit` bytes and never past `out`'s end, and
113
+ * answers where the output ends, or a negative `ZLIB_ERR_*` or
114
+ * `DEFLATE_ERR_*`. A window outside its array, or a negative `limit`,
115
+ * panics. Allocates nothing.
116
+ */
117
+ decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 {
118
+ zlibWindow("ZlibDecompressor.decompress", src, off, len)
119
+ zlibWindow("ZlibDecompressor.decompress", out, at, 0)
120
+ if (limit < 0) {
121
+ panic("ZlibDecompressor.decompress: a negative limit")
122
+ }
123
+ if (len < 2) {
124
+ return DEFLATE_ERR_TRUNCATED
125
+ }
126
+ const cmf: i32 = toI32(src[off])
127
+ const flg: i32 = toI32(src[off + 1])
128
+ if ((cmf & 15) !== 8 || cmf >> 4 > 7 || (cmf * 256 + flg) % 31 !== 0) {
129
+ return ZLIB_ERR_HEADER
130
+ }
131
+ if ((flg & 0x20) !== 0) {
132
+ return ZLIB_ERR_DICTIONARY
133
+ }
134
+ const room: i32 = toI32(out.length) - at
135
+ const outEnd: i32 = at + (limit < room ? limit : room)
136
+ const inf: Inflater = this.inflater
137
+ inf.reset()
138
+ const end: i32 = off + len
139
+ const r: i32 = inf.inflate(src, off + 2, end, false, out, at, outEnd, at)
140
+ if (r === INFLATE_NEED_OUTPUT) {
141
+ return DEFLATE_ERR_OUTPUT_LIMIT
142
+ }
143
+ if (r !== INFLATE_DONE) {
144
+ return r
145
+ }
146
+ const q: i32 = inf.inPos
147
+ if (end - q < 4) {
148
+ return DEFLATE_ERR_TRUNCATED
149
+ }
150
+ const want: u32 =
151
+ (toU32(src[q]) << 24) | (toU32(src[q + 1]) << 16) | (toU32(src[q + 2]) << 8) | toU32(src[q + 3])
152
+ if (adler32(out, at, inf.outPos - at) !== want) {
153
+ return ZLIB_ERR_ADLER
154
+ }
155
+ if (q + 4 !== end) {
156
+ return DEFLATE_ERR_TRAILING
157
+ }
158
+ return inf.outPos
159
+ }
160
+ }
161
+
162
+ /** `ZlibCompressor.compress` at `level`, building the compressor for this call alone. */
163
+ export const zlibCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, level: i32): i32 =>
164
+ new ZlibCompressor(level).compress(src, off, len, out, at)
165
+
166
+ /** `ZlibDecompressor.decompress`, building the decompressor for this call alone. */
167
+ export const zlibDecompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 =>
168
+ new ZlibDecompressor().decompress(src, off, len, out, at, limit)
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2020 the fiat-crypto authors (see the AUTHORS file).
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -112,7 +112,7 @@
112
112
  * functions share the importing program's flat symbol namespace
113
113
  * (`docs/wp26-stdlib.md` §3e).
114
114
  */
115
- import { Secret, expose, exposeWith, wipe } from "nish:secret"
115
+ import { Secret, expose, exposeWith, secret, wipe } from "nish:secret"
116
116
  import { SHA256_BLOCK, SHA256_SIZE, Sha256, sha256 } from "nish/crypto/sha256"
117
117
  import { timingSafeEqual } from "nish/crypto/ct"
118
118
 
@@ -5268,6 +5268,68 @@ const p256PublicKeyExposed = (priv: u8[]): u8[] | null => {
5268
5268
  return encoded
5269
5269
  }
5270
5270
 
5271
+ /**
5272
+ * The ECDH shared secret of `priv` and the peer's public key `peerPoint`
5273
+ * (SEC 1 §3.3.1): the 32-byte big-endian x coordinate of `priv * peerPoint`,
5274
+ * as TLS 1.3 uses it (RFC 8446 §7.4.2). Answers `null` unless `priv` is a
5275
+ * private key `p256PublicKey` would take and `peerPoint` is a 65-byte
5276
+ * uncompressed point on the curve, and for a product at infinity, which a
5277
+ * valid key and point cannot give. An x of zero is a point like any other
5278
+ * and is answered.
5279
+ */
5280
+ export const p256Ecdh = (priv: Secret<u8[]>, peerPoint: u8[]): Secret<u8[]> | null => {
5281
+ const shared: u8[] | null = exposeWith(priv, peerPoint, p256EcdhExposed)
5282
+ if (shared === null) {
5283
+ return null
5284
+ }
5285
+ return secret(shared)
5286
+ }
5287
+
5288
+ /**
5289
+ * `p256Ecdh` on the plain key, for `exposeWith` to run. The scratch, the
5290
+ * product and every limb of `X / Z` follow the key, so all are wiped; the
5291
+ * inversion is written out here, rather than through `p256AffineX`, so that its
5292
+ * accumulator and Z's inverse are this function's to wipe.
5293
+ */
5294
+ const p256EcdhExposed = (priv: u8[], peerPoint: u8[]): u8[] | null => {
5295
+ if (!p256ScalarInRange(priv)) {
5296
+ return null
5297
+ }
5298
+ const peer: P256ProjectivePoint | null = p256DecodePoint(peerPoint)
5299
+ if (peer === null) {
5300
+ return null
5301
+ }
5302
+ const point = new P256ProjectivePoint()
5303
+ const scratch = new P256PointScratch()
5304
+ p256ScalarMult(point, priv, peer, scratch)
5305
+ p256WipeScratch(scratch)
5306
+ const infinity: boolean = p256FiatNonzero(point.z) === 0
5307
+ // 1 / Z as Z^(p - 2), as p256FieldInvert computes it, over limbs wiped below.
5308
+ const e: u32[] = p256Prime()
5309
+ e[0] = 0xfffffffd
5310
+ const acc: u32[] = p256Limbs()
5311
+ p256FiatSetOne(acc)
5312
+ for (let i: i32 = 255; i >= 0; i--) {
5313
+ p256FiatSquare(acc, acc)
5314
+ if (p256Bit(e, i)) {
5315
+ p256FiatMul(acc, acc, point.z)
5316
+ }
5317
+ }
5318
+ const x: u32[] = p256Limbs()
5319
+ p256FiatMul(x, point.x, acc)
5320
+ p256FiatMul(x, x, p256PlainOne())
5321
+ const out: u8[] = new Array<u8>(P256_SCALAR_SIZE)
5322
+ p256LimbsToBytes(out, 0, x)
5323
+ wipe(x)
5324
+ wipe(acc)
5325
+ p256WipePoint(point)
5326
+ if (infinity) {
5327
+ wipe(out)
5328
+ return null
5329
+ }
5330
+ return out
5331
+ }
5332
+
5271
5333
  /**
5272
5334
  * The ECDSA signature (FIPS 186-4 §6.4) of `digest` under `priv`, as `r || s`,
5273
5335
  * 64 bytes, with the deterministic nonce of RFC 6979 §3.2 over HMAC-SHA-256.