@amritk/nish-aarch64-linux 0.34.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +22 -13
- package/bin/nish +0 -0
- package/docs/AI.md +86 -16
- package/package.json +1 -1
- package/runtime/async.d.mts +49 -0
- package/runtime/async.mjs +27 -0
- package/runtime/nish.d.ts +4681 -427
- package/runtime/nish.h +6 -2
- package/runtime/nish.mjs +4 -0
- package/runtime/runtime-net.c +22 -0
- package/runtime/runtime-shm.c +93 -2
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/README.md +131 -131
- package/std/base64.ts +34 -5
- package/std/bytes.ts +258 -42
- 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 +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- 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 +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- 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 +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +297 -77
- package/std/net/http1-server.ts +430 -94
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +275 -101
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +801 -112
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +264 -52
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +1124 -201
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- 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 +430 -40
- package/std/sync/shm.ts +222 -15
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- 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
|
|
@@ -110,14 +120,14 @@ const AES_BATCH: i32 = 64
|
|
|
110
120
|
* H = E(K, 0^128), so a key used for many messages is expanded once.
|
|
111
121
|
*
|
|
112
122
|
* 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
|
-
*
|
|
123
|
+
* because tests build keys by hand and read their words, and a key built or
|
|
124
|
+
* edited by hand is not trusted: one whose shape is not what `aesKey` makes
|
|
125
|
+
* answers `null` everywhere (`aesKeyUsable`), and one whose H is zero opens
|
|
126
|
+
* nothing (`aesGcmOpen`). On the runtime kernels' path GHASH takes H and its
|
|
127
|
+
* powers from `roundKeys`, while `ghashLengths` and a non-12-byte IV's J0 take
|
|
128
|
+
* it from `h`; `aesKey` writes both from one schedule, so they agree. A key
|
|
129
|
+
* edited so that they disagree is outside the contract, and what it seals then
|
|
130
|
+
* depends on which path the process runs.
|
|
121
131
|
*
|
|
122
132
|
* Both are secret. A holder done with a key hands it to `aesKeyWipe`, whose
|
|
123
133
|
* volatile stores zero all of `roundKeys` (the kernels' tail of H's powers
|
|
@@ -140,6 +150,14 @@ export class AesKey {
|
|
|
140
150
|
*/
|
|
141
151
|
h: u64[]
|
|
142
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Wraps a schedule array and allocates `h`; neither is filled. `aesKey` calls
|
|
155
|
+
* this and then fills both, and a caller makes keys with `aesKey`.
|
|
156
|
+
*
|
|
157
|
+
* @param rounds Nr: 10 for AES-128, 14 for AES-256.
|
|
158
|
+
* @param roundKeys The schedule array, `8 * (rounds + 1)` words; it is kept,
|
|
159
|
+
* not copied.
|
|
160
|
+
*/
|
|
143
161
|
constructor(rounds: i32, roundKeys: u64[]) {
|
|
144
162
|
this.rounds = rounds
|
|
145
163
|
this.roundKeys = roundKeys
|
|
@@ -154,6 +172,8 @@ export class AesKey {
|
|
|
154
172
|
* is about to be. `rounds` is not secret and stays. A wiped key seals under
|
|
155
173
|
* an all-zero schedule and, its H being zero, opens nothing (`aesGcmOpen`);
|
|
156
174
|
* `aesKeyInto` installs a new key into it.
|
|
175
|
+
*
|
|
176
|
+
* @param key The key to wipe; it stays usable as an `aesKeyInto` target.
|
|
157
177
|
*/
|
|
158
178
|
export const aesKeyWipe = (key: AesKey): void => {
|
|
159
179
|
wipe(key.roundKeys)
|
|
@@ -484,6 +504,10 @@ const aesAddRoundKey = (q: u64[], rk: u64[], at: i32): void => {
|
|
|
484
504
|
* `at`, which is a round number times eight. Exported so that
|
|
485
505
|
* `tests/link/crypto_aes` can hold the copy the disassembly check reads to
|
|
486
506
|
* this one; a caller has no other use for it.
|
|
507
|
+
*
|
|
508
|
+
* @param q The bitsliced state of four blocks, eight words, updated in place.
|
|
509
|
+
* @param rk The bitsliced round keys.
|
|
510
|
+
* @param at Where this round's eight words start in `rk`.
|
|
487
511
|
*/
|
|
488
512
|
export const aesBitslicedRound = (q: u64[], rk: u64[], at: i32): void => {
|
|
489
513
|
aesSbox(q)
|
|
@@ -700,6 +724,11 @@ const aesEncryptOne = (key: AesKey, src: u8[], dst: u8[]): void => {
|
|
|
700
724
|
* Makes an `AesKey` from 16 bytes (AES-128) or 32 (AES-256): the key
|
|
701
725
|
* schedule, bitsliced, and H = E(K, 0^128) for GCM. Any other length — 24
|
|
702
726
|
* bytes included, since AES-192 is not offered — answers `null`.
|
|
727
|
+
*
|
|
728
|
+
* @param key The raw key, 16 or 32 bytes; it is read, not kept, and the caller
|
|
729
|
+
* still wipes it.
|
|
730
|
+
* @returns A fresh key, which the caller hands to `aesKeyWipe` when done, or
|
|
731
|
+
* `null` for another length.
|
|
703
732
|
*/
|
|
704
733
|
export const aesKey = (key: u8[]): AesKey | null => {
|
|
705
734
|
const length: i32 = toI32(key.length)
|
|
@@ -737,6 +766,11 @@ export const aesKey = (key: u8[]): AesKey | null => {
|
|
|
737
766
|
*
|
|
738
767
|
* `aesKey` makes its key through this, so this is the one call of
|
|
739
768
|
* `aesScheduleInto`, and the checks its contract needs are made here.
|
|
769
|
+
*
|
|
770
|
+
* @param key The raw key, 16 or 32 bytes; it is read, not kept, and the caller
|
|
771
|
+
* still wipes it.
|
|
772
|
+
* @param out The key to overwrite, its `roundKeys` sized for this key length.
|
|
773
|
+
* @returns `true` if `out` now holds the key; `false` if it was left as it was.
|
|
740
774
|
*/
|
|
741
775
|
export const aesKeyInto = (key: u8[], out: AesKey): boolean => {
|
|
742
776
|
const length: i32 = toI32(key.length)
|
|
@@ -759,7 +793,15 @@ export const aesKeyInto = (key: u8[], out: AesKey): boolean => {
|
|
|
759
793
|
return true
|
|
760
794
|
}
|
|
761
795
|
|
|
762
|
-
/**
|
|
796
|
+
/**
|
|
797
|
+
* One block encrypted (FIPS 197 §5.1, ECB): 16 bytes in, 16 fresh bytes out, or
|
|
798
|
+
* `null` for another length.
|
|
799
|
+
*
|
|
800
|
+
* @param key The expanded key.
|
|
801
|
+
* @param block The 16 bytes to encrypt; it is read, not kept.
|
|
802
|
+
* @returns A fresh 16-byte block, or `null` for another length or a key
|
|
803
|
+
* `aesKey` would not make.
|
|
804
|
+
*/
|
|
763
805
|
export const aesEncryptBlock = (key: AesKey, block: u8[]): u8[] | null => {
|
|
764
806
|
if (!aesKeyUsable(key) || toI32(block.length) !== AES_BLOCK) {
|
|
765
807
|
return null
|
|
@@ -773,6 +815,11 @@ export const aesEncryptBlock = (key: AesKey, block: u8[]): u8[] | null => {
|
|
|
773
815
|
* The AES header-protection mask of RFC 9001 §5.4.3: the first five bytes of
|
|
774
816
|
* the 16-byte `sample` encrypted under the header-protection key. `null` for
|
|
775
817
|
* a sample of another length.
|
|
818
|
+
*
|
|
819
|
+
* @param key The header-protection key.
|
|
820
|
+
* @param sample The 16 bytes sampled from the packet's ciphertext.
|
|
821
|
+
* @returns A fresh 5-byte mask, or `null` for another sample length or a key
|
|
822
|
+
* `aesKey` would not make.
|
|
776
823
|
*/
|
|
777
824
|
export const aesHeaderMask = (key: AesKey, sample: u8[]): u8[] | null => {
|
|
778
825
|
if (!aesKeyUsable(key) || toI32(sample.length) !== AES_BLOCK) {
|
|
@@ -836,6 +883,10 @@ const ghashMul32 = (x: u64, y: u64): u64 => {
|
|
|
836
883
|
* The 128 × 128 product is Karatsuba twice over: three 64 × 64 products, each
|
|
837
884
|
* of three 32 × 32 ones from `ghashMul32`. Straight-line code; exported, like
|
|
838
885
|
* `aesBitslicedRound`, so the disassembly check's copy can be held to it.
|
|
886
|
+
*
|
|
887
|
+
* @param y The running GHASH value, two words, multiplied in place.
|
|
888
|
+
* @param hHi The high half of H.
|
|
889
|
+
* @param hLo The low half of H.
|
|
839
890
|
*/
|
|
840
891
|
export const ghashMultiply = (y: u64[], hHi: u64, hLo: u64): void => {
|
|
841
892
|
const low32: u64 = (toU64(1) << toU64(32)) - toU64(1)
|
|
@@ -1068,6 +1119,12 @@ const aesGcmTag = (y: u64[], key: AesKey, j0: u8[], aad: u8[], text: u8[], textL
|
|
|
1068
1119
|
* every bit of both tags is read and nothing is branched on until the caller
|
|
1069
1120
|
* tests the one answer. Exported, like `aesBitslicedRound`, so the
|
|
1070
1121
|
* disassembly check's copy can be held to it.
|
|
1122
|
+
*
|
|
1123
|
+
* @param tagHi The high half of the expected tag.
|
|
1124
|
+
* @param tagLo The low half of the expected tag.
|
|
1125
|
+
* @param gotHi The high half of the received tag.
|
|
1126
|
+
* @param gotLo The low half of the received tag.
|
|
1127
|
+
* @returns All-ones if the tags match, zero if they do not.
|
|
1071
1128
|
*/
|
|
1072
1129
|
export const aesGcmTagMask = (tagHi: u64, tagLo: u64, gotHi: u64, gotLo: u64): u64 =>
|
|
1073
1130
|
ctEq((tagHi ^ gotHi) | (tagLo ^ gotLo), toU64(0))
|
|
@@ -1078,6 +1135,13 @@ export const aesGcmTagMask = (tagHi: u64, tagLo: u64, gotHi: u64, gotLo: u64): u
|
|
|
1078
1135
|
* bytes is the fast and usual one. `null` for an empty IV, for a key
|
|
1079
1136
|
* `aesKey` did not make, and for a plaintext longer than 2^31 - 17 bytes,
|
|
1080
1137
|
* whose sealed form would not fit in an array.
|
|
1138
|
+
*
|
|
1139
|
+
* @param key The expanded key.
|
|
1140
|
+
* @param iv The nonce, any non-zero length; it must never repeat under one key.
|
|
1141
|
+
* @param aad Data authenticated but not encrypted; may be empty.
|
|
1142
|
+
* @param plaintext The message to encrypt; it is read, not kept.
|
|
1143
|
+
* @returns A fresh array of ciphertext then tag, `plaintext.length + 16` bytes,
|
|
1144
|
+
* or `null` for bad input.
|
|
1081
1145
|
*/
|
|
1082
1146
|
export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u8[] | null => {
|
|
1083
1147
|
const length: i32 = toI32(plaintext.length)
|
|
@@ -1105,6 +1169,13 @@ export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u
|
|
|
1105
1169
|
* the key is not one `aesKey` made.
|
|
1106
1170
|
* The tag is computed over the received ciphertext and compared in constant
|
|
1107
1171
|
* time before anything is decrypted, so a forgery yields no plaintext at all.
|
|
1172
|
+
*
|
|
1173
|
+
* @param key The expanded key.
|
|
1174
|
+
* @param iv The nonce the message was sealed with.
|
|
1175
|
+
* @param aad The data authenticated with it; must match what was sealed.
|
|
1176
|
+
* @param sealed The ciphertext followed by its 16-byte tag.
|
|
1177
|
+
* @returns A fresh array holding the plaintext, or `null` if the tag fails or
|
|
1178
|
+
* the input is bad.
|
|
1108
1179
|
*/
|
|
1109
1180
|
export const aesGcmOpen = (key: AesKey, iv: u8[], aad: u8[], sealed: u8[]): u8[] | null => {
|
|
1110
1181
|
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).
|
|
@@ -92,6 +97,10 @@ const base64urlSextetOf = (c: i32): i32 => {
|
|
|
92
97
|
* so one index walks the input and the only branches are on how many bits are
|
|
93
98
|
* waiting — which depends on the position, never on the bytes. An array longer
|
|
94
99
|
* than 2^31 - 1 bytes panics.
|
|
100
|
+
*
|
|
101
|
+
* @param data The bytes to encode; they are read, not changed.
|
|
102
|
+
* @returns The unpadded base64url text, `ceil(4n / 3)` characters for `n`
|
|
103
|
+
* bytes.
|
|
95
104
|
*/
|
|
96
105
|
export const base64urlEncode = (data: u8[]): string => {
|
|
97
106
|
// Refused before `toI32`, which saturates a longer length under
|
|
@@ -130,6 +139,10 @@ export const base64urlEncode = (data: u8[]): string => {
|
|
|
130
139
|
* Each group of four characters becomes one 24-bit word, six bits a
|
|
131
140
|
* character, which splits into three bytes; a short final group of two or
|
|
132
141
|
* three characters fills the top of the word and gives one or two.
|
|
142
|
+
*
|
|
143
|
+
* @param text The unpadded base64url text, usually from a peer.
|
|
144
|
+
* @returns A fresh array of the decoded bytes, or `null` when `text` is not the
|
|
145
|
+
* canonical spelling of any bytes.
|
|
133
146
|
*/
|
|
134
147
|
export const base64urlDecode = (text: string): u8[] | null => {
|
|
135
148
|
// A text past 2^31 - 1 bytes is refused before `toI32`, which would
|
|
@@ -238,6 +251,10 @@ const base64StdPadSextetOf = (c: i32): i32 => {
|
|
|
238
251
|
* `data` in padded standard base64, as one line: PEM's body, and SCRAM's
|
|
239
252
|
* nonces, salts and proofs (RFC 5802 §5.1). One `=` closes a final group of
|
|
240
253
|
* two bytes, two a group of one. An array longer than 2^31 - 1 bytes panics.
|
|
254
|
+
*
|
|
255
|
+
* @param data The bytes to encode; they are read, not changed.
|
|
256
|
+
* @returns The padded standard base64 text, `4 × ceil(n / 3)` characters for
|
|
257
|
+
* `n` bytes, with no line breaks.
|
|
241
258
|
*/
|
|
242
259
|
export const base64StdPadEncode = (data: u8[]): string => {
|
|
243
260
|
if (data.length > 2147483647) {
|
|
@@ -272,6 +289,11 @@ export const base64StdPadEncode = (data: u8[]): string => {
|
|
|
272
289
|
* The empty text is the spelling of no bytes (RFC 4648 §10). Where the padding
|
|
273
290
|
* is depends on the length alone; the characters themselves are read by masks
|
|
274
291
|
* and judged once, after the last.
|
|
292
|
+
*
|
|
293
|
+
* @param text The padded standard base64 text, with no line breaks or
|
|
294
|
+
* whitespace.
|
|
295
|
+
* @returns A fresh array of the decoded bytes, or `null` when `text` is not
|
|
296
|
+
* their canonical padded spelling.
|
|
275
297
|
*/
|
|
276
298
|
export const base64StdPadDecode = (text: string): u8[] | null => {
|
|
277
299
|
if (text.length > 2147483647) {
|
|
@@ -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) {
|
package/std/crypto/bcrypt.ts
CHANGED
|
@@ -829,6 +829,13 @@ const bcryptSameWiped = (rebuilt: u8[], hash: string): boolean => {
|
|
|
829
829
|
* 60 characters, `$2b$`, the two-digit cost, `$`, the salt and the digest in
|
|
830
830
|
* bcrypt's base64. The salt must come from a CSPRNG and be fresh for each
|
|
831
831
|
* hash. An `Err` for a salt of another length or a cost outside 4–31.
|
|
832
|
+
*
|
|
833
|
+
* @param password The password, read only inside `exposeWith`; the caller still
|
|
834
|
+
* owns and wipes it.
|
|
835
|
+
* @param salt 16 fresh random bytes.
|
|
836
|
+
* @param cost The log2 of the round count, 4 to 31.
|
|
837
|
+
* @returns The 60-character hash, which is public, or an `Err` naming the bad
|
|
838
|
+
* salt or cost.
|
|
832
839
|
*/
|
|
833
840
|
export const bcryptHash = (password: Secret<u8[]>, salt: u8[], cost: i32): Result<string, string> => {
|
|
834
841
|
if (salt.length !== 16) {
|
|
@@ -848,6 +855,13 @@ export const bcryptHash = (password: Secret<u8[]>, salt: u8[], cost: i32): Resul
|
|
|
848
855
|
* constant-time compare of all 60 characters with the hash rebuilt from the
|
|
849
856
|
* password and `hash`'s own prefix, salt and cost. `$2a$`, `$2b$` and `$2y$`
|
|
850
857
|
* are read; an `Err` names what makes `hash` not a bcrypt hash.
|
|
858
|
+
*
|
|
859
|
+
* @param password The password, read only inside `exposeWith`; the caller still
|
|
860
|
+
* owns and wipes it.
|
|
861
|
+
* @param hash A stored hash; its cost sets the work, so it must be one you
|
|
862
|
+
* stored.
|
|
863
|
+
* @returns `Ok(true)` only for this password's hash, `Ok(false)` for another's,
|
|
864
|
+
* or an `Err` for a malformed hash.
|
|
851
865
|
*/
|
|
852
866
|
export const bcryptVerify = (password: Secret<u8[]>, hash: string): Result<boolean, string> => {
|
|
853
867
|
const parsed: Result<BcryptParsed, string> = bcryptParseHash(hash)
|
|
@@ -1805,6 +1819,18 @@ const bcryptSlotsOf = (mask: i32): i32[] => {
|
|
|
1805
1819
|
* `bcryptHash`. Which w is fastest per hash depends on the core
|
|
1806
1820
|
* (`bcryptCalibrate` measures it). An `Err` for `salts` of another length
|
|
1807
1821
|
* than 96 or a cost outside 4–31.
|
|
1822
|
+
*
|
|
1823
|
+
* @param p0 Lane 0's password, always hashed; read only inside `exposeWith`,
|
|
1824
|
+
* and the caller still owns and wipes it.
|
|
1825
|
+
* @param p1 Lane 1's password, or `null` to skip the lane.
|
|
1826
|
+
* @param p2 Lane 2's password, or `null` to skip the lane.
|
|
1827
|
+
* @param p3 Lane 3's password, or `null` to skip the lane.
|
|
1828
|
+
* @param p4 Lane 4's password, or `null` to skip the lane.
|
|
1829
|
+
* @param p5 Lane 5's password, or `null` to skip the lane.
|
|
1830
|
+
* @param salts 96 fresh random bytes, lane k's salt at 16k.
|
|
1831
|
+
* @param cost The log2 of the round count, 4 to 31.
|
|
1832
|
+
* @returns Six hash strings, "" for a lane not given, or an `Err` for bad salts
|
|
1833
|
+
* or cost.
|
|
1808
1834
|
*/
|
|
1809
1835
|
export const bcryptHashN = (
|
|
1810
1836
|
p0: Secret<u8[]>,
|
|
@@ -1860,6 +1886,16 @@ export const bcryptHashN = (
|
|
|
1860
1886
|
* `bcryptHashN` for up to four passwords, `salts` 64 bytes: the same hashes,
|
|
1861
1887
|
* four strings. An `Err` for `salts` of another length than 64 or a cost
|
|
1862
1888
|
* outside 4–31.
|
|
1889
|
+
*
|
|
1890
|
+
* @param p0 Lane 0's password, always hashed; read only inside `exposeWith`,
|
|
1891
|
+
* and the caller still owns and wipes it.
|
|
1892
|
+
* @param p1 Lane 1's password, or `null` to skip the lane.
|
|
1893
|
+
* @param p2 Lane 2's password, or `null` to skip the lane.
|
|
1894
|
+
* @param p3 Lane 3's password, or `null` to skip the lane.
|
|
1895
|
+
* @param salts 64 fresh random bytes, lane k's salt at 16k.
|
|
1896
|
+
* @param cost The log2 of the round count, 4 to 31.
|
|
1897
|
+
* @returns Four hash strings, "" for a lane not given, or an `Err` for bad
|
|
1898
|
+
* salts or cost.
|
|
1863
1899
|
*/
|
|
1864
1900
|
export const bcryptHash4 = (
|
|
1865
1901
|
p0: Secret<u8[]>,
|
|
@@ -1928,6 +1964,18 @@ const bcryptJoin = (out: u8[], at: i32, prefix: u8[], digests: u8[], k: i32): vo
|
|
|
1928
1964
|
* runs as `bcryptVerify`. A malformed hash in any given lane makes the whole
|
|
1929
1965
|
* call an `Err` naming the lane ("bcrypt: lane 2: …") before any hashing, so
|
|
1930
1966
|
* one bad row in a batch cannot be read as a wrong password.
|
|
1967
|
+
*
|
|
1968
|
+
* @param p0 Lane 0's password, always hashed; read only inside `exposeWith`,
|
|
1969
|
+
* and the caller still owns and wipes it.
|
|
1970
|
+
* @param p1 Lane 1's password, or `null` to skip the lane.
|
|
1971
|
+
* @param p2 Lane 2's password, or `null` to skip the lane.
|
|
1972
|
+
* @param p3 Lane 3's password, or `null` to skip the lane.
|
|
1973
|
+
* @param p4 Lane 4's password, or `null` to skip the lane.
|
|
1974
|
+
* @param p5 Lane 5's password, or `null` to skip the lane.
|
|
1975
|
+
* @param hashes Six strings, lane k's stored hash at k; a skipped lane's is not
|
|
1976
|
+
* read.
|
|
1977
|
+
* @returns Six answers, `false` for a skipped lane, or an `Err` naming a
|
|
1978
|
+
* malformed lane or a count that is not six.
|
|
1931
1979
|
*/
|
|
1932
1980
|
export const bcryptVerifyN = (
|
|
1933
1981
|
p0: Secret<u8[]>,
|
|
@@ -1990,6 +2038,16 @@ export const bcryptVerifyN = (
|
|
|
1990
2038
|
/**
|
|
1991
2039
|
* `bcryptVerifyN` for up to four passwords and four hashes: the same
|
|
1992
2040
|
* answers, four of them, and the same lane errors.
|
|
2041
|
+
*
|
|
2042
|
+
* @param p0 Lane 0's password, always hashed; read only inside `exposeWith`,
|
|
2043
|
+
* and the caller still owns and wipes it.
|
|
2044
|
+
* @param p1 Lane 1's password, or `null` to skip the lane.
|
|
2045
|
+
* @param p2 Lane 2's password, or `null` to skip the lane.
|
|
2046
|
+
* @param p3 Lane 3's password, or `null` to skip the lane.
|
|
2047
|
+
* @param hashes Four strings, lane k's stored hash at k; a skipped lane's is
|
|
2048
|
+
* not read.
|
|
2049
|
+
* @returns Four answers, `false` for a skipped lane, or an `Err` naming a
|
|
2050
|
+
* malformed lane or a count that is not four.
|
|
1993
2051
|
*/
|
|
1994
2052
|
export const bcryptVerify4 = (
|
|
1995
2053
|
p0: Secret<u8[]>,
|
|
@@ -2167,6 +2225,8 @@ const bcryptCalibrateTime = (width: i32): i64 => {
|
|
|
2167
2225
|
* width turns on the core's load ports, its registers and what else is
|
|
2168
2226
|
* running, and it keeps nothing between calls, so call it once, at startup,
|
|
2169
2227
|
* and keep the answer. A few tens of milliseconds of one core.
|
|
2228
|
+
*
|
|
2229
|
+
* @returns How many passwords, 1 to 6, to give each batch call.
|
|
2170
2230
|
*/
|
|
2171
2231
|
export const bcryptCalibrate = (): i32 => {
|
|
2172
2232
|
let best: i32 = 1
|
|
@@ -310,6 +310,14 @@ const chacha20CounterFits = (counter: u32, len: i32): boolean => {
|
|
|
310
310
|
* word positions `a`, `b`, `c` and `d`, which is what §2.1.1 and §2.2.1 test.
|
|
311
311
|
* `chacha20Core` does the same on locals. Answers `null`
|
|
312
312
|
* when a position is not inside `state`.
|
|
313
|
+
*
|
|
314
|
+
* @param state The words to start from; it is read, not changed.
|
|
315
|
+
* @param a The first word position.
|
|
316
|
+
* @param b The second word position.
|
|
317
|
+
* @param c The third word position.
|
|
318
|
+
* @param d The fourth word position.
|
|
319
|
+
* @returns A fresh copy of `state` with the quarter round applied, or `null`
|
|
320
|
+
* for a position outside it.
|
|
313
321
|
*/
|
|
314
322
|
export const chacha20QuarterRound = (state: u32[], a: i32, b: i32, c: i32, d: i32): u32[] | null => {
|
|
315
323
|
const n: i32 = toI32(state.length)
|
|
@@ -335,6 +343,13 @@ export const chacha20QuarterRound = (state: u32[], a: i32, b: i32, c: i32, d: i3
|
|
|
335
343
|
* The ChaCha20 block function (RFC 8439 §2.3): the 64 bytes of keystream for
|
|
336
344
|
* block `counter` under `key` and `nonce`. Answers `null` unless `key` is
|
|
337
345
|
* `CHACHA20_KEY_SIZE` bytes and `nonce` is `CHACHA20POLY1305_NONCE_SIZE`.
|
|
346
|
+
*
|
|
347
|
+
* @param key The 32-byte key; it is read, not kept, and the caller still wipes
|
|
348
|
+
* it.
|
|
349
|
+
* @param counter The block number.
|
|
350
|
+
* @param nonce The 12-byte nonce.
|
|
351
|
+
* @returns 64 fresh bytes of keystream, secret like the key, or `null` for a
|
|
352
|
+
* bad key or nonce length.
|
|
338
353
|
*/
|
|
339
354
|
export const chacha20Block = (key: u8[], counter: u32, nonce: u8[]): u8[] | null => {
|
|
340
355
|
if (toI32(key.length) !== CHACHA20_KEY_SIZE || toI32(nonce.length) !== CHACHA20POLY1305_NONCE_SIZE) {
|
|
@@ -353,6 +368,13 @@ export const chacha20Block = (key: u8[], counter: u32, nonce: u8[]): u8[] | null
|
|
|
353
368
|
* `key` is `CHACHA20_KEY_SIZE` bytes and `nonce` is
|
|
354
369
|
* `CHACHA20POLY1305_NONCE_SIZE`, and `null` when `data` would run the 32-bit
|
|
355
370
|
* counter past its last value rather than repeat the keystream.
|
|
371
|
+
*
|
|
372
|
+
* @param key The 32-byte key; it is read, not kept, and the caller still wipes
|
|
373
|
+
* it.
|
|
374
|
+
* @param counter The block number the keystream starts at.
|
|
375
|
+
* @param nonce The 12-byte nonce.
|
|
376
|
+
* @param data The bytes to encrypt or decrypt; it is read, not changed.
|
|
377
|
+
* @returns A fresh array of `data.length` bytes, or `null` for bad input.
|
|
356
378
|
*/
|
|
357
379
|
export const chacha20 = (key: u8[], counter: u32, nonce: u8[], data: u8[]): u8[] | null => {
|
|
358
380
|
const len: i32 = toI32(data.length)
|
|
@@ -571,6 +593,11 @@ const poly1305Absorb = (h: u64[], r: u64[], buf: u8[], at: i32, len: i32, zeroPa
|
|
|
571
593
|
* `key`, `r` then `s`. A key must never authenticate two messages; the AEAD
|
|
572
594
|
* below makes a fresh one per nonce. Answers `null` unless `key` is
|
|
573
595
|
* `POLY1305_KEY_SIZE` bytes.
|
|
596
|
+
*
|
|
597
|
+
* @param key The 32-byte one-time key, `r` then `s`; it is read, not kept, and
|
|
598
|
+
* the caller still wipes it.
|
|
599
|
+
* @param msg The message to authenticate.
|
|
600
|
+
* @returns A fresh 16-byte tag, or `null` for a bad key length.
|
|
574
601
|
*/
|
|
575
602
|
export const poly1305 = (key: u8[], msg: u8[]): u8[] | null => {
|
|
576
603
|
if (toI32(key.length) !== POLY1305_KEY_SIZE) {
|
|
@@ -592,6 +619,12 @@ export const poly1305 = (key: u8[], msg: u8[]): u8[] | null => {
|
|
|
592
619
|
* ChaCha20 block with counter 0 under the AEAD's key and nonce. Answers
|
|
593
620
|
* `null` unless `key` is `CHACHA20_KEY_SIZE` bytes and `nonce` is
|
|
594
621
|
* `CHACHA20POLY1305_NONCE_SIZE`.
|
|
622
|
+
*
|
|
623
|
+
* @param key The 32-byte key; it is read, not kept, and the caller still wipes
|
|
624
|
+
* it.
|
|
625
|
+
* @param nonce The 12-byte nonce.
|
|
626
|
+
* @returns A fresh 32-byte one-time key, which the caller wipes, or `null` for
|
|
627
|
+
* a bad key or nonce length.
|
|
595
628
|
*/
|
|
596
629
|
export const poly1305KeyGen = (key: u8[], nonce: u8[]): u8[] | null => {
|
|
597
630
|
if (toI32(key.length) !== CHACHA20_KEY_SIZE || toI32(nonce.length) !== CHACHA20POLY1305_NONCE_SIZE) {
|
|
@@ -665,6 +698,14 @@ const chacha20Poly1305TagMatch = (tag: u8[], sealed: u8[], at: i32): u32 => {
|
|
|
665
698
|
* never be used twice with one key. Answers `null` unless `key` is
|
|
666
699
|
* `CHACHA20_KEY_SIZE` bytes and `nonce` is `CHACHA20POLY1305_NONCE_SIZE`, or
|
|
667
700
|
* when the sealed message would not fit in an array.
|
|
701
|
+
*
|
|
702
|
+
* @param key The 32-byte key; it is read, not kept, and the caller still wipes
|
|
703
|
+
* it.
|
|
704
|
+
* @param nonce The 12-byte nonce; it must never repeat under one key.
|
|
705
|
+
* @param aad Data authenticated but not encrypted; may be empty.
|
|
706
|
+
* @param plaintext The message to encrypt; it is read, not kept.
|
|
707
|
+
* @returns A fresh array of ciphertext then tag, `plaintext.length + 16` bytes,
|
|
708
|
+
* or `null` for bad input.
|
|
668
709
|
*/
|
|
669
710
|
export const chacha20Poly1305Seal = (key: u8[], nonce: u8[], aad: u8[], plaintext: u8[]): u8[] | null => {
|
|
670
711
|
const len: i32 = toI32(plaintext.length)
|
|
@@ -700,6 +741,14 @@ export const chacha20Poly1305Seal = (key: u8[], nonce: u8[], aad: u8[], plaintex
|
|
|
700
741
|
* time before a byte is decrypted, so a forgery gets `null` and nothing else.
|
|
701
742
|
* Answers `null` too unless `key` is `CHACHA20_KEY_SIZE` bytes and `nonce` is
|
|
702
743
|
* `CHACHA20POLY1305_NONCE_SIZE`, and when `sealed` is shorter than a tag.
|
|
744
|
+
*
|
|
745
|
+
* @param key The 32-byte key; it is read, not kept, and the caller still wipes
|
|
746
|
+
* it.
|
|
747
|
+
* @param nonce The 12-byte nonce the message was sealed with.
|
|
748
|
+
* @param aad The data authenticated with it; must match what was sealed.
|
|
749
|
+
* @param sealed The ciphertext followed by its 16-byte tag.
|
|
750
|
+
* @returns A fresh array holding the plaintext, or `null` if the tag fails or
|
|
751
|
+
* the input is bad.
|
|
703
752
|
*/
|
|
704
753
|
export const chacha20Poly1305Open = (key: u8[], nonce: u8[], aad: u8[], sealed: u8[]): u8[] | null => {
|
|
705
754
|
const total: i32 = toI32(sealed.length)
|
|
@@ -740,6 +789,10 @@ export const chacha20Poly1305Open = (key: u8[], nonce: u8[], aad: u8[], sealed:
|
|
|
740
789
|
* `sample` (little-endian) and whose nonce is the other twelve, under the
|
|
741
790
|
* header-protection key `hpKey`. Answers `null` unless `hpKey` is
|
|
742
791
|
* `CHACHA20_KEY_SIZE` bytes and `sample` is `CHACHA20_HEADER_SAMPLE_SIZE`.
|
|
792
|
+
*
|
|
793
|
+
* @param hpKey The 32-byte header-protection key; it is read, not kept.
|
|
794
|
+
* @param sample The 16 bytes sampled from the packet's ciphertext.
|
|
795
|
+
* @returns A fresh 5-byte mask, or `null` for a bad key or sample length.
|
|
743
796
|
*/
|
|
744
797
|
export const chacha20HeaderMask = (hpKey: u8[], sample: u8[]): u8[] | null => {
|
|
745
798
|
if (toI32(hpKey.length) !== CHACHA20_KEY_SIZE || toI32(sample.length) !== CHACHA20_HEADER_SAMPLE_SIZE) {
|