nish 0.35.0 → 0.37.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 (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. 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`
@@ -24,27 +30,29 @@
24
30
  *
25
31
  * **What is ported and what is not.** The `p256Fiat*` functions are
26
32
  * fiat-crypto's generated, formally verified Montgomery arithmetic on eight
27
- * `u32` limbs with `u64` products — `mul`, `square`, `add`, `sub`,
28
- * `selectznz`, `nonzero` and `set_one` mod p, and `mul`, `add` and `set_one`
29
- * mod n — statement for statement, with fiat's variable names. Three things
30
- * change in the translation and nothing else does. C's out-pointers become a
31
- * `u64` answer: each of fiat's `addcarryx`, `subborrowx` and `mulx` helpers
32
- * here answers its two outputs packed, the first in the low 32 bits and the
33
- * second above them, and the caller splits them, declaring only the halves
34
- * fiat goes on to read. And each function opens with
35
- * a length guard, so that the literal indices after it compile with no bounds
36
- * check. And `square` reads its input's eight limbs into locals first, which
37
- * changes no arithmetic; it kept the input's pointer from being spilled in a
38
- * way the aarch64 disassembly check once misread (#334). The
39
- * scalar functions use the field's four helpers, which fiat generates
40
- * identically for both. The nonce's inverse, `p256ScalarInverse`, is
41
- * libsecp256k1's 32-bit safegcd (`modinv32`), function for function; the
42
- * section that holds it says what changes. Everything else in this file — the
43
- * curve, the encodings, the other inversions and ECDSA — is written for this
44
- * module from the documents it names.
33
+ * `u32` limbs with `u64` products — `mul`, `square`, `add`, `sub`, `selectznz`,
34
+ * `nonzero` and `set_one` mod p, and `mul`, `add` and `set_one` mod n —
35
+ * statement for statement, with fiat's variable names. Three things change in
36
+ * the translation and nothing else does.
45
37
  *
46
- * **The curve** is `y^2 = x^3 - 3x + b` over GF(p), p = 2^256 - 2^224 + 2^192
47
- * + 2^96 - 1, with the base point G and its order n from SEC 2 §2.4.2 (FIPS
38
+ * C's out-pointers become a `u64` answer: each of fiat's `addcarryx`,
39
+ * `subborrowx` and `mulx` helpers here answers its two outputs packed, the
40
+ * first in the low 32 bits and the second above them, and the caller splits
41
+ * them, declaring only the halves fiat goes on to read. And each function opens
42
+ * with a length guard, so that the literal indices after it compile with no
43
+ * bounds check.
44
+ *
45
+ * And `square` reads its input's eight limbs into locals first, which changes
46
+ * no arithmetic; it kept the input's pointer from being spilled in a way the
47
+ * aarch64 disassembly check once misread (#334). The scalar functions use the
48
+ * field's four helpers, which fiat generates identically for both. The nonce's
49
+ * inverse, `p256ScalarInverse`, is libsecp256k1's 32-bit safegcd (`modinv32`),
50
+ * function for function; the section that holds it says what changes.
51
+ * Everything else in this file — the curve, the encodings, the other inversions
52
+ * and ECDSA — is written for this module from the documents it names.
53
+ *
54
+ * **The curve** is `y^2 = x^3 - 3x + b` over GF(p),
55
+ * p = 2^256 - 2^224 + 2^192 + 2^96 - 1, with the base point G and its order n from SEC 2 §2.4.2 (FIPS
48
56
  * 186-4 §D.1.2.3). Points are projective (X : Y : Z), Z = 0 the identity, and
49
57
  * are added and doubled with the complete formulas of J. Renes, C. Costello and
50
58
  * L. Batina, "Complete addition formulas for prime order elliptic curves"
@@ -58,44 +66,49 @@
58
66
  * sums of a tooth's multiples of G, one per 4-bit offset, as affine points in
59
67
  * the Montgomery domain (`p256BaseTable`, 15 KiB, written by
60
68
  * `scripts/gen-p256-table.mjs`). Four rows of 16 mixed additions with a
61
- * doubling between rows replace the 256 doublings and 64 additions of a
62
- * window, whatever the scalar. Each addition reads all fifteen entries of its
63
- * sub-table and keeps one with fiat's conditional move, steered by `ctEq` on
64
- * the digit; a zero digit keeps none, and the sum is then discarded by the
65
- * same conditional move rather than skipped, so neither a branch nor an
66
- * address depends on the scalar. `p256ScalarMult`, a fixed 4-bit window over
67
- * sixteen multiples of a point, is what verification multiplies G and the
68
- * received key by, and the reference the comb is held to bit for bit
69
- * (`tests/link/crypto_p256_agree`). The signer inverts the nonce by
70
- * Bernstein and Yang's safegcd, `p256ScalarInverse`: twenty batches of thirty
71
- * divsteps, each a fixed run of masks and adds, whatever the scalar, so its
72
- * loops count constants and no branch or address follows the nonce; Fermat's
73
- * `p256ScalarInvert` is the reference it is held to bit for bit
74
- * (`tests/link/crypto_p256_inverse`) and what verification still uses. The
75
- * other inversions raise to the public exponents p - 2 and n - 2, so their
76
- * branches follow a constant. The
77
- * branches that remain are on public facts: a length, a limb or bit position,
78
- * whether a received key or signature is in range, and — once in about 2^32
79
- * signatures — RFC 6979's retry when a candidate nonce is not below n.
69
+ * doubling between rows replace the 256 doublings and 64 additions of a window,
70
+ * whatever the scalar.
71
+ *
72
+ * Each addition reads all fifteen entries of its sub-table and keeps one with
73
+ * fiat's conditional move, steered by `ctEq` on the digit; a zero digit keeps
74
+ * none, and the sum is then discarded by the same conditional move rather than
75
+ * skipped, so neither a branch nor an address depends on the scalar.
76
+ * `p256ScalarMult`, a fixed 4-bit window over sixteen multiples of a point, is
77
+ * what verification multiplies G and the received key by, and the reference the
78
+ * comb is held to bit for bit (`tests/link/crypto_p256_agree`).
79
+ *
80
+ * The signer inverts the nonce by Bernstein and Yang's safegcd,
81
+ * `p256ScalarInverse`: twenty batches of thirty divsteps, each a fixed run of
82
+ * masks and adds, whatever the scalar, so its loops count constants and no
83
+ * branch or address follows the nonce; Fermat's `p256ScalarInvert` is the
84
+ * reference it is held to bit for bit (`tests/link/crypto_p256_inverse`) and
85
+ * what verification still uses. The other inversions raise to the public
86
+ * exponents p - 2 and n - 2, so their branches follow a constant.
87
+ *
88
+ * The branches that remain are on public facts: a length, a limb or bit
89
+ * position, whether a received key or signature is in range, and — once in
90
+ * about 2^32 signatures — RFC 6979's retry when a candidate nonce is not below
91
+ * n.
80
92
  *
81
93
  * **What is verified by disassembly and what is discipline.**
82
- * `tests/cases/ct_asm_p256` holds copies of fiat's field multiply, square, add
83
- * and subtract, its scalar multiply and its conditional move, the table read
84
- * by a secret digit, the complete doubling and addition, one window step of
85
- * `p256ScalarMult`, the comb's digit, entry read, mixed addition,
86
- * conditional keep and one whole comb step, and safegcd's thirty divsteps and
87
- * its two matrix updates, to the check of
88
- * `tests/ct-asm.js`, which refuses any branch, call it cannot follow or
89
- * secret-indexed access in them on x86-64 and aarch64, following each call
90
- * into the field functions. The copies of the point functions take coordinate
91
- * arrays rather than points, the table reads' loops are written out there and
92
- * their offsets computed with wrapping arithmetic, as that fixture's header
93
- * says; its `main` runs RFC 6979 A.2.5 through them, by the window and by the
94
- * comb, and three inverses through the safegcd copies. The 64-window and 4-row
95
- * loops, the table build, the inversions (safegcd's twenty-batch loop, its
96
- * conversions, normalisation and limb loops among them), the encodings and the
97
- * nonce derivation are constant time by construction rather than by that
98
- * proof.
94
+ *
95
+ * `tests/cases/ct_asm_p256` holds copies of fiat's field multiply, square, add
96
+ * and subtract, its scalar multiply and its conditional move, the table read by
97
+ * a secret digit, the complete doubling and addition, one window step of
98
+ * `p256ScalarMult`, the comb's digit, entry read, mixed addition, conditional
99
+ * keep and one whole comb step, and safegcd's thirty divsteps and its two
100
+ * matrix updates, to the check of `tests/ct-asm.js`, which refuses any branch,
101
+ * call it cannot follow or secret-indexed access in them on x86-64 and aarch64,
102
+ * following each call into the field functions.
103
+ *
104
+ * The copies of the point functions take coordinate arrays rather than points,
105
+ * the table reads' loops are written out there and their offsets computed with
106
+ * wrapping arithmetic, as that fixture's header says; its `main` runs RFC 6979
107
+ * A.2.5 through them, by the window and by the comb, and three inverses through
108
+ * the safegcd copies. The 64-window and 4-row loops, the table build, the
109
+ * inversions (safegcd's twenty-batch loop, its conversions, normalisation and
110
+ * limb loops among them), the encodings and the nonce derivation are constant
111
+ * time by construction rather than by that proof.
99
112
  *
100
113
  * Verification multiplies only public values, by `p256ScalarMult` alone.
101
114
  *
@@ -4055,8 +4068,11 @@ const p256ScalarInverse = (out: u32[], a: u32[]): void => {
4055
4068
  * Z = 0 is the identity, which the complete formulas take like any other point.
4056
4069
  */
4057
4070
  class P256ProjectivePoint {
4071
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4058
4072
  x: u32[]
4073
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4059
4074
  y: u32[]
4075
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4060
4076
  z: u32[]
4061
4077
 
4062
4078
  /** The identity, (0 : 1 : 0). */
@@ -4074,13 +4090,21 @@ class P256ProjectivePoint {
4074
4090
  * selects into, and the curve's `b`, which the formulas multiply by.
4075
4091
  */
4076
4092
  class P256PointScratch {
4093
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4077
4094
  t0: u32[]
4095
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4078
4096
  t1: u32[]
4097
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4079
4098
  t2: u32[]
4099
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4080
4100
  t3: u32[]
4101
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4081
4102
  t4: u32[]
4103
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4082
4104
  sum: P256ProjectivePoint
4105
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4083
4106
  entry: P256ProjectivePoint
4107
+ /** @internal Read and written by this module's point functions; the class is not exported. */
4084
4108
  b: u32[]
4085
4109
 
4086
4110
  constructor() {
@@ -5247,6 +5271,9 @@ const p256SignScalar = (priv: u8[], z: u32[]): u8[] => {
5247
5271
  * The public key of `priv`: `priv * G` in the 65-byte uncompressed SEC 1
5248
5272
  * encoding. Answers `null` unless `priv` is `P256_SCALAR_SIZE` bytes holding a
5249
5273
  * scalar in [1, n) — 0 and n and everything above are not private keys.
5274
+ *
5275
+ * @param priv The private key, 32 big-endian bytes.
5276
+ * @returns The 65-byte public key, or `null` for a key that is not one.
5250
5277
  */
5251
5278
  export const p256PublicKey = (priv: Secret<u8[]>): u8[] | null => expose(priv, p256PublicKeyExposed)
5252
5279
 
@@ -5276,6 +5303,11 @@ const p256PublicKeyExposed = (priv: u8[]): u8[] | null => {
5276
5303
  * uncompressed point on the curve, and for a product at infinity, which a
5277
5304
  * valid key and point cannot give. An x of zero is a point like any other
5278
5305
  * and is answered.
5306
+ *
5307
+ * @param priv This side's private key, 32 big-endian bytes.
5308
+ * @param peerPoint The peer's 65-byte uncompressed public key.
5309
+ * @returns The 32-byte shared secret, or `null` for a refused key or point and
5310
+ * a product at infinity.
5279
5311
  */
5280
5312
  export const p256Ecdh = (priv: Secret<u8[]>, peerPoint: u8[]): Secret<u8[]> | null => {
5281
5313
  const shared: u8[] | null = exposeWith(priv, peerPoint, p256EcdhExposed)
@@ -5337,6 +5369,10 @@ const p256EcdhExposed = (priv: u8[], peerPoint: u8[]): u8[] | null => {
5337
5369
  * bits. The same key and digest always give the same signature, and `s` is
5338
5370
  * not normalised to the lower half. Answers `null` for a private key
5339
5371
  * `p256PublicKey` would refuse.
5372
+ *
5373
+ * @param priv The private key, 32 big-endian bytes.
5374
+ * @param digest The message's hash; only its leftmost 256 bits count.
5375
+ * @returns The 64-byte signature `r || s`, or `null` for a refused key.
5340
5376
  */
5341
5377
  export const p256Sign = (priv: Secret<u8[]>, digest: u8[]): u8[] | null =>
5342
5378
  exposeWith(priv, digest, p256SignExposed)
@@ -5364,6 +5400,11 @@ const p256SignExposed = (priv: u8[], digest: u8[]): u8[] | null => {
5364
5400
  * verifies too, as FIPS 186-4 and SEC 1 have it. Nothing here asks for the
5365
5401
  * lower `s`, so a protocol that needs one signature per message and key (a
5366
5402
  * signature used as an identifier, say) has to refuse the upper half itself.
5403
+ *
5404
+ * @param pub The signer's 65-byte uncompressed public key.
5405
+ * @param digest The message's hash; only its leftmost 256 bits count.
5406
+ * @param sig The 64-byte signature `r || s`.
5407
+ * @returns `true` when the signature is valid, `false` for anything else.
5367
5408
  */
5368
5409
  export const p256Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
5369
5410
  if (toI32(sig.length) !== 64) {
@@ -5420,10 +5461,23 @@ export const p256Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
5420
5461
  return timingSafeEqual(vBytes, rBytes)
5421
5462
  }
5422
5463
 
5423
- /** `p256Sign` of the SHA-256 of `msg`: ECDSA with SHA-256 over the whole message. */
5464
+ /**
5465
+ * `p256Sign` of the SHA-256 of `msg`: ECDSA with SHA-256 over the whole message.
5466
+ *
5467
+ * @param priv The private key, 32 big-endian bytes.
5468
+ * @param msg The whole message, hashed here.
5469
+ * @returns The 64-byte signature `r || s`, or `null` for a refused key.
5470
+ */
5424
5471
  export const p256SignSha256 = (priv: Secret<u8[]>, msg: u8[]): u8[] | null => p256Sign(priv, sha256(msg))
5425
5472
 
5426
- /** `p256Verify` of the SHA-256 of `msg`. */
5473
+ /**
5474
+ * `p256Verify` of the SHA-256 of `msg`.
5475
+ *
5476
+ * @param pub The signer's 65-byte uncompressed public key.
5477
+ * @param msg The whole message, hashed here.
5478
+ * @param sig The 64-byte signature `r || s`.
5479
+ * @returns `true` when the signature is valid, `false` for anything else.
5480
+ */
5427
5481
  export const p256VerifySha256 = (pub: u8[], msg: u8[], sig: u8[]): boolean =>
5428
5482
  p256Verify(pub, sha256(msg), sig)
5429
5483
 
@@ -5653,34 +5707,121 @@ const p256WindowFits = (length: i32, at: i32, len: i32): boolean => at >= 0 && l
5653
5707
  * const ok: boolean = p256SignSha256Into(scratch, priv, msg, 0, msg.length, sig, 0);
5654
5708
  */
5655
5709
  export class P256SignScratch {
5656
- /** FIPS 180-4 §4.2.2's round constants. */
5710
+ /**
5711
+ * FIPS 180-4 §4.2.2's round constants.
5712
+ *
5713
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5714
+ */
5657
5715
  kt: u32[]
5658
- /** A SHA-256 hash value, schedule and padded tail. */
5716
+ /**
5717
+ * A SHA-256 hash value.
5718
+ *
5719
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5720
+ */
5659
5721
  hash: u32[]
5722
+ /**
5723
+ * A SHA-256 message schedule.
5724
+ *
5725
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5726
+ */
5660
5727
  schedule: u32[]
5728
+ /**
5729
+ * A SHA-256 message's padded tail.
5730
+ *
5731
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5732
+ */
5661
5733
  tail: u8[]
5662
- /** An HMAC key block and the message after it. */
5734
+ /**
5735
+ * An HMAC key block and the message after it.
5736
+ *
5737
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5738
+ */
5663
5739
  block: u8[]
5664
- /** `V || sep`, where every key update's message starts (RFC 6979 §3.2 steps d, f and h.3). */
5740
+ /**
5741
+ * `V || sep`, where every key update's message starts (RFC 6979 §3.2 steps d, f and h.3).
5742
+ *
5743
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5744
+ */
5665
5745
  message: u8[]
5666
- /** RFC 6979's `K` and `V`; the last `V` is the nonce. */
5746
+ /**
5747
+ * RFC 6979's `K`.
5748
+ *
5749
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5750
+ */
5667
5751
  k: u8[]
5752
+ /**
5753
+ * RFC 6979's `V`; the last `V` is the nonce.
5754
+ *
5755
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5756
+ */
5668
5757
  v: u8[]
5669
- /** The message's digest, then int2octets of it mod n: RFC 6979's `h1`. */
5758
+ /**
5759
+ * The message's digest, then int2octets of it mod n: RFC 6979's `h1`.
5760
+ *
5761
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5762
+ */
5670
5763
  h1: u8[]
5671
- /** The digest mod n, `r`, the nonce point's affine x, and `k^-1` in the Montgomery domain. */
5764
+ /**
5765
+ * The digest mod n.
5766
+ *
5767
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5768
+ */
5672
5769
  z: u32[]
5770
+ /**
5771
+ * The signature's `r`.
5772
+ *
5773
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5774
+ */
5673
5775
  r: u32[]
5776
+ /**
5777
+ * The nonce point's affine x.
5778
+ *
5779
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5780
+ */
5674
5781
  x: u32[]
5782
+ /**
5783
+ * The nonce `k`, then `k^-1`, in the Montgomery domain.
5784
+ *
5785
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5786
+ */
5675
5787
  kM: u32[]
5676
- /** The order n and R^2 mod n. */
5788
+ /**
5789
+ * The order n.
5790
+ *
5791
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5792
+ */
5677
5793
  n: u32[]
5794
+ /**
5795
+ * R^2 mod n.
5796
+ *
5797
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5798
+ */
5678
5799
  r2: u32[]
5679
- /** The nonce's multiple of G, the comb table of G and point scratch. */
5800
+ /**
5801
+ * The nonce's multiple of G.
5802
+ *
5803
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5804
+ */
5680
5805
  point: P256ProjectivePoint
5806
+ /**
5807
+ * The comb table of G.
5808
+ *
5809
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5810
+ */
5681
5811
  table: u32[]
5812
+ /**
5813
+ * The point arithmetic's scratch.
5814
+ *
5815
+ * @internal Read and written by `p256SignSha256Into` and its steps alone.
5816
+ */
5682
5817
  points: P256PointScratch
5683
5818
 
5819
+ /**
5820
+ * Makes the arrays every signature reuses, the round constants and the
5821
+ * base point's comb table included, so the base-point multiplication works
5822
+ * in them; what `p256SignSha256Into` still allocates per call is released
5823
+ * when its arena block ends, so nothing is kept.
5824
+ */
5684
5825
  constructor() {
5685
5826
  this.kt = [
5686
5827
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
@@ -5803,6 +5944,15 @@ const p256WipeSignScratch = (sc: P256SignScratch): void => {
5803
5944
  * leaves the arena where it was; every secret the scratch held — RFC 6979's
5804
5945
  * `K` and `V`, the nonce's inverse, its point and the point scratch — is wiped
5805
5946
  * before it returns, as are the fresh arrays the key's own steps worked in.
5947
+ *
5948
+ * @param sc The scratch to sign in, one per signer.
5949
+ * @param priv The private key, 32 big-endian bytes.
5950
+ * @param msg The array holding the message.
5951
+ * @param off Where the message starts in `msg`.
5952
+ * @param len The message's length in bytes.
5953
+ * @param out The array the signature is written into.
5954
+ * @param at Where the 64 bytes start in `out`.
5955
+ * @returns `true` once the signature is written, `false` for a refused key.
5806
5956
  */
5807
5957
  export const p256SignSha256Into = (
5808
5958
  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
@@ -21,27 +24,30 @@
21
24
  *
22
25
  * Ported from fiat-crypto (https://github.com/mit-plv/fiat-crypto), fiat-c/src/p384_32.c and fiat-c/src/p384_scalar_32.c. Copyright (c) 2015-2020 the fiat-crypto authors (see the AUTHORS file). Used under the MIT licence; see std/crypto/LICENSE-fiat-crypto-p384.
23
26
  *
24
- * **What is ported and what is not.** The `p384Fiat*` functions are
25
- * fiat-crypto's generated, formally verified Montgomery arithmetic on twelve
26
- * `u32` limbs with `u64` products — `mul`, `add`, `sub`, `selectznz`,
27
- * `nonzero` and `set_one` mod p, and `mul` and `set_one` mod n — statement for
28
- * statement, with fiat's variable names, translated the way
29
- * `nish/crypto/p256` translates P-256's: C's out-pointers become a `u64`
30
- * answer, each of fiat's `addcarryx`, `subborrowx` and `mulx` helpers here
31
- * answering its two outputs packed, the first in the low 32 bits and the
32
- * second above them, and the caller declaring only the halves fiat goes on to
33
- * read; and each function opens with a length guard, so that the literal
34
- * indices after it compile with no bounds check. fiat's 64-bit `p384_64.c`
35
- * needs a 128-bit product, which Nish has not got, so the 32-bit files are the
36
- * ones ported. fiat's `square` is not: a square is a `mul` of an element by
37
- * itself, which costs verification a little time and saves this file 2,500
38
- * lines. The scalar functions use the field's four helpers, which fiat
39
- * generates identically for both. Everything else in this file — the curve,
40
- * the encodings, the inversions and ECDSA — is written for this module from
41
- * the documents it names.
27
+ * **What is ported and what is not.**
42
28
  *
43
- * **The curve** is `y^2 = x^3 - 3x + b` over GF(p), p = 2^384 - 2^128 - 2^96
44
- * + 2^32 - 1, with the base point G and its order n from SEC 2 §2.5.1 (FIPS
29
+ * The `p384Fiat*` functions are fiat-crypto's generated, formally verified
30
+ * Montgomery arithmetic on twelve `u32` limbs with `u64` products — `mul`,
31
+ * `add`, `sub`, `selectznz`, `nonzero` and `set_one` mod p, and `mul` and
32
+ * `set_one` mod n — statement for statement, with fiat's variable names,
33
+ * translated the way `nish/crypto/p256` translates P-256's:
34
+ * - C's out-pointers become a `u64` answer, each of fiat's `addcarryx`,
35
+ * `subborrowx` and `mulx` helpers here answering its two outputs packed, the
36
+ * first in the low 32 bits and the second above them, and the caller
37
+ * declaring only the halves fiat goes on to read;
38
+ * - and each function opens with a length guard, so that the literal indices
39
+ * after it compile with no bounds check.
40
+ *
41
+ * fiat's 64-bit `p384_64.c` needs a 128-bit product, which Nish has not got, so
42
+ * the 32-bit files are the ones ported. fiat's `square` is not: a square is a
43
+ * `mul` of an element by itself, which costs verification a little time and
44
+ * saves this file 2,500 lines. The scalar functions use the field's four
45
+ * helpers, which fiat generates identically for both. Everything else in this
46
+ * file — the curve, the encodings, the inversions and ECDSA — is written for
47
+ * this module from the documents it names.
48
+ *
49
+ * **The curve** is `y^2 = x^3 - 3x + b` over GF(p),
50
+ * p = 2^384 - 2^128 - 2^96 + 2^32 - 1, with the base point G and its order n from SEC 2 §2.5.1 (FIPS
45
51
  * 186-4 §D.1.2.4). Points are projective (X : Y : Z), Z = 0 the identity, and
46
52
  * are added and doubled with the complete formulas of J. Renes, C. Costello and
47
53
  * L. Batina, "Complete addition formulas for prime order elliptic curves"
@@ -5708,8 +5714,11 @@ const p384ScalarInvert = (out: u32[], a: u32[]): void => {
5708
5714
  * Z = 0 is the identity, which the complete formulas take like any other point.
5709
5715
  */
5710
5716
  class P384ProjectivePoint {
5717
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5711
5718
  x: u32[]
5719
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5712
5720
  y: u32[]
5721
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5713
5722
  z: u32[]
5714
5723
 
5715
5724
  /** The identity, (0 : 1 : 0). */
@@ -5727,12 +5736,19 @@ class P384ProjectivePoint {
5727
5736
  * formulas multiply by.
5728
5737
  */
5729
5738
  class P384PointScratch {
5739
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5730
5740
  t0: u32[]
5741
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5731
5742
  t1: u32[]
5743
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5732
5744
  t2: u32[]
5745
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5733
5746
  t3: u32[]
5747
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5734
5748
  t4: u32[]
5749
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5735
5750
  sum: P384ProjectivePoint
5751
+ /** @internal Read and written by this module's point functions; the class is not exported. */
5736
5752
  b: u32[]
5737
5753
 
5738
5754
  constructor() {
@@ -5995,6 +6011,11 @@ const p384DigestScalar = (out: u32[], digest: u8[]): void => {
5995
6011
  * verifies too, as FIPS 186-4 and SEC 1 have it. Nothing here asks for the
5996
6012
  * lower `s`, so a protocol that needs one signature per message and key has to
5997
6013
  * refuse the upper half itself.
6014
+ *
6015
+ * @param pub The signer's 97-byte uncompressed public key.
6016
+ * @param digest The message's hash; only its leftmost 384 bits count.
6017
+ * @param sig The 96-byte signature `r || s`.
6018
+ * @returns `true` when the signature is valid, `false` for anything else.
5998
6019
  */
5999
6020
  export const p384Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
6000
6021
  if (toI32(sig.length) !== 96) {
@@ -6039,6 +6060,13 @@ export const p384Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
6039
6060
  return timingSafeEqual(v, rBytes)
6040
6061
  }
6041
6062
 
6042
- /** `p384Verify` of the SHA-384 of `msg`: ECDSA with SHA-384 over the whole message. */
6063
+ /**
6064
+ * `p384Verify` of the SHA-384 of `msg`: ECDSA with SHA-384 over the whole message.
6065
+ *
6066
+ * @param pub The signer's 97-byte uncompressed public key.
6067
+ * @param msg The whole message, hashed here.
6068
+ * @param sig The 96-byte signature `r || s`.
6069
+ * @returns `true` when the signature is valid, `false` for anything else.
6070
+ */
6043
6071
  export const p384VerifySha384 = (pub: u8[], msg: u8[], sig: u8[]): boolean =>
6044
6072
  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())