@amritk/nish-aarch64-linux 0.50.0 → 0.51.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.
Files changed (54) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +4 -1
  3. package/package.json +1 -1
  4. package/runtime/nish.d.ts +82 -2
  5. package/runtime/nish.h +5 -0
  6. package/runtime/runtime-host.c +21 -5
  7. package/std/README.md +7 -7
  8. package/std/base64.ts +61 -95
  9. package/std/bytes.ts +6 -4
  10. package/std/cli-args.ts +150 -107
  11. package/std/compress/checksum.ts +68 -10
  12. package/std/compress/deflate.ts +193 -100
  13. package/std/compress/gzip.ts +124 -49
  14. package/std/compress/zlib.ts +57 -15
  15. package/std/crypto/aes.ts +51 -9
  16. package/std/crypto/ed25519.ts +12 -10
  17. package/std/crypto/hkdf.ts +405 -283
  18. package/std/crypto/hmac.ts +483 -148
  19. package/std/crypto/jwt.ts +19 -21
  20. package/std/crypto/p256.ts +5 -7
  21. package/std/crypto/pki-roots.ts +28 -8
  22. package/std/crypto/rsa-sign.ts +5 -2
  23. package/std/crypto/sha256.ts +81 -26
  24. package/std/crypto/sha512.ts +135 -49
  25. package/std/json-value.ts +14 -15
  26. package/std/net/buffer-pool.ts +25 -14
  27. package/std/net/fetch.ts +108 -27
  28. package/std/net/handler.ts +11 -22
  29. package/std/net/hpack.ts +38 -19
  30. package/std/net/http-compress.ts +21 -12
  31. package/std/net/http-server.ts +9 -4
  32. package/std/net/http1-client-codec.ts +22 -15
  33. package/std/net/http1-codec.ts +37 -36
  34. package/std/net/http3-server.ts +127 -45
  35. package/std/net/http3.ts +99 -50
  36. package/std/net/postgres-async.ts +5 -10
  37. package/std/net/qpack.ts +83 -17
  38. package/std/net/quic-listener.ts +42 -27
  39. package/std/net/quic-packet.ts +91 -50
  40. package/std/net/quic-stream.ts +29 -22
  41. package/std/net/quic.ts +48 -23
  42. package/std/net/static.ts +5 -3
  43. package/std/net/tls/client.ts +81 -34
  44. package/std/net/tls/codec.ts +3 -4
  45. package/std/net/tls/record.ts +54 -22
  46. package/std/net/tls/schedule.ts +339 -149
  47. package/std/net/tls.ts +42 -12
  48. package/std/net/upstream.ts +18 -19
  49. package/std/net/url.ts +78 -21
  50. package/std/net/websocket.ts +3 -11
  51. package/std/net/webtransport.ts +95 -21
  52. package/std/raster.ts +12 -2
  53. package/std/term-pixels.ts +18 -2
  54. package/std/text.ts +5 -5
@@ -30,6 +30,9 @@
30
30
  * refused, not ignored: two or more bytes without the `1f 8b` magic are
31
31
  * `GZIP_ERR_HEADER`, while a single byte, or fewer than 10 that start with
32
32
  * `1f 8b`, is `DEFLATE_ERR_TRUNCATED`. The `limit` bounds the whole output.
33
+ * A window outside its array is `GZIP_ERR_WINDOW`, with nothing read or
34
+ * written (`decompressRange` answers `DEFLATE_ERR_WINDOW`, as the deflate
35
+ * decoders do).
33
36
  *
34
37
  * **Streaming.** `GzipWriter` compresses a body that arrives in pieces and
35
38
  * `GzipReader` decompresses one: see each class.
@@ -40,6 +43,7 @@ import { Crc32 } from "nish/compress/checksum"
40
43
  import {
41
44
  DEFLATE_ERR_OUTPUT_LIMIT,
42
45
  DEFLATE_ERR_TRUNCATED,
46
+ DEFLATE_ERR_WINDOW,
43
47
  DEFLATE_NO_ROOM,
44
48
  Deflater,
45
49
  FixedDeflater,
@@ -71,6 +75,15 @@ export const GZIP_ERR_SIZE: i32 = -11
71
75
  */
72
76
  export const GZIP_ERR_FINISHED: i32 = -20
73
77
 
78
+ /**
79
+ * A window outside its array, answered with nothing read or written by
80
+ * `GzipCompressor.compress`, `GzipFixedCompressor.compress`,
81
+ * `GzipDecompressor.decompress`, `GzipWriter.write`, `flush` and `finish`,
82
+ * and `GzipReader.feed` and `read`. It changes no state: the call may be
83
+ * made again with a window that fits.
84
+ */
85
+ export const GZIP_ERR_WINDOW: i32 = -21
86
+
74
87
  /** `GzipReader`: no more output yet; give it input. */
75
88
  export const GZIP_NEED_INPUT: i32 = 0
76
89
 
@@ -91,11 +104,10 @@ const GZIP_READER_WINDOW: i32 = 32768
91
104
  /** The bytes a `GzipReader` buffers of input. */
92
105
  const GZIP_READER_INPUT: i32 = 16384
93
106
 
94
- /** Panics unless `buf[off .. off + len)` lies inside `buf`. */
95
- const gzipWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
96
- if (off < 0 || len < 0 || off > toI32(buf.length) - len) {
97
- panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${toI32(buf.length)} bytes`)
98
- }
107
+ /** Whether `buf[off .. off + len)` lies inside `buf`. */
108
+ const gzipInside = (buf: u8[], off: i32, len: i32): boolean => {
109
+ const size: i32 = toI32(buf.length)
110
+ return off >= 0 && len >= 0 && off <= size && len <= size - off
99
111
  }
100
112
 
101
113
  /** The byte at `b[i]`, or -1 when `i` is outside `b`. */
@@ -230,7 +242,13 @@ const gzipReadHeader = (crc: Crc32, src: u8[], p: i32, end: i32): i32 => {
230
242
  return DEFLATE_ERR_TRUNCATED
231
243
  }
232
244
  const want: i32 = (h0 & 255) | ((h1 & 255) << 8)
233
- if (toI32(crc.update(toU32(0), src, p, toI32(toI64(q) - toI64(p))) & 0xffff) !== want) {
245
+ // `h1` was read inside `src`, so `[p, q)` lies inside it and `ofRange`
246
+ // answers no -1 here; were it to, the header ran past the input.
247
+ const got: i64 = crc.ofRange(src, p, q)
248
+ if (got < 0) {
249
+ return DEFLATE_ERR_TRUNCATED
250
+ }
251
+ if (toI32(got & 0xffff) !== want) {
234
252
  return GZIP_ERR_HEADER
235
253
  }
236
254
  q = toI32(toI64(q) + 2)
@@ -274,7 +292,7 @@ export class GzipCompressor {
274
292
  * Compresses `src[off .. off + len)` into `out` from `at` as one gzip
275
293
  * member, and answers where it ends, or `DEFLATE_NO_ROOM` (-1) when `out`
276
294
  * cannot hold it; `compressBound(len)` bytes always can. A window outside
277
- * its array panics. Allocates nothing.
295
+ * its array is `GZIP_ERR_WINDOW`. Allocates nothing.
278
296
  *
279
297
  * @param src The array that holds the data to compress.
280
298
  * @param off Where in `src` the data starts.
@@ -283,11 +301,13 @@ export class GzipCompressor {
283
301
  * @param at Where in `out` the member starts.
284
302
  * @returns The index in `out` one past the member's last byte, so the
285
303
  * member is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
286
- * bytes of `out` from `at` may have been overwritten and mean nothing.
304
+ * bytes of `out` from `at` may have been overwritten and mean nothing;
305
+ * or `GZIP_ERR_WINDOW`, with nothing written.
287
306
  */
288
307
  compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
289
- gzipWindow("GzipCompressor.compress", src, off, len)
290
- gzipWindow("GzipCompressor.compress", out, at, 0)
308
+ if (!gzipInside(src, off, len) || !gzipInside(out, at, 0)) {
309
+ return GZIP_ERR_WINDOW
310
+ }
291
311
  if (toI64(toI32(out.length)) - toI64(at) < toI64(GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE)) {
292
312
  return DEFLATE_NO_ROOM
293
313
  }
@@ -340,7 +360,8 @@ export class GzipFixedCompressor {
340
360
  * member, and answers where it ends, or `DEFLATE_NO_ROOM` (-1) as soon as
341
361
  * it would need more than `room` bytes, so a caller passing the most it
342
362
  * will take stops early on an input that does not shrink.
343
- * `gzipFixedBound(len)` always fits. A window outside its array panics.
363
+ * `gzipFixedBound(len)` always fits. A window outside its array is
364
+ * `GZIP_ERR_WINDOW`.
344
365
  * Allocates nothing.
345
366
  *
346
367
  * @param src The array that holds the data to compress.
@@ -353,11 +374,12 @@ export class GzipFixedCompressor {
353
374
  * @returns The index in `out` one past the member's last byte, so the
354
375
  * member is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
355
376
  * bytes of `out[at .. at + room)` may have been overwritten and mean
356
- * nothing.
377
+ * nothing; or `GZIP_ERR_WINDOW`, with nothing written.
357
378
  */
358
379
  compress(src: u8[], off: i32, len: i32, out: u8[], at: i32, room: i32): i32 {
359
- gzipWindow("GzipFixedCompressor.compress", src, off, len)
360
- gzipWindow("GzipFixedCompressor.compress", out, at, room)
380
+ if (!gzipInside(src, off, len) || !gzipInside(out, at, room)) {
381
+ return GZIP_ERR_WINDOW
382
+ }
361
383
  if (room < GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE) {
362
384
  return DEFLATE_NO_ROOM
363
385
  }
@@ -399,8 +421,12 @@ export class GzipDecompressor {
399
421
  * answers where the output ends; or `DEFLATE_ERR_OUTPUT_LIMIT` when the
400
422
  * data is longer, `GZIP_ERR_HEADER`, `GZIP_ERR_CRC`, `GZIP_ERR_SIZE`,
401
423
  * `DEFLATE_ERR_TRUNCATED` or another `DEFLATE_ERR_*` for a malformed
402
- * stream. An empty input is truncated. A window outside its array, or a
403
- * negative `limit`, panics. Allocates nothing.
424
+ * stream. An empty input is truncated. A window outside its array is
425
+ * `GZIP_ERR_WINDOW`, with nothing written. A negative `limit` panics: a
426
+ * caller contract, so a constant `limit`, or one behind the caller's own
427
+ * `limit >= 0` test, proves it at the call (`decompressRange` answers it
428
+ * instead).
429
+ * Allocates nothing.
404
430
  *
405
431
  * @param src The array that holds the gzip data, one member or several.
406
432
  * @param off Where in `src` the first member starts.
@@ -418,21 +444,43 @@ export class GzipDecompressor {
418
444
  * @returns The index in `out` one past the data's last byte, so the data
419
445
  * is `out[at .. result)`; or one of the negative codes above, after
420
446
  * which the bytes of `out` from `at` may have been overwritten and mean
421
- * nothing.
447
+ * nothing; or `GZIP_ERR_WINDOW`.
422
448
  */
423
449
  decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 {
424
- gzipWindow("GzipDecompressor.decompress", src, off, len)
425
- gzipWindow("GzipDecompressor.decompress", out, at, 0)
426
450
  if (limit < 0) {
427
451
  panic("GzipDecompressor.decompress: a negative limit")
428
452
  }
429
- // Positions are added in i64: the windows were checked in `gzipWindow`,
430
- // but the prover does not carry that across the call.
453
+ if (!gzipInside(src, off, len) || !gzipInside(out, at, 0)) {
454
+ return GZIP_ERR_WINDOW
455
+ }
456
+ return this.decompressRange(src, off, toI32(toI64(off) + toI64(len)), out, at, limit)
457
+ }
458
+
459
+ /**
460
+ * `decompress` over `src[off .. end)`, a range given by its two ends, that
461
+ * answers a misuse rather than panicking: a range outside `src`, an output
462
+ * start outside `out` or a negative `limit` is `DEFLATE_ERR_WINDOW`, with
463
+ * nothing read or written. `nish/net/http-compress`'s decoder calls this
464
+ * one, so a body it decodes reaches no panic.
465
+ *
466
+ * @internal Called by std/net/http-compress.ts's body decoder.
467
+ * @param src The array that holds the gzip data, one member or several.
468
+ * @param off Where in `src` the first member starts.
469
+ * @param end One past the last byte of the members.
470
+ * @param out The caller's array the data is written into.
471
+ * @param at Where in `out` the data starts.
472
+ * @param limit The most bytes of data to accept over all the members.
473
+ * @returns The index in `out` one past the data's last byte, or a negative
474
+ * code, as `decompress` answers; or `DEFLATE_ERR_WINDOW`.
475
+ */
476
+ decompressRange(src: u8[], off: i32, end: i32, out: u8[], at: i32, limit: i32): i32 {
477
+ if (off < 0 || off > end || end > toI32(src.length) || at < 0 || at > toI32(out.length) || limit < 0) {
478
+ return DEFLATE_ERR_WINDOW
479
+ }
431
480
  let outEnd: i32 = toI32(out.length)
432
481
  if (toI64(limit) < toI64(outEnd) - toI64(at)) {
433
482
  outEnd = toI32(toI64(at) + toI64(limit))
434
483
  }
435
- const end: i32 = toI32(toI64(off) + toI64(len))
436
484
  let p: i32 = off
437
485
  let op: i32 = at
438
486
  let first: boolean = true
@@ -444,7 +492,7 @@ export class GzipDecompressor {
444
492
  }
445
493
  const inf: Inflater = this.inflater
446
494
  inf.reset()
447
- const r: i32 = inf.inflate(src, body, end, false, out, op, outEnd, op)
495
+ const r: i32 = inf.inflateAll(src, body, end, out, op, outEnd)
448
496
  if (r === INFLATE_NEED_OUTPUT) {
449
497
  return DEFLATE_ERR_OUTPUT_LIMIT
450
498
  }
@@ -457,14 +505,20 @@ export class GzipDecompressor {
457
505
  if (toI64(end) - toI64(q) < toI64(GZIP_TRAILER_SIZE) || wantCrc < 0 || wantSize < 0) {
458
506
  return DEFLATE_ERR_TRUNCATED
459
507
  }
460
- const n: i32 = toI32(toI64(inf.outPos) - toI64(op))
461
- if (wantCrc !== toI64(this.crc.update(toU32(0), out, op, n))) {
508
+ const outPos: i32 = inf.outPos
509
+ // `inflateAll` wrote `out[op .. outPos)`, so `ofRange` answers no -1.
510
+ const got: i64 = this.crc.ofRange(out, op, outPos)
511
+ if (got < 0) {
512
+ return DEFLATE_ERR_WINDOW
513
+ }
514
+ if (wantCrc !== got) {
462
515
  return GZIP_ERR_CRC
463
516
  }
464
- if (wantSize !== toI64(toU32(n))) {
517
+ // ISIZE is the length mod 2^32, and this member's length is under 2^31.
518
+ if (wantSize !== toI64(outPos) - toI64(op)) {
465
519
  return GZIP_ERR_SIZE
466
520
  }
467
- op = inf.outPos
521
+ op = outPos
468
522
  p = toI32(toI64(q) + toI64(GZIP_TRAILER_SIZE))
469
523
  }
470
524
  return op
@@ -483,14 +537,14 @@ export class GzipDecompressor {
483
537
  * @param at Where in `out` the member starts.
484
538
  * @param level How hard to compress, 0 to 9; another level panics.
485
539
  * @returns The index in `out` one past the member's last byte, or
486
- * `DEFLATE_NO_ROOM`, as `GzipCompressor.compress` answers.
540
+ * `DEFLATE_NO_ROOM` or `GZIP_ERR_WINDOW`, as `GzipCompressor.compress` answers.
487
541
  */
488
542
  export const gzipCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, level: i32): i32 =>
489
543
  new GzipCompressor(level).compress(src, off, len, out, at)
490
544
 
491
545
  /**
492
546
  * `GzipDecompressor.decompress`, building the decompressor for this call
493
- * alone.
547
+ * alone. A negative `limit` panics: a caller contract, as `decompress`'s is.
494
548
  *
495
549
  * @param src The array that holds the gzip data, one member or several.
496
550
  * @param off Where in `src` the first member starts.
@@ -499,10 +553,14 @@ export const gzipCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32,
499
553
  * @param at Where in `out` the data starts.
500
554
  * @param limit The most bytes of data to accept over all the members.
501
555
  * @returns The index in `out` one past the data's last byte, or a negative
502
- * code, as `GzipDecompressor.decompress` answers.
556
+ * code (`GZIP_ERR_WINDOW` among them), as `GzipDecompressor.decompress` answers.
503
557
  */
504
- export const gzipDecompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 =>
505
- new GzipDecompressor().decompress(src, off, len, out, at, limit)
558
+ export const gzipDecompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 => {
559
+ if (limit < 0) {
560
+ panic("gzipDecompress: a negative limit")
561
+ }
562
+ return new GzipDecompressor().decompress(src, off, len, out, at, limit)
563
+ }
506
564
 
507
565
  /**
508
566
  * A streaming gzip compressor, for a body that does not fit in one buffer:
@@ -656,7 +714,8 @@ export class GzipWriter {
656
714
  * in `out` from `at` ends (often `at`: input waits until 64 KB are
657
715
  * pending), or `DEFLATE_NO_ROOM` when `out` has less than `bound(len)`
658
716
  * bytes from `at`. Writing after `finish` answers `GZIP_ERR_FINISHED`; a
659
- * window outside its array panics.
717
+ * window outside its array answers `GZIP_ERR_WINDOW`, with nothing taken
718
+ * or written.
660
719
  *
661
720
  * @param src The array that holds the next piece of the body; its bytes
662
721
  * are copied, so the caller may reuse it once `write` returns.
@@ -669,11 +728,13 @@ export class GzipWriter {
669
728
  * `DEFLATE_NO_ROOM`, when nothing was taken and nothing written; or
670
729
  * `GZIP_ERR_FINISHED`, after `finish` (nothing taken or written) or
671
730
  * when the output overran `bound(len)`, after which the bytes of `out`
672
- * from `at` mean nothing and the body cannot be completed.
731
+ * from `at` mean nothing and the body cannot be completed; or
732
+ * `GZIP_ERR_WINDOW`.
673
733
  */
674
734
  write(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
675
- gzipWindow("GzipWriter.write", src, off, len)
676
- gzipWindow("GzipWriter.write", out, at, 0)
735
+ if (!gzipInside(src, off, len) || !gzipInside(out, at, 0)) {
736
+ return GZIP_ERR_WINDOW
737
+ }
677
738
  if (this.finished) {
678
739
  return GZIP_ERR_FINISHED
679
740
  }
@@ -715,17 +776,20 @@ export class GzipWriter {
715
776
  * reader can decode every byte written so far (zlib's `Z_SYNC_FLUSH`).
716
777
  * Answers the new end of `out`, or `DEFLATE_NO_ROOM` below `bound(0)`.
717
778
  * Flushing after `finish` answers `GZIP_ERR_FINISHED`; a window outside
718
- * `out` panics.
779
+ * `out` answers `GZIP_ERR_WINDOW`, with nothing written.
719
780
  *
720
781
  * @param out The caller's array the output is written into.
721
782
  * @param at Where in `out` the output starts.
722
783
  * @returns The index in `out` one past the last byte written, so the
723
784
  * caller sends `out[at .. result)`; or `DEFLATE_NO_ROOM`, when nothing
724
785
  * was written; or `GZIP_ERR_FINISHED`, after `finish` or when the
725
- * output overran `bound(0)`, as `write` answers it.
786
+ * output overran `bound(0)`, as `write` answers it; or
787
+ * `GZIP_ERR_WINDOW`.
726
788
  */
727
789
  flush(out: u8[], at: i32): i32 {
728
- gzipWindow("GzipWriter.flush", out, at, 0)
790
+ if (!gzipInside(out, at, 0)) {
791
+ return GZIP_ERR_WINDOW
792
+ }
729
793
  if (this.finished) {
730
794
  return GZIP_ERR_FINISHED
731
795
  }
@@ -747,7 +811,7 @@ export class GzipWriter {
747
811
  * trailer; answers the new end of `out`, or `DEFLATE_NO_ROOM` below
748
812
  * `bound(0)`. `start` begins another body; until then `write`, `flush`
749
813
  * and `finish` answer `GZIP_ERR_FINISHED`. A window outside `out`
750
- * panics.
814
+ * answers `GZIP_ERR_WINDOW`, with nothing written.
751
815
  *
752
816
  * @param out The caller's array the output is written into.
753
817
  * @param at Where in `out` the output starts.
@@ -755,10 +819,12 @@ export class GzipWriter {
755
819
  * caller sends `out[at .. result)` as the end of the body; or
756
820
  * `DEFLATE_NO_ROOM`, when nothing was written and the body is not yet
757
821
  * finished; or `GZIP_ERR_FINISHED`, after `finish` or when the output
758
- * overran `bound(0)`, as `write` answers it.
822
+ * overran `bound(0)`, as `write` answers it; or `GZIP_ERR_WINDOW`.
759
823
  */
760
824
  finish(out: u8[], at: i32): i32 {
761
- gzipWindow("GzipWriter.finish", out, at, 0)
825
+ if (!gzipInside(out, at, 0)) {
826
+ return GZIP_ERR_WINDOW
827
+ }
762
828
  if (this.finished) {
763
829
  return GZIP_ERR_FINISHED
764
830
  }
@@ -924,17 +990,20 @@ export class GzipReader {
924
990
 
925
991
  /**
926
992
  * Copies as much of `src[off .. off + len)` as fits and answers how much.
927
- * A window outside `src` panics.
993
+ * A window outside `src` is `GZIP_ERR_WINDOW`, with nothing taken.
928
994
  *
929
995
  * @param src The array that holds the next input; its bytes are copied,
930
996
  * so the caller may reuse it once `feed` returns.
931
997
  * @param off Where in `src` the input starts.
932
998
  * @param len How many bytes of input are offered.
933
999
  * @returns How many bytes were taken, from 0 to `len`: the caller offers
934
- * the rest again after `read` has used some input up.
1000
+ * the rest again after `read` has used some input up; or
1001
+ * `GZIP_ERR_WINDOW`.
935
1002
  */
936
1003
  feed(src: u8[], off: i32, len: i32): i32 {
937
- gzipWindow("GzipReader.feed", src, off, len)
1004
+ if (!gzipInside(src, off, len)) {
1005
+ return GZIP_ERR_WINDOW
1006
+ }
938
1007
  const room: i32 = this.inputRoom()
939
1008
  const take: i32 = len < room ? len : room
940
1009
  copyBytes(this.input, this.inEnd, src, off, take)
@@ -1079,9 +1148,12 @@ export class GzipReader {
1079
1148
 
1080
1149
  /**
1081
1150
  * Copies up to `max` decoded bytes into `out` from `at` and answers how
1082
- * many (at least 1; `max` is at least 1), or `GZIP_NEED_INPUT` when it needs more input first,
1151
+ * many (at least 1), or `GZIP_NEED_INPUT` when it needs more input first,
1083
1152
  * `GZIP_END` once the body has ended, or a negative error, after which it
1084
- * answers that error again until `start`. A window outside `out` panics.
1153
+ * answers that error again until `start`. A window outside `out` is
1154
+ * `GZIP_ERR_WINDOW`, with nothing copied and the reader unchanged. A
1155
+ * `max` below 1 panics: a caller contract, so a constant `max`, or one
1156
+ * behind the caller's own `max >= 1` test, proves it at the call.
1085
1157
  *
1086
1158
  * @param out The caller's array the decoded bytes are copied into.
1087
1159
  * @param at Where in `out` they start.
@@ -1090,13 +1162,16 @@ export class GzipReader {
1090
1162
  * @returns A count, not a position: the bytes are `out[at .. at + result)`.
1091
1163
  * Or `GZIP_NEED_INPUT` (0), `GZIP_END` (-100), or a negative error code
1092
1164
  * (`GZIP_ERR_*`, `DEFLATE_ERR_TRUNCATED`, `DEFLATE_ERR_OUTPUT_LIMIT` or
1093
- * another `DEFLATE_ERR_*`), which is final.
1165
+ * another `DEFLATE_ERR_*`), which is final; or `GZIP_ERR_WINDOW`,
1166
+ * which is not.
1094
1167
  */
1095
1168
  read(out: u8[], at: i32, max: i32): i32 {
1096
- gzipWindow("GzipReader.read", out, at, max)
1097
1169
  if (max < 1) {
1098
1170
  panic("GzipReader.read: room for no bytes")
1099
1171
  }
1172
+ if (!gzipInside(out, at, max)) {
1173
+ return GZIP_ERR_WINDOW
1174
+ }
1100
1175
  if (this.phase < 0) {
1101
1176
  return this.phase
1102
1177
  }
@@ -23,11 +23,12 @@
23
23
  *
24
24
  * Written from RFC 1950.
25
25
  */
26
- import { adler32 } from "nish/compress/checksum"
26
+ import { adler32, adler32Range } from "nish/compress/checksum"
27
27
  import {
28
28
  DEFLATE_ERR_OUTPUT_LIMIT,
29
29
  DEFLATE_ERR_TRAILING,
30
30
  DEFLATE_ERR_TRUNCATED,
31
+ DEFLATE_ERR_WINDOW,
31
32
  DEFLATE_NO_ROOM,
32
33
  Deflater,
33
34
  INFLATE_DONE,
@@ -115,7 +116,13 @@ export class ZlibCompressor {
115
116
  }
116
117
  // `out`'s length is read again after the checksum for the trailer's
117
118
  // stores: the prover forgets an array's length across a call.
118
- const a: u32 = adler32(src, off, len)
119
+ const a64: i64 = adler32(src, off, len)
120
+ if (a64 < 0) {
121
+ // Cannot happen: `zlibWindow` checked this window above. The prover
122
+ // does not carry that across the calls between, so it is answered.
123
+ return DEFLATE_ERR_WINDOW
124
+ }
125
+ const a: u32 = toU32(a64)
119
126
  const e1: i32 = toI32(toI64(end) + 1)
120
127
  const e2: i32 = toI32(toI64(end) + 2)
121
128
  const e3: i32 = toI32(toI64(end) + 3)
@@ -145,7 +152,7 @@ export class ZlibDecompressor {
145
152
  * `at`, writing at most `limit` bytes and never past `out`'s end, and
146
153
  * answers where the output ends, or a negative `ZLIB_ERR_*` or
147
154
  * `DEFLATE_ERR_*`. A window outside its array, or a negative `limit`,
148
- * panics. Allocates nothing.
155
+ * panics: `decompressRange` answers those instead. Allocates nothing.
149
156
  *
150
157
  * @param src The array that holds the zlib stream.
151
158
  * @param off Where in `src` the stream starts.
@@ -164,17 +171,47 @@ export class ZlibDecompressor {
164
171
  * nothing.
165
172
  */
166
173
  decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 {
167
- zlibWindow("ZlibDecompressor.decompress", src, off, len)
168
- zlibWindow("ZlibDecompressor.decompress", out, at, 0)
169
- if (limit < 0) {
170
- panic("ZlibDecompressor.decompress: a negative limit")
174
+ // One test for every argument, so the call's misuse is one panic site.
175
+ if (
176
+ off < 0 ||
177
+ len < 0 ||
178
+ off > toI32(src.length) - len ||
179
+ at < 0 ||
180
+ at > toI32(out.length) ||
181
+ limit < 0
182
+ ) {
183
+ panic("ZlibDecompressor.decompress: a window outside its array, or a negative limit")
184
+ }
185
+ return this.decompressRange(src, off, toI32(toI64(off) + toI64(len)), out, at, limit)
186
+ }
187
+
188
+ /**
189
+ * `decompress` over `src[off .. end)`, a range given by its two ends, that
190
+ * answers a misuse rather than panicking: a range outside `src`, an output
191
+ * start outside `out` or a negative `limit` is `DEFLATE_ERR_WINDOW`, with
192
+ * nothing read or written. `nish/net/http-compress`'s decoder calls this
193
+ * one, so a body it decodes reaches no panic.
194
+ *
195
+ * @internal Called by std/net/http-compress.ts's body decoder.
196
+ * @param src The array that holds the zlib stream.
197
+ * @param off Where in `src` the stream starts.
198
+ * @param end One past the stream's last byte.
199
+ * @param out The caller's array the data is written into.
200
+ * @param at Where in `out` the data starts.
201
+ * @param limit The most bytes of data to accept.
202
+ * @returns The index in `out` one past the data's last byte, or a negative
203
+ * code, as `decompress` answers; or `DEFLATE_ERR_WINDOW`.
204
+ */
205
+ decompressRange(src: u8[], off: i32, end: i32, out: u8[], at: i32, limit: i32): i32 {
206
+ if (off < 0 || off > end || end > toI32(src.length) || at < 0 || at > toI32(out.length) || limit < 0) {
207
+ return DEFLATE_ERR_WINDOW
171
208
  }
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
209
  const n: i32 = toI32(src.length)
210
+ // Taken in 64 bits: `off` is at most the input's end, but the prover
211
+ // cannot see that `off + 2` stays in range from the test below.
176
212
  const second: i32 = toI32(toI64(off) + 1)
177
- if (len < 2 || off < 0 || off >= n || second < 0 || second >= n) {
213
+ const start: i32 = toI32(toI64(off) + 2)
214
+ if (end - off < 2 || off >= n || second < 0 || second >= n) {
178
215
  return DEFLATE_ERR_TRUNCATED
179
216
  }
180
217
  const cmf: i32 = toI32(src[off])
@@ -191,8 +228,7 @@ export class ZlibDecompressor {
191
228
  }
192
229
  const inf: Inflater = this.inflater
193
230
  inf.reset()
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)
231
+ const r: i32 = inf.inflateAll(src, start, end, out, at, outEnd)
196
232
  if (r === INFLATE_NEED_OUTPUT) {
197
233
  return DEFLATE_ERR_OUTPUT_LIMIT
198
234
  }
@@ -217,13 +253,19 @@ export class ZlibDecompressor {
217
253
  return DEFLATE_ERR_TRUNCATED
218
254
  }
219
255
  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) {
256
+ const outPos: i32 = inf.outPos
257
+ // `inflateAll` wrote `out[at .. outPos)`, so `adler32Range` answers no -1.
258
+ const got: i64 = adler32Range(out, at, outPos)
259
+ if (got < 0) {
260
+ return DEFLATE_ERR_WINDOW
261
+ }
262
+ if (got !== toI64(want)) {
221
263
  return ZLIB_ERR_ADLER
222
264
  }
223
265
  if (toI64(q) + 4 !== toI64(end)) {
224
266
  return DEFLATE_ERR_TRAILING
225
267
  }
226
- return inf.outPos
268
+ return outPos
227
269
  }
228
270
  }
229
271
 
package/std/crypto/aes.ts CHANGED
@@ -279,6 +279,11 @@ const aesUnpack = (buf: u8[], q: u64[]): void => {
279
279
  * are the circuit's XNOR gates, the affine constant 0x63.
280
280
  */
281
281
  const aesSbox = (q: u64[]): void => {
282
+ // Every state is `new Array<u64>(8)`, so this cannot happen; it lets the
283
+ // compiler prove the literal indices below.
284
+ if (q.length < 8) {
285
+ return
286
+ }
282
287
  const x0: u64 = q[7]
283
288
  const x1: u64 = q[6]
284
289
  const x2: u64 = q[5]
@@ -436,6 +441,11 @@ const aesShiftPlane = (x: u64): u64 =>
436
441
 
437
442
  /** ShiftRows (FIPS 197 §5.1.2) on the whole state. */
438
443
  const aesShiftRows = (q: u64[]): void => {
444
+ // Every state is `new Array<u64>(8)`, so this cannot happen; it lets the
445
+ // compiler prove the literal indices below.
446
+ if (q.length < 8) {
447
+ return
448
+ }
439
449
  // The last plane first: its checked read proves the other seven.
440
450
  const p7: u64 = q[7]
441
451
  q[0] = aesShiftPlane(q[0])
@@ -464,6 +474,11 @@ const aesRowAfterNext = (x: u64): u64 =>
464
474
  * for the reduction by x^8 + x^4 + x^3 + x + 1.
465
475
  */
466
476
  const aesMixColumns = (q: u64[]): void => {
477
+ // Every state is `new Array<u64>(8)`, so this cannot happen; it lets the
478
+ // compiler prove the literal indices below.
479
+ if (q.length < 8) {
480
+ return
481
+ }
467
482
  // The last plane first: its checked read proves the other seven.
468
483
  const n7: u64 = aesNextRow(q[7])
469
484
  const n0: u64 = aesNextRow(q[0])
@@ -499,6 +514,12 @@ const aesMixColumns = (q: u64[]): void => {
499
514
  */
500
515
  const aesAddRoundKey = (q: u64[], rk: u64[], at: i32): void => {
501
516
  const base: i32 = at & 127
517
+ // Every state is `new Array<u64>(8)` and every `at` is a round of the
518
+ // key's own schedule, so this cannot happen; it lets the compiler prove the
519
+ // indices below.
520
+ if (q.length < 8 || base > toI32(rk.length) - 8) {
521
+ return
522
+ }
502
523
  // The first and last key words and the last plane first: their checked
503
524
  // reads prove every other index below.
504
525
  const k0: u64 = rk[base]
@@ -517,8 +538,9 @@ const aesAddRoundKey = (q: u64[], rk: u64[], at: i32): void => {
517
538
  /**
518
539
  * One full middle round of the cipher on bitsliced state — SubBytes,
519
540
  * ShiftRows, MixColumns, then the round key of eight words from `at` — for
520
- * four blocks at once. Straight-line code with no branch and no index but
521
- * `at`, which is a round number times eight. Exported so that
541
+ * four blocks at once. No branch but each step's test of the state's and the
542
+ * keys' lengths, which no secret moves, and no index but `at`, which is a
543
+ * round number times eight. Exported so that
522
544
  * `tests/link/crypto_aes` can hold the copy the disassembly check reads to
523
545
  * this one; a caller has no other use for it.
524
546
  *
@@ -691,6 +713,20 @@ const aesScheduleInto = (key: u8[], rounds: i32, rk: u64[]): void => {
691
713
  */
692
714
  const aesLoad64 = (data: u8[], at: i32, end: i32): u64 => {
693
715
  const length: i32 = toI32(data.length)
716
+ // A whole block, which every block but a partial last one is: eight loads
717
+ // and no per-byte test.
718
+ if (at >= 0 && end >= 8 && at <= end - 8 && at <= length - 8) {
719
+ return (
720
+ (toU64(data[at]) << toU64(56)) |
721
+ (toU64(data[at + 1]) << toU64(48)) |
722
+ (toU64(data[at + 2]) << toU64(40)) |
723
+ (toU64(data[at + 3]) << toU64(32)) |
724
+ (toU64(data[at + 4]) << toU64(24)) |
725
+ (toU64(data[at + 5]) << toU64(16)) |
726
+ (toU64(data[at + 6]) << toU64(8)) |
727
+ toU64(data[at + 7])
728
+ )
729
+ }
694
730
  let v: u64 = 0
695
731
  for (let i: i32 = 0; i < 8; i++) {
696
732
  const from: i32 = at + i
@@ -947,17 +983,22 @@ const ghashMul32 = (x: u64, y: u64): u64 => {
947
983
  * paper cited in the header).
948
984
  *
949
985
  * The 128 × 128 product is Karatsuba twice over: three 64 × 64 products, each
950
- * of three 32 × 32 ones from `ghashMul32`. Straight-line code; exported, like
951
- * `aesBitslicedRound`, so the disassembly check's copy can be held to it.
986
+ * of three 32 × 32 ones from `ghashMul32`. Straight-line code past one test of
987
+ * `y`'s length, which no secret moves; exported, like `aesBitslicedRound`, so
988
+ * the disassembly check's copy can be held to it.
952
989
  *
953
- * @param y The running GHASH value, two words, multiplied in place.
990
+ * @param y The running GHASH value, two words, multiplied in place; a shorter
991
+ * array is left as it was.
954
992
  * @param hHi The high half of H.
955
993
  * @param hLo The low half of H.
956
994
  */
957
995
  export const ghashMultiply = (y: u64[], hHi: u64, hLo: u64): void => {
958
996
  const low32: u64 = (toU64(1) << toU64(32)) - toU64(1)
959
997
  const thirtyTwo: u64 = toU64(32)
960
- // The second word first: its checked read proves the first.
998
+ // Every caller's state is two words; a shorter one is left as it was.
999
+ if (y.length < 2) {
1000
+ return
1001
+ }
961
1002
  const a0: u64 = y[1]
962
1003
  const a1: u64 = y[0]
963
1004
  const a2: u64 = a1 ^ a0
@@ -1022,7 +1063,7 @@ export const ghashMultiply = (y: u64[], hHi: u64, hLo: u64): void => {
1022
1063
  * compiler may replace that call with a runtime kernel.
1023
1064
  */
1024
1065
  const ghashBlocksInto = (y: u64[], hHi: u64, hLo: u64, _rk: u64[], data: u8[], length: i32): void => {
1025
- if (toI32(y.length) < 2 || length < 0) {
1066
+ if (y.length < 2 || length < 0) {
1026
1067
  return
1027
1068
  }
1028
1069
  // Counted in blocks rather than stepped by 16, so no offset is ever formed
@@ -1048,7 +1089,7 @@ const ghashUpdate = (y: u64[], key: AesKey, data: u8[], length: i32): void => {
1048
1089
  const h: u64[] = key.h
1049
1090
  if (
1050
1091
  toI32(h.length) < 2 ||
1051
- toI32(y.length) < 2 ||
1092
+ y.length < 2 ||
1052
1093
  length < 0 ||
1053
1094
  length > toI32(data.length) ||
1054
1095
  !aesKeyUsable(key)
@@ -1259,7 +1300,8 @@ export const aesGcmSeal = (key: AesKey, iv: u8[], aad: u8[], plaintext: u8[]): u
1259
1300
  const hLo: u64 = h[1]
1260
1301
  const hHi: u64 = h[0]
1261
1302
  const j0: u8[] = aesGcmJ0(key, hHi, hLo, iv)
1262
- const out: u8[] = new Array<u8>(length + AES_GCM_TAG_SIZE)
1303
+ const sealed: i32 = length + AES_GCM_TAG_SIZE
1304
+ const out: u8[] = new Array<u8>(sealed)
1263
1305
  aesGcmCtr(key, j0, plaintext, out, length)
1264
1306
  const tag: u64[] = new Array<u64>(2)
1265
1307
  aesGcmTag(tag, key, hHi, hLo, j0, aad, out, length)