@amritk/nish-aarch64-linux 0.42.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
@@ -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
- if (toI32(out.length) - at < 6) {
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
- let flg: i32 = flevel << 6
101
- flg += (toI32(31) - ((cmf * 256 + flg) % 31)) % 31
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[at + 1] = toU8(flg)
104
- const end: i32 = this.deflater.compress(src, off, len, out, at + 2)
105
- if (end < 0 || toI32(out.length) - end < 4) {
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[end + 1] = toU8((a >>> 16) & 255)
111
- out[end + 2] = toU8((a >>> 8) & 255)
112
- out[end + 3] = toU8(a & 255)
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
- if (len < 2) {
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[off + 1])
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
- const room: i32 = toI32(out.length) - at
167
- const outEnd: i32 = at + (limit < room ? limit : room)
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
- if (end - q < 4) {
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
- (toU32(src[q]) << 24) | (toU32(src[q + 1]) << 16) | (toU32(src[q + 2]) << 8) | toU32(src[q + 3])
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(q[7])
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
- const p: u32 = toU32(at)
494
- q[0] = q[0] ^ rk[p]
495
- q[1] = q[1] ^ rk[p + 1]
496
- q[2] = q[2] ^ rk[p + 2]
497
- q[3] = q[3] ^ rk[p + 3]
498
- q[4] = q[4] ^ rk[p + 4]
499
- q[5] = q[5] ^ rk[p + 5]
500
- q[6] = q[6] ^ rk[p + 6]
501
- q[7] = q[7] ^ rk[p + 7]
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
- (toU32(data[at]) << toU32(24)) |
569
- (toU32(data[at + 1]) << toU32(16)) |
570
- (toU32(data[at + 2]) << toU32(8)) |
571
- toU32(data[at + 3])
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
- const nk: i32 = rounds - 6
594
- const total: i32 = (rounds + 1) * 4
595
- const w: u32[] = new Array<u32>(total)
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
- const wLength: i32 = toI32(w.length)
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
- for (let i: i32 = 0; i < wLength; i++) {
602
- if (i < nk) {
603
- const at: i32 = 4 * i
604
- if (at >= 0 && at + 3 < keyLength) {
605
- w[i] = aesLoad32(key, at)
606
- }
607
- } else {
608
- let temp: u32 = w[i - 1]
609
- if (i % nk === 0) {
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 <= rounds; 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 = (rounds + 1) * 8
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
- const expanded: AesKey = new AesKey(rounds, new Array<u64>((rounds + 1) * 8))
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
- if (toI32(roundKeys.length) !== (rounds + 1) * 8 || toI32(hash.length) !== 2) {
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
- hash[0] = aesLoad64(h, 0, 8)
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
- const a1: u64 = y[0]
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
- const blocks: i32 = (length >> 4) + ((length & 15) !== 0 ? 1 : 0)
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
- if (toI32(y.length) < 2 || length < 0 || length > toI32(data.length) || !aesKeyUsable(key)) {
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, key.h[0], key.h[1], key.roundKeys, data, length)
1029
+ ghashBlocksInto(y, h[0], h[1], key.roundKeys, data, length)
988
1030
  }
989
1031
 
990
- /** The last GHASH block: the AAD's and the ciphertext's lengths in bits, 64 bits each. */
991
- const ghashLengths = (y: u64[], key: AesKey, aadLength: i32, textLength: i32): void => {
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, key.h[0], key.h[1])
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>(AES_BLOCK)
1051
+ const j0: u8[] = new Array<u8>(16)
1006
1052
  if (length === 12) {
1007
- aesCopyPrefix(j0, iv, 12)
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, key, 0, length)
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
- (toI64(length) + toI64(15)) >> toI64(4) <= (toI64(1) << toI64(32)) - toI64(2)
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>(AES_BATCH)
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
- const batches: i32 = (length >> 6) + ((length & 63) !== 0 ? 1 : 0)
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 = (y: u64[], key: AesKey, j0: u8[], aad: u8[], text: u8[], textLength: i32): void => {
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, key, aadLength, textLength)
1173
+ ghashLengths(y, hHi, hLo, aadLength, textLength)
1114
1174
  const mask: u8[] = new Array<u8>(AES_BLOCK)
1115
1175
  aesEncryptOne(key, j0, mask)
1116
- y[0] = y[0] ^ aesLoad64(mask, 0, 16)
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 longest: i32 = 0x7fffffef
1156
- if (!aesKeyUsable(key) || toI32(iv.length) === 0 || length > longest || !aesGcmLengthFits(length)) {
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
- const j0: u8[] = aesGcmJ0(key, iv)
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, tag[1])
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
- if (!aesKeyUsable(key) || toI32(iv.length) === 0 || total < AES_GCM_TAG_SIZE) {
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
- tag[1],
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)) {