@amritk/nish-x86_64-linux 0.35.0 → 0.36.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/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
@@ -3,12 +3,18 @@
3
3
  * deterministic nonces of RFC 6979.
4
4
  *
5
5
  * import { p256PublicKey, p256SignSha256, p256VerifySha256 } from "nish/crypto/p256";
6
+ * import { Secret, secret, wipe } from "nish:secret";
6
7
  *
7
- * const priv: Secret<u8[]> = secret(readKey()); // nish:secret
8
- * const pub: u8[] | null = p256PublicKey(priv); // 65 bytes, 0x04 || X || Y
9
- * const sig: u8[] | null = p256SignSha256(priv, message); // 64 bytes, r || s
10
- * wipe(priv);
11
- * const ok: boolean = p256VerifySha256(pub, message, sig);
8
+ * const raw: u8[] = new Array<u8>(32)
9
+ * crypto.getRandomValues(raw) // in [1, n) but for about one draw in 2^32
10
+ * const priv: Secret<u8[]> = secret(raw)
11
+ * const message: u8[] = [104, 105]
12
+ * const pub: u8[] | null = p256PublicKey(priv) // 65 bytes, 0x04 || X || Y
13
+ * const sig: u8[] | null = p256SignSha256(priv, message) // 64 bytes, r || s
14
+ * wipe(priv)
15
+ * if (pub !== null && sig !== null) {
16
+ * p256VerifySha256(pub, message, sig) // true
17
+ * }
12
18
  *
13
19
  * A private key is 32 big-endian bytes holding a scalar in [1, n); a public
14
20
  * key is the uncompressed SEC1 encoding of SEC 1 §2.3.3; a signature is `r`
@@ -4055,8 +4061,11 @@ const p256ScalarInverse = (out: u32[], a: u32[]): void => {
4055
4061
  * Z = 0 is the identity, which the complete formulas take like any other point.
4056
4062
  */
4057
4063
  class P256ProjectivePoint {
4064
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4058
4065
  x: u32[]
4066
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4059
4067
  y: u32[]
4068
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4060
4069
  z: u32[]
4061
4070
 
4062
4071
  /** The identity, (0 : 1 : 0). */
@@ -4074,13 +4083,21 @@ class P256ProjectivePoint {
4074
4083
  * selects into, and the curve's `b`, which the formulas multiply by.
4075
4084
  */
4076
4085
  class P256PointScratch {
4086
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4077
4087
  t0: u32[]
4088
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4078
4089
  t1: u32[]
4090
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4079
4091
  t2: u32[]
4092
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4080
4093
  t3: u32[]
4094
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4081
4095
  t4: u32[]
4096
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4082
4097
  sum: P256ProjectivePoint
4098
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4083
4099
  entry: P256ProjectivePoint
4100
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4084
4101
  b: u32[]
4085
4102
 
4086
4103
  constructor() {
@@ -5247,6 +5264,9 @@ const p256SignScalar = (priv: u8[], z: u32[]): u8[] => {
5247
5264
  * The public key of `priv`: `priv * G` in the 65-byte uncompressed SEC 1
5248
5265
  * encoding. Answers `null` unless `priv` is `P256_SCALAR_SIZE` bytes holding a
5249
5266
  * scalar in [1, n) — 0 and n and everything above are not private keys.
5267
+ *
5268
+ * @param priv The private key, 32 big-endian bytes.
5269
+ * @returns The 65-byte public key, or `null` for a key that is not one.
5250
5270
  */
5251
5271
  export const p256PublicKey = (priv: Secret<u8[]>): u8[] | null => expose(priv, p256PublicKeyExposed)
5252
5272
 
@@ -5276,6 +5296,11 @@ const p256PublicKeyExposed = (priv: u8[]): u8[] | null => {
5276
5296
  * uncompressed point on the curve, and for a product at infinity, which a
5277
5297
  * valid key and point cannot give. An x of zero is a point like any other
5278
5298
  * and is answered.
5299
+ *
5300
+ * @param priv This side's private key, 32 big-endian bytes.
5301
+ * @param peerPoint The peer's 65-byte uncompressed public key.
5302
+ * @returns The 32-byte shared secret, or `null` for a refused key or point and
5303
+ * a product at infinity.
5279
5304
  */
5280
5305
  export const p256Ecdh = (priv: Secret<u8[]>, peerPoint: u8[]): Secret<u8[]> | null => {
5281
5306
  const shared: u8[] | null = exposeWith(priv, peerPoint, p256EcdhExposed)
@@ -5337,6 +5362,10 @@ const p256EcdhExposed = (priv: u8[], peerPoint: u8[]): u8[] | null => {
5337
5362
  * bits. The same key and digest always give the same signature, and `s` is
5338
5363
  * not normalised to the lower half. Answers `null` for a private key
5339
5364
  * `p256PublicKey` would refuse.
5365
+ *
5366
+ * @param priv The private key, 32 big-endian bytes.
5367
+ * @param digest The message's hash; only its leftmost 256 bits count.
5368
+ * @returns The 64-byte signature `r || s`, or `null` for a refused key.
5340
5369
  */
5341
5370
  export const p256Sign = (priv: Secret<u8[]>, digest: u8[]): u8[] | null =>
5342
5371
  exposeWith(priv, digest, p256SignExposed)
@@ -5364,6 +5393,11 @@ const p256SignExposed = (priv: u8[], digest: u8[]): u8[] | null => {
5364
5393
  * verifies too, as FIPS 186-4 and SEC 1 have it. Nothing here asks for the
5365
5394
  * lower `s`, so a protocol that needs one signature per message and key (a
5366
5395
  * signature used as an identifier, say) has to refuse the upper half itself.
5396
+ *
5397
+ * @param pub The signer's 65-byte uncompressed public key.
5398
+ * @param digest The message's hash; only its leftmost 256 bits count.
5399
+ * @param sig The 64-byte signature `r || s`.
5400
+ * @returns `true` when the signature is valid, `false` for anything else.
5367
5401
  */
5368
5402
  export const p256Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
5369
5403
  if (toI32(sig.length) !== 64) {
@@ -5420,10 +5454,23 @@ export const p256Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
5420
5454
  return timingSafeEqual(vBytes, rBytes)
5421
5455
  }
5422
5456
 
5423
- /** `p256Sign` of the SHA-256 of `msg`: ECDSA with SHA-256 over the whole message. */
5457
+ /**
5458
+ * `p256Sign` of the SHA-256 of `msg`: ECDSA with SHA-256 over the whole message.
5459
+ *
5460
+ * @param priv The private key, 32 big-endian bytes.
5461
+ * @param msg The whole message, hashed here.
5462
+ * @returns The 64-byte signature `r || s`, or `null` for a refused key.
5463
+ */
5424
5464
  export const p256SignSha256 = (priv: Secret<u8[]>, msg: u8[]): u8[] | null => p256Sign(priv, sha256(msg))
5425
5465
 
5426
- /** `p256Verify` of the SHA-256 of `msg`. */
5466
+ /**
5467
+ * `p256Verify` of the SHA-256 of `msg`.
5468
+ *
5469
+ * @param pub The signer's 65-byte uncompressed public key.
5470
+ * @param msg The whole message, hashed here.
5471
+ * @param sig The 64-byte signature `r || s`.
5472
+ * @returns `true` when the signature is valid, `false` for anything else.
5473
+ */
5427
5474
  export const p256VerifySha256 = (pub: u8[], msg: u8[], sig: u8[]): boolean =>
5428
5475
  p256Verify(pub, sha256(msg), sig)
5429
5476
 
@@ -5653,34 +5700,121 @@ const p256WindowFits = (length: i32, at: i32, len: i32): boolean => at >= 0 && l
5653
5700
  * const ok: boolean = p256SignSha256Into(scratch, priv, msg, 0, msg.length, sig, 0);
5654
5701
  */
5655
5702
  export class P256SignScratch {
5656
- /** FIPS 180-4 §4.2.2's round constants. */
5703
+ /**
5704
+ * FIPS 180-4 §4.2.2's round constants.
5705
+ *
5706
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5707
+ */
5657
5708
  kt: u32[]
5658
- /** A SHA-256 hash value, schedule and padded tail. */
5709
+ /**
5710
+ * A SHA-256 hash value.
5711
+ *
5712
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5713
+ */
5659
5714
  hash: u32[]
5715
+ /**
5716
+ * A SHA-256 message schedule.
5717
+ *
5718
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5719
+ */
5660
5720
  schedule: u32[]
5721
+ /**
5722
+ * A SHA-256 message's padded tail.
5723
+ *
5724
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5725
+ */
5661
5726
  tail: u8[]
5662
- /** An HMAC key block and the message after it. */
5727
+ /**
5728
+ * An HMAC key block and the message after it.
5729
+ *
5730
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5731
+ */
5663
5732
  block: u8[]
5664
- /** `V || sep`, where every key update's message starts (RFC 6979 §3.2 steps d, f and h.3). */
5733
+ /**
5734
+ * `V || sep`, where every key update's message starts (RFC 6979 §3.2 steps d, f and h.3).
5735
+ *
5736
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5737
+ */
5665
5738
  message: u8[]
5666
- /** RFC 6979's `K` and `V`; the last `V` is the nonce. */
5739
+ /**
5740
+ * RFC 6979's `K`.
5741
+ *
5742
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5743
+ */
5667
5744
  k: u8[]
5745
+ /**
5746
+ * RFC 6979's `V`; the last `V` is the nonce.
5747
+ *
5748
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5749
+ */
5668
5750
  v: u8[]
5669
- /** The message's digest, then int2octets of it mod n: RFC 6979's `h1`. */
5751
+ /**
5752
+ * The message's digest, then int2octets of it mod n: RFC 6979's `h1`.
5753
+ *
5754
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5755
+ */
5670
5756
  h1: u8[]
5671
- /** The digest mod n, `r`, the nonce point's affine x, and `k^-1` in the Montgomery domain. */
5757
+ /**
5758
+ * The digest mod n.
5759
+ *
5760
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5761
+ */
5672
5762
  z: u32[]
5763
+ /**
5764
+ * The signature's `r`.
5765
+ *
5766
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5767
+ */
5673
5768
  r: u32[]
5769
+ /**
5770
+ * The nonce point's affine x.
5771
+ *
5772
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5773
+ */
5674
5774
  x: u32[]
5775
+ /**
5776
+ * The nonce `k`, then `k^-1`, in the Montgomery domain.
5777
+ *
5778
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5779
+ */
5675
5780
  kM: u32[]
5676
- /** The order n and R^2 mod n. */
5781
+ /**
5782
+ * The order n.
5783
+ *
5784
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5785
+ */
5677
5786
  n: u32[]
5787
+ /**
5788
+ * R^2 mod n.
5789
+ *
5790
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5791
+ */
5678
5792
  r2: u32[]
5679
- /** The nonce's multiple of G, the comb table of G and point scratch. */
5793
+ /**
5794
+ * The nonce's multiple of G.
5795
+ *
5796
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5797
+ */
5680
5798
  point: P256ProjectivePoint
5799
+ /**
5800
+ * The comb table of G.
5801
+ *
5802
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5803
+ */
5681
5804
  table: u32[]
5805
+ /**
5806
+ * The point arithmetic's scratch.
5807
+ *
5808
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5809
+ */
5682
5810
  points: P256PointScratch
5683
5811
 
5812
+ /**
5813
+ * Makes the arrays every signature reuses, the round constants and the
5814
+ * base point's comb table included, so the base-point multiplication works
5815
+ * in them; what `p256SignSha256Into` still allocates per call is released
5816
+ * when its arena block ends, so nothing is kept.
5817
+ */
5684
5818
  constructor() {
5685
5819
  this.kt = [
5686
5820
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
@@ -5803,6 +5937,15 @@ const p256WipeSignScratch = (sc: P256SignScratch): void => {
5803
5937
  * leaves the arena where it was; every secret the scratch held — RFC 6979's
5804
5938
  * `K` and `V`, the nonce's inverse, its point and the point scratch — is wiped
5805
5939
  * before it returns, as are the fresh arrays the key's own steps worked in.
5940
+ *
5941
+ * @param sc The scratch to sign in, one per signer.
5942
+ * @param priv The private key, 32 big-endian bytes.
5943
+ * @param msg The array holding the message.
5944
+ * @param off Where the message starts in `msg`.
5945
+ * @param len The message's length in bytes.
5946
+ * @param out The array the signature is written into.
5947
+ * @param at Where the 64 bytes start in `out`.
5948
+ * @returns `true` once the signature is written, `false` for a refused key.
5806
5949
  */
5807
5950
  export const p256SignSha256Into = (
5808
5951
  sc: P256SignScratch,
@@ -4,8 +4,11 @@
4
4
  *
5
5
  * import { p384Verify, p384VerifySha384 } from "nish/crypto/p384";
6
6
  *
7
- * const ok: boolean = p384VerifySha384(pub, message, sig); // pub: 97 bytes, sig: 96
8
- * const same: boolean = p384Verify(pub, sha256(message), sig); // a digest from any hash
7
+ * // `pub` is a 97-byte key and `sig` a 96-byte `r || s`, a certificate's, say
8
+ * const signedBy = (pub: u8[], message: u8[], sig: u8[]): boolean =>
9
+ * p384VerifySha384(pub, message, sig)
10
+ * const signedDigest = (pub: u8[], digest: u8[], sig: u8[]): boolean =>
11
+ * p384Verify(pub, digest, sig) // a digest from any hash
9
12
  *
10
13
  * A public key is the uncompressed SEC1 encoding of SEC 1 §2.3.3, 0x04 then X
11
14
  * and Y as 48 big-endian bytes each; a signature is `r` and `s` as 48
@@ -5708,8 +5711,11 @@ const p384ScalarInvert = (out: u32[], a: u32[]): void => {
5708
5711
  * Z = 0 is the identity, which the complete formulas take like any other point.
5709
5712
  */
5710
5713
  class P384ProjectivePoint {
5714
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5711
5715
  x: u32[]
5716
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5712
5717
  y: u32[]
5718
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5713
5719
  z: u32[]
5714
5720
 
5715
5721
  /** The identity, (0 : 1 : 0). */
@@ -5727,12 +5733,19 @@ class P384ProjectivePoint {
5727
5733
  * formulas multiply by.
5728
5734
  */
5729
5735
  class P384PointScratch {
5736
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5730
5737
  t0: u32[]
5738
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5731
5739
  t1: u32[]
5740
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5732
5741
  t2: u32[]
5742
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5733
5743
  t3: u32[]
5744
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5734
5745
  t4: u32[]
5746
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5735
5747
  sum: P384ProjectivePoint
5748
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5736
5749
  b: u32[]
5737
5750
 
5738
5751
  constructor() {
@@ -5995,6 +6008,11 @@ const p384DigestScalar = (out: u32[], digest: u8[]): void => {
5995
6008
  * verifies too, as FIPS 186-4 and SEC 1 have it. Nothing here asks for the
5996
6009
  * lower `s`, so a protocol that needs one signature per message and key has to
5997
6010
  * refuse the upper half itself.
6011
+ *
6012
+ * @param pub The signer's 97-byte uncompressed public key.
6013
+ * @param digest The message's hash; only its leftmost 384 bits count.
6014
+ * @param sig The 96-byte signature `r || s`.
6015
+ * @returns `true` when the signature is valid, `false` for anything else.
5998
6016
  */
5999
6017
  export const p384Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
6000
6018
  if (toI32(sig.length) !== 96) {
@@ -6039,6 +6057,13 @@ export const p384Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
6039
6057
  return timingSafeEqual(v, rBytes)
6040
6058
  }
6041
6059
 
6042
- /** `p384Verify` of the SHA-384 of `msg`: ECDSA with SHA-384 over the whole message. */
6060
+ /**
6061
+ * `p384Verify` of the SHA-384 of `msg`: ECDSA with SHA-384 over the whole message.
6062
+ *
6063
+ * @param pub The signer's 97-byte uncompressed public key.
6064
+ * @param msg The whole message, hashed here.
6065
+ * @param sig The 96-byte signature `r || s`.
6066
+ * @returns `true` when the signature is valid, `false` for anything else.
6067
+ */
6043
6068
  export const p384VerifySha384 = (pub: u8[], msg: u8[], sig: u8[]): boolean =>
6044
6069
  p384Verify(pub, sha384(msg), sig)
@@ -6,7 +6,7 @@
6
6
  * import { PkiRoots, pkiLoadSystemRoots } from "nish/crypto/pki-roots";
7
7
  *
8
8
  * const store: PkiRoots = pkiLoadSystemRoots();
9
- * // store.roots: X509Certificate[], parsed, no two with the same DER
9
+ * // store.roots: the parsed roots, no two with the same DER
10
10
  * // store.sources: the files they were read from, in order
11
11
  * // store.skipped: the PEM blocks and files that were not a root
12
12
  *
@@ -61,6 +61,7 @@ export class PkiRoots {
61
61
  /** The PEM blocks that were not a root, and the files too large or past the cap to read. */
62
62
  skipped: i32 = 0
63
63
 
64
+ /** An empty load: no roots, no sources, nothing skipped. */
64
65
  constructor() {
65
66
  this.roots = []
66
67
  this.sources = []
@@ -74,6 +75,8 @@ export class PkiRoots {
74
75
  * `crypto/x509/root_linux.go`, cited as facts, not copied code: Debian, Ubuntu,
75
76
  * Arch and Gentoo; Fedora and RHEL; openSUSE; OpenELEC; CentOS and RHEL 7;
76
77
  * Alpine, macOS and the BSDs.
78
+ *
79
+ * @returns The bundle paths, in a fresh array each call.
77
80
  */
78
81
  export const pkiSystemBundles = (): string[] => [
79
82
  "/etc/ssl/certs/ca-certificates.crt",
@@ -89,8 +92,11 @@ export const PKI_SYSTEM_DIR: string = "/etc/ssl/certs"
89
92
 
90
93
  /** What one load has read so far, and the DER already kept. */
91
94
  class PkiRootsLoad {
95
+ /** @internal */
92
96
  out: PkiRoots
97
+ /** @internal */
93
98
  seen: Set<string>
99
+ /** @internal */
94
100
  files: i32 = 0
95
101
 
96
102
  constructor() {
@@ -217,7 +223,14 @@ const pkiRootsIsDigit = (c: i32): boolean => c >= 48 && c <= 57
217
223
 
218
224
  const pkiRootsIsHex = (c: i32): boolean => pkiRootsIsDigit(c) || (c >= 97 && c <= 102)
219
225
 
220
- /** Whether a directory entry is one a trust directory holds: `*.pem`, `*.crt`, or the hash form `xxxxxxxx.N`. */
226
+ /**
227
+ * Whether a directory entry is one a trust directory holds: `*.pem`, `*.crt`,
228
+ * or the hash form `xxxxxxxx.N`.
229
+ *
230
+ * @param name The entry's name, without its directory.
231
+ * @returns `true` for a name ending `.pem` or `.crt` with something before
232
+ * it, or eight lowercase hex digits, a dot and one or more decimal digits.
233
+ */
221
234
  export const pkiRootsDirEntry = (name: string): boolean => {
222
235
  if (name.endsWith(".pem") || name.endsWith(".crt")) {
223
236
  return name.length > 4
@@ -259,6 +272,11 @@ const pkiRootsDir = (load: PkiRootsLoad, dir: string): void => {
259
272
  * first of `bundles` that reads as a file, then every directory of `dirs`, in
260
273
  * that order. A path that cannot be read contributes nothing; a duplicate DER
261
274
  * is kept once, where it was first read.
275
+ *
276
+ * @param file A file to read first, or `null` for none.
277
+ * @param dirs The directories to read every trust entry of.
278
+ * @param bundles The candidate bundle files; only the first that reads is used.
279
+ * @returns A new `PkiRoots` with what was found, empty when nothing was.
262
280
  */
263
281
  export const pkiLoadRoots = (file: string | null, dirs: string[], bundles: string[]): PkiRoots => {
264
282
  const load: PkiRootsLoad = new PkiRootsLoad()
@@ -298,6 +316,15 @@ const pkiRootsSplitDirs = (list: string): string[] => {
298
316
  * the order can be tested against fixture trees rather than the host's `/etc`.
299
317
  * `certFile` and `certDir` are `SSL_CERT_FILE` and `SSL_CERT_DIR`, `null`
300
318
  * when unset; an empty value counts as unset.
319
+ *
320
+ * @param certFile The value of `SSL_CERT_FILE`, or `null` when unset.
321
+ * @param certDir The value of `SSL_CERT_DIR`, a `:`-separated list, or `null`
322
+ * when unset.
323
+ * @param dirs The directories read when neither variable is set; ignored
324
+ * when either is.
325
+ * @param bundles The bundle files tried when neither variable is set;
326
+ * ignored when either is.
327
+ * @returns A new `PkiRoots`, as `pkiLoadRoots` gives it.
301
328
  */
302
329
  export const pkiLoadRootsFor = (
303
330
  certFile: string | null,
@@ -317,6 +344,9 @@ export const pkiLoadRootsFor = (
317
344
  * The roots this program should trust: those `SSL_CERT_FILE` and
318
345
  * `SSL_CERT_DIR` name when either is set and not empty, and otherwise the
319
346
  * first of `pkiSystemBundles()` that exists and `/etc/ssl/certs`.
347
+ *
348
+ * @returns A new `PkiRoots`, read afresh on every call; empty when nothing
349
+ * was found.
320
350
  */
321
351
  export const pkiLoadSystemRoots = (): PkiRoots =>
322
352
  pkiLoadRootsFor(getenv("SSL_CERT_FILE"), getenv("SSL_CERT_DIR"), [PKI_SYSTEM_DIR], pkiSystemBundles())
package/std/crypto/pki.ts CHANGED
@@ -7,13 +7,22 @@
7
7
  * import { pkiLoadSystemRoots } from "nish/crypto/pki-roots";
8
8
  * import { PKI_OK, PkiResult, PkiTrustStore, pkiReason, pkiTrustStore, pkiVerifyServer } from "nish/crypto/pki";
9
9
  *
10
- * const store: PkiTrustStore = pkiTrustStore(pkiLoadSystemRoots().roots);
11
- * // chain: each certificate's DER as the server sent it, its own first
12
- * const r: PkiResult = pkiVerifyServer(chain, "example.com", toI64(Date.now()), store);
13
- * if (r.code !== PKI_OK) {
14
- * console.log(pkiReason(r.code));
15
- * }
16
- * // r.path, when PKI_OK: the leaf first, the trusted root last
10
+ * // chain: each certificate's DER as the server sent it, the leaf first
11
+ * const check = (chain: u8[][], host: string, store: PkiTrustStore): boolean => {
12
+ * const r: PkiResult = pkiVerifyServer(chain, host, toI64(Date.now()), store);
13
+ * if (r.code !== PKI_OK) {
14
+ * console.log(pkiReason(r.code));
15
+ * return false;
16
+ * }
17
+ * // r.path: the leaf first, the trusted root last
18
+ * return true;
19
+ * };
20
+ *
21
+ * export const main = (): i32 => {
22
+ * const store: PkiTrustStore = pkiTrustStore(pkiLoadSystemRoots().roots);
23
+ * const chain: u8[][] = [];
24
+ * return check(chain, "example.com", store) ? 0 : 1;
25
+ * };
17
26
  *
18
27
  * The network chooses every byte of `chain` (docs/security/crypto-pki.md,
19
28
  * "Threat model"), so every refusal has a reason code of its own and every
@@ -155,9 +164,10 @@ const PKI_AT_ROOT: i32 = 1000
155
164
  export class PkiTrustStore {
156
165
  /** Every root, in the order given, no two with the same DER. */
157
166
  roots: X509Certificate[]
158
- /** The roots under each subject, keyed by the subject's DER in hex. */
167
+ /** @internal The roots under each subject, keyed by the subject's DER in hex. */
159
168
  bySubject: Map<string, X509Certificate[]>
160
169
 
170
+ /** An empty store; `pkiTrustStore` builds a filled one. */
161
171
  constructor() {
162
172
  this.roots = []
163
173
  this.bySubject = new Map<string, X509Certificate[]>()
@@ -171,6 +181,12 @@ export class PkiResult {
171
181
  /** When `PKI_OK`, the path: the leaf first, the root last; otherwise empty. */
172
182
  path: X509Certificate[]
173
183
 
184
+ /**
185
+ * One verification's answer, as `pkiVerifyServer` gives it.
186
+ *
187
+ * @param code `PKI_OK` or the reason the chain was refused.
188
+ * @param path The path found, kept as given, not copied.
189
+ */
174
190
  constructor(code: i32, path: X509Certificate[]) {
175
191
  this.code = code
176
192
  this.path = path
@@ -179,18 +195,25 @@ export class PkiResult {
179
195
 
180
196
  /** One call's search: the pool, the host, the path so far, and what has been spent. */
181
197
  class PkiSearch {
198
+ /** @internal */
182
199
  store: PkiTrustStore
200
+ /** @internal */
183
201
  pool: X509Certificate[]
202
+ /** @internal */
184
203
  path: X509Certificate[]
185
- /** The host as a DNS name, normalised, or `""` when it is an address. */
204
+ /** @internal The host as a DNS name, normalised, or `""` when it is an address. */
186
205
  host: string
206
+ /** @internal */
187
207
  nowMs: i64 = 0
208
+ /** @internal */
188
209
  signatures: i32 = 0
210
+ /** @internal */
189
211
  nameChecks: i32 = 0
190
- /** The reason of the attempt nearest a root, and how near it got. */
212
+ /** @internal The reason of the attempt nearest a root, and how near it got. */
191
213
  bestCode: i32 = 0
214
+ /** @internal */
192
215
  bestScore: i32 = -1
193
- /** Whether a bound was spent, which ends the search. */
216
+ /** @internal Whether a bound was spent, which ends the search. */
194
217
  spent: boolean = false
195
218
 
196
219
  constructor(store: PkiTrustStore, pool: X509Certificate[], host: string, nowMs: i64) {
@@ -485,7 +508,12 @@ const pkiHostDns = (hostname: string): string | null => {
485
508
  * compared without case after one trailing dot is stripped from each side,
486
509
  * against the dNSNames, a wildcard only as a whole leftmost label. The
487
510
  * subject's common name is never read, so a certificate without a
488
- * subjectAltName names no host.
511
+ * subjectAltName names no host. Nothing but the names is checked.
512
+ *
513
+ * @param leaf The certificate the server presented as its own.
514
+ * @param hostname The name or address dialled: a DNS name, ASCII, or an IPv4
515
+ * or IPv6 address, the IPv6 one bare or in brackets.
516
+ * @returns `true` when one of `leaf`'s names matches `hostname`.
489
517
  */
490
518
  export const pkiMatchHostname = (leaf: X509Certificate, hostname: string): boolean => {
491
519
  const ip: u8[] | null = pkiParseIp(pkiStripDot(hostname))
@@ -610,6 +638,11 @@ const pkiSignatureCode = (cert: X509Certificate, issuer: X509Certificate): i32 =
610
638
  * or SHA-384 under P-256 or P-384, RSA PKCS#1 v1.5 with SHA-256, SHA-384 or
611
639
  * SHA-512 and RSASSA-PSS under a key of `PKI_MIN_RSA_BITS` or more whose
612
640
  * modulus is a whole number of bytes, and Ed25519. Nothing else about either certificate is checked.
641
+ *
642
+ * @param cert The certificate whose signature is checked.
643
+ * @param issuer The certificate whose subject public key is to have made it.
644
+ * @returns `true` when the signature verifies; `false` for a bad signature, a
645
+ * weak or unparsable key, or an algorithm not listed here.
613
646
  */
614
647
  export const pkiVerifySignature = (cert: X509Certificate, issuer: X509Certificate): boolean =>
615
648
  pkiSignatureCode(cert, issuer) === PKI_OK
@@ -1061,7 +1094,15 @@ const pkiLeafCode = (leaf: X509Certificate, hostname: string, nowMs: i64): i32 =
1061
1094
  return pkiMatchHostname(leaf, hostname) ? PKI_OK : PKI_ERR_HOSTNAME
1062
1095
  }
1063
1096
 
1064
- /** A store of `roots`, indexed by subject; a root whose DER is one already given is kept once. */
1097
+ /**
1098
+ * A store of `roots`, indexed by subject; a root whose DER is one already
1099
+ * given is kept once. Nothing about a root is checked here: its validity and
1100
+ * extensions are checked when a path reaches it.
1101
+ *
1102
+ * @param roots The certificates to trust, as `x509ParseCertificate` gave them.
1103
+ * @returns A new store, whose lists hold the given certificates themselves,
1104
+ * not copies; `roots` itself is not kept.
1105
+ */
1065
1106
  export const pkiTrustStore = (roots: X509Certificate[]): PkiTrustStore => {
1066
1107
  const store = new PkiTrustStore()
1067
1108
  for (const root of roots) {
@@ -1093,7 +1134,27 @@ export const pkiTrustStore = (roots: X509Certificate[]): PkiTrustStore => {
1093
1134
  * ASCII (an internationalised name in its A-label form); `nowMs` is the
1094
1135
  * instant to judge validity at, milliseconds since 1970-01-01 UTC; `store`
1095
1136
  * holds the roots trusted. `PKI_OK` with the path, or the reason it was
1096
- * refused — see the module's header for which reason when several apply.
1137
+ * refused.
1138
+ *
1139
+ * When several reasons apply, the first in this order is given: an empty
1140
+ * chain is `PKI_ERR_MALFORMED`; more than `PKI_MAX_CHAIN_CERTS` certificates
1141
+ * or `PKI_MAX_CHAIN_BYTES` bytes is `PKI_ERR_CHAIN_TOO_LONG`; then, certificate
1142
+ * by certificate in the order sent, one that does not parse is
1143
+ * `PKI_ERR_MALFORMED` and one with more than `PKI_MAX_NAMES` names or
1144
+ * `PKI_MAX_SUBTREES` subtrees is `PKI_ERR_BUDGET`; then the leaf's own checks,
1145
+ * in turn its validity, an unknown critical extension, being a CA, its key
1146
+ * usage, its extended key usage and the host name; then the path search,
1147
+ * whose reason the module's header ranks, or `PKI_ERR_BUDGET` when it ran out
1148
+ * of signatures or name comparisons; and last, once a path is found, the
1149
+ * leaf's profile.
1150
+ *
1151
+ * @param chain Each certificate's DER as the server sent it, the leaf first.
1152
+ * @param hostname The name or address dialled.
1153
+ * @param nowMs The instant to judge validity at, in milliseconds since
1154
+ * 1970-01-01 UTC.
1155
+ * @param store The roots trusted.
1156
+ * @returns A new result: `PKI_OK` with a fresh path, or a reason with an
1157
+ * empty one.
1097
1158
  */
1098
1159
  export const pkiVerifyServer = (
1099
1160
  chain: u8[][],
@@ -1150,7 +1211,12 @@ export const pkiVerifyServer = (
1150
1211
  return new PkiResult(s.spent ? PKI_ERR_BUDGET : s.bestCode, none)
1151
1212
  }
1152
1213
 
1153
- /** A sentence for `code`, for a log or an error message. */
1214
+ /**
1215
+ * A sentence for `code`, for a log or an error message.
1216
+ *
1217
+ * @param code `PKI_OK` or a `PKI_ERR_` constant.
1218
+ * @returns What the code means, or "unknown reason" for any other number.
1219
+ */
1154
1220
  export const pkiReason = (code: i32): string => {
1155
1221
  if (code === PKI_OK) {
1156
1222
  return "the chain is trusted and names the host"
package/std/crypto/rsa.ts CHANGED
@@ -5,9 +5,14 @@
5
5
  *
6
6
  * import { RSA_HASH_SHA256, RsaPublicKey, rsaParsePublicKey, rsaVerifyPkcs1v15, rsaVerifyPss } from "nish/crypto/rsa";
7
7
  *
8
- * const key: RsaPublicKey | null = rsaParsePublicKey(der); // PKCS#1 RSAPublicKey
9
- * const ok: boolean = key !== null && rsaVerifyPkcs1v15(key, RSA_HASH_SHA256, message, sig);
10
- * const pss: boolean = key !== null && rsaVerifyPss(key, RSA_HASH_SHA256, 32, message, sig);
8
+ * // der: a PKCS#1 RSAPublicKey; sig: as long as the modulus
9
+ * const verify = (der: u8[], message: u8[], sig: u8[]): boolean => {
10
+ * const key: RsaPublicKey | null = rsaParsePublicKey(der);
11
+ * if (key === null) {
12
+ * return false;
13
+ * }
14
+ * return rsaVerifyPkcs1v15(key, RSA_HASH_SHA256, message, sig) || rsaVerifyPss(key, RSA_HASH_SHA256, 32, message, sig);
15
+ * };
11
16
  *
12
17
  * **The key** is the DER of PKCS#1's `RSAPublicKey ::= SEQUENCE { modulus
13
18
  * INTEGER, publicExponent INTEGER }` (RFC 8017 §A.1.1), nothing before it and
@@ -87,6 +92,13 @@ export class RsaPublicKey {
87
92
  /** The modulus's length in bits: 2048 for a 2048-bit key. */
88
93
  bits: i32
89
94
 
95
+ /**
96
+ * Keeps the arrays given, without a copy or a check.
97
+ *
98
+ * @param n The modulus, big-endian with no leading zero byte.
99
+ * @param e The public exponent, big-endian with no leading zero byte.
100
+ * @param bits The modulus's length in bits.
101
+ */
90
102
  constructor(n: u8[], e: u8[], bits: i32) {
91
103
  this.n = n
92
104
  this.e = e
@@ -213,6 +225,10 @@ const rsaBytesBelow = (a: u8[], b: u8[]): boolean => {
213
225
  * after it; for a modulus that is even or not `RSA_MIN_BITS` to
214
226
  * `RSA_MAX_BITS` bits long; and for an exponent that is even, below 3, or
215
227
  * not below 2^32 (which also keeps it below the modulus).
228
+ *
229
+ * @param der The key's DER, nothing before it and nothing after it.
230
+ * @returns A new key whose modulus and exponent are copied out of `der`, or
231
+ * `null`.
216
232
  */
217
233
  export const rsaParsePublicKey = (der: u8[]): RsaPublicKey | null => {
218
234
  const total: i32 = toI32(der.length)
@@ -553,6 +569,12 @@ const rsaSameBytes = (a: u8[], b: u8[]): boolean => {
553
569
  * §9.2) and compared with what it does open to. `false` for an unknown hash
554
570
  * id, a modulus too short for the encoding, a signature that is not exactly
555
571
  * as long as the modulus or not below it, and any other bytes.
572
+ *
573
+ * @param key The signer's public key.
574
+ * @param hash `RSA_HASH_SHA256`, `RSA_HASH_SHA384` or `RSA_HASH_SHA512`.
575
+ * @param msg The message signed, whole; it is hashed here.
576
+ * @param sig The signature, big-endian.
577
+ * @returns `true` only when `sig` is `key`'s signature of `msg`.
556
578
  */
557
579
  export const rsaVerifyPkcs1v15 = (key: RsaPublicKey, hash: i32, msg: u8[], sig: u8[]): boolean => {
558
580
  const hashSize: i32 = rsaHashSize(hash)
@@ -613,6 +635,14 @@ const rsaMgf1 = (hash: i32, seed: u8[], length: i32): u8[] => {
613
635
  * the salt, and the hash. `false` for any of them, an unknown hash id, a
614
636
  * negative `saltLen`, and a signature that is not exactly as long as the
615
637
  * modulus or not below it.
638
+ *
639
+ * @param key The signer's public key.
640
+ * @param hash `RSA_HASH_SHA256`, `RSA_HASH_SHA384` or `RSA_HASH_SHA512`.
641
+ * @param saltLen The salt's length in bytes; a signature with any other
642
+ * length is refused.
643
+ * @param msg The message signed, whole; it is hashed here.
644
+ * @param sig The signature, big-endian.
645
+ * @returns `true` only when `sig` is `key`'s signature of `msg`.
616
646
  */
617
647
  export const rsaVerifyPss = (key: RsaPublicKey, hash: i32, saltLen: i32, msg: u8[], sig: u8[]): boolean => {
618
648
  const hashSize: i32 = rsaHashSize(hash)