@amritk/nish-x86_64-linux 0.49.1 → 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 (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
@@ -374,15 +374,31 @@ class Sha512Engine {
374
374
  }
375
375
  }
376
376
 
377
- /** @internal Called by `Sha512` and `Sha384`, which hold the engine. */
378
- absorb(data: u8[], off: i32, len: i32): void {
379
- if (this.finished) {
380
- panic("sha512: update after digest")
381
- }
377
+ /**
378
+ * Absorbs `data[off .. off + len)`: `false`, absorbing nothing, for a
379
+ * window outside `data` or an engine `finish` has ended.
380
+ *
381
+ * @internal Called by `Sha512` and `Sha384`, which hold the engine.
382
+ */
383
+ absorb(data: u8[], off: i32, len: i32): boolean {
382
384
  const size: i32 = toI32(data.length)
383
- if (off < 0 || len < 0 || off > size - len) {
384
- panic("sha512: the window is outside the buffer")
385
+ if (this.finished || off < 0 || len < 0 || off > size - len) {
386
+ return false
385
387
  }
388
+ this.absorbWindow(data, off, len)
389
+ return true
390
+ }
391
+
392
+ /**
393
+ * The body of `absorb` past its tests: absorbs `data[off .. off + len)`,
394
+ * a window its caller has checked lies inside `data`. Each loop's condition
395
+ * beyond its count bounds its own indices, so a bad window reads nothing
396
+ * outside `data`; it would only miscount, which is why every caller checks.
397
+ *
398
+ * @internal Called by `absorb` and `absorbWhole`.
399
+ */
400
+ absorbWindow(data: u8[], off: i32, len: i32): void {
401
+ const size: i32 = toI32(data.length)
386
402
  this.count = this.count + toU64(len)
387
403
  const block: u8[] = this.block
388
404
  const room: i32 = toI32(block.length)
@@ -421,19 +437,44 @@ class Sha512Engine {
421
437
 
422
438
  /**
423
439
  * Absorbs all of `data`. An array of up to 2^31 - 1 bytes goes through
424
- * `absorb`. A longer one (possible under `--number-mode f64`, where `toI32`
425
- * of its length would saturate and leave a prefix) is walked with a `number`
426
- * offset instead, a block at a time through `block`, so it is absorbed
427
- * whole. After `finish` it panics, as `absorb` does.
440
+ * `absorbWindow`. A longer one (possible under `--number-mode f64`, where
441
+ * `toI32` of its length would saturate and leave a prefix) is walked with a
442
+ * `number` offset instead, a block at a time through `block`, so it is
443
+ * absorbed whole. After `finish` it answers `false` and absorbs nothing,
444
+ * as `absorb` does; otherwise `true`.
445
+ *
446
+ * @internal Called by `nish/crypto/hmac` and `nish/crypto/hkdf`.
447
+ */
448
+ absorbAll(data: u8[]): boolean {
449
+ if (this.finished) {
450
+ return false
451
+ }
452
+ this.absorbWhole(data)
453
+ return true
454
+ }
455
+
456
+ /**
457
+ * The same answer as `absorbAll`, kept for callers written against it.
428
458
  *
429
- * @internal Called by `sha512`, `sha384` and `nish/crypto/hmac`.
459
+ * @internal Called by `nish/crypto/hkdf`.
430
460
  */
431
- absorbAll(data: u8[]): void {
461
+ tryAbsorbAll(data: u8[]): boolean {
462
+ return this.absorbAll(data)
463
+ }
464
+
465
+ /**
466
+ * `absorbAll` past its finished test, for a caller holding an engine that
467
+ * `finish` cannot have ended: one it made itself and has not digested
468
+ * (`sha512`, `sha384`, the HMAC key and pad blocks, a tag's inner digest).
469
+ * The whole array is the window, so there is no window to get wrong.
470
+ *
471
+ * @internal Called by `sha512`, `sha384`, `nish/crypto/hmac`,
472
+ * `nish/crypto/hkdf` and `nish/crypto/ed25519`.
473
+ */
474
+ absorbWhole(data: u8[]): void {
432
475
  const n: number = data.length
433
- if (this.finished || n <= 2147483647) {
434
- // A finished engine panics in `absorb` before it reads the window, so
435
- // the saturated length of a longer array is never used.
436
- this.absorb(data, OFFSET_ZERO, toI32(n))
476
+ if (n <= 2147483647) {
477
+ this.absorbWindow(data, OFFSET_ZERO, toI32(n))
437
478
  return
438
479
  }
439
480
  this.count += toU64(toI64(n))
@@ -482,13 +523,34 @@ class Sha512Engine {
482
523
  /**
483
524
  * Pads (§5.1.2), compresses what is left, and writes the first
484
525
  * `out.length / 8` words of H(N) (§6.4.2, §6.5), at most eight, into `out`.
526
+ * A second call writes the same words: the padding is absorbed once.
485
527
  *
486
528
  * @internal Called by `Sha512` and `Sha384`, which hold the engine.
487
529
  */
488
530
  finish(out: u8[]): void {
489
- if (this.finished) {
490
- panic("sha512: digest taken twice")
531
+ if (!this.finished) {
532
+ this.pad()
491
533
  }
534
+ // Both sizes are whole words, so the digest is the first `out.length / 8`.
535
+ const state: u64[] = this.state
536
+ const size: i32 = toI32(out.length)
537
+ let p: i32 = 0
538
+ for (let i: i32 = 0; i < 8 && i < toI32(state.length) && p < size - 7; i++) {
539
+ const word: u64 = state[i]
540
+ out[p] = toU8(word >> 56)
541
+ out[p + 1] = toU8(word >> 48)
542
+ out[p + 2] = toU8(word >> 40)
543
+ out[p + 3] = toU8(word >> 32)
544
+ out[p + 4] = toU8(word >> 24)
545
+ out[p + 5] = toU8(word >> 16)
546
+ out[p + 6] = toU8(word >> 8)
547
+ out[p + 7] = toU8(word)
548
+ p += 8
549
+ }
550
+ }
551
+
552
+ /** Appends the padding and the length (§5.1.2) and compresses them: `finish`'s first call. */
553
+ private pad(): void {
492
554
  this.finished = true
493
555
  const block = this.block
494
556
  const room: i32 = toI32(block.length)
@@ -514,29 +576,14 @@ class Sha512Engine {
514
576
  shift += 8
515
577
  }
516
578
  this.compress(block, OFFSET_ZERO)
517
- // Both sizes are whole words, so the digest is the first `out.length / 8`.
518
- const state: u64[] = this.state
519
- const size: i32 = toI32(out.length)
520
- let p: i32 = 0
521
- for (let i: i32 = 0; i < 8 && i < toI32(state.length) && p < size - 7; i++) {
522
- const word: u64 = state[i]
523
- out[p] = toU8(word >> 56)
524
- out[p + 1] = toU8(word >> 48)
525
- out[p + 2] = toU8(word >> 40)
526
- out[p + 3] = toU8(word >> 32)
527
- out[p + 4] = toU8(word >> 24)
528
- out[p + 5] = toU8(word >> 16)
529
- out[p + 6] = toU8(word >> 8)
530
- out[p + 7] = toU8(word)
531
- p += 8
532
- }
533
579
  }
534
580
  }
535
581
 
536
582
  /**
537
583
  * A streaming SHA-512 hasher. `update` takes a window `(data, off, len)`;
538
- * `digest` answers a fresh 64-byte array, after which the hasher is spent and
539
- * another `update` or `digest` panics.
584
+ * `digest` answers a fresh 64-byte array and ends the message: a later
585
+ * `update` answers `false` and absorbs nothing, and a later `digest` answers
586
+ * the same digest.
540
587
  */
541
588
  export class Sha512 {
542
589
  /**
@@ -555,15 +602,34 @@ export class Sha512 {
555
602
  }
556
603
 
557
604
  /**
558
- * Absorbs `data[off .. off + len)`. A window outside `data`, or an update
559
- * after `digest`, panics.
605
+ * Absorbs `data[off .. off + len)`. A window outside `data`, or a call
606
+ * after `digest`, answers `false` and absorbs nothing rather than hashing a
607
+ * short read. Check it, since a digest over the wrong bytes looks exactly
608
+ * like a digest over the right ones.
609
+ *
610
+ * @param data The buffer holding the window; it is read, not kept.
611
+ * @param off Where the window starts in `data`.
612
+ * @param len How many bytes to absorb; 0 absorbs nothing.
613
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
614
+ * for a window outside `data` or a hasher already digested.
615
+ */
616
+ update(data: u8[], off: i32, len: i32): boolean {
617
+ return this.engine.absorb(data, off, len)
618
+ }
619
+
620
+ /**
621
+ * The same answer as `update`, kept for callers written against it: a
622
+ * window outside `data`, or an update after `digest`, answers `false` and
623
+ * absorbs nothing.
560
624
  *
561
625
  * @param data The buffer holding the window; it is read, not kept.
562
626
  * @param off Where the window starts in `data`.
563
627
  * @param len How many bytes to absorb; 0 absorbs nothing.
628
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
629
+ * for a window outside `data` or a hasher already digested.
564
630
  */
565
- update(data: u8[], off: i32, len: i32): void {
566
- this.engine.absorb(data, off, len)
631
+ tryUpdate(data: u8[], off: i32, len: i32): boolean {
632
+ return this.engine.absorb(data, off, len)
567
633
  }
568
634
 
569
635
  /**
@@ -581,7 +647,7 @@ export class Sha512 {
581
647
 
582
648
  /**
583
649
  * Pads the message (§5.1.2) and answers its digest. Ends the computation: a
584
- * second `digest` panics.
650
+ * second `digest` answers the same digest.
585
651
  *
586
652
  * @returns A fresh 64-byte digest, which the caller owns.
587
653
  */
@@ -613,15 +679,34 @@ export class Sha384 {
613
679
  }
614
680
 
615
681
  /**
616
- * Absorbs `data[off .. off + len)`. A window outside `data`, or an update
617
- * after `digest`, panics.
682
+ * Absorbs `data[off .. off + len)`. A window outside `data`, or a call
683
+ * after `digest`, answers `false` and absorbs nothing rather than hashing a
684
+ * short read. Check it, since a digest over the wrong bytes looks exactly
685
+ * like a digest over the right ones.
686
+ *
687
+ * @param data The buffer holding the window; it is read, not kept.
688
+ * @param off Where the window starts in `data`.
689
+ * @param len How many bytes to absorb; 0 absorbs nothing.
690
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
691
+ * for a window outside `data` or a hasher already digested.
692
+ */
693
+ update(data: u8[], off: i32, len: i32): boolean {
694
+ return this.engine.absorb(data, off, len)
695
+ }
696
+
697
+ /**
698
+ * The same answer as `update`, kept for callers written against it: a
699
+ * window outside `data`, or an update after `digest`, answers `false` and
700
+ * absorbs nothing.
618
701
  *
619
702
  * @param data The buffer holding the window; it is read, not kept.
620
703
  * @param off Where the window starts in `data`.
621
704
  * @param len How many bytes to absorb; 0 absorbs nothing.
705
+ * @returns `true` once the window is absorbed; `false`, absorbing nothing,
706
+ * for a window outside `data` or a hasher already digested.
622
707
  */
623
- update(data: u8[], off: i32, len: i32): void {
624
- this.engine.absorb(data, off, len)
708
+ tryUpdate(data: u8[], off: i32, len: i32): boolean {
709
+ return this.engine.absorb(data, off, len)
625
710
  }
626
711
 
627
712
  /**
@@ -639,7 +724,8 @@ export class Sha384 {
639
724
 
640
725
  /**
641
726
  * Pads the message (§5.1.2) and answers its digest, the first 48 bytes of the
642
- * final hash value (§6.5). Ends the computation: a second `digest` panics.
727
+ * final hash value (§6.5). Ends the computation: a second `digest` answers
728
+ * the same digest.
643
729
  *
644
730
  * @returns A fresh 48-byte digest, which the caller owns.
645
731
  */
@@ -660,7 +746,7 @@ export class Sha384 {
660
746
  */
661
747
  export const sha512 = (data: u8[]): u8[] => {
662
748
  const hasher = new Sha512()
663
- hasher.engine.absorbAll(data)
749
+ hasher.engine.absorbWhole(data)
664
750
  return hasher.digest()
665
751
  }
666
752
 
@@ -673,6 +759,6 @@ export const sha512 = (data: u8[]): u8[] => {
673
759
  */
674
760
  export const sha384 = (data: u8[]): u8[] => {
675
761
  const hasher = new Sha384()
676
- hasher.engine.absorbAll(data)
762
+ hasher.engine.absorbWhole(data)
677
763
  return hasher.digest()
678
764
  }
@@ -97,6 +97,7 @@
97
97
  */
98
98
 
99
99
  import { Secret, exposeWith, secret, wipe } from "nish:secret"
100
+ import { wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
100
101
 
101
102
  /** The length in bytes of a scalar, a u-coordinate and an X25519 output. */
102
103
  export const X25519_SIZE: i32 = 32
@@ -105,12 +106,12 @@ export const X25519_SIZE: i32 = 32
105
106
  const X25519_A24: i64 = 121665
106
107
 
107
108
  /**
108
- * `a + b` on limbs: the `u64` sum, read back as `i64`; `f25519Sub64` and
109
- * `f25519Mul64` are its difference and product. Every bound this module states
110
- * keeps the true result inside `i64`, so the answer is the signed one bit for
111
- * bit; the arithmetic is unsigned because signed arithmetic is checked for an
112
- * overflow, and a check is a branch on a secret limb. Each is one
113
- * instruction once LLVM inlines it.
109
+ * `a + b` on limbs, through `wrappingAdd`; `f25519Sub64` and `f25519Mul64` are
110
+ * its difference and product, through `wrappingSub` and `wrappingMul`. Every
111
+ * bound this module states keeps the true result inside `i64`, so the wrap
112
+ * never happens and the answer is the signed one; the wrapping call is there
113
+ * because plain signed arithmetic is checked for an overflow, and a check is a
114
+ * branch on a secret limb. Each is one instruction once LLVM inlines it.
114
115
  *
115
116
  * @param a The first limb.
116
117
  * @param b The second limb.
@@ -118,9 +119,9 @@ const X25519_A24: i64 = 121665
118
119
  *
119
120
  * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
120
121
  */
121
- export const f25519Add64 = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
122
+ export const f25519Add64 = (a: i64, b: i64): i64 => wrappingAdd(a, b)
122
123
  /**
123
- * `a - b` on limbs, unsigned and read back as `i64`, as `f25519Add64` explains.
124
+ * `a - b` on limbs, through `wrappingSub`, as `f25519Add64` explains.
124
125
  *
125
126
  * @param a The limb subtracted from.
126
127
  * @param b The limb subtracted.
@@ -128,9 +129,9 @@ export const f25519Add64 = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
128
129
  *
129
130
  * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
130
131
  */
131
- export const f25519Sub64 = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
132
+ export const f25519Sub64 = (a: i64, b: i64): i64 => wrappingSub(a, b)
132
133
  /**
133
- * `a * b` on limbs, unsigned and read back as `i64`, as `f25519Add64` explains.
134
+ * `a * b` on limbs, through `wrappingMul`, as `f25519Add64` explains.
134
135
  *
135
136
  * @param a The first limb.
136
137
  * @param b The second limb.
@@ -138,7 +139,7 @@ export const f25519Sub64 = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
138
139
  *
139
140
  * @internal Imported by `nish/crypto/ed25519`, which works over the same field.
140
141
  */
141
- export const f25519Mul64 = (a: i64, b: i64): i64 => toI64(toU64(a) * toU64(b))
142
+ export const f25519Mul64 = (a: i64, b: i64): i64 => wrappingMul(a, b)
142
143
 
143
144
  /** The width of limb `i`: 26 bits for an even limb, 25 for an odd one. */
144
145
  const f25519Width = (i: i32): i64 => {
@@ -111,6 +111,7 @@
111
111
  import { Secret, secret, wipe } from "nish:secret"
112
112
  import { base64StdPadDecode, base64StdPadEncode } from "nish/crypto/base64url"
113
113
  import { p256PublicKey, p256SignSha256, p256VerifySha256 } from "nish/crypto/p256"
114
+ import { rsaParsePrivateKey } from "nish/crypto/rsa-sign"
114
115
  import { sha256 } from "nish/crypto/sha256"
115
116
 
116
117
  /** The longest validity `x509MintSelfSigned` writes, in days: WebTransport's two weeks. */
@@ -398,6 +399,28 @@ const x509DerRead = (der: u8[], at: i32, limit: i32): X509DerElement | null => {
398
399
  return e
399
400
  }
400
401
 
402
+ /**
403
+ * `a + b` when neither is negative and the sum is at most `limit`, else `-1`:
404
+ * an offset past a length the parser has checked. It is summed as differences
405
+ * of ordered values that are not negative, which src/bounds.ts proves where it
406
+ * cannot carry `b <= limit - a` into `a + b`; past the first two checks, the
407
+ * others are always false and only state that order for the proof.
408
+ */
409
+ const x509Sum = (a: i32, b: i32, limit: i32): i32 => {
410
+ if (a < 0 || b < 0 || a > limit) {
411
+ return -1
412
+ }
413
+ const room: i32 = limit - a
414
+ if (b > room) {
415
+ return -1
416
+ }
417
+ const gap: i32 = room - b
418
+ if (gap < 0 || gap > limit) {
419
+ return -1
420
+ }
421
+ return limit - gap
422
+ }
423
+
401
424
  /**
402
425
  * `x509DerRead` into `into`, answering whether it read one: what a loop uses
403
426
  * so a pass allocates nothing it leaves behind. `into` is unspecified on `false`.
@@ -417,13 +440,14 @@ const x509DerReadInto = (der: u8[], at: i32, limit: i32, into: X509DerElement):
417
440
  if (length >= 0x80) {
418
441
  const count: i32 = length & 0x7f
419
442
  // 0x80 alone is BER's indefinite length, which DER forbids.
420
- if (count < 1 || count > 4 || count > limit - start) {
443
+ // `start < der.length` follows from the first check; spelled out because
444
+ // it is what proves the sum below.
445
+ if (count < 1 || count > 4 || count > limit - start || start >= toI32(der.length)) {
421
446
  return false
422
447
  }
423
- // Unsigned: `count <= limit - start`, so the sum cannot wrap.
424
- const stop: i32 = toI32(toU32(start) + toU32(count))
425
- // `start < stop <= limit <= der.length`, spelled out for the same reason.
426
- if (stop > toI32(der.length) || start >= toI32(der.length)) {
448
+ const stop: i32 = x509Sum(start, count, limit)
449
+ // `stop <= limit <= der.length`, spelled out for the same reason.
450
+ if (stop < 0 || stop > toI32(der.length)) {
427
451
  return false
428
452
  }
429
453
  if (toI32(der[start]) === 0 || (count === 4 && toI32(der[start]) >= 0x80)) {
@@ -438,15 +462,15 @@ const x509DerReadInto = (der: u8[], at: i32, limit: i32, into: X509DerElement):
438
462
  }
439
463
  start = stop
440
464
  }
441
- // Unsigned: `start <= limit` on both paths, so the difference cannot wrap.
442
- if (toU32(length) > toU32(limit) - toU32(start)) {
465
+ // `-1` for a length past the limit, or one read from four octets as negative.
466
+ const end: i32 = x509Sum(start, length, limit)
467
+ if (end < 0) {
443
468
  return false
444
469
  }
445
470
  into.tag = tag
446
471
  into.at = at
447
472
  into.start = start
448
- // Unsigned: `length <= limit - start`, so the sum cannot wrap.
449
- into.end = toI32(toU32(start) + toU32(length))
473
+ into.end = end
450
474
  return true
451
475
  }
452
476
 
@@ -485,8 +509,7 @@ const x509DerSize = (der: u8[], e: X509DerElement): i32 => {
485
509
  if (start < 0 || end < start || end > toI32(der.length)) {
486
510
  return -1
487
511
  }
488
- // Unsigned: `start <= end`, so the difference cannot wrap.
489
- return toI32(toU32(end) - toU32(start))
512
+ return end - start
490
513
  }
491
514
 
492
515
  /**
@@ -499,8 +522,7 @@ const x509DerOctet = (der: u8[], e: X509DerElement, k: i32): i32 => {
499
522
  if (k < 0 || k >= n) {
500
523
  return -1
501
524
  }
502
- // Unsigned: `k < end - start`, so the sum cannot wrap.
503
- const i: i32 = toI32(toU32(e.start) + toU32(k))
525
+ const i: i32 = x509Sum(e.start, k, toI32(der.length))
504
526
  if (i < 0 || i >= toI32(der.length)) {
505
527
  return -1
506
528
  }
@@ -615,8 +637,13 @@ const x509DerBitStringOctets = (der: u8[], e: X509DerElement): u8[] | null => {
615
637
  if (e.tag !== X509_TAG_BIT_STRING || !x509DerBitStringSound(der, e) || x509DerOctet(der, e, 0) !== 0) {
616
638
  return null
617
639
  }
618
- // Unsigned: a sound BIT STRING has an octet at `e.start`, so the sum cannot wrap.
619
- return x509DerSlice(der, toI32(toU32(e.start) + 1), e.end)
640
+ const start: i32 = e.start
641
+ // A sound BIT STRING has an octet at `start`, so it is below the DER's length;
642
+ // spelled out because that is what proves `start + 1`.
643
+ if (start < 0 || start >= toI32(der.length)) {
644
+ return null
645
+ }
646
+ return x509DerSlice(der, start + 1, e.end)
620
647
  }
621
648
 
622
649
  /**
@@ -666,8 +693,13 @@ const x509DerTime = (der: u8[], e: X509DerElement): i64 => {
666
693
  if (e.tag === X509_TAG_UTC_TIME && n === 13) {
667
694
  year = x509DerDigits(der, e, 0, 2)
668
695
  if (year >= 0) {
669
- // Unsigned: two digits, so the sum cannot wrap.
670
- year = toI32(toU32(year) + (year >= 50 ? 1900 : 2000))
696
+ // The mask keeps what two digits hold (at most 99) and lets
697
+ // src/bounds.ts prove the sum.
698
+ if (year >= 50) {
699
+ year = (year & 127) + 1900
700
+ } else {
701
+ year = (year & 127) + 2000
702
+ }
671
703
  }
672
704
  at = 2
673
705
  } else if (e.tag === X509_TAG_GENERALIZED_TIME && n === 15) {
@@ -697,14 +729,13 @@ const x509DerTime = (der: u8[], e: X509DerElement): i64 => {
697
729
  // itself. 1969 * 365 + 492 - 19 + 4 is the same count for 1970.
698
730
  const y: i32 = year - 1
699
731
  const years: i32 = 365 * y + y / 4 - y / 100 + y / 400 - 719162
700
- // Unsigned: at most 365 days, so the sum cannot wrap.
701
- let yday: u32 = toU32(day - 1)
732
+ // At most 365 days, which the mask keeps: it lets src/bounds.ts prove the
733
+ // sum, as `years` is bounded by the year's checks above.
734
+ let yday: i32 = day - 1
702
735
  for (let m: i32 = 1; m < month; m++) {
703
- yday = yday + toU32(x509DaysInMonth(year, m))
736
+ yday = (yday & 511) + (x509DaysInMonth(year, m) & 31)
704
737
  }
705
- // Two's complement: the true sum lies well inside i32 (the year is at most
706
- // 9999), so the unsigned add gives it exactly, negative or not.
707
- const days: i32 = toI32(toU32(years) + yday)
738
+ const days: i32 = years + (yday & 511)
708
739
  const seconds: i32 = hour * 3600 + minute * 60 + second
709
740
  return toI64(days) * 86400000 + toI64(seconds) * 1000
710
741
  }
@@ -1165,31 +1196,36 @@ const x509DerTimeAt = (seconds: i64): u8[] => {
1165
1196
  let days: i32 = toI32(seconds / 86400)
1166
1197
  const rest: i32 = toI32(seconds % 86400)
1167
1198
  let year: i32 = 1970
1168
- while (year < 9999) {
1199
+ // `days >= 0` changes nothing (a negative count is below any length and
1200
+ // stops the loop as before); it is what proves the subtraction.
1201
+ while (year < 9999 && days >= 0) {
1169
1202
  const length: i32 = x509IsLeapYear(year) ? 366 : 365
1170
1203
  if (days < length) {
1171
1204
  break
1172
1205
  }
1173
- // Unsigned: `days` is at least `length` here, so the difference cannot wrap.
1174
- days = toI32(toU32(days) - toU32(length))
1206
+ // Not negative (the loop's test) less the year's length, which the mask
1207
+ // keeps: src/bounds.ts proves the difference.
1208
+ days = days - (length & 511)
1175
1209
  year++
1176
1210
  }
1177
1211
  let month: i32 = 1
1178
- while (month < 12) {
1212
+ while (month < 12 && days >= 0) {
1179
1213
  const length: i32 = x509DaysInMonth(year, month)
1180
1214
  if (days < length) {
1181
1215
  break
1182
1216
  }
1183
- // Unsigned: `days` is at least `length` here, so the difference cannot wrap.
1184
- days = toI32(toU32(days) - toU32(length))
1217
+ // Not negative (the loop's test) less the month's length, which the mask
1218
+ // keeps: src/bounds.ts proves the difference.
1219
+ days = days - (length & 31)
1185
1220
  month++
1186
1221
  }
1187
1222
  const text: u8[] = []
1188
1223
  const utc: boolean = year < 2050
1189
1224
  x509PushDigits(text, year, !utc)
1190
1225
  x509PushDigits(text, month, false)
1191
- // Unsigned: `days` is below its month's length here, so the sum cannot wrap.
1192
- x509PushDigits(text, toI32(toU32(days) + 1), false)
1226
+ // `days` is below its month's length for any time up to 9999-12-31, which the
1227
+ // mask keeps: it lets src/bounds.ts prove the sum.
1228
+ x509PushDigits(text, (days & 31) + 1, false)
1193
1229
  x509PushDigits(text, rest / 3600, false)
1194
1230
  x509PushDigits(text, (rest / 60) % 60, false)
1195
1231
  x509PushDigits(text, rest % 60, false)
@@ -1326,8 +1362,12 @@ export const pemToDer = (pem: string, label: string): u8[][] | null => {
1326
1362
  while (lineEnd >= 0 && lineEnd < toI32(pem.length) && toI32(pem.charCodeAt(lineEnd)) !== 10) {
1327
1363
  lineEnd = lineEnd + 1
1328
1364
  }
1329
- // Unsigned: `lineEnd` is at most the text's length, so the sum cannot wrap.
1330
- const next: i32 = toI32(toU32(lineEnd) + 1)
1365
+ // Past the newline; at the text's end, the end itself, which stops the loop
1366
+ // just as one past it did.
1367
+ let next: i32 = n
1368
+ if (lineEnd >= 0 && lineEnd < n) {
1369
+ next = lineEnd + 1
1370
+ }
1331
1371
  // `lineEnd` is in the text, said again for the bounds proof.
1332
1372
  if (
1333
1373
  lineEnd > lineStart &&
@@ -1568,6 +1608,80 @@ export const x509ParseP256PrivateKey = (pem: string): Secret<u8[]> | null => {
1568
1608
  return key
1569
1609
  }
1570
1610
 
1611
+ /**
1612
+ * The RSA key of a PKCS#8 PrivateKeyInfo (RFC 5208 §5) or OneAsymmetricKey
1613
+ * (RFC 5958 §2): version 0 or 1, the algorithm rsaEncryption with NULL
1614
+ * parameters (RFC 3279 §2.3.1), and a PKCS#1 RSAPrivateKey filling the OCTET
1615
+ * STRING, which `rsaParsePrivateKey` reads and tries. The optional `[0]`
1616
+ * attributes are skipped; anything after them, a version 1 public key
1617
+ * included, is refused, since nothing here would check it against the key.
1618
+ */
1619
+ const x509Pkcs8RsaKey = (der: u8[]): Secret<u8[]> | null => {
1620
+ const total: i32 = toI32(der.length)
1621
+ const seq: X509DerElement | null = x509DerExpect(der, 0, total, X509_TAG_SEQUENCE)
1622
+ if (seq === null || seq.end !== total) {
1623
+ return null
1624
+ }
1625
+ const version: X509DerElement | null = x509DerRead(der, seq.start, seq.end)
1626
+ if (version === null) {
1627
+ return null
1628
+ }
1629
+ const v: i32 = x509DerSmallInteger(der, version)
1630
+ if (v !== 0 && v !== 1) {
1631
+ return null
1632
+ }
1633
+ const alg: X509DerElement | null = x509DerRead(der, version.end, seq.end)
1634
+ if (alg === null || !x509DerIsAlgorithmNull(der, alg, x509OidPkcs1(1))) {
1635
+ return null
1636
+ }
1637
+ const key: X509DerElement | null = x509DerExpect(der, alg.end, seq.end, X509_TAG_OCTET_STRING)
1638
+ if (key === null) {
1639
+ return null
1640
+ }
1641
+ let at: i32 = key.end
1642
+ if (x509DerPeek(der, at, seq.end) === 0xa0) {
1643
+ const attributes: X509DerElement | null = x509DerRead(der, at, seq.end)
1644
+ if (attributes === null) {
1645
+ return null
1646
+ }
1647
+ at = attributes.end
1648
+ }
1649
+ if (at !== seq.end) {
1650
+ return null
1651
+ }
1652
+ return rsaParsePrivateKey(der, key.start, key.end)
1653
+ }
1654
+
1655
+ /**
1656
+ * The RSA private key in `pem`, as the `Secret` `nish/crypto/rsa-sign` signs
1657
+ * with: one `RSA PRIVATE KEY` block (PKCS#1, RFC 8017 §A.1.2) or one `PRIVATE
1658
+ * KEY` block (unencrypted PKCS#8 with rsaEncryption, RFC 5208 or RFC 5958),
1659
+ * and not both. The key must be 2048 to 4096 bits, with an odd modulus and
1660
+ * a private exponent that signs under its modulus (`rsaParsePrivateKey`).
1661
+ * `null` for no such block or more than one, for a smaller or larger key, for
1662
+ * an EC or any other key, and for anything malformed. Every block decoded
1663
+ * from `pem` is wiped before this returns, whichever answer it gives; `pem`
1664
+ * itself is the caller's string, which nothing can wipe (X509-7 in
1665
+ * docs/security/crypto-x509.md).
1666
+ *
1667
+ * @param pem The PEM text holding the key.
1668
+ * @returns The key in `rsaParsePrivateKey`'s layout, in a fresh `Secret` the caller wipes, or `null`.
1669
+ */
1670
+ export const x509ParseRsaPrivateKey = (pem: string): Secret<u8[]> | null => {
1671
+ const pkcs1: u8[][] | null = pemToDer(pem, "RSA PRIVATE KEY")
1672
+ const pkcs8: u8[][] | null = pemToDer(pem, "PRIVATE KEY")
1673
+ let key: Secret<u8[]> | null = null
1674
+ if (pkcs1 !== null && pkcs8 === null && toI32(pkcs1.length) === 1) {
1675
+ const der: u8[] = x509FirstBlock(pkcs1)
1676
+ key = rsaParsePrivateKey(der, 0, toI32(der.length))
1677
+ } else if (pkcs8 !== null && pkcs1 === null && toI32(pkcs8.length) === 1) {
1678
+ key = x509Pkcs8RsaKey(x509FirstBlock(pkcs8))
1679
+ }
1680
+ x509WipeBlocks(pkcs1)
1681
+ x509WipeBlocks(pkcs8)
1682
+ return key
1683
+ }
1684
+
1571
1685
  // ---------------------------------------------------------------------------
1572
1686
  // Certificates (RFC 5280 §4.1)
1573
1687
  // ---------------------------------------------------------------------------
@@ -1766,8 +1880,13 @@ const x509ReadSubjectAltName = (der: u8[], start: i32, end: i32, out: X509Certif
1766
1880
  } else if (kind === X509_GN_IP && (length === 4 || length === 16)) {
1767
1881
  out.sanIp.push(x509DerSlice(der, name.start, name.end))
1768
1882
  } else if (kind === X509_GN_DIR || kind === X509_GN_OTHER) {
1769
- // Unsigned: one per name read, fewer than the DER's octets, so the sum cannot wrap.
1770
- out.sanOther = toI32(toU32(out.sanOther) + 1)
1883
+ const other: i32 = out.sanOther
1884
+ // One per name read, so fewer than the DER's octets: always false, and
1885
+ // what proves the count's step.
1886
+ if (other < 0 || other >= toI32(der.length)) {
1887
+ return false
1888
+ }
1889
+ out.sanOther = other + 1
1771
1890
  } else {
1772
1891
  return false
1773
1892
  }
@@ -1971,8 +2090,10 @@ const x509ReadSubtrees = (
1971
2090
  if (length !== 8 && length !== 32) {
1972
2091
  return false
1973
2092
  }
1974
- // Unsigned: the half is in the element, so the sum cannot wrap.
1975
- const half: i32 = toI32(toU32(base.start) + toU32(length / 2))
2093
+ const half: i32 = x509Sum(base.start, length / 2, base.end)
2094
+ if (half < 0) {
2095
+ return false
2096
+ }
1976
2097
  subtree.ip = x509DerSlice(der, base.start, half)
1977
2098
  subtree.mask = x509DerSlice(der, half, base.end)
1978
2099
  if (!x509MaskContiguous(subtree.mask)) {
@@ -2564,12 +2685,15 @@ export const x509MintSelfSigned = (
2564
2685
  if (start < 0 || start > 253402300799) {
2565
2686
  return null
2566
2687
  }
2567
- // Unsigned: `start` is below 2^38 and `days` at most `X509_MAX_DAYS`, so the sum cannot wrap.
2568
- const end: i64 = toI64(toU64(start) + toU64(days) * 86400)
2688
+ // In `u64`: `start` is below 2^38 and `days` at most `X509_MAX_DAYS`, so the
2689
+ // sum comes nowhere near 2^64.
2690
+ const stop: u64 = toU64(start) + toU64(days) * toU64(86400)
2569
2691
  // 9999-12-31T23:59:59Z, the last second RFC 5280's times can spell.
2570
- if (end > 253402300799) {
2692
+ const last: i64 = 253402300799
2693
+ if (stop > toU64(last)) {
2571
2694
  return null
2572
2695
  }
2696
+ const end: i64 = toI64(stop)
2573
2697
  const name: u8[] = x509Bytes(commonName)
2574
2698
  const nameLength: i32 = toI32(name.length)
2575
2699
  if (nameLength < 1 || nameLength > X509_MAX_COMMON_NAME || !x509IsUtf8Name(name)) {