@amritk/nish-x86_64-linux 0.44.0 → 0.46.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 +3 -1
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1315 -222
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +171 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/crypto/hmac.ts
CHANGED
|
@@ -19,9 +19,11 @@
|
|
|
19
19
|
* mac.update(record, off, len);
|
|
20
20
|
* const tag2: u8[] = mac.digest();
|
|
21
21
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* A `digest` ends the computation: a later `update` or a second `digest`
|
|
23
|
+
* panics, and so does a window outside its buffer. For SHA-384 and SHA-512
|
|
24
|
+
* the inner hasher panics. For SHA-256 the inner `Sha256` answers a second
|
|
25
|
+
* digest with the digest of its wiped, all-zero state, so the panic comes
|
|
26
|
+
* from the outer hasher's `update`, before anything is written. The only branch on the key is on its length,
|
|
25
27
|
* which HMAC does not keep secret; a received tag is compared with
|
|
26
28
|
* `timingSafeEqual`, never `===`.
|
|
27
29
|
*
|
|
@@ -85,9 +87,11 @@ const hmacPadInto = (out: u8[], key: u8[], off: i32, len: i32, pad: i32): void =
|
|
|
85
87
|
const keyLength: i32 = toI32(key.length)
|
|
86
88
|
// Bounded by both lengths, so both indices are proved in range.
|
|
87
89
|
let i: i32 = 0
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
let p: i32 = off
|
|
91
|
+
while (i < len && i < outLength && p >= 0 && p < keyLength) {
|
|
92
|
+
out[i] = key[p] ^ padByte
|
|
90
93
|
i += 1
|
|
94
|
+
p += 1
|
|
91
95
|
}
|
|
92
96
|
while (i < outLength) {
|
|
93
97
|
out[i] = padByte
|
|
@@ -95,16 +99,6 @@ const hmacPadInto = (out: u8[], key: u8[], off: i32, len: i32, pad: i32): void =
|
|
|
95
99
|
}
|
|
96
100
|
}
|
|
97
101
|
|
|
98
|
-
/**
|
|
99
|
-
* `key XOR pad` over a whole block of `blockSize` bytes, in a fresh array, with
|
|
100
|
-
* `key` already no longer than a block (`hmacPadInto`).
|
|
101
|
-
*/
|
|
102
|
-
const hmacPadBlock = (key: u8[], blockSize: i32, pad: i32): u8[] => {
|
|
103
|
-
const out: u8[] = new Array<u8>(blockSize)
|
|
104
|
-
hmacPadInto(out, key, HMAC_FROM, toI32(key.length), pad)
|
|
105
|
-
return out
|
|
106
|
-
}
|
|
107
|
-
|
|
108
102
|
/**
|
|
109
103
|
* Zeroes everything `h` holds of the key and the message it was fed: its hash
|
|
110
104
|
* value, its pending block and its message schedule, with `wipe`'s volatile
|
|
@@ -141,20 +135,9 @@ const hmacWipeSha512 = (h: Sha512): void => {
|
|
|
141
135
|
h.engine.finished = true
|
|
142
136
|
}
|
|
143
137
|
|
|
144
|
-
/**
|
|
145
|
-
* Panics on a key longer than 2^31 - 1 bytes, before any `toI32` of its
|
|
146
|
-
* length: under `--number-mode f64` that would saturate, and the key hashed
|
|
147
|
-
* would be a prefix, so two keys sharing it would MAC alike (K1-1).
|
|
148
|
-
*/
|
|
149
|
-
const hmacCheckKey = (key: u8[]): void => {
|
|
150
|
-
if (key.length > 2147483647) {
|
|
151
|
-
panic("hmac: a key longer than 2^31 - 1 bytes")
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
138
|
/** Whether `[off, off + len)` is a window inside an array of `length` elements. */
|
|
156
139
|
const hmacWindowFits = (length: i32, off: i32, len: i32): boolean =>
|
|
157
|
-
off >= 0 && len >= 0 && off <= length - len
|
|
140
|
+
off >= 0 && len >= 0 && length >= 0 && off <= length - len
|
|
158
141
|
|
|
159
142
|
/**
|
|
160
143
|
* Keys `inner` with `k XOR ipad` and `outer` with `k XOR opad`, `k` no longer
|
|
@@ -162,7 +145,8 @@ const hmacWindowFits = (length: i32, off: i32, len: i32): boolean =>
|
|
|
162
145
|
* wiped once both have absorbed it.
|
|
163
146
|
*/
|
|
164
147
|
const hmacPadsSha256 = (inner: Sha256, outer: Sha256, k: u8[]): void => {
|
|
165
|
-
const pad: u8[] =
|
|
148
|
+
const pad: u8[] = new Array<u8>(SHA256_BLOCK)
|
|
149
|
+
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_IPAD)
|
|
166
150
|
inner.update(pad, HMAC_FROM, SHA256_BLOCK)
|
|
167
151
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_OPAD)
|
|
168
152
|
outer.update(pad, HMAC_FROM, SHA256_BLOCK)
|
|
@@ -175,13 +159,12 @@ const hmacPadsSha256 = (inner: Sha256, outer: Sha256, k: u8[]): void => {
|
|
|
175
159
|
* and that hasher are wiped once the key blocks are absorbed.
|
|
176
160
|
*/
|
|
177
161
|
const hmacKeySha256 = (inner: Sha256, outer: Sha256, key: u8[]): void => {
|
|
178
|
-
hmacCheckKey(key)
|
|
179
162
|
if (toI32(key.length) <= SHA256_BLOCK) {
|
|
180
163
|
hmacPadsSha256(inner, outer, key)
|
|
181
164
|
return
|
|
182
165
|
}
|
|
183
166
|
const hasher = new Sha256()
|
|
184
|
-
hasher.
|
|
167
|
+
hasher.updateAll(key)
|
|
185
168
|
const hashed: u8[] = hasher.digest()
|
|
186
169
|
hmacWipeSha256(hasher)
|
|
187
170
|
hmacPadsSha256(inner, outer, hashed)
|
|
@@ -190,7 +173,8 @@ const hmacKeySha256 = (inner: Sha256, outer: Sha256, key: u8[]): void => {
|
|
|
190
173
|
|
|
191
174
|
/** `hmacPadsSha256` for SHA-384's 128-byte block. */
|
|
192
175
|
const hmacPadsSha384 = (inner: Sha384, outer: Sha384, k: u8[]): void => {
|
|
193
|
-
const pad: u8[] =
|
|
176
|
+
const pad: u8[] = new Array<u8>(SHA512_BLOCK)
|
|
177
|
+
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_IPAD)
|
|
194
178
|
inner.update(pad, HMAC_FROM, SHA512_BLOCK)
|
|
195
179
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_OPAD)
|
|
196
180
|
outer.update(pad, HMAC_FROM, SHA512_BLOCK)
|
|
@@ -203,13 +187,12 @@ const hmacPadsSha384 = (inner: Sha384, outer: Sha384, k: u8[]): void => {
|
|
|
203
187
|
* and that hasher are wiped once the key blocks are absorbed.
|
|
204
188
|
*/
|
|
205
189
|
const hmacKeySha384 = (inner: Sha384, outer: Sha384, key: u8[]): void => {
|
|
206
|
-
hmacCheckKey(key)
|
|
207
190
|
if (toI32(key.length) <= SHA512_BLOCK) {
|
|
208
191
|
hmacPadsSha384(inner, outer, key)
|
|
209
192
|
return
|
|
210
193
|
}
|
|
211
194
|
const hasher = new Sha384()
|
|
212
|
-
hasher.
|
|
195
|
+
hasher.engine.absorbAll(key)
|
|
213
196
|
const hashed: u8[] = hasher.digest()
|
|
214
197
|
hmacWipeSha384(hasher)
|
|
215
198
|
hmacPadsSha384(inner, outer, hashed)
|
|
@@ -218,7 +201,8 @@ const hmacKeySha384 = (inner: Sha384, outer: Sha384, key: u8[]): void => {
|
|
|
218
201
|
|
|
219
202
|
/** `hmacPadsSha256` for SHA-512's 128-byte block. */
|
|
220
203
|
const hmacPadsSha512 = (inner: Sha512, outer: Sha512, k: u8[]): void => {
|
|
221
|
-
const pad: u8[] =
|
|
204
|
+
const pad: u8[] = new Array<u8>(SHA512_BLOCK)
|
|
205
|
+
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_IPAD)
|
|
222
206
|
inner.update(pad, HMAC_FROM, SHA512_BLOCK)
|
|
223
207
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_OPAD)
|
|
224
208
|
outer.update(pad, HMAC_FROM, SHA512_BLOCK)
|
|
@@ -231,13 +215,12 @@ const hmacPadsSha512 = (inner: Sha512, outer: Sha512, k: u8[]): void => {
|
|
|
231
215
|
* and that hasher are wiped once the key blocks are absorbed.
|
|
232
216
|
*/
|
|
233
217
|
const hmacKeySha512 = (inner: Sha512, outer: Sha512, key: u8[]): void => {
|
|
234
|
-
hmacCheckKey(key)
|
|
235
218
|
if (toI32(key.length) <= SHA512_BLOCK) {
|
|
236
219
|
hmacPadsSha512(inner, outer, key)
|
|
237
220
|
return
|
|
238
221
|
}
|
|
239
222
|
const hasher = new Sha512()
|
|
240
|
-
hasher.
|
|
223
|
+
hasher.engine.absorbAll(key)
|
|
241
224
|
const hashed: u8[] = hasher.digest()
|
|
242
225
|
hmacWipeSha512(hasher)
|
|
243
226
|
hmacPadsSha512(inner, outer, hashed)
|
|
@@ -305,7 +288,7 @@ export class HmacSha256 {
|
|
|
305
288
|
* Writes the 32-byte tag at `out[at .. at + 32)`, ends the computation and
|
|
306
289
|
* wipes both hashers and the inner digest, with nothing allocated. A window
|
|
307
290
|
* outside `out` panics before anything is written; so does a second
|
|
308
|
-
* `digest` or `digestInto`, in the
|
|
291
|
+
* `digest` or `digestInto`, in the outer hasher's `update`.
|
|
309
292
|
*
|
|
310
293
|
* @param out The caller's array the tag is written into.
|
|
311
294
|
* @param at Where the 32 tag bytes start in `out`.
|
|
@@ -428,55 +411,46 @@ export class HmacSha512 {
|
|
|
428
411
|
/**
|
|
429
412
|
* The HMAC-SHA-256 of all of `data` under `key`, as a fresh 32-byte array.
|
|
430
413
|
*
|
|
431
|
-
* `data` longer than 2^31 - 1 bytes
|
|
432
|
-
*
|
|
433
|
-
*
|
|
434
|
-
*
|
|
414
|
+
* `data` longer than 2^31 - 1 bytes (possible under `--number-mode f64`) is
|
|
415
|
+
* MACed whole, as `sha256` hashes it, never cut to a prefix: a tag that
|
|
416
|
+
* verified every message sharing a prefix would be a forgery. A key that long
|
|
417
|
+
* is hashed whole in the constructor for the same reason.
|
|
435
418
|
*
|
|
436
419
|
* @param key The secret key; it is read, not kept.
|
|
437
420
|
* @param data The whole message.
|
|
438
421
|
* @returns A fresh 32-byte tag, which the caller owns.
|
|
439
422
|
*/
|
|
440
423
|
export const hmacSha256 = (key: u8[], data: u8[]): u8[] => {
|
|
441
|
-
if (data.length > 2147483647) {
|
|
442
|
-
panic("hmacSha256: a message longer than 2^31 - 1 bytes")
|
|
443
|
-
}
|
|
444
424
|
const mac = new HmacSha256(key)
|
|
445
|
-
mac.
|
|
425
|
+
mac.inner.updateAll(data)
|
|
446
426
|
return mac.digest()
|
|
447
427
|
}
|
|
448
428
|
|
|
449
429
|
/**
|
|
450
430
|
* The HMAC-SHA-384 of all of `data` under `key`, as a fresh 48-byte array.
|
|
451
|
-
* `data` longer than 2^31 - 1 bytes
|
|
431
|
+
* `data` longer than 2^31 - 1 bytes is MACed whole, as in `hmacSha256`.
|
|
452
432
|
*
|
|
453
433
|
* @param key The secret key; it is read, not kept.
|
|
454
434
|
* @param data The whole message.
|
|
455
435
|
* @returns A fresh 48-byte tag, which the caller owns.
|
|
456
436
|
*/
|
|
457
437
|
export const hmacSha384 = (key: u8[], data: u8[]): u8[] => {
|
|
458
|
-
if (data.length > 2147483647) {
|
|
459
|
-
panic("hmacSha384: a message longer than 2^31 - 1 bytes")
|
|
460
|
-
}
|
|
461
438
|
const mac = new HmacSha384(key)
|
|
462
|
-
mac.
|
|
439
|
+
mac.inner.engine.absorbAll(data)
|
|
463
440
|
return mac.digest()
|
|
464
441
|
}
|
|
465
442
|
|
|
466
443
|
/**
|
|
467
444
|
* The HMAC-SHA-512 of all of `data` under `key`, as a fresh 64-byte array.
|
|
468
|
-
* `data` longer than 2^31 - 1 bytes
|
|
445
|
+
* `data` longer than 2^31 - 1 bytes is MACed whole, as in `hmacSha256`.
|
|
469
446
|
*
|
|
470
447
|
* @param key The secret key; it is read, not kept.
|
|
471
448
|
* @param data The whole message.
|
|
472
449
|
* @returns A fresh 64-byte tag, which the caller owns.
|
|
473
450
|
*/
|
|
474
451
|
export const hmacSha512 = (key: u8[], data: u8[]): u8[] => {
|
|
475
|
-
if (data.length > 2147483647) {
|
|
476
|
-
panic("hmacSha512: a message longer than 2^31 - 1 bytes")
|
|
477
|
-
}
|
|
478
452
|
const mac = new HmacSha512(key)
|
|
479
|
-
mac.
|
|
453
|
+
mac.inner.engine.absorbAll(data)
|
|
480
454
|
return mac.digest()
|
|
481
455
|
}
|
|
482
456
|
|
|
@@ -724,8 +698,14 @@ export class HmacSha256Scratch {
|
|
|
724
698
|
this.outer.update(innerDigest, HMAC_FROM, SHA256_SIZE)
|
|
725
699
|
secureZero(innerDigest)
|
|
726
700
|
const tag: u8[] = this.outer.digest()
|
|
727
|
-
|
|
728
|
-
|
|
701
|
+
let p: i32 = at
|
|
702
|
+
for (
|
|
703
|
+
let i: i32 = 0;
|
|
704
|
+
i < SHA256_SIZE && i < toI32(tag.length) && p >= 0 && p < toI32(out.length);
|
|
705
|
+
i += 1
|
|
706
|
+
) {
|
|
707
|
+
out[p] = tag[i]
|
|
708
|
+
p += 1
|
|
729
709
|
}
|
|
730
710
|
secureZero(tag)
|
|
731
711
|
}
|
|
@@ -839,8 +819,14 @@ export class HmacSha384Scratch {
|
|
|
839
819
|
this.outer.update(innerDigest, HMAC_FROM, SHA384_SIZE)
|
|
840
820
|
secureZero(innerDigest)
|
|
841
821
|
const tag: u8[] = this.outer.digest()
|
|
842
|
-
|
|
843
|
-
|
|
822
|
+
let p: i32 = at
|
|
823
|
+
for (
|
|
824
|
+
let i: i32 = 0;
|
|
825
|
+
i < SHA384_SIZE && i < toI32(tag.length) && p >= 0 && p < toI32(out.length);
|
|
826
|
+
i += 1
|
|
827
|
+
) {
|
|
828
|
+
out[p] = tag[i]
|
|
829
|
+
p += 1
|
|
844
830
|
}
|
|
845
831
|
secureZero(tag)
|
|
846
832
|
}
|
|
@@ -912,14 +898,15 @@ const pbkdf2HmacSha256Exposed = (password: u8[], request: Pbkdf2Request): u8[] |
|
|
|
912
898
|
const dk: u8[] = new Array<u8>(length)
|
|
913
899
|
// dkLen is an i32, so it never reaches RFC 8018's (2^32 - 1) * hLen bound,
|
|
914
900
|
// and the block index below never passes 2^26.
|
|
915
|
-
let block:
|
|
901
|
+
let block: u32 = 1
|
|
916
902
|
let done: i32 = 0
|
|
917
|
-
while (done < length) {
|
|
903
|
+
while (done >= 0 && done < length) {
|
|
918
904
|
// U_1 = PRF(P, S || INT(i)), INT(i) four bytes, most significant first.
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
905
|
+
let shift: u32 = 24
|
|
906
|
+
for (let b: i32 = 0; b < PBKDF2_INDEX && b < toI32(index.length); b += 1) {
|
|
907
|
+
index[b] = toU8(block >>> shift)
|
|
908
|
+
shift -= 8
|
|
909
|
+
}
|
|
923
910
|
hmacCopySha256(keyedInner, work)
|
|
924
911
|
work.update(salt, HMAC_FROM, toI32(salt.length))
|
|
925
912
|
work.update(index, HMAC_FROM, PBKDF2_INDEX)
|
|
@@ -936,11 +923,15 @@ const pbkdf2HmacSha256Exposed = (password: u8[], request: Pbkdf2Request): u8[] |
|
|
|
936
923
|
t[j] = t[j] ^ u[j]
|
|
937
924
|
}
|
|
938
925
|
}
|
|
939
|
-
|
|
940
|
-
for (
|
|
941
|
-
|
|
926
|
+
// T_i fills the next 32 bytes of DK, or what is left of it.
|
|
927
|
+
for (
|
|
928
|
+
let j: i32 = 0;
|
|
929
|
+
j < SHA256_SIZE && j < toI32(t.length) && done >= 0 && done < toI32(dk.length);
|
|
930
|
+
j += 1
|
|
931
|
+
) {
|
|
932
|
+
dk[done] = t[j]
|
|
933
|
+
done += 1
|
|
942
934
|
}
|
|
943
|
-
done += take
|
|
944
935
|
block += 1
|
|
945
936
|
}
|
|
946
937
|
wipe(u)
|
|
@@ -1021,6 +1012,9 @@ export const HMAC_SHA512: i32 = 3
|
|
|
1021
1012
|
/** Words of one chaining value: eight, for SHA-256's `u32` and SHA-512's `u64` alike. */
|
|
1022
1013
|
const HMAC_WORDS: i32 = 8
|
|
1023
1014
|
|
|
1015
|
+
/** Both midstates, inner then outer: twice `HMAC_WORDS`. */
|
|
1016
|
+
const HMAC_MIDSTATE_WORDS: i32 = 16
|
|
1017
|
+
|
|
1024
1018
|
/** The tag length of `hash` in bytes, 0 for no hash `HmacKey` knows. */
|
|
1025
1019
|
const hmacTagSize = (hash: i32): i32 => {
|
|
1026
1020
|
if (hash === HMAC_SHA256) {
|
|
@@ -1042,21 +1036,23 @@ const hmacTagSize = (hash: i32): i32 => {
|
|
|
1042
1036
|
* wiped before it answers.
|
|
1043
1037
|
*/
|
|
1044
1038
|
const hmacMidstates = (key: u8[], hash: i32): u64[] => {
|
|
1045
|
-
const out: u64[] = new Array<u64>(
|
|
1039
|
+
const out: u64[] = new Array<u64>(HMAC_MIDSTATE_WORDS)
|
|
1046
1040
|
if (hash === HMAC_SHA256) {
|
|
1047
1041
|
const inner = new Sha256()
|
|
1048
1042
|
const outer = new Sha256()
|
|
1049
1043
|
hmacKeySha256(inner, outer, key)
|
|
1044
|
+
const from: u32[] = inner.state
|
|
1045
|
+
const to: u32[] = outer.state
|
|
1046
|
+
const size: i32 = toI32(out.length)
|
|
1047
|
+
let j: i32 = HMAC_WORDS
|
|
1050
1048
|
for (
|
|
1051
1049
|
let i: i32 = 0;
|
|
1052
|
-
i < HMAC_WORDS && i < toI32(
|
|
1050
|
+
i < HMAC_WORDS && i < toI32(from.length) && i < toI32(to.length) && i < size && j >= 0 && j < size;
|
|
1053
1051
|
i += 1
|
|
1054
1052
|
) {
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
out[j] = toU64(outer.state[i])
|
|
1059
|
-
}
|
|
1053
|
+
out[i] = toU64(from[i])
|
|
1054
|
+
out[j] = toU64(to[i])
|
|
1055
|
+
j += 1
|
|
1060
1056
|
}
|
|
1061
1057
|
hmacWipeSha256(inner)
|
|
1062
1058
|
hmacWipeSha256(outer)
|
|
@@ -1080,12 +1076,16 @@ const hmacMidstates = (key: u8[], hash: i32): u64[] => {
|
|
|
1080
1076
|
|
|
1081
1077
|
/** `out[0 .. 8) = inner`, `out[8 .. 16) = outer`, for the SHA-512 family. */
|
|
1082
1078
|
const hmacMidstates512 = (out: u64[], inner: u64[], outer: u64[]): void => {
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1079
|
+
const size: i32 = toI32(out.length)
|
|
1080
|
+
let j: i32 = HMAC_WORDS
|
|
1081
|
+
for (
|
|
1082
|
+
let i: i32 = 0;
|
|
1083
|
+
i < HMAC_WORDS && i < toI32(inner.length) && i < toI32(outer.length) && i < size && j >= 0 && j < size;
|
|
1084
|
+
i += 1
|
|
1085
|
+
) {
|
|
1086
|
+
out[i] = inner[i]
|
|
1087
|
+
out[j] = outer[i]
|
|
1088
|
+
j += 1
|
|
1089
1089
|
}
|
|
1090
1090
|
}
|
|
1091
1091
|
|
|
@@ -1196,7 +1196,7 @@ export class HmacKey {
|
|
|
1196
1196
|
|
|
1197
1197
|
/** A plain copy of the 16 midstate words, for `expose` to run; the caller wipes it. */
|
|
1198
1198
|
const hmacCopyWords = (pads: u64[]): u64[] => {
|
|
1199
|
-
const out: u64[] = new Array<u64>(
|
|
1199
|
+
const out: u64[] = new Array<u64>(HMAC_MIDSTATE_WORDS)
|
|
1200
1200
|
for (let i: i32 = 0; i < toI32(out.length) && i < toI32(pads.length); i += 1) {
|
|
1201
1201
|
out[i] = pads[i]
|
|
1202
1202
|
}
|
|
@@ -1207,8 +1207,10 @@ const hmacCopyWords = (pads: u64[]): u64[] => {
|
|
|
1207
1207
|
const hmacResume256 = (h: Sha256, mid: u64[], at: i32): void => {
|
|
1208
1208
|
hmacWipeSha256(h)
|
|
1209
1209
|
const state: u32[] = h.state
|
|
1210
|
-
|
|
1211
|
-
|
|
1210
|
+
let p: i32 = at
|
|
1211
|
+
for (let i: i32 = 0; i < HMAC_WORDS && i < toI32(state.length) && p >= 0 && p < toI32(mid.length); i += 1) {
|
|
1212
|
+
state[i] = toU32(mid[p])
|
|
1213
|
+
p += 1
|
|
1212
1214
|
}
|
|
1213
1215
|
h.total = toI64(SHA256_BLOCK)
|
|
1214
1216
|
h.finished = false
|
|
@@ -1218,8 +1220,10 @@ const hmacResume256 = (h: Sha256, mid: u64[], at: i32): void => {
|
|
|
1218
1220
|
const hmacResume512 = (h: Sha512, mid: u64[], at: i32): void => {
|
|
1219
1221
|
hmacWipeSha512(h)
|
|
1220
1222
|
const state: u64[] = h.engine.state
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
+
let p: i32 = at
|
|
1224
|
+
for (let i: i32 = 0; i < HMAC_WORDS && i < toI32(state.length) && p >= 0 && p < toI32(mid.length); i += 1) {
|
|
1225
|
+
state[i] = mid[p]
|
|
1226
|
+
p += 1
|
|
1223
1227
|
}
|
|
1224
1228
|
h.engine.count = toU64(SHA512_BLOCK)
|
|
1225
1229
|
h.engine.finished = false
|
|
@@ -1290,8 +1294,10 @@ export const hmacKeyedInto = (key: HmacKey, data: u8[], off: i32, len: i32, out:
|
|
|
1290
1294
|
}
|
|
1291
1295
|
using _scope = arena()
|
|
1292
1296
|
const tag: u8[] = hmacKeyedTag(key, data, off, len)
|
|
1293
|
-
|
|
1294
|
-
|
|
1297
|
+
let p: i32 = at
|
|
1298
|
+
for (let i: i32 = 0; i < size && i < toI32(tag.length) && p >= 0 && p < toI32(out.length); i += 1) {
|
|
1299
|
+
out[p] = tag[i]
|
|
1300
|
+
p += 1
|
|
1295
1301
|
}
|
|
1296
1302
|
wipe(tag)
|
|
1297
1303
|
}
|