@amritk/nish-x86_64-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.
- package/bin/nish +0 -0
- package/docs/AI.md +4 -1
- package/package.json +1 -1
- package/runtime/nish.d.ts +82 -2
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +7 -7
- package/std/base64.ts +61 -95
- package/std/bytes.ts +6 -4
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +193 -100
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +483 -148
- package/std/crypto/jwt.ts +19 -21
- package/std/crypto/p256.ts +5 -7
- package/std/crypto/pki-roots.ts +28 -8
- package/std/crypto/rsa-sign.ts +5 -2
- package/std/crypto/sha256.ts +81 -26
- package/std/crypto/sha512.ts +135 -49
- package/std/json-value.ts +14 -15
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/fetch.ts +108 -27
- package/std/net/handler.ts +11 -22
- package/std/net/hpack.ts +38 -19
- package/std/net/http-compress.ts +21 -12
- package/std/net/http-server.ts +9 -4
- package/std/net/http1-client-codec.ts +22 -15
- package/std/net/http1-codec.ts +37 -36
- package/std/net/http3-server.ts +127 -45
- package/std/net/http3.ts +99 -50
- package/std/net/postgres-async.ts +5 -10
- package/std/net/qpack.ts +83 -17
- package/std/net/quic-listener.ts +42 -27
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-stream.ts +29 -22
- package/std/net/quic.ts +48 -23
- package/std/net/static.ts +5 -3
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +54 -22
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls.ts +42 -12
- package/std/net/upstream.ts +18 -19
- package/std/net/url.ts +78 -21
- package/std/net/websocket.ts +3 -11
- package/std/net/webtransport.ts +95 -21
- package/std/raster.ts +12 -2
- package/std/term-pixels.ts +18 -2
- package/std/text.ts +5 -5
package/std/crypto/hmac.ts
CHANGED
|
@@ -19,13 +19,15 @@
|
|
|
19
19
|
* mac.update(record, off, len);
|
|
20
20
|
* const tag2: u8[] = mac.digest();
|
|
21
21
|
*
|
|
22
|
-
* A `digest` ends the computation
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* `
|
|
22
|
+
* A `digest` ends the computation. For `HmacSha256`, a later `update` answers
|
|
23
|
+
* `false` and absorbs nothing, a second `digest` answers an empty array, a
|
|
24
|
+
* second `digestInto` answers `false`, and a window outside its buffer answers
|
|
25
|
+
* `false` with nothing absorbed or written. `HmacSha384` and `HmacSha512`
|
|
26
|
+
* answer the same way, `digest` an empty array once the tag is taken, and so
|
|
27
|
+
* do the scratch classes' `begin`, `update` and `finishInto`: each answers
|
|
28
|
+
* `false` where it once panicked, and its `try` twin is the same answer. The
|
|
29
|
+
* only branch on the key is on its length, which HMAC does not keep secret; a
|
|
30
|
+
* received tag is compared with `timingSafeEqual`, never `===`.
|
|
29
31
|
*
|
|
30
32
|
* **What the key reaches is wiped** (CLAUDE.md, "Security"). The key block
|
|
31
33
|
* is wiped once both hashers have absorbed it, and so is the hash
|
|
@@ -104,7 +106,7 @@ const hmacPadInto = (out: u8[], key: u8[], off: i32, len: i32, pad: i32): void =
|
|
|
104
106
|
* value, its pending block and its message schedule, with `wipe`'s volatile
|
|
105
107
|
* stores, which no optimiser removes however dead the hasher is about to be.
|
|
106
108
|
* The hasher is left finished, so that feeding it before it is started again
|
|
107
|
-
*
|
|
109
|
+
* answers `false` and absorbs nothing, as feeding a spent one always has.
|
|
108
110
|
*/
|
|
109
111
|
const hmacWipeSha256 = (h: Sha256): void => {
|
|
110
112
|
wipe(h.state)
|
|
@@ -147,9 +149,9 @@ const hmacWindowFits = (length: i32, off: i32, len: i32): boolean =>
|
|
|
147
149
|
const hmacPadsSha256 = (inner: Sha256, outer: Sha256, k: u8[]): void => {
|
|
148
150
|
const pad: u8[] = new Array<u8>(SHA256_BLOCK)
|
|
149
151
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_IPAD)
|
|
150
|
-
inner.
|
|
152
|
+
inner.absorbWhole(pad)
|
|
151
153
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_OPAD)
|
|
152
|
-
outer.
|
|
154
|
+
outer.absorbWhole(pad)
|
|
153
155
|
wipe(pad)
|
|
154
156
|
}
|
|
155
157
|
|
|
@@ -164,7 +166,7 @@ const hmacKeySha256 = (inner: Sha256, outer: Sha256, key: u8[]): void => {
|
|
|
164
166
|
return
|
|
165
167
|
}
|
|
166
168
|
const hasher = new Sha256()
|
|
167
|
-
hasher.
|
|
169
|
+
hasher.absorbWhole(key)
|
|
168
170
|
const hashed: u8[] = hasher.digest()
|
|
169
171
|
hmacWipeSha256(hasher)
|
|
170
172
|
hmacPadsSha256(inner, outer, hashed)
|
|
@@ -175,9 +177,9 @@ const hmacKeySha256 = (inner: Sha256, outer: Sha256, key: u8[]): void => {
|
|
|
175
177
|
const hmacPadsSha384 = (inner: Sha384, outer: Sha384, k: u8[]): void => {
|
|
176
178
|
const pad: u8[] = new Array<u8>(SHA512_BLOCK)
|
|
177
179
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_IPAD)
|
|
178
|
-
inner.
|
|
180
|
+
inner.engine.absorbWhole(pad)
|
|
179
181
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_OPAD)
|
|
180
|
-
outer.
|
|
182
|
+
outer.engine.absorbWhole(pad)
|
|
181
183
|
wipe(pad)
|
|
182
184
|
}
|
|
183
185
|
|
|
@@ -192,7 +194,7 @@ const hmacKeySha384 = (inner: Sha384, outer: Sha384, key: u8[]): void => {
|
|
|
192
194
|
return
|
|
193
195
|
}
|
|
194
196
|
const hasher = new Sha384()
|
|
195
|
-
hasher.engine.
|
|
197
|
+
hasher.engine.absorbWhole(key)
|
|
196
198
|
const hashed: u8[] = hasher.digest()
|
|
197
199
|
hmacWipeSha384(hasher)
|
|
198
200
|
hmacPadsSha384(inner, outer, hashed)
|
|
@@ -203,9 +205,9 @@ const hmacKeySha384 = (inner: Sha384, outer: Sha384, key: u8[]): void => {
|
|
|
203
205
|
const hmacPadsSha512 = (inner: Sha512, outer: Sha512, k: u8[]): void => {
|
|
204
206
|
const pad: u8[] = new Array<u8>(SHA512_BLOCK)
|
|
205
207
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_IPAD)
|
|
206
|
-
inner.
|
|
208
|
+
inner.engine.absorbWhole(pad)
|
|
207
209
|
hmacPadInto(pad, k, HMAC_FROM, toI32(k.length), HMAC_OPAD)
|
|
208
|
-
outer.
|
|
210
|
+
outer.engine.absorbWhole(pad)
|
|
209
211
|
wipe(pad)
|
|
210
212
|
}
|
|
211
213
|
|
|
@@ -220,7 +222,7 @@ const hmacKeySha512 = (inner: Sha512, outer: Sha512, key: u8[]): void => {
|
|
|
220
222
|
return
|
|
221
223
|
}
|
|
222
224
|
const hasher = new Sha512()
|
|
223
|
-
hasher.engine.
|
|
225
|
+
hasher.engine.absorbWhole(key)
|
|
224
226
|
const hashed: u8[] = hasher.digest()
|
|
225
227
|
hmacWipeSha512(hasher)
|
|
226
228
|
hmacPadsSha512(inner, outer, hashed)
|
|
@@ -261,44 +263,123 @@ export class HmacSha256 {
|
|
|
261
263
|
}
|
|
262
264
|
|
|
263
265
|
/**
|
|
264
|
-
* Absorbs `data[off .. off + len)
|
|
265
|
-
* `
|
|
266
|
+
* Absorbs `data[off .. off + len)`. A window outside `data`, or any window
|
|
267
|
+
* once the tag is taken, absorbs nothing and answers `false`, as
|
|
268
|
+
* `Sha256.update` does: check it, since a tag over the wrong bytes looks
|
|
269
|
+
* exactly like a tag over the right ones.
|
|
266
270
|
*
|
|
267
271
|
* @param data The buffer holding the window; it is read, not kept.
|
|
268
272
|
* @param off Where the window starts in `data`.
|
|
269
273
|
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
274
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
275
|
+
* absorbed, for a window outside `data` or a computation whose tag is
|
|
276
|
+
* taken.
|
|
270
277
|
*/
|
|
271
|
-
update(data: u8[], off: i32, len: i32):
|
|
272
|
-
this.inner.update(data, off, len)
|
|
278
|
+
update(data: u8[], off: i32, len: i32): boolean {
|
|
279
|
+
return this.inner.update(data, off, len)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* The same answer as `update`, kept for the callers written when `update`
|
|
284
|
+
* had none: a window outside `data`, or any window once the tag is taken,
|
|
285
|
+
* absorbs nothing and answers `false`.
|
|
286
|
+
*
|
|
287
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
288
|
+
* @param off Where the window starts in `data`.
|
|
289
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
290
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
291
|
+
* absorbed, for a window outside `data` or a computation whose tag is
|
|
292
|
+
* taken.
|
|
293
|
+
*/
|
|
294
|
+
tryUpdate(data: u8[], off: i32, len: i32): boolean {
|
|
295
|
+
return this.inner.tryUpdate(data, off, len)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* `update` of all of `data`, past the tag test, for a caller that made this
|
|
300
|
+
* computation itself and has not taken its tag (the HKDF expansion). The
|
|
301
|
+
* whole array is the window, so there is no window to get wrong.
|
|
302
|
+
*
|
|
303
|
+
* @internal Called by `nish/crypto/hkdf`.
|
|
304
|
+
*/
|
|
305
|
+
absorbWhole(data: u8[]): void {
|
|
306
|
+
this.inner.absorbWhole(data)
|
|
273
307
|
}
|
|
274
308
|
|
|
275
309
|
/**
|
|
276
310
|
* Ends the computation and wipes both hashers and the inner digest. The tag
|
|
277
|
-
* is the one allocation.
|
|
311
|
+
* is the one allocation. A second `digest` or `digestInto` answers no tag:
|
|
312
|
+
* the hashers are wiped, and what they would answer matches nothing.
|
|
278
313
|
*
|
|
279
|
-
* @returns A fresh 32-byte tag, which the caller owns
|
|
314
|
+
* @returns A fresh 32-byte tag, which the caller owns; an empty array once
|
|
315
|
+
* the tag is taken.
|
|
280
316
|
*/
|
|
281
317
|
digest(): u8[] {
|
|
282
318
|
const tag: u8[] = new Array<u8>(SHA256_SIZE)
|
|
283
|
-
this.
|
|
319
|
+
if (!this.checkedTagInto(tag, HMAC_FROM)) {
|
|
320
|
+
return new Array<u8>(0)
|
|
321
|
+
}
|
|
284
322
|
return tag
|
|
285
323
|
}
|
|
286
324
|
|
|
287
325
|
/**
|
|
288
326
|
* Writes the 32-byte tag at `out[at .. at + 32)`, ends the computation and
|
|
289
327
|
* wipes both hashers and the inner digest, with nothing allocated. A window
|
|
290
|
-
* outside `out`
|
|
291
|
-
*
|
|
328
|
+
* outside `out` answers `false` with nothing written and the computation
|
|
329
|
+
* left as it was, rather than writing the part of the tag that fits. A
|
|
330
|
+
* second `digest` or `digestInto` writes nothing and answers `false`.
|
|
292
331
|
*
|
|
293
332
|
* @param out The caller's array the tag is written into.
|
|
294
333
|
* @param at Where the 32 tag bytes start in `out`.
|
|
334
|
+
* @returns `true` once the tag is written; `false`, with `out` untouched,
|
|
335
|
+
* for a window outside `out` or once the tag is taken.
|
|
295
336
|
*/
|
|
296
|
-
digestInto(out: u8[], at: i32):
|
|
337
|
+
digestInto(out: u8[], at: i32): boolean {
|
|
297
338
|
if (!hmacWindowFits(toI32(out.length), at, SHA256_SIZE)) {
|
|
298
|
-
|
|
339
|
+
return false
|
|
299
340
|
}
|
|
341
|
+
return this.checkedTagInto(out, at)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* `tagInto` with the inner digest absorbed by the outer hasher's `update`,
|
|
346
|
+
* which answers `false` on a computation whose tag is already taken: then
|
|
347
|
+
* nothing is written to `out`.
|
|
348
|
+
*
|
|
349
|
+
* @param out The array the tag is written into, with 32 bytes free at `at`.
|
|
350
|
+
* @param at Where the 32 tag bytes start in `out`.
|
|
351
|
+
* @returns Whether the tag was written: `false` once it is taken.
|
|
352
|
+
*/
|
|
353
|
+
private checkedTagInto(out: u8[], at: i32): boolean {
|
|
354
|
+
this.inner.digestInto(this.innerDigest, HMAC_FROM)
|
|
355
|
+
const fresh: boolean = this.outer.update(this.innerDigest, HMAC_FROM, SHA256_SIZE)
|
|
356
|
+
wipe(this.innerDigest)
|
|
357
|
+
if (!fresh) {
|
|
358
|
+
return false
|
|
359
|
+
}
|
|
360
|
+
this.outer.digestInto(out, at)
|
|
361
|
+
hmacWipeSha256(this.inner)
|
|
362
|
+
hmacWipeSha256(this.outer)
|
|
363
|
+
return true
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @internal `digestInto` past its tests, for `digest`, `hmacSha256` and
|
|
368
|
+
* `hkdfExpandSha256`, each on a computation whose tag it has not taken and
|
|
369
|
+
* with 32 bytes free at `at`. The hashers' `digestInto` re-test the window
|
|
370
|
+
* and write nothing outside it.
|
|
371
|
+
*
|
|
372
|
+
* @param out The array the tag is written into, with 32 bytes free at `at`.
|
|
373
|
+
* @param at Where the 32 tag bytes start in `out`.
|
|
374
|
+
*/
|
|
375
|
+
tagInto(out: u8[], at: i32): void {
|
|
300
376
|
this.inner.digestInto(this.innerDigest, HMAC_FROM)
|
|
301
|
-
|
|
377
|
+
// `absorbWindow`, not `absorbWhole`: the windowed absorb `update` makes,
|
|
378
|
+
// so LTO keeps the block offset's range that sha256's hot loop relies on
|
|
379
|
+
// (an `absorbWhole` caller here cost crypto-sha256 0.1%). The outer hasher
|
|
380
|
+
// is this computation's own and untouched, and the window is the whole
|
|
381
|
+
// 32-byte inner digest.
|
|
382
|
+
this.outer.absorbWindow(this.innerDigest, HMAC_FROM, SHA256_SIZE)
|
|
302
383
|
wipe(this.innerDigest)
|
|
303
384
|
this.outer.digestInto(out, at)
|
|
304
385
|
hmacWipeSha256(this.inner)
|
|
@@ -330,25 +411,79 @@ export class HmacSha384 {
|
|
|
330
411
|
}
|
|
331
412
|
|
|
332
413
|
/**
|
|
333
|
-
* Absorbs `data[off .. off + len)
|
|
334
|
-
* `
|
|
414
|
+
* Absorbs `data[off .. off + len)`. A window outside `data`, or any window
|
|
415
|
+
* once the tag is taken, absorbs nothing and answers `false`, as
|
|
416
|
+
* `Sha384.update` does: check it, since a tag over the wrong bytes looks
|
|
417
|
+
* exactly like a tag over the right ones.
|
|
335
418
|
*
|
|
336
419
|
* @param data The buffer holding the window; it is read, not kept.
|
|
337
420
|
* @param off Where the window starts in `data`.
|
|
338
421
|
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
422
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
423
|
+
* absorbed, for a window outside `data` or a computation whose tag is
|
|
424
|
+
* taken.
|
|
339
425
|
*/
|
|
340
|
-
update(data: u8[], off: i32, len: i32):
|
|
341
|
-
this.inner.update(data, off, len)
|
|
426
|
+
update(data: u8[], off: i32, len: i32): boolean {
|
|
427
|
+
return this.inner.update(data, off, len)
|
|
342
428
|
}
|
|
343
429
|
|
|
344
430
|
/**
|
|
345
|
-
*
|
|
431
|
+
* The same answer as `update`, kept for the callers written when `update`
|
|
432
|
+
* had none: a window outside `data`, or any window once the tag is taken,
|
|
433
|
+
* absorbs nothing and answers `false`.
|
|
346
434
|
*
|
|
347
|
-
* @
|
|
435
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
436
|
+
* @param off Where the window starts in `data`.
|
|
437
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
438
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
439
|
+
* absorbed, for a window outside `data` or a computation whose tag is
|
|
440
|
+
* taken.
|
|
441
|
+
*/
|
|
442
|
+
tryUpdate(data: u8[], off: i32, len: i32): boolean {
|
|
443
|
+
return this.inner.tryUpdate(data, off, len)
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* `update` of all of `data`, past the tag test, for a caller that made this
|
|
448
|
+
* computation itself and has not taken its tag (the HKDF expansion). The
|
|
449
|
+
* whole array is the window, so there is no window to get wrong.
|
|
450
|
+
*
|
|
451
|
+
* @internal Called by `nish/crypto/hkdf`.
|
|
452
|
+
*/
|
|
453
|
+
absorbWhole(data: u8[]): void {
|
|
454
|
+
this.inner.engine.absorbWhole(data)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Ends the computation and wipes both hashers. A second `digest` answers no
|
|
459
|
+
* tag: the hashers are wiped, and what they would answer matches nothing.
|
|
460
|
+
*
|
|
461
|
+
* @returns A fresh 48-byte tag, which the caller owns; an empty array once
|
|
462
|
+
* the tag is taken.
|
|
348
463
|
*/
|
|
349
464
|
digest(): u8[] {
|
|
350
465
|
const innerDigest: u8[] = this.inner.digest()
|
|
351
|
-
|
|
466
|
+
// `false` once the tag is taken: the wiped outer hasher is finished.
|
|
467
|
+
const fresh: boolean = this.outer.update(innerDigest, HMAC_FROM, SHA384_SIZE)
|
|
468
|
+
wipe(innerDigest)
|
|
469
|
+
if (!fresh) {
|
|
470
|
+
return new Array<u8>(0)
|
|
471
|
+
}
|
|
472
|
+
const tag: u8[] = this.outer.digest()
|
|
473
|
+
hmacWipeSha384(this.inner)
|
|
474
|
+
hmacWipeSha384(this.outer)
|
|
475
|
+
return tag
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* @internal `digest` past its test, for `digest`, `hmacSha384` and the HKDF
|
|
480
|
+
* expansion, each on a computation whose tag it has not taken.
|
|
481
|
+
*
|
|
482
|
+
* @returns A fresh 48-byte tag, which the caller owns.
|
|
483
|
+
*/
|
|
484
|
+
tag(): u8[] {
|
|
485
|
+
const innerDigest: u8[] = this.inner.digest()
|
|
486
|
+
this.outer.engine.absorbWhole(innerDigest)
|
|
352
487
|
wipe(innerDigest)
|
|
353
488
|
const tag: u8[] = this.outer.digest()
|
|
354
489
|
hmacWipeSha384(this.inner)
|
|
@@ -381,25 +516,68 @@ export class HmacSha512 {
|
|
|
381
516
|
}
|
|
382
517
|
|
|
383
518
|
/**
|
|
384
|
-
* Absorbs `data[off .. off + len)
|
|
385
|
-
* `
|
|
519
|
+
* Absorbs `data[off .. off + len)`. A window outside `data`, or any window
|
|
520
|
+
* once the tag is taken, absorbs nothing and answers `false`, as
|
|
521
|
+
* `Sha512.update` does: check it, since a tag over the wrong bytes looks
|
|
522
|
+
* exactly like a tag over the right ones.
|
|
386
523
|
*
|
|
387
524
|
* @param data The buffer holding the window; it is read, not kept.
|
|
388
525
|
* @param off Where the window starts in `data`.
|
|
389
526
|
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
527
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
528
|
+
* absorbed, for a window outside `data` or a computation whose tag is
|
|
529
|
+
* taken.
|
|
390
530
|
*/
|
|
391
|
-
update(data: u8[], off: i32, len: i32):
|
|
392
|
-
this.inner.update(data, off, len)
|
|
531
|
+
update(data: u8[], off: i32, len: i32): boolean {
|
|
532
|
+
return this.inner.update(data, off, len)
|
|
393
533
|
}
|
|
394
534
|
|
|
395
535
|
/**
|
|
396
|
-
*
|
|
536
|
+
* The same answer as `update`, kept for the callers written when `update`
|
|
537
|
+
* had none: a window outside `data`, or any window once the tag is taken,
|
|
538
|
+
* absorbs nothing and answers `false`.
|
|
397
539
|
*
|
|
398
|
-
* @
|
|
540
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
541
|
+
* @param off Where the window starts in `data`.
|
|
542
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
543
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
544
|
+
* absorbed, for a window outside `data` or a computation whose tag is
|
|
545
|
+
* taken.
|
|
546
|
+
*/
|
|
547
|
+
tryUpdate(data: u8[], off: i32, len: i32): boolean {
|
|
548
|
+
return this.inner.tryUpdate(data, off, len)
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Ends the computation and wipes both hashers. A second `digest` answers no
|
|
553
|
+
* tag: the hashers are wiped, and what they would answer matches nothing.
|
|
554
|
+
*
|
|
555
|
+
* @returns A fresh 64-byte tag, which the caller owns; an empty array once
|
|
556
|
+
* the tag is taken.
|
|
399
557
|
*/
|
|
400
558
|
digest(): u8[] {
|
|
401
559
|
const innerDigest: u8[] = this.inner.digest()
|
|
402
|
-
|
|
560
|
+
// `false` once the tag is taken: the wiped outer hasher is finished.
|
|
561
|
+
const fresh: boolean = this.outer.update(innerDigest, HMAC_FROM, SHA512_SIZE)
|
|
562
|
+
wipe(innerDigest)
|
|
563
|
+
if (!fresh) {
|
|
564
|
+
return new Array<u8>(0)
|
|
565
|
+
}
|
|
566
|
+
const tag: u8[] = this.outer.digest()
|
|
567
|
+
hmacWipeSha512(this.inner)
|
|
568
|
+
hmacWipeSha512(this.outer)
|
|
569
|
+
return tag
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* @internal `digest` past its test, for `digest`, `hmacSha512` and the HKDF
|
|
574
|
+
* expansion, each on a computation whose tag it has not taken.
|
|
575
|
+
*
|
|
576
|
+
* @returns A fresh 64-byte tag, which the caller owns.
|
|
577
|
+
*/
|
|
578
|
+
tag(): u8[] {
|
|
579
|
+
const innerDigest: u8[] = this.inner.digest()
|
|
580
|
+
this.outer.engine.absorbWhole(innerDigest)
|
|
403
581
|
wipe(innerDigest)
|
|
404
582
|
const tag: u8[] = this.outer.digest()
|
|
405
583
|
hmacWipeSha512(this.inner)
|
|
@@ -422,8 +600,10 @@ export class HmacSha512 {
|
|
|
422
600
|
*/
|
|
423
601
|
export const hmacSha256 = (key: u8[], data: u8[]): u8[] => {
|
|
424
602
|
const mac = new HmacSha256(key)
|
|
425
|
-
mac.inner.
|
|
426
|
-
|
|
603
|
+
mac.inner.absorbWhole(data)
|
|
604
|
+
const tag: u8[] = new Array<u8>(SHA256_SIZE)
|
|
605
|
+
mac.tagInto(tag, HMAC_FROM)
|
|
606
|
+
return tag
|
|
427
607
|
}
|
|
428
608
|
|
|
429
609
|
/**
|
|
@@ -436,8 +616,8 @@ export const hmacSha256 = (key: u8[], data: u8[]): u8[] => {
|
|
|
436
616
|
*/
|
|
437
617
|
export const hmacSha384 = (key: u8[], data: u8[]): u8[] => {
|
|
438
618
|
const mac = new HmacSha384(key)
|
|
439
|
-
mac.inner.engine.
|
|
440
|
-
return mac.
|
|
619
|
+
mac.inner.engine.absorbWhole(data)
|
|
620
|
+
return mac.tag()
|
|
441
621
|
}
|
|
442
622
|
|
|
443
623
|
/**
|
|
@@ -450,8 +630,8 @@ export const hmacSha384 = (key: u8[], data: u8[]): u8[] => {
|
|
|
450
630
|
*/
|
|
451
631
|
export const hmacSha512 = (key: u8[], data: u8[]): u8[] => {
|
|
452
632
|
const mac = new HmacSha512(key)
|
|
453
|
-
mac.inner.engine.
|
|
454
|
-
return mac.
|
|
633
|
+
mac.inner.engine.absorbWhole(data)
|
|
634
|
+
return mac.tag()
|
|
455
635
|
}
|
|
456
636
|
|
|
457
637
|
/**
|
|
@@ -597,8 +777,10 @@ export const hmacCopySha384 = (from: Sha384, to: Sha384): void => {
|
|
|
597
777
|
*
|
|
598
778
|
* The key block is wiped with `secureZero` once both hashers have absorbed
|
|
599
779
|
* it, and so is every digest on the way; `wipe` zeroes the hashers too, for a
|
|
600
|
-
* caller done with the key. A key or tag window outside its array
|
|
601
|
-
* a
|
|
780
|
+
* caller done with the key. A key, message or tag window outside its array
|
|
781
|
+
* does nothing and answers `false`, and after a key window that does not fit,
|
|
782
|
+
* `finishInto` answers `false` until a `begin` succeeds. `tryBegin`,
|
|
783
|
+
* `tryUpdate` and `tryFinishInto` are the same answers under older names.
|
|
602
784
|
*/
|
|
603
785
|
export class HmacSha256Scratch {
|
|
604
786
|
/** H(K XOR ipad, …): keyed by `begin`, fed the message by `update`. */
|
|
@@ -621,33 +803,67 @@ export class HmacSha256Scratch {
|
|
|
621
803
|
this.fresh = new Sha256()
|
|
622
804
|
this.pad = new Array<u8>(SHA256_BLOCK)
|
|
623
805
|
this.keyHash = new Array<u8>(SHA256_SIZE)
|
|
806
|
+
// Unkeyed until `begin`: `finishInto` refuses a finished outer hasher.
|
|
807
|
+
this.outer.finished = true
|
|
624
808
|
}
|
|
625
809
|
|
|
626
810
|
/**
|
|
627
811
|
* Keys a new computation with `key[off .. off + len)`, a key longer than 64
|
|
628
|
-
* bytes by its SHA-256 (RFC 2104 §2).
|
|
812
|
+
* bytes by its SHA-256 (RFC 2104 §2). A key window outside `key` keys
|
|
813
|
+
* nothing and answers `false`, after which `finishInto` writes no tag until
|
|
814
|
+
* a `begin` succeeds.
|
|
629
815
|
*
|
|
630
816
|
* @param key The buffer holding the key; it is read, not kept.
|
|
631
817
|
* @param off Where the key starts in `key`.
|
|
632
818
|
* @param len The key's length in bytes.
|
|
819
|
+
* @returns `true` once the hashers are keyed; `false` for a key window
|
|
820
|
+
* outside `key`.
|
|
633
821
|
*/
|
|
634
|
-
begin(key: u8[], off: i32, len: i32):
|
|
822
|
+
begin(key: u8[], off: i32, len: i32): boolean {
|
|
635
823
|
if (!hmacWindowFits(toI32(key.length), off, len)) {
|
|
636
|
-
|
|
824
|
+
// A finished outer hasher is what `finishInto` refuses: no tag until a
|
|
825
|
+
// `begin` succeeds.
|
|
826
|
+
this.outer.finished = true
|
|
827
|
+
return false
|
|
637
828
|
}
|
|
638
829
|
if (len <= SHA256_BLOCK) {
|
|
639
|
-
this.keyBlocks(key, off, len)
|
|
640
|
-
|
|
830
|
+
if (!this.keyBlocks(key, off, len)) {
|
|
831
|
+
this.outer.finished = true
|
|
832
|
+
return false
|
|
833
|
+
}
|
|
834
|
+
return true
|
|
641
835
|
}
|
|
642
836
|
hmacCopySha256(this.fresh, this.inner)
|
|
643
|
-
this.inner.
|
|
837
|
+
if (!this.inner.tryUpdate(key, off, len)) {
|
|
838
|
+
this.outer.finished = true
|
|
839
|
+
return false
|
|
840
|
+
}
|
|
644
841
|
const digest: u8[] = this.inner.digest()
|
|
645
842
|
for (let i: i32 = 0; i < toI32(digest.length) && i < toI32(this.keyHash.length); i += 1) {
|
|
646
843
|
this.keyHash[i] = digest[i]
|
|
647
844
|
}
|
|
648
845
|
secureZero(digest)
|
|
649
|
-
this.keyBlocks(this.keyHash, HMAC_FROM, SHA256_SIZE)
|
|
846
|
+
const keyed: boolean = this.keyBlocks(this.keyHash, HMAC_FROM, SHA256_SIZE)
|
|
650
847
|
secureZero(this.keyHash)
|
|
848
|
+
if (!keyed) {
|
|
849
|
+
this.outer.finished = true
|
|
850
|
+
return false
|
|
851
|
+
}
|
|
852
|
+
return true
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/**
|
|
856
|
+
* The same answer as `begin`, kept for callers written against it: a key
|
|
857
|
+
* window outside `key` keys nothing and answers `false`.
|
|
858
|
+
*
|
|
859
|
+
* @param key The buffer holding the key; it is read, not kept.
|
|
860
|
+
* @param off Where the key starts in `key`.
|
|
861
|
+
* @param len The key's length in bytes.
|
|
862
|
+
* @returns `true` once the hashers are keyed; `false` for a key window
|
|
863
|
+
* outside `key`.
|
|
864
|
+
*/
|
|
865
|
+
tryBegin(key: u8[], off: i32, len: i32): boolean {
|
|
866
|
+
return this.begin(key, off, len)
|
|
651
867
|
}
|
|
652
868
|
|
|
653
869
|
/**
|
|
@@ -655,48 +871,77 @@ export class HmacSha256Scratch {
|
|
|
655
871
|
* inner hasher and XOR opad into the outer one, each started again, and
|
|
656
872
|
* wipes the block. A method of its own so that `begin` never keeps the
|
|
657
873
|
* caller's key in a local it reassigns, which would make the key look
|
|
658
|
-
* stored to the arena analysis.
|
|
874
|
+
* stored to the arena analysis. Answers whether both hashers took their
|
|
875
|
+
* block, which a fresh hasher and a whole pad always do.
|
|
659
876
|
*/
|
|
660
|
-
private keyBlocks(key: u8[], off: i32, len: i32):
|
|
877
|
+
private keyBlocks(key: u8[], off: i32, len: i32): boolean {
|
|
661
878
|
// Starting a hasher again copies only the live part of `fresh`, so the
|
|
662
879
|
// last computation's block and schedule are wiped first (K1-7).
|
|
663
880
|
hmacWipeSha256(this.inner)
|
|
664
881
|
hmacWipeSha256(this.outer)
|
|
665
882
|
hmacPadInto(this.pad, key, off, len, HMAC_IPAD)
|
|
666
883
|
hmacCopySha256(this.fresh, this.inner)
|
|
667
|
-
this.inner.
|
|
884
|
+
const inner: boolean = this.inner.tryUpdate(this.pad, HMAC_FROM, SHA256_BLOCK)
|
|
668
885
|
hmacPadInto(this.pad, key, off, len, HMAC_OPAD)
|
|
669
886
|
hmacCopySha256(this.fresh, this.outer)
|
|
670
|
-
this.outer.
|
|
887
|
+
const outer: boolean = this.outer.tryUpdate(this.pad, HMAC_FROM, SHA256_BLOCK)
|
|
671
888
|
secureZero(this.pad)
|
|
889
|
+
return inner && outer
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Absorbs `data[off .. off + len)`; a window outside `data` absorbs
|
|
894
|
+
* nothing and answers `false`, as `Sha256.update` does.
|
|
895
|
+
*
|
|
896
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
897
|
+
* @param off Where the window starts in `data`.
|
|
898
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
899
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
900
|
+
* absorbed, for a window outside `data` or a finished computation.
|
|
901
|
+
*/
|
|
902
|
+
update(data: u8[], off: i32, len: i32): boolean {
|
|
903
|
+
return this.inner.update(data, off, len)
|
|
672
904
|
}
|
|
673
905
|
|
|
674
906
|
/**
|
|
675
|
-
*
|
|
676
|
-
*
|
|
907
|
+
* `update` for a caller that handles a bad window, as `Sha256.tryUpdate`
|
|
908
|
+
* does.
|
|
677
909
|
*
|
|
678
910
|
* @param data The buffer holding the window; it is read, not kept.
|
|
679
911
|
* @param off Where the window starts in `data`.
|
|
680
912
|
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
913
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
914
|
+
* absorbed, for a window outside `data` or a computation already
|
|
915
|
+
* finished.
|
|
681
916
|
*/
|
|
682
|
-
|
|
683
|
-
this.inner.
|
|
917
|
+
tryUpdate(data: u8[], off: i32, len: i32): boolean {
|
|
918
|
+
return this.inner.tryUpdate(data, off, len)
|
|
684
919
|
}
|
|
685
920
|
|
|
686
921
|
/**
|
|
687
|
-
* Writes the 32-byte tag at `out[at]` and ends the computation
|
|
688
|
-
* outside `out
|
|
922
|
+
* Writes the 32-byte tag at `out[at]` and ends the computation. It writes
|
|
923
|
+
* nothing and answers `false` for a window outside `out`, or until a
|
|
924
|
+
* `begin` keys the
|
|
925
|
+
* hashers again after a tag, a `wipe` or a refused key window.
|
|
689
926
|
*
|
|
690
927
|
* @param out The caller's array the tag is written into.
|
|
691
928
|
* @param at Where the 32 tag bytes start in `out`.
|
|
929
|
+
* @returns `true` once the tag is written; `false`, with `out` untouched,
|
|
930
|
+
* when it is refused.
|
|
692
931
|
*/
|
|
693
|
-
finishInto(out: u8[], at: i32):
|
|
694
|
-
if (!hmacWindowFits(toI32(out.length), at, SHA256_SIZE)) {
|
|
695
|
-
|
|
932
|
+
finishInto(out: u8[], at: i32): boolean {
|
|
933
|
+
if (this.outer.finished || !hmacWindowFits(toI32(out.length), at, SHA256_SIZE)) {
|
|
934
|
+
return false
|
|
696
935
|
}
|
|
697
936
|
const innerDigest: u8[] = this.inner.digest()
|
|
698
|
-
this.outer.
|
|
937
|
+
const fed: boolean = this.outer.tryUpdate(innerDigest, HMAC_FROM, SHA256_SIZE)
|
|
699
938
|
secureZero(innerDigest)
|
|
939
|
+
if (!fed) {
|
|
940
|
+
hmacWipeSha256(this.inner)
|
|
941
|
+
hmacWipeSha256(this.outer)
|
|
942
|
+
this.outer.finished = true
|
|
943
|
+
return false
|
|
944
|
+
}
|
|
700
945
|
const tag: u8[] = this.outer.digest()
|
|
701
946
|
let p: i32 = at
|
|
702
947
|
for (
|
|
@@ -708,6 +953,19 @@ export class HmacSha256Scratch {
|
|
|
708
953
|
p += 1
|
|
709
954
|
}
|
|
710
955
|
secureZero(tag)
|
|
956
|
+
return true
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* The same answer as `finishInto`, kept for callers written against it.
|
|
961
|
+
*
|
|
962
|
+
* @param out The caller's array the tag is written into.
|
|
963
|
+
* @param at Where the tag bytes start in `out`.
|
|
964
|
+
* @returns `true` once the tag is written; `false`, with `out` untouched,
|
|
965
|
+
* when it is refused.
|
|
966
|
+
*/
|
|
967
|
+
tryFinishInto(out: u8[], at: i32): boolean {
|
|
968
|
+
return this.finishInto(out, at)
|
|
711
969
|
}
|
|
712
970
|
|
|
713
971
|
/** Zeroes both keyed hashers, for a caller done with the key; `begin` starts again. */
|
|
@@ -742,33 +1000,67 @@ export class HmacSha384Scratch {
|
|
|
742
1000
|
this.fresh = new Sha384()
|
|
743
1001
|
this.pad = new Array<u8>(SHA512_BLOCK)
|
|
744
1002
|
this.keyHash = new Array<u8>(SHA384_SIZE)
|
|
1003
|
+
// Unkeyed until `begin`: `finishInto` refuses a finished outer hasher.
|
|
1004
|
+
this.outer.engine.finished = true
|
|
745
1005
|
}
|
|
746
1006
|
|
|
747
1007
|
/**
|
|
748
1008
|
* Keys a new computation with `key[off .. off + len)`, a key longer than 128
|
|
749
|
-
* bytes by its SHA-384 (RFC 2104 §2).
|
|
1009
|
+
* bytes by its SHA-384 (RFC 2104 §2). A key window outside `key` keys
|
|
1010
|
+
* nothing and answers `false`, after which `finishInto` writes no tag until
|
|
1011
|
+
* a `begin` succeeds.
|
|
750
1012
|
*
|
|
751
1013
|
* @param key The buffer holding the key; it is read, not kept.
|
|
752
1014
|
* @param off Where the key starts in `key`.
|
|
753
1015
|
* @param len The key's length in bytes.
|
|
1016
|
+
* @returns `true` once the hashers are keyed; `false` for a key window
|
|
1017
|
+
* outside `key`.
|
|
754
1018
|
*/
|
|
755
|
-
begin(key: u8[], off: i32, len: i32):
|
|
1019
|
+
begin(key: u8[], off: i32, len: i32): boolean {
|
|
756
1020
|
if (!hmacWindowFits(toI32(key.length), off, len)) {
|
|
757
|
-
|
|
1021
|
+
// A finished outer hasher is what `finishInto` refuses: no tag until a
|
|
1022
|
+
// `begin` succeeds.
|
|
1023
|
+
this.outer.engine.finished = true
|
|
1024
|
+
return false
|
|
758
1025
|
}
|
|
759
1026
|
if (len <= SHA512_BLOCK) {
|
|
760
|
-
this.keyBlocks(key, off, len)
|
|
761
|
-
|
|
1027
|
+
if (!this.keyBlocks(key, off, len)) {
|
|
1028
|
+
this.outer.engine.finished = true
|
|
1029
|
+
return false
|
|
1030
|
+
}
|
|
1031
|
+
return true
|
|
762
1032
|
}
|
|
763
1033
|
hmacCopySha384(this.fresh, this.inner)
|
|
764
|
-
this.inner.
|
|
1034
|
+
if (!this.inner.tryUpdate(key, off, len)) {
|
|
1035
|
+
this.outer.engine.finished = true
|
|
1036
|
+
return false
|
|
1037
|
+
}
|
|
765
1038
|
const digest: u8[] = this.inner.digest()
|
|
766
1039
|
for (let i: i32 = 0; i < toI32(digest.length) && i < toI32(this.keyHash.length); i += 1) {
|
|
767
1040
|
this.keyHash[i] = digest[i]
|
|
768
1041
|
}
|
|
769
1042
|
secureZero(digest)
|
|
770
|
-
this.keyBlocks(this.keyHash, HMAC_FROM, SHA384_SIZE)
|
|
1043
|
+
const keyed: boolean = this.keyBlocks(this.keyHash, HMAC_FROM, SHA384_SIZE)
|
|
771
1044
|
secureZero(this.keyHash)
|
|
1045
|
+
if (!keyed) {
|
|
1046
|
+
this.outer.engine.finished = true
|
|
1047
|
+
return false
|
|
1048
|
+
}
|
|
1049
|
+
return true
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
/**
|
|
1053
|
+
* The same answer as `begin`, kept for callers written against it: a key
|
|
1054
|
+
* window outside `key` keys nothing and answers `false`.
|
|
1055
|
+
*
|
|
1056
|
+
* @param key The buffer holding the key; it is read, not kept.
|
|
1057
|
+
* @param off Where the key starts in `key`.
|
|
1058
|
+
* @param len The key's length in bytes.
|
|
1059
|
+
* @returns `true` once the hashers are keyed; `false` for a key window
|
|
1060
|
+
* outside `key`.
|
|
1061
|
+
*/
|
|
1062
|
+
tryBegin(key: u8[], off: i32, len: i32): boolean {
|
|
1063
|
+
return this.begin(key, off, len)
|
|
772
1064
|
}
|
|
773
1065
|
|
|
774
1066
|
/**
|
|
@@ -776,48 +1068,77 @@ export class HmacSha384Scratch {
|
|
|
776
1068
|
* inner hasher and XOR opad into the outer one, each started again, and
|
|
777
1069
|
* wipes the block. A method of its own so that `begin` never keeps the
|
|
778
1070
|
* caller's key in a local it reassigns, which would make the key look
|
|
779
|
-
* stored to the arena analysis.
|
|
1071
|
+
* stored to the arena analysis. Answers whether both hashers took their
|
|
1072
|
+
* block, which a fresh hasher and a whole pad always do.
|
|
780
1073
|
*/
|
|
781
|
-
private keyBlocks(key: u8[], off: i32, len: i32):
|
|
1074
|
+
private keyBlocks(key: u8[], off: i32, len: i32): boolean {
|
|
782
1075
|
// Starting a hasher again copies only the live part of `fresh`, so the
|
|
783
1076
|
// last computation's block and schedule are wiped first (K1-7).
|
|
784
1077
|
hmacWipeSha384(this.inner)
|
|
785
1078
|
hmacWipeSha384(this.outer)
|
|
786
1079
|
hmacPadInto(this.pad, key, off, len, HMAC_IPAD)
|
|
787
1080
|
hmacCopySha384(this.fresh, this.inner)
|
|
788
|
-
this.inner.
|
|
1081
|
+
const inner: boolean = this.inner.tryUpdate(this.pad, HMAC_FROM, SHA512_BLOCK)
|
|
789
1082
|
hmacPadInto(this.pad, key, off, len, HMAC_OPAD)
|
|
790
1083
|
hmacCopySha384(this.fresh, this.outer)
|
|
791
|
-
this.outer.
|
|
1084
|
+
const outer: boolean = this.outer.tryUpdate(this.pad, HMAC_FROM, SHA512_BLOCK)
|
|
792
1085
|
secureZero(this.pad)
|
|
1086
|
+
return inner && outer
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* Absorbs `data[off .. off + len)`; a window outside `data` absorbs
|
|
1091
|
+
* nothing and answers `false`, as `Sha384.update` does.
|
|
1092
|
+
*
|
|
1093
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
1094
|
+
* @param off Where the window starts in `data`.
|
|
1095
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
1096
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
1097
|
+
* absorbed, for a window outside `data` or a finished computation.
|
|
1098
|
+
*/
|
|
1099
|
+
update(data: u8[], off: i32, len: i32): boolean {
|
|
1100
|
+
return this.inner.update(data, off, len)
|
|
793
1101
|
}
|
|
794
1102
|
|
|
795
1103
|
/**
|
|
796
|
-
*
|
|
797
|
-
*
|
|
1104
|
+
* `update` for a caller that handles a bad window, as `Sha384.tryUpdate`
|
|
1105
|
+
* does.
|
|
798
1106
|
*
|
|
799
1107
|
* @param data The buffer holding the window; it is read, not kept.
|
|
800
1108
|
* @param off Where the window starts in `data`.
|
|
801
1109
|
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
1110
|
+
* @returns `true` once the window is absorbed; `false`, with nothing
|
|
1111
|
+
* absorbed, for a window outside `data` or a computation already
|
|
1112
|
+
* finished.
|
|
802
1113
|
*/
|
|
803
|
-
|
|
804
|
-
this.inner.
|
|
1114
|
+
tryUpdate(data: u8[], off: i32, len: i32): boolean {
|
|
1115
|
+
return this.inner.tryUpdate(data, off, len)
|
|
805
1116
|
}
|
|
806
1117
|
|
|
807
1118
|
/**
|
|
808
|
-
* Writes the 48-byte tag at `out[at]` and ends the computation
|
|
809
|
-
* outside `out
|
|
1119
|
+
* Writes the 48-byte tag at `out[at]` and ends the computation. It writes
|
|
1120
|
+
* nothing and answers `false` for a window outside `out`, or until a
|
|
1121
|
+
* `begin` keys the
|
|
1122
|
+
* hashers again after a tag, a `wipe` or a refused key window.
|
|
810
1123
|
*
|
|
811
1124
|
* @param out The caller's array the tag is written into.
|
|
812
1125
|
* @param at Where the 48 tag bytes start in `out`.
|
|
1126
|
+
* @returns `true` once the tag is written; `false`, with `out` untouched,
|
|
1127
|
+
* when it is refused.
|
|
813
1128
|
*/
|
|
814
|
-
finishInto(out: u8[], at: i32):
|
|
815
|
-
if (!hmacWindowFits(toI32(out.length), at, SHA384_SIZE)) {
|
|
816
|
-
|
|
1129
|
+
finishInto(out: u8[], at: i32): boolean {
|
|
1130
|
+
if (this.outer.engine.finished || !hmacWindowFits(toI32(out.length), at, SHA384_SIZE)) {
|
|
1131
|
+
return false
|
|
817
1132
|
}
|
|
818
1133
|
const innerDigest: u8[] = this.inner.digest()
|
|
819
|
-
this.outer.
|
|
1134
|
+
const fed: boolean = this.outer.tryUpdate(innerDigest, HMAC_FROM, SHA384_SIZE)
|
|
820
1135
|
secureZero(innerDigest)
|
|
1136
|
+
if (!fed) {
|
|
1137
|
+
hmacWipeSha384(this.inner)
|
|
1138
|
+
hmacWipeSha384(this.outer)
|
|
1139
|
+
this.outer.engine.finished = true
|
|
1140
|
+
return false
|
|
1141
|
+
}
|
|
821
1142
|
const tag: u8[] = this.outer.digest()
|
|
822
1143
|
let p: i32 = at
|
|
823
1144
|
for (
|
|
@@ -829,6 +1150,19 @@ export class HmacSha384Scratch {
|
|
|
829
1150
|
p += 1
|
|
830
1151
|
}
|
|
831
1152
|
secureZero(tag)
|
|
1153
|
+
return true
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* The same answer as `finishInto`, kept for callers written against it.
|
|
1158
|
+
*
|
|
1159
|
+
* @param out The caller's array the tag is written into.
|
|
1160
|
+
* @param at Where the tag bytes start in `out`.
|
|
1161
|
+
* @returns `true` once the tag is written; `false`, with `out` untouched,
|
|
1162
|
+
* when it is refused.
|
|
1163
|
+
*/
|
|
1164
|
+
tryFinishInto(out: u8[], at: i32): boolean {
|
|
1165
|
+
return this.finishInto(out, at)
|
|
832
1166
|
}
|
|
833
1167
|
|
|
834
1168
|
/** Zeroes both keyed hashers, for a caller done with the key; `begin` starts again. */
|
|
@@ -1002,12 +1336,18 @@ export const pbkdf2HmacSha256 = (
|
|
|
1002
1336
|
// `p256SignSha256Into` has (`std/crypto/p256.ts`): a plain copy of a secret
|
|
1003
1337
|
// step's answer, used in caller-owned scratch and wiped before return.
|
|
1004
1338
|
|
|
1005
|
-
/**
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1339
|
+
/**
|
|
1340
|
+
* `HmacKey`'s hash. An enum, not an `i32`, so a hash `HmacKey` cannot key is
|
|
1341
|
+
* a compile error rather than a value it must refuse.
|
|
1342
|
+
*/
|
|
1343
|
+
export enum HmacHash {
|
|
1344
|
+
/** HMAC-SHA-256: 32-byte tags. */
|
|
1345
|
+
Sha256 = 1,
|
|
1346
|
+
/** HMAC-SHA-384: 48-byte tags. */
|
|
1347
|
+
Sha384 = 2,
|
|
1348
|
+
/** HMAC-SHA-512: 64-byte tags. */
|
|
1349
|
+
Sha512 = 3,
|
|
1350
|
+
}
|
|
1011
1351
|
|
|
1012
1352
|
/** Words of one chaining value: eight, for SHA-256's `u32` and SHA-512's `u64` alike. */
|
|
1013
1353
|
const HMAC_WORDS: i32 = 8
|
|
@@ -1015,18 +1355,15 @@ const HMAC_WORDS: i32 = 8
|
|
|
1015
1355
|
/** Both midstates, inner then outer: twice `HMAC_WORDS`. */
|
|
1016
1356
|
const HMAC_MIDSTATE_WORDS: i32 = 16
|
|
1017
1357
|
|
|
1018
|
-
/** The tag length of `hash` in bytes
|
|
1019
|
-
const hmacTagSize = (hash:
|
|
1020
|
-
if (hash ===
|
|
1358
|
+
/** The tag length of `hash` in bytes. */
|
|
1359
|
+
const hmacTagSize = (hash: HmacHash): i32 => {
|
|
1360
|
+
if (hash === HmacHash.Sha256) {
|
|
1021
1361
|
return SHA256_SIZE
|
|
1022
1362
|
}
|
|
1023
|
-
if (hash ===
|
|
1363
|
+
if (hash === HmacHash.Sha384) {
|
|
1024
1364
|
return SHA384_SIZE
|
|
1025
1365
|
}
|
|
1026
|
-
|
|
1027
|
-
return SHA512_SIZE
|
|
1028
|
-
}
|
|
1029
|
-
return 0
|
|
1366
|
+
return SHA512_SIZE
|
|
1030
1367
|
}
|
|
1031
1368
|
|
|
1032
1369
|
/**
|
|
@@ -1035,9 +1372,9 @@ const hmacTagSize = (hash: i32): i32 => {
|
|
|
1035
1372
|
* outer, SHA-256's `u32` words widened to `u64`. The two keying hashers are
|
|
1036
1373
|
* wiped before it answers.
|
|
1037
1374
|
*/
|
|
1038
|
-
const hmacMidstates = (key: u8[], hash:
|
|
1375
|
+
const hmacMidstates = (key: u8[], hash: HmacHash): u64[] => {
|
|
1039
1376
|
const out: u64[] = new Array<u64>(HMAC_MIDSTATE_WORDS)
|
|
1040
|
-
if (hash ===
|
|
1377
|
+
if (hash === HmacHash.Sha256) {
|
|
1041
1378
|
const inner = new Sha256()
|
|
1042
1379
|
const outer = new Sha256()
|
|
1043
1380
|
hmacKeySha256(inner, outer, key)
|
|
@@ -1056,7 +1393,7 @@ const hmacMidstates = (key: u8[], hash: i32): u64[] => {
|
|
|
1056
1393
|
}
|
|
1057
1394
|
hmacWipeSha256(inner)
|
|
1058
1395
|
hmacWipeSha256(outer)
|
|
1059
|
-
} else if (hash ===
|
|
1396
|
+
} else if (hash === HmacHash.Sha384) {
|
|
1060
1397
|
const inner = new Sha384()
|
|
1061
1398
|
const outer = new Sha384()
|
|
1062
1399
|
hmacKeySha384(inner, outer, key)
|
|
@@ -1095,7 +1432,7 @@ const hmacMidstates512 = (out: u64[], inner: u64[], outer: u64[]): void => {
|
|
|
1095
1432
|
* makes or checks starts from them, so a tag costs the message's blocks and
|
|
1096
1433
|
* two finishing blocks, and never the key's two again.
|
|
1097
1434
|
*
|
|
1098
|
-
* const key = new HmacKey(
|
|
1435
|
+
* const key = new HmacKey(HmacHash.Sha256, secretKey); // once
|
|
1099
1436
|
* hmacKeyedInto(key, data, 0, data.length, tag, 0); // per message
|
|
1100
1437
|
* const ok: boolean = hmacKeyedVerify(key, data, 0, data.length, tag, 0);
|
|
1101
1438
|
* key.wipe(); // when done
|
|
@@ -1103,7 +1440,6 @@ const hmacMidstates512 = (out: u64[], inner: u64[], outer: u64[]): void => {
|
|
|
1103
1440
|
* The tags are `hmacSha256`'s, `hmacSha384`'s and `hmacSha512`'s for the same
|
|
1104
1441
|
* key. The key itself is read only inside `exposeWith`, and the hashers that
|
|
1105
1442
|
* keyed it are wiped; the caller still owns and wipes the `Secret` it passed.
|
|
1106
|
-
* An unknown `hash` panics.
|
|
1107
1443
|
*
|
|
1108
1444
|
* The key also owns two hashers of its family, made once, that each tag is
|
|
1109
1445
|
* computed in and that are wiped before the tag call returns. One key is
|
|
@@ -1135,11 +1471,11 @@ export class HmacKey {
|
|
|
1135
1471
|
/** @internal Read by `hmacKeyedInto`, `hmacKeyedVerify` and their helpers. */
|
|
1136
1472
|
readonly outer512: Sha512
|
|
1137
1473
|
/**
|
|
1138
|
-
*
|
|
1474
|
+
* Which HMAC the key computes.
|
|
1139
1475
|
*
|
|
1140
1476
|
* @internal Read by `hmacKeyedInto`, `hmacKeyedVerify` and their helpers.
|
|
1141
1477
|
*/
|
|
1142
|
-
readonly hash:
|
|
1478
|
+
readonly hash: HmacHash
|
|
1143
1479
|
/**
|
|
1144
1480
|
* Whether the key still MACs: `wipe()` clears it. A wiped key's chaining
|
|
1145
1481
|
* values are zeros anyone can resume from, so a tag under them proves
|
|
@@ -1150,17 +1486,13 @@ export class HmacKey {
|
|
|
1150
1486
|
live: boolean
|
|
1151
1487
|
|
|
1152
1488
|
/**
|
|
1153
|
-
* Absorbs `key`'s two pad blocks for `hash` and keeps the chaining values.
|
|
1154
|
-
* unknown `hash` panics.
|
|
1489
|
+
* Absorbs `key`'s two pad blocks for `hash` and keeps the chaining values.
|
|
1155
1490
|
*
|
|
1156
|
-
* @param hash Which HMAC: `
|
|
1491
|
+
* @param hash Which HMAC: `HmacHash.Sha256`, `Sha384` or `Sha512`.
|
|
1157
1492
|
* @param key The key, read only inside `exposeWith`; the caller still owns
|
|
1158
1493
|
* and wipes it.
|
|
1159
1494
|
*/
|
|
1160
|
-
constructor(hash:
|
|
1161
|
-
if (hmacTagSize(hash) === 0) {
|
|
1162
|
-
panic("HmacKey: an unknown hash")
|
|
1163
|
-
}
|
|
1495
|
+
constructor(hash: HmacHash, key: Secret<u8[]>) {
|
|
1164
1496
|
this.hash = hash
|
|
1165
1497
|
this.pads = secret(exposeWith(key, hash, hmacMidstates))
|
|
1166
1498
|
this.inner256 = new Sha256()
|
|
@@ -1177,7 +1509,7 @@ export class HmacKey {
|
|
|
1177
1509
|
/**
|
|
1178
1510
|
* The tag length in bytes.
|
|
1179
1511
|
*
|
|
1180
|
-
* @returns 32 for `
|
|
1512
|
+
* @returns 32 for `HmacHash.Sha256`, 48 for `Sha384`, 64 for `Sha512`.
|
|
1181
1513
|
*/
|
|
1182
1514
|
size(): i32 {
|
|
1183
1515
|
return hmacTagSize(this.hash)
|
|
@@ -1230,14 +1562,14 @@ const hmacResume512 = (h: Sha512, mid: u64[], at: i32): void => {
|
|
|
1230
1562
|
}
|
|
1231
1563
|
|
|
1232
1564
|
/**
|
|
1233
|
-
* The tag of `data[off .. off + len)
|
|
1234
|
-
*
|
|
1235
|
-
*
|
|
1236
|
-
* and both hashers wiped
|
|
1565
|
+
* The tag of `data[off .. off + len)`, a window both callers check first (so
|
|
1566
|
+
* no `update` here answers `false`), under `key`, as a fresh array the caller
|
|
1567
|
+
* wipes: the midstates copied out of their `Secret`, both hashers resumed, fed
|
|
1568
|
+
* and finished, and the copy, the inner digest and both hashers wiped after.
|
|
1237
1569
|
*/
|
|
1238
1570
|
const hmacKeyedTag = (key: HmacKey, data: u8[], off: i32, len: i32): u8[] => {
|
|
1239
1571
|
const mid: u64[] = expose(key.pads, hmacCopyWords)
|
|
1240
|
-
if (key.hash ===
|
|
1572
|
+
if (key.hash === HmacHash.Sha256) {
|
|
1241
1573
|
const inner: Sha256 = key.inner256
|
|
1242
1574
|
const outer: Sha256 = key.outer256
|
|
1243
1575
|
hmacResume256(inner, mid, 0)
|
|
@@ -1252,7 +1584,7 @@ const hmacKeyedTag = (key: HmacKey, data: u8[], off: i32, len: i32): u8[] => {
|
|
|
1252
1584
|
hmacWipeSha256(outer)
|
|
1253
1585
|
return tag
|
|
1254
1586
|
}
|
|
1255
|
-
const size: i32 = key.hash ===
|
|
1587
|
+
const size: i32 = key.hash === HmacHash.Sha384 ? SHA384_SIZE : SHA512_SIZE
|
|
1256
1588
|
const inner: Sha512 = key.inner512
|
|
1257
1589
|
const outer: Sha512 = key.outer512
|
|
1258
1590
|
hmacResume512(inner, mid, 0)
|
|
@@ -1270,27 +1602,28 @@ const hmacKeyedTag = (key: HmacKey, data: u8[], off: i32, len: i32): u8[] => {
|
|
|
1270
1602
|
|
|
1271
1603
|
/**
|
|
1272
1604
|
* The tag of `data[off .. off + len)` under `key`, written at
|
|
1273
|
-
* `out[at .. at + key.size())`. A window outside its array
|
|
1274
|
-
* anything is read. It runs in an
|
|
1275
|
-
* so a tag leaves the arena where
|
|
1605
|
+
* `out[at .. at + key.size())`. A wiped key, or a window outside its array,
|
|
1606
|
+
* writes nothing and answers `false` before anything is read. It runs in an
|
|
1607
|
+
* arena block of its own and stores nothing, so a tag leaves the arena where
|
|
1608
|
+
* it was.
|
|
1276
1609
|
*
|
|
1277
|
-
* @param key The absorbed key; a wiped one
|
|
1610
|
+
* @param key The absorbed key; a wiped one writes no tag.
|
|
1278
1611
|
* @param data The buffer holding the message.
|
|
1279
1612
|
* @param off Where the message starts in `data`.
|
|
1280
1613
|
* @param len The message's length in bytes.
|
|
1281
1614
|
* @param out The caller's array the tag is written into.
|
|
1282
1615
|
* @param at Where the tag starts in `out`.
|
|
1616
|
+
* @returns `true` once the tag is written; `false`, with `out` untouched, for
|
|
1617
|
+
* a wiped key or a message or tag window outside its array.
|
|
1283
1618
|
*/
|
|
1284
|
-
export const hmacKeyedInto = (key: HmacKey, data: u8[], off: i32, len: i32, out: u8[], at: i32):
|
|
1619
|
+
export const hmacKeyedInto = (key: HmacKey, data: u8[], off: i32, len: i32, out: u8[], at: i32): boolean => {
|
|
1285
1620
|
const size: i32 = key.size()
|
|
1286
|
-
if (
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
if (!hmacWindowFits(toI32(out.length), at, size)) {
|
|
1293
|
-
panic("hmacKeyedInto: the tag window is outside its array")
|
|
1621
|
+
if (
|
|
1622
|
+
!key.live ||
|
|
1623
|
+
!hmacWindowFits(toI32(data.length), off, len) ||
|
|
1624
|
+
!hmacWindowFits(toI32(out.length), at, size)
|
|
1625
|
+
) {
|
|
1626
|
+
return false
|
|
1294
1627
|
}
|
|
1295
1628
|
using _scope = arena()
|
|
1296
1629
|
const tag: u8[] = hmacKeyedTag(key, data, off, len)
|
|
@@ -1300,14 +1633,15 @@ export const hmacKeyedInto = (key: HmacKey, data: u8[], off: i32, len: i32, out:
|
|
|
1300
1633
|
p += 1
|
|
1301
1634
|
}
|
|
1302
1635
|
wipe(tag)
|
|
1636
|
+
return true
|
|
1303
1637
|
}
|
|
1304
1638
|
|
|
1305
1639
|
/**
|
|
1306
1640
|
* Whether `tag[tagAt .. tagAt + key.size())` is the tag of
|
|
1307
1641
|
* `data[off .. off + len)` under `key`, compared in constant time and the
|
|
1308
|
-
* computed tag wiped, since it is the one tag a forger needs. A
|
|
1309
|
-
* that does not fit answers `false
|
|
1310
|
-
*
|
|
1642
|
+
* computed tag wiped, since it is the one tag a forger needs. A message or
|
|
1643
|
+
* tag window that does not fit answers `false`. It runs in an arena block of
|
|
1644
|
+
* its own and stores nothing.
|
|
1311
1645
|
*
|
|
1312
1646
|
* @param key The absorbed key; a wiped one answers `false`.
|
|
1313
1647
|
* @param data The buffer holding the message.
|
|
@@ -1326,10 +1660,11 @@ export const hmacKeyedVerify = (
|
|
|
1326
1660
|
tagAt: i32
|
|
1327
1661
|
): boolean => {
|
|
1328
1662
|
const size: i32 = key.size()
|
|
1329
|
-
if (
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1663
|
+
if (
|
|
1664
|
+
!key.live ||
|
|
1665
|
+
!hmacWindowFits(toI32(data.length), off, len) ||
|
|
1666
|
+
!hmacWindowFits(toI32(tag.length), tagAt, size)
|
|
1667
|
+
) {
|
|
1333
1668
|
return false
|
|
1334
1669
|
}
|
|
1335
1670
|
using _scope = arena()
|