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.
- package/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/crypto/aes.ts
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
* `nish/crypto/aes` — AES-128 and AES-256 (FIPS 197), GCM on top of them
|
|
3
3
|
* (NIST SP 800-38D), and QUIC's AES header-protection mask (RFC 9001 §5.4.3).
|
|
4
4
|
*
|
|
5
|
+
* import { aesKey, aesGcmSeal, aesGcmOpen, aesKeyWipe } from "nish/crypto/aes";
|
|
6
|
+
*
|
|
7
|
+
* const key = aesKey(keyBytes); // 16 or 32 bytes, else null
|
|
8
|
+
* if (key !== null) {
|
|
9
|
+
* // never seal two messages under one key with the same IV
|
|
10
|
+
* const sealed: u8[] | null = aesGcmSeal(key, iv, aad, plaintext);
|
|
11
|
+
* const opened: u8[] | null = sealed === null ? null : aesGcmOpen(key, iv, aad, sealed);
|
|
12
|
+
* aesKeyWipe(key);
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
5
15
|
* Written from the specifications and the papers below, in this module's own
|
|
6
16
|
* layout, with one exception: `ghashMul32` is adapted from BearSSL's
|
|
7
17
|
* `bmul64`, and carries BearSSL's notice. Nothing else here is ported from
|
|
@@ -30,49 +40,53 @@
|
|
|
30
40
|
* `q[0..7]`, word `b` holding bit `b` of all 64 bytes. Inside a word, block `k`
|
|
31
41
|
* owns the 16 bits from `16k`, and in those the byte in row `r` and column `c`
|
|
32
42
|
* of the AES state is bit `4r + c`: each row is one nibble. That makes the two
|
|
33
|
-
* permutations cheap.
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
43
|
+
* permutations cheap.
|
|
44
|
+
*
|
|
45
|
+
* ShiftRows turns row `r` by `r` columns, which is a rotation of nibble `r` by
|
|
46
|
+
* `r` bits; MixColumns needs each byte's neighbour in the next row of its
|
|
47
|
+
* column, which is the word rotated by four bits inside each 16-bit lane.
|
|
48
|
+
* Getting 64 bytes in and out of that layout is an 8 × 8 bit-matrix transpose
|
|
49
|
+
* per byte lane (`aesTranspose`, three rounds of masked swaps between words)
|
|
50
|
+
* after placing each byte in the word and slot the transpose will carry to its
|
|
51
|
+
* bit (`aesPack`, `aesUnpack`).
|
|
40
52
|
*
|
|
41
53
|
* **Constant time.** No table is read anywhere, key schedule included: the
|
|
42
|
-
* S-box is the circuit, and `SubWord` in the key schedule runs the same
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* one
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
54
|
+
* S-box is the circuit, and `SubWord` in the key schedule runs the same circuit
|
|
55
|
+
* on a four-byte state. No branch or index depends on a key, a plaintext, a tag
|
|
56
|
+
* or H: every `if` and every loop bound is on a length, a block number or the
|
|
57
|
+
* round count.
|
|
58
|
+
*
|
|
59
|
+
* GHASH multiplies with no table and no data-dependent shift, and `aesGcmOpen`
|
|
60
|
+
* computes the whole tag and compares it with `aesGcmTagMask` — a difference
|
|
61
|
+
* ORed into one word and handed to `ctEq` — before it decrypts or returns
|
|
62
|
+
* anything. What `tests/ct-asm.js` verifies by disassembly on x86-64 and
|
|
63
|
+
* aarch64 is one full round (`aesBitslicedRound`: S-box circuit, ShiftRows,
|
|
64
|
+
* MixColumns, AddRoundKey), one GHASH multiply (`ghashMultiply`) and the tag
|
|
65
|
+
* compare (`aesGcmTagMask`).
|
|
66
|
+
*
|
|
67
|
+
* A golden case compiles to one module and cannot import this one, so
|
|
68
|
+
* `tests/cases/ct_asm_aes` holds a verbatim copy of those three and the helpers
|
|
69
|
+
* they call, and `tests/link/crypto_aes` checks that the copy and this module
|
|
70
|
+
* agree on generated inputs, so neither can drift from the other unnoticed.
|
|
71
|
+
* Everything around them — packing bytes, the key schedule, the loops over
|
|
72
|
+
* blocks — is the discipline above and not a verified property.
|
|
59
73
|
*
|
|
60
74
|
* **The seams.** Four private functions do nearly all of the cipher's work,
|
|
61
75
|
* each called from exactly one place: `aesScheduleInto` (the key schedule),
|
|
62
76
|
* `aesBlockInto` (one block), `aesCtrInto` (GCTR over a run of bytes) and
|
|
63
77
|
* `ghashBlocksInto` (GHASH over a run of bytes). Each takes the arrays its
|
|
64
|
-
* caller passes, writes into them and allocates nothing the caller needs
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* `docs/wp38-simd.md`). The
|
|
73
|
-
*
|
|
74
|
-
* `roundKeys`, which a process only ever
|
|
75
|
-
* `nish_aes_hw` gives it.
|
|
78
|
+
* caller passes, writes into them and allocates nothing the caller needs back,
|
|
79
|
+
* and its one caller checks every precondition it names first.
|
|
80
|
+
*
|
|
81
|
+
* That is what lets the compiler, natively, put each of those four calls behind
|
|
82
|
+
* a branch on the runtime's `nish_aes_hw` (`src/kernels.ts`, lowered in
|
|
83
|
+
* `src/emit.ts`): on a CPU with AES-NI and PCLMULQDQ, or the ARMv8 AES and
|
|
84
|
+
* PMULL instructions, the call goes to a kernel in `runtime/runtime-aes.c` that
|
|
85
|
+
* does no bounds checks of its own; on any other CPU, under `NISH_AES=soft`, on
|
|
86
|
+
* wasm32 and under Node, the body here runs (WP38 S5, `docs/wp38-simd.md`). The
|
|
87
|
+
* bodies are the bitsliced code, and keep their own defensive checks. The
|
|
88
|
+
* kernel keeps its own layout in a key's `roundKeys`, which a process only ever
|
|
89
|
+
* reads under the one answer `nish_aes_hw` gives it.
|
|
76
90
|
*
|
|
77
91
|
* What runs outside the seams, and so on the bitsliced code whichever way
|
|
78
92
|
* they go: `ghashLengths`, the last GHASH block of every tag, and so the end
|
|
@@ -110,14 +124,14 @@ const AES_BATCH: i32 = 64
|
|
|
110
124
|
* H = E(K, 0^128), so a key used for many messages is expanded once.
|
|
111
125
|
*
|
|
112
126
|
* Make one with `aesKey`. The constructor and the fields are public only
|
|
113
|
-
* because
|
|
114
|
-
* is not trusted: one whose shape is not what `aesKey` makes
|
|
115
|
-
* everywhere (`aesKeyUsable`), and one whose H is zero opens
|
|
116
|
-
* (`aesGcmOpen`). On the runtime kernels' path GHASH takes H and its
|
|
117
|
-
* from `roundKeys`, while `ghashLengths` and a non-12-byte IV's J0 take
|
|
118
|
-
* from `h`; `aesKey` writes both from one schedule, so they agree. A key
|
|
119
|
-
* edited so that they disagree is outside the contract, and what it seals
|
|
120
|
-
*
|
|
127
|
+
* because tests build keys by hand and read their words, and a key built or
|
|
128
|
+
* edited by hand is not trusted: one whose shape is not what `aesKey` makes
|
|
129
|
+
* answers `null` everywhere (`aesKeyUsable`), and one whose H is zero opens
|
|
130
|
+
* nothing (`aesGcmOpen`). On the runtime kernels' path GHASH takes H and its
|
|
131
|
+
* powers from `roundKeys`, while `ghashLengths` and a non-12-byte IV's J0 take
|
|
132
|
+
* it from `h`; `aesKey` writes both from one schedule, so they agree. A key
|
|
133
|
+
* edited so that they disagree is outside the contract, and what it seals then
|
|
134
|
+
* depends on which path the process runs.
|
|
121
135
|
*
|
|
122
136
|
* Both are secret. A holder done with a key hands it to `aesKeyWipe`, whose
|
|
123
137
|
* volatile stores zero all of `roundKeys` (the kernels' tail of H's powers
|
|
@@ -140,6 +154,14 @@ export class AesKey {
|
|
|
140
154
|
*/
|
|
141
155
|
h: u64[]
|
|
142
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Wraps a schedule array and allocates `h`; neither is filled. `aesKey` calls
|
|
159
|
+
* this and then fills both, and a caller makes keys with `aesKey`.
|
|
160
|
+
*
|
|
161
|
+
* @param rounds Nr: 10 for AES-128, 14 for AES-256.
|
|
162
|
+
* @param roundKeys The schedule array, `8 * (rounds + 1)` words; it is kept,
|
|
163
|
+
* not copied.
|
|
164
|
+
*/
|
|
143
165
|
constructor(rounds: i32, roundKeys: u64[]) {
|
|
144
166
|
this.rounds = rounds
|
|
145
167
|
this.roundKeys = roundKeys
|
|
@@ -154,6 +176,8 @@ export class AesKey {
|
|
|
154
176
|
* is about to be. `rounds` is not secret and stays. A wiped key seals under
|
|
155
177
|
* an all-zero schedule and, its H being zero, opens nothing (`aesGcmOpen`);
|
|
156
178
|
* `aesKeyInto` installs a new key into it.
|
|
179
|
+
*
|
|
180
|
+
* @param key The key to wipe; it stays usable as an `aesKeyInto` target.
|
|
157
181
|
*/
|
|
158
182
|
export const aesKeyWipe = (key: AesKey): void => {
|
|
159
183
|
wipe(key.roundKeys)
|
|
@@ -484,6 +508,10 @@ const aesAddRoundKey = (q: u64[], rk: u64[], at: i32): void => {
|
|
|
484
508
|
* `at`, which is a round number times eight. Exported so that
|
|
485
509
|
* `tests/link/crypto_aes` can hold the copy the disassembly check reads to
|
|
486
510
|
* this one; a caller has no other use for it.
|
|
511
|
+
*
|
|
512
|
+
* @param q The bitsliced state of four blocks, eight words, updated in place.
|
|
513
|
+
* @param rk The bitsliced round keys.
|
|
514
|
+
* @param at Where this round's eight words start in `rk`.
|
|
487
515
|
*/
|
|
488
516
|
export const aesBitslicedRound = (q: u64[], rk: u64[], at: i32): void => {
|
|
489
517
|
aesSbox(q)
|
|
@@ -700,6 +728,11 @@ const aesEncryptOne = (key: AesKey, src: u8[], dst: u8[]): void => {
|
|
|
700
728
|
* Makes an `AesKey` from 16 bytes (AES-128) or 32 (AES-256): the key
|
|
701
729
|
* schedule, bitsliced, and H = E(K, 0^128) for GCM. Any other length — 24
|
|
702
730
|
* bytes included, since AES-192 is not offered — answers `null`.
|
|
731
|
+
*
|
|
732
|
+
* @param key The raw key, 16 or 32 bytes; it is read, not kept, and the caller
|
|
733
|
+
* still wipes it.
|
|
734
|
+
* @returns A fresh key, which the caller hands to `aesKeyWipe` when done, or
|
|
735
|
+
* `null` for another length.
|
|
703
736
|
*/
|
|
704
737
|
export const aesKey = (key: u8[]): AesKey | null => {
|
|
705
738
|
const length: i32 = toI32(key.length)
|
|
@@ -727,16 +760,22 @@ export const aesKey = (key: u8[]): AesKey | null => {
|
|
|
727
760
|
* arena scope may be put around; this stores numbers only, so a caller that
|
|
728
761
|
* keeps one `AesKey` per connection installs every key inside a `using a =
|
|
729
762
|
* arena()` block and leaves the arena where it was. The schedule and H are
|
|
730
|
-
* secret: the caller hands `out` to `aesKeyWipe` when it is done with the
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
*
|
|
734
|
-
*
|
|
735
|
-
* copies are the kernels' own, in
|
|
736
|
-
* leave in vector registers no wipe here
|
|
763
|
+
* secret: the caller hands `out` to `aesKeyWipe` when it is done with the key.
|
|
764
|
+
*
|
|
765
|
+
* The schedule is written straight into `out.roundKeys`, and the working copies
|
|
766
|
+
* the bitsliced schedule and the block H passes through are wiped with `wipe`'s
|
|
767
|
+
* volatile stores before they are released. On a CPU that runs the runtime's
|
|
768
|
+
* kernels (the seams, in the module docs) those copies are the kernels' own, in
|
|
769
|
+
* `runtime/runtime-aes.c`, and what they leave in vector registers no wipe here
|
|
770
|
+
* reaches.
|
|
737
771
|
*
|
|
738
772
|
* `aesKey` makes its key through this, so this is the one call of
|
|
739
773
|
* `aesScheduleInto`, and the checks its contract needs are made here.
|
|
774
|
+
*
|
|
775
|
+
* @param key The raw key, 16 or 32 bytes; it is read, not kept, and the caller
|
|
776
|
+
* still wipes it.
|
|
777
|
+
* @param out The key to overwrite, its `roundKeys` sized for this key length.
|
|
778
|
+
* @returns `true` if `out` now holds the key; `false` if it was left as it was.
|
|
740
779
|
*/
|
|
741
780
|
export const aesKeyInto = (key: u8[], out: AesKey): boolean => {
|
|
742
781
|
const length: i32 = toI32(key.length)
|
|
@@ -759,7 +798,15 @@ export const aesKeyInto = (key: u8[], out: AesKey): boolean => {
|
|
|
759
798
|
return true
|
|
760
799
|
}
|
|
761
800
|
|
|
762
|
-
/**
|
|
801
|
+
/**
|
|
802
|
+
* One block encrypted (FIPS 197 §5.1, ECB): 16 bytes in, 16 fresh bytes out, or
|
|
803
|
+
* `null` for another length.
|
|
804
|
+
*
|
|
805
|
+
* @param key The expanded key.
|
|
806
|
+
* @param block The 16 bytes to encrypt; it is read, not kept.
|
|
807
|
+
* @returns A fresh 16-byte block, or `null` for another length or a key
|
|
808
|
+
* `aesKey` would not make.
|
|
809
|
+
*/
|
|
763
810
|
export const aesEncryptBlock = (key: AesKey, block: u8[]): u8[] | null => {
|
|
764
811
|
if (!aesKeyUsable(key) || toI32(block.length) !== AES_BLOCK) {
|
|
765
812
|
return null
|
|
@@ -773,6 +820,11 @@ export const aesEncryptBlock = (key: AesKey, block: u8[]): u8[] | null => {
|
|
|
773
820
|
* The AES header-protection mask of RFC 9001 §5.4.3: the first five bytes of
|
|
774
821
|
* the 16-byte `sample` encrypted under the header-protection key. `null` for
|
|
775
822
|
* a sample of another length.
|
|
823
|
+
*
|
|
824
|
+
* @param key The header-protection key.
|
|
825
|
+
* @param sample The 16 bytes sampled from the packet's ciphertext.
|
|
826
|
+
* @returns A fresh 5-byte mask, or `null` for another sample length or a key
|
|
827
|
+
* `aesKey` would not make.
|
|
776
828
|
*/
|
|
777
829
|
export const aesHeaderMask = (key: AesKey, sample: u8[]): u8[] | null => {
|
|
778
830
|
if (!aesKeyUsable(key) || toI32(sample.length) !== AES_BLOCK) {
|
|
@@ -836,6 +888,10 @@ const ghashMul32 = (x: u64, y: u64): u64 => {
|
|
|
836
888
|
* The 128 × 128 product is Karatsuba twice over: three 64 × 64 products, each
|
|
837
889
|
* of three 32 × 32 ones from `ghashMul32`. Straight-line code; exported, like
|
|
838
890
|
* `aesBitslicedRound`, so the disassembly check's copy can be held to it.
|
|
891
|
+
*
|
|
892
|
+
* @param y The running GHASH value, two words, multiplied in place.
|
|
893
|
+
* @param hHi The high half of H.
|
|
894
|
+
* @param hLo The low half of H.
|
|
839
895
|
*/
|
|
840
896
|
export const ghashMultiply = (y: u64[], hHi: u64, hLo: u64): void => {
|
|
841
897
|
const low32: u64 = (toU64(1) << toU64(32)) - toU64(1)
|
|
@@ -1068,6 +1124,12 @@ const aesGcmTag = (y: u64[], key: AesKey, j0: u8[], aad: u8[], text: u8[], textL
|
|
|
1068
1124
|
* every bit of both tags is read and nothing is branched on until the caller
|
|
1069
1125
|
* tests the one answer. Exported, like `aesBitslicedRound`, so the
|
|
1070
1126
|
* disassembly check's copy can be held to it.
|
|
1127
|
+
*
|
|
1128
|
+
* @param tagHi The high half of the expected tag.
|
|
1129
|
+
* @param tagLo The low half of the expected tag.
|
|
1130
|
+
* @param gotHi The high half of the received tag.
|
|
1131
|
+
* @param gotLo The low half of the received tag.
|
|
1132
|
+
* @returns All-ones if the tags match, zero if they do not.
|
|
1071
1133
|
*/
|
|
1072
1134
|
export const aesGcmTagMask = (tagHi: u64, tagLo: u64, gotHi: u64, gotLo: u64): u64 =>
|
|
1073
1135
|
ctEq((tagHi ^ gotHi) | (tagLo ^ gotLo), toU64(0))
|
|
@@ -1078,6 +1140,13 @@ export const aesGcmTagMask = (tagHi: u64, tagLo: u64, gotHi: u64, gotLo: u64): u
|
|
|
1078
1140
|
* bytes is the fast and usual one. `null` for an empty IV, for a key
|
|
1079
1141
|
* `aesKey` did not make, and for a plaintext longer than 2^31 - 17 bytes,
|
|
1080
1142
|
* whose sealed form would not fit in an array.
|
|
1143
|
+
*
|
|
1144
|
+
* @param key The expanded key.
|
|
1145
|
+
* @param iv The nonce, any non-zero length; it must never repeat under one key.
|
|
1146
|
+
* @param aad Data authenticated but not encrypted; may be empty.
|
|
1147
|
+
* @param plaintext The message to encrypt; it is read, not kept.
|
|
1148
|
+
* @returns A fresh array of ciphertext then tag, `plaintext.length + 16` bytes,
|
|
1149
|
+
* or `null` for bad input.
|
|
1081
1150
|
*/
|
|
1082
1151
|
export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u8[] | null => {
|
|
1083
1152
|
const length: i32 = toI32(plaintext.length)
|
|
@@ -1105,6 +1174,13 @@ export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u
|
|
|
1105
1174
|
* the key is not one `aesKey` made.
|
|
1106
1175
|
* The tag is computed over the received ciphertext and compared in constant
|
|
1107
1176
|
* time before anything is decrypted, so a forgery yields no plaintext at all.
|
|
1177
|
+
*
|
|
1178
|
+
* @param key The expanded key.
|
|
1179
|
+
* @param iv The nonce the message was sealed with.
|
|
1180
|
+
* @param aad The data authenticated with it; must match what was sealed.
|
|
1181
|
+
* @param sealed The ciphertext followed by its 16-byte tag.
|
|
1182
|
+
* @returns A fresh array holding the plaintext, or `null` if the tag fails or
|
|
1183
|
+
* the input is bad.
|
|
1108
1184
|
*/
|
|
1109
1185
|
export const aesGcmOpen = (key: AesKey, iv: u8[], aad: u8[], sealed: u8[]): u8[] | null => {
|
|
1110
1186
|
const total: i32 = toI32(sealed.length)
|
package/std/crypto/base64url.ts
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
* `nish/crypto/base64url` — RFC 4648 §5 base64url, unpadded, as JWS and the
|
|
3
3
|
* relay's grants spell a key or a tag inside a URL or a header.
|
|
4
4
|
*
|
|
5
|
+
* import { base64urlEncode, base64urlDecode } from "nish/crypto/base64url";
|
|
6
|
+
*
|
|
7
|
+
* const text: string = base64urlEncode(tag); // no `=` padding
|
|
8
|
+
* const back: u8[] | null = base64urlDecode(text); // null for any other spelling
|
|
9
|
+
*
|
|
5
10
|
* The alphabet is `A-Z a-z 0-9 - _`, and there is no `=`: the length of the
|
|
6
11
|
* text already says how many bytes the last group holds (RFC 4648 §3.2 lets a
|
|
7
12
|
* specification that knows its lengths drop the padding, and RFC 7515 §2 does).
|
|
@@ -26,11 +31,13 @@
|
|
|
26
31
|
* **Neither direction indexes or branches on the data.** What is encoded here
|
|
27
32
|
* is usually a key, a nonce or a MAC, and a lookup table indexed by a secret
|
|
28
33
|
* sextet leaves its trace in the cache. So a sextet becomes a character, and a
|
|
29
|
-
* character a sextet, by arithmetic on range masks: `(lo - 1 - c) & (c - hi -
|
|
30
|
-
* is negative exactly when `lo <= c <= hi`, and an arithmetic shift by 31
|
|
31
|
-
* that sign into an all-ones or all-zeros mask.
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
+
* character a sextet, by arithmetic on range masks: `(lo - 1 - c) & (c - hi -
|
|
35
|
+
* 1)` is negative exactly when `lo <= c <= hi`, and an arithmetic shift by 31
|
|
36
|
+
* turns that sign into an all-ones or all-zeros mask.
|
|
37
|
+
*
|
|
38
|
+
* A malformed character is not refused where it is found either: the verdict is
|
|
39
|
+
* ORed into one word and read once, after the whole text. The text's length is
|
|
40
|
+
* public and is tested first.
|
|
34
41
|
*
|
|
35
42
|
* Private helpers share the importing program's flat symbol namespace
|
|
36
43
|
* (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
|
|
@@ -92,6 +99,10 @@ const base64urlSextetOf = (c: i32): i32 => {
|
|
|
92
99
|
* so one index walks the input and the only branches are on how many bits are
|
|
93
100
|
* waiting — which depends on the position, never on the bytes. An array longer
|
|
94
101
|
* than 2^31 - 1 bytes panics.
|
|
102
|
+
*
|
|
103
|
+
* @param data The bytes to encode; they are read, not changed.
|
|
104
|
+
* @returns The unpadded base64url text, `ceil(4n / 3)` characters for `n`
|
|
105
|
+
* bytes.
|
|
95
106
|
*/
|
|
96
107
|
export const base64urlEncode = (data: u8[]): string => {
|
|
97
108
|
// Refused before `toI32`, which saturates a longer length under
|
|
@@ -130,6 +141,10 @@ export const base64urlEncode = (data: u8[]): string => {
|
|
|
130
141
|
* Each group of four characters becomes one 24-bit word, six bits a
|
|
131
142
|
* character, which splits into three bytes; a short final group of two or
|
|
132
143
|
* three characters fills the top of the word and gives one or two.
|
|
144
|
+
*
|
|
145
|
+
* @param text The unpadded base64url text, usually from a peer.
|
|
146
|
+
* @returns A fresh array of the decoded bytes, or `null` when `text` is not the
|
|
147
|
+
* canonical spelling of any bytes.
|
|
133
148
|
*/
|
|
134
149
|
export const base64urlDecode = (text: string): u8[] | null => {
|
|
135
150
|
// A text past 2^31 - 1 bytes is refused before `toI32`, which would
|
|
@@ -238,6 +253,10 @@ const base64StdPadSextetOf = (c: i32): i32 => {
|
|
|
238
253
|
* `data` in padded standard base64, as one line: PEM's body, and SCRAM's
|
|
239
254
|
* nonces, salts and proofs (RFC 5802 §5.1). One `=` closes a final group of
|
|
240
255
|
* two bytes, two a group of one. An array longer than 2^31 - 1 bytes panics.
|
|
256
|
+
*
|
|
257
|
+
* @param data The bytes to encode; they are read, not changed.
|
|
258
|
+
* @returns The padded standard base64 text, `4 × ceil(n / 3)` characters for
|
|
259
|
+
* `n` bytes, with no line breaks.
|
|
241
260
|
*/
|
|
242
261
|
export const base64StdPadEncode = (data: u8[]): string => {
|
|
243
262
|
if (data.length > 2147483647) {
|
|
@@ -272,6 +291,11 @@ export const base64StdPadEncode = (data: u8[]): string => {
|
|
|
272
291
|
* The empty text is the spelling of no bytes (RFC 4648 §10). Where the padding
|
|
273
292
|
* is depends on the length alone; the characters themselves are read by masks
|
|
274
293
|
* and judged once, after the last.
|
|
294
|
+
*
|
|
295
|
+
* @param text The padded standard base64 text, with no line breaks or
|
|
296
|
+
* whitespace.
|
|
297
|
+
* @returns A fresh array of the decoded bytes, or `null` when `text` is not
|
|
298
|
+
* their canonical padded spelling.
|
|
275
299
|
*/
|
|
276
300
|
export const base64StdPadDecode = (text: string): u8[] | null => {
|
|
277
301
|
if (text.length > 2147483647) {
|
|
@@ -22,19 +22,19 @@
|
|
|
22
22
|
* itself; this module holds them until there are enough, or until a short
|
|
23
23
|
* window has passed.
|
|
24
24
|
*
|
|
25
|
-
* **When a batch is sent.** `run(now)` sends work when the queue holds at
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* due.
|
|
25
|
+
* **When a batch is sent.** `run(now)` sends work when the queue holds at least
|
|
26
|
+
* `width` requests, or when the oldest has waited `holdNanos`; a hold of 0
|
|
27
|
+
* sends whatever is queued at every `run`. It takes at most `width × cores`
|
|
28
|
+
* requests a run, the oldest first, and spreads them evenly over `min(cores,
|
|
29
|
+
* taken)` tasks, each running its share as one batch, so a run takes about one
|
|
30
|
+
* batch's time and the event loop gets control back between runs.
|
|
31
|
+
*
|
|
32
|
+
* Spreading rather than filling is deliberate: while a run is going nothing
|
|
33
|
+
* else can use the free cores, and a batch of two on each of three cores ends
|
|
34
|
+
* sooner than one of six on one. Under load the requests that arrive during a
|
|
35
|
+
* run fill the next one, so the batches fill on their own; at light load each
|
|
36
|
+
* request runs alone on a core, as a task per login would. `waitNanos(now)`
|
|
37
|
+
* says how long the loop may sleep before a `run` is due.
|
|
38
38
|
*
|
|
39
39
|
* **`run` blocks.** It opens a scope, runs the shares, one on the calling
|
|
40
40
|
* thread, and returns once every share has finished: about one batch, 60–95
|
|
@@ -42,6 +42,14 @@
|
|
|
42
42
|
* password-hashing guide describes for a scope on the event loop's thread;
|
|
43
43
|
* give the batcher a thread of its own to avoid it.
|
|
44
44
|
*
|
|
45
|
+
* **On several cores, a batcher per process.** One batcher over every core
|
|
46
|
+
* cannot start a run while one is going, so at light load a login waits for
|
|
47
|
+
* the run in progress while cores sit idle. A prefork server with one process
|
|
48
|
+
* a core, each with a batcher of `cores: 1`, overlaps the processes' runs:
|
|
49
|
+
* measured, it matched a plain process per core at light load and one
|
|
50
|
+
* batcher over every core under load (docs/security/crypto-bcrypt.md, "One
|
|
51
|
+
* batcher per process").
|
|
52
|
+
*
|
|
45
53
|
* **The batcher keeps its own copy of each password.** `verify` and `hash`
|
|
46
54
|
* borrow the caller's `Secret<u8[]>`, as any function taking one does: they
|
|
47
55
|
* copy it, inside `exposeWith`, into a holder of the batcher's own (a class
|
|
@@ -59,8 +67,9 @@
|
|
|
59
67
|
* then verifies each of that batch's lanes alone, so only the damaged row
|
|
60
68
|
* answers the `Err` and the rest answer as they would have. A `hash` whose
|
|
61
69
|
* salt is not 16 bytes is answered with its `Err` at the next `run`, without
|
|
62
|
-
* being batched.
|
|
63
|
-
*
|
|
70
|
+
* being batched. A run does not answer in arrival order: bad-salt answers
|
|
71
|
+
* come first, then verifies grouped by cost, then hashes, so match answers to
|
|
72
|
+
* requests by `ticket`.
|
|
64
73
|
*
|
|
65
74
|
* Names carry the `bcryptBatch` prefix because a `std/` module's private
|
|
66
75
|
* functions share the importing program's flat symbol namespace
|
|
@@ -109,6 +118,14 @@ export class BcryptAnswer {
|
|
|
109
118
|
/** "" unless the request is an `Err`, as `bcryptVerify` or `bcryptHash` words it. */
|
|
110
119
|
error: string
|
|
111
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Holds one answer as given; `run` makes these.
|
|
123
|
+
*
|
|
124
|
+
* @param ticket The ticket of the request answered.
|
|
125
|
+
* @param ok The answer: the password matched, or the hash was made.
|
|
126
|
+
* @param hash The `$2b$` hash a hash request made, or "".
|
|
127
|
+
* @param error The `Err`'s text, or "" for none.
|
|
128
|
+
*/
|
|
112
129
|
constructor(ticket: i32, ok: boolean, hash: string, error: string) {
|
|
113
130
|
this.ticket = ticket
|
|
114
131
|
this.ok = ok
|
|
@@ -122,12 +139,14 @@ export class BcryptAnswer {
|
|
|
122
139
|
* queue's array, made at the password's length and filled by `bcryptBatchTake`.
|
|
123
140
|
*/
|
|
124
141
|
class BcryptBatchKey {
|
|
142
|
+
/** @internal Read by this module's batch functions; the class is not exported. */
|
|
125
143
|
readonly key: Secret<u8[]>
|
|
126
144
|
|
|
127
145
|
constructor(length: i32) {
|
|
128
146
|
this.key = secret(new Array<u8>(length))
|
|
129
147
|
}
|
|
130
148
|
|
|
149
|
+
/** @internal Called by `BcryptBatcher.run`; the class is not exported. */
|
|
131
150
|
wipe(): void {
|
|
132
151
|
wipe(this.key)
|
|
133
152
|
}
|
|
@@ -162,13 +181,25 @@ const bcryptBatchTake = (password: Secret<u8[]>): BcryptBatchKey => {
|
|
|
162
181
|
* `Err`.
|
|
163
182
|
*/
|
|
164
183
|
class BcryptBatchShare {
|
|
184
|
+
/** @internal Read by this module's task functions; the class is not exported. */
|
|
165
185
|
passwords: BcryptBatchKey[]
|
|
186
|
+
/** @internal Read by this module's task functions; the class is not exported. */
|
|
166
187
|
kinds: i32[]
|
|
167
|
-
/**
|
|
188
|
+
/**
|
|
189
|
+
* A verify's stored hash, "" for a hash.
|
|
190
|
+
*
|
|
191
|
+
* @internal Read by this module's task functions; the class is not exported.
|
|
192
|
+
*/
|
|
168
193
|
hashes: string[]
|
|
169
|
-
/**
|
|
194
|
+
/**
|
|
195
|
+
* 16 bytes a request; a verify's are zeros and are not read.
|
|
196
|
+
*
|
|
197
|
+
* @internal Read by this module's task functions; the class is not exported.
|
|
198
|
+
*/
|
|
170
199
|
salts: u8[]
|
|
200
|
+
/** @internal Read by this module's task functions; the class is not exported. */
|
|
171
201
|
cost: i32
|
|
202
|
+
/** @internal Read by this module's task functions; the class is not exported. */
|
|
172
203
|
width: i32
|
|
173
204
|
|
|
174
205
|
constructor(cost: i32, width: i32) {
|
|
@@ -425,23 +456,29 @@ const bcryptBatchAnswerOf = (ticket: i32, kind: i32, said: string): BcryptAnswer
|
|
|
425
456
|
* `waitNanos` says when the next `run` is due.
|
|
426
457
|
*/
|
|
427
458
|
export class BcryptBatcher {
|
|
428
|
-
holdNanos: i64
|
|
459
|
+
private holdNanos: i64
|
|
429
460
|
/** The queue, from `head`: each request's copy of its password, kind, stored hash, salt (16 bytes each), ticket and arrival. */
|
|
430
|
-
passwords: BcryptBatchKey[]
|
|
431
|
-
kinds: i32[]
|
|
432
|
-
hashes: string[]
|
|
433
|
-
salts: u8[]
|
|
434
|
-
tickets: i32[]
|
|
435
|
-
arrived: i64[]
|
|
461
|
+
private passwords: BcryptBatchKey[]
|
|
462
|
+
private kinds: i32[]
|
|
463
|
+
private hashes: string[]
|
|
464
|
+
private salts: u8[]
|
|
465
|
+
private tickets: i32[]
|
|
466
|
+
private arrived: i64[]
|
|
436
467
|
/** Answers made without a batch (a salt of the wrong length), handed out by the next `run`. */
|
|
437
|
-
early: BcryptAnswer[]
|
|
438
|
-
width: i32
|
|
439
|
-
cores: i32
|
|
440
|
-
cost: i32
|
|
441
|
-
head: i32
|
|
468
|
+
private early: BcryptAnswer[]
|
|
469
|
+
private width: i32
|
|
470
|
+
private cores: i32
|
|
471
|
+
private cost: i32
|
|
472
|
+
private head: i32
|
|
442
473
|
/** The next ticket. */
|
|
443
|
-
next: i32
|
|
474
|
+
private next: i32
|
|
444
475
|
|
|
476
|
+
/**
|
|
477
|
+
* Takes `config` as it is and starts an empty queue. `bcryptBatcher` checks
|
|
478
|
+
* the config first; make batchers with it.
|
|
479
|
+
*
|
|
480
|
+
* @param config The policy; its fields are kept, not checked.
|
|
481
|
+
*/
|
|
445
482
|
constructor(config: BcryptBatchConfig) {
|
|
446
483
|
this.width = config.width
|
|
447
484
|
this.holdNanos = config.holdNanos
|
|
@@ -458,7 +495,11 @@ export class BcryptBatcher {
|
|
|
458
495
|
this.next = 0
|
|
459
496
|
}
|
|
460
497
|
|
|
461
|
-
/**
|
|
498
|
+
/**
|
|
499
|
+
* How many requests are queued or answered and not yet handed out by `run`.
|
|
500
|
+
*
|
|
501
|
+
* @returns The count; 0 once `run` has handed out every answer.
|
|
502
|
+
*/
|
|
462
503
|
pending(): i32 {
|
|
463
504
|
return toI32(this.kinds.length) - this.head + toI32(this.early.length)
|
|
464
505
|
}
|
|
@@ -468,6 +509,13 @@ export class BcryptBatcher {
|
|
|
468
509
|
* (`monotonicNanos()`), and answers its ticket. The batcher queues a copy
|
|
469
510
|
* of `password` and wipes the copy once the verify has run; the caller
|
|
470
511
|
* wipes its own.
|
|
512
|
+
*
|
|
513
|
+
* @param password The password to check; the batcher copies it, and the
|
|
514
|
+
* caller still owns and wipes it.
|
|
515
|
+
* @param hash The stored hash; its cost sets the work, so it must be one you
|
|
516
|
+
* stored.
|
|
517
|
+
* @param now The time of the call, from `monotonicNanos()`.
|
|
518
|
+
* @returns The ticket its `BcryptAnswer` will carry.
|
|
471
519
|
*/
|
|
472
520
|
verify(password: Secret<u8[]>, hash: string, now: i64): i32 {
|
|
473
521
|
const ticket: i32 = this.next
|
|
@@ -482,6 +530,12 @@ export class BcryptBatcher {
|
|
|
482
530
|
* and answers its ticket. The batcher queues a copy of `password` and
|
|
483
531
|
* wipes the copy once the hash is made; a salt of another length is
|
|
484
532
|
* answered with its `Err` by the next `run`, and no copy is made.
|
|
533
|
+
*
|
|
534
|
+
* @param password The password to hash; the batcher copies it unless the salt
|
|
535
|
+
* is bad, and the caller still owns and wipes it.
|
|
536
|
+
* @param salt 16 fresh random bytes; read, not kept.
|
|
537
|
+
* @param now The time of the call, from `monotonicNanos()`.
|
|
538
|
+
* @returns The ticket its `BcryptAnswer` will carry.
|
|
485
539
|
*/
|
|
486
540
|
hash(password: Secret<u8[]>, salt: u8[], now: i64): i32 {
|
|
487
541
|
const ticket: i32 = this.next
|
|
@@ -495,7 +549,7 @@ export class BcryptBatcher {
|
|
|
495
549
|
}
|
|
496
550
|
|
|
497
551
|
/** Appends one request to the queue. */
|
|
498
|
-
enqueue(password: Secret<u8[]>, kind: i32, hash: string, salt: u8[], ticket: i32, now: i64): void {
|
|
552
|
+
private enqueue(password: Secret<u8[]>, kind: i32, hash: string, salt: u8[], ticket: i32, now: i64): void {
|
|
499
553
|
this.passwords.push(bcryptBatchTake(password))
|
|
500
554
|
this.kinds.push(kind)
|
|
501
555
|
this.hashes.push(hash)
|
|
@@ -510,6 +564,10 @@ export class BcryptBatcher {
|
|
|
510
564
|
* Nanoseconds from `now` until a `run` is due: 0 when it is due now (a full
|
|
511
565
|
* batch is queued, the oldest request has waited `holdNanos`, or an answer
|
|
512
566
|
* is waiting), -1 when nothing is queued.
|
|
567
|
+
*
|
|
568
|
+
* @param now The time of the call, from `monotonicNanos()`.
|
|
569
|
+
* @returns How many nanoseconds the loop may wait before the next `run`: 0
|
|
570
|
+
* for now, -1 for no work.
|
|
513
571
|
*/
|
|
514
572
|
waitNanos(now: i64): i64 {
|
|
515
573
|
if (toI32(this.early.length) > 0) {
|
|
@@ -532,6 +590,12 @@ export class BcryptBatcher {
|
|
|
532
590
|
* oldest requests, spread evenly over up to `cores` tasks, each running
|
|
533
591
|
* its share as batches of up to `width`. Returns once every task has
|
|
534
592
|
* finished, with the copy of each password it ran wiped.
|
|
593
|
+
*
|
|
594
|
+
* @param now The time of the call, from `monotonicNanos()`.
|
|
595
|
+
* @returns A fresh array of the answers this call made, possibly empty:
|
|
596
|
+
* bad-salt answers first, then the verifies grouped by cost, lowest
|
|
597
|
+
* first, then the hashes. Match answers to requests by `ticket`, not by
|
|
598
|
+
* position.
|
|
535
599
|
*/
|
|
536
600
|
run(now: i64): BcryptAnswer[] {
|
|
537
601
|
const answers: BcryptAnswer[] = []
|
|
@@ -607,7 +671,7 @@ export class BcryptBatcher {
|
|
|
607
671
|
* The queue indices of the `taken` oldest requests, verifies by the cost
|
|
608
672
|
* their hash names and then hashes, each group in arrival order.
|
|
609
673
|
*/
|
|
610
|
-
orderOf(taken: i32): i32[] {
|
|
674
|
+
private orderOf(taken: i32): i32[] {
|
|
611
675
|
const keyed: i32[] = []
|
|
612
676
|
const keys: i32[] = []
|
|
613
677
|
for (let i: i32 = this.head; i < this.head + taken && i < toI32(this.kinds.length); i++) {
|
|
@@ -643,7 +707,7 @@ export class BcryptBatcher {
|
|
|
643
707
|
}
|
|
644
708
|
|
|
645
709
|
/** Drops the requests before `head` once they are most of the queue, so a long-lived batcher's arrays stay small. */
|
|
646
|
-
compact(): void {
|
|
710
|
+
private compact(): void {
|
|
647
711
|
if (this.head < 64 || this.head * 2 < toI32(this.kinds.length)) {
|
|
648
712
|
return
|
|
649
713
|
}
|
|
@@ -684,6 +748,9 @@ export class BcryptBatcher {
|
|
|
684
748
|
* A new batcher for `config`, or an `Err` naming the field out of range: a
|
|
685
749
|
* width outside 1 to 6, a negative hold, fewer than one core, or a cost
|
|
686
750
|
* outside 4 to 31.
|
|
751
|
+
*
|
|
752
|
+
* @param config The policy: width, hold, cores and cost.
|
|
753
|
+
* @returns A new, empty batcher, or an `Err` naming the field out of range.
|
|
687
754
|
*/
|
|
688
755
|
export const bcryptBatcher = (config: BcryptBatchConfig): Result<BcryptBatcher, string> => {
|
|
689
756
|
if (config.width < 1 || config.width > BCRYPT_BATCH_MAX_WIDTH) {
|