@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/compress/zlib.ts
CHANGED
|
@@ -83,7 +83,12 @@ export class ZlibCompressor {
|
|
|
83
83
|
compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
|
|
84
84
|
zlibWindow("ZlibCompressor.compress", src, off, len)
|
|
85
85
|
zlibWindow("ZlibCompressor.compress", out, at, 0)
|
|
86
|
-
|
|
86
|
+
// The header's two bytes, and room for at least the trailer's four
|
|
87
|
+
// after them. Positions are added in i64: the window was checked in
|
|
88
|
+
// `zlibWindow`, but the prover does not carry that across the call.
|
|
89
|
+
const n: i32 = toI32(out.length)
|
|
90
|
+
const second: i32 = toI32(toI64(at) + 1)
|
|
91
|
+
if (at < 0 || at >= n || second < 0 || second >= n || toI64(n) - toI64(at) < 6) {
|
|
87
92
|
return DEFLATE_NO_ROOM
|
|
88
93
|
}
|
|
89
94
|
const level: i32 = this.deflater.level
|
|
@@ -97,20 +102,32 @@ export class ZlibCompressor {
|
|
|
97
102
|
flevel = 2
|
|
98
103
|
}
|
|
99
104
|
const cmf: i32 = 0x78
|
|
100
|
-
|
|
101
|
-
|
|
105
|
+
// FLEVEL is two bits, so the mask changes nothing and bounds `flg`;
|
|
106
|
+
// FCHECK makes `cmf * 256 + flg` (0x7800 + flg) a multiple of 31.
|
|
107
|
+
let flg: i32 = (flevel & 3) * 64
|
|
108
|
+
flg += (toI32(31) - ((0x7800 + flg) % 31)) % 31
|
|
102
109
|
out[at] = toU8(cmf)
|
|
103
|
-
out[
|
|
104
|
-
const end: i32 = this.deflater.compress(src, off, len, out, at + 2)
|
|
105
|
-
|
|
110
|
+
out[second] = toU8(flg)
|
|
111
|
+
const end: i32 = this.deflater.compress(src, off, len, out, toI32(toI64(at) + 2))
|
|
112
|
+
// A trailer that cannot fit is refused before the input is checksummed.
|
|
113
|
+
if (end < 0 || toI64(end) + 4 > toI64(out.length)) {
|
|
106
114
|
return DEFLATE_NO_ROOM
|
|
107
115
|
}
|
|
116
|
+
// `out`'s length is read again after the checksum for the trailer's
|
|
117
|
+
// stores: the prover forgets an array's length across a call.
|
|
108
118
|
const a: u32 = adler32(src, off, len)
|
|
119
|
+
const e1: i32 = toI32(toI64(end) + 1)
|
|
120
|
+
const e2: i32 = toI32(toI64(end) + 2)
|
|
121
|
+
const e3: i32 = toI32(toI64(end) + 3)
|
|
122
|
+
const m: i32 = toI32(out.length)
|
|
123
|
+
if (end < 0 || end >= m || e1 < 0 || e1 >= m || e2 < 0 || e2 >= m || e3 < 0 || e3 >= m) {
|
|
124
|
+
return DEFLATE_NO_ROOM
|
|
125
|
+
}
|
|
109
126
|
out[end] = toU8(a >>> 24)
|
|
110
|
-
out[
|
|
111
|
-
out[
|
|
112
|
-
out[
|
|
113
|
-
return end + 4
|
|
127
|
+
out[e1] = toU8((a >>> 16) & 255)
|
|
128
|
+
out[e2] = toU8((a >>> 8) & 255)
|
|
129
|
+
out[e3] = toU8(a & 255)
|
|
130
|
+
return toI32(toI64(end) + 4)
|
|
114
131
|
}
|
|
115
132
|
}
|
|
116
133
|
|
|
@@ -152,23 +169,30 @@ export class ZlibDecompressor {
|
|
|
152
169
|
if (limit < 0) {
|
|
153
170
|
panic("ZlibDecompressor.decompress: a negative limit")
|
|
154
171
|
}
|
|
155
|
-
|
|
172
|
+
// The window was checked in `zlibWindow`; the prover does not carry
|
|
173
|
+
// that across the call, so the header's bytes are checked again here
|
|
174
|
+
// and positions are added in i64.
|
|
175
|
+
const n: i32 = toI32(src.length)
|
|
176
|
+
const second: i32 = toI32(toI64(off) + 1)
|
|
177
|
+
if (len < 2 || off < 0 || off >= n || second < 0 || second >= n) {
|
|
156
178
|
return DEFLATE_ERR_TRUNCATED
|
|
157
179
|
}
|
|
158
180
|
const cmf: i32 = toI32(src[off])
|
|
159
|
-
const flg: i32 = toI32(src[
|
|
181
|
+
const flg: i32 = toI32(src[second])
|
|
160
182
|
if ((cmf & 15) !== 8 || cmf >> 4 > 7 || (cmf * 256 + flg) % 31 !== 0) {
|
|
161
183
|
return ZLIB_ERR_HEADER
|
|
162
184
|
}
|
|
163
185
|
if ((flg & 0x20) !== 0) {
|
|
164
186
|
return ZLIB_ERR_DICTIONARY
|
|
165
187
|
}
|
|
166
|
-
|
|
167
|
-
|
|
188
|
+
let outEnd: i32 = toI32(out.length)
|
|
189
|
+
if (toI64(limit) < toI64(outEnd) - toI64(at)) {
|
|
190
|
+
outEnd = toI32(toI64(at) + toI64(limit))
|
|
191
|
+
}
|
|
168
192
|
const inf: Inflater = this.inflater
|
|
169
193
|
inf.reset()
|
|
170
|
-
const end: i32 = off + len
|
|
171
|
-
const r: i32 = inf.inflate(src, off + 2, end, false, out, at, outEnd, at)
|
|
194
|
+
const end: i32 = toI32(toI64(off) + toI64(len))
|
|
195
|
+
const r: i32 = inf.inflate(src, toI32(toI64(off) + 2), end, false, out, at, outEnd, at)
|
|
172
196
|
if (r === INFLATE_NEED_OUTPUT) {
|
|
173
197
|
return DEFLATE_ERR_OUTPUT_LIMIT
|
|
174
198
|
}
|
|
@@ -176,15 +200,27 @@ export class ZlibDecompressor {
|
|
|
176
200
|
return r
|
|
177
201
|
}
|
|
178
202
|
const q: i32 = inf.inPos
|
|
179
|
-
|
|
203
|
+
const q1: i32 = toI32(toI64(q) + 1)
|
|
204
|
+
const q2: i32 = toI32(toI64(q) + 2)
|
|
205
|
+
const q3: i32 = toI32(toI64(q) + 3)
|
|
206
|
+
if (
|
|
207
|
+
toI64(end) - toI64(q) < 4 ||
|
|
208
|
+
q < 0 ||
|
|
209
|
+
q >= n ||
|
|
210
|
+
q1 < 0 ||
|
|
211
|
+
q1 >= n ||
|
|
212
|
+
q2 < 0 ||
|
|
213
|
+
q2 >= n ||
|
|
214
|
+
q3 < 0 ||
|
|
215
|
+
q3 >= n
|
|
216
|
+
) {
|
|
180
217
|
return DEFLATE_ERR_TRUNCATED
|
|
181
218
|
}
|
|
182
|
-
const want: u32 =
|
|
183
|
-
|
|
184
|
-
if (adler32(out, at, inf.outPos - at) !== want) {
|
|
219
|
+
const want: u32 = (toU32(src[q]) << 24) | (toU32(src[q1]) << 16) | (toU32(src[q2]) << 8) | toU32(src[q3])
|
|
220
|
+
if (adler32(out, at, toI32(toI64(inf.outPos) - toI64(at))) !== want) {
|
|
185
221
|
return ZLIB_ERR_ADLER
|
|
186
222
|
}
|
|
187
|
-
if (q + 4 !== end) {
|
|
223
|
+
if (toI64(q) + 4 !== toI64(end)) {
|
|
188
224
|
return DEFLATE_ERR_TRAILING
|
|
189
225
|
}
|
|
190
226
|
return inf.outPos
|
package/std/crypto/aes.ts
CHANGED
|
@@ -436,6 +436,8 @@ const aesShiftPlane = (x: u64): u64 =>
|
|
|
436
436
|
|
|
437
437
|
/** ShiftRows (FIPS 197 §5.1.2) on the whole state. */
|
|
438
438
|
const aesShiftRows = (q: u64[]): void => {
|
|
439
|
+
// The last plane first: its checked read proves the other seven.
|
|
440
|
+
const p7: u64 = q[7]
|
|
439
441
|
q[0] = aesShiftPlane(q[0])
|
|
440
442
|
q[1] = aesShiftPlane(q[1])
|
|
441
443
|
q[2] = aesShiftPlane(q[2])
|
|
@@ -443,7 +445,7 @@ const aesShiftRows = (q: u64[]): void => {
|
|
|
443
445
|
q[4] = aesShiftPlane(q[4])
|
|
444
446
|
q[5] = aesShiftPlane(q[5])
|
|
445
447
|
q[6] = aesShiftPlane(q[6])
|
|
446
|
-
q[7] = aesShiftPlane(
|
|
448
|
+
q[7] = aesShiftPlane(p7)
|
|
447
449
|
}
|
|
448
450
|
|
|
449
451
|
/** Each byte replaced by the byte one row down its column (row 3 by row 0): nibbles turned by one. */
|
|
@@ -462,6 +464,8 @@ const aesRowAfterNext = (x: u64): u64 =>
|
|
|
462
464
|
* for the reduction by x^8 + x^4 + x^3 + x + 1.
|
|
463
465
|
*/
|
|
464
466
|
const aesMixColumns = (q: u64[]): void => {
|
|
467
|
+
// The last plane first: its checked read proves the other seven.
|
|
468
|
+
const n7: u64 = aesNextRow(q[7])
|
|
465
469
|
const n0: u64 = aesNextRow(q[0])
|
|
466
470
|
const n1: u64 = aesNextRow(q[1])
|
|
467
471
|
const n2: u64 = aesNextRow(q[2])
|
|
@@ -469,7 +473,6 @@ const aesMixColumns = (q: u64[]): void => {
|
|
|
469
473
|
const n4: u64 = aesNextRow(q[4])
|
|
470
474
|
const n5: u64 = aesNextRow(q[5])
|
|
471
475
|
const n6: u64 = aesNextRow(q[6])
|
|
472
|
-
const n7: u64 = aesNextRow(q[7])
|
|
473
476
|
const t0: u64 = q[0] ^ n0
|
|
474
477
|
const t1: u64 = q[1] ^ n1
|
|
475
478
|
const t2: u64 = q[2] ^ n2
|
|
@@ -490,15 +493,19 @@ const aesMixColumns = (q: u64[]): void => {
|
|
|
490
493
|
|
|
491
494
|
/** AddRoundKey (FIPS 197 §5.1.4): the eight words of `rk` from `at` into the state. */
|
|
492
495
|
const aesAddRoundKey = (q: u64[], rk: u64[], at: i32): void => {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
q[
|
|
499
|
-
q[
|
|
500
|
-
q[
|
|
501
|
-
q[
|
|
496
|
+
// The first and last key words and the last plane first: their checked
|
|
497
|
+
// reads prove every other index below.
|
|
498
|
+
const k0: u64 = rk[at]
|
|
499
|
+
const k7: u64 = rk[at + 7]
|
|
500
|
+
const q7: u64 = q[7]
|
|
501
|
+
q[0] = q[0] ^ k0
|
|
502
|
+
q[1] = q[1] ^ rk[at + 1]
|
|
503
|
+
q[2] = q[2] ^ rk[at + 2]
|
|
504
|
+
q[3] = q[3] ^ rk[at + 3]
|
|
505
|
+
q[4] = q[4] ^ rk[at + 4]
|
|
506
|
+
q[5] = q[5] ^ rk[at + 5]
|
|
507
|
+
q[6] = q[6] ^ rk[at + 6]
|
|
508
|
+
q[7] = q7 ^ k7
|
|
502
509
|
}
|
|
503
510
|
|
|
504
511
|
/**
|
|
@@ -564,18 +571,25 @@ const aesSubWord = (w: u32, q: u64[]): u32 => {
|
|
|
564
571
|
}
|
|
565
572
|
|
|
566
573
|
/** Four bytes of `data` from `at` as a big-endian word; the caller has checked the window. */
|
|
567
|
-
const aesLoad32 = (data: u8[], at: i32): u32 =>
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
574
|
+
const aesLoad32 = (data: u8[], at: i32): u32 => {
|
|
575
|
+
// The last byte first: every caller's `at` is non-negative, so its checked
|
|
576
|
+
// read proves the other three.
|
|
577
|
+
const b3: u32 = toU32(data[at + 3])
|
|
578
|
+
return (
|
|
579
|
+
(toU32(data[at]) << toU32(24)) |
|
|
580
|
+
(toU32(data[at + 1]) << toU32(16)) |
|
|
581
|
+
(toU32(data[at + 2]) << toU32(8)) |
|
|
582
|
+
b3
|
|
583
|
+
)
|
|
584
|
+
}
|
|
572
585
|
|
|
573
586
|
/** `w` big-endian into `out[at .. at + 4)`; the caller has checked the window. */
|
|
574
587
|
const aesStore32 = (out: u8[], at: i32, w: u32): void => {
|
|
588
|
+
// The first and last bytes first: their checked stores prove the two between.
|
|
575
589
|
out[at] = toU8(w >> toU32(24))
|
|
590
|
+
out[at + 3] = toU8(w)
|
|
576
591
|
out[at + 1] = toU8(w >> toU32(16))
|
|
577
592
|
out[at + 2] = toU8(w >> toU32(8))
|
|
578
|
-
out[at + 3] = toU8(w)
|
|
579
593
|
}
|
|
580
594
|
|
|
581
595
|
/**
|
|
@@ -588,37 +602,50 @@ const aesStore32 = (out: u8[], at: i32, w: u32): void => {
|
|
|
588
602
|
* that `rounds` is 10 or 14 to match, and that `rk` holds the
|
|
589
603
|
* `8 * (rounds + 1)` words this writes; the compiler may replace that call
|
|
590
604
|
* with a runtime kernel, which writes its own layout into the same words.
|
|
605
|
+
* `rounds` is an `i32` because the kernel takes one; the schedule is
|
|
606
|
+
* AES-128's for 10 and AES-256's for 14, the two values it can be.
|
|
591
607
|
*/
|
|
592
608
|
const aesScheduleInto = (key: u8[], rounds: i32, rk: u64[]): void => {
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
const
|
|
609
|
+
// The sizes are chosen from the two schedules, so each is a literal the
|
|
610
|
+
// bounds prover can see: Nk words of key, 4 * (rounds + 1) words in all.
|
|
611
|
+
const aes128: boolean = rounds === 10
|
|
612
|
+
const nk: integer<4, 8> = aes128 ? 4 : 8
|
|
613
|
+
const total: integer<44, 60> = aes128 ? 44 : 60
|
|
614
|
+
const lastRound: integer<10, 14> = aes128 ? 10 : 14
|
|
615
|
+
// Sized for AES-256; AES-128 uses the first 44 words.
|
|
616
|
+
const w: u32[] = new Array<u32>(60)
|
|
596
617
|
const q: u64[] = new Array<u64>(8)
|
|
597
618
|
const keyLength: i32 = toI32(key.length)
|
|
598
|
-
|
|
619
|
+
// The key's own Nk words.
|
|
620
|
+
for (let i: i32 = 0; i < nk; i++) {
|
|
621
|
+
const at: i32 = 4 * i
|
|
622
|
+
if (at + 3 < keyLength) {
|
|
623
|
+
w[i] = aesLoad32(key, at)
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
// Each later word. `prev` is w[i - 1], carried so its index needs no proof,
|
|
627
|
+
// and Nk is 4 or 8, a power of two, so i mod Nk is `i & (nk - 1)` for the
|
|
628
|
+
// non-negative `i` here and the loop does not divide.
|
|
599
629
|
// Rcon[i/Nk] = x^(i/Nk - 1) in GF(2^8), doubled once per Nk words.
|
|
600
630
|
let rcon: u32 = 1
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
temp = aesSubWord((temp << toU32(8)) | (temp >> toU32(24)), q) ^ (rcon << toU32(24))
|
|
611
|
-
rcon = ((rcon << toU32(1)) ^ (toU32(0x1b) & (toU32(0) - (rcon >> toU32(7))))) & toU32(0xff)
|
|
612
|
-
} else if (nk > 6 && i % nk === 4) {
|
|
613
|
-
temp = aesSubWord(temp, q)
|
|
614
|
-
}
|
|
615
|
-
w[i] = w[i - nk] ^ temp
|
|
631
|
+
let prev: u32 = w[nk - 1]
|
|
632
|
+
for (let i: i32 = nk; i < total; i++) {
|
|
633
|
+
const pos: i32 = i & (nk - 1)
|
|
634
|
+
let temp: u32 = prev
|
|
635
|
+
if (pos === 0) {
|
|
636
|
+
temp = aesSubWord((temp << toU32(8)) | (temp >> toU32(24)), q) ^ (rcon << toU32(24))
|
|
637
|
+
rcon = ((rcon << toU32(1)) ^ (toU32(0x1b) & (toU32(0) - (rcon >> toU32(7))))) & toU32(0xff)
|
|
638
|
+
} else if (nk > 6 && pos === 4) {
|
|
639
|
+
temp = aesSubWord(temp, q)
|
|
616
640
|
}
|
|
641
|
+
const word: u32 = w[i - nk] ^ temp
|
|
642
|
+
w[i] = word
|
|
643
|
+
prev = word
|
|
617
644
|
}
|
|
618
645
|
// Each round key, written into all four blocks of a batch and packed.
|
|
619
646
|
const buf: u8[] = new Array<u8>(AES_BATCH)
|
|
620
647
|
const rkLength: i32 = toI32(rk.length)
|
|
621
|
-
for (let round: i32 = 0; round <=
|
|
648
|
+
for (let round: i32 = 0; round <= lastRound; round++) {
|
|
622
649
|
for (let k: i32 = 0; k < 4; k++) {
|
|
623
650
|
for (let j: i32 = 0; j < 4; j++) {
|
|
624
651
|
aesStore32(buf, 16 * k + 4 * j, w[4 * round + j])
|
|
@@ -685,7 +712,7 @@ const aesCopyPrefix = (dst: u8[], src: u8[], count: i32): void => {
|
|
|
685
712
|
*/
|
|
686
713
|
const aesKeyUsable = (key: AesKey): boolean => {
|
|
687
714
|
const rounds: i32 = key.rounds
|
|
688
|
-
const words: i32 =
|
|
715
|
+
const words: i32 = rounds === 10 ? 88 : 120
|
|
689
716
|
return (
|
|
690
717
|
(rounds === 10 || rounds === 14) && toI32(key.roundKeys.length) === words && toI32(key.h.length) === 2
|
|
691
718
|
)
|
|
@@ -740,7 +767,10 @@ export const aesKey = (key: u8[]): AesKey | null => {
|
|
|
740
767
|
return null
|
|
741
768
|
}
|
|
742
769
|
const rounds: i32 = length === 16 ? 10 : 14
|
|
743
|
-
|
|
770
|
+
// Literal sizes: 88 words for AES-128's eleven round keys, 120 for
|
|
771
|
+
// AES-256's fifteen, eight words a round key.
|
|
772
|
+
const roundKeys: u64[] = length === 16 ? new Array<u64>(88) : new Array<u64>(120)
|
|
773
|
+
const expanded: AesKey = new AesKey(rounds, roundKeys)
|
|
744
774
|
if (!aesKeyInto(key, expanded)) {
|
|
745
775
|
return null
|
|
746
776
|
}
|
|
@@ -785,15 +815,17 @@ export const aesKeyInto = (key: u8[], out: AesKey): boolean => {
|
|
|
785
815
|
const rounds: i32 = length === 16 ? 10 : 14
|
|
786
816
|
const roundKeys: u64[] = out.roundKeys
|
|
787
817
|
const hash: u64[] = out.h
|
|
788
|
-
|
|
818
|
+
const words: i32 = length === 16 ? 88 : 120
|
|
819
|
+
if (toI32(roundKeys.length) !== words || toI32(hash.length) !== 2) {
|
|
789
820
|
return false
|
|
790
821
|
}
|
|
791
822
|
aesScheduleInto(key, rounds, roundKeys)
|
|
792
823
|
out.rounds = rounds
|
|
793
824
|
const h: u8[] = new Array<u8>(AES_BLOCK)
|
|
794
825
|
aesEncryptOne(out, new Array<u8>(AES_BLOCK), h)
|
|
795
|
-
|
|
826
|
+
// The second word first: its checked store proves the first.
|
|
796
827
|
hash[1] = aesLoad64(h, 8, 16)
|
|
828
|
+
hash[0] = aesLoad64(h, 0, 8)
|
|
797
829
|
wipe(h)
|
|
798
830
|
return true
|
|
799
831
|
}
|
|
@@ -896,8 +928,9 @@ const ghashMul32 = (x: u64, y: u64): u64 => {
|
|
|
896
928
|
export const ghashMultiply = (y: u64[], hHi: u64, hLo: u64): void => {
|
|
897
929
|
const low32: u64 = (toU64(1) << toU64(32)) - toU64(1)
|
|
898
930
|
const thirtyTwo: u64 = toU64(32)
|
|
899
|
-
|
|
931
|
+
// The second word first: its checked read proves the first.
|
|
900
932
|
const a0: u64 = y[1]
|
|
933
|
+
const a1: u64 = y[0]
|
|
901
934
|
const a2: u64 = a1 ^ a0
|
|
902
935
|
const b2: u64 = hHi ^ hLo
|
|
903
936
|
|
|
@@ -960,12 +993,14 @@ export const ghashMultiply = (y: u64[], hHi: u64, hLo: u64): void => {
|
|
|
960
993
|
* compiler may replace that call with a runtime kernel.
|
|
961
994
|
*/
|
|
962
995
|
const ghashBlocksInto = (y: u64[], hHi: u64, hLo: u64, _rk: u64[], data: u8[], length: i32): void => {
|
|
963
|
-
if (toI32(y.length) < 2) {
|
|
996
|
+
if (toI32(y.length) < 2 || length < 0) {
|
|
964
997
|
return
|
|
965
998
|
}
|
|
966
999
|
// Counted in blocks rather than stepped by 16, so no offset is ever formed
|
|
967
1000
|
// past `length`: an AAD or IV within 16 bytes of 2^31 would overflow `at + 16`.
|
|
968
|
-
|
|
1001
|
+
// The partial block's 1 is `((length & 15) + 15) >> 4`, in shifts and
|
|
1002
|
+
// masks the bounds prover follows.
|
|
1003
|
+
const blocks: i32 = (length >> 4) + (((length & 15) + 15) >> 4)
|
|
969
1004
|
for (let block: i32 = 0; block < blocks; block++) {
|
|
970
1005
|
const at: i32 = block * 16
|
|
971
1006
|
y[0] = y[0] ^ aesLoad64(data, at, length)
|
|
@@ -981,17 +1016,28 @@ const ghashBlocksInto = (y: u64[], hHi: u64, hLo: u64, _rk: u64[], data: u8[], l
|
|
|
981
1016
|
* caller passes one.
|
|
982
1017
|
*/
|
|
983
1018
|
const ghashUpdate = (y: u64[], key: AesKey, data: u8[], length: i32): void => {
|
|
984
|
-
|
|
1019
|
+
const h: u64[] = key.h
|
|
1020
|
+
if (
|
|
1021
|
+
toI32(h.length) < 2 ||
|
|
1022
|
+
toI32(y.length) < 2 ||
|
|
1023
|
+
length < 0 ||
|
|
1024
|
+
length > toI32(data.length) ||
|
|
1025
|
+
!aesKeyUsable(key)
|
|
1026
|
+
) {
|
|
985
1027
|
return
|
|
986
1028
|
}
|
|
987
|
-
ghashBlocksInto(y,
|
|
1029
|
+
ghashBlocksInto(y, h[0], h[1], key.roundKeys, data, length)
|
|
988
1030
|
}
|
|
989
1031
|
|
|
990
|
-
/**
|
|
991
|
-
|
|
1032
|
+
/**
|
|
1033
|
+
* The last GHASH block: the AAD's and the ciphertext's lengths in bits, 64
|
|
1034
|
+
* bits each. `hHi` and `hLo` are H's words, read by the entry point that
|
|
1035
|
+
* checked the key's shape.
|
|
1036
|
+
*/
|
|
1037
|
+
const ghashLengths = (y: u64[], hHi: u64, hLo: u64, aadLength: i32, textLength: i32): void => {
|
|
992
1038
|
y[0] = y[0] ^ (toU64(aadLength) << toU64(3))
|
|
993
1039
|
y[1] = y[1] ^ (toU64(textLength) << toU64(3))
|
|
994
|
-
ghashMultiply(y,
|
|
1040
|
+
ghashMultiply(y, hHi, hLo)
|
|
995
1041
|
}
|
|
996
1042
|
|
|
997
1043
|
/**
|
|
@@ -1000,17 +1046,18 @@ const ghashLengths = (y: u64[], key: AesKey, aadLength: i32, textLength: i32): v
|
|
|
1000
1046
|
* block and followed by its length in bits. That GHASH, with the IV, says
|
|
1001
1047
|
* something of H, so the GHASH state is wiped here and the caller wipes J0.
|
|
1002
1048
|
*/
|
|
1003
|
-
const aesGcmJ0 = (key: AesKey, iv: u8[]): u8[] => {
|
|
1049
|
+
const aesGcmJ0 = (key: AesKey, hHi: u64, hLo: u64, iv: u8[]): u8[] => {
|
|
1004
1050
|
const length: i32 = toI32(iv.length)
|
|
1005
|
-
const j0: u8[] = new Array<u8>(
|
|
1051
|
+
const j0: u8[] = new Array<u8>(16)
|
|
1006
1052
|
if (length === 12) {
|
|
1007
|
-
|
|
1053
|
+
// The counter byte first, while the length is still known to be 16.
|
|
1008
1054
|
j0[15] = 1
|
|
1055
|
+
aesCopyPrefix(j0, iv, 12)
|
|
1009
1056
|
return j0
|
|
1010
1057
|
}
|
|
1011
1058
|
const y: u64[] = new Array<u64>(2)
|
|
1012
1059
|
ghashUpdate(y, key, iv, length)
|
|
1013
|
-
ghashLengths(y,
|
|
1060
|
+
ghashLengths(y, hHi, hLo, 0, length)
|
|
1014
1061
|
aesStore64(j0, 0, y[0])
|
|
1015
1062
|
aesStore64(j0, 8, y[1])
|
|
1016
1063
|
// GHASH of the IV under H: with the IV, it says something of H.
|
|
@@ -1026,8 +1073,11 @@ const aesGcmJ0 = (key: AesKey, iv: u8[]): u8[] => {
|
|
|
1026
1073
|
* bound is stated where the counter is, rather than left to the width of an
|
|
1027
1074
|
* array length.
|
|
1028
1075
|
*/
|
|
1029
|
-
const aesGcmLengthFits = (length: i32): boolean =>
|
|
1030
|
-
(
|
|
1076
|
+
const aesGcmLengthFits = (length: i32): boolean => {
|
|
1077
|
+
// (2^32 - 2) blocks of 16 bytes: at most that many blocks is at most this many bytes.
|
|
1078
|
+
const most: i64 = 0xfffffffe0
|
|
1079
|
+
return toI64(length) <= most
|
|
1080
|
+
}
|
|
1031
1081
|
|
|
1032
1082
|
/**
|
|
1033
1083
|
* The GCTR seam (SP 800-38D §6.5) from inc32(J0): `dst[i] = src[i] ⊕
|
|
@@ -1048,13 +1098,14 @@ const aesCtrInto = (rk: u64[], rounds: i32, j0: u8[], src: u8[], dst: u8[], leng
|
|
|
1048
1098
|
return
|
|
1049
1099
|
}
|
|
1050
1100
|
const counter: u32 = aesLoad32(j0, 12)
|
|
1051
|
-
const buf: u8[] = new Array<u8>(
|
|
1101
|
+
const buf: u8[] = new Array<u8>(64)
|
|
1052
1102
|
const q: u64[] = new Array<u64>(8)
|
|
1053
1103
|
const srcLength: i32 = toI32(src.length)
|
|
1054
1104
|
const dstLength: i32 = toI32(dst.length)
|
|
1055
1105
|
const bufLength: i32 = toI32(buf.length)
|
|
1056
1106
|
// Counted in batches, like `ghashUpdate`'s blocks, so `base` never passes `length`.
|
|
1057
|
-
|
|
1107
|
+
// The partial batch's 1 is `((length & 63) + 63) >> 6`, as there.
|
|
1108
|
+
const batches: i32 = (length >> 6) + (((length & 63) + 63) >> 6)
|
|
1058
1109
|
for (let batch: i32 = 0; batch < batches; batch++) {
|
|
1059
1110
|
const base: i32 = batch * AES_BATCH
|
|
1060
1111
|
for (let k: i32 = 0; k < 4; k++) {
|
|
@@ -1104,17 +1155,27 @@ const aesGcmCtr = (key: AesKey, j0: u8[], src: u8[], dst: u8[], length: i32): vo
|
|
|
1104
1155
|
* E(K, J0) ⊕ S, GCM's tag (SP 800-38D §7.1 step 6), into `y` (zero, two
|
|
1105
1156
|
* words) as two big-endian words: S is the GHASH of the AAD and the
|
|
1106
1157
|
* ciphertext `text[0 .. textLength)`. E(K, J0)'s block is wiped once it is
|
|
1107
|
-
* folded in.
|
|
1158
|
+
* folded in. `hHi` and `hLo` are H's words, as `key.h` holds them.
|
|
1108
1159
|
*/
|
|
1109
|
-
const aesGcmTag = (
|
|
1160
|
+
const aesGcmTag = (
|
|
1161
|
+
y: u64[],
|
|
1162
|
+
key: AesKey,
|
|
1163
|
+
hHi: u64,
|
|
1164
|
+
hLo: u64,
|
|
1165
|
+
j0: u8[],
|
|
1166
|
+
aad: u8[],
|
|
1167
|
+
text: u8[],
|
|
1168
|
+
textLength: i32
|
|
1169
|
+
): void => {
|
|
1110
1170
|
const aadLength: i32 = toI32(aad.length)
|
|
1111
1171
|
ghashUpdate(y, key, aad, aadLength)
|
|
1112
1172
|
ghashUpdate(y, key, text, textLength)
|
|
1113
|
-
ghashLengths(y,
|
|
1173
|
+
ghashLengths(y, hHi, hLo, aadLength, textLength)
|
|
1114
1174
|
const mask: u8[] = new Array<u8>(AES_BLOCK)
|
|
1115
1175
|
aesEncryptOne(key, j0, mask)
|
|
1116
|
-
|
|
1176
|
+
// The second word first: its checked read proves the first.
|
|
1117
1177
|
y[1] = y[1] ^ aesLoad64(mask, 8, 16)
|
|
1178
|
+
y[0] = y[0] ^ aesLoad64(mask, 0, 16)
|
|
1118
1179
|
wipe(mask)
|
|
1119
1180
|
}
|
|
1120
1181
|
|
|
@@ -1152,17 +1213,28 @@ export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u
|
|
|
1152
1213
|
const length: i32 = toI32(plaintext.length)
|
|
1153
1214
|
// 2^31 - 17: the longest plaintext whose sealed form, sixteen bytes longer,
|
|
1154
1215
|
// is still an array length.
|
|
1155
|
-
const
|
|
1156
|
-
if (
|
|
1216
|
+
const h: u64[] = key.h
|
|
1217
|
+
if (
|
|
1218
|
+
length > 0x7fffffef ||
|
|
1219
|
+
toI32(h.length) !== 2 ||
|
|
1220
|
+
!aesKeyUsable(key) ||
|
|
1221
|
+
toI32(iv.length) === 0 ||
|
|
1222
|
+
!aesGcmLengthFits(length)
|
|
1223
|
+
) {
|
|
1157
1224
|
return null
|
|
1158
1225
|
}
|
|
1159
|
-
|
|
1226
|
+
// H's words, read where the length test above proves both.
|
|
1227
|
+
const hLo: u64 = h[1]
|
|
1228
|
+
const hHi: u64 = h[0]
|
|
1229
|
+
const j0: u8[] = aesGcmJ0(key, hHi, hLo, iv)
|
|
1160
1230
|
const out: u8[] = new Array<u8>(length + AES_GCM_TAG_SIZE)
|
|
1161
1231
|
aesGcmCtr(key, j0, plaintext, out, length)
|
|
1162
1232
|
const tag: u64[] = new Array<u64>(2)
|
|
1163
|
-
aesGcmTag(tag, key, j0, aad, out, length)
|
|
1233
|
+
aesGcmTag(tag, key, hHi, hLo, j0, aad, out, length)
|
|
1234
|
+
// The second word first: its checked read proves the first.
|
|
1235
|
+
const tagLo: u64 = tag[1]
|
|
1164
1236
|
aesStore64(out, length, tag[0])
|
|
1165
|
-
aesStore64(out, length + 8,
|
|
1237
|
+
aesStore64(out, length + 8, tagLo)
|
|
1166
1238
|
wipe(j0)
|
|
1167
1239
|
return out
|
|
1168
1240
|
}
|
|
@@ -1184,29 +1256,35 @@ export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u
|
|
|
1184
1256
|
*/
|
|
1185
1257
|
export const aesGcmOpen = (key: AesKey, iv: u8[], aad: u8[], sealed: u8[]): u8[] | null => {
|
|
1186
1258
|
const total: i32 = toI32(sealed.length)
|
|
1187
|
-
|
|
1259
|
+
const h: u64[] = key.h
|
|
1260
|
+
if (toI32(h.length) !== 2 || !aesKeyUsable(key) || toI32(iv.length) === 0 || total < AES_GCM_TAG_SIZE) {
|
|
1188
1261
|
return null
|
|
1189
1262
|
}
|
|
1263
|
+
// Under H = 0 GHASH is zero for every input, so the tag E(K, J0) would
|
|
1264
|
+
// authenticate any ciphertext and AAD. Only a key built or edited by hand
|
|
1265
|
+
// has it (for one from `aesKey` the chance is 2^-128), and such a key opens
|
|
1266
|
+
// nothing. H is secret, so this is folded into the mask below, not branched
|
|
1267
|
+
// on; it is read here, where the length test above still proves both words.
|
|
1268
|
+
const hLo: u64 = h[1]
|
|
1269
|
+
const hHi: u64 = h[0]
|
|
1270
|
+
const hashKeySet: u64 = ~ctEq(hHi | hLo, toU64(0))
|
|
1190
1271
|
// `total` is an array length and at least a tag, so neither this nor the
|
|
1191
1272
|
// tag's second word at `length + 8` can leave the range of an `i32`.
|
|
1192
1273
|
const length: i32 = total - AES_GCM_TAG_SIZE
|
|
1193
1274
|
if (!aesGcmLengthFits(length)) {
|
|
1194
1275
|
return null
|
|
1195
1276
|
}
|
|
1196
|
-
const j0: u8[] = aesGcmJ0(key, iv)
|
|
1277
|
+
const j0: u8[] = aesGcmJ0(key, hHi, hLo, iv)
|
|
1197
1278
|
const tag: u64[] = new Array<u64>(2)
|
|
1198
|
-
aesGcmTag(tag, key, j0, aad, sealed, length)
|
|
1279
|
+
aesGcmTag(tag, key, hHi, hLo, j0, aad, sealed, length)
|
|
1280
|
+
// The second word first: its checked read proves the first.
|
|
1281
|
+
const tagLo: u64 = tag[1]
|
|
1199
1282
|
const same: u64 = aesGcmTagMask(
|
|
1200
1283
|
tag[0],
|
|
1201
|
-
|
|
1284
|
+
tagLo,
|
|
1202
1285
|
aesLoad64(sealed, length, total),
|
|
1203
1286
|
aesLoad64(sealed, length + 8, total)
|
|
1204
1287
|
)
|
|
1205
|
-
// Under H = 0 GHASH is zero for every input, so the tag E(K, J0) would
|
|
1206
|
-
// authenticate any ciphertext and AAD. Only a key built or edited by hand
|
|
1207
|
-
// has it (for one from `aesKey` the chance is 2^-128), and such a key opens
|
|
1208
|
-
// nothing. H is secret, so this is folded into the mask, not branched on.
|
|
1209
|
-
const hashKeySet: u64 = ~ctEq(key.h[0] | key.h[1], toU64(0))
|
|
1210
1288
|
wipe(tag)
|
|
1211
1289
|
let out: u8[] | null = null
|
|
1212
1290
|
if ((same & hashKeySet) !== toU64(0)) {
|