@amritk/nish-x86_64-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.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/crypto/sha512.ts
CHANGED
|
@@ -260,27 +260,15 @@ const sha384Initial = (): u64[] => [
|
|
|
260
260
|
w64(0x47b5481d, 0xbefa4fa4),
|
|
261
261
|
]
|
|
262
262
|
|
|
263
|
-
/** The big-endian 64-bit word at `buf[off]`. */
|
|
264
|
-
const loadWord = (buf: u8[], off: i32): u64 => {
|
|
265
|
-
let word: u64 = 0
|
|
266
|
-
for (let i: i32 = 0; i < 8; i++) {
|
|
267
|
-
word = (word << 8) | toU64(buf[off + i])
|
|
268
|
-
}
|
|
269
|
-
return word
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/** Writes `word` big-endian into `buf[off]` .. `buf[off + 7]`. */
|
|
273
|
-
const storeWord = (buf: u8[], off: i32, word: u64): void => {
|
|
274
|
-
for (let i: i32 = 0; i < 8; i++) {
|
|
275
|
-
buf[off + i] = toU8(word >> toU64((7 - i) * 8))
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
263
|
/**
|
|
280
264
|
* The state both hashes share: the hash value, the partial block, and the
|
|
281
265
|
* message length in bytes. §5.1.2 appends the length in bits as 128 bits; a
|
|
282
266
|
* byte count in a `u64` shifted left by three fills those 128 bits for any
|
|
283
267
|
* message shorter than 2^64 bytes, which is every message there can be.
|
|
268
|
+
*
|
|
269
|
+
* No index in it is checked at run time: each read and write sits under a
|
|
270
|
+
* loop or block condition that proves it in range, and every caller's
|
|
271
|
+
* arguments make that condition hold.
|
|
284
272
|
*/
|
|
285
273
|
class Sha512Engine {
|
|
286
274
|
state: u64[]
|
|
@@ -304,72 +292,86 @@ class Sha512Engine {
|
|
|
304
292
|
private compress(src: u8[], off: i32): void {
|
|
305
293
|
const w = this.schedule
|
|
306
294
|
const h = this.state
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
295
|
+
const n: i32 = toI32(src.length)
|
|
296
|
+
if (toI32(h.length) >= 8 && toI32(w.length) >= 80) {
|
|
297
|
+
// Step 1: the sixteen big-endian words of the block (§3.1), then the
|
|
298
|
+
// rest of the schedule. `p < n - 7` proves each `src[p + k]`.
|
|
299
|
+
let p: i32 = off
|
|
300
|
+
for (let t: i32 = 0; t < 16 && p >= 0 && p < n - 7; t++) {
|
|
301
|
+
w[t] =
|
|
302
|
+
(toU64(src[p]) << 56) |
|
|
303
|
+
(toU64(src[p + 1]) << 48) |
|
|
304
|
+
(toU64(src[p + 2]) << 40) |
|
|
305
|
+
(toU64(src[p + 3]) << 32) |
|
|
306
|
+
(toU64(src[p + 4]) << 24) |
|
|
307
|
+
(toU64(src[p + 5]) << 16) |
|
|
308
|
+
(toU64(src[p + 6]) << 8) |
|
|
309
|
+
toU64(src[p + 7])
|
|
310
|
+
p += 8
|
|
311
|
+
}
|
|
312
|
+
for (let t: i32 = 16; t < 80; t++) {
|
|
313
|
+
// Named offsets rather than `w[t - 2]`, so the bounds prover sees each index.
|
|
314
|
+
const t2: i32 = t - 2
|
|
315
|
+
const t7: i32 = t - 7
|
|
316
|
+
const t15: i32 = t - 15
|
|
317
|
+
const t16: i32 = t - 16
|
|
318
|
+
w[t] = smallSigma1(w[t2]) + w[t7] + smallSigma0(w[t15]) + w[t16]
|
|
319
|
+
}
|
|
320
|
+
let a = h[0]
|
|
321
|
+
let b = h[1]
|
|
322
|
+
let c = h[2]
|
|
323
|
+
let d = h[3]
|
|
324
|
+
let e = h[4]
|
|
325
|
+
let f = h[5]
|
|
326
|
+
let g = h[6]
|
|
327
|
+
let hh = h[7]
|
|
328
|
+
// §6.4.2 step 4, eight rounds per pass. Rather than shift eight words down
|
|
329
|
+
// each round, the rounds rename them: T1 accumulates in the word that
|
|
330
|
+
// becomes the new `a`, and the word that becomes the new `e` is `d + T1`,
|
|
331
|
+
// so after eight rounds every word is back in its own name. Each index is
|
|
332
|
+
// `u` minus a constant, which the bounds prover can see; `u + 1` it cannot.
|
|
333
|
+
for (let u: i32 = 7; u < 80; u += 8) {
|
|
334
|
+
const r0: i32 = u - 7
|
|
335
|
+
const r1: i32 = u - 6
|
|
336
|
+
const r2: i32 = u - 5
|
|
337
|
+
const r3: i32 = u - 4
|
|
338
|
+
const r4: i32 = u - 3
|
|
339
|
+
const r5: i32 = u - 2
|
|
340
|
+
const r6: i32 = u - 1
|
|
341
|
+
hh = hh + bigSigma1(e) + ((e & f) ^ (~e & g)) + roundConstant(r0) + w[r0]
|
|
342
|
+
d = d + hh
|
|
343
|
+
hh = hh + bigSigma0(a) + ((a & b) ^ (a & c) ^ (b & c))
|
|
344
|
+
g = g + bigSigma1(d) + ((d & e) ^ (~d & f)) + roundConstant(r1) + w[r1]
|
|
345
|
+
c = c + g
|
|
346
|
+
g = g + bigSigma0(hh) + ((hh & a) ^ (hh & b) ^ (a & b))
|
|
347
|
+
f = f + bigSigma1(c) + ((c & d) ^ (~c & e)) + roundConstant(r2) + w[r2]
|
|
348
|
+
b = b + f
|
|
349
|
+
f = f + bigSigma0(g) + ((g & hh) ^ (g & a) ^ (hh & a))
|
|
350
|
+
e = e + bigSigma1(b) + ((b & c) ^ (~b & d)) + roundConstant(r3) + w[r3]
|
|
351
|
+
a = a + e
|
|
352
|
+
e = e + bigSigma0(f) + ((f & g) ^ (f & hh) ^ (g & hh))
|
|
353
|
+
d = d + bigSigma1(a) + ((a & b) ^ (~a & c)) + roundConstant(r4) + w[r4]
|
|
354
|
+
hh = hh + d
|
|
355
|
+
d = d + bigSigma0(e) + ((e & f) ^ (e & g) ^ (f & g))
|
|
356
|
+
c = c + bigSigma1(hh) + ((hh & a) ^ (~hh & b)) + roundConstant(r5) + w[r5]
|
|
357
|
+
g = g + c
|
|
358
|
+
c = c + bigSigma0(d) + ((d & e) ^ (d & f) ^ (e & f))
|
|
359
|
+
b = b + bigSigma1(g) + ((g & hh) ^ (~g & a)) + roundConstant(r6) + w[r6]
|
|
360
|
+
f = f + b
|
|
361
|
+
b = b + bigSigma0(c) + ((c & d) ^ (c & e) ^ (d & e))
|
|
362
|
+
a = a + bigSigma1(f) + ((f & g) ^ (~f & hh)) + roundConstant(u) + w[u]
|
|
363
|
+
e = e + a
|
|
364
|
+
a = a + bigSigma0(b) + ((b & c) ^ (b & d) ^ (c & d))
|
|
365
|
+
}
|
|
366
|
+
h[0] = h[0] + a
|
|
367
|
+
h[1] = h[1] + b
|
|
368
|
+
h[2] = h[2] + c
|
|
369
|
+
h[3] = h[3] + d
|
|
370
|
+
h[4] = h[4] + e
|
|
371
|
+
h[5] = h[5] + f
|
|
372
|
+
h[6] = h[6] + g
|
|
373
|
+
h[7] = h[7] + hh
|
|
364
374
|
}
|
|
365
|
-
h[0] = h[0] + a
|
|
366
|
-
h[1] = h[1] + b
|
|
367
|
-
h[2] = h[2] + c
|
|
368
|
-
h[3] = h[3] + d
|
|
369
|
-
h[4] = h[4] + e
|
|
370
|
-
h[5] = h[5] + f
|
|
371
|
-
h[6] = h[6] + g
|
|
372
|
-
h[7] = h[7] + hh
|
|
373
375
|
}
|
|
374
376
|
|
|
375
377
|
/** @internal Called by `Sha512` and `Sha384`, which hold the engine. */
|
|
@@ -377,36 +379,81 @@ class Sha512Engine {
|
|
|
377
379
|
if (this.finished) {
|
|
378
380
|
panic("sha512: update after digest")
|
|
379
381
|
}
|
|
380
|
-
|
|
382
|
+
const size: i32 = toI32(data.length)
|
|
383
|
+
if (off < 0 || len < 0 || off > size - len) {
|
|
381
384
|
panic("sha512: the window is outside the buffer")
|
|
382
385
|
}
|
|
383
386
|
this.count = this.count + toU64(len)
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
387
|
+
const block: u8[] = this.block
|
|
388
|
+
const room: i32 = toI32(block.length)
|
|
389
|
+
let at: i32 = off
|
|
390
|
+
let left: i32 = len
|
|
391
|
+
let fill: i32 = this.filled
|
|
392
|
+
// Top up a partly filled block first, so that the loop below only ever
|
|
393
|
+
// starts at a block boundary. Each loop's condition beyond its count is
|
|
394
|
+
// what proves its indices in range; the window check above makes it hold.
|
|
395
|
+
if (fill > 0) {
|
|
396
|
+
while (left > 0 && fill >= 0 && fill < SHA512_BLOCK && fill < room && at >= 0 && at < size) {
|
|
397
|
+
block[fill] = data[at]
|
|
398
|
+
fill += 1
|
|
399
|
+
at += 1
|
|
400
|
+
left -= 1
|
|
391
401
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
left = left - take
|
|
395
|
-
if (this.filled < SHA512_BLOCK) {
|
|
402
|
+
if (fill < SHA512_BLOCK) {
|
|
403
|
+
this.filled = fill
|
|
396
404
|
return
|
|
397
405
|
}
|
|
398
|
-
this.compress(
|
|
399
|
-
this.filled = 0
|
|
406
|
+
this.compress(block, OFFSET_ZERO)
|
|
400
407
|
}
|
|
401
|
-
while (left >=
|
|
408
|
+
while (left >= 128 && at >= 0 && at <= size - 128) {
|
|
402
409
|
this.compress(data, at)
|
|
403
|
-
at
|
|
404
|
-
left
|
|
410
|
+
at += 128
|
|
411
|
+
left -= 128
|
|
412
|
+
}
|
|
413
|
+
fill = 0
|
|
414
|
+
while (fill < left && fill < room && at >= 0 && at < size) {
|
|
415
|
+
block[fill] = data[at]
|
|
416
|
+
fill += 1
|
|
417
|
+
at += 1
|
|
418
|
+
}
|
|
419
|
+
this.filled = fill
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* 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.
|
|
428
|
+
*
|
|
429
|
+
* @internal Called by `sha512`, `sha384` and `nish/crypto/hmac`.
|
|
430
|
+
*/
|
|
431
|
+
absorbAll(data: u8[]): void {
|
|
432
|
+
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))
|
|
437
|
+
return
|
|
405
438
|
}
|
|
406
|
-
|
|
407
|
-
|
|
439
|
+
this.count += toU64(toI64(n))
|
|
440
|
+
const block: u8[] = this.block
|
|
441
|
+
const room: i32 = toI32(block.length)
|
|
442
|
+
let fill: i32 = this.filled
|
|
443
|
+
let off: number = 0
|
|
444
|
+
while (true) {
|
|
445
|
+
while (fill >= 0 && fill < SHA512_BLOCK && fill < room && off >= 0 && off < n) {
|
|
446
|
+
block[fill] = data[off]
|
|
447
|
+
fill += 1
|
|
448
|
+
off += 1
|
|
449
|
+
}
|
|
450
|
+
if (fill < SHA512_BLOCK) {
|
|
451
|
+
this.filled = fill
|
|
452
|
+
return
|
|
453
|
+
}
|
|
454
|
+
this.compress(block, OFFSET_ZERO)
|
|
455
|
+
fill = 0
|
|
408
456
|
}
|
|
409
|
-
this.filled = left
|
|
410
457
|
}
|
|
411
458
|
|
|
412
459
|
/**
|
|
@@ -416,48 +463,73 @@ class Sha512Engine {
|
|
|
416
463
|
* @internal Called by `Sha512` and `Sha384`, which hold the engine.
|
|
417
464
|
*/
|
|
418
465
|
copyInto(twin: Sha512Engine): void {
|
|
419
|
-
|
|
420
|
-
|
|
466
|
+
const from: u64[] = this.state
|
|
467
|
+
const to: u64[] = twin.state
|
|
468
|
+
for (let i: i32 = 0; i < 8 && i < toI32(from.length) && i < toI32(to.length); i++) {
|
|
469
|
+
to[i] = from[i]
|
|
421
470
|
}
|
|
422
|
-
|
|
423
|
-
|
|
471
|
+
const fill: i32 = this.filled
|
|
472
|
+
const src: u8[] = this.block
|
|
473
|
+
const dst: u8[] = twin.block
|
|
474
|
+
for (let i: i32 = 0; i < fill && i < toI32(src.length) && i < toI32(dst.length); i++) {
|
|
475
|
+
dst[i] = src[i]
|
|
424
476
|
}
|
|
425
|
-
twin.filled =
|
|
477
|
+
twin.filled = fill
|
|
426
478
|
twin.count = this.count
|
|
427
479
|
twin.finished = this.finished
|
|
428
480
|
}
|
|
429
481
|
|
|
430
482
|
/**
|
|
431
|
-
* Pads (§5.1.2), compresses what is left, and
|
|
483
|
+
* Pads (§5.1.2), compresses what is left, and writes the first
|
|
484
|
+
* `out.length / 8` words of H(N) (§6.4.2, §6.5), at most eight, into `out`.
|
|
432
485
|
*
|
|
433
486
|
* @internal Called by `Sha512` and `Sha384`, which hold the engine.
|
|
434
487
|
*/
|
|
435
|
-
finish(
|
|
488
|
+
finish(out: u8[]): void {
|
|
436
489
|
if (this.finished) {
|
|
437
490
|
panic("sha512: digest taken twice")
|
|
438
491
|
}
|
|
439
492
|
this.finished = true
|
|
440
493
|
const block = this.block
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
494
|
+
const room: i32 = toI32(block.length)
|
|
495
|
+
const fill: i32 = this.filled
|
|
496
|
+
// The 0x80 that ends the message, then zeros to the end of the block.
|
|
497
|
+
for (let i: i32 = fill; i >= 0 && i < room; i++) {
|
|
498
|
+
block[i] = i === fill ? 0x80 : 0
|
|
445
499
|
}
|
|
446
|
-
if (
|
|
500
|
+
if (fill >= LENGTH_AT) {
|
|
447
501
|
this.compress(block, OFFSET_ZERO)
|
|
448
|
-
for (let i: i32 = 0; i < LENGTH_AT && i <
|
|
502
|
+
for (let i: i32 = 0; i < LENGTH_AT && i < room; i++) {
|
|
449
503
|
block[i] = 0
|
|
450
504
|
}
|
|
451
505
|
}
|
|
452
|
-
|
|
453
|
-
|
|
506
|
+
// The length in bits, big-endian in the last 16 bytes: the top 64 bits are
|
|
507
|
+
// `count >> 61`, the bottom 64 are `count << 3`.
|
|
508
|
+
const high: u64 = this.count >> 61
|
|
509
|
+
const low: u64 = this.count << 3
|
|
510
|
+
let shift: u64 = 0
|
|
511
|
+
for (let i: i32 = 127; i >= 120 && i < room; i--) {
|
|
512
|
+
block[i] = toU8(low >> shift)
|
|
513
|
+
block[i - 8] = toU8(high >> shift)
|
|
514
|
+
shift += 8
|
|
515
|
+
}
|
|
454
516
|
this.compress(block, OFFSET_ZERO)
|
|
455
|
-
// Both sizes are whole words, so the digest is the first `
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
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
|
|
459
532
|
}
|
|
460
|
-
return out
|
|
461
533
|
}
|
|
462
534
|
}
|
|
463
535
|
|
|
@@ -514,7 +586,9 @@ export class Sha512 {
|
|
|
514
586
|
* @returns A fresh 64-byte digest, which the caller owns.
|
|
515
587
|
*/
|
|
516
588
|
digest(): u8[] {
|
|
517
|
-
|
|
589
|
+
const out: u8[] = new Array<u8>(SHA512_SIZE)
|
|
590
|
+
this.engine.finish(out)
|
|
591
|
+
return out
|
|
518
592
|
}
|
|
519
593
|
}
|
|
520
594
|
|
|
@@ -570,40 +644,35 @@ export class Sha384 {
|
|
|
570
644
|
* @returns A fresh 48-byte digest, which the caller owns.
|
|
571
645
|
*/
|
|
572
646
|
digest(): u8[] {
|
|
573
|
-
|
|
647
|
+
const out: u8[] = new Array<u8>(SHA384_SIZE)
|
|
648
|
+
this.engine.finish(out)
|
|
649
|
+
return out
|
|
574
650
|
}
|
|
575
651
|
}
|
|
576
652
|
|
|
577
653
|
/**
|
|
578
654
|
* The SHA-512 digest of all of `data`, as a fresh 64-byte array. An array
|
|
579
|
-
* longer than 2^31 - 1 bytes
|
|
580
|
-
*
|
|
581
|
-
* would be of a prefix.
|
|
655
|
+
* longer than 2^31 - 1 bytes (possible under `--number-mode f64`) is hashed
|
|
656
|
+
* whole, never cut to a prefix.
|
|
582
657
|
*
|
|
583
658
|
* @param data The whole message; it is read, not changed.
|
|
584
659
|
* @returns A fresh 64-byte digest.
|
|
585
660
|
*/
|
|
586
661
|
export const sha512 = (data: u8[]): u8[] => {
|
|
587
|
-
if (data.length > 2147483647) {
|
|
588
|
-
panic("sha512: a message longer than 2^31 - 1 bytes")
|
|
589
|
-
}
|
|
590
662
|
const hasher = new Sha512()
|
|
591
|
-
hasher.
|
|
663
|
+
hasher.engine.absorbAll(data)
|
|
592
664
|
return hasher.digest()
|
|
593
665
|
}
|
|
594
666
|
|
|
595
667
|
/**
|
|
596
668
|
* The SHA-384 digest of all of `data`, as a fresh 48-byte array. An array
|
|
597
|
-
* longer than 2^31 - 1 bytes
|
|
669
|
+
* longer than 2^31 - 1 bytes is hashed whole, as in `sha512`.
|
|
598
670
|
*
|
|
599
671
|
* @param data The whole message; it is read, not changed.
|
|
600
672
|
* @returns A fresh 48-byte digest.
|
|
601
673
|
*/
|
|
602
674
|
export const sha384 = (data: u8[]): u8[] => {
|
|
603
|
-
if (data.length > 2147483647) {
|
|
604
|
-
panic("sha384: a message longer than 2^31 - 1 bytes")
|
|
605
|
-
}
|
|
606
675
|
const hasher = new Sha384()
|
|
607
|
-
hasher.
|
|
676
|
+
hasher.engine.absorbAll(data)
|
|
608
677
|
return hasher.digest()
|
|
609
678
|
}
|
|
@@ -54,9 +54,15 @@ export const x509DrawP256Key = (): Secret<u8[]> => {
|
|
|
54
54
|
* @returns The 20 serial octets, big-endian, in a fresh array.
|
|
55
55
|
*/
|
|
56
56
|
export const x509DrawSerial = (): u8[] => {
|
|
57
|
-
const
|
|
58
|
-
crypto.getRandomValues(
|
|
59
|
-
|
|
57
|
+
const drawn: u8[] = new Array<u8>(X509_MAX_SERIAL)
|
|
58
|
+
crypto.getRandomValues(drawn)
|
|
59
|
+
// Copied rather than indexed: a call forgets the length the bounds proof
|
|
60
|
+
// knew, and a for-of reads each drawn octet without an index to prove, so
|
|
61
|
+
// every octet of the serial is one the CSPRNG drew.
|
|
62
|
+
const serial: u8[] = []
|
|
63
|
+
for (const b of drawn) {
|
|
64
|
+
serial.push(serial.length === 0 ? (b & toU8(0x3f)) | toU8(0x40) : b)
|
|
65
|
+
}
|
|
60
66
|
return serial
|
|
61
67
|
}
|
|
62
68
|
|