@amritk/nish-aarch64-linux 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 (128) hide show
  1. package/INSTALL.md +73 -59
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +50 -4
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/nish.d.ts +4849 -430
  8. package/runtime/nish.h +25 -0
  9. package/runtime/page.d.ts +53 -0
  10. package/runtime/page.mjs +146 -0
  11. package/runtime/runtime-async.c +557 -0
  12. package/runtime/runtime-wasm.c +63 -7
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +252 -15
  16. package/std/README.md +107 -82
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +279 -59
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +128 -52
  28. package/std/crypto/base64url.ts +29 -5
  29. package/std/crypto/bcrypt-batch.ts +101 -34
  30. package/std/crypto/bcrypt.ts +93 -27
  31. package/std/crypto/chacha20poly1305.ts +69 -14
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +66 -16
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +242 -37
  36. package/std/crypto/jwt.ts +204 -72
  37. package/std/crypto/p256.ts +222 -72
  38. package/std/crypto/p384.ts +51 -23
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +99 -30
  41. package/std/crypto/rsa.ts +56 -22
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +163 -24
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +133 -66
  48. package/std/host/ring.ts +124 -0
  49. package/std/json-equal.ts +31 -11
  50. package/std/json-format.ts +112 -13
  51. package/std/json-number.ts +76 -26
  52. package/std/json-pointer.ts +23 -1
  53. package/std/json-tokenizer.ts +126 -51
  54. package/std/json-typed.ts +757 -96
  55. package/std/json-value.ts +438 -157
  56. package/std/json.ts +170 -38
  57. package/std/map.ts +25 -7
  58. package/std/math.ts +36 -4
  59. package/std/net/buffer-pool.ts +42 -11
  60. package/std/net/dns-config.ts +109 -23
  61. package/std/net/dns-message.ts +62 -7
  62. package/std/net/dns-resolver.ts +247 -53
  63. package/std/net/dns.ts +203 -35
  64. package/std/net/fetch.ts +1267 -290
  65. package/std/net/handler.ts +296 -80
  66. package/std/net/hpack.ts +290 -77
  67. package/std/net/http-compress.ts +152 -36
  68. package/std/net/http-fields.ts +85 -9
  69. package/std/net/http1-client-codec.ts +129 -44
  70. package/std/net/http1-codec.ts +279 -114
  71. package/std/net/http1-server.ts +409 -139
  72. package/std/net/http2-frame.ts +289 -51
  73. package/std/net/http2-tls.ts +152 -49
  74. package/std/net/http2.ts +314 -160
  75. package/std/net/http3-frame.ts +165 -27
  76. package/std/net/http3-server.ts +282 -131
  77. package/std/net/http3.ts +377 -173
  78. package/std/net/outbound.ts +37 -3
  79. package/std/net/postgres-carrier.ts +144 -8
  80. package/std/net/postgres.ts +898 -260
  81. package/std/net/prefork.ts +202 -65
  82. package/std/net/qpack.ts +120 -32
  83. package/std/net/quic-conn-ack.ts +60 -12
  84. package/std/net/quic-conn-cid.ts +135 -27
  85. package/std/net/quic-conn-params.ts +109 -22
  86. package/std/net/quic-datagram.ts +50 -3
  87. package/std/net/quic-frame.ts +333 -23
  88. package/std/net/quic-listener.ts +161 -75
  89. package/std/net/quic-packet.ts +373 -73
  90. package/std/net/quic-recovery.ts +287 -65
  91. package/std/net/quic-stream.ts +436 -146
  92. package/std/net/quic.ts +693 -323
  93. package/std/net/router.ts +87 -40
  94. package/std/net/tls/client.ts +167 -51
  95. package/std/net/tls/codec.ts +494 -68
  96. package/std/net/tls/record-client.ts +140 -73
  97. package/std/net/tls/record-server.ts +233 -105
  98. package/std/net/tls/record.ts +142 -36
  99. package/std/net/tls/schedule.ts +235 -26
  100. package/std/net/tls/ticket.ts +161 -40
  101. package/std/net/tls/verify.ts +57 -10
  102. package/std/net/tls-tcp.ts +308 -95
  103. package/std/net/tls.ts +276 -102
  104. package/std/net/upstream.ts +81 -45
  105. package/std/net/url.ts +109 -22
  106. package/std/net/websocket.ts +140 -26
  107. package/std/net/webtransport.ts +243 -106
  108. package/std/pair.ts +10 -2
  109. package/std/random.ts +42 -15
  110. package/std/regex.ts +422 -257
  111. package/std/region.ts +70 -15
  112. package/std/secret.ts +52 -11
  113. package/std/sort.ts +17 -2
  114. package/std/sqlite.ts +256 -35
  115. package/std/sync/ring.ts +229 -56
  116. package/std/sync/shm.ts +203 -23
  117. package/std/term-io.ts +154 -78
  118. package/std/term-pixels.ts +144 -19
  119. package/std/term-probe.ts +77 -35
  120. package/std/term-rate.ts +199 -118
  121. package/std/term.ts +400 -175
  122. package/std/testing.ts +130 -32
  123. package/std/text.ts +84 -17
  124. package/std/threads.ts +248 -56
  125. package/std/yaml-json.ts +111 -97
  126. package/std/yaml-lines.ts +47 -15
  127. package/std/yaml-write.ts +45 -30
  128. package/std/yaml.ts +764 -384
@@ -18,13 +18,15 @@
18
18
  * passwords (lane 0 always, a null lane skipped) and run their Blowfish
19
19
  * encryptions interleaved in one loop, so one core does the work of several
20
20
  * hashes in well under the time of as many; each result is the single path's,
21
- * byte for byte. `bcryptVerifyN` groups lanes by the cost their hash names,
22
- * and a malformed hash in any lane is an `Err` for the whole call, naming the
23
- * lane, before anything is hashed. `bcryptHash4` and `bcryptVerify4` are the
24
- * same for up to four. Which width is fastest depends on the core (how many
25
- * loads it issues a cycle, how much L1 the states fill), so `bcryptCalibrate`
26
- * measures it: it times widths one to six at cost 4 on throwaway passwords
27
- * and answers the one with the least time per hash. Call it once, at startup.
21
+ * byte for byte. `bcryptVerifyN` groups lanes by the cost their hash names, and
22
+ * a malformed hash in any lane is an `Err` for the whole call, naming the lane,
23
+ * before anything is hashed. `bcryptHash4` and `bcryptVerify4` are the same for
24
+ * up to four.
25
+ *
26
+ * Which width is fastest depends on the core (how many loads it issues a cycle,
27
+ * how much L1 the states fill), so `bcryptCalibrate` measures it: it times
28
+ * widths one to six at cost 4 on throwaway passwords and answers the one with
29
+ * the least time per hash. Call it once, at startup.
28
30
  *
29
31
  * **The password is a `Secret<u8[]>`** (`nish:secret`). The key schedule and
30
32
  * the 2^cost rounds run inside `exposeWith`; the Blowfish state (the P-array
@@ -40,14 +42,17 @@
40
42
  * repeated until 72 bytes are taken: a password longer than 72 bytes hashes as
41
43
  * its first 72, and bytes after an embedded NUL are not read, as under
42
44
  * `crypt_r`; docs/security/crypto-bcrypt.md says why a longer password is cut
43
- * rather than refused. Bytes above 127 are unsigned, as `$2b$` and
44
- * `$2y$` define them. A `$2a$` hash gets crypt_blowfish's countermeasure for
45
- * the old sign-extension bug: when an 8-bit byte was sign-extended into a key
46
- * word and yet the buggy and the correct key came out equal, one bit of the
47
- * first P word is flipped, as crypt_blowfish flips it, so the `$2a$` digest of
48
- * such a password (`\xff\xff\xa3`) is not its `$2y$` one. It takes an 8-bit
49
- * byte after the first byte of a key word with only 0xff bytes before it, every
50
- * such byte in all 18 words being so placed, which UTF-8 never holds. `$2x$` (the bug itself) is refused.
45
+ * rather than refused. Bytes above 127 are unsigned, as `$2b$` and `$2y$`
46
+ * define them.
47
+ *
48
+ * A `$2a$` hash gets crypt_blowfish's countermeasure for the old sign-extension
49
+ * bug: when an 8-bit byte was sign-extended into a key word and yet the buggy
50
+ * and the correct key came out equal, one bit of the first P word is flipped,
51
+ * as crypt_blowfish flips it, so the `$2a$` digest of such a password
52
+ * (`\xff\xff\xa3`) is not its `$2y$` one. It takes an 8-bit byte after the
53
+ * first byte of a key word with only 0xff bytes before it, every such byte in
54
+ * all 18 words being so placed, which UTF-8 never holds. `$2x$` (the bug
55
+ * itself) is refused.
51
56
  *
52
57
  * **Errors are `Err`s.** A salt that is not 16 bytes, a cost outside 4–31, or
53
58
  * a hash string that is not one (the wrong length, an unknown prefix, a cost
@@ -70,18 +75,19 @@
70
75
  * milliseconds of one core; a server runs it off its event loop, on a task
71
76
  * (docs/guide/using-nish/password-hashing.md).
72
77
  *
73
- * The algorithm is Provos and Mazières's ("A Future-Adaptable Password
74
- * Scheme", USENIX 1999). The code is adapted from Solar Designer's
75
- * crypt_blowfish 1.3, which carries the notice below, and the 1042 initial
76
- * words, the hexadecimal digits of π, are its `BF_init_state`. Closest
77
- * upstream for each part: `bcryptInitState` is `BF_init_state`;
78
- * `bcryptEncrypt` and the rounds in `bcryptBody` are `BF_ENCRYPT`/`BF_ROUND`;
79
- * `bcryptBody` is `BF_body`; `bcryptSetKey` is `BF_set_key`; `bcryptDecodeSalt`
80
- * and `bcryptEncode` are `BF_decode` and `BF_encode`; `bcryptExposed` is the
81
- * part of `BF_crypt` after the setting is parsed, and `bcryptParse` that
82
- * parse. The state is one flat `u32[]` with P first (words 0–17) and the
83
- * S-boxes after (18–1041), so the body walks it in one pass, and every S-box
84
- * index is an `i32` masked to 0–255, which the checker proves in bounds
78
+ * The algorithm is Provos and Mazières's ("A Future-Adaptable Password Scheme",
79
+ * USENIX 1999). The code is adapted from Solar Designer's crypt_blowfish 1.3,
80
+ * which carries the notice below, and the 1042 initial words, the hexadecimal
81
+ * digits of π, are its `BF_init_state`. Closest upstream for each part:
82
+ * `bcryptInitState` is `BF_init_state`; `bcryptEncrypt` and the rounds in
83
+ * `bcryptBody` are `BF_ENCRYPT`/`BF_ROUND`; `bcryptBody` is `BF_body`;
84
+ * `bcryptSetKey` is `BF_set_key`; `bcryptDecodeSalt` and `bcryptEncode` are
85
+ * `BF_decode` and `BF_encode`; `bcryptExposed` is the part of `BF_crypt` after
86
+ * the setting is parsed, and `bcryptParse` that parse.
87
+ *
88
+ * The state is one flat `u32[]` with P first (words 0–17) and the S-boxes after
89
+ * (18–1041), so the body walks it in one pass, and every S-box index is an
90
+ * `i32` masked to 0–255, which the checker proves in bounds
85
91
  * (`BCRYPT_STATE_WORDS`). The magic-string encryption, the parsing and the
86
92
  * alphabet are separate functions so a several-lane kernel can share them.
87
93
  *
@@ -829,6 +835,13 @@ const bcryptSameWiped = (rebuilt: u8[], hash: string): boolean => {
829
835
  * 60 characters, `$2b$`, the two-digit cost, `$`, the salt and the digest in
830
836
  * bcrypt's base64. The salt must come from a CSPRNG and be fresh for each
831
837
  * hash. An `Err` for a salt of another length or a cost outside 4–31.
838
+ *
839
+ * @param password The password, read only inside `exposeWith`; the caller still
840
+ * owns and wipes it.
841
+ * @param salt 16 fresh random bytes.
842
+ * @param cost The log2 of the round count, 4 to 31.
843
+ * @returns The 60-character hash, which is public, or an `Err` naming the bad
844
+ * salt or cost.
832
845
  */
833
846
  export const bcryptHash = (password: Secret<u8[]>, salt: u8[], cost: i32): Result<string, string> => {
834
847
  if (salt.length !== 16) {
@@ -848,6 +861,13 @@ export const bcryptHash = (password: Secret<u8[]>, salt: u8[], cost: i32): Resul
848
861
  * constant-time compare of all 60 characters with the hash rebuilt from the
849
862
  * password and `hash`'s own prefix, salt and cost. `$2a$`, `$2b$` and `$2y$`
850
863
  * are read; an `Err` names what makes `hash` not a bcrypt hash.
864
+ *
865
+ * @param password The password, read only inside `exposeWith`; the caller still
866
+ * owns and wipes it.
867
+ * @param hash A stored hash; its cost sets the work, so it must be one you
868
+ * stored.
869
+ * @returns `Ok(true)` only for this password's hash, `Ok(false)` for another's,
870
+ * or an `Err` for a malformed hash.
851
871
  */
852
872
  export const bcryptVerify = (password: Secret<u8[]>, hash: string): Result<boolean, string> => {
853
873
  const parsed: Result<BcryptParsed, string> = bcryptParseHash(hash)
@@ -1805,6 +1825,18 @@ const bcryptSlotsOf = (mask: i32): i32[] => {
1805
1825
  * `bcryptHash`. Which w is fastest per hash depends on the core
1806
1826
  * (`bcryptCalibrate` measures it). An `Err` for `salts` of another length
1807
1827
  * than 96 or a cost outside 4–31.
1828
+ *
1829
+ * @param p0 Lane 0's password, always hashed; read only inside `exposeWith`,
1830
+ * and the caller still owns and wipes it.
1831
+ * @param p1 Lane 1's password, or `null` to skip the lane.
1832
+ * @param p2 Lane 2's password, or `null` to skip the lane.
1833
+ * @param p3 Lane 3's password, or `null` to skip the lane.
1834
+ * @param p4 Lane 4's password, or `null` to skip the lane.
1835
+ * @param p5 Lane 5's password, or `null` to skip the lane.
1836
+ * @param salts 96 fresh random bytes, lane k's salt at 16k.
1837
+ * @param cost The log2 of the round count, 4 to 31.
1838
+ * @returns Six hash strings, "" for a lane not given, or an `Err` for bad salts
1839
+ * or cost.
1808
1840
  */
1809
1841
  export const bcryptHashN = (
1810
1842
  p0: Secret<u8[]>,
@@ -1860,6 +1892,16 @@ export const bcryptHashN = (
1860
1892
  * `bcryptHashN` for up to four passwords, `salts` 64 bytes: the same hashes,
1861
1893
  * four strings. An `Err` for `salts` of another length than 64 or a cost
1862
1894
  * outside 4–31.
1895
+ *
1896
+ * @param p0 Lane 0's password, always hashed; read only inside `exposeWith`,
1897
+ * and the caller still owns and wipes it.
1898
+ * @param p1 Lane 1's password, or `null` to skip the lane.
1899
+ * @param p2 Lane 2's password, or `null` to skip the lane.
1900
+ * @param p3 Lane 3's password, or `null` to skip the lane.
1901
+ * @param salts 64 fresh random bytes, lane k's salt at 16k.
1902
+ * @param cost The log2 of the round count, 4 to 31.
1903
+ * @returns Four hash strings, "" for a lane not given, or an `Err` for bad
1904
+ * salts or cost.
1863
1905
  */
1864
1906
  export const bcryptHash4 = (
1865
1907
  p0: Secret<u8[]>,
@@ -1928,6 +1970,18 @@ const bcryptJoin = (out: u8[], at: i32, prefix: u8[], digests: u8[], k: i32): vo
1928
1970
  * runs as `bcryptVerify`. A malformed hash in any given lane makes the whole
1929
1971
  * call an `Err` naming the lane ("bcrypt: lane 2: …") before any hashing, so
1930
1972
  * one bad row in a batch cannot be read as a wrong password.
1973
+ *
1974
+ * @param p0 Lane 0's password, always hashed; read only inside `exposeWith`,
1975
+ * and the caller still owns and wipes it.
1976
+ * @param p1 Lane 1's password, or `null` to skip the lane.
1977
+ * @param p2 Lane 2's password, or `null` to skip the lane.
1978
+ * @param p3 Lane 3's password, or `null` to skip the lane.
1979
+ * @param p4 Lane 4's password, or `null` to skip the lane.
1980
+ * @param p5 Lane 5's password, or `null` to skip the lane.
1981
+ * @param hashes Six strings, lane k's stored hash at k; a skipped lane's is not
1982
+ * read.
1983
+ * @returns Six answers, `false` for a skipped lane, or an `Err` naming a
1984
+ * malformed lane or a count that is not six.
1931
1985
  */
1932
1986
  export const bcryptVerifyN = (
1933
1987
  p0: Secret<u8[]>,
@@ -1990,6 +2044,16 @@ export const bcryptVerifyN = (
1990
2044
  /**
1991
2045
  * `bcryptVerifyN` for up to four passwords and four hashes: the same
1992
2046
  * answers, four of them, and the same lane errors.
2047
+ *
2048
+ * @param p0 Lane 0's password, always hashed; read only inside `exposeWith`,
2049
+ * and the caller still owns and wipes it.
2050
+ * @param p1 Lane 1's password, or `null` to skip the lane.
2051
+ * @param p2 Lane 2's password, or `null` to skip the lane.
2052
+ * @param p3 Lane 3's password, or `null` to skip the lane.
2053
+ * @param hashes Four strings, lane k's stored hash at k; a skipped lane's is
2054
+ * not read.
2055
+ * @returns Four answers, `false` for a skipped lane, or an `Err` naming a
2056
+ * malformed lane or a count that is not four.
1993
2057
  */
1994
2058
  export const bcryptVerify4 = (
1995
2059
  p0: Secret<u8[]>,
@@ -2167,6 +2231,8 @@ const bcryptCalibrateTime = (width: i32): i64 => {
2167
2231
  * width turns on the core's load ports, its registers and what else is
2168
2232
  * running, and it keeps nothing between calls, so call it once, at startup,
2169
2233
  * and keep the answer. A few tens of milliseconds of one core.
2234
+ *
2235
+ * @returns How many passwords, 1 to 6, to give each batch call.
2170
2236
  */
2171
2237
  export const bcryptCalibrate = (): i32 => {
2172
2238
  let best: i32 = 1
@@ -37,25 +37,27 @@
37
37
  * makes the arithmetic fit: a limb product is below 2^56, five of them sum
38
38
  * below 2^58, and 2^130 = 5 (mod p) folds the top of a product back into the
39
39
  * bottom as a multiply by five.
40
- * `poly1305Block` says why each sum fits. The reduction is by carries and
41
- * masks, and the final reduction into `[0, p)` picks between `h` and `h - p`
42
- * with `ctSelect` on a mask made from a borrow, never with a comparison.
40
+ *
41
+ * `poly1305Block` says why each sum fits. The reduction is by carries and
42
+ * masks, and the final reduction into `[0, p)` picks between `h` and `h - p`
43
+ * with `ctSelect` on a mask made from a borrow, never with a comparison.
43
44
  *
44
45
  * **Constant time.** Nothing branches on, or indexes by, a key, a keystream
45
46
  * word, the accumulator or a tag. Every `if` and every loop bound is on a
46
47
  * length, an offset or a block number, all of which the peer already knows.
47
48
  * `chacha20Poly1305Open` computes the tag over the received ciphertext and
48
- * compares all sixteen bytes by ORing their differences into one word before
49
- * it decrypts anything, and a mismatch is decided on that one word. Verified by
50
- * disassembly (`tests/cases/ct_asm_chacha20poly1305`, read by
51
- * `tests/ct-asm.js` for x86-64 and aarch64): the key clamp, one Poly1305 block
52
- * (the load, the multiply and the partial reduction), the final reduction and
53
- * the addition of `s`, and the tag compare, each straight-line with no
54
- * secret-addressed load. The fixture holds copies of those four functions,
55
- * because the check reads one compiled module; `tests/link/crypto_chacha20poly1305`
56
- * holds the copies to these originals. The ChaCha20 rounds and the loops that
57
- * drive both halves are discipline, not proof: the check refuses a loop's own
58
- * back-edge, so it cannot read them.
49
+ * compares all sixteen bytes by ORing their differences into one word before it
50
+ * decrypts anything, and a mismatch is decided on that one word.
51
+ *
52
+ * Verified by disassembly (`tests/cases/ct_asm_chacha20poly1305`, read by
53
+ * `tests/ct-asm.js` for x86-64 and aarch64): the key clamp, one Poly1305 block
54
+ * (the load, the multiply and the partial reduction), the final reduction and
55
+ * the addition of `s`, and the tag compare, each straight-line with no
56
+ * secret-addressed load. The fixture holds copies of those four functions,
57
+ * because the check reads one compiled module;
58
+ * `tests/link/crypto_chacha20poly1305` holds the copies to these originals. The
59
+ * ChaCha20 rounds and the loops that drive both halves are discipline, not
60
+ * proof: the check refuses a loop's own back-edge, so it cannot read them.
59
61
  *
60
62
  * Private names carry the `chacha20` / `poly1305` prefix because a `std/`
61
63
  * module's private functions share the importing program's flat symbol
@@ -310,6 +312,14 @@ const chacha20CounterFits = (counter: u32, len: i32): boolean => {
310
312
  * word positions `a`, `b`, `c` and `d`, which is what §2.1.1 and §2.2.1 test.
311
313
  * `chacha20Core` does the same on locals. Answers `null`
312
314
  * when a position is not inside `state`.
315
+ *
316
+ * @param state The words to start from; it is read, not changed.
317
+ * @param a The first word position.
318
+ * @param b The second word position.
319
+ * @param c The third word position.
320
+ * @param d The fourth word position.
321
+ * @returns A fresh copy of `state` with the quarter round applied, or `null`
322
+ * for a position outside it.
313
323
  */
314
324
  export const chacha20QuarterRound = (state: u32[], a: i32, b: i32, c: i32, d: i32): u32[] | null => {
315
325
  const n: i32 = toI32(state.length)
@@ -335,6 +345,13 @@ export const chacha20QuarterRound = (state: u32[], a: i32, b: i32, c: i32, d: i3
335
345
  * The ChaCha20 block function (RFC 8439 §2.3): the 64 bytes of keystream for
336
346
  * block `counter` under `key` and `nonce`. Answers `null` unless `key` is
337
347
  * `CHACHA20_KEY_SIZE` bytes and `nonce` is `CHACHA20POLY1305_NONCE_SIZE`.
348
+ *
349
+ * @param key The 32-byte key; it is read, not kept, and the caller still wipes
350
+ * it.
351
+ * @param counter The block number.
352
+ * @param nonce The 12-byte nonce.
353
+ * @returns 64 fresh bytes of keystream, secret like the key, or `null` for a
354
+ * bad key or nonce length.
338
355
  */
339
356
  export const chacha20Block = (key: u8[], counter: u32, nonce: u8[]): u8[] | null => {
340
357
  if (toI32(key.length) !== CHACHA20_KEY_SIZE || toI32(nonce.length) !== CHACHA20POLY1305_NONCE_SIZE) {
@@ -353,6 +370,13 @@ export const chacha20Block = (key: u8[], counter: u32, nonce: u8[]): u8[] | null
353
370
  * `key` is `CHACHA20_KEY_SIZE` bytes and `nonce` is
354
371
  * `CHACHA20POLY1305_NONCE_SIZE`, and `null` when `data` would run the 32-bit
355
372
  * counter past its last value rather than repeat the keystream.
373
+ *
374
+ * @param key The 32-byte key; it is read, not kept, and the caller still wipes
375
+ * it.
376
+ * @param counter The block number the keystream starts at.
377
+ * @param nonce The 12-byte nonce.
378
+ * @param data The bytes to encrypt or decrypt; it is read, not changed.
379
+ * @returns A fresh array of `data.length` bytes, or `null` for bad input.
356
380
  */
357
381
  export const chacha20 = (key: u8[], counter: u32, nonce: u8[], data: u8[]): u8[] | null => {
358
382
  const len: i32 = toI32(data.length)
@@ -571,6 +595,11 @@ const poly1305Absorb = (h: u64[], r: u64[], buf: u8[], at: i32, len: i32, zeroPa
571
595
  * `key`, `r` then `s`. A key must never authenticate two messages; the AEAD
572
596
  * below makes a fresh one per nonce. Answers `null` unless `key` is
573
597
  * `POLY1305_KEY_SIZE` bytes.
598
+ *
599
+ * @param key The 32-byte one-time key, `r` then `s`; it is read, not kept, and
600
+ * the caller still wipes it.
601
+ * @param msg The message to authenticate.
602
+ * @returns A fresh 16-byte tag, or `null` for a bad key length.
574
603
  */
575
604
  export const poly1305 = (key: u8[], msg: u8[]): u8[] | null => {
576
605
  if (toI32(key.length) !== POLY1305_KEY_SIZE) {
@@ -592,6 +621,12 @@ export const poly1305 = (key: u8[], msg: u8[]): u8[] | null => {
592
621
  * ChaCha20 block with counter 0 under the AEAD's key and nonce. Answers
593
622
  * `null` unless `key` is `CHACHA20_KEY_SIZE` bytes and `nonce` is
594
623
  * `CHACHA20POLY1305_NONCE_SIZE`.
624
+ *
625
+ * @param key The 32-byte key; it is read, not kept, and the caller still wipes
626
+ * it.
627
+ * @param nonce The 12-byte nonce.
628
+ * @returns A fresh 32-byte one-time key, which the caller wipes, or `null` for
629
+ * a bad key or nonce length.
595
630
  */
596
631
  export const poly1305KeyGen = (key: u8[], nonce: u8[]): u8[] | null => {
597
632
  if (toI32(key.length) !== CHACHA20_KEY_SIZE || toI32(nonce.length) !== CHACHA20POLY1305_NONCE_SIZE) {
@@ -665,6 +700,14 @@ const chacha20Poly1305TagMatch = (tag: u8[], sealed: u8[], at: i32): u32 => {
665
700
  * never be used twice with one key. Answers `null` unless `key` is
666
701
  * `CHACHA20_KEY_SIZE` bytes and `nonce` is `CHACHA20POLY1305_NONCE_SIZE`, or
667
702
  * when the sealed message would not fit in an array.
703
+ *
704
+ * @param key The 32-byte key; it is read, not kept, and the caller still wipes
705
+ * it.
706
+ * @param nonce The 12-byte nonce; it must never repeat under one key.
707
+ * @param aad Data authenticated but not encrypted; may be empty.
708
+ * @param plaintext The message to encrypt; it is read, not kept.
709
+ * @returns A fresh array of ciphertext then tag, `plaintext.length + 16` bytes,
710
+ * or `null` for bad input.
668
711
  */
669
712
  export const chacha20Poly1305Seal = (key: u8[], nonce: u8[], aad: u8[], plaintext: u8[]): u8[] | null => {
670
713
  const len: i32 = toI32(plaintext.length)
@@ -700,6 +743,14 @@ export const chacha20Poly1305Seal = (key: u8[], nonce: u8[], aad: u8[], plaintex
700
743
  * time before a byte is decrypted, so a forgery gets `null` and nothing else.
701
744
  * Answers `null` too unless `key` is `CHACHA20_KEY_SIZE` bytes and `nonce` is
702
745
  * `CHACHA20POLY1305_NONCE_SIZE`, and when `sealed` is shorter than a tag.
746
+ *
747
+ * @param key The 32-byte key; it is read, not kept, and the caller still wipes
748
+ * it.
749
+ * @param nonce The 12-byte nonce the message was sealed with.
750
+ * @param aad The data authenticated with it; must match what was sealed.
751
+ * @param sealed The ciphertext followed by its 16-byte tag.
752
+ * @returns A fresh array holding the plaintext, or `null` if the tag fails or
753
+ * the input is bad.
703
754
  */
704
755
  export const chacha20Poly1305Open = (key: u8[], nonce: u8[], aad: u8[], sealed: u8[]): u8[] | null => {
705
756
  const total: i32 = toI32(sealed.length)
@@ -740,6 +791,10 @@ export const chacha20Poly1305Open = (key: u8[], nonce: u8[], aad: u8[], sealed:
740
791
  * `sample` (little-endian) and whose nonce is the other twelve, under the
741
792
  * header-protection key `hpKey`. Answers `null` unless `hpKey` is
742
793
  * `CHACHA20_KEY_SIZE` bytes and `sample` is `CHACHA20_HEADER_SAMPLE_SIZE`.
794
+ *
795
+ * @param hpKey The 32-byte header-protection key; it is read, not kept.
796
+ * @param sample The 16 bytes sampled from the packet's ciphertext.
797
+ * @returns A fresh 5-byte mask, or `null` for a bad key or sample length.
743
798
  */
744
799
  export const chacha20HeaderMask = (hpKey: u8[], sample: u8[]): u8[] | null => {
745
800
  if (toI32(hpKey.length) !== CHACHA20_KEY_SIZE || toI32(sample.length) !== CHACHA20_HEADER_SAMPLE_SIZE) {
package/std/crypto/ct.ts CHANGED
@@ -1,21 +1,27 @@
1
1
  /**
2
2
  * `nish/crypto/ct` — comparing secrets without telling the clock where they differ.
3
3
  *
4
- * An `===` loop over two MACs stops at the first differing byte, so the time it
5
- * takes says how many leading bytes an attacker already guessed right, and a
6
- * tag can be recovered one byte at a time. These functions read every byte of
7
- * the window whatever it holds: each pair is XORed, the differences are ORed
8
- * into one accumulator, and that accumulator is tested once, after the loop.
4
+ * import { timingSafeEqual } from "nish/crypto/ct";
9
5
  *
10
- * What is *not* secret is checked first and may branch: the two lengths, and
11
- * whether a window lies inside its array. A caller comparing a received tag
12
- * against an expected one already knows both lengths, so refusing a mismatch
13
- * early leaks nothing the attacker did not send.
6
+ * const ok: boolean = timingSafeEqual(expectedTag, receivedTag);
14
7
  *
15
- * The names are `timingSafeEqual*`, after Node's `crypto.timingSafeEqual`,
16
- * rather than `ctEq`: that one is reserved for the builtin WP34 N6 will add,
17
- * together with the disassembly check that proves the loop stays branch-free
18
- * after LLVM has seen it. Until then this is the discipline and not a proof.
8
+ * **Every byte is read.** An `===` loop over two MACs stops at the first
9
+ * differing byte, so the time it takes says how many leading bytes an attacker
10
+ * already guessed right, and a tag can be recovered one byte at a time. These
11
+ * functions read every byte of the window whatever it holds: each pair is
12
+ * XORed, the differences are ORed into one accumulator, and that accumulator is
13
+ * tested once, after the loop.
14
+ *
15
+ * **Lengths may branch.** What is *not* secret is checked first and may branch:
16
+ * the two lengths, and whether a window lies inside its array. A caller
17
+ * comparing a received tag against an expected one already knows both lengths,
18
+ * so refusing a mismatch early leaks nothing the attacker did not send.
19
+ *
20
+ * **A discipline, not yet a proof.** The names are `timingSafeEqual*`, after
21
+ * Node's `crypto.timingSafeEqual`, rather than `ctEq`: that one is reserved for
22
+ * the builtin WP34 N6 will add, together with the disassembly check that proves
23
+ * the loop stays branch-free after LLVM has seen it. Until then this is the
24
+ * discipline and not a proof.
19
25
  */
20
26
 
21
27
  /**
@@ -29,6 +35,11 @@
29
35
  * it would have looked the same length and been compared only that far. An
30
36
  * array longer than that answers `false` too — no `i32` index reaches its end,
31
37
  * and a compare that cannot read every byte must not say "equal".
38
+ *
39
+ * @param a One byte string, such as the tag that was received.
40
+ * @param b The other, such as the tag computed for comparison.
41
+ * @returns `true` only when both lengths match and every byte does; `false` for
42
+ * different lengths or a length past 2^31 - 1.
32
43
  */
33
44
  export const timingSafeEqual = (a: u8[], b: u8[]): boolean => {
34
45
  if (a.length !== b.length || a.length > 2147483647) {
@@ -53,6 +64,14 @@ export const timingSafeEqual = (a: u8[], b: u8[]): boolean => {
53
64
  * first out-of-range read. That is a decision about the caller's position: a
54
65
  * verifier handed a truncated record should say "not equal", not take the
55
66
  * process down, and the bounds are public, so testing them first is safe.
67
+ *
68
+ * @param a The array holding the first window.
69
+ * @param aOff Where the first window starts in `a`.
70
+ * @param b The array holding the second window.
71
+ * @param bOff Where the second window starts in `b`.
72
+ * @param len How many bytes each window covers.
73
+ * @returns `true` only when both windows lie inside their arrays and every byte
74
+ * matches; `false` for a window out of range.
56
75
  */
57
76
  export const timingSafeEqualAt = (a: u8[], aOff: i32, b: u8[], bOff: i32, len: i32): boolean => {
58
77
  const aLen: i32 = toI32(a.length)
@@ -1,6 +1,20 @@
1
1
  /**
2
2
  * `nish/crypto/ed25519` — the Ed25519 signature scheme of RFC 8032 (§5.1).
3
3
  *
4
+ * import { ED25519_SEED_SIZE, ed25519PublicKey, ed25519Sign, ed25519Verify } from "nish/crypto/ed25519";
5
+ * import { Secret, secret, wipe } from "nish:secret";
6
+ *
7
+ * const raw: u8[] = new Array<u8>(ED25519_SEED_SIZE)
8
+ * crypto.getRandomValues(raw) // any 32 bytes are a seed
9
+ * const seed: Secret<u8[]> = secret(raw)
10
+ * const message: u8[] = [104, 105]
11
+ * const pub: u8[] | null = ed25519PublicKey(seed) // 32 bytes
12
+ * const sig: u8[] | null = ed25519Sign(seed, message) // 64 bytes, R || S
13
+ * wipe(seed)
14
+ * if (pub !== null && sig !== null) {
15
+ * ed25519Verify(pub, message, sig) // true
16
+ * }
17
+ *
4
18
  * Written from the specification (RFC 8032 §5.1.1 to §5.1.7), in this
5
19
  * module's own structure: nothing here is ported from another implementation.
6
20
  * The field is `nish/crypto/x25519`'s GF(2^255 - 19), imported rather than
@@ -23,20 +37,21 @@
23
37
  * is carried first, and so is every cached coordinate. The field functions
24
38
  * state their own bounds (`nish/crypto/x25519`).
25
39
  *
26
- * **Constant time, by construction.** Signing and the public key multiply
27
- * the base point by a secret scalar, and that multiplication branches on and
40
+ * **Constant time, by construction.** Signing and the public key multiply the
41
+ * base point by a secret scalar, and that multiplication branches on and
28
42
  * indexes by nothing the scalar reaches. The scalar is recoded into 64 signed
29
43
  * radix-16 digits in [-8, 8]; each step doubles four times and adds the table
30
- * entry for the digit, which is chosen by reading all eight entries and
31
- * keeping one under a mask, and negated with a masked swap and a masked
32
- * select. The digit arithmetic and the masks use `f25519Add64`, `f25519Sub64`
33
- * and `f25519Mul64`, because signed overflow is undefined and a checked
34
- * operation branches. The reductions mod L run a fixed 512 steps of a
35
- * shift and a masked conditional subtraction over 32-bit limbs in `u64`,
36
- * whose subtraction wraps. Every `if` and every loop bound is on a public
37
- * quantity: a digit position, a limb number, a bit position or a length.
38
- * This is discipline, checked by reading, not by a disassembly fixture as
39
- * `f25519Mul` and the X25519 ladder are (ECC-5, docs/security/crypto-ecc.md).
44
+ * entry for the digit, which is chosen by reading all eight entries and keeping
45
+ * one under a mask, and negated with a masked swap and a masked select.
46
+ *
47
+ * The digit arithmetic and the masks use `f25519Add64`, `f25519Sub64` and
48
+ * `f25519Mul64`, because signed overflow is undefined and a checked operation
49
+ * branches. The reductions mod L run a fixed 512 steps of a shift and a masked
50
+ * conditional subtraction over 32-bit limbs in `u64`, whose subtraction wraps.
51
+ * Every `if` and every loop bound is on a public quantity: a digit position, a
52
+ * limb number, a bit position or a length. This is discipline, checked by
53
+ * reading, not by a disassembly fixture as `f25519Mul` and the X25519 ladder
54
+ * are (ECC-5, docs/security/crypto-ecc.md).
40
55
  *
41
56
  * **Verification is variable time.** Every value `ed25519Verify` handles is
42
57
  * public — the key, the message and the signature — so it may branch on them:
@@ -48,10 +63,12 @@
48
63
  * below p or that is not on the curve, and an x of zero with the sign bit set
49
64
  * are all refused (§5.1.3). R is not decoded: [S]B - [k]A is encoded and
50
65
  * compared with R's bytes, and since the encoding is canonical, an R that is
51
- * not the canonical encoding of a point never matches. A public key of small
52
- * order is not refused, as RFC 8032 does not ask it to be: under such a key,
53
- * some signatures verify for messages its owner never signed, so a protocol
54
- * that needs a signature to bind one key checks the key itself.
66
+ * not the canonical encoding of a point never matches.
67
+ *
68
+ * A public key of small order is not refused, as RFC 8032 does not ask it to
69
+ * be: under such a key, some signatures verify for messages its owner never
70
+ * signed, so a protocol that needs a signature to bind one key checks the key
71
+ * itself.
55
72
  *
56
73
  * **The seed is a `Secret`** (`nish:secret`, docs/LANGUAGE.md "Secrets").
57
74
  * `ed25519PublicKey` and `ed25519Sign` take the 32-byte seed as a
@@ -152,9 +169,13 @@ const ed25519Order = (): u64[] => [
152
169
 
153
170
  /** A point (X : Y : Z : T) in extended coordinates. */
154
171
  class Ed25519Point {
172
+ /** @internal Read and written by this module's point functions; the class is not exported. */
155
173
  x: i64[]
174
+ /** @internal Read and written by this module's point functions; the class is not exported. */
156
175
  y: i64[]
176
+ /** @internal Read and written by this module's point functions; the class is not exported. */
157
177
  z: i64[]
178
+ /** @internal Read and written by this module's point functions; the class is not exported. */
158
179
  t: i64[]
159
180
 
160
181
  /** The identity, (0 : 1 : 1 : 0). */
@@ -168,9 +189,13 @@ class Ed25519Point {
168
189
 
169
190
  /** A point in the form the addition reads: (Y + X, Y - X, 2 d T, 2 Z), each carried. */
170
191
  class Ed25519Cached {
192
+ /** @internal Read and written by this module's point functions; the class is not exported. */
171
193
  ypx: i64[]
194
+ /** @internal Read and written by this module's point functions; the class is not exported. */
172
195
  ymx: i64[]
196
+ /** @internal Read and written by this module's point functions; the class is not exported. */
173
197
  t2d: i64[]
198
+ /** @internal Read and written by this module's point functions; the class is not exported. */
174
199
  z2: i64[]
175
200
 
176
201
  /** The identity, (1, 1, 0, 2). */
@@ -188,16 +213,27 @@ class Ed25519Cached {
188
213
  * is built in; and 2 d, which the cached form multiplies by.
189
214
  */
190
215
  class Ed25519Scratch {
216
+ /** @internal Read and written by this module's point functions; the class is not exported. */
191
217
  a: i64[]
218
+ /** @internal Read and written by this module's point functions; the class is not exported. */
192
219
  b: i64[]
220
+ /** @internal Read and written by this module's point functions; the class is not exported. */
193
221
  c: i64[]
222
+ /** @internal Read and written by this module's point functions; the class is not exported. */
194
223
  d: i64[]
224
+ /** @internal Read and written by this module's point functions; the class is not exported. */
195
225
  e: i64[]
226
+ /** @internal Read and written by this module's point functions; the class is not exported. */
196
227
  f: i64[]
228
+ /** @internal Read and written by this module's point functions; the class is not exported. */
197
229
  g: i64[]
230
+ /** @internal Read and written by this module's point functions; the class is not exported. */
198
231
  h: i64[]
232
+ /** @internal Read and written by this module's point functions; the class is not exported. */
199
233
  entry: Ed25519Cached
234
+ /** @internal Read and written by this module's point functions; the class is not exported. */
200
235
  sum: Ed25519Point
236
+ /** @internal Read and written by this module's point functions; the class is not exported. */
201
237
  d2: i64[]
202
238
 
203
239
  constructor() {
@@ -716,6 +752,10 @@ const ed25519Challenge = (r: u8[], pub: u8[], msg: u8[]): u8[] => {
716
752
  /**
717
753
  * The public key of `seed` (RFC 8032 §5.1.5): the encoding of `a * B`, as 32
718
754
  * plain bytes. Answers `null` unless `seed` is `ED25519_SEED_SIZE` bytes.
755
+ *
756
+ * @param seed The private key, 32 bytes.
757
+ * @returns The 32-byte encoded public key, or `null` for a seed of the wrong
758
+ * length.
719
759
  */
720
760
  export const ed25519PublicKey = (seed: Secret<u8[]>): u8[] | null => expose(seed, ed25519PublicKeyExposed)
721
761
 
@@ -742,6 +782,11 @@ const ed25519PublicKeyExposed = (seed: u8[]): u8[] | null => {
742
782
  * bytes. The public key is derived from the seed again on every call. A
743
783
  * message longer than 2^31 - 1 bytes panics, as in `sha512`: its length does
744
784
  * not survive `toI32`, and the signature would be of a prefix.
785
+ *
786
+ * @param seed The private key, 32 bytes.
787
+ * @param msg The whole message signed.
788
+ * @returns The 64-byte signature `R || S`, or `null` for a seed of the wrong
789
+ * length.
745
790
  */
746
791
  export const ed25519Sign = (seed: Secret<u8[]>, msg: u8[]): u8[] | null => {
747
792
  if (msg.length > 2147483647) {
@@ -805,6 +850,11 @@ const ed25519SignExposed = (seed: u8[], msg: u8[]): u8[] | null => {
805
850
  * bytes panics, as in `ed25519Sign`: a check of a prefix would accept the
806
851
  * signature for any longer message that shares it. Every value here is
807
852
  * public, so the answer may be reached by branches.
853
+ *
854
+ * @param pub The signer's 32-byte encoded public key.
855
+ * @param msg The whole message.
856
+ * @param sig The 64-byte signature `R || S`.
857
+ * @returns `true` when the signature is valid, `false` for anything else.
808
858
  */
809
859
  export const ed25519Verify = (pub: u8[], msg: u8[], sig: u8[]): boolean => {
810
860
  if (msg.length > 2147483647) {