@amritk/nish-aarch64-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.
- package/INSTALL.md +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +237 -160
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +94 -11
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +14 -11
- package/std/base64.ts +74 -35
- package/std/bytes.ts +207 -97
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +213 -115
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +484 -149
- package/std/crypto/jwt.ts +51 -40
- package/std/crypto/p256.ts +14 -19
- package/std/crypto/pki-roots.ts +35 -12
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1635 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +212 -90
- package/std/crypto/sha512.ts +135 -49
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +12 -6
- package/std/json-value.ts +236 -119
- package/std/json.ts +81 -46
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +224 -61
- package/std/net/handler.ts +189 -88
- package/std/net/hpack.ts +216 -103
- package/std/net/http-compress.ts +104 -42
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +326 -24
- package/std/net/http1-client-codec.ts +159 -55
- package/std/net/http1-codec.ts +427 -237
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +139 -60
- package/std/net/http3.ts +140 -79
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +97 -64
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +147 -40
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +61 -39
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +36 -27
- package/std/net/quic.ts +84 -54
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +86 -36
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +80 -35
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +194 -50
- package/std/net/upstream.ts +83 -38
- package/std/net/url.ts +170 -94
- package/std/net/websocket.ts +19 -20
- package/std/net/webtransport.ts +123 -33
- package/std/raster.ts +12 -2
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +176 -24
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +38 -16
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
package/std/crypto/rsa.ts
CHANGED
|
@@ -207,14 +207,16 @@ const rsaBitLength = (v: u8[]): i32 => {
|
|
|
207
207
|
return -1
|
|
208
208
|
}
|
|
209
209
|
const top: i32 = toI32(v[0])
|
|
210
|
-
|
|
210
|
+
// The leading zero bits of the top byte, eight for a zero byte.
|
|
211
|
+
let missing: i32 = 8
|
|
211
212
|
for (let b: i32 = 0; b < 8; b++) {
|
|
212
213
|
if (top >> b !== 0) {
|
|
213
|
-
|
|
214
|
+
missing = 7 - b
|
|
214
215
|
}
|
|
215
216
|
}
|
|
216
|
-
//
|
|
217
|
-
|
|
217
|
+
// The masks keep what the checks above already hold (fewer than 2^28 bytes,
|
|
218
|
+
// at most eight missing bits), and let src/bounds.ts prove the count fits.
|
|
219
|
+
return (length & 0x0fffffff) * 8 - (missing & 0xf)
|
|
218
220
|
}
|
|
219
221
|
|
|
220
222
|
/**
|
|
@@ -738,3 +740,23 @@ export const rsaVerifyPss = (key: RsaPublicKey, hash: i32, saltLen: i32, msg: u8
|
|
|
738
740
|
}
|
|
739
741
|
return timingSafeEqual(rsaDigest(hash, prime), h)
|
|
740
742
|
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Verifies an RSASSA-PSS signature under a public key given as its DER, as
|
|
746
|
+
* `rsaVerifyPss` does after `rsaParsePublicKey`: for a caller that holds the
|
|
747
|
+
* key's bytes and no `RsaPublicKey`.
|
|
748
|
+
*
|
|
749
|
+
* @param der The key as `rsaParsePublicKey` reads it, whole.
|
|
750
|
+
* @param hash `RSA_HASH_SHA256`, `RSA_HASH_SHA384` or `RSA_HASH_SHA512`.
|
|
751
|
+
* @param saltLen The salt's length in bytes.
|
|
752
|
+
* @param msg The message signed, whole.
|
|
753
|
+
* @param sig The signature, big-endian.
|
|
754
|
+
* @returns `true` only when `der` parses and `sig` is its signature of `msg`.
|
|
755
|
+
*/
|
|
756
|
+
export const rsaVerifyPssDer = (der: u8[], hash: i32, saltLen: i32, msg: u8[], sig: u8[]): boolean => {
|
|
757
|
+
const key: RsaPublicKey | null = rsaParsePublicKey(der)
|
|
758
|
+
if (key === null) {
|
|
759
|
+
return false
|
|
760
|
+
}
|
|
761
|
+
return rsaVerifyPss(key, hash, saltLen, msg, sig)
|
|
762
|
+
}
|
package/std/crypto/sha256.ts
CHANGED
|
@@ -460,20 +460,20 @@ const sha256Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
|
|
|
460
460
|
* the digest does not depend on where the message was split. `digest` pads the
|
|
461
461
|
* message (§5.1.1), runs the last block or two, and ends the computation: the
|
|
462
462
|
* padding is written into this hasher's own state, so a later `update` would
|
|
463
|
-
* carry the padding as message,
|
|
464
|
-
* value. A second `digest` answers
|
|
465
|
-
*
|
|
466
|
-
* on.
|
|
463
|
+
* carry the padding as message, so it answers `false` and absorbs nothing
|
|
464
|
+
* instead of hashing a wrong value. A second `digest` answers
|
|
465
|
+
* the same digest again, and a `copy` of a finished hasher is finished too.
|
|
466
|
+
* `copy` first when the computation has to go on.
|
|
467
467
|
*/
|
|
468
468
|
export class Sha256 {
|
|
469
469
|
/**
|
|
470
|
-
* The message length absorbed so far, in bytes.
|
|
471
|
-
* appends the length in bits
|
|
472
|
-
* 2^31 bytes where an `i32` would wrap.
|
|
470
|
+
* The message length absorbed so far, in bytes. A `u64` because §5.1.1
|
|
471
|
+
* appends the length in bits modulo 2^64, which a `u64` wraps to by its type,
|
|
472
|
+
* and a transcript can pass 2^31 bytes where an `i32` would wrap.
|
|
473
473
|
*
|
|
474
474
|
* @internal Set by `nish/crypto/hmac` and `nish/net/postgres` when they wipe, copy or restart a hasher.
|
|
475
475
|
*/
|
|
476
|
-
total:
|
|
476
|
+
total: u64 = 0
|
|
477
477
|
/** H_0 .. H_7, the eight words of the intermediate hash value (§6.2.2 step 4). */
|
|
478
478
|
state: u32[]
|
|
479
479
|
/** The pending bytes of the current block; the first `fill` of them are live. */
|
|
@@ -515,61 +515,34 @@ export class Sha256 {
|
|
|
515
515
|
}
|
|
516
516
|
|
|
517
517
|
/**
|
|
518
|
-
* Absorbs `data[off .. off + len)`. A window outside `data
|
|
519
|
-
*
|
|
520
|
-
*
|
|
518
|
+
* Absorbs `data[off .. off + len)`. A window outside `data`, or a call
|
|
519
|
+
* after `digest`, answers `false` and absorbs nothing rather than hashing a
|
|
520
|
+
* short read. Check it, since a digest over the wrong bytes looks exactly
|
|
521
|
+
* like a digest over the right ones.
|
|
521
522
|
*
|
|
522
523
|
* @param data The buffer holding the window; it is read, not kept.
|
|
523
524
|
* @param off Where the window starts in `data`.
|
|
524
525
|
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
526
|
+
* @returns `true` once the window is absorbed; `false`, absorbing nothing,
|
|
527
|
+
* for a window outside `data` or a hasher already digested.
|
|
525
528
|
*/
|
|
526
|
-
update(data: u8[], off: i32, len: i32):
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
// Top up a partly filled block first, so that the loop below only ever
|
|
544
|
-
// starts at a block boundary. Each loop's condition beyond its count is
|
|
545
|
-
// what proves its indices in range; the window check above makes it hold.
|
|
546
|
-
if (fill > 0) {
|
|
547
|
-
while (left > 0 && fill >= 0 && fill < SHA256_BLOCK && fill < room && at >= 0 && at < size) {
|
|
548
|
-
block[fill] = data[at]
|
|
549
|
-
fill += 1
|
|
550
|
-
at += 1
|
|
551
|
-
left -= 1
|
|
552
|
-
}
|
|
553
|
-
if (fill < SHA256_BLOCK) {
|
|
554
|
-
this.fill = fill
|
|
555
|
-
return
|
|
556
|
-
}
|
|
557
|
-
sha256Compress(state, schedule, block, 0)
|
|
558
|
-
}
|
|
559
|
-
// Whole blocks are compressed straight out of the caller's buffer, with no
|
|
560
|
-
// copy through `block`.
|
|
561
|
-
while (left >= 64 && at >= 0 && at <= size - 64) {
|
|
562
|
-
sha256Compress(state, schedule, data, at)
|
|
563
|
-
at += 64
|
|
564
|
-
left -= 64
|
|
565
|
-
}
|
|
566
|
-
fill = 0
|
|
567
|
-
while (fill < left && fill < room && at >= 0 && at < size) {
|
|
568
|
-
block[fill] = data[at]
|
|
569
|
-
fill += 1
|
|
570
|
-
at += 1
|
|
571
|
-
}
|
|
572
|
-
this.fill = fill
|
|
529
|
+
update(data: u8[], off: i32, len: i32): boolean {
|
|
530
|
+
return !this.finished && sha256Absorb(this, data, off, len)
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* The same answer as `update`, kept for callers written against it: a
|
|
535
|
+
* window outside `data`, or an update after `digest`, answers `false` and
|
|
536
|
+
* absorbs nothing.
|
|
537
|
+
*
|
|
538
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
539
|
+
* @param off Where the window starts in `data`.
|
|
540
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
541
|
+
* @returns `true` once the window is absorbed; `false`, absorbing nothing,
|
|
542
|
+
* for a window outside `data` or a hasher already digested.
|
|
543
|
+
*/
|
|
544
|
+
tryUpdate(data: u8[], off: i32, len: i32): boolean {
|
|
545
|
+
return !this.finished && sha256Absorb(this, data, off, len)
|
|
573
546
|
}
|
|
574
547
|
|
|
575
548
|
/**
|
|
@@ -577,39 +550,61 @@ export class Sha256 {
|
|
|
577
550
|
* `update`. A longer one (possible under `--number-mode f64`, where `toI32`
|
|
578
551
|
* of its length would saturate and leave a prefix) is walked with a `number`
|
|
579
552
|
* offset instead, a block at a time through `block`, so it is absorbed
|
|
580
|
-
* whole. After `digest` it
|
|
553
|
+
* whole. After `digest` it answers `false` and absorbs nothing, as
|
|
554
|
+
* `update` does.
|
|
581
555
|
*
|
|
582
556
|
* @param data The message, or the rest of it; it is read, not kept.
|
|
557
|
+
* @returns `true` once `data` is absorbed; `false`, absorbing nothing, for a
|
|
558
|
+
* hasher already digested.
|
|
583
559
|
*/
|
|
584
|
-
updateAll(data: u8[]):
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
// A finished hasher panics in `update` before it reads the window, so
|
|
588
|
-
// the saturated length of a longer array is never used.
|
|
589
|
-
const from: i32 = 0
|
|
590
|
-
this.update(data, from, toI32(n))
|
|
591
|
-
return
|
|
560
|
+
updateAll(data: u8[]): boolean {
|
|
561
|
+
if (this.finished) {
|
|
562
|
+
return false
|
|
592
563
|
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
return
|
|
609
|
-
}
|
|
610
|
-
sha256Compress(state, schedule, block, 0)
|
|
611
|
-
fill = 0
|
|
564
|
+
sha256AbsorbWhole(this, data)
|
|
565
|
+
return true
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* The same answer as `updateAll`, kept for callers written against it:
|
|
570
|
+
* after `digest` it answers `false` and absorbs nothing.
|
|
571
|
+
*
|
|
572
|
+
* @param data The message, or the rest of it; it is read, not kept.
|
|
573
|
+
* @returns `true` once `data` is absorbed; `false`, absorbing nothing, for a
|
|
574
|
+
* hasher already digested.
|
|
575
|
+
*/
|
|
576
|
+
tryUpdateAll(data: u8[]): boolean {
|
|
577
|
+
if (this.finished) {
|
|
578
|
+
return false
|
|
612
579
|
}
|
|
580
|
+
sha256AbsorbWhole(this, data)
|
|
581
|
+
return true
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* `updateAll` past its finished test, for a caller holding a hasher that
|
|
586
|
+
* `digest` cannot have ended: one it made itself and has not digested (the
|
|
587
|
+
* HMAC key and pad blocks, a tag's inner digest). The whole array is the
|
|
588
|
+
* window, so there is no window to get wrong.
|
|
589
|
+
*
|
|
590
|
+
* @internal Called by `nish/crypto/hmac`.
|
|
591
|
+
*/
|
|
592
|
+
absorbWhole(data: u8[]): void {
|
|
593
|
+
sha256AbsorbWhole(this, data)
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* `update` past its finished test, for the same callers as `absorbWhole`
|
|
598
|
+
* with a window (an HMAC tag's inner digest). A window outside `data`
|
|
599
|
+
* absorbs nothing, as `tryUpdate`'s does.
|
|
600
|
+
*
|
|
601
|
+
* @internal Called by `nish/crypto/hmac`.
|
|
602
|
+
* @param data The bytes to read from; they are read, not kept.
|
|
603
|
+
* @param off Where the window starts in `data`.
|
|
604
|
+
* @param len How many bytes the window holds.
|
|
605
|
+
*/
|
|
606
|
+
absorbWindow(data: u8[], off: i32, len: i32): void {
|
|
607
|
+
sha256Absorb(this, data, off, len)
|
|
613
608
|
}
|
|
614
609
|
|
|
615
610
|
/**
|
|
@@ -663,7 +658,7 @@ export class Sha256 {
|
|
|
663
658
|
* hasher as it was and answers `false`, rather than writing the part of the
|
|
664
659
|
* digest that fits. Once finished, the hash value is the digest, so a second
|
|
665
660
|
* `digest` or `digestInto` writes the same 32 bytes again; an `update` after
|
|
666
|
-
* it
|
|
661
|
+
* it answers `false` and absorbs nothing.
|
|
667
662
|
*
|
|
668
663
|
* @param out The caller's array the digest is written into.
|
|
669
664
|
* @param at Where the 32 digest bytes start in `out`.
|
|
@@ -688,7 +683,10 @@ export class Sha256 {
|
|
|
688
683
|
const room: i32 = toI32(block.length)
|
|
689
684
|
const fill: i32 = this.fill
|
|
690
685
|
for (let i: i32 = fill; i >= 0 && i < room; i += 1) {
|
|
691
|
-
block[i] =
|
|
686
|
+
block[i] = 0
|
|
687
|
+
}
|
|
688
|
+
if (fill >= 0 && fill < room) {
|
|
689
|
+
block[fill] = 0x80
|
|
692
690
|
}
|
|
693
691
|
if (fill >= 56) {
|
|
694
692
|
sha256Compress(state, schedule, block, 0)
|
|
@@ -696,7 +694,7 @@ export class Sha256 {
|
|
|
696
694
|
block[i] = 0
|
|
697
695
|
}
|
|
698
696
|
}
|
|
699
|
-
const bits: u64 =
|
|
697
|
+
const bits: u64 = this.total << 3
|
|
700
698
|
let shift: u64 = 0
|
|
701
699
|
for (let i: i32 = 63; i >= 56 && i < room; i -= 1) {
|
|
702
700
|
block[i] = toU8(bits >>> shift)
|
|
@@ -717,6 +715,100 @@ export class Sha256 {
|
|
|
717
715
|
}
|
|
718
716
|
}
|
|
719
717
|
|
|
718
|
+
/**
|
|
719
|
+
* The body of `Sha256.update`, after its finished check: absorbs
|
|
720
|
+
* `data[off .. off + len)` into `h`, or absorbs nothing and returns false
|
|
721
|
+
* when that window lies outside `data`. Each loop's own condition still
|
|
722
|
+
* bounds its indices; the window check is what makes them hold.
|
|
723
|
+
*/
|
|
724
|
+
const sha256Absorb = (h: Sha256, data: u8[], off: i32, len: i32): boolean => {
|
|
725
|
+
const size: i32 = toI32(data.length)
|
|
726
|
+
if (off < 0 || len < 0 || off > size || len > size - off) {
|
|
727
|
+
return false
|
|
728
|
+
}
|
|
729
|
+
// §5.1.1 appends the length modulo 2^64, so the count wraps as a `u64`.
|
|
730
|
+
h.total = h.total + toU64(len)
|
|
731
|
+
const state: u32[] = h.state
|
|
732
|
+
const schedule: u32[] = h.schedule
|
|
733
|
+
const block: u8[] = h.block
|
|
734
|
+
const room: i32 = toI32(block.length)
|
|
735
|
+
let at: i32 = off
|
|
736
|
+
let left: i32 = len
|
|
737
|
+
let fill: i32 = h.fill
|
|
738
|
+
// Top up a partly filled block first, so that the loop below only ever
|
|
739
|
+
// starts at a block boundary. Each loop's condition beyond its count is
|
|
740
|
+
// what proves its indices in range; the window check above makes it hold.
|
|
741
|
+
if (fill > 0) {
|
|
742
|
+
while (left > 0 && fill >= 0 && fill < SHA256_BLOCK && fill < room && at >= 0 && at < size) {
|
|
743
|
+
block[fill] = data[at]
|
|
744
|
+
fill += 1
|
|
745
|
+
at += 1
|
|
746
|
+
left -= 1
|
|
747
|
+
}
|
|
748
|
+
if (fill < SHA256_BLOCK) {
|
|
749
|
+
h.fill = fill
|
|
750
|
+
return true
|
|
751
|
+
}
|
|
752
|
+
sha256Compress(state, schedule, block, 0)
|
|
753
|
+
}
|
|
754
|
+
// Whole blocks are compressed straight out of the caller's buffer, with no
|
|
755
|
+
// copy through `block`.
|
|
756
|
+
while (left >= 64 && at >= 0 && at <= size - 64) {
|
|
757
|
+
sha256Compress(state, schedule, data, at)
|
|
758
|
+
at += 64
|
|
759
|
+
left -= 64
|
|
760
|
+
}
|
|
761
|
+
fill = 0
|
|
762
|
+
while (fill < left && fill < room && at >= 0 && at < size) {
|
|
763
|
+
block[fill] = data[at]
|
|
764
|
+
fill += 1
|
|
765
|
+
at += 1
|
|
766
|
+
}
|
|
767
|
+
h.fill = fill
|
|
768
|
+
return true
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* The body of `Sha256.updateAll` for an array longer than 2^31 - 1 bytes,
|
|
773
|
+
* after its finished check: walks `data` with a `number` offset, a block at a
|
|
774
|
+
* time through `h.block`, so it is absorbed whole.
|
|
775
|
+
*/
|
|
776
|
+
const sha256AbsorbLong = (h: Sha256, data: u8[]): void => {
|
|
777
|
+
const n: number = data.length
|
|
778
|
+
h.total = h.total + toU64(n)
|
|
779
|
+
const state: u32[] = h.state
|
|
780
|
+
const schedule: u32[] = h.schedule
|
|
781
|
+
const block: u8[] = h.block
|
|
782
|
+
const room: i32 = toI32(block.length)
|
|
783
|
+
let fill: i32 = h.fill
|
|
784
|
+
let off: number = 0
|
|
785
|
+
while (true) {
|
|
786
|
+
while (fill >= 0 && fill < SHA256_BLOCK && fill < room && off >= 0 && off < n) {
|
|
787
|
+
block[fill] = data[off]
|
|
788
|
+
fill += 1
|
|
789
|
+
off += 1
|
|
790
|
+
}
|
|
791
|
+
if (fill < SHA256_BLOCK) {
|
|
792
|
+
h.fill = fill
|
|
793
|
+
return
|
|
794
|
+
}
|
|
795
|
+
sha256Compress(state, schedule, block, 0)
|
|
796
|
+
fill = 0
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/** Absorbs all of `data` into `h`, past the finished check (`sha256`). */
|
|
801
|
+
const sha256AbsorbWhole = (h: Sha256, data: u8[]): void => {
|
|
802
|
+
const n: number = data.length
|
|
803
|
+
if (n <= 2147483647) {
|
|
804
|
+
// The window is the whole array, so it always lies inside it.
|
|
805
|
+
const from: i32 = 0
|
|
806
|
+
sha256Absorb(h, data, from, toI32(n))
|
|
807
|
+
} else {
|
|
808
|
+
sha256AbsorbLong(h, data)
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
720
812
|
/**
|
|
721
813
|
* The SHA-256 digest of all of `data`, as a fresh 32-byte array.
|
|
722
814
|
*
|
|
@@ -727,7 +819,37 @@ export class Sha256 {
|
|
|
727
819
|
* @returns A fresh 32-byte digest.
|
|
728
820
|
*/
|
|
729
821
|
export const sha256 = (data: u8[]): u8[] => {
|
|
822
|
+
// A fresh hasher is not finished, so the message goes straight to the
|
|
823
|
+
// absorbing bodies, past `update`'s and `updateAll`'s finished checks.
|
|
730
824
|
const hasher: Sha256 = new Sha256()
|
|
731
|
-
hasher
|
|
825
|
+
sha256AbsorbWhole(hasher, data)
|
|
732
826
|
return hasher.digest()
|
|
733
827
|
}
|
|
828
|
+
|
|
829
|
+
/**
|
|
830
|
+
* A fresh `Sha256` that has absorbed `a || b || c || d || e` and is not yet
|
|
831
|
+
* finished. This is the start of a hash whose message is keyed material,
|
|
832
|
+
* such as an HMAC's padded key block followed by its message: the parts are
|
|
833
|
+
* absorbed in place, never joined into an array that a `push` could outgrow
|
|
834
|
+
* and leave behind, and the caller, which owns the hasher, calls `digest`
|
|
835
|
+
* and then wipes its `state`, `block` and `schedule`. Pass an empty array for
|
|
836
|
+
* a part the message lacks.
|
|
837
|
+
*
|
|
838
|
+
* @param a The first part of the message; it is read, not changed.
|
|
839
|
+
* @param b The second part, read after `a`.
|
|
840
|
+
* @param c The third part, read after `b`.
|
|
841
|
+
* @param d The fourth part, read after `c`.
|
|
842
|
+
* @param e The fifth part, read after `d`.
|
|
843
|
+
* @returns A hasher holding the five parts, ready for `digest`.
|
|
844
|
+
*/
|
|
845
|
+
export const sha256Absorbed = (a: u8[], b: u8[], c: u8[], d: u8[], e: u8[]): Sha256 => {
|
|
846
|
+
// A fresh hasher is not finished, so the parts go straight to the
|
|
847
|
+
// absorbing bodies, past `update`'s finished check.
|
|
848
|
+
const hasher: Sha256 = new Sha256()
|
|
849
|
+
sha256AbsorbWhole(hasher, a)
|
|
850
|
+
sha256AbsorbWhole(hasher, b)
|
|
851
|
+
sha256AbsorbWhole(hasher, c)
|
|
852
|
+
sha256AbsorbWhole(hasher, d)
|
|
853
|
+
sha256AbsorbWhole(hasher, e)
|
|
854
|
+
return hasher
|
|
855
|
+
}
|