@amritk/nish-x86_64-linux 0.24.0 → 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 (42) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +24 -4
  3. package/package.json +1 -1
  4. package/runtime/browser.mjs +4 -3
  5. package/runtime/core/host.d.ts +8 -0
  6. package/runtime/core/host.mjs +50 -2
  7. package/runtime/nish.d.ts +100 -0
  8. package/runtime/nish.h +34 -0
  9. package/runtime/nish.mjs +21 -0
  10. package/runtime/node.mjs +57 -6
  11. package/runtime/runtime-net.c +44 -3
  12. package/runtime/runtime-os.c +115 -12
  13. package/scripts/build.sh +13 -3
  14. package/std/README.md +25 -8
  15. package/std/cli-args.ts +1216 -0
  16. package/std/cli-flags.ts +585 -0
  17. package/std/cli.ts +308 -19
  18. package/std/compress/checksum.ts +132 -20
  19. package/std/compress/deflate.ts +638 -248
  20. package/std/compress/gzip.ts +20 -6
  21. package/std/crypto/LICENSE-fiat-crypto-p384 +21 -0
  22. package/std/crypto/p256.ts +63 -1
  23. package/std/crypto/p384.ts +6044 -0
  24. package/std/crypto/pki-roots.ts +322 -0
  25. package/std/crypto/pki.ts +1207 -0
  26. package/std/crypto/rsa.ts +663 -0
  27. package/std/crypto/x509.ts +1054 -58
  28. package/std/net/dns-config.ts +734 -0
  29. package/std/net/dns-message.ts +517 -0
  30. package/std/net/dns-resolver.ts +1015 -0
  31. package/std/net/dns.ts +478 -0
  32. package/std/net/http1-client.ts +1227 -0
  33. package/std/net/http1-server.ts +7 -2
  34. package/std/net/http1.ts +9 -9
  35. package/std/net/tls/client.ts +1094 -0
  36. package/std/net/tls/codec.ts +585 -3
  37. package/std/net/tls/record-client.ts +1192 -0
  38. package/std/net/tls/verify.ts +221 -0
  39. package/std/net/tls-tcp.ts +642 -11
  40. package/std/net/tls.ts +2 -2
  41. package/std/net/url.ts +559 -4
  42. package/std/yaml-write.ts +415 -0
@@ -67,7 +67,7 @@ export const GZIP_END: i32 = -100
67
67
  const GZIP_HEADER_SIZE: i32 = 10
68
68
  const GZIP_TRAILER_SIZE: i32 = 8
69
69
 
70
- /** The bytes a `GzipWriter` holds before it compresses them: one window of history and two of input. */
70
+ /** The bytes a `GzipWriter` holds: 32 KB of history, and up to 64 KB of input before it compresses them. */
71
71
  const GZIP_WRITER_HISTORY: i32 = 32768
72
72
  const GZIP_WRITER_INPUT: i32 = 65536
73
73
 
@@ -337,9 +337,16 @@ export class GzipWriter {
337
337
  this.finished = false
338
338
  }
339
339
 
340
- /** The most bytes a `write` of `len` bytes, or a `flush` or `finish` after it, can produce. */
340
+ /**
341
+ * The most bytes a `write` of `len` bytes, or a `flush` or `finish` after
342
+ * it, can produce. Less than 64 KB is pending before the call, and the
343
+ * call compresses once per 64 KB pending: `deflateBound` of all of it,
344
+ * with the constant part of `deflateBound` again for each compression
345
+ * after the first.
346
+ */
341
347
  bound(len: i32): i32 {
342
- return deflateBound(GZIP_WRITER_INPUT + len) + GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE + 10
348
+ const drains: i32 = (len >> 16) + 1
349
+ return deflateBound(GZIP_WRITER_INPUT + len) + drains * 18 + GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE + 10
343
350
  }
344
351
 
345
352
  /** Writes the header into `out` at `at` if it has not been; answers the new position. */
@@ -404,16 +411,17 @@ export class GzipWriter {
404
411
  this.size = this.size + toU32(len)
405
412
  let p: i32 = off
406
413
  const end: i32 = off + len
407
- const cap: i32 = toI32(this.window.length)
414
+ // Compressed once 64 KB is pending, the first window too: `pending` is
415
+ // at most `GZIP_WRITER_HISTORY` here, so the window always has the room.
408
416
  while (p < end) {
409
- let take: i32 = cap - this.fill
417
+ let take: i32 = GZIP_WRITER_INPUT - (this.fill - this.pending)
410
418
  if (take > end - p) {
411
419
  take = end - p
412
420
  }
413
421
  copyBytes(this.window, this.fill, src, p, take)
414
422
  this.fill += take
415
423
  p += take
416
- if (this.fill === cap) {
424
+ if (this.fill - this.pending === GZIP_WRITER_INPUT) {
417
425
  o = this.drain(out, o, false)
418
426
  if (o < 0) {
419
427
  panic("GzipWriter.write: a block larger than its bound")
@@ -439,6 +447,9 @@ export class GzipWriter {
439
447
  let o: i32 = this.header(out, at)
440
448
  if (this.fill > this.pending) {
441
449
  o = this.drain(out, o, false)
450
+ if (o < 0) {
451
+ panic("GzipWriter.flush: a block larger than its bound")
452
+ }
442
453
  }
443
454
  o = this.deflater.writeStored(this.window, toI32(0), toI32(0), false, out, o, toI32(out.length))
444
455
  return o
@@ -459,6 +470,9 @@ export class GzipWriter {
459
470
  }
460
471
  let o: i32 = this.header(out, at)
461
472
  o = this.drain(out, o, true)
473
+ if (o < 0) {
474
+ panic("GzipWriter.finish: a block larger than its bound")
475
+ }
462
476
  o = this.deflater.flushBits(out, o, toI32(out.length))
463
477
  if (o < 0 || toI32(out.length) - o < GZIP_TRAILER_SIZE) {
464
478
  panic("GzipWriter.finish: a block larger than its bound")
@@ -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.