Upload via GUI (7543 Dateien)
This commit is contained in:
+401
@@ -0,0 +1,401 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.argon2idAsync = exports.argon2iAsync = exports.argon2dAsync = exports.argon2id = exports.argon2i = exports.argon2d = void 0;
|
||||||
|
/**
|
||||||
|
* Argon2 KDF from RFC 9106. Can be used to create a key from password and salt.
|
||||||
|
* We suggest to use Scrypt. JS Argon is 2-10x slower than native code because of 64-bitness:
|
||||||
|
* * argon uses uint64, but JS doesn't have fast uint64array
|
||||||
|
* * uint64 multiplication is 1/3 of time
|
||||||
|
* * `P` function would be very nice with u64, because most of value will be in registers,
|
||||||
|
* hovewer with u32 it will require 32 registers, which is too much.
|
||||||
|
* * JS arrays do slow bound checks, so reading from `A2_BUF` slows it down
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
const _u64_ts_1 = require("./_u64.js");
|
||||||
|
const blake2_ts_1 = require("./blake2.js");
|
||||||
|
const utils_ts_1 = require("./utils.js");
|
||||||
|
const AT = { Argond2d: 0, Argon2i: 1, Argon2id: 2 };
|
||||||
|
const ARGON2_SYNC_POINTS = 4;
|
||||||
|
const abytesOrZero = (buf) => {
|
||||||
|
if (buf === undefined)
|
||||||
|
return Uint8Array.of();
|
||||||
|
return (0, utils_ts_1.kdfInputToBytes)(buf);
|
||||||
|
};
|
||||||
|
// u32 * u32 = u64
|
||||||
|
function mul(a, b) {
|
||||||
|
const aL = a & 0xffff;
|
||||||
|
const aH = a >>> 16;
|
||||||
|
const bL = b & 0xffff;
|
||||||
|
const bH = b >>> 16;
|
||||||
|
const ll = Math.imul(aL, bL);
|
||||||
|
const hl = Math.imul(aH, bL);
|
||||||
|
const lh = Math.imul(aL, bH);
|
||||||
|
const hh = Math.imul(aH, bH);
|
||||||
|
const carry = (ll >>> 16) + (hl & 0xffff) + lh;
|
||||||
|
const high = (hh + (hl >>> 16) + (carry >>> 16)) | 0;
|
||||||
|
const low = (carry << 16) | (ll & 0xffff);
|
||||||
|
return { h: high, l: low };
|
||||||
|
}
|
||||||
|
function mul2(a, b) {
|
||||||
|
// 2 * a * b (via shifts)
|
||||||
|
const { h, l } = mul(a, b);
|
||||||
|
return { h: ((h << 1) | (l >>> 31)) & 4294967295, l: (l << 1) & 4294967295 };
|
||||||
|
}
|
||||||
|
// BlaMka permutation for Argon2
|
||||||
|
// A + B + (2 * u32(A) * u32(B))
|
||||||
|
function blamka(Ah, Al, Bh, Bl) {
|
||||||
|
const { h: Ch, l: Cl } = mul2(Al, Bl);
|
||||||
|
// A + B + (2 * A * B)
|
||||||
|
const Rll = (0, _u64_ts_1.add3L)(Al, Bl, Cl);
|
||||||
|
return { h: (0, _u64_ts_1.add3H)(Rll, Ah, Bh, Ch), l: Rll | 0 };
|
||||||
|
}
|
||||||
|
// Temporary block buffer
|
||||||
|
const A2_BUF = new Uint32Array(256); // 1024 bytes (matrix 16x16)
|
||||||
|
function G(a, b, c, d) {
|
||||||
|
let Al = A2_BUF[2 * a], Ah = A2_BUF[2 * a + 1]; // prettier-ignore
|
||||||
|
let Bl = A2_BUF[2 * b], Bh = A2_BUF[2 * b + 1]; // prettier-ignore
|
||||||
|
let Cl = A2_BUF[2 * c], Ch = A2_BUF[2 * c + 1]; // prettier-ignore
|
||||||
|
let Dl = A2_BUF[2 * d], Dh = A2_BUF[2 * d + 1]; // prettier-ignore
|
||||||
|
({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: (0, _u64_ts_1.rotr32H)(Dh, Dl), Dl: (0, _u64_ts_1.rotr32L)(Dh, Dl) });
|
||||||
|
({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: (0, _u64_ts_1.rotrSH)(Bh, Bl, 24), Bl: (0, _u64_ts_1.rotrSL)(Bh, Bl, 24) });
|
||||||
|
({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: (0, _u64_ts_1.rotrSH)(Dh, Dl, 16), Dl: (0, _u64_ts_1.rotrSL)(Dh, Dl, 16) });
|
||||||
|
({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: (0, _u64_ts_1.rotrBH)(Bh, Bl, 63), Bl: (0, _u64_ts_1.rotrBL)(Bh, Bl, 63) });
|
||||||
|
(A2_BUF[2 * a] = Al), (A2_BUF[2 * a + 1] = Ah);
|
||||||
|
(A2_BUF[2 * b] = Bl), (A2_BUF[2 * b + 1] = Bh);
|
||||||
|
(A2_BUF[2 * c] = Cl), (A2_BUF[2 * c + 1] = Ch);
|
||||||
|
(A2_BUF[2 * d] = Dl), (A2_BUF[2 * d + 1] = Dh);
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
function P(v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11, v12, v13, v14, v15) {
|
||||||
|
G(v00, v04, v08, v12);
|
||||||
|
G(v01, v05, v09, v13);
|
||||||
|
G(v02, v06, v10, v14);
|
||||||
|
G(v03, v07, v11, v15);
|
||||||
|
G(v00, v05, v10, v15);
|
||||||
|
G(v01, v06, v11, v12);
|
||||||
|
G(v02, v07, v08, v13);
|
||||||
|
G(v03, v04, v09, v14);
|
||||||
|
}
|
||||||
|
function block(x, xPos, yPos, outPos, needXor) {
|
||||||
|
for (let i = 0; i < 256; i++)
|
||||||
|
A2_BUF[i] = x[xPos + i] ^ x[yPos + i];
|
||||||
|
// columns (8)
|
||||||
|
for (let i = 0; i < 128; i += 16) {
|
||||||
|
// prettier-ignore
|
||||||
|
P(i, i + 1, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7, i + 8, i + 9, i + 10, i + 11, i + 12, i + 13, i + 14, i + 15);
|
||||||
|
}
|
||||||
|
// rows (8)
|
||||||
|
for (let i = 0; i < 16; i += 2) {
|
||||||
|
// prettier-ignore
|
||||||
|
P(i, i + 1, i + 16, i + 17, i + 32, i + 33, i + 48, i + 49, i + 64, i + 65, i + 80, i + 81, i + 96, i + 97, i + 112, i + 113);
|
||||||
|
}
|
||||||
|
if (needXor)
|
||||||
|
for (let i = 0; i < 256; i++)
|
||||||
|
x[outPos + i] ^= A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
|
||||||
|
else
|
||||||
|
for (let i = 0; i < 256; i++)
|
||||||
|
x[outPos + i] = A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
|
||||||
|
(0, utils_ts_1.clean)(A2_BUF);
|
||||||
|
}
|
||||||
|
// Variable-Length Hash Function H'
|
||||||
|
function Hp(A, dkLen) {
|
||||||
|
const A8 = (0, utils_ts_1.u8)(A);
|
||||||
|
const T = new Uint32Array(1);
|
||||||
|
const T8 = (0, utils_ts_1.u8)(T);
|
||||||
|
T[0] = dkLen;
|
||||||
|
// Fast path
|
||||||
|
if (dkLen <= 64)
|
||||||
|
return blake2_ts_1.blake2b.create({ dkLen }).update(T8).update(A8).digest();
|
||||||
|
const out = new Uint8Array(dkLen);
|
||||||
|
let V = blake2_ts_1.blake2b.create({}).update(T8).update(A8).digest();
|
||||||
|
let pos = 0;
|
||||||
|
// First block
|
||||||
|
out.set(V.subarray(0, 32));
|
||||||
|
pos += 32;
|
||||||
|
// Rest blocks
|
||||||
|
for (; dkLen - pos > 64; pos += 32) {
|
||||||
|
const Vh = blake2_ts_1.blake2b.create({}).update(V);
|
||||||
|
Vh.digestInto(V);
|
||||||
|
Vh.destroy();
|
||||||
|
out.set(V.subarray(0, 32), pos);
|
||||||
|
}
|
||||||
|
// Last block
|
||||||
|
out.set((0, blake2_ts_1.blake2b)(V, { dkLen: dkLen - pos }), pos);
|
||||||
|
(0, utils_ts_1.clean)(V, T);
|
||||||
|
return (0, utils_ts_1.u32)(out);
|
||||||
|
}
|
||||||
|
// Used only inside process block!
|
||||||
|
function indexAlpha(r, s, laneLen, segmentLen, index, randL, sameLane = false) {
|
||||||
|
// This is ugly, but close enough to reference implementation.
|
||||||
|
let area;
|
||||||
|
if (r === 0) {
|
||||||
|
if (s === 0)
|
||||||
|
area = index - 1;
|
||||||
|
else if (sameLane)
|
||||||
|
area = s * segmentLen + index - 1;
|
||||||
|
else
|
||||||
|
area = s * segmentLen + (index == 0 ? -1 : 0);
|
||||||
|
}
|
||||||
|
else if (sameLane)
|
||||||
|
area = laneLen - segmentLen + index - 1;
|
||||||
|
else
|
||||||
|
area = laneLen - segmentLen + (index == 0 ? -1 : 0);
|
||||||
|
const startPos = r !== 0 && s !== ARGON2_SYNC_POINTS - 1 ? (s + 1) * segmentLen : 0;
|
||||||
|
const rel = area - 1 - mul(area, mul(randL, randL).h).h;
|
||||||
|
return (startPos + rel) % laneLen;
|
||||||
|
}
|
||||||
|
const maxUint32 = Math.pow(2, 32);
|
||||||
|
function isU32(num) {
|
||||||
|
return Number.isSafeInteger(num) && num >= 0 && num < maxUint32;
|
||||||
|
}
|
||||||
|
function argon2Opts(opts) {
|
||||||
|
const merged = {
|
||||||
|
version: 0x13,
|
||||||
|
dkLen: 32,
|
||||||
|
maxmem: maxUint32 - 1,
|
||||||
|
asyncTick: 10,
|
||||||
|
};
|
||||||
|
for (let [k, v] of Object.entries(opts))
|
||||||
|
if (v != null)
|
||||||
|
merged[k] = v;
|
||||||
|
const { dkLen, p, m, t, version, onProgress } = merged;
|
||||||
|
if (!isU32(dkLen) || dkLen < 4)
|
||||||
|
throw new Error('dkLen should be at least 4 bytes');
|
||||||
|
if (!isU32(p) || p < 1 || p >= Math.pow(2, 24))
|
||||||
|
throw new Error('p should be 1 <= p < 2^24');
|
||||||
|
if (!isU32(m))
|
||||||
|
throw new Error('m should be 0 <= m < 2^32');
|
||||||
|
if (!isU32(t) || t < 1)
|
||||||
|
throw new Error('t (iterations) should be 1 <= t < 2^32');
|
||||||
|
if (onProgress !== undefined && typeof onProgress !== 'function')
|
||||||
|
throw new Error('progressCb should be function');
|
||||||
|
/*
|
||||||
|
Memory size m MUST be an integer number of kibibytes from 8*p to 2^(32)-1. The actual number of blocks is m', which is m rounded down to the nearest multiple of 4*p.
|
||||||
|
*/
|
||||||
|
if (!isU32(m) || m < 8 * p)
|
||||||
|
throw new Error('memory should be at least 8*p bytes');
|
||||||
|
if (version !== 0x10 && version !== 0x13)
|
||||||
|
throw new Error('unknown version=' + version);
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
function argon2Init(password, salt, type, opts) {
|
||||||
|
password = (0, utils_ts_1.kdfInputToBytes)(password);
|
||||||
|
salt = (0, utils_ts_1.kdfInputToBytes)(salt);
|
||||||
|
(0, utils_ts_1.abytes)(password);
|
||||||
|
(0, utils_ts_1.abytes)(salt);
|
||||||
|
if (!isU32(password.length))
|
||||||
|
throw new Error('password should be less than 4 GB');
|
||||||
|
if (!isU32(salt.length) || salt.length < 8)
|
||||||
|
throw new Error('salt should be at least 8 bytes and less than 4 GB');
|
||||||
|
if (!Object.values(AT).includes(type))
|
||||||
|
throw new Error('invalid type');
|
||||||
|
let { p, dkLen, m, t, version, key, personalization, maxmem, onProgress, asyncTick } = argon2Opts(opts);
|
||||||
|
// Validation
|
||||||
|
key = abytesOrZero(key);
|
||||||
|
personalization = abytesOrZero(personalization);
|
||||||
|
// H_0 = H^(64)(LE32(p) || LE32(T) || LE32(m) || LE32(t) ||
|
||||||
|
// LE32(v) || LE32(y) || LE32(length(P)) || P ||
|
||||||
|
// LE32(length(S)) || S || LE32(length(K)) || K ||
|
||||||
|
// LE32(length(X)) || X)
|
||||||
|
const h = blake2_ts_1.blake2b.create({});
|
||||||
|
const BUF = new Uint32Array(1);
|
||||||
|
const BUF8 = (0, utils_ts_1.u8)(BUF);
|
||||||
|
for (let item of [p, dkLen, m, t, version, type]) {
|
||||||
|
BUF[0] = item;
|
||||||
|
h.update(BUF8);
|
||||||
|
}
|
||||||
|
for (let i of [password, salt, key, personalization]) {
|
||||||
|
BUF[0] = i.length; // BUF is u32 array, this is valid
|
||||||
|
h.update(BUF8).update(i);
|
||||||
|
}
|
||||||
|
const H0 = new Uint32Array(18);
|
||||||
|
const H0_8 = (0, utils_ts_1.u8)(H0);
|
||||||
|
h.digestInto(H0_8);
|
||||||
|
// 256 u32 = 1024 (BLOCK_SIZE), fills A2_BUF on processing
|
||||||
|
// Params
|
||||||
|
const lanes = p;
|
||||||
|
// m' = 4 * p * floor (m / 4p)
|
||||||
|
const mP = 4 * p * Math.floor(m / (ARGON2_SYNC_POINTS * p));
|
||||||
|
//q = m' / p columns
|
||||||
|
const laneLen = Math.floor(mP / p);
|
||||||
|
const segmentLen = Math.floor(laneLen / ARGON2_SYNC_POINTS);
|
||||||
|
const memUsed = mP * 256;
|
||||||
|
if (!isU32(maxmem) || memUsed > maxmem)
|
||||||
|
throw new Error('mem should be less than 2**32, got: maxmem=' + maxmem + ', memused=' + memUsed);
|
||||||
|
const B = new Uint32Array(memUsed);
|
||||||
|
// Fill first blocks
|
||||||
|
for (let l = 0; l < p; l++) {
|
||||||
|
const i = 256 * laneLen * l;
|
||||||
|
// B[i][0] = H'^(1024)(H_0 || LE32(0) || LE32(i))
|
||||||
|
H0[17] = l;
|
||||||
|
H0[16] = 0;
|
||||||
|
B.set(Hp(H0, 1024), i);
|
||||||
|
// B[i][1] = H'^(1024)(H_0 || LE32(1) || LE32(i))
|
||||||
|
H0[16] = 1;
|
||||||
|
B.set(Hp(H0, 1024), i + 256);
|
||||||
|
}
|
||||||
|
let perBlock = () => { };
|
||||||
|
if (onProgress) {
|
||||||
|
const totalBlock = t * ARGON2_SYNC_POINTS * p * segmentLen;
|
||||||
|
// Invoke callback if progress changes from 10.01 to 10.02
|
||||||
|
// Allows to draw smooth progress bar on up to 8K screen
|
||||||
|
const callbackPer = Math.max(Math.floor(totalBlock / 10000), 1);
|
||||||
|
let blockCnt = 0;
|
||||||
|
perBlock = () => {
|
||||||
|
blockCnt++;
|
||||||
|
if (onProgress && (!(blockCnt % callbackPer) || blockCnt === totalBlock))
|
||||||
|
onProgress(blockCnt / totalBlock);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
(0, utils_ts_1.clean)(BUF, H0);
|
||||||
|
return { type, mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick };
|
||||||
|
}
|
||||||
|
function argon2Output(B, p, laneLen, dkLen) {
|
||||||
|
const B_final = new Uint32Array(256);
|
||||||
|
for (let l = 0; l < p; l++)
|
||||||
|
for (let j = 0; j < 256; j++)
|
||||||
|
B_final[j] ^= B[256 * (laneLen * l + laneLen - 1) + j];
|
||||||
|
const res = (0, utils_ts_1.u8)(Hp(B_final, dkLen));
|
||||||
|
(0, utils_ts_1.clean)(B_final);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
function processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor) {
|
||||||
|
if (offset % laneLen)
|
||||||
|
prev = offset - 1;
|
||||||
|
let randL, randH;
|
||||||
|
if (dataIndependent) {
|
||||||
|
let i128 = index % 128;
|
||||||
|
if (i128 === 0) {
|
||||||
|
address[256 + 12]++;
|
||||||
|
block(address, 256, 2 * 256, 0, false);
|
||||||
|
block(address, 0, 2 * 256, 0, false);
|
||||||
|
}
|
||||||
|
randL = address[2 * i128];
|
||||||
|
randH = address[2 * i128 + 1];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const T = 256 * prev;
|
||||||
|
randL = B[T];
|
||||||
|
randH = B[T + 1];
|
||||||
|
}
|
||||||
|
// address block
|
||||||
|
const refLane = r === 0 && s === 0 ? l : randH % lanes;
|
||||||
|
const refPos = indexAlpha(r, s, laneLen, segmentLen, index, randL, refLane == l);
|
||||||
|
const refBlock = laneLen * refLane + refPos;
|
||||||
|
// B[i][j] = G(B[i][j-1], B[l][z])
|
||||||
|
block(B, 256 * prev, 256 * refBlock, offset * 256, needXor);
|
||||||
|
}
|
||||||
|
function argon2(type, password, salt, opts) {
|
||||||
|
const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock } = argon2Init(password, salt, type, opts);
|
||||||
|
// Pre-loop setup
|
||||||
|
// [address, input, zero_block] format so we can pass single U32 to block function
|
||||||
|
const address = new Uint32Array(3 * 256);
|
||||||
|
address[256 + 6] = mP;
|
||||||
|
address[256 + 8] = t;
|
||||||
|
address[256 + 10] = type;
|
||||||
|
for (let r = 0; r < t; r++) {
|
||||||
|
const needXor = r !== 0 && version === 0x13;
|
||||||
|
address[256 + 0] = r;
|
||||||
|
for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
|
||||||
|
address[256 + 4] = s;
|
||||||
|
const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
|
||||||
|
for (let l = 0; l < p; l++) {
|
||||||
|
address[256 + 2] = l;
|
||||||
|
address[256 + 12] = 0;
|
||||||
|
let startPos = 0;
|
||||||
|
if (r === 0 && s === 0) {
|
||||||
|
startPos = 2;
|
||||||
|
if (dataIndependent) {
|
||||||
|
address[256 + 12]++;
|
||||||
|
block(address, 256, 2 * 256, 0, false);
|
||||||
|
block(address, 0, 2 * 256, 0, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// current block postion
|
||||||
|
let offset = l * laneLen + s * segmentLen + startPos;
|
||||||
|
// previous block position
|
||||||
|
let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
|
||||||
|
for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
|
||||||
|
perBlock();
|
||||||
|
processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(0, utils_ts_1.clean)(address);
|
||||||
|
return argon2Output(B, p, laneLen, dkLen);
|
||||||
|
}
|
||||||
|
/** argon2d GPU-resistant version. */
|
||||||
|
const argon2d = (password, salt, opts) => argon2(AT.Argond2d, password, salt, opts);
|
||||||
|
exports.argon2d = argon2d;
|
||||||
|
/** argon2i side-channel-resistant version. */
|
||||||
|
const argon2i = (password, salt, opts) => argon2(AT.Argon2i, password, salt, opts);
|
||||||
|
exports.argon2i = argon2i;
|
||||||
|
/** argon2id, combining i+d, the most popular version from RFC 9106 */
|
||||||
|
const argon2id = (password, salt, opts) => argon2(AT.Argon2id, password, salt, opts);
|
||||||
|
exports.argon2id = argon2id;
|
||||||
|
async function argon2Async(type, password, salt, opts) {
|
||||||
|
const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick } = argon2Init(password, salt, type, opts);
|
||||||
|
// Pre-loop setup
|
||||||
|
// [address, input, zero_block] format so we can pass single U32 to block function
|
||||||
|
const address = new Uint32Array(3 * 256);
|
||||||
|
address[256 + 6] = mP;
|
||||||
|
address[256 + 8] = t;
|
||||||
|
address[256 + 10] = type;
|
||||||
|
let ts = Date.now();
|
||||||
|
for (let r = 0; r < t; r++) {
|
||||||
|
const needXor = r !== 0 && version === 0x13;
|
||||||
|
address[256 + 0] = r;
|
||||||
|
for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
|
||||||
|
address[256 + 4] = s;
|
||||||
|
const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
|
||||||
|
for (let l = 0; l < p; l++) {
|
||||||
|
address[256 + 2] = l;
|
||||||
|
address[256 + 12] = 0;
|
||||||
|
let startPos = 0;
|
||||||
|
if (r === 0 && s === 0) {
|
||||||
|
startPos = 2;
|
||||||
|
if (dataIndependent) {
|
||||||
|
address[256 + 12]++;
|
||||||
|
block(address, 256, 2 * 256, 0, false);
|
||||||
|
block(address, 0, 2 * 256, 0, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// current block postion
|
||||||
|
let offset = l * laneLen + s * segmentLen + startPos;
|
||||||
|
// previous block position
|
||||||
|
let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
|
||||||
|
for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
|
||||||
|
perBlock();
|
||||||
|
processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor);
|
||||||
|
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
||||||
|
const diff = Date.now() - ts;
|
||||||
|
if (!(diff >= 0 && diff < asyncTick)) {
|
||||||
|
await (0, utils_ts_1.nextTick)();
|
||||||
|
ts += diff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(0, utils_ts_1.clean)(address);
|
||||||
|
return argon2Output(B, p, laneLen, dkLen);
|
||||||
|
}
|
||||||
|
/** argon2d async GPU-resistant version. */
|
||||||
|
const argon2dAsync = (password, salt, opts) => argon2Async(AT.Argond2d, password, salt, opts);
|
||||||
|
exports.argon2dAsync = argon2dAsync;
|
||||||
|
/** argon2i async side-channel-resistant version. */
|
||||||
|
const argon2iAsync = (password, salt, opts) => argon2Async(AT.Argon2i, password, salt, opts);
|
||||||
|
exports.argon2iAsync = argon2iAsync;
|
||||||
|
/** argon2id async, combining i+d, the most popular version from RFC 9106 */
|
||||||
|
const argon2idAsync = (password, salt, opts) => argon2Async(AT.Argon2id, password, salt, opts);
|
||||||
|
exports.argon2idAsync = argon2idAsync;
|
||||||
|
//# sourceMappingURL=argon2.js.map
|
||||||
+1
File diff suppressed because one or more lines are too long
+106
@@ -0,0 +1,106 @@
|
|||||||
|
import { Hash, type CHashO, type Input } from './utils.ts';
|
||||||
|
/** Blake1 options. Basically just "salt" */
|
||||||
|
export type BlakeOpts = {
|
||||||
|
salt?: Uint8Array;
|
||||||
|
};
|
||||||
|
declare abstract class BLAKE1<T extends BLAKE1<T>> extends Hash<T> {
|
||||||
|
protected finished: boolean;
|
||||||
|
protected length: number;
|
||||||
|
protected pos: number;
|
||||||
|
protected destroyed: boolean;
|
||||||
|
protected buffer: Uint8Array;
|
||||||
|
protected view: DataView;
|
||||||
|
protected salt: Uint32Array;
|
||||||
|
abstract compress(view: DataView, offset: number, withLength?: boolean): void;
|
||||||
|
protected abstract get(): number[];
|
||||||
|
protected abstract set(...args: number[]): void;
|
||||||
|
readonly blockLen: number;
|
||||||
|
readonly outputLen: number;
|
||||||
|
private lengthFlag;
|
||||||
|
private counterLen;
|
||||||
|
protected constants: Uint32Array;
|
||||||
|
constructor(blockLen: number, outputLen: number, lengthFlag: number, counterLen: number, saltLen: number, constants: Uint32Array, opts?: BlakeOpts);
|
||||||
|
update(data: Input): this;
|
||||||
|
destroy(): void;
|
||||||
|
_cloneInto(to?: T): T;
|
||||||
|
clone(): T;
|
||||||
|
digestInto(out: Uint8Array): void;
|
||||||
|
digest(): Uint8Array;
|
||||||
|
}
|
||||||
|
declare class Blake1_32 extends BLAKE1<Blake1_32> {
|
||||||
|
private v0;
|
||||||
|
private v1;
|
||||||
|
private v2;
|
||||||
|
private v3;
|
||||||
|
private v4;
|
||||||
|
private v5;
|
||||||
|
private v6;
|
||||||
|
private v7;
|
||||||
|
constructor(outputLen: number, IV: Uint32Array, lengthFlag: number, opts?: BlakeOpts);
|
||||||
|
protected get(): [number, number, number, number, number, number, number, number];
|
||||||
|
protected set(v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number): void;
|
||||||
|
destroy(): void;
|
||||||
|
compress(view: DataView, offset: number, withLength?: boolean): void;
|
||||||
|
}
|
||||||
|
declare class Blake1_64 extends BLAKE1<Blake1_64> {
|
||||||
|
private v0l;
|
||||||
|
private v0h;
|
||||||
|
private v1l;
|
||||||
|
private v1h;
|
||||||
|
private v2l;
|
||||||
|
private v2h;
|
||||||
|
private v3l;
|
||||||
|
private v3h;
|
||||||
|
private v4l;
|
||||||
|
private v4h;
|
||||||
|
private v5l;
|
||||||
|
private v5h;
|
||||||
|
private v6l;
|
||||||
|
private v6h;
|
||||||
|
private v7l;
|
||||||
|
private v7h;
|
||||||
|
constructor(outputLen: number, IV: Uint32Array, lengthFlag: number, opts?: BlakeOpts);
|
||||||
|
protected get(): [
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number
|
||||||
|
];
|
||||||
|
protected set(v0l: number, v0h: number, v1l: number, v1h: number, v2l: number, v2h: number, v3l: number, v3h: number, v4l: number, v4h: number, v5l: number, v5h: number, v6l: number, v6h: number, v7l: number, v7h: number): void;
|
||||||
|
destroy(): void;
|
||||||
|
compress(view: DataView, offset: number, withLength?: boolean): void;
|
||||||
|
}
|
||||||
|
export declare class BLAKE224 extends Blake1_32 {
|
||||||
|
constructor(opts?: BlakeOpts);
|
||||||
|
}
|
||||||
|
export declare class BLAKE256 extends Blake1_32 {
|
||||||
|
constructor(opts?: BlakeOpts);
|
||||||
|
}
|
||||||
|
export declare class BLAKE384 extends Blake1_64 {
|
||||||
|
constructor(opts?: BlakeOpts);
|
||||||
|
}
|
||||||
|
export declare class BLAKE512 extends Blake1_64 {
|
||||||
|
constructor(opts?: BlakeOpts);
|
||||||
|
}
|
||||||
|
/** blake1-224 hash function */
|
||||||
|
export declare const blake224: CHashO;
|
||||||
|
/** blake1-256 hash function */
|
||||||
|
export declare const blake256: CHashO;
|
||||||
|
/** blake1-384 hash function */
|
||||||
|
export declare const blake384: CHashO;
|
||||||
|
/** blake1-512 hash function */
|
||||||
|
export declare const blake512: CHashO;
|
||||||
|
export {};
|
||||||
|
//# sourceMappingURL=blake1.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake1.d.ts","sourceRoot":"","sources":["src/blake1.ts"],"names":[],"mappings":"AA4BA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,SAAS;gBACzB,IAAI,GAAE,SAAc;CAGjC;AACD,qBAAa,QAAS,SAAQ,SAAS;gBACzB,IAAI,GAAE,SAAc;CAGjC;AACD,qBAAa,QAAS,SAAQ,SAAS;gBACzB,IAAI,GAAE,SAAc;CAGjC;AACD,qBAAa,QAAS,SAAQ,SAAS;gBACzB,IAAI,GAAE,SAAc;CAGjC;AACD,+BAA+B;AAC/B,eAAO,MAAM,QAAQ,EAAE,MAEtB,CAAC;AACF,+BAA+B;AAC/B,eAAO,MAAM,QAAQ,EAAE,MAEtB,CAAC;AACF,+BAA+B;AAC/B,eAAO,MAAM,QAAQ,EAAE,MAEtB,CAAC;AACF,+BAA+B;AAC/B,eAAO,MAAM,QAAQ,EAAE,MAEtB,CAAC"}
|
||||||
+459
@@ -0,0 +1,459 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.blake512 = exports.blake384 = exports.blake256 = exports.blake224 = exports.BLAKE512 = exports.BLAKE384 = exports.BLAKE256 = exports.BLAKE224 = void 0;
|
||||||
|
/**
|
||||||
|
* Blake1 legacy hash function, one of SHA3 proposals.
|
||||||
|
* Rarely used. Check out blake2 or blake3 instead.
|
||||||
|
* https://www.aumasson.jp/blake/blake.pdf
|
||||||
|
*
|
||||||
|
* In the best case, there are 0 allocations.
|
||||||
|
*
|
||||||
|
* Differences from blake2:
|
||||||
|
*
|
||||||
|
* - BE instead of LE
|
||||||
|
* - Paddings, similar to MD5, RIPEMD, SHA1, SHA2, but:
|
||||||
|
* - length flag is located before actual length
|
||||||
|
* - padding block is compressed differently (no lengths)
|
||||||
|
* Instead of msg[sigma[k]], we have `msg[sigma[k]] ^ constants[sigma[k-1]]`
|
||||||
|
* (-1 for g1, g2 without -1)
|
||||||
|
* - Salt is XOR-ed into constants instead of state
|
||||||
|
* - Salt is XOR-ed with output in `compress`
|
||||||
|
* - Additional rows (+64 bytes) in SIGMA for new rounds
|
||||||
|
* - Different round count:
|
||||||
|
* - 14 / 10 rounds in blake256 / blake2s
|
||||||
|
* - 16 / 12 rounds in blake512 / blake2b
|
||||||
|
* - blake512: G1b: rotr 24 -> 25, G2b: rotr 63 -> 11
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
const _blake_ts_1 = require("./_blake.js");
|
||||||
|
const _md_ts_1 = require("./_md.js");
|
||||||
|
const u64 = require("./_u64.js");
|
||||||
|
// prettier-ignore
|
||||||
|
const utils_ts_1 = require("./utils.js");
|
||||||
|
// Empty zero-filled salt
|
||||||
|
const EMPTY_SALT = /* @__PURE__ */ new Uint32Array(8);
|
||||||
|
class BLAKE1 extends utils_ts_1.Hash {
|
||||||
|
constructor(blockLen, outputLen, lengthFlag, counterLen, saltLen, constants, opts = {}) {
|
||||||
|
super();
|
||||||
|
this.finished = false;
|
||||||
|
this.length = 0;
|
||||||
|
this.pos = 0;
|
||||||
|
this.destroyed = false;
|
||||||
|
const { salt } = opts;
|
||||||
|
this.blockLen = blockLen;
|
||||||
|
this.outputLen = outputLen;
|
||||||
|
this.lengthFlag = lengthFlag;
|
||||||
|
this.counterLen = counterLen;
|
||||||
|
this.buffer = new Uint8Array(blockLen);
|
||||||
|
this.view = (0, utils_ts_1.createView)(this.buffer);
|
||||||
|
if (salt) {
|
||||||
|
let slt = salt;
|
||||||
|
slt = (0, utils_ts_1.toBytes)(slt);
|
||||||
|
(0, utils_ts_1.abytes)(slt);
|
||||||
|
if (slt.length !== 4 * saltLen)
|
||||||
|
throw new Error('wrong salt length');
|
||||||
|
const salt32 = (this.salt = new Uint32Array(saltLen));
|
||||||
|
const sv = (0, utils_ts_1.createView)(slt);
|
||||||
|
this.constants = constants.slice();
|
||||||
|
for (let i = 0, offset = 0; i < salt32.length; i++, offset += 4) {
|
||||||
|
salt32[i] = sv.getUint32(offset, false);
|
||||||
|
this.constants[i] ^= salt32[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.salt = EMPTY_SALT;
|
||||||
|
this.constants = constants;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
update(data) {
|
||||||
|
(0, utils_ts_1.aexists)(this);
|
||||||
|
data = (0, utils_ts_1.toBytes)(data);
|
||||||
|
(0, utils_ts_1.abytes)(data);
|
||||||
|
// From _md, but update length before each compress
|
||||||
|
const { view, buffer, blockLen } = this;
|
||||||
|
const len = data.length;
|
||||||
|
let dataView;
|
||||||
|
for (let pos = 0; pos < len;) {
|
||||||
|
const take = Math.min(blockLen - this.pos, len - pos);
|
||||||
|
// Fast path: we have at least one block in input, cast it to view and process
|
||||||
|
if (take === blockLen) {
|
||||||
|
if (!dataView)
|
||||||
|
dataView = (0, utils_ts_1.createView)(data);
|
||||||
|
for (; blockLen <= len - pos; pos += blockLen) {
|
||||||
|
this.length += blockLen;
|
||||||
|
this.compress(dataView, pos);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buffer.set(data.subarray(pos, pos + take), this.pos);
|
||||||
|
this.pos += take;
|
||||||
|
pos += take;
|
||||||
|
if (this.pos === blockLen) {
|
||||||
|
this.length += blockLen;
|
||||||
|
this.compress(view, 0, true);
|
||||||
|
this.pos = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
this.destroyed = true;
|
||||||
|
if (this.salt !== EMPTY_SALT) {
|
||||||
|
(0, utils_ts_1.clean)(this.salt, this.constants);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_cloneInto(to) {
|
||||||
|
to || (to = new this.constructor());
|
||||||
|
to.set(...this.get());
|
||||||
|
const { buffer, length, finished, destroyed, constants, salt, pos } = this;
|
||||||
|
to.buffer.set(buffer);
|
||||||
|
to.constants = constants.slice();
|
||||||
|
to.destroyed = destroyed;
|
||||||
|
to.finished = finished;
|
||||||
|
to.length = length;
|
||||||
|
to.pos = pos;
|
||||||
|
to.salt = salt.slice();
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
clone() {
|
||||||
|
return this._cloneInto();
|
||||||
|
}
|
||||||
|
digestInto(out) {
|
||||||
|
(0, utils_ts_1.aexists)(this);
|
||||||
|
(0, utils_ts_1.aoutput)(out, this);
|
||||||
|
this.finished = true;
|
||||||
|
// Padding
|
||||||
|
const { buffer, blockLen, counterLen, lengthFlag, view } = this;
|
||||||
|
(0, utils_ts_1.clean)(buffer.subarray(this.pos)); // clean buf
|
||||||
|
const counter = BigInt((this.length + this.pos) * 8);
|
||||||
|
const counterPos = blockLen - counterLen - 1;
|
||||||
|
buffer[this.pos] |= 128; // End block flag
|
||||||
|
this.length += this.pos; // add unwritten length
|
||||||
|
// Not enough in buffer for length: write what we have.
|
||||||
|
if (this.pos > counterPos) {
|
||||||
|
this.compress(view, 0);
|
||||||
|
(0, utils_ts_1.clean)(buffer);
|
||||||
|
this.pos = 0;
|
||||||
|
}
|
||||||
|
// Difference with md: here we have lengthFlag!
|
||||||
|
buffer[counterPos] |= lengthFlag; // Length flag
|
||||||
|
// We always set 8 byte length flag. Because length will overflow significantly sooner.
|
||||||
|
(0, _md_ts_1.setBigUint64)(view, blockLen - 8, counter, false);
|
||||||
|
this.compress(view, 0, this.pos !== 0); // don't add length if length is not empty block?
|
||||||
|
// Write output
|
||||||
|
(0, utils_ts_1.clean)(buffer);
|
||||||
|
const v = (0, utils_ts_1.createView)(out);
|
||||||
|
const state = this.get();
|
||||||
|
for (let i = 0; i < this.outputLen / 4; ++i)
|
||||||
|
v.setUint32(i * 4, state[i]);
|
||||||
|
}
|
||||||
|
digest() {
|
||||||
|
const { buffer, outputLen } = this;
|
||||||
|
this.digestInto(buffer);
|
||||||
|
const res = buffer.slice(0, outputLen);
|
||||||
|
this.destroy();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Constants
|
||||||
|
const B64C = /* @__PURE__ */ Uint32Array.from([
|
||||||
|
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
|
||||||
|
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
|
||||||
|
0x9216d5d9, 0x8979fb1b, 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
|
||||||
|
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69,
|
||||||
|
]);
|
||||||
|
// first half of C512
|
||||||
|
const B32C = B64C.slice(0, 16);
|
||||||
|
const B256_IV = _md_ts_1.SHA256_IV.slice();
|
||||||
|
const B224_IV = _md_ts_1.SHA224_IV.slice();
|
||||||
|
const B384_IV = _md_ts_1.SHA384_IV.slice();
|
||||||
|
const B512_IV = _md_ts_1.SHA512_IV.slice();
|
||||||
|
function generateTBL256() {
|
||||||
|
const TBL = [];
|
||||||
|
for (let i = 0, j = 0; i < 14; i++, j += 16) {
|
||||||
|
for (let offset = 1; offset < 16; offset += 2) {
|
||||||
|
TBL.push(B32C[_blake_ts_1.BSIGMA[j + offset]]);
|
||||||
|
TBL.push(B32C[_blake_ts_1.BSIGMA[j + offset - 1]]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Uint32Array(TBL);
|
||||||
|
}
|
||||||
|
const TBL256 = /* @__PURE__ */ generateTBL256(); // C256[SIGMA[X]] precompute
|
||||||
|
// Reusable temporary buffer
|
||||||
|
const BLAKE256_W = /* @__PURE__ */ new Uint32Array(16);
|
||||||
|
class Blake1_32 extends BLAKE1 {
|
||||||
|
constructor(outputLen, IV, lengthFlag, opts = {}) {
|
||||||
|
super(64, outputLen, lengthFlag, 8, 4, B32C, opts);
|
||||||
|
this.v0 = IV[0] | 0;
|
||||||
|
this.v1 = IV[1] | 0;
|
||||||
|
this.v2 = IV[2] | 0;
|
||||||
|
this.v3 = IV[3] | 0;
|
||||||
|
this.v4 = IV[4] | 0;
|
||||||
|
this.v5 = IV[5] | 0;
|
||||||
|
this.v6 = IV[6] | 0;
|
||||||
|
this.v7 = IV[7] | 0;
|
||||||
|
}
|
||||||
|
get() {
|
||||||
|
const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
|
||||||
|
return [v0, v1, v2, v3, v4, v5, v6, v7];
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
set(v0, v1, v2, v3, v4, v5, v6, v7) {
|
||||||
|
this.v0 = v0 | 0;
|
||||||
|
this.v1 = v1 | 0;
|
||||||
|
this.v2 = v2 | 0;
|
||||||
|
this.v3 = v3 | 0;
|
||||||
|
this.v4 = v4 | 0;
|
||||||
|
this.v5 = v5 | 0;
|
||||||
|
this.v6 = v6 | 0;
|
||||||
|
this.v7 = v7 | 0;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
super.destroy();
|
||||||
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
compress(view, offset, withLength = true) {
|
||||||
|
for (let i = 0; i < 16; i++, offset += 4)
|
||||||
|
BLAKE256_W[i] = view.getUint32(offset, false);
|
||||||
|
// NOTE: we cannot re-use compress from blake2s, since there is additional xor over u256[SIGMA[e]]
|
||||||
|
let v00 = this.v0 | 0;
|
||||||
|
let v01 = this.v1 | 0;
|
||||||
|
let v02 = this.v2 | 0;
|
||||||
|
let v03 = this.v3 | 0;
|
||||||
|
let v04 = this.v4 | 0;
|
||||||
|
let v05 = this.v5 | 0;
|
||||||
|
let v06 = this.v6 | 0;
|
||||||
|
let v07 = this.v7 | 0;
|
||||||
|
let v08 = this.constants[0] | 0;
|
||||||
|
let v09 = this.constants[1] | 0;
|
||||||
|
let v10 = this.constants[2] | 0;
|
||||||
|
let v11 = this.constants[3] | 0;
|
||||||
|
const { h, l } = u64.fromBig(BigInt(withLength ? this.length * 8 : 0));
|
||||||
|
let v12 = (this.constants[4] ^ l) >>> 0;
|
||||||
|
let v13 = (this.constants[5] ^ l) >>> 0;
|
||||||
|
let v14 = (this.constants[6] ^ h) >>> 0;
|
||||||
|
let v15 = (this.constants[7] ^ h) >>> 0;
|
||||||
|
// prettier-ignore
|
||||||
|
for (let i = 0, k = 0, j = 0; i < 14; i++) {
|
||||||
|
({ a: v00, b: v04, c: v08, d: v12 } = (0, _blake_ts_1.G1s)(v00, v04, v08, v12, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v00, b: v04, c: v08, d: v12 } = (0, _blake_ts_1.G2s)(v00, v04, v08, v12, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v01, b: v05, c: v09, d: v13 } = (0, _blake_ts_1.G1s)(v01, v05, v09, v13, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v01, b: v05, c: v09, d: v13 } = (0, _blake_ts_1.G2s)(v01, v05, v09, v13, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v02, b: v06, c: v10, d: v14 } = (0, _blake_ts_1.G1s)(v02, v06, v10, v14, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v02, b: v06, c: v10, d: v14 } = (0, _blake_ts_1.G2s)(v02, v06, v10, v14, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v03, b: v07, c: v11, d: v15 } = (0, _blake_ts_1.G1s)(v03, v07, v11, v15, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v03, b: v07, c: v11, d: v15 } = (0, _blake_ts_1.G2s)(v03, v07, v11, v15, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v00, b: v05, c: v10, d: v15 } = (0, _blake_ts_1.G1s)(v00, v05, v10, v15, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v00, b: v05, c: v10, d: v15 } = (0, _blake_ts_1.G2s)(v00, v05, v10, v15, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v01, b: v06, c: v11, d: v12 } = (0, _blake_ts_1.G1s)(v01, v06, v11, v12, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v01, b: v06, c: v11, d: v12 } = (0, _blake_ts_1.G2s)(v01, v06, v11, v12, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v02, b: v07, c: v08, d: v13 } = (0, _blake_ts_1.G1s)(v02, v07, v08, v13, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v02, b: v07, c: v08, d: v13 } = (0, _blake_ts_1.G2s)(v02, v07, v08, v13, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v03, b: v04, c: v09, d: v14 } = (0, _blake_ts_1.G1s)(v03, v04, v09, v14, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v03, b: v04, c: v09, d: v14 } = (0, _blake_ts_1.G2s)(v03, v04, v09, v14, BLAKE256_W[_blake_ts_1.BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
}
|
||||||
|
this.v0 = (this.v0 ^ v00 ^ v08 ^ this.salt[0]) >>> 0;
|
||||||
|
this.v1 = (this.v1 ^ v01 ^ v09 ^ this.salt[1]) >>> 0;
|
||||||
|
this.v2 = (this.v2 ^ v02 ^ v10 ^ this.salt[2]) >>> 0;
|
||||||
|
this.v3 = (this.v3 ^ v03 ^ v11 ^ this.salt[3]) >>> 0;
|
||||||
|
this.v4 = (this.v4 ^ v04 ^ v12 ^ this.salt[0]) >>> 0;
|
||||||
|
this.v5 = (this.v5 ^ v05 ^ v13 ^ this.salt[1]) >>> 0;
|
||||||
|
this.v6 = (this.v6 ^ v06 ^ v14 ^ this.salt[2]) >>> 0;
|
||||||
|
this.v7 = (this.v7 ^ v07 ^ v15 ^ this.salt[3]) >>> 0;
|
||||||
|
(0, utils_ts_1.clean)(BLAKE256_W);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
||||||
|
const BLAKE512_W = /* @__PURE__ */ new Uint32Array(32);
|
||||||
|
function generateTBL512() {
|
||||||
|
const TBL = [];
|
||||||
|
for (let r = 0, k = 0; r < 16; r++, k += 16) {
|
||||||
|
for (let offset = 1; offset < 16; offset += 2) {
|
||||||
|
TBL.push(B64C[_blake_ts_1.BSIGMA[k + offset] * 2 + 0]);
|
||||||
|
TBL.push(B64C[_blake_ts_1.BSIGMA[k + offset] * 2 + 1]);
|
||||||
|
TBL.push(B64C[_blake_ts_1.BSIGMA[k + offset - 1] * 2 + 0]);
|
||||||
|
TBL.push(B64C[_blake_ts_1.BSIGMA[k + offset - 1] * 2 + 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Uint32Array(TBL);
|
||||||
|
}
|
||||||
|
const TBL512 = /* @__PURE__ */ generateTBL512(); // C512[SIGMA[X]] precompute
|
||||||
|
// Mixing function G splitted in two halfs
|
||||||
|
function G1b(a, b, c, d, msg, k) {
|
||||||
|
const Xpos = 2 * _blake_ts_1.BSIGMA[k];
|
||||||
|
const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
|
||||||
|
let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
|
||||||
|
let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
|
||||||
|
let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
|
||||||
|
let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
|
||||||
|
// v[a] = (v[a] + v[b] + x) | 0;
|
||||||
|
let ll = u64.add3L(Al, Bl, Xl);
|
||||||
|
Ah = u64.add3H(ll, Ah, Bh, Xh) >>> 0;
|
||||||
|
Al = (ll | 0) >>> 0;
|
||||||
|
// v[d] = rotr(v[d] ^ v[a], 32)
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
|
||||||
|
// v[c] = (v[c] + v[d]) | 0;
|
||||||
|
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
||||||
|
// v[b] = rotr(v[b] ^ v[c], 25)
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 25), Bl: u64.rotrSL(Bh, Bl, 25) });
|
||||||
|
(BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah);
|
||||||
|
(BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh);
|
||||||
|
(BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch);
|
||||||
|
(BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh);
|
||||||
|
}
|
||||||
|
function G2b(a, b, c, d, msg, k) {
|
||||||
|
const Xpos = 2 * _blake_ts_1.BSIGMA[k];
|
||||||
|
const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
|
||||||
|
let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
|
||||||
|
let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
|
||||||
|
let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
|
||||||
|
let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
|
||||||
|
// v[a] = (v[a] + v[b] + x) | 0;
|
||||||
|
let ll = u64.add3L(Al, Bl, Xl);
|
||||||
|
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
||||||
|
Al = ll | 0;
|
||||||
|
// v[d] = rotr(v[d] ^ v[a], 16)
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
|
||||||
|
// v[c] = (v[c] + v[d]) | 0;
|
||||||
|
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
||||||
|
// v[b] = rotr(v[b] ^ v[c], 11)
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 11), Bl: u64.rotrSL(Bh, Bl, 11) });
|
||||||
|
(BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah);
|
||||||
|
(BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh);
|
||||||
|
(BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch);
|
||||||
|
(BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh);
|
||||||
|
}
|
||||||
|
class Blake1_64 extends BLAKE1 {
|
||||||
|
constructor(outputLen, IV, lengthFlag, opts = {}) {
|
||||||
|
super(128, outputLen, lengthFlag, 16, 8, B64C, opts);
|
||||||
|
this.v0l = IV[0] | 0;
|
||||||
|
this.v0h = IV[1] | 0;
|
||||||
|
this.v1l = IV[2] | 0;
|
||||||
|
this.v1h = IV[3] | 0;
|
||||||
|
this.v2l = IV[4] | 0;
|
||||||
|
this.v2h = IV[5] | 0;
|
||||||
|
this.v3l = IV[6] | 0;
|
||||||
|
this.v3h = IV[7] | 0;
|
||||||
|
this.v4l = IV[8] | 0;
|
||||||
|
this.v4h = IV[9] | 0;
|
||||||
|
this.v5l = IV[10] | 0;
|
||||||
|
this.v5h = IV[11] | 0;
|
||||||
|
this.v6l = IV[12] | 0;
|
||||||
|
this.v6h = IV[13] | 0;
|
||||||
|
this.v7l = IV[14] | 0;
|
||||||
|
this.v7h = IV[15] | 0;
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
get() {
|
||||||
|
let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
|
||||||
|
return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
|
||||||
|
this.v0l = v0l | 0;
|
||||||
|
this.v0h = v0h | 0;
|
||||||
|
this.v1l = v1l | 0;
|
||||||
|
this.v1h = v1h | 0;
|
||||||
|
this.v2l = v2l | 0;
|
||||||
|
this.v2h = v2h | 0;
|
||||||
|
this.v3l = v3l | 0;
|
||||||
|
this.v3h = v3h | 0;
|
||||||
|
this.v4l = v4l | 0;
|
||||||
|
this.v4h = v4h | 0;
|
||||||
|
this.v5l = v5l | 0;
|
||||||
|
this.v5h = v5h | 0;
|
||||||
|
this.v6l = v6l | 0;
|
||||||
|
this.v6h = v6h | 0;
|
||||||
|
this.v7l = v7l | 0;
|
||||||
|
this.v7h = v7h | 0;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
super.destroy();
|
||||||
|
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
compress(view, offset, withLength = true) {
|
||||||
|
for (let i = 0; i < 32; i++, offset += 4)
|
||||||
|
BLAKE512_W[i] = view.getUint32(offset, false);
|
||||||
|
this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
|
||||||
|
BBUF.set(this.constants.subarray(0, 16), 16);
|
||||||
|
if (withLength) {
|
||||||
|
const { h, l } = u64.fromBig(BigInt(this.length * 8));
|
||||||
|
BBUF[24] = (BBUF[24] ^ h) >>> 0;
|
||||||
|
BBUF[25] = (BBUF[25] ^ l) >>> 0;
|
||||||
|
BBUF[26] = (BBUF[26] ^ h) >>> 0;
|
||||||
|
BBUF[27] = (BBUF[27] ^ l) >>> 0;
|
||||||
|
}
|
||||||
|
for (let i = 0, k = 0; i < 16; i++) {
|
||||||
|
G1b(0, 4, 8, 12, BLAKE512_W, k++);
|
||||||
|
G2b(0, 4, 8, 12, BLAKE512_W, k++);
|
||||||
|
G1b(1, 5, 9, 13, BLAKE512_W, k++);
|
||||||
|
G2b(1, 5, 9, 13, BLAKE512_W, k++);
|
||||||
|
G1b(2, 6, 10, 14, BLAKE512_W, k++);
|
||||||
|
G2b(2, 6, 10, 14, BLAKE512_W, k++);
|
||||||
|
G1b(3, 7, 11, 15, BLAKE512_W, k++);
|
||||||
|
G2b(3, 7, 11, 15, BLAKE512_W, k++);
|
||||||
|
G1b(0, 5, 10, 15, BLAKE512_W, k++);
|
||||||
|
G2b(0, 5, 10, 15, BLAKE512_W, k++);
|
||||||
|
G1b(1, 6, 11, 12, BLAKE512_W, k++);
|
||||||
|
G2b(1, 6, 11, 12, BLAKE512_W, k++);
|
||||||
|
G1b(2, 7, 8, 13, BLAKE512_W, k++);
|
||||||
|
G2b(2, 7, 8, 13, BLAKE512_W, k++);
|
||||||
|
G1b(3, 4, 9, 14, BLAKE512_W, k++);
|
||||||
|
G2b(3, 4, 9, 14, BLAKE512_W, k++);
|
||||||
|
}
|
||||||
|
this.v0l ^= BBUF[0] ^ BBUF[16] ^ this.salt[0];
|
||||||
|
this.v0h ^= BBUF[1] ^ BBUF[17] ^ this.salt[1];
|
||||||
|
this.v1l ^= BBUF[2] ^ BBUF[18] ^ this.salt[2];
|
||||||
|
this.v1h ^= BBUF[3] ^ BBUF[19] ^ this.salt[3];
|
||||||
|
this.v2l ^= BBUF[4] ^ BBUF[20] ^ this.salt[4];
|
||||||
|
this.v2h ^= BBUF[5] ^ BBUF[21] ^ this.salt[5];
|
||||||
|
this.v3l ^= BBUF[6] ^ BBUF[22] ^ this.salt[6];
|
||||||
|
this.v3h ^= BBUF[7] ^ BBUF[23] ^ this.salt[7];
|
||||||
|
this.v4l ^= BBUF[8] ^ BBUF[24] ^ this.salt[0];
|
||||||
|
this.v4h ^= BBUF[9] ^ BBUF[25] ^ this.salt[1];
|
||||||
|
this.v5l ^= BBUF[10] ^ BBUF[26] ^ this.salt[2];
|
||||||
|
this.v5h ^= BBUF[11] ^ BBUF[27] ^ this.salt[3];
|
||||||
|
this.v6l ^= BBUF[12] ^ BBUF[28] ^ this.salt[4];
|
||||||
|
this.v6h ^= BBUF[13] ^ BBUF[29] ^ this.salt[5];
|
||||||
|
this.v7l ^= BBUF[14] ^ BBUF[30] ^ this.salt[6];
|
||||||
|
this.v7h ^= BBUF[15] ^ BBUF[31] ^ this.salt[7];
|
||||||
|
(0, utils_ts_1.clean)(BBUF, BLAKE512_W);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class BLAKE224 extends Blake1_32 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
super(28, B224_IV, 0, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BLAKE224 = BLAKE224;
|
||||||
|
class BLAKE256 extends Blake1_32 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
super(32, B256_IV, 1, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BLAKE256 = BLAKE256;
|
||||||
|
class BLAKE384 extends Blake1_64 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
super(48, B384_IV, 0, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BLAKE384 = BLAKE384;
|
||||||
|
class BLAKE512 extends Blake1_64 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
super(64, B512_IV, 1, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BLAKE512 = BLAKE512;
|
||||||
|
/** blake1-224 hash function */
|
||||||
|
exports.blake224 = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE224(opts));
|
||||||
|
/** blake1-256 hash function */
|
||||||
|
exports.blake256 = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE256(opts));
|
||||||
|
/** blake1-384 hash function */
|
||||||
|
exports.blake384 = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE384(opts));
|
||||||
|
/** blake1-512 hash function */
|
||||||
|
exports.blake512 = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE512(opts));
|
||||||
|
//# sourceMappingURL=blake1.js.map
|
||||||
+1
File diff suppressed because one or more lines are too long
+116
@@ -0,0 +1,116 @@
|
|||||||
|
import { Hash, type CHashO, type Input } from './utils.ts';
|
||||||
|
/** Blake hash options. dkLen is output length. key is used in MAC mode. salt is used in KDF mode. */
|
||||||
|
export type Blake2Opts = {
|
||||||
|
dkLen?: number;
|
||||||
|
key?: Input;
|
||||||
|
salt?: Input;
|
||||||
|
personalization?: Input;
|
||||||
|
};
|
||||||
|
/** Class, from which others are subclassed. */
|
||||||
|
export declare abstract class BLAKE2<T extends BLAKE2<T>> extends Hash<T> {
|
||||||
|
protected abstract compress(msg: Uint32Array, offset: number, isLast: boolean): void;
|
||||||
|
protected abstract get(): number[];
|
||||||
|
protected abstract set(...args: number[]): void;
|
||||||
|
abstract destroy(): void;
|
||||||
|
protected buffer: Uint8Array;
|
||||||
|
protected buffer32: Uint32Array;
|
||||||
|
protected finished: boolean;
|
||||||
|
protected destroyed: boolean;
|
||||||
|
protected length: number;
|
||||||
|
protected pos: number;
|
||||||
|
readonly blockLen: number;
|
||||||
|
readonly outputLen: number;
|
||||||
|
constructor(blockLen: number, outputLen: number);
|
||||||
|
update(data: Input): this;
|
||||||
|
digestInto(out: Uint8Array): void;
|
||||||
|
digest(): Uint8Array;
|
||||||
|
_cloneInto(to?: T): T;
|
||||||
|
clone(): T;
|
||||||
|
}
|
||||||
|
export declare class BLAKE2b extends BLAKE2<BLAKE2b> {
|
||||||
|
private v0l;
|
||||||
|
private v0h;
|
||||||
|
private v1l;
|
||||||
|
private v1h;
|
||||||
|
private v2l;
|
||||||
|
private v2h;
|
||||||
|
private v3l;
|
||||||
|
private v3h;
|
||||||
|
private v4l;
|
||||||
|
private v4h;
|
||||||
|
private v5l;
|
||||||
|
private v5h;
|
||||||
|
private v6l;
|
||||||
|
private v6h;
|
||||||
|
private v7l;
|
||||||
|
private v7h;
|
||||||
|
constructor(opts?: Blake2Opts);
|
||||||
|
protected get(): [
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number
|
||||||
|
];
|
||||||
|
protected set(v0l: number, v0h: number, v1l: number, v1h: number, v2l: number, v2h: number, v3l: number, v3h: number, v4l: number, v4h: number, v5l: number, v5h: number, v6l: number, v6h: number, v7l: number, v7h: number): void;
|
||||||
|
protected compress(msg: Uint32Array, offset: number, isLast: boolean): void;
|
||||||
|
destroy(): void;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
||||||
|
*/
|
||||||
|
export declare const blake2b: CHashO;
|
||||||
|
export type Num16 = {
|
||||||
|
v0: number;
|
||||||
|
v1: number;
|
||||||
|
v2: number;
|
||||||
|
v3: number;
|
||||||
|
v4: number;
|
||||||
|
v5: number;
|
||||||
|
v6: number;
|
||||||
|
v7: number;
|
||||||
|
v8: number;
|
||||||
|
v9: number;
|
||||||
|
v10: number;
|
||||||
|
v11: number;
|
||||||
|
v12: number;
|
||||||
|
v13: number;
|
||||||
|
v14: number;
|
||||||
|
v15: number;
|
||||||
|
};
|
||||||
|
export declare function compress(s: Uint8Array, offset: number, msg: Uint32Array, rounds: number, v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number, v8: number, v9: number, v10: number, v11: number, v12: number, v13: number, v14: number, v15: number): Num16;
|
||||||
|
export declare class BLAKE2s extends BLAKE2<BLAKE2s> {
|
||||||
|
private v0;
|
||||||
|
private v1;
|
||||||
|
private v2;
|
||||||
|
private v3;
|
||||||
|
private v4;
|
||||||
|
private v5;
|
||||||
|
private v6;
|
||||||
|
private v7;
|
||||||
|
constructor(opts?: Blake2Opts);
|
||||||
|
protected get(): [number, number, number, number, number, number, number, number];
|
||||||
|
protected set(v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number): void;
|
||||||
|
protected compress(msg: Uint32Array, offset: number, isLast: boolean): void;
|
||||||
|
destroy(): void;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. 1.5x faster than blake2b in JS.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
||||||
|
*/
|
||||||
|
export declare const blake2s: CHashO;
|
||||||
|
//# sourceMappingURL=blake2.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake2.d.ts","sourceRoot":"","sources":["src/blake2.ts"],"names":[],"mappings":"AASA,OAAO,EAEmB,IAAI,EAC5B,KAAK,MAAM,EAAE,KAAK,KAAK,EACxB,MAAM,YAAY,CAAC;AAEpB,qGAAqG;AACrG,MAAM,MAAM,UAAU,GAAG;IACvB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,KAAK,CAAC;IACZ,IAAI,CAAC,EAAE,KAAK,CAAC;IACb,eAAe,CAAC,EAAE,KAAK,CAAC;CACzB,CAAC;AA+EF,+CAA+C;AAC/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;IAC3B,OAAO,CAAC,EAAE,CAAiB;IAC3B,OAAO,CAAC,EAAE,CAAiB;IAC3B,OAAO,CAAC,EAAE,CAAiB;gBAEf,IAAI,GAAE,UAAe;IA+BjC,SAAS,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC;IAKjF,SAAS,CAAC,GAAG,CACX,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAC7F,IAAI;IAUP,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI;IAkB3E,OAAO,IAAI,IAAI;CAKhB;AAED;;;;GAIG;AACH,eAAO,MAAM,OAAO,EAAE,MAErB,CAAC"}
|
||||||
+420
@@ -0,0 +1,420 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.blake2s = exports.BLAKE2s = exports.blake2b = exports.BLAKE2b = exports.BLAKE2 = void 0;
|
||||||
|
exports.compress = compress;
|
||||||
|
/**
|
||||||
|
* blake2b (64-bit) & blake2s (8 to 32-bit) hash functions.
|
||||||
|
* b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
const _blake_ts_1 = require("./_blake.js");
|
||||||
|
const _md_ts_1 = require("./_md.js");
|
||||||
|
const u64 = require("./_u64.js");
|
||||||
|
// prettier-ignore
|
||||||
|
const utils_ts_1 = require("./utils.js");
|
||||||
|
// Same as SHA512_IV, but swapped endianness: LE instead of BE. iv[1] is iv[0], etc.
|
||||||
|
const B2B_IV = /* @__PURE__ */ Uint32Array.from([
|
||||||
|
0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
|
||||||
|
0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
|
||||||
|
]);
|
||||||
|
// Temporary buffer
|
||||||
|
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
||||||
|
// Mixing function G splitted in two halfs
|
||||||
|
function G1b(a, b, c, d, msg, x) {
|
||||||
|
// NOTE: V is LE here
|
||||||
|
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
||||||
|
let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
|
||||||
|
let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
|
||||||
|
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
|
||||||
|
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
|
||||||
|
// v[a] = (v[a] + v[b] + x) | 0;
|
||||||
|
let ll = u64.add3L(Al, Bl, Xl);
|
||||||
|
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
||||||
|
Al = ll | 0;
|
||||||
|
// v[d] = rotr(v[d] ^ v[a], 32)
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
|
||||||
|
// v[c] = (v[c] + v[d]) | 0;
|
||||||
|
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
||||||
|
// v[b] = rotr(v[b] ^ v[c], 24)
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 24), Bl: u64.rotrSL(Bh, Bl, 24) });
|
||||||
|
(BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
|
||||||
|
(BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
|
||||||
|
(BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
|
||||||
|
(BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
|
||||||
|
}
|
||||||
|
function G2b(a, b, c, d, msg, x) {
|
||||||
|
// NOTE: V is LE here
|
||||||
|
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
||||||
|
let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
|
||||||
|
let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
|
||||||
|
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
|
||||||
|
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
|
||||||
|
// v[a] = (v[a] + v[b] + x) | 0;
|
||||||
|
let ll = u64.add3L(Al, Bl, Xl);
|
||||||
|
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
||||||
|
Al = ll | 0;
|
||||||
|
// v[d] = rotr(v[d] ^ v[a], 16)
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
|
||||||
|
// v[c] = (v[c] + v[d]) | 0;
|
||||||
|
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
||||||
|
// v[b] = rotr(v[b] ^ v[c], 63)
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: u64.rotrBH(Bh, Bl, 63), Bl: u64.rotrBL(Bh, Bl, 63) });
|
||||||
|
(BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
|
||||||
|
(BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
|
||||||
|
(BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
|
||||||
|
(BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
|
||||||
|
}
|
||||||
|
function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
|
||||||
|
(0, utils_ts_1.anumber)(keyLen);
|
||||||
|
if (outputLen < 0 || outputLen > keyLen)
|
||||||
|
throw new Error('outputLen bigger than keyLen');
|
||||||
|
const { key, salt, personalization } = opts;
|
||||||
|
if (key !== undefined && (key.length < 1 || key.length > keyLen))
|
||||||
|
throw new Error('key length must be undefined or 1..' + keyLen);
|
||||||
|
if (salt !== undefined && salt.length !== saltLen)
|
||||||
|
throw new Error('salt must be undefined or ' + saltLen);
|
||||||
|
if (personalization !== undefined && personalization.length !== persLen)
|
||||||
|
throw new Error('personalization must be undefined or ' + persLen);
|
||||||
|
}
|
||||||
|
/** Class, from which others are subclassed. */
|
||||||
|
class BLAKE2 extends utils_ts_1.Hash {
|
||||||
|
constructor(blockLen, outputLen) {
|
||||||
|
super();
|
||||||
|
this.finished = false;
|
||||||
|
this.destroyed = false;
|
||||||
|
this.length = 0;
|
||||||
|
this.pos = 0;
|
||||||
|
(0, utils_ts_1.anumber)(blockLen);
|
||||||
|
(0, utils_ts_1.anumber)(outputLen);
|
||||||
|
this.blockLen = blockLen;
|
||||||
|
this.outputLen = outputLen;
|
||||||
|
this.buffer = new Uint8Array(blockLen);
|
||||||
|
this.buffer32 = (0, utils_ts_1.u32)(this.buffer);
|
||||||
|
}
|
||||||
|
update(data) {
|
||||||
|
(0, utils_ts_1.aexists)(this);
|
||||||
|
data = (0, utils_ts_1.toBytes)(data);
|
||||||
|
(0, utils_ts_1.abytes)(data);
|
||||||
|
// Main difference with other hashes: there is flag for last block,
|
||||||
|
// so we cannot process current block before we know that there
|
||||||
|
// is the next one. This significantly complicates logic and reduces ability
|
||||||
|
// to do zero-copy processing
|
||||||
|
const { blockLen, buffer, buffer32 } = this;
|
||||||
|
const len = data.length;
|
||||||
|
const offset = data.byteOffset;
|
||||||
|
const buf = data.buffer;
|
||||||
|
for (let pos = 0; pos < len;) {
|
||||||
|
// If buffer is full and we still have input (don't process last block, same as blake2s)
|
||||||
|
if (this.pos === blockLen) {
|
||||||
|
(0, utils_ts_1.swap32IfBE)(buffer32);
|
||||||
|
this.compress(buffer32, 0, false);
|
||||||
|
(0, utils_ts_1.swap32IfBE)(buffer32);
|
||||||
|
this.pos = 0;
|
||||||
|
}
|
||||||
|
const take = Math.min(blockLen - this.pos, len - pos);
|
||||||
|
const dataOffset = offset + pos;
|
||||||
|
// full block && aligned to 4 bytes && not last in input
|
||||||
|
if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
|
||||||
|
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
|
||||||
|
(0, utils_ts_1.swap32IfBE)(data32);
|
||||||
|
for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) {
|
||||||
|
this.length += blockLen;
|
||||||
|
this.compress(data32, pos32, false);
|
||||||
|
}
|
||||||
|
(0, utils_ts_1.swap32IfBE)(data32);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buffer.set(data.subarray(pos, pos + take), this.pos);
|
||||||
|
this.pos += take;
|
||||||
|
this.length += take;
|
||||||
|
pos += take;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
digestInto(out) {
|
||||||
|
(0, utils_ts_1.aexists)(this);
|
||||||
|
(0, utils_ts_1.aoutput)(out, this);
|
||||||
|
const { pos, buffer32 } = this;
|
||||||
|
this.finished = true;
|
||||||
|
// Padding
|
||||||
|
(0, utils_ts_1.clean)(this.buffer.subarray(pos));
|
||||||
|
(0, utils_ts_1.swap32IfBE)(buffer32);
|
||||||
|
this.compress(buffer32, 0, true);
|
||||||
|
(0, utils_ts_1.swap32IfBE)(buffer32);
|
||||||
|
const out32 = (0, utils_ts_1.u32)(out);
|
||||||
|
this.get().forEach((v, i) => (out32[i] = (0, utils_ts_1.swap8IfBE)(v)));
|
||||||
|
}
|
||||||
|
digest() {
|
||||||
|
const { buffer, outputLen } = this;
|
||||||
|
this.digestInto(buffer);
|
||||||
|
const res = buffer.slice(0, outputLen);
|
||||||
|
this.destroy();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
_cloneInto(to) {
|
||||||
|
const { buffer, length, finished, destroyed, outputLen, pos } = this;
|
||||||
|
to || (to = new this.constructor({ dkLen: outputLen }));
|
||||||
|
to.set(...this.get());
|
||||||
|
to.buffer.set(buffer);
|
||||||
|
to.destroyed = destroyed;
|
||||||
|
to.finished = finished;
|
||||||
|
to.length = length;
|
||||||
|
to.pos = pos;
|
||||||
|
// @ts-ignore
|
||||||
|
to.outputLen = outputLen;
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
clone() {
|
||||||
|
return this._cloneInto();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BLAKE2 = BLAKE2;
|
||||||
|
class BLAKE2b extends BLAKE2 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
const olen = opts.dkLen === undefined ? 64 : opts.dkLen;
|
||||||
|
super(128, olen);
|
||||||
|
// Same as SHA-512, but LE
|
||||||
|
this.v0l = B2B_IV[0] | 0;
|
||||||
|
this.v0h = B2B_IV[1] | 0;
|
||||||
|
this.v1l = B2B_IV[2] | 0;
|
||||||
|
this.v1h = B2B_IV[3] | 0;
|
||||||
|
this.v2l = B2B_IV[4] | 0;
|
||||||
|
this.v2h = B2B_IV[5] | 0;
|
||||||
|
this.v3l = B2B_IV[6] | 0;
|
||||||
|
this.v3h = B2B_IV[7] | 0;
|
||||||
|
this.v4l = B2B_IV[8] | 0;
|
||||||
|
this.v4h = B2B_IV[9] | 0;
|
||||||
|
this.v5l = B2B_IV[10] | 0;
|
||||||
|
this.v5h = B2B_IV[11] | 0;
|
||||||
|
this.v6l = B2B_IV[12] | 0;
|
||||||
|
this.v6h = B2B_IV[13] | 0;
|
||||||
|
this.v7l = B2B_IV[14] | 0;
|
||||||
|
this.v7h = B2B_IV[15] | 0;
|
||||||
|
checkBlake2Opts(olen, opts, 64, 16, 16);
|
||||||
|
let { key, personalization, salt } = opts;
|
||||||
|
let keyLength = 0;
|
||||||
|
if (key !== undefined) {
|
||||||
|
key = (0, utils_ts_1.toBytes)(key);
|
||||||
|
keyLength = key.length;
|
||||||
|
}
|
||||||
|
this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
||||||
|
if (salt !== undefined) {
|
||||||
|
salt = (0, utils_ts_1.toBytes)(salt);
|
||||||
|
const slt = (0, utils_ts_1.u32)(salt);
|
||||||
|
this.v4l ^= (0, utils_ts_1.swap8IfBE)(slt[0]);
|
||||||
|
this.v4h ^= (0, utils_ts_1.swap8IfBE)(slt[1]);
|
||||||
|
this.v5l ^= (0, utils_ts_1.swap8IfBE)(slt[2]);
|
||||||
|
this.v5h ^= (0, utils_ts_1.swap8IfBE)(slt[3]);
|
||||||
|
}
|
||||||
|
if (personalization !== undefined) {
|
||||||
|
personalization = (0, utils_ts_1.toBytes)(personalization);
|
||||||
|
const pers = (0, utils_ts_1.u32)(personalization);
|
||||||
|
this.v6l ^= (0, utils_ts_1.swap8IfBE)(pers[0]);
|
||||||
|
this.v6h ^= (0, utils_ts_1.swap8IfBE)(pers[1]);
|
||||||
|
this.v7l ^= (0, utils_ts_1.swap8IfBE)(pers[2]);
|
||||||
|
this.v7h ^= (0, utils_ts_1.swap8IfBE)(pers[3]);
|
||||||
|
}
|
||||||
|
if (key !== undefined) {
|
||||||
|
// Pad to blockLen and update
|
||||||
|
const tmp = new Uint8Array(this.blockLen);
|
||||||
|
tmp.set(key);
|
||||||
|
this.update(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
get() {
|
||||||
|
let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
|
||||||
|
return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
|
||||||
|
this.v0l = v0l | 0;
|
||||||
|
this.v0h = v0h | 0;
|
||||||
|
this.v1l = v1l | 0;
|
||||||
|
this.v1h = v1h | 0;
|
||||||
|
this.v2l = v2l | 0;
|
||||||
|
this.v2h = v2h | 0;
|
||||||
|
this.v3l = v3l | 0;
|
||||||
|
this.v3h = v3h | 0;
|
||||||
|
this.v4l = v4l | 0;
|
||||||
|
this.v4h = v4h | 0;
|
||||||
|
this.v5l = v5l | 0;
|
||||||
|
this.v5h = v5h | 0;
|
||||||
|
this.v6l = v6l | 0;
|
||||||
|
this.v6h = v6h | 0;
|
||||||
|
this.v7l = v7l | 0;
|
||||||
|
this.v7h = v7h | 0;
|
||||||
|
}
|
||||||
|
compress(msg, offset, isLast) {
|
||||||
|
this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
|
||||||
|
BBUF.set(B2B_IV, 16); // Second half from IV.
|
||||||
|
let { h, l } = u64.fromBig(BigInt(this.length));
|
||||||
|
BBUF[24] = B2B_IV[8] ^ l; // Low word of the offset.
|
||||||
|
BBUF[25] = B2B_IV[9] ^ h; // High word.
|
||||||
|
// Invert all bits for last block
|
||||||
|
if (isLast) {
|
||||||
|
BBUF[28] = ~BBUF[28];
|
||||||
|
BBUF[29] = ~BBUF[29];
|
||||||
|
}
|
||||||
|
let j = 0;
|
||||||
|
const s = _blake_ts_1.BSIGMA;
|
||||||
|
for (let i = 0; i < 12; i++) {
|
||||||
|
G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
|
||||||
|
}
|
||||||
|
this.v0l ^= BBUF[0] ^ BBUF[16];
|
||||||
|
this.v0h ^= BBUF[1] ^ BBUF[17];
|
||||||
|
this.v1l ^= BBUF[2] ^ BBUF[18];
|
||||||
|
this.v1h ^= BBUF[3] ^ BBUF[19];
|
||||||
|
this.v2l ^= BBUF[4] ^ BBUF[20];
|
||||||
|
this.v2h ^= BBUF[5] ^ BBUF[21];
|
||||||
|
this.v3l ^= BBUF[6] ^ BBUF[22];
|
||||||
|
this.v3h ^= BBUF[7] ^ BBUF[23];
|
||||||
|
this.v4l ^= BBUF[8] ^ BBUF[24];
|
||||||
|
this.v4h ^= BBUF[9] ^ BBUF[25];
|
||||||
|
this.v5l ^= BBUF[10] ^ BBUF[26];
|
||||||
|
this.v5h ^= BBUF[11] ^ BBUF[27];
|
||||||
|
this.v6l ^= BBUF[12] ^ BBUF[28];
|
||||||
|
this.v6h ^= BBUF[13] ^ BBUF[29];
|
||||||
|
this.v7l ^= BBUF[14] ^ BBUF[30];
|
||||||
|
this.v7h ^= BBUF[15] ^ BBUF[31];
|
||||||
|
(0, utils_ts_1.clean)(BBUF);
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
this.destroyed = true;
|
||||||
|
(0, utils_ts_1.clean)(this.buffer32);
|
||||||
|
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BLAKE2b = BLAKE2b;
|
||||||
|
/**
|
||||||
|
* Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
||||||
|
*/
|
||||||
|
exports.blake2b = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE2b(opts));
|
||||||
|
// prettier-ignore
|
||||||
|
function compress(s, offset, msg, rounds, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
|
||||||
|
let j = 0;
|
||||||
|
for (let i = 0; i < rounds; i++) {
|
||||||
|
({ a: v0, b: v4, c: v8, d: v12 } = (0, _blake_ts_1.G1s)(v0, v4, v8, v12, msg[offset + s[j++]]));
|
||||||
|
({ a: v0, b: v4, c: v8, d: v12 } = (0, _blake_ts_1.G2s)(v0, v4, v8, v12, msg[offset + s[j++]]));
|
||||||
|
({ a: v1, b: v5, c: v9, d: v13 } = (0, _blake_ts_1.G1s)(v1, v5, v9, v13, msg[offset + s[j++]]));
|
||||||
|
({ a: v1, b: v5, c: v9, d: v13 } = (0, _blake_ts_1.G2s)(v1, v5, v9, v13, msg[offset + s[j++]]));
|
||||||
|
({ a: v2, b: v6, c: v10, d: v14 } = (0, _blake_ts_1.G1s)(v2, v6, v10, v14, msg[offset + s[j++]]));
|
||||||
|
({ a: v2, b: v6, c: v10, d: v14 } = (0, _blake_ts_1.G2s)(v2, v6, v10, v14, msg[offset + s[j++]]));
|
||||||
|
({ a: v3, b: v7, c: v11, d: v15 } = (0, _blake_ts_1.G1s)(v3, v7, v11, v15, msg[offset + s[j++]]));
|
||||||
|
({ a: v3, b: v7, c: v11, d: v15 } = (0, _blake_ts_1.G2s)(v3, v7, v11, v15, msg[offset + s[j++]]));
|
||||||
|
({ a: v0, b: v5, c: v10, d: v15 } = (0, _blake_ts_1.G1s)(v0, v5, v10, v15, msg[offset + s[j++]]));
|
||||||
|
({ a: v0, b: v5, c: v10, d: v15 } = (0, _blake_ts_1.G2s)(v0, v5, v10, v15, msg[offset + s[j++]]));
|
||||||
|
({ a: v1, b: v6, c: v11, d: v12 } = (0, _blake_ts_1.G1s)(v1, v6, v11, v12, msg[offset + s[j++]]));
|
||||||
|
({ a: v1, b: v6, c: v11, d: v12 } = (0, _blake_ts_1.G2s)(v1, v6, v11, v12, msg[offset + s[j++]]));
|
||||||
|
({ a: v2, b: v7, c: v8, d: v13 } = (0, _blake_ts_1.G1s)(v2, v7, v8, v13, msg[offset + s[j++]]));
|
||||||
|
({ a: v2, b: v7, c: v8, d: v13 } = (0, _blake_ts_1.G2s)(v2, v7, v8, v13, msg[offset + s[j++]]));
|
||||||
|
({ a: v3, b: v4, c: v9, d: v14 } = (0, _blake_ts_1.G1s)(v3, v4, v9, v14, msg[offset + s[j++]]));
|
||||||
|
({ a: v3, b: v4, c: v9, d: v14 } = (0, _blake_ts_1.G2s)(v3, v4, v9, v14, msg[offset + s[j++]]));
|
||||||
|
}
|
||||||
|
return { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 };
|
||||||
|
}
|
||||||
|
const B2S_IV = _md_ts_1.SHA256_IV;
|
||||||
|
class BLAKE2s extends BLAKE2 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
const olen = opts.dkLen === undefined ? 32 : opts.dkLen;
|
||||||
|
super(64, olen);
|
||||||
|
// Internal state, same as SHA-256
|
||||||
|
this.v0 = B2S_IV[0] | 0;
|
||||||
|
this.v1 = B2S_IV[1] | 0;
|
||||||
|
this.v2 = B2S_IV[2] | 0;
|
||||||
|
this.v3 = B2S_IV[3] | 0;
|
||||||
|
this.v4 = B2S_IV[4] | 0;
|
||||||
|
this.v5 = B2S_IV[5] | 0;
|
||||||
|
this.v6 = B2S_IV[6] | 0;
|
||||||
|
this.v7 = B2S_IV[7] | 0;
|
||||||
|
checkBlake2Opts(olen, opts, 32, 8, 8);
|
||||||
|
let { key, personalization, salt } = opts;
|
||||||
|
let keyLength = 0;
|
||||||
|
if (key !== undefined) {
|
||||||
|
key = (0, utils_ts_1.toBytes)(key);
|
||||||
|
keyLength = key.length;
|
||||||
|
}
|
||||||
|
this.v0 ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
||||||
|
if (salt !== undefined) {
|
||||||
|
salt = (0, utils_ts_1.toBytes)(salt);
|
||||||
|
const slt = (0, utils_ts_1.u32)(salt);
|
||||||
|
this.v4 ^= (0, utils_ts_1.swap8IfBE)(slt[0]);
|
||||||
|
this.v5 ^= (0, utils_ts_1.swap8IfBE)(slt[1]);
|
||||||
|
}
|
||||||
|
if (personalization !== undefined) {
|
||||||
|
personalization = (0, utils_ts_1.toBytes)(personalization);
|
||||||
|
const pers = (0, utils_ts_1.u32)(personalization);
|
||||||
|
this.v6 ^= (0, utils_ts_1.swap8IfBE)(pers[0]);
|
||||||
|
this.v7 ^= (0, utils_ts_1.swap8IfBE)(pers[1]);
|
||||||
|
}
|
||||||
|
if (key !== undefined) {
|
||||||
|
// Pad to blockLen and update
|
||||||
|
(0, utils_ts_1.abytes)(key);
|
||||||
|
const tmp = new Uint8Array(this.blockLen);
|
||||||
|
tmp.set(key);
|
||||||
|
this.update(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get() {
|
||||||
|
const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
|
||||||
|
return [v0, v1, v2, v3, v4, v5, v6, v7];
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
set(v0, v1, v2, v3, v4, v5, v6, v7) {
|
||||||
|
this.v0 = v0 | 0;
|
||||||
|
this.v1 = v1 | 0;
|
||||||
|
this.v2 = v2 | 0;
|
||||||
|
this.v3 = v3 | 0;
|
||||||
|
this.v4 = v4 | 0;
|
||||||
|
this.v5 = v5 | 0;
|
||||||
|
this.v6 = v6 | 0;
|
||||||
|
this.v7 = v7 | 0;
|
||||||
|
}
|
||||||
|
compress(msg, offset, isLast) {
|
||||||
|
const { h, l } = u64.fromBig(BigInt(this.length));
|
||||||
|
// prettier-ignore
|
||||||
|
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = compress(_blake_ts_1.BSIGMA, offset, msg, 10, this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7, B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l ^ B2S_IV[4], h ^ B2S_IV[5], isLast ? ~B2S_IV[6] : B2S_IV[6], B2S_IV[7]);
|
||||||
|
this.v0 ^= v0 ^ v8;
|
||||||
|
this.v1 ^= v1 ^ v9;
|
||||||
|
this.v2 ^= v2 ^ v10;
|
||||||
|
this.v3 ^= v3 ^ v11;
|
||||||
|
this.v4 ^= v4 ^ v12;
|
||||||
|
this.v5 ^= v5 ^ v13;
|
||||||
|
this.v6 ^= v6 ^ v14;
|
||||||
|
this.v7 ^= v7 ^ v15;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
this.destroyed = true;
|
||||||
|
(0, utils_ts_1.clean)(this.buffer32);
|
||||||
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BLAKE2s = BLAKE2s;
|
||||||
|
/**
|
||||||
|
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. 1.5x faster than blake2b in JS.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
||||||
|
*/
|
||||||
|
exports.blake2s = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE2s(opts));
|
||||||
|
//# sourceMappingURL=blake2.js.map
|
||||||
+1
File diff suppressed because one or more lines are too long
+11
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* Blake2b hash function. Focuses on 64-bit platforms, but in JS speed different from Blake2s is negligible.
|
||||||
|
* @module
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
import { BLAKE2b as B2B, blake2b as b2b } from './blake2.ts';
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const BLAKE2b: typeof B2B;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const blake2b: typeof b2b;
|
||||||
|
//# sourceMappingURL=blake2b.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake2b.d.ts","sourceRoot":"","sources":["src/blake2b.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,aAAa,CAAC;AAC7D,+DAA+D;AAC/D,eAAO,MAAM,OAAO,EAAE,OAAO,GAAS,CAAC;AACvC,+DAA+D;AAC/D,eAAO,MAAM,OAAO,EAAE,OAAO,GAAS,CAAC"}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.blake2b = exports.BLAKE2b = void 0;
|
||||||
|
/**
|
||||||
|
* Blake2b hash function. Focuses on 64-bit platforms, but in JS speed different from Blake2s is negligible.
|
||||||
|
* @module
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
const blake2_ts_1 = require("./blake2.js");
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
exports.BLAKE2b = blake2_ts_1.BLAKE2b;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
exports.blake2b = blake2_ts_1.blake2b;
|
||||||
|
//# sourceMappingURL=blake2b.js.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake2b.js","sourceRoot":"","sources":["src/blake2b.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,2CAA6D;AAC7D,+DAA+D;AAClD,QAAA,OAAO,GAAe,mBAAG,CAAC;AACvC,+DAA+D;AAClD,QAAA,OAAO,GAAe,mBAAG,CAAC"}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. blake2b for 64-bit, but in JS it is slower.
|
||||||
|
* @module
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
import { G1s as G1s_n, G2s as G2s_n } from './_blake.ts';
|
||||||
|
import { BLAKE2s as B2S, blake2s as b2s, compress as compress_n } from './blake2.ts';
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const B2S_IV: Uint32Array;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const G1s: typeof G1s_n;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const G2s: typeof G2s_n;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const compress: typeof compress_n;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const BLAKE2s: typeof B2S;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const blake2s: typeof b2s;
|
||||||
|
//# sourceMappingURL=blake2s.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake2s.d.ts","sourceRoot":"","sources":["src/blake2s.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,GAAG,IAAI,KAAK,EAAE,GAAG,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,OAAO,IAAI,GAAG,EAAE,QAAQ,IAAI,UAAU,EAAE,MAAM,aAAa,CAAC;AACrF,+DAA+D;AAC/D,eAAO,MAAM,MAAM,EAAE,WAAuB,CAAC;AAC7C,+DAA+D;AAC/D,eAAO,MAAM,GAAG,EAAE,OAAO,KAAa,CAAC;AACvC,+DAA+D;AAC/D,eAAO,MAAM,GAAG,EAAE,OAAO,KAAa,CAAC;AACvC,+DAA+D;AAC/D,eAAO,MAAM,QAAQ,EAAE,OAAO,UAAuB,CAAC;AACtD,+DAA+D;AAC/D,eAAO,MAAM,OAAO,EAAE,OAAO,GAAS,CAAC;AACvC,+DAA+D;AAC/D,eAAO,MAAM,OAAO,EAAE,OAAO,GAAS,CAAC"}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.blake2s = exports.BLAKE2s = exports.compress = exports.G2s = exports.G1s = exports.B2S_IV = void 0;
|
||||||
|
/**
|
||||||
|
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. blake2b for 64-bit, but in JS it is slower.
|
||||||
|
* @module
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
const _blake_ts_1 = require("./_blake.js");
|
||||||
|
const _md_ts_1 = require("./_md.js");
|
||||||
|
const blake2_ts_1 = require("./blake2.js");
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
exports.B2S_IV = _md_ts_1.SHA256_IV;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
exports.G1s = _blake_ts_1.G1s;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
exports.G2s = _blake_ts_1.G2s;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
exports.compress = blake2_ts_1.compress;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
exports.BLAKE2s = blake2_ts_1.BLAKE2s;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
exports.blake2s = blake2_ts_1.blake2s;
|
||||||
|
//# sourceMappingURL=blake2s.js.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake2s.js","sourceRoot":"","sources":["src/blake2s.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,2CAAyD;AACzD,qCAAqC;AACrC,2CAAqF;AACrF,+DAA+D;AAClD,QAAA,MAAM,GAAgB,kBAAS,CAAC;AAC7C,+DAA+D;AAClD,QAAA,GAAG,GAAiB,eAAK,CAAC;AACvC,+DAA+D;AAClD,QAAA,GAAG,GAAiB,eAAK,CAAC;AACvC,+DAA+D;AAClD,QAAA,QAAQ,GAAsB,oBAAU,CAAC;AACtD,+DAA+D;AAClD,QAAA,OAAO,GAAe,mBAAG,CAAC;AACvC,+DAA+D;AAClD,QAAA,OAAO,GAAe,mBAAG,CAAC"}
|
||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
import { BLAKE2 } from './blake2.ts';
|
||||||
|
import { type CHashXO, type HashXOF, type Input } from './utils.ts';
|
||||||
|
/**
|
||||||
|
* Ensure to use EITHER `key` OR `context`, not both.
|
||||||
|
*
|
||||||
|
* * `key`: 32-byte MAC key.
|
||||||
|
* * `context`: string for KDF. Should be hardcoded, globally unique, and application - specific.
|
||||||
|
* A good default format for the context string is "[application] [commit timestamp] [purpose]".
|
||||||
|
*/
|
||||||
|
export type Blake3Opts = {
|
||||||
|
dkLen?: number;
|
||||||
|
key?: Input;
|
||||||
|
context?: Input;
|
||||||
|
};
|
||||||
|
/** Blake3 hash. Can be used as MAC and KDF. */
|
||||||
|
export declare class BLAKE3 extends BLAKE2<BLAKE3> implements HashXOF<BLAKE3> {
|
||||||
|
private chunkPos;
|
||||||
|
private chunksDone;
|
||||||
|
private flags;
|
||||||
|
private IV;
|
||||||
|
private state;
|
||||||
|
private stack;
|
||||||
|
private posOut;
|
||||||
|
private bufferOut32;
|
||||||
|
private bufferOut;
|
||||||
|
private chunkOut;
|
||||||
|
private enableXOF;
|
||||||
|
constructor(opts?: Blake3Opts, flags?: number);
|
||||||
|
protected get(): [];
|
||||||
|
protected set(): void;
|
||||||
|
private b2Compress;
|
||||||
|
protected compress(buf: Uint32Array, bufPos?: number, isLast?: boolean): void;
|
||||||
|
_cloneInto(to?: BLAKE3): BLAKE3;
|
||||||
|
destroy(): void;
|
||||||
|
private b2CompressOut;
|
||||||
|
protected finish(): void;
|
||||||
|
private writeInto;
|
||||||
|
xofInto(out: Uint8Array): Uint8Array;
|
||||||
|
xof(bytes: number): Uint8Array;
|
||||||
|
digestInto(out: Uint8Array): Uint8Array;
|
||||||
|
digest(): Uint8Array;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* BLAKE3 hash function. Can be used as MAC and KDF.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - `dkLen` for output length, `key` for MAC mode, `context` for KDF mode
|
||||||
|
* @example
|
||||||
|
* const data = new Uint8Array(32);
|
||||||
|
* const hash = blake3(data);
|
||||||
|
* const mac = blake3(data, { key: new Uint8Array(32) });
|
||||||
|
* const kdf = blake3(data, { context: 'application name' });
|
||||||
|
*/
|
||||||
|
export declare const blake3: CHashXO;
|
||||||
|
//# sourceMappingURL=blake3.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake3.d.ts","sourceRoot":"","sources":["src/blake3.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,MAAM,EAAY,MAAM,aAAa,CAAC;AAE/C,OAAO,EAGL,KAAK,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,KAAK,EACvC,MAAM,YAAY,CAAC;AAwBpB;;;;;;GAMG;AACH,MAAM,MAAM,UA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AC"}
|
||||||
+255
@@ -0,0 +1,255 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.blake3 = exports.BLAKE3 = void 0;
|
||||||
|
/**
|
||||||
|
* Blake3 fast hash is Blake2 with reduced security (round count). Can also be used as MAC & KDF.
|
||||||
|
*
|
||||||
|
* It is advertised as "the fastest cryptographic hash". However, it isn't true in JS.
|
||||||
|
* Why is this so slow? While it should be 6x faster than blake2b, perf diff is only 20%:
|
||||||
|
*
|
||||||
|
* * There is only 30% reduction in number of rounds from blake2s
|
||||||
|
* * Speed-up comes from tree structure, which is parallelized using SIMD & threading.
|
||||||
|
* These features are not present in JS, so we only get overhead from trees.
|
||||||
|
* * Parallelization only happens on 1024-byte chunks: there is no benefit for small inputs.
|
||||||
|
* * It is still possible to make it faster using: a) loop unrolling b) web workers c) wasm
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
const _md_ts_1 = require("./_md.js");
|
||||||
|
const _u64_ts_1 = require("./_u64.js");
|
||||||
|
const blake2_ts_1 = require("./blake2.js");
|
||||||
|
// prettier-ignore
|
||||||
|
const utils_ts_1 = require("./utils.js");
|
||||||
|
// Flag bitset
|
||||||
|
const B3_Flags = {
|
||||||
|
CHUNK_START: 0b1,
|
||||||
|
CHUNK_END: 0b10,
|
||||||
|
PARENT: 0b100,
|
||||||
|
ROOT: 0b1000,
|
||||||
|
KEYED_HASH: 0b10000,
|
||||||
|
DERIVE_KEY_CONTEXT: 0b100000,
|
||||||
|
DERIVE_KEY_MATERIAL: 0b1000000,
|
||||||
|
};
|
||||||
|
const B3_IV = _md_ts_1.SHA256_IV.slice();
|
||||||
|
const B3_SIGMA = /* @__PURE__ */ (() => {
|
||||||
|
const Id = Array.from({ length: 16 }, (_, i) => i);
|
||||||
|
const permute = (arr) => [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8].map((i) => arr[i]);
|
||||||
|
const res = [];
|
||||||
|
for (let i = 0, v = Id; i < 7; i++, v = permute(v))
|
||||||
|
res.push(...v);
|
||||||
|
return Uint8Array.from(res);
|
||||||
|
})();
|
||||||
|
/** Blake3 hash. Can be used as MAC and KDF. */
|
||||||
|
class BLAKE3 extends blake2_ts_1.BLAKE2 {
|
||||||
|
constructor(opts = {}, flags = 0) {
|
||||||
|
super(64, opts.dkLen === undefined ? 32 : opts.dkLen);
|
||||||
|
this.chunkPos = 0; // Position of current block in chunk
|
||||||
|
this.chunksDone = 0; // How many chunks we already have
|
||||||
|
this.flags = 0 | 0;
|
||||||
|
this.stack = [];
|
||||||
|
// Output
|
||||||
|
this.posOut = 0;
|
||||||
|
this.bufferOut32 = new Uint32Array(16);
|
||||||
|
this.chunkOut = 0; // index of output chunk
|
||||||
|
this.enableXOF = true;
|
||||||
|
const { key, context } = opts;
|
||||||
|
const hasContext = context !== undefined;
|
||||||
|
if (key !== undefined) {
|
||||||
|
if (hasContext)
|
||||||
|
throw new Error('Only "key" or "context" can be specified at same time');
|
||||||
|
const k = (0, utils_ts_1.toBytes)(key).slice();
|
||||||
|
(0, utils_ts_1.abytes)(k, 32);
|
||||||
|
this.IV = (0, utils_ts_1.u32)(k);
|
||||||
|
(0, utils_ts_1.swap32IfBE)(this.IV);
|
||||||
|
this.flags = flags | B3_Flags.KEYED_HASH;
|
||||||
|
}
|
||||||
|
else if (hasContext) {
|
||||||
|
const ctx = (0, utils_ts_1.toBytes)(context);
|
||||||
|
const contextKey = new BLAKE3({ dkLen: 32 }, B3_Flags.DERIVE_KEY_CONTEXT)
|
||||||
|
.update(ctx)
|
||||||
|
.digest();
|
||||||
|
this.IV = (0, utils_ts_1.u32)(contextKey);
|
||||||
|
(0, utils_ts_1.swap32IfBE)(this.IV);
|
||||||
|
this.flags = flags | B3_Flags.DERIVE_KEY_MATERIAL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.IV = B3_IV.slice();
|
||||||
|
this.flags = flags;
|
||||||
|
}
|
||||||
|
this.state = this.IV.slice();
|
||||||
|
this.bufferOut = (0, utils_ts_1.u8)(this.bufferOut32);
|
||||||
|
}
|
||||||
|
// Unused
|
||||||
|
get() {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
set() { }
|
||||||
|
b2Compress(counter, flags, buf, bufPos = 0) {
|
||||||
|
const { state: s, pos } = this;
|
||||||
|
const { h, l } = (0, _u64_ts_1.fromBig)(BigInt(counter), true);
|
||||||
|
// prettier-ignore
|
||||||
|
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = (0, blake2_ts_1.compress)(B3_SIGMA, bufPos, buf, 7, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], h, l, pos, flags);
|
||||||
|
s[0] = v0 ^ v8;
|
||||||
|
s[1] = v1 ^ v9;
|
||||||
|
s[2] = v2 ^ v10;
|
||||||
|
s[3] = v3 ^ v11;
|
||||||
|
s[4] = v4 ^ v12;
|
||||||
|
s[5] = v5 ^ v13;
|
||||||
|
s[6] = v6 ^ v14;
|
||||||
|
s[7] = v7 ^ v15;
|
||||||
|
}
|
||||||
|
compress(buf, bufPos = 0, isLast = false) {
|
||||||
|
// Compress last block
|
||||||
|
let flags = this.flags;
|
||||||
|
if (!this.chunkPos)
|
||||||
|
flags |= B3_Flags.CHUNK_START;
|
||||||
|
if (this.chunkPos === 15 || isLast)
|
||||||
|
flags |= B3_Flags.CHUNK_END;
|
||||||
|
if (!isLast)
|
||||||
|
this.pos = this.blockLen;
|
||||||
|
this.b2Compress(this.chunksDone, flags, buf, bufPos);
|
||||||
|
this.chunkPos += 1;
|
||||||
|
// If current block is last in chunk (16 blocks), then compress chunks
|
||||||
|
if (this.chunkPos === 16 || isLast) {
|
||||||
|
let chunk = this.state;
|
||||||
|
this.state = this.IV.slice();
|
||||||
|
// If not the last one, compress only when there are trailing zeros in chunk counter
|
||||||
|
// chunks used as binary tree where current stack is path. Zero means current leaf is finished and can be compressed.
|
||||||
|
// 1 (001) - leaf not finished (just push current chunk to stack)
|
||||||
|
// 2 (010) - leaf finished at depth=1 (merge with last elm on stack and push back)
|
||||||
|
// 3 (011) - last leaf not finished
|
||||||
|
// 4 (100) - leafs finished at depth=1 and depth=2
|
||||||
|
for (let last, chunks = this.chunksDone + 1; isLast || !(chunks & 1); chunks >>= 1) {
|
||||||
|
if (!(last = this.stack.pop()))
|
||||||
|
break;
|
||||||
|
this.buffer32.set(last, 0);
|
||||||
|
this.buffer32.set(chunk, 8);
|
||||||
|
this.pos = this.blockLen;
|
||||||
|
this.b2Compress(0, this.flags | B3_Flags.PARENT, this.buffer32, 0);
|
||||||
|
chunk = this.state;
|
||||||
|
this.state = this.IV.slice();
|
||||||
|
}
|
||||||
|
this.chunksDone++;
|
||||||
|
this.chunkPos = 0;
|
||||||
|
this.stack.push(chunk);
|
||||||
|
}
|
||||||
|
this.pos = 0;
|
||||||
|
}
|
||||||
|
_cloneInto(to) {
|
||||||
|
to = super._cloneInto(to);
|
||||||
|
const { IV, flags, state, chunkPos, posOut, chunkOut, stack, chunksDone } = this;
|
||||||
|
to.state.set(state.slice());
|
||||||
|
to.stack = stack.map((i) => Uint32Array.from(i));
|
||||||
|
to.IV.set(IV);
|
||||||
|
to.flags = flags;
|
||||||
|
to.chunkPos = chunkPos;
|
||||||
|
to.chunksDone = chunksDone;
|
||||||
|
to.posOut = posOut;
|
||||||
|
to.chunkOut = chunkOut;
|
||||||
|
to.enableXOF = this.enableXOF;
|
||||||
|
to.bufferOut32.set(this.bufferOut32);
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
this.destroyed = true;
|
||||||
|
(0, utils_ts_1.clean)(this.state, this.buffer32, this.IV, this.bufferOut32);
|
||||||
|
(0, utils_ts_1.clean)(...this.stack);
|
||||||
|
}
|
||||||
|
// Same as b2Compress, but doesn't modify state and returns 16 u32 array (instead of 8)
|
||||||
|
b2CompressOut() {
|
||||||
|
const { state: s, pos, flags, buffer32, bufferOut32: out32 } = this;
|
||||||
|
const { h, l } = (0, _u64_ts_1.fromBig)(BigInt(this.chunkOut++));
|
||||||
|
(0, utils_ts_1.swap32IfBE)(buffer32);
|
||||||
|
// prettier-ignore
|
||||||
|
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = (0, blake2_ts_1.compress)(B3_SIGMA, 0, buffer32, 7, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], l, h, pos, flags);
|
||||||
|
out32[0] = v0 ^ v8;
|
||||||
|
out32[1] = v1 ^ v9;
|
||||||
|
out32[2] = v2 ^ v10;
|
||||||
|
out32[3] = v3 ^ v11;
|
||||||
|
out32[4] = v4 ^ v12;
|
||||||
|
out32[5] = v5 ^ v13;
|
||||||
|
out32[6] = v6 ^ v14;
|
||||||
|
out32[7] = v7 ^ v15;
|
||||||
|
out32[8] = s[0] ^ v8;
|
||||||
|
out32[9] = s[1] ^ v9;
|
||||||
|
out32[10] = s[2] ^ v10;
|
||||||
|
out32[11] = s[3] ^ v11;
|
||||||
|
out32[12] = s[4] ^ v12;
|
||||||
|
out32[13] = s[5] ^ v13;
|
||||||
|
out32[14] = s[6] ^ v14;
|
||||||
|
out32[15] = s[7] ^ v15;
|
||||||
|
(0, utils_ts_1.swap32IfBE)(buffer32);
|
||||||
|
(0, utils_ts_1.swap32IfBE)(out32);
|
||||||
|
this.posOut = 0;
|
||||||
|
}
|
||||||
|
finish() {
|
||||||
|
if (this.finished)
|
||||||
|
return;
|
||||||
|
this.finished = true;
|
||||||
|
// Padding
|
||||||
|
(0, utils_ts_1.clean)(this.buffer.subarray(this.pos));
|
||||||
|
// Process last chunk
|
||||||
|
let flags = this.flags | B3_Flags.ROOT;
|
||||||
|
if (this.stack.length) {
|
||||||
|
flags |= B3_Flags.PARENT;
|
||||||
|
(0, utils_ts_1.swap32IfBE)(this.buffer32);
|
||||||
|
this.compress(this.buffer32, 0, true);
|
||||||
|
(0, utils_ts_1.swap32IfBE)(this.buffer32);
|
||||||
|
this.chunksDone = 0;
|
||||||
|
this.pos = this.blockLen;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
flags |= (!this.chunkPos ? B3_Flags.CHUNK_START : 0) | B3_Flags.CHUNK_END;
|
||||||
|
}
|
||||||
|
this.flags = flags;
|
||||||
|
this.b2CompressOut();
|
||||||
|
}
|
||||||
|
writeInto(out) {
|
||||||
|
(0, utils_ts_1.aexists)(this, false);
|
||||||
|
(0, utils_ts_1.abytes)(out);
|
||||||
|
this.finish();
|
||||||
|
const { blockLen, bufferOut } = this;
|
||||||
|
for (let pos = 0, len = out.length; pos < len;) {
|
||||||
|
if (this.posOut >= blockLen)
|
||||||
|
this.b2CompressOut();
|
||||||
|
const take = Math.min(blockLen - this.posOut, len - pos);
|
||||||
|
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
||||||
|
this.posOut += take;
|
||||||
|
pos += take;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
xofInto(out) {
|
||||||
|
if (!this.enableXOF)
|
||||||
|
throw new Error('XOF is not possible after digest call');
|
||||||
|
return this.writeInto(out);
|
||||||
|
}
|
||||||
|
xof(bytes) {
|
||||||
|
(0, utils_ts_1.anumber)(bytes);
|
||||||
|
return this.xofInto(new Uint8Array(bytes));
|
||||||
|
}
|
||||||
|
digestInto(out) {
|
||||||
|
(0, utils_ts_1.aoutput)(out, this);
|
||||||
|
if (this.finished)
|
||||||
|
throw new Error('digest() was already called');
|
||||||
|
this.enableXOF = false;
|
||||||
|
this.writeInto(out);
|
||||||
|
this.destroy();
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
digest() {
|
||||||
|
return this.digestInto(new Uint8Array(this.outputLen));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.BLAKE3 = BLAKE3;
|
||||||
|
/**
|
||||||
|
* BLAKE3 hash function. Can be used as MAC and KDF.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - `dkLen` for output length, `key` for MAC mode, `context` for KDF mode
|
||||||
|
* @example
|
||||||
|
* const data = new Uint8Array(32);
|
||||||
|
* const hash = blake3(data);
|
||||||
|
* const mac = blake3(data, { key: new Uint8Array(32) });
|
||||||
|
* const kdf = blake3(data, { context: 'application name' });
|
||||||
|
*/
|
||||||
|
exports.blake3 = (0, utils_ts_1.createXOFer)((opts) => new BLAKE3(opts));
|
||||||
|
//# sourceMappingURL=blake3.js.map
|
||||||
+1
File diff suppressed because one or more lines are too long
+2
@@ -0,0 +1,2 @@
|
|||||||
|
export declare const crypto: any;
|
||||||
|
//# sourceMappingURL=crypto.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["src/crypto.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,MAAM,EAAE,GACqE,CAAC"}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.crypto = void 0;
|
||||||
|
exports.crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
|
||||||
|
//# sourceMappingURL=crypto.js.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["src/crypto.ts"],"names":[],"mappings":";;;AAOa,QAAA,MAAM,GACjB,OAAO,UAAU,KAAK,QAAQ,IAAI,QAAQ,IAAI,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC"}
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
export declare const crypto: any;
|
||||||
|
//# sourceMappingURL=cryptoNode.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"cryptoNode.d.ts","sourceRoot":"","sources":["src/cryptoNode.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,MAAM,EAAE,GAKJ,CAAC"}
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.crypto = void 0;
|
||||||
|
/**
|
||||||
|
* Internal webcrypto alias.
|
||||||
|
* We prefer WebCrypto aka globalThis.crypto, which exists in node.js 16+.
|
||||||
|
* Falls back to Node.js built-in crypto for Node.js <=v14.
|
||||||
|
* See utils.ts for details.
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
// @ts-ignore
|
||||||
|
const nc = require("node:crypto");
|
||||||
|
exports.crypto = nc && typeof nc === 'object' && 'webcrypto' in nc
|
||||||
|
? nc.webcrypto
|
||||||
|
: nc && typeof nc === 'object' && 'randomBytes' in nc
|
||||||
|
? nc
|
||||||
|
: undefined;
|
||||||
|
//# sourceMappingURL=cryptoNode.js.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"cryptoNode.js","sourceRoot":"","sources":["src/cryptoNode.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,aAAa;AACb,kCAAkC;AACrB,QAAA,MAAM,GACjB,EAAE,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,WAAW,IAAI,EAAE;IAC/C,CAAC,CAAE,EAAE,CAAC,SAAiB;IACvB,CAAC,CAAC,EAAE,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,aAAa,IAAI,EAAE;QACnD,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,SAAS,CAAC"}
|
||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
export declare function scrypt(password: string, salt: string): Uint8Array;
|
||||||
|
export declare function pbkdf2(password: string, salt: string): Uint8Array;
|
||||||
|
/**
|
||||||
|
* Derives main seed. Takes a lot of time. Prefer `eskdf` method instead.
|
||||||
|
*/
|
||||||
|
export declare function deriveMainSeed(username: string, password: string): Uint8Array;
|
||||||
|
type AccountID = number | string;
|
||||||
|
type OptsLength = {
|
||||||
|
keyLength: number;
|
||||||
|
};
|
||||||
|
type OptsMod = {
|
||||||
|
modulus: bigint;
|
||||||
|
};
|
||||||
|
type KeyOpts = undefined | OptsLength | OptsMod;
|
||||||
|
export interface ESKDF {
|
||||||
|
/**
|
||||||
|
* Derives a child key. Child key will not be associated with any
|
||||||
|
* other child key because of properties of underlying KDF.
|
||||||
|
*
|
||||||
|
* @param protocol - 3-15 character protocol name
|
||||||
|
* @param accountId - numeric identifier of account
|
||||||
|
* @param options - `keyLength: 64` or `modulus: 41920438n`
|
||||||
|
* @example deriveChildKey('aes', 0)
|
||||||
|
*/
|
||||||
|
deriveChildKey: (protocol: string, accountId: AccountID, options?: KeyOpts) => Uint8Array;
|
||||||
|
/**
|
||||||
|
* Deletes the main seed from eskdf instance
|
||||||
|
*/
|
||||||
|
expire: () => void;
|
||||||
|
/**
|
||||||
|
* Account fingerprint
|
||||||
|
*/
|
||||||
|
fingerprint: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* ESKDF
|
||||||
|
* @param username - username, email, or identifier, min: 8 characters, should have enough entropy
|
||||||
|
* @param password - password, min: 8 characters, should have enough entropy
|
||||||
|
* @example
|
||||||
|
* const kdf = await eskdf('example-university', 'beginning-new-example');
|
||||||
|
* const key = kdf.deriveChildKey('aes', 0);
|
||||||
|
* console.log(kdf.fingerprint);
|
||||||
|
* kdf.expire();
|
||||||
|
*/
|
||||||
|
export declare function eskdf(username: string, password: string): Promise<ESKDF>;
|
||||||
|
export {};
|
||||||
|
//# sourceMappingURL=eskdf.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"eskdf.d.ts","sourceRoot":"","sources":["src/eskdf.ts"],"names":[],"mappings":"AAiBA,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAEjE;AAGD,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAEjE;AAiBD;;GAEG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,CAY7E;AAED,KAAK,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;AAgCjC,KAAK,UAAU,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC;AACxC,KAAK,OAAO,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AACnC,KAAK,OAAO,GAAG,SAAS,GAAG,UAAU,GAAG,OAAO,CAAC;AAwChD,MAAM,WAAW,KAAK;IACpB;;;;;;;;OAQG;IACH,cAAc,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,UAAU,CAAC;IAC1F;;OAEG;IACH,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;GASG;AACH,wBAAsB,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,CAuB9E"}
|
||||||
+166
@@ -0,0 +1,166 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.scrypt = scrypt;
|
||||||
|
exports.pbkdf2 = pbkdf2;
|
||||||
|
exports.deriveMainSeed = deriveMainSeed;
|
||||||
|
exports.eskdf = eskdf;
|
||||||
|
/**
|
||||||
|
* Experimental KDF for AES.
|
||||||
|
*/
|
||||||
|
const hkdf_ts_1 = require("./hkdf.js");
|
||||||
|
const pbkdf2_ts_1 = require("./pbkdf2.js");
|
||||||
|
const scrypt_ts_1 = require("./scrypt.js");
|
||||||
|
const sha256_ts_1 = require("./sha256.js");
|
||||||
|
const utils_ts_1 = require("./utils.js");
|
||||||
|
// A tiny KDF for various applications like AES key-gen.
|
||||||
|
// Uses HKDF in a non-standard way, so it's not "KDF-secure", only "PRF-secure".
|
||||||
|
// Which is good enough: assume sha2-256 retained preimage resistance.
|
||||||
|
const SCRYPT_FACTOR = 2 ** 19;
|
||||||
|
const PBKDF2_FACTOR = 2 ** 17;
|
||||||
|
// Scrypt KDF
|
||||||
|
function scrypt(password, salt) {
|
||||||
|
return (0, scrypt_ts_1.scrypt)(password, salt, { N: SCRYPT_FACTOR, r: 8, p: 1, dkLen: 32 });
|
||||||
|
}
|
||||||
|
// PBKDF2-HMAC-SHA256
|
||||||
|
function pbkdf2(password, salt) {
|
||||||
|
return (0, pbkdf2_ts_1.pbkdf2)(sha256_ts_1.sha256, password, salt, { c: PBKDF2_FACTOR, dkLen: 32 });
|
||||||
|
}
|
||||||
|
// Combines two 32-byte byte arrays
|
||||||
|
function xor32(a, b) {
|
||||||
|
(0, utils_ts_1.abytes)(a, 32);
|
||||||
|
(0, utils_ts_1.abytes)(b, 32);
|
||||||
|
const arr = new Uint8Array(32);
|
||||||
|
for (let i = 0; i < 32; i++) {
|
||||||
|
arr[i] = a[i] ^ b[i];
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
function strHasLength(str, min, max) {
|
||||||
|
return typeof str === 'string' && str.length >= min && str.length <= max;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Derives main seed. Takes a lot of time. Prefer `eskdf` method instead.
|
||||||
|
*/
|
||||||
|
function deriveMainSeed(username, password) {
|
||||||
|
if (!strHasLength(username, 8, 255))
|
||||||
|
throw new Error('invalid username');
|
||||||
|
if (!strHasLength(password, 8, 255))
|
||||||
|
throw new Error('invalid password');
|
||||||
|
// Declared like this to throw off minifiers which auto-convert .fromCharCode(1) to actual string.
|
||||||
|
// String with non-ascii may be problematic in some envs
|
||||||
|
const codes = { _1: 1, _2: 2 };
|
||||||
|
const sep = { s: String.fromCharCode(codes._1), p: String.fromCharCode(codes._2) };
|
||||||
|
const scr = scrypt(password + sep.s, username + sep.s);
|
||||||
|
const pbk = pbkdf2(password + sep.p, username + sep.p);
|
||||||
|
const res = xor32(scr, pbk);
|
||||||
|
(0, utils_ts_1.clean)(scr, pbk);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Converts protocol & accountId pair to HKDF salt & info params.
|
||||||
|
*/
|
||||||
|
function getSaltInfo(protocol, accountId = 0) {
|
||||||
|
// Note that length here also repeats two lines below
|
||||||
|
// We do an additional length check here to reduce the scope of DoS attacks
|
||||||
|
if (!(strHasLength(protocol, 3, 15) && /^[a-z0-9]{3,15}$/.test(protocol))) {
|
||||||
|
throw new Error('invalid protocol');
|
||||||
|
}
|
||||||
|
// Allow string account ids for some protocols
|
||||||
|
const allowsStr = /^password\d{0,3}|ssh|tor|file$/.test(protocol);
|
||||||
|
let salt; // Extract salt. Default is undefined.
|
||||||
|
if (typeof accountId === 'string') {
|
||||||
|
if (!allowsStr)
|
||||||
|
throw new Error('accountId must be a number');
|
||||||
|
if (!strHasLength(accountId, 1, 255))
|
||||||
|
throw new Error('accountId must be string of length 1..255');
|
||||||
|
salt = (0, utils_ts_1.kdfInputToBytes)(accountId);
|
||||||
|
}
|
||||||
|
else if (Number.isSafeInteger(accountId)) {
|
||||||
|
if (accountId < 0 || accountId > Math.pow(2, 32) - 1)
|
||||||
|
throw new Error('invalid accountId');
|
||||||
|
// Convert to Big Endian Uint32
|
||||||
|
salt = new Uint8Array(4);
|
||||||
|
(0, utils_ts_1.createView)(salt).setUint32(0, accountId, false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new Error('accountId must be a number' + (allowsStr ? ' or string' : ''));
|
||||||
|
}
|
||||||
|
const info = (0, utils_ts_1.kdfInputToBytes)(protocol);
|
||||||
|
return { salt, info };
|
||||||
|
}
|
||||||
|
function countBytes(num) {
|
||||||
|
if (typeof num !== 'bigint' || num <= BigInt(128))
|
||||||
|
throw new Error('invalid number');
|
||||||
|
return Math.ceil(num.toString(2).length / 8);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Parses keyLength and modulus options to extract length of result key.
|
||||||
|
* If modulus is used, adds 64 bits to it as per FIPS 186 B.4.1 to combat modulo bias.
|
||||||
|
*/
|
||||||
|
function getKeyLength(options) {
|
||||||
|
if (!options || typeof options !== 'object')
|
||||||
|
return 32;
|
||||||
|
const hasLen = 'keyLength' in options;
|
||||||
|
const hasMod = 'modulus' in options;
|
||||||
|
if (hasLen && hasMod)
|
||||||
|
throw new Error('cannot combine keyLength and modulus options');
|
||||||
|
if (!hasLen && !hasMod)
|
||||||
|
throw new Error('must have either keyLength or modulus option');
|
||||||
|
// FIPS 186 B.4.1 requires at least 64 more bits
|
||||||
|
const l = hasMod ? countBytes(options.modulus) + 8 : options.keyLength;
|
||||||
|
if (!(typeof l === 'number' && l >= 16 && l <= 8192))
|
||||||
|
throw new Error('invalid keyLength');
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Converts key to bigint and divides it by modulus. Big Endian.
|
||||||
|
* Implements FIPS 186 B.4.1, which removes 0 and modulo bias from output.
|
||||||
|
*/
|
||||||
|
function modReduceKey(key, modulus) {
|
||||||
|
const _1 = BigInt(1);
|
||||||
|
const num = BigInt('0x' + (0, utils_ts_1.bytesToHex)(key)); // check for ui8a, then bytesToNumber()
|
||||||
|
const res = (num % (modulus - _1)) + _1; // Remove 0 from output
|
||||||
|
if (res < _1)
|
||||||
|
throw new Error('expected positive number'); // Guard against bad values
|
||||||
|
const len = key.length - 8; // FIPS requires 64 more bits = 8 bytes
|
||||||
|
const hex = res.toString(16).padStart(len * 2, '0'); // numberToHex()
|
||||||
|
const bytes = (0, utils_ts_1.hexToBytes)(hex);
|
||||||
|
if (bytes.length !== len)
|
||||||
|
throw new Error('invalid length of result key');
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* ESKDF
|
||||||
|
* @param username - username, email, or identifier, min: 8 characters, should have enough entropy
|
||||||
|
* @param password - password, min: 8 characters, should have enough entropy
|
||||||
|
* @example
|
||||||
|
* const kdf = await eskdf('example-university', 'beginning-new-example');
|
||||||
|
* const key = kdf.deriveChildKey('aes', 0);
|
||||||
|
* console.log(kdf.fingerprint);
|
||||||
|
* kdf.expire();
|
||||||
|
*/
|
||||||
|
async function eskdf(username, password) {
|
||||||
|
// We are using closure + object instead of class because
|
||||||
|
// we want to make `seed` non-accessible for any external function.
|
||||||
|
let seed = deriveMainSeed(username, password);
|
||||||
|
function deriveCK(protocol, accountId = 0, options) {
|
||||||
|
(0, utils_ts_1.abytes)(seed, 32);
|
||||||
|
const { salt, info } = getSaltInfo(protocol, accountId); // validate protocol & accountId
|
||||||
|
const keyLength = getKeyLength(options); // validate options
|
||||||
|
const key = (0, hkdf_ts_1.hkdf)(sha256_ts_1.sha256, seed, salt, info, keyLength);
|
||||||
|
// Modulus has already been validated
|
||||||
|
return options && 'modulus' in options ? modReduceKey(key, options.modulus) : key;
|
||||||
|
}
|
||||||
|
function expire() {
|
||||||
|
if (seed)
|
||||||
|
seed.fill(1);
|
||||||
|
seed = undefined;
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
const fingerprint = Array.from(deriveCK('fingerprint', 0))
|
||||||
|
.slice(0, 6)
|
||||||
|
.map((char) => char.toString(16).padStart(2, '0').toUpperCase())
|
||||||
|
.join(':');
|
||||||
|
return Object.freeze({ deriveChildKey: deriveCK, expire, fingerprint });
|
||||||
|
}
|
||||||
|
//# sourceMappingURL=eskdf.js.map
|
||||||
+1
File diff suppressed because one or more lines are too long
+32
@@ -0,0 +1,32 @@
|
|||||||
|
import { type KDFInput } from './utils.ts';
|
||||||
|
/**
|
||||||
|
* Argon2 options.
|
||||||
|
* * t: time cost, m: mem cost in kb, p: parallelization.
|
||||||
|
* * key: optional key. personalization: arbitrary extra data.
|
||||||
|
* * dkLen: desired number of output bytes.
|
||||||
|
*/
|
||||||
|
export type ArgonOpts = {
|
||||||
|
t: number;
|
||||||
|
m: number;
|
||||||
|
p: number;
|
||||||
|
version?: number;
|
||||||
|
key?: KDFInput;
|
||||||
|
personalization?: KDFInput;
|
||||||
|
dkLen?: number;
|
||||||
|
asyncTick?: number;
|
||||||
|
maxmem?: number;
|
||||||
|
onProgress?: (progress: number) => void;
|
||||||
|
};
|
||||||
|
/** argon2d GPU-resistant version. */
|
||||||
|
export declare const argon2d: (password: KDFInput, salt: KDFInput, opts: ArgonOpts) => Uint8Array;
|
||||||
|
/** argon2i side-channel-resistant version. */
|
||||||
|
export declare const argon2i: (password: KDFInput, salt: KDFInput, opts: ArgonOpts) => Uint8Array;
|
||||||
|
/** argon2id, combining i+d, the most popular version from RFC 9106 */
|
||||||
|
export declare const argon2id: (password: KDFInput, salt: KDFInput, opts: ArgonOpts) => Uint8Array;
|
||||||
|
/** argon2d async GPU-resistant version. */
|
||||||
|
export declare const argon2dAsync: (password: KDFInput, salt: KDFInput, opts: ArgonOpts) => Promise<Uint8Array>;
|
||||||
|
/** argon2i async side-channel-resistant version. */
|
||||||
|
export declare const argon2iAsync: (password: KDFInput, salt: KDFInput, opts: ArgonOpts) => Promise<Uint8Array>;
|
||||||
|
/** argon2id async, combining i+d, the most popular version from RFC 9106 */
|
||||||
|
export declare const argon2idAsync: (password: KDFInput, salt: KDFInput, opts: ArgonOpts) => Promise<Uint8Array>;
|
||||||
|
//# sourceMappingURL=argon2.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"argon2.d.ts","sourceRoot":"","sources":["../src/argon2.ts"],"names":[],"mappings":"AAYA,OAAO,EAAqD,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAkK9F;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,QAAQ,CAAC;IACf,eAAe,CAAC,EAAE,QAAQ,CAAC;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC,CAAC;AAuNF,qCAAqC;AACrC,eAAO,MAAM,OAAO,GAAI,UAAU,QAAQ,EAAE,MAAM,QAAQ,EAAE,MAAM,SAAS,KAAG,UACnC,CAAC;AAC5C,8CAA8C;AAC9C,eAAO,MAAM,OAAO,GAAI,UAAU,QAAQ,EAAE,MAAM,QAAQ,EAAE,MAAM,SAAS,KAAG,UACpC,CAAC;AAC3C,sEAAsE;AACtE,eAAO,MAAM,QAAQ,GAAI,UAAU,QAAQ,EAAE,MAAM,QAAQ,EAAE,MAAM,SAAS,KAAG,UACpC,CAAC;AAiE5C,2CAA2C;AAC3C,eAAO,MAAM,YAAY,GACvB,UAAU,QAAQ,EAClB,MAAM,QAAQ,EACd,MAAM,SAAS,KACd,OAAO,CAAC,UAAU,CAAmD,CAAC;AACzE,oDAAoD;AACpD,eAAO,MAAM,YAAY,GACvB,UAAU,QAAQ,EAClB,MAAM,QAAQ,EACd,MAAM,SAAS,KACd,OAAO,CAAC,UAAU,CAAkD,CAAC;AACxE,4EAA4E;AAC5E,eAAO,MAAM,aAAa,GACxB,UAAU,QAAQ,EAClB,MAAM,QAAQ,EACd,MAAM,SAAS,KACd,OAAO,CAAC,UAAU,CAAmD,CAAC"}
|
||||||
+392
@@ -0,0 +1,392 @@
|
|||||||
|
/**
|
||||||
|
* Argon2 KDF from RFC 9106. Can be used to create a key from password and salt.
|
||||||
|
* We suggest to use Scrypt. JS Argon is 2-10x slower than native code because of 64-bitness:
|
||||||
|
* * argon uses uint64, but JS doesn't have fast uint64array
|
||||||
|
* * uint64 multiplication is 1/3 of time
|
||||||
|
* * `P` function would be very nice with u64, because most of value will be in registers,
|
||||||
|
* hovewer with u32 it will require 32 registers, which is too much.
|
||||||
|
* * JS arrays do slow bound checks, so reading from `A2_BUF` slows it down
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
import { add3H, add3L, rotr32H, rotr32L, rotrBH, rotrBL, rotrSH, rotrSL } from "./_u64.js";
|
||||||
|
import { blake2b } from "./blake2.js";
|
||||||
|
import { abytes, clean, kdfInputToBytes, nextTick, u32, u8 } from "./utils.js";
|
||||||
|
const AT = { Argond2d: 0, Argon2i: 1, Argon2id: 2 };
|
||||||
|
const ARGON2_SYNC_POINTS = 4;
|
||||||
|
const abytesOrZero = (buf) => {
|
||||||
|
if (buf === undefined)
|
||||||
|
return Uint8Array.of();
|
||||||
|
return kdfInputToBytes(buf);
|
||||||
|
};
|
||||||
|
// u32 * u32 = u64
|
||||||
|
function mul(a, b) {
|
||||||
|
const aL = a & 0xffff;
|
||||||
|
const aH = a >>> 16;
|
||||||
|
const bL = b & 0xffff;
|
||||||
|
const bH = b >>> 16;
|
||||||
|
const ll = Math.imul(aL, bL);
|
||||||
|
const hl = Math.imul(aH, bL);
|
||||||
|
const lh = Math.imul(aL, bH);
|
||||||
|
const hh = Math.imul(aH, bH);
|
||||||
|
const carry = (ll >>> 16) + (hl & 0xffff) + lh;
|
||||||
|
const high = (hh + (hl >>> 16) + (carry >>> 16)) | 0;
|
||||||
|
const low = (carry << 16) | (ll & 0xffff);
|
||||||
|
return { h: high, l: low };
|
||||||
|
}
|
||||||
|
function mul2(a, b) {
|
||||||
|
// 2 * a * b (via shifts)
|
||||||
|
const { h, l } = mul(a, b);
|
||||||
|
return { h: ((h << 1) | (l >>> 31)) & 4294967295, l: (l << 1) & 4294967295 };
|
||||||
|
}
|
||||||
|
// BlaMka permutation for Argon2
|
||||||
|
// A + B + (2 * u32(A) * u32(B))
|
||||||
|
function blamka(Ah, Al, Bh, Bl) {
|
||||||
|
const { h: Ch, l: Cl } = mul2(Al, Bl);
|
||||||
|
// A + B + (2 * A * B)
|
||||||
|
const Rll = add3L(Al, Bl, Cl);
|
||||||
|
return { h: add3H(Rll, Ah, Bh, Ch), l: Rll | 0 };
|
||||||
|
}
|
||||||
|
// Temporary block buffer
|
||||||
|
const A2_BUF = new Uint32Array(256); // 1024 bytes (matrix 16x16)
|
||||||
|
function G(a, b, c, d) {
|
||||||
|
let Al = A2_BUF[2 * a], Ah = A2_BUF[2 * a + 1]; // prettier-ignore
|
||||||
|
let Bl = A2_BUF[2 * b], Bh = A2_BUF[2 * b + 1]; // prettier-ignore
|
||||||
|
let Cl = A2_BUF[2 * c], Ch = A2_BUF[2 * c + 1]; // prettier-ignore
|
||||||
|
let Dl = A2_BUF[2 * d], Dh = A2_BUF[2 * d + 1]; // prettier-ignore
|
||||||
|
({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: rotr32H(Dh, Dl), Dl: rotr32L(Dh, Dl) });
|
||||||
|
({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: rotrSH(Bh, Bl, 24), Bl: rotrSL(Bh, Bl, 24) });
|
||||||
|
({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: rotrSH(Dh, Dl, 16), Dl: rotrSL(Dh, Dl, 16) });
|
||||||
|
({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: rotrBH(Bh, Bl, 63), Bl: rotrBL(Bh, Bl, 63) });
|
||||||
|
(A2_BUF[2 * a] = Al), (A2_BUF[2 * a + 1] = Ah);
|
||||||
|
(A2_BUF[2 * b] = Bl), (A2_BUF[2 * b + 1] = Bh);
|
||||||
|
(A2_BUF[2 * c] = Cl), (A2_BUF[2 * c + 1] = Ch);
|
||||||
|
(A2_BUF[2 * d] = Dl), (A2_BUF[2 * d + 1] = Dh);
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
function P(v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11, v12, v13, v14, v15) {
|
||||||
|
G(v00, v04, v08, v12);
|
||||||
|
G(v01, v05, v09, v13);
|
||||||
|
G(v02, v06, v10, v14);
|
||||||
|
G(v03, v07, v11, v15);
|
||||||
|
G(v00, v05, v10, v15);
|
||||||
|
G(v01, v06, v11, v12);
|
||||||
|
G(v02, v07, v08, v13);
|
||||||
|
G(v03, v04, v09, v14);
|
||||||
|
}
|
||||||
|
function block(x, xPos, yPos, outPos, needXor) {
|
||||||
|
for (let i = 0; i < 256; i++)
|
||||||
|
A2_BUF[i] = x[xPos + i] ^ x[yPos + i];
|
||||||
|
// columns (8)
|
||||||
|
for (let i = 0; i < 128; i += 16) {
|
||||||
|
// prettier-ignore
|
||||||
|
P(i, i + 1, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7, i + 8, i + 9, i + 10, i + 11, i + 12, i + 13, i + 14, i + 15);
|
||||||
|
}
|
||||||
|
// rows (8)
|
||||||
|
for (let i = 0; i < 16; i += 2) {
|
||||||
|
// prettier-ignore
|
||||||
|
P(i, i + 1, i + 16, i + 17, i + 32, i + 33, i + 48, i + 49, i + 64, i + 65, i + 80, i + 81, i + 96, i + 97, i + 112, i + 113);
|
||||||
|
}
|
||||||
|
if (needXor)
|
||||||
|
for (let i = 0; i < 256; i++)
|
||||||
|
x[outPos + i] ^= A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
|
||||||
|
else
|
||||||
|
for (let i = 0; i < 256; i++)
|
||||||
|
x[outPos + i] = A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
|
||||||
|
clean(A2_BUF);
|
||||||
|
}
|
||||||
|
// Variable-Length Hash Function H'
|
||||||
|
function Hp(A, dkLen) {
|
||||||
|
const A8 = u8(A);
|
||||||
|
const T = new Uint32Array(1);
|
||||||
|
const T8 = u8(T);
|
||||||
|
T[0] = dkLen;
|
||||||
|
// Fast path
|
||||||
|
if (dkLen <= 64)
|
||||||
|
return blake2b.create({ dkLen }).update(T8).update(A8).digest();
|
||||||
|
const out = new Uint8Array(dkLen);
|
||||||
|
let V = blake2b.create({}).update(T8).update(A8).digest();
|
||||||
|
let pos = 0;
|
||||||
|
// First block
|
||||||
|
out.set(V.subarray(0, 32));
|
||||||
|
pos += 32;
|
||||||
|
// Rest blocks
|
||||||
|
for (; dkLen - pos > 64; pos += 32) {
|
||||||
|
const Vh = blake2b.create({}).update(V);
|
||||||
|
Vh.digestInto(V);
|
||||||
|
Vh.destroy();
|
||||||
|
out.set(V.subarray(0, 32), pos);
|
||||||
|
}
|
||||||
|
// Last block
|
||||||
|
out.set(blake2b(V, { dkLen: dkLen - pos }), pos);
|
||||||
|
clean(V, T);
|
||||||
|
return u32(out);
|
||||||
|
}
|
||||||
|
// Used only inside process block!
|
||||||
|
function indexAlpha(r, s, laneLen, segmentLen, index, randL, sameLane = false) {
|
||||||
|
// This is ugly, but close enough to reference implementation.
|
||||||
|
let area;
|
||||||
|
if (r === 0) {
|
||||||
|
if (s === 0)
|
||||||
|
area = index - 1;
|
||||||
|
else if (sameLane)
|
||||||
|
area = s * segmentLen + index - 1;
|
||||||
|
else
|
||||||
|
area = s * segmentLen + (index == 0 ? -1 : 0);
|
||||||
|
}
|
||||||
|
else if (sameLane)
|
||||||
|
area = laneLen - segmentLen + index - 1;
|
||||||
|
else
|
||||||
|
area = laneLen - segmentLen + (index == 0 ? -1 : 0);
|
||||||
|
const startPos = r !== 0 && s !== ARGON2_SYNC_POINTS - 1 ? (s + 1) * segmentLen : 0;
|
||||||
|
const rel = area - 1 - mul(area, mul(randL, randL).h).h;
|
||||||
|
return (startPos + rel) % laneLen;
|
||||||
|
}
|
||||||
|
const maxUint32 = Math.pow(2, 32);
|
||||||
|
function isU32(num) {
|
||||||
|
return Number.isSafeInteger(num) && num >= 0 && num < maxUint32;
|
||||||
|
}
|
||||||
|
function argon2Opts(opts) {
|
||||||
|
const merged = {
|
||||||
|
version: 0x13,
|
||||||
|
dkLen: 32,
|
||||||
|
maxmem: maxUint32 - 1,
|
||||||
|
asyncTick: 10,
|
||||||
|
};
|
||||||
|
for (let [k, v] of Object.entries(opts))
|
||||||
|
if (v != null)
|
||||||
|
merged[k] = v;
|
||||||
|
const { dkLen, p, m, t, version, onProgress } = merged;
|
||||||
|
if (!isU32(dkLen) || dkLen < 4)
|
||||||
|
throw new Error('dkLen should be at least 4 bytes');
|
||||||
|
if (!isU32(p) || p < 1 || p >= Math.pow(2, 24))
|
||||||
|
throw new Error('p should be 1 <= p < 2^24');
|
||||||
|
if (!isU32(m))
|
||||||
|
throw new Error('m should be 0 <= m < 2^32');
|
||||||
|
if (!isU32(t) || t < 1)
|
||||||
|
throw new Error('t (iterations) should be 1 <= t < 2^32');
|
||||||
|
if (onProgress !== undefined && typeof onProgress !== 'function')
|
||||||
|
throw new Error('progressCb should be function');
|
||||||
|
/*
|
||||||
|
Memory size m MUST be an integer number of kibibytes from 8*p to 2^(32)-1. The actual number of blocks is m', which is m rounded down to the nearest multiple of 4*p.
|
||||||
|
*/
|
||||||
|
if (!isU32(m) || m < 8 * p)
|
||||||
|
throw new Error('memory should be at least 8*p bytes');
|
||||||
|
if (version !== 0x10 && version !== 0x13)
|
||||||
|
throw new Error('unknown version=' + version);
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
function argon2Init(password, salt, type, opts) {
|
||||||
|
password = kdfInputToBytes(password);
|
||||||
|
salt = kdfInputToBytes(salt);
|
||||||
|
abytes(password);
|
||||||
|
abytes(salt);
|
||||||
|
if (!isU32(password.length))
|
||||||
|
throw new Error('password should be less than 4 GB');
|
||||||
|
if (!isU32(salt.length) || salt.length < 8)
|
||||||
|
throw new Error('salt should be at least 8 bytes and less than 4 GB');
|
||||||
|
if (!Object.values(AT).includes(type))
|
||||||
|
throw new Error('invalid type');
|
||||||
|
let { p, dkLen, m, t, version, key, personalization, maxmem, onProgress, asyncTick } = argon2Opts(opts);
|
||||||
|
// Validation
|
||||||
|
key = abytesOrZero(key);
|
||||||
|
personalization = abytesOrZero(personalization);
|
||||||
|
// H_0 = H^(64)(LE32(p) || LE32(T) || LE32(m) || LE32(t) ||
|
||||||
|
// LE32(v) || LE32(y) || LE32(length(P)) || P ||
|
||||||
|
// LE32(length(S)) || S || LE32(length(K)) || K ||
|
||||||
|
// LE32(length(X)) || X)
|
||||||
|
const h = blake2b.create({});
|
||||||
|
const BUF = new Uint32Array(1);
|
||||||
|
const BUF8 = u8(BUF);
|
||||||
|
for (let item of [p, dkLen, m, t, version, type]) {
|
||||||
|
BUF[0] = item;
|
||||||
|
h.update(BUF8);
|
||||||
|
}
|
||||||
|
for (let i of [password, salt, key, personalization]) {
|
||||||
|
BUF[0] = i.length; // BUF is u32 array, this is valid
|
||||||
|
h.update(BUF8).update(i);
|
||||||
|
}
|
||||||
|
const H0 = new Uint32Array(18);
|
||||||
|
const H0_8 = u8(H0);
|
||||||
|
h.digestInto(H0_8);
|
||||||
|
// 256 u32 = 1024 (BLOCK_SIZE), fills A2_BUF on processing
|
||||||
|
// Params
|
||||||
|
const lanes = p;
|
||||||
|
// m' = 4 * p * floor (m / 4p)
|
||||||
|
const mP = 4 * p * Math.floor(m / (ARGON2_SYNC_POINTS * p));
|
||||||
|
//q = m' / p columns
|
||||||
|
const laneLen = Math.floor(mP / p);
|
||||||
|
const segmentLen = Math.floor(laneLen / ARGON2_SYNC_POINTS);
|
||||||
|
const memUsed = mP * 256;
|
||||||
|
if (!isU32(maxmem) || memUsed > maxmem)
|
||||||
|
throw new Error('mem should be less than 2**32, got: maxmem=' + maxmem + ', memused=' + memUsed);
|
||||||
|
const B = new Uint32Array(memUsed);
|
||||||
|
// Fill first blocks
|
||||||
|
for (let l = 0; l < p; l++) {
|
||||||
|
const i = 256 * laneLen * l;
|
||||||
|
// B[i][0] = H'^(1024)(H_0 || LE32(0) || LE32(i))
|
||||||
|
H0[17] = l;
|
||||||
|
H0[16] = 0;
|
||||||
|
B.set(Hp(H0, 1024), i);
|
||||||
|
// B[i][1] = H'^(1024)(H_0 || LE32(1) || LE32(i))
|
||||||
|
H0[16] = 1;
|
||||||
|
B.set(Hp(H0, 1024), i + 256);
|
||||||
|
}
|
||||||
|
let perBlock = () => { };
|
||||||
|
if (onProgress) {
|
||||||
|
const totalBlock = t * ARGON2_SYNC_POINTS * p * segmentLen;
|
||||||
|
// Invoke callback if progress changes from 10.01 to 10.02
|
||||||
|
// Allows to draw smooth progress bar on up to 8K screen
|
||||||
|
const callbackPer = Math.max(Math.floor(totalBlock / 10000), 1);
|
||||||
|
let blockCnt = 0;
|
||||||
|
perBlock = () => {
|
||||||
|
blockCnt++;
|
||||||
|
if (onProgress && (!(blockCnt % callbackPer) || blockCnt === totalBlock))
|
||||||
|
onProgress(blockCnt / totalBlock);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
clean(BUF, H0);
|
||||||
|
return { type, mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick };
|
||||||
|
}
|
||||||
|
function argon2Output(B, p, laneLen, dkLen) {
|
||||||
|
const B_final = new Uint32Array(256);
|
||||||
|
for (let l = 0; l < p; l++)
|
||||||
|
for (let j = 0; j < 256; j++)
|
||||||
|
B_final[j] ^= B[256 * (laneLen * l + laneLen - 1) + j];
|
||||||
|
const res = u8(Hp(B_final, dkLen));
|
||||||
|
clean(B_final);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
function processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor) {
|
||||||
|
if (offset % laneLen)
|
||||||
|
prev = offset - 1;
|
||||||
|
let randL, randH;
|
||||||
|
if (dataIndependent) {
|
||||||
|
let i128 = index % 128;
|
||||||
|
if (i128 === 0) {
|
||||||
|
address[256 + 12]++;
|
||||||
|
block(address, 256, 2 * 256, 0, false);
|
||||||
|
block(address, 0, 2 * 256, 0, false);
|
||||||
|
}
|
||||||
|
randL = address[2 * i128];
|
||||||
|
randH = address[2 * i128 + 1];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const T = 256 * prev;
|
||||||
|
randL = B[T];
|
||||||
|
randH = B[T + 1];
|
||||||
|
}
|
||||||
|
// address block
|
||||||
|
const refLane = r === 0 && s === 0 ? l : randH % lanes;
|
||||||
|
const refPos = indexAlpha(r, s, laneLen, segmentLen, index, randL, refLane == l);
|
||||||
|
const refBlock = laneLen * refLane + refPos;
|
||||||
|
// B[i][j] = G(B[i][j-1], B[l][z])
|
||||||
|
block(B, 256 * prev, 256 * refBlock, offset * 256, needXor);
|
||||||
|
}
|
||||||
|
function argon2(type, password, salt, opts) {
|
||||||
|
const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock } = argon2Init(password, salt, type, opts);
|
||||||
|
// Pre-loop setup
|
||||||
|
// [address, input, zero_block] format so we can pass single U32 to block function
|
||||||
|
const address = new Uint32Array(3 * 256);
|
||||||
|
address[256 + 6] = mP;
|
||||||
|
address[256 + 8] = t;
|
||||||
|
address[256 + 10] = type;
|
||||||
|
for (let r = 0; r < t; r++) {
|
||||||
|
const needXor = r !== 0 && version === 0x13;
|
||||||
|
address[256 + 0] = r;
|
||||||
|
for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
|
||||||
|
address[256 + 4] = s;
|
||||||
|
const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
|
||||||
|
for (let l = 0; l < p; l++) {
|
||||||
|
address[256 + 2] = l;
|
||||||
|
address[256 + 12] = 0;
|
||||||
|
let startPos = 0;
|
||||||
|
if (r === 0 && s === 0) {
|
||||||
|
startPos = 2;
|
||||||
|
if (dataIndependent) {
|
||||||
|
address[256 + 12]++;
|
||||||
|
block(address, 256, 2 * 256, 0, false);
|
||||||
|
block(address, 0, 2 * 256, 0, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// current block postion
|
||||||
|
let offset = l * laneLen + s * segmentLen + startPos;
|
||||||
|
// previous block position
|
||||||
|
let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
|
||||||
|
for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
|
||||||
|
perBlock();
|
||||||
|
processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clean(address);
|
||||||
|
return argon2Output(B, p, laneLen, dkLen);
|
||||||
|
}
|
||||||
|
/** argon2d GPU-resistant version. */
|
||||||
|
export const argon2d = (password, salt, opts) => argon2(AT.Argond2d, password, salt, opts);
|
||||||
|
/** argon2i side-channel-resistant version. */
|
||||||
|
export const argon2i = (password, salt, opts) => argon2(AT.Argon2i, password, salt, opts);
|
||||||
|
/** argon2id, combining i+d, the most popular version from RFC 9106 */
|
||||||
|
export const argon2id = (password, salt, opts) => argon2(AT.Argon2id, password, salt, opts);
|
||||||
|
async function argon2Async(type, password, salt, opts) {
|
||||||
|
const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick } = argon2Init(password, salt, type, opts);
|
||||||
|
// Pre-loop setup
|
||||||
|
// [address, input, zero_block] format so we can pass single U32 to block function
|
||||||
|
const address = new Uint32Array(3 * 256);
|
||||||
|
address[256 + 6] = mP;
|
||||||
|
address[256 + 8] = t;
|
||||||
|
address[256 + 10] = type;
|
||||||
|
let ts = Date.now();
|
||||||
|
for (let r = 0; r < t; r++) {
|
||||||
|
const needXor = r !== 0 && version === 0x13;
|
||||||
|
address[256 + 0] = r;
|
||||||
|
for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
|
||||||
|
address[256 + 4] = s;
|
||||||
|
const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
|
||||||
|
for (let l = 0; l < p; l++) {
|
||||||
|
address[256 + 2] = l;
|
||||||
|
address[256 + 12] = 0;
|
||||||
|
let startPos = 0;
|
||||||
|
if (r === 0 && s === 0) {
|
||||||
|
startPos = 2;
|
||||||
|
if (dataIndependent) {
|
||||||
|
address[256 + 12]++;
|
||||||
|
block(address, 256, 2 * 256, 0, false);
|
||||||
|
block(address, 0, 2 * 256, 0, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// current block postion
|
||||||
|
let offset = l * laneLen + s * segmentLen + startPos;
|
||||||
|
// previous block position
|
||||||
|
let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
|
||||||
|
for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
|
||||||
|
perBlock();
|
||||||
|
processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor);
|
||||||
|
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
||||||
|
const diff = Date.now() - ts;
|
||||||
|
if (!(diff >= 0 && diff < asyncTick)) {
|
||||||
|
await nextTick();
|
||||||
|
ts += diff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clean(address);
|
||||||
|
return argon2Output(B, p, laneLen, dkLen);
|
||||||
|
}
|
||||||
|
/** argon2d async GPU-resistant version. */
|
||||||
|
export const argon2dAsync = (password, salt, opts) => argon2Async(AT.Argond2d, password, salt, opts);
|
||||||
|
/** argon2i async side-channel-resistant version. */
|
||||||
|
export const argon2iAsync = (password, salt, opts) => argon2Async(AT.Argon2i, password, salt, opts);
|
||||||
|
/** argon2id async, combining i+d, the most popular version from RFC 9106 */
|
||||||
|
export const argon2idAsync = (password, salt, opts) => argon2Async(AT.Argon2id, password, salt, opts);
|
||||||
|
//# sourceMappingURL=argon2.js.map
|
||||||
+1
File diff suppressed because one or more lines are too long
+106
@@ -0,0 +1,106 @@
|
|||||||
|
import { Hash, type CHashO, type Input } from './utils.ts';
|
||||||
|
/** Blake1 options. Basically just "salt" */
|
||||||
|
export type BlakeOpts = {
|
||||||
|
salt?: Uint8Array;
|
||||||
|
};
|
||||||
|
declare abstract class BLAKE1<T extends BLAKE1<T>> extends Hash<T> {
|
||||||
|
protected finished: boolean;
|
||||||
|
protected length: number;
|
||||||
|
protected pos: number;
|
||||||
|
protected destroyed: boolean;
|
||||||
|
protected buffer: Uint8Array;
|
||||||
|
protected view: DataView;
|
||||||
|
protected salt: Uint32Array;
|
||||||
|
abstract compress(view: DataView, offset: number, withLength?: boolean): void;
|
||||||
|
protected abstract get(): number[];
|
||||||
|
protected abstract set(...args: number[]): void;
|
||||||
|
readonly blockLen: number;
|
||||||
|
readonly outputLen: number;
|
||||||
|
private lengthFlag;
|
||||||
|
private counterLen;
|
||||||
|
protected constants: Uint32Array;
|
||||||
|
constructor(blockLen: number, outputLen: number, lengthFlag: number, counterLen: number, saltLen: number, constants: Uint32Array, opts?: BlakeOpts);
|
||||||
|
update(data: Input): this;
|
||||||
|
destroy(): void;
|
||||||
|
_cloneInto(to?: T): T;
|
||||||
|
clone(): T;
|
||||||
|
digestInto(out: Uint8Array): void;
|
||||||
|
digest(): Uint8Array;
|
||||||
|
}
|
||||||
|
declare class Blake1_32 extends BLAKE1<Blake1_32> {
|
||||||
|
private v0;
|
||||||
|
private v1;
|
||||||
|
private v2;
|
||||||
|
private v3;
|
||||||
|
private v4;
|
||||||
|
private v5;
|
||||||
|
private v6;
|
||||||
|
private v7;
|
||||||
|
constructor(outputLen: number, IV: Uint32Array, lengthFlag: number, opts?: BlakeOpts);
|
||||||
|
protected get(): [number, number, number, number, number, number, number, number];
|
||||||
|
protected set(v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number): void;
|
||||||
|
destroy(): void;
|
||||||
|
compress(view: DataView, offset: number, withLength?: boolean): void;
|
||||||
|
}
|
||||||
|
declare class Blake1_64 extends BLAKE1<Blake1_64> {
|
||||||
|
private v0l;
|
||||||
|
private v0h;
|
||||||
|
private v1l;
|
||||||
|
private v1h;
|
||||||
|
private v2l;
|
||||||
|
private v2h;
|
||||||
|
private v3l;
|
||||||
|
private v3h;
|
||||||
|
private v4l;
|
||||||
|
private v4h;
|
||||||
|
private v5l;
|
||||||
|
private v5h;
|
||||||
|
private v6l;
|
||||||
|
private v6h;
|
||||||
|
private v7l;
|
||||||
|
private v7h;
|
||||||
|
constructor(outputLen: number, IV: Uint32Array, lengthFlag: number, opts?: BlakeOpts);
|
||||||
|
protected get(): [
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number
|
||||||
|
];
|
||||||
|
protected set(v0l: number, v0h: number, v1l: number, v1h: number, v2l: number, v2h: number, v3l: number, v3h: number, v4l: number, v4h: number, v5l: number, v5h: number, v6l: number, v6h: number, v7l: number, v7h: number): void;
|
||||||
|
destroy(): void;
|
||||||
|
compress(view: DataView, offset: number, withLength?: boolean): void;
|
||||||
|
}
|
||||||
|
export declare class BLAKE224 extends Blake1_32 {
|
||||||
|
constructor(opts?: BlakeOpts);
|
||||||
|
}
|
||||||
|
export declare class BLAKE256 extends Blake1_32 {
|
||||||
|
constructor(opts?: BlakeOpts);
|
||||||
|
}
|
||||||
|
export declare class BLAKE384 extends Blake1_64 {
|
||||||
|
constructor(opts?: BlakeOpts);
|
||||||
|
}
|
||||||
|
export declare class BLAKE512 extends Blake1_64 {
|
||||||
|
constructor(opts?: BlakeOpts);
|
||||||
|
}
|
||||||
|
/** blake1-224 hash function */
|
||||||
|
export declare const blake224: CHashO;
|
||||||
|
/** blake1-256 hash function */
|
||||||
|
export declare const blake256: CHashO;
|
||||||
|
/** blake1-384 hash function */
|
||||||
|
export declare const blake384: CHashO;
|
||||||
|
/** blake1-512 hash function */
|
||||||
|
export declare const blake512: CHashO;
|
||||||
|
export {};
|
||||||
|
//# sourceMappingURL=blake1.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake1.d.ts","sourceRoot":"","sources":["../src/blake1.ts"],"names":[],"mappings":"AA4BA,OAAO,EAGO,IAAI,EAChB,KAAK,MAAM,EAAE,KAAK,KAAK,EACxB,MAAM,YAAY,CAAC;AAEpB,4CAA4C;AAC5C,MAAM,MAAM,SAAS,GAAG;IACtB,IAAI,CAAC,EAAE,UAAU,CAAC;CACnB,CAAC;AAKF,uBAAe,MAAM,CAAC,CAAC,SAAS,MAAM,CAAC,CAAC,CAAC,CAAE,SAAQ,IAAI,CAAC,CAAC,CAAC;IACxD,SAAS,CAAC,QAAQ,UAAS;IAC3B,SAAS,CAAC,MAAM,SAAK;IACrB,SAAS,CAAC,GAAG,SAAK;IAClB,SAAS,CAAC,SAAS,UAAS;IAE5B,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC;IAC7B,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC;IACzB,SAAS,CAAC,IAAI,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,OAAO,GAAG,IAAI;IAC7E,SAAS,CAAC,QAAQ,CAAC,GAAG,IAAI,MAAM,EAAE;IAClC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI;IAE/C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,UAAU,CAAS;IAC3B,SAAS,CAAC,SAAS,EAAE,WAAW,CAAC;gBAG/B,QAAQ,EAAE,MAAM,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AAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GACjD,IAAI;IAkBP,OAAO,IAAI,IAAI;IAIf,QAAQ,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,UAAO,GAAG,IAAI;CAiDlE;AAED,qBAAa,QAAS,SAAQ,SAAS;gBACzB,IAAI,GAAE,SAAc;CAGjC;AACD,qBAAa,QAAS,SAAQ,SAAS;gBACzB,IAAI,GAAE,SAAc;CAGjC;AACD,qBAAa,QAAS,SAAQ,SAAS;gBACzB,IAAI,GAAE,SAAc;CAGjC;AACD,qBAAa,QAAS,SAAQ,SAAS;gBACzB,IAAI,GAAE,SAAc;CAGjC;AACD,+BAA+B;AAC/B,eAAO,MAAM,QAAQ,EAAE,MAEtB,CAAC;AACF,+BAA+B;AAC/B,eAAO,MAAM,QAAQ,EAAE,MAEtB,CAAC;AACF,+BAA+B;AAC/B,eAAO,MAAM,QAAQ,EAAE,MAEtB,CAAC;AACF,+BAA+B;AAC/B,eAAO,MAAM,QAAQ,EAAE,MAEtB,CAAC"}
|
||||||
+452
@@ -0,0 +1,452 @@
|
|||||||
|
/**
|
||||||
|
* Blake1 legacy hash function, one of SHA3 proposals.
|
||||||
|
* Rarely used. Check out blake2 or blake3 instead.
|
||||||
|
* https://www.aumasson.jp/blake/blake.pdf
|
||||||
|
*
|
||||||
|
* In the best case, there are 0 allocations.
|
||||||
|
*
|
||||||
|
* Differences from blake2:
|
||||||
|
*
|
||||||
|
* - BE instead of LE
|
||||||
|
* - Paddings, similar to MD5, RIPEMD, SHA1, SHA2, but:
|
||||||
|
* - length flag is located before actual length
|
||||||
|
* - padding block is compressed differently (no lengths)
|
||||||
|
* Instead of msg[sigma[k]], we have `msg[sigma[k]] ^ constants[sigma[k-1]]`
|
||||||
|
* (-1 for g1, g2 without -1)
|
||||||
|
* - Salt is XOR-ed into constants instead of state
|
||||||
|
* - Salt is XOR-ed with output in `compress`
|
||||||
|
* - Additional rows (+64 bytes) in SIGMA for new rounds
|
||||||
|
* - Different round count:
|
||||||
|
* - 14 / 10 rounds in blake256 / blake2s
|
||||||
|
* - 16 / 12 rounds in blake512 / blake2b
|
||||||
|
* - blake512: G1b: rotr 24 -> 25, G2b: rotr 63 -> 11
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
import { BSIGMA, G1s, G2s } from "./_blake.js";
|
||||||
|
import { setBigUint64, SHA224_IV, SHA256_IV, SHA384_IV, SHA512_IV } from "./_md.js";
|
||||||
|
import * as u64 from "./_u64.js";
|
||||||
|
// prettier-ignore
|
||||||
|
import { abytes, aexists, aoutput, clean, createOptHasher, createView, Hash, toBytes, } from "./utils.js";
|
||||||
|
// Empty zero-filled salt
|
||||||
|
const EMPTY_SALT = /* @__PURE__ */ new Uint32Array(8);
|
||||||
|
class BLAKE1 extends Hash {
|
||||||
|
constructor(blockLen, outputLen, lengthFlag, counterLen, saltLen, constants, opts = {}) {
|
||||||
|
super();
|
||||||
|
this.finished = false;
|
||||||
|
this.length = 0;
|
||||||
|
this.pos = 0;
|
||||||
|
this.destroyed = false;
|
||||||
|
const { salt } = opts;
|
||||||
|
this.blockLen = blockLen;
|
||||||
|
this.outputLen = outputLen;
|
||||||
|
this.lengthFlag = lengthFlag;
|
||||||
|
this.counterLen = counterLen;
|
||||||
|
this.buffer = new Uint8Array(blockLen);
|
||||||
|
this.view = createView(this.buffer);
|
||||||
|
if (salt) {
|
||||||
|
let slt = salt;
|
||||||
|
slt = toBytes(slt);
|
||||||
|
abytes(slt);
|
||||||
|
if (slt.length !== 4 * saltLen)
|
||||||
|
throw new Error('wrong salt length');
|
||||||
|
const salt32 = (this.salt = new Uint32Array(saltLen));
|
||||||
|
const sv = createView(slt);
|
||||||
|
this.constants = constants.slice();
|
||||||
|
for (let i = 0, offset = 0; i < salt32.length; i++, offset += 4) {
|
||||||
|
salt32[i] = sv.getUint32(offset, false);
|
||||||
|
this.constants[i] ^= salt32[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.salt = EMPTY_SALT;
|
||||||
|
this.constants = constants;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
update(data) {
|
||||||
|
aexists(this);
|
||||||
|
data = toBytes(data);
|
||||||
|
abytes(data);
|
||||||
|
// From _md, but update length before each compress
|
||||||
|
const { view, buffer, blockLen } = this;
|
||||||
|
const len = data.length;
|
||||||
|
let dataView;
|
||||||
|
for (let pos = 0; pos < len;) {
|
||||||
|
const take = Math.min(blockLen - this.pos, len - pos);
|
||||||
|
// Fast path: we have at least one block in input, cast it to view and process
|
||||||
|
if (take === blockLen) {
|
||||||
|
if (!dataView)
|
||||||
|
dataView = createView(data);
|
||||||
|
for (; blockLen <= len - pos; pos += blockLen) {
|
||||||
|
this.length += blockLen;
|
||||||
|
this.compress(dataView, pos);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buffer.set(data.subarray(pos, pos + take), this.pos);
|
||||||
|
this.pos += take;
|
||||||
|
pos += take;
|
||||||
|
if (this.pos === blockLen) {
|
||||||
|
this.length += blockLen;
|
||||||
|
this.compress(view, 0, true);
|
||||||
|
this.pos = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
this.destroyed = true;
|
||||||
|
if (this.salt !== EMPTY_SALT) {
|
||||||
|
clean(this.salt, this.constants);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_cloneInto(to) {
|
||||||
|
to || (to = new this.constructor());
|
||||||
|
to.set(...this.get());
|
||||||
|
const { buffer, length, finished, destroyed, constants, salt, pos } = this;
|
||||||
|
to.buffer.set(buffer);
|
||||||
|
to.constants = constants.slice();
|
||||||
|
to.destroyed = destroyed;
|
||||||
|
to.finished = finished;
|
||||||
|
to.length = length;
|
||||||
|
to.pos = pos;
|
||||||
|
to.salt = salt.slice();
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
clone() {
|
||||||
|
return this._cloneInto();
|
||||||
|
}
|
||||||
|
digestInto(out) {
|
||||||
|
aexists(this);
|
||||||
|
aoutput(out, this);
|
||||||
|
this.finished = true;
|
||||||
|
// Padding
|
||||||
|
const { buffer, blockLen, counterLen, lengthFlag, view } = this;
|
||||||
|
clean(buffer.subarray(this.pos)); // clean buf
|
||||||
|
const counter = BigInt((this.length + this.pos) * 8);
|
||||||
|
const counterPos = blockLen - counterLen - 1;
|
||||||
|
buffer[this.pos] |= 128; // End block flag
|
||||||
|
this.length += this.pos; // add unwritten length
|
||||||
|
// Not enough in buffer for length: write what we have.
|
||||||
|
if (this.pos > counterPos) {
|
||||||
|
this.compress(view, 0);
|
||||||
|
clean(buffer);
|
||||||
|
this.pos = 0;
|
||||||
|
}
|
||||||
|
// Difference with md: here we have lengthFlag!
|
||||||
|
buffer[counterPos] |= lengthFlag; // Length flag
|
||||||
|
// We always set 8 byte length flag. Because length will overflow significantly sooner.
|
||||||
|
setBigUint64(view, blockLen - 8, counter, false);
|
||||||
|
this.compress(view, 0, this.pos !== 0); // don't add length if length is not empty block?
|
||||||
|
// Write output
|
||||||
|
clean(buffer);
|
||||||
|
const v = createView(out);
|
||||||
|
const state = this.get();
|
||||||
|
for (let i = 0; i < this.outputLen / 4; ++i)
|
||||||
|
v.setUint32(i * 4, state[i]);
|
||||||
|
}
|
||||||
|
digest() {
|
||||||
|
const { buffer, outputLen } = this;
|
||||||
|
this.digestInto(buffer);
|
||||||
|
const res = buffer.slice(0, outputLen);
|
||||||
|
this.destroy();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Constants
|
||||||
|
const B64C = /* @__PURE__ */ Uint32Array.from([
|
||||||
|
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
|
||||||
|
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
|
||||||
|
0x9216d5d9, 0x8979fb1b, 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
|
||||||
|
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69,
|
||||||
|
]);
|
||||||
|
// first half of C512
|
||||||
|
const B32C = B64C.slice(0, 16);
|
||||||
|
const B256_IV = SHA256_IV.slice();
|
||||||
|
const B224_IV = SHA224_IV.slice();
|
||||||
|
const B384_IV = SHA384_IV.slice();
|
||||||
|
const B512_IV = SHA512_IV.slice();
|
||||||
|
function generateTBL256() {
|
||||||
|
const TBL = [];
|
||||||
|
for (let i = 0, j = 0; i < 14; i++, j += 16) {
|
||||||
|
for (let offset = 1; offset < 16; offset += 2) {
|
||||||
|
TBL.push(B32C[BSIGMA[j + offset]]);
|
||||||
|
TBL.push(B32C[BSIGMA[j + offset - 1]]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Uint32Array(TBL);
|
||||||
|
}
|
||||||
|
const TBL256 = /* @__PURE__ */ generateTBL256(); // C256[SIGMA[X]] precompute
|
||||||
|
// Reusable temporary buffer
|
||||||
|
const BLAKE256_W = /* @__PURE__ */ new Uint32Array(16);
|
||||||
|
class Blake1_32 extends BLAKE1 {
|
||||||
|
constructor(outputLen, IV, lengthFlag, opts = {}) {
|
||||||
|
super(64, outputLen, lengthFlag, 8, 4, B32C, opts);
|
||||||
|
this.v0 = IV[0] | 0;
|
||||||
|
this.v1 = IV[1] | 0;
|
||||||
|
this.v2 = IV[2] | 0;
|
||||||
|
this.v3 = IV[3] | 0;
|
||||||
|
this.v4 = IV[4] | 0;
|
||||||
|
this.v5 = IV[5] | 0;
|
||||||
|
this.v6 = IV[6] | 0;
|
||||||
|
this.v7 = IV[7] | 0;
|
||||||
|
}
|
||||||
|
get() {
|
||||||
|
const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
|
||||||
|
return [v0, v1, v2, v3, v4, v5, v6, v7];
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
set(v0, v1, v2, v3, v4, v5, v6, v7) {
|
||||||
|
this.v0 = v0 | 0;
|
||||||
|
this.v1 = v1 | 0;
|
||||||
|
this.v2 = v2 | 0;
|
||||||
|
this.v3 = v3 | 0;
|
||||||
|
this.v4 = v4 | 0;
|
||||||
|
this.v5 = v5 | 0;
|
||||||
|
this.v6 = v6 | 0;
|
||||||
|
this.v7 = v7 | 0;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
super.destroy();
|
||||||
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
compress(view, offset, withLength = true) {
|
||||||
|
for (let i = 0; i < 16; i++, offset += 4)
|
||||||
|
BLAKE256_W[i] = view.getUint32(offset, false);
|
||||||
|
// NOTE: we cannot re-use compress from blake2s, since there is additional xor over u256[SIGMA[e]]
|
||||||
|
let v00 = this.v0 | 0;
|
||||||
|
let v01 = this.v1 | 0;
|
||||||
|
let v02 = this.v2 | 0;
|
||||||
|
let v03 = this.v3 | 0;
|
||||||
|
let v04 = this.v4 | 0;
|
||||||
|
let v05 = this.v5 | 0;
|
||||||
|
let v06 = this.v6 | 0;
|
||||||
|
let v07 = this.v7 | 0;
|
||||||
|
let v08 = this.constants[0] | 0;
|
||||||
|
let v09 = this.constants[1] | 0;
|
||||||
|
let v10 = this.constants[2] | 0;
|
||||||
|
let v11 = this.constants[3] | 0;
|
||||||
|
const { h, l } = u64.fromBig(BigInt(withLength ? this.length * 8 : 0));
|
||||||
|
let v12 = (this.constants[4] ^ l) >>> 0;
|
||||||
|
let v13 = (this.constants[5] ^ l) >>> 0;
|
||||||
|
let v14 = (this.constants[6] ^ h) >>> 0;
|
||||||
|
let v15 = (this.constants[7] ^ h) >>> 0;
|
||||||
|
// prettier-ignore
|
||||||
|
for (let i = 0, k = 0, j = 0; i < 14; i++) {
|
||||||
|
({ a: v00, b: v04, c: v08, d: v12 } = G1s(v00, v04, v08, v12, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v00, b: v04, c: v08, d: v12 } = G2s(v00, v04, v08, v12, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v01, b: v05, c: v09, d: v13 } = G1s(v01, v05, v09, v13, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v01, b: v05, c: v09, d: v13 } = G2s(v01, v05, v09, v13, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v02, b: v06, c: v10, d: v14 } = G1s(v02, v06, v10, v14, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v02, b: v06, c: v10, d: v14 } = G2s(v02, v06, v10, v14, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v03, b: v07, c: v11, d: v15 } = G1s(v03, v07, v11, v15, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v03, b: v07, c: v11, d: v15 } = G2s(v03, v07, v11, v15, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v00, b: v05, c: v10, d: v15 } = G1s(v00, v05, v10, v15, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v00, b: v05, c: v10, d: v15 } = G2s(v00, v05, v10, v15, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v01, b: v06, c: v11, d: v12 } = G1s(v01, v06, v11, v12, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v01, b: v06, c: v11, d: v12 } = G2s(v01, v06, v11, v12, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v02, b: v07, c: v08, d: v13 } = G1s(v02, v07, v08, v13, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v02, b: v07, c: v08, d: v13 } = G2s(v02, v07, v08, v13, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v03, b: v04, c: v09, d: v14 } = G1s(v03, v04, v09, v14, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
({ a: v03, b: v04, c: v09, d: v14 } = G2s(v03, v04, v09, v14, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
||||||
|
}
|
||||||
|
this.v0 = (this.v0 ^ v00 ^ v08 ^ this.salt[0]) >>> 0;
|
||||||
|
this.v1 = (this.v1 ^ v01 ^ v09 ^ this.salt[1]) >>> 0;
|
||||||
|
this.v2 = (this.v2 ^ v02 ^ v10 ^ this.salt[2]) >>> 0;
|
||||||
|
this.v3 = (this.v3 ^ v03 ^ v11 ^ this.salt[3]) >>> 0;
|
||||||
|
this.v4 = (this.v4 ^ v04 ^ v12 ^ this.salt[0]) >>> 0;
|
||||||
|
this.v5 = (this.v5 ^ v05 ^ v13 ^ this.salt[1]) >>> 0;
|
||||||
|
this.v6 = (this.v6 ^ v06 ^ v14 ^ this.salt[2]) >>> 0;
|
||||||
|
this.v7 = (this.v7 ^ v07 ^ v15 ^ this.salt[3]) >>> 0;
|
||||||
|
clean(BLAKE256_W);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
||||||
|
const BLAKE512_W = /* @__PURE__ */ new Uint32Array(32);
|
||||||
|
function generateTBL512() {
|
||||||
|
const TBL = [];
|
||||||
|
for (let r = 0, k = 0; r < 16; r++, k += 16) {
|
||||||
|
for (let offset = 1; offset < 16; offset += 2) {
|
||||||
|
TBL.push(B64C[BSIGMA[k + offset] * 2 + 0]);
|
||||||
|
TBL.push(B64C[BSIGMA[k + offset] * 2 + 1]);
|
||||||
|
TBL.push(B64C[BSIGMA[k + offset - 1] * 2 + 0]);
|
||||||
|
TBL.push(B64C[BSIGMA[k + offset - 1] * 2 + 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Uint32Array(TBL);
|
||||||
|
}
|
||||||
|
const TBL512 = /* @__PURE__ */ generateTBL512(); // C512[SIGMA[X]] precompute
|
||||||
|
// Mixing function G splitted in two halfs
|
||||||
|
function G1b(a, b, c, d, msg, k) {
|
||||||
|
const Xpos = 2 * BSIGMA[k];
|
||||||
|
const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
|
||||||
|
let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
|
||||||
|
let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
|
||||||
|
let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
|
||||||
|
let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
|
||||||
|
// v[a] = (v[a] + v[b] + x) | 0;
|
||||||
|
let ll = u64.add3L(Al, Bl, Xl);
|
||||||
|
Ah = u64.add3H(ll, Ah, Bh, Xh) >>> 0;
|
||||||
|
Al = (ll | 0) >>> 0;
|
||||||
|
// v[d] = rotr(v[d] ^ v[a], 32)
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
|
||||||
|
// v[c] = (v[c] + v[d]) | 0;
|
||||||
|
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
||||||
|
// v[b] = rotr(v[b] ^ v[c], 25)
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 25), Bl: u64.rotrSL(Bh, Bl, 25) });
|
||||||
|
(BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah);
|
||||||
|
(BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh);
|
||||||
|
(BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch);
|
||||||
|
(BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh);
|
||||||
|
}
|
||||||
|
function G2b(a, b, c, d, msg, k) {
|
||||||
|
const Xpos = 2 * BSIGMA[k];
|
||||||
|
const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
|
||||||
|
let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
|
||||||
|
let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
|
||||||
|
let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
|
||||||
|
let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
|
||||||
|
// v[a] = (v[a] + v[b] + x) | 0;
|
||||||
|
let ll = u64.add3L(Al, Bl, Xl);
|
||||||
|
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
||||||
|
Al = ll | 0;
|
||||||
|
// v[d] = rotr(v[d] ^ v[a], 16)
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
|
||||||
|
// v[c] = (v[c] + v[d]) | 0;
|
||||||
|
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
||||||
|
// v[b] = rotr(v[b] ^ v[c], 11)
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 11), Bl: u64.rotrSL(Bh, Bl, 11) });
|
||||||
|
(BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah);
|
||||||
|
(BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh);
|
||||||
|
(BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch);
|
||||||
|
(BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh);
|
||||||
|
}
|
||||||
|
class Blake1_64 extends BLAKE1 {
|
||||||
|
constructor(outputLen, IV, lengthFlag, opts = {}) {
|
||||||
|
super(128, outputLen, lengthFlag, 16, 8, B64C, opts);
|
||||||
|
this.v0l = IV[0] | 0;
|
||||||
|
this.v0h = IV[1] | 0;
|
||||||
|
this.v1l = IV[2] | 0;
|
||||||
|
this.v1h = IV[3] | 0;
|
||||||
|
this.v2l = IV[4] | 0;
|
||||||
|
this.v2h = IV[5] | 0;
|
||||||
|
this.v3l = IV[6] | 0;
|
||||||
|
this.v3h = IV[7] | 0;
|
||||||
|
this.v4l = IV[8] | 0;
|
||||||
|
this.v4h = IV[9] | 0;
|
||||||
|
this.v5l = IV[10] | 0;
|
||||||
|
this.v5h = IV[11] | 0;
|
||||||
|
this.v6l = IV[12] | 0;
|
||||||
|
this.v6h = IV[13] | 0;
|
||||||
|
this.v7l = IV[14] | 0;
|
||||||
|
this.v7h = IV[15] | 0;
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
get() {
|
||||||
|
let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
|
||||||
|
return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
|
||||||
|
this.v0l = v0l | 0;
|
||||||
|
this.v0h = v0h | 0;
|
||||||
|
this.v1l = v1l | 0;
|
||||||
|
this.v1h = v1h | 0;
|
||||||
|
this.v2l = v2l | 0;
|
||||||
|
this.v2h = v2h | 0;
|
||||||
|
this.v3l = v3l | 0;
|
||||||
|
this.v3h = v3h | 0;
|
||||||
|
this.v4l = v4l | 0;
|
||||||
|
this.v4h = v4h | 0;
|
||||||
|
this.v5l = v5l | 0;
|
||||||
|
this.v5h = v5h | 0;
|
||||||
|
this.v6l = v6l | 0;
|
||||||
|
this.v6h = v6h | 0;
|
||||||
|
this.v7l = v7l | 0;
|
||||||
|
this.v7h = v7h | 0;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
super.destroy();
|
||||||
|
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
compress(view, offset, withLength = true) {
|
||||||
|
for (let i = 0; i < 32; i++, offset += 4)
|
||||||
|
BLAKE512_W[i] = view.getUint32(offset, false);
|
||||||
|
this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
|
||||||
|
BBUF.set(this.constants.subarray(0, 16), 16);
|
||||||
|
if (withLength) {
|
||||||
|
const { h, l } = u64.fromBig(BigInt(this.length * 8));
|
||||||
|
BBUF[24] = (BBUF[24] ^ h) >>> 0;
|
||||||
|
BBUF[25] = (BBUF[25] ^ l) >>> 0;
|
||||||
|
BBUF[26] = (BBUF[26] ^ h) >>> 0;
|
||||||
|
BBUF[27] = (BBUF[27] ^ l) >>> 0;
|
||||||
|
}
|
||||||
|
for (let i = 0, k = 0; i < 16; i++) {
|
||||||
|
G1b(0, 4, 8, 12, BLAKE512_W, k++);
|
||||||
|
G2b(0, 4, 8, 12, BLAKE512_W, k++);
|
||||||
|
G1b(1, 5, 9, 13, BLAKE512_W, k++);
|
||||||
|
G2b(1, 5, 9, 13, BLAKE512_W, k++);
|
||||||
|
G1b(2, 6, 10, 14, BLAKE512_W, k++);
|
||||||
|
G2b(2, 6, 10, 14, BLAKE512_W, k++);
|
||||||
|
G1b(3, 7, 11, 15, BLAKE512_W, k++);
|
||||||
|
G2b(3, 7, 11, 15, BLAKE512_W, k++);
|
||||||
|
G1b(0, 5, 10, 15, BLAKE512_W, k++);
|
||||||
|
G2b(0, 5, 10, 15, BLAKE512_W, k++);
|
||||||
|
G1b(1, 6, 11, 12, BLAKE512_W, k++);
|
||||||
|
G2b(1, 6, 11, 12, BLAKE512_W, k++);
|
||||||
|
G1b(2, 7, 8, 13, BLAKE512_W, k++);
|
||||||
|
G2b(2, 7, 8, 13, BLAKE512_W, k++);
|
||||||
|
G1b(3, 4, 9, 14, BLAKE512_W, k++);
|
||||||
|
G2b(3, 4, 9, 14, BLAKE512_W, k++);
|
||||||
|
}
|
||||||
|
this.v0l ^= BBUF[0] ^ BBUF[16] ^ this.salt[0];
|
||||||
|
this.v0h ^= BBUF[1] ^ BBUF[17] ^ this.salt[1];
|
||||||
|
this.v1l ^= BBUF[2] ^ BBUF[18] ^ this.salt[2];
|
||||||
|
this.v1h ^= BBUF[3] ^ BBUF[19] ^ this.salt[3];
|
||||||
|
this.v2l ^= BBUF[4] ^ BBUF[20] ^ this.salt[4];
|
||||||
|
this.v2h ^= BBUF[5] ^ BBUF[21] ^ this.salt[5];
|
||||||
|
this.v3l ^= BBUF[6] ^ BBUF[22] ^ this.salt[6];
|
||||||
|
this.v3h ^= BBUF[7] ^ BBUF[23] ^ this.salt[7];
|
||||||
|
this.v4l ^= BBUF[8] ^ BBUF[24] ^ this.salt[0];
|
||||||
|
this.v4h ^= BBUF[9] ^ BBUF[25] ^ this.salt[1];
|
||||||
|
this.v5l ^= BBUF[10] ^ BBUF[26] ^ this.salt[2];
|
||||||
|
this.v5h ^= BBUF[11] ^ BBUF[27] ^ this.salt[3];
|
||||||
|
this.v6l ^= BBUF[12] ^ BBUF[28] ^ this.salt[4];
|
||||||
|
this.v6h ^= BBUF[13] ^ BBUF[29] ^ this.salt[5];
|
||||||
|
this.v7l ^= BBUF[14] ^ BBUF[30] ^ this.salt[6];
|
||||||
|
this.v7h ^= BBUF[15] ^ BBUF[31] ^ this.salt[7];
|
||||||
|
clean(BBUF, BLAKE512_W);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class BLAKE224 extends Blake1_32 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
super(28, B224_IV, 0, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class BLAKE256 extends Blake1_32 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
super(32, B256_IV, 1, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class BLAKE384 extends Blake1_64 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
super(48, B384_IV, 0, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class BLAKE512 extends Blake1_64 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
super(64, B512_IV, 1, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/** blake1-224 hash function */
|
||||||
|
export const blake224 = /* @__PURE__ */ createOptHasher((opts) => new BLAKE224(opts));
|
||||||
|
/** blake1-256 hash function */
|
||||||
|
export const blake256 = /* @__PURE__ */ createOptHasher((opts) => new BLAKE256(opts));
|
||||||
|
/** blake1-384 hash function */
|
||||||
|
export const blake384 = /* @__PURE__ */ createOptHasher((opts) => new BLAKE384(opts));
|
||||||
|
/** blake1-512 hash function */
|
||||||
|
export const blake512 = /* @__PURE__ */ createOptHasher((opts) => new BLAKE512(opts));
|
||||||
|
//# sourceMappingURL=blake1.js.map
|
||||||
+1
File diff suppressed because one or more lines are too long
+116
@@ -0,0 +1,116 @@
|
|||||||
|
import { Hash, type CHashO, type Input } from './utils.ts';
|
||||||
|
/** Blake hash options. dkLen is output length. key is used in MAC mode. salt is used in KDF mode. */
|
||||||
|
export type Blake2Opts = {
|
||||||
|
dkLen?: number;
|
||||||
|
key?: Input;
|
||||||
|
salt?: Input;
|
||||||
|
personalization?: Input;
|
||||||
|
};
|
||||||
|
/** Class, from which others are subclassed. */
|
||||||
|
export declare abstract class BLAKE2<T extends BLAKE2<T>> extends Hash<T> {
|
||||||
|
protected abstract compress(msg: Uint32Array, offset: number, isLast: boolean): void;
|
||||||
|
protected abstract get(): number[];
|
||||||
|
protected abstract set(...args: number[]): void;
|
||||||
|
abstract destroy(): void;
|
||||||
|
protected buffer: Uint8Array;
|
||||||
|
protected buffer32: Uint32Array;
|
||||||
|
protected finished: boolean;
|
||||||
|
protected destroyed: boolean;
|
||||||
|
protected length: number;
|
||||||
|
protected pos: number;
|
||||||
|
readonly blockLen: number;
|
||||||
|
readonly outputLen: number;
|
||||||
|
constructor(blockLen: number, outputLen: number);
|
||||||
|
update(data: Input): this;
|
||||||
|
digestInto(out: Uint8Array): void;
|
||||||
|
digest(): Uint8Array;
|
||||||
|
_cloneInto(to?: T): T;
|
||||||
|
clone(): T;
|
||||||
|
}
|
||||||
|
export declare class BLAKE2b extends BLAKE2<BLAKE2b> {
|
||||||
|
private v0l;
|
||||||
|
private v0h;
|
||||||
|
private v1l;
|
||||||
|
private v1h;
|
||||||
|
private v2l;
|
||||||
|
private v2h;
|
||||||
|
private v3l;
|
||||||
|
private v3h;
|
||||||
|
private v4l;
|
||||||
|
private v4h;
|
||||||
|
private v5l;
|
||||||
|
private v5h;
|
||||||
|
private v6l;
|
||||||
|
private v6h;
|
||||||
|
private v7l;
|
||||||
|
private v7h;
|
||||||
|
constructor(opts?: Blake2Opts);
|
||||||
|
protected get(): [
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number,
|
||||||
|
number
|
||||||
|
];
|
||||||
|
protected set(v0l: number, v0h: number, v1l: number, v1h: number, v2l: number, v2h: number, v3l: number, v3h: number, v4l: number, v4h: number, v5l: number, v5h: number, v6l: number, v6h: number, v7l: number, v7h: number): void;
|
||||||
|
protected compress(msg: Uint32Array, offset: number, isLast: boolean): void;
|
||||||
|
destroy(): void;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
||||||
|
*/
|
||||||
|
export declare const blake2b: CHashO;
|
||||||
|
export type Num16 = {
|
||||||
|
v0: number;
|
||||||
|
v1: number;
|
||||||
|
v2: number;
|
||||||
|
v3: number;
|
||||||
|
v4: number;
|
||||||
|
v5: number;
|
||||||
|
v6: number;
|
||||||
|
v7: number;
|
||||||
|
v8: number;
|
||||||
|
v9: number;
|
||||||
|
v10: number;
|
||||||
|
v11: number;
|
||||||
|
v12: number;
|
||||||
|
v13: number;
|
||||||
|
v14: number;
|
||||||
|
v15: number;
|
||||||
|
};
|
||||||
|
export declare function compress(s: Uint8Array, offset: number, msg: Uint32Array, rounds: number, v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number, v8: number, v9: number, v10: number, v11: number, v12: number, v13: number, v14: number, v15: number): Num16;
|
||||||
|
export declare class BLAKE2s extends BLAKE2<BLAKE2s> {
|
||||||
|
private v0;
|
||||||
|
private v1;
|
||||||
|
private v2;
|
||||||
|
private v3;
|
||||||
|
private v4;
|
||||||
|
private v5;
|
||||||
|
private v6;
|
||||||
|
private v7;
|
||||||
|
constructor(opts?: Blake2Opts);
|
||||||
|
protected get(): [number, number, number, number, number, number, number, number];
|
||||||
|
protected set(v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number): void;
|
||||||
|
protected compress(msg: Uint32Array, offset: number, isLast: boolean): void;
|
||||||
|
destroy(): void;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. 1.5x faster than blake2b in JS.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
||||||
|
*/
|
||||||
|
export declare const blake2s: CHashO;
|
||||||
|
//# sourceMappingURL=blake2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js, so s is 1.5x faster.
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
import { BSIGMA, G1s, G2s } from "./_blake.js";
|
||||||
|
import { SHA256_IV } from "./_md.js";
|
||||||
|
import * as u64 from "./_u64.js";
|
||||||
|
// prettier-ignore
|
||||||
|
import { abytes, aexists, anumber, aoutput, clean, createOptHasher, Hash, swap32IfBE, swap8IfBE, toBytes, u32 } from "./utils.js";
|
||||||
|
// Same as SHA512_IV, but swapped endianness: LE instead of BE. iv[1] is iv[0], etc.
|
||||||
|
const B2B_IV = /* @__PURE__ */ Uint32Array.from([
|
||||||
|
0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
|
||||||
|
0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
|
||||||
|
]);
|
||||||
|
// Temporary buffer
|
||||||
|
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
||||||
|
// Mixing function G splitted in two halfs
|
||||||
|
function G1b(a, b, c, d, msg, x) {
|
||||||
|
// NOTE: V is LE here
|
||||||
|
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
||||||
|
let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
|
||||||
|
let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
|
||||||
|
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
|
||||||
|
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
|
||||||
|
// v[a] = (v[a] + v[b] + x) | 0;
|
||||||
|
let ll = u64.add3L(Al, Bl, Xl);
|
||||||
|
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
||||||
|
Al = ll | 0;
|
||||||
|
// v[d] = rotr(v[d] ^ v[a], 32)
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
|
||||||
|
// v[c] = (v[c] + v[d]) | 0;
|
||||||
|
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
||||||
|
// v[b] = rotr(v[b] ^ v[c], 24)
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 24), Bl: u64.rotrSL(Bh, Bl, 24) });
|
||||||
|
(BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
|
||||||
|
(BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
|
||||||
|
(BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
|
||||||
|
(BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
|
||||||
|
}
|
||||||
|
function G2b(a, b, c, d, msg, x) {
|
||||||
|
// NOTE: V is LE here
|
||||||
|
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
||||||
|
let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
|
||||||
|
let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
|
||||||
|
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
|
||||||
|
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
|
||||||
|
// v[a] = (v[a] + v[b] + x) | 0;
|
||||||
|
let ll = u64.add3L(Al, Bl, Xl);
|
||||||
|
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
||||||
|
Al = ll | 0;
|
||||||
|
// v[d] = rotr(v[d] ^ v[a], 16)
|
||||||
|
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
||||||
|
({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
|
||||||
|
// v[c] = (v[c] + v[d]) | 0;
|
||||||
|
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
||||||
|
// v[b] = rotr(v[b] ^ v[c], 63)
|
||||||
|
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
||||||
|
({ Bh, Bl } = { Bh: u64.rotrBH(Bh, Bl, 63), Bl: u64.rotrBL(Bh, Bl, 63) });
|
||||||
|
(BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
|
||||||
|
(BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
|
||||||
|
(BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
|
||||||
|
(BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
|
||||||
|
}
|
||||||
|
function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
|
||||||
|
anumber(keyLen);
|
||||||
|
if (outputLen < 0 || outputLen > keyLen)
|
||||||
|
throw new Error('outputLen bigger than keyLen');
|
||||||
|
const { key, salt, personalization } = opts;
|
||||||
|
if (key !== undefined && (key.length < 1 || key.length > keyLen))
|
||||||
|
throw new Error('key length must be undefined or 1..' + keyLen);
|
||||||
|
if (salt !== undefined && salt.length !== saltLen)
|
||||||
|
throw new Error('salt must be undefined or ' + saltLen);
|
||||||
|
if (personalization !== undefined && personalization.length !== persLen)
|
||||||
|
throw new Error('personalization must be undefined or ' + persLen);
|
||||||
|
}
|
||||||
|
/** Class, from which others are subclassed. */
|
||||||
|
export class BLAKE2 extends Hash {
|
||||||
|
constructor(blockLen, outputLen) {
|
||||||
|
super();
|
||||||
|
this.finished = false;
|
||||||
|
this.destroyed = false;
|
||||||
|
this.length = 0;
|
||||||
|
this.pos = 0;
|
||||||
|
anumber(blockLen);
|
||||||
|
anumber(outputLen);
|
||||||
|
this.blockLen = blockLen;
|
||||||
|
this.outputLen = outputLen;
|
||||||
|
this.buffer = new Uint8Array(blockLen);
|
||||||
|
this.buffer32 = u32(this.buffer);
|
||||||
|
}
|
||||||
|
update(data) {
|
||||||
|
aexists(this);
|
||||||
|
data = toBytes(data);
|
||||||
|
abytes(data);
|
||||||
|
// Main difference with other hashes: there is flag for last block,
|
||||||
|
// so we cannot process current block before we know that there
|
||||||
|
// is the next one. This significantly complicates logic and reduces ability
|
||||||
|
// to do zero-copy processing
|
||||||
|
const { blockLen, buffer, buffer32 } = this;
|
||||||
|
const len = data.length;
|
||||||
|
const offset = data.byteOffset;
|
||||||
|
const buf = data.buffer;
|
||||||
|
for (let pos = 0; pos < len;) {
|
||||||
|
// If buffer is full and we still have input (don't process last block, same as blake2s)
|
||||||
|
if (this.pos === blockLen) {
|
||||||
|
swap32IfBE(buffer32);
|
||||||
|
this.compress(buffer32, 0, false);
|
||||||
|
swap32IfBE(buffer32);
|
||||||
|
this.pos = 0;
|
||||||
|
}
|
||||||
|
const take = Math.min(blockLen - this.pos, len - pos);
|
||||||
|
const dataOffset = offset + pos;
|
||||||
|
// full block && aligned to 4 bytes && not last in input
|
||||||
|
if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
|
||||||
|
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
|
||||||
|
swap32IfBE(data32);
|
||||||
|
for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) {
|
||||||
|
this.length += blockLen;
|
||||||
|
this.compress(data32, pos32, false);
|
||||||
|
}
|
||||||
|
swap32IfBE(data32);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buffer.set(data.subarray(pos, pos + take), this.pos);
|
||||||
|
this.pos += take;
|
||||||
|
this.length += take;
|
||||||
|
pos += take;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
digestInto(out) {
|
||||||
|
aexists(this);
|
||||||
|
aoutput(out, this);
|
||||||
|
const { pos, buffer32 } = this;
|
||||||
|
this.finished = true;
|
||||||
|
// Padding
|
||||||
|
clean(this.buffer.subarray(pos));
|
||||||
|
swap32IfBE(buffer32);
|
||||||
|
this.compress(buffer32, 0, true);
|
||||||
|
swap32IfBE(buffer32);
|
||||||
|
const out32 = u32(out);
|
||||||
|
this.get().forEach((v, i) => (out32[i] = swap8IfBE(v)));
|
||||||
|
}
|
||||||
|
digest() {
|
||||||
|
const { buffer, outputLen } = this;
|
||||||
|
this.digestInto(buffer);
|
||||||
|
const res = buffer.slice(0, outputLen);
|
||||||
|
this.destroy();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
_cloneInto(to) {
|
||||||
|
const { buffer, length, finished, destroyed, outputLen, pos } = this;
|
||||||
|
to || (to = new this.constructor({ dkLen: outputLen }));
|
||||||
|
to.set(...this.get());
|
||||||
|
to.buffer.set(buffer);
|
||||||
|
to.destroyed = destroyed;
|
||||||
|
to.finished = finished;
|
||||||
|
to.length = length;
|
||||||
|
to.pos = pos;
|
||||||
|
// @ts-ignore
|
||||||
|
to.outputLen = outputLen;
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
clone() {
|
||||||
|
return this._cloneInto();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class BLAKE2b extends BLAKE2 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
const olen = opts.dkLen === undefined ? 64 : opts.dkLen;
|
||||||
|
super(128, olen);
|
||||||
|
// Same as SHA-512, but LE
|
||||||
|
this.v0l = B2B_IV[0] | 0;
|
||||||
|
this.v0h = B2B_IV[1] | 0;
|
||||||
|
this.v1l = B2B_IV[2] | 0;
|
||||||
|
this.v1h = B2B_IV[3] | 0;
|
||||||
|
this.v2l = B2B_IV[4] | 0;
|
||||||
|
this.v2h = B2B_IV[5] | 0;
|
||||||
|
this.v3l = B2B_IV[6] | 0;
|
||||||
|
this.v3h = B2B_IV[7] | 0;
|
||||||
|
this.v4l = B2B_IV[8] | 0;
|
||||||
|
this.v4h = B2B_IV[9] | 0;
|
||||||
|
this.v5l = B2B_IV[10] | 0;
|
||||||
|
this.v5h = B2B_IV[11] | 0;
|
||||||
|
this.v6l = B2B_IV[12] | 0;
|
||||||
|
this.v6h = B2B_IV[13] | 0;
|
||||||
|
this.v7l = B2B_IV[14] | 0;
|
||||||
|
this.v7h = B2B_IV[15] | 0;
|
||||||
|
checkBlake2Opts(olen, opts, 64, 16, 16);
|
||||||
|
let { key, personalization, salt } = opts;
|
||||||
|
let keyLength = 0;
|
||||||
|
if (key !== undefined) {
|
||||||
|
key = toBytes(key);
|
||||||
|
keyLength = key.length;
|
||||||
|
}
|
||||||
|
this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
||||||
|
if (salt !== undefined) {
|
||||||
|
salt = toBytes(salt);
|
||||||
|
const slt = u32(salt);
|
||||||
|
this.v4l ^= swap8IfBE(slt[0]);
|
||||||
|
this.v4h ^= swap8IfBE(slt[1]);
|
||||||
|
this.v5l ^= swap8IfBE(slt[2]);
|
||||||
|
this.v5h ^= swap8IfBE(slt[3]);
|
||||||
|
}
|
||||||
|
if (personalization !== undefined) {
|
||||||
|
personalization = toBytes(personalization);
|
||||||
|
const pers = u32(personalization);
|
||||||
|
this.v6l ^= swap8IfBE(pers[0]);
|
||||||
|
this.v6h ^= swap8IfBE(pers[1]);
|
||||||
|
this.v7l ^= swap8IfBE(pers[2]);
|
||||||
|
this.v7h ^= swap8IfBE(pers[3]);
|
||||||
|
}
|
||||||
|
if (key !== undefined) {
|
||||||
|
// Pad to blockLen and update
|
||||||
|
const tmp = new Uint8Array(this.blockLen);
|
||||||
|
tmp.set(key);
|
||||||
|
this.update(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
get() {
|
||||||
|
let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
|
||||||
|
return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
|
||||||
|
this.v0l = v0l | 0;
|
||||||
|
this.v0h = v0h | 0;
|
||||||
|
this.v1l = v1l | 0;
|
||||||
|
this.v1h = v1h | 0;
|
||||||
|
this.v2l = v2l | 0;
|
||||||
|
this.v2h = v2h | 0;
|
||||||
|
this.v3l = v3l | 0;
|
||||||
|
this.v3h = v3h | 0;
|
||||||
|
this.v4l = v4l | 0;
|
||||||
|
this.v4h = v4h | 0;
|
||||||
|
this.v5l = v5l | 0;
|
||||||
|
this.v5h = v5h | 0;
|
||||||
|
this.v6l = v6l | 0;
|
||||||
|
this.v6h = v6h | 0;
|
||||||
|
this.v7l = v7l | 0;
|
||||||
|
this.v7h = v7h | 0;
|
||||||
|
}
|
||||||
|
compress(msg, offset, isLast) {
|
||||||
|
this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
|
||||||
|
BBUF.set(B2B_IV, 16); // Second half from IV.
|
||||||
|
let { h, l } = u64.fromBig(BigInt(this.length));
|
||||||
|
BBUF[24] = B2B_IV[8] ^ l; // Low word of the offset.
|
||||||
|
BBUF[25] = B2B_IV[9] ^ h; // High word.
|
||||||
|
// Invert all bits for last block
|
||||||
|
if (isLast) {
|
||||||
|
BBUF[28] = ~BBUF[28];
|
||||||
|
BBUF[29] = ~BBUF[29];
|
||||||
|
}
|
||||||
|
let j = 0;
|
||||||
|
const s = BSIGMA;
|
||||||
|
for (let i = 0; i < 12; i++) {
|
||||||
|
G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
|
||||||
|
G1b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
|
||||||
|
G2b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
|
||||||
|
}
|
||||||
|
this.v0l ^= BBUF[0] ^ BBUF[16];
|
||||||
|
this.v0h ^= BBUF[1] ^ BBUF[17];
|
||||||
|
this.v1l ^= BBUF[2] ^ BBUF[18];
|
||||||
|
this.v1h ^= BBUF[3] ^ BBUF[19];
|
||||||
|
this.v2l ^= BBUF[4] ^ BBUF[20];
|
||||||
|
this.v2h ^= BBUF[5] ^ BBUF[21];
|
||||||
|
this.v3l ^= BBUF[6] ^ BBUF[22];
|
||||||
|
this.v3h ^= BBUF[7] ^ BBUF[23];
|
||||||
|
this.v4l ^= BBUF[8] ^ BBUF[24];
|
||||||
|
this.v4h ^= BBUF[9] ^ BBUF[25];
|
||||||
|
this.v5l ^= BBUF[10] ^ BBUF[26];
|
||||||
|
this.v5h ^= BBUF[11] ^ BBUF[27];
|
||||||
|
this.v6l ^= BBUF[12] ^ BBUF[28];
|
||||||
|
this.v6h ^= BBUF[13] ^ BBUF[29];
|
||||||
|
this.v7l ^= BBUF[14] ^ BBUF[30];
|
||||||
|
this.v7h ^= BBUF[15] ^ BBUF[31];
|
||||||
|
clean(BBUF);
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
this.destroyed = true;
|
||||||
|
clean(this.buffer32);
|
||||||
|
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
||||||
|
*/
|
||||||
|
export const blake2b = /* @__PURE__ */ createOptHasher((opts) => new BLAKE2b(opts));
|
||||||
|
// prettier-ignore
|
||||||
|
export function compress(s, offset, msg, rounds, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
|
||||||
|
let j = 0;
|
||||||
|
for (let i = 0; i < rounds; i++) {
|
||||||
|
({ a: v0, b: v4, c: v8, d: v12 } = G1s(v0, v4, v8, v12, msg[offset + s[j++]]));
|
||||||
|
({ a: v0, b: v4, c: v8, d: v12 } = G2s(v0, v4, v8, v12, msg[offset + s[j++]]));
|
||||||
|
({ a: v1, b: v5, c: v9, d: v13 } = G1s(v1, v5, v9, v13, msg[offset + s[j++]]));
|
||||||
|
({ a: v1, b: v5, c: v9, d: v13 } = G2s(v1, v5, v9, v13, msg[offset + s[j++]]));
|
||||||
|
({ a: v2, b: v6, c: v10, d: v14 } = G1s(v2, v6, v10, v14, msg[offset + s[j++]]));
|
||||||
|
({ a: v2, b: v6, c: v10, d: v14 } = G2s(v2, v6, v10, v14, msg[offset + s[j++]]));
|
||||||
|
({ a: v3, b: v7, c: v11, d: v15 } = G1s(v3, v7, v11, v15, msg[offset + s[j++]]));
|
||||||
|
({ a: v3, b: v7, c: v11, d: v15 } = G2s(v3, v7, v11, v15, msg[offset + s[j++]]));
|
||||||
|
({ a: v0, b: v5, c: v10, d: v15 } = G1s(v0, v5, v10, v15, msg[offset + s[j++]]));
|
||||||
|
({ a: v0, b: v5, c: v10, d: v15 } = G2s(v0, v5, v10, v15, msg[offset + s[j++]]));
|
||||||
|
({ a: v1, b: v6, c: v11, d: v12 } = G1s(v1, v6, v11, v12, msg[offset + s[j++]]));
|
||||||
|
({ a: v1, b: v6, c: v11, d: v12 } = G2s(v1, v6, v11, v12, msg[offset + s[j++]]));
|
||||||
|
({ a: v2, b: v7, c: v8, d: v13 } = G1s(v2, v7, v8, v13, msg[offset + s[j++]]));
|
||||||
|
({ a: v2, b: v7, c: v8, d: v13 } = G2s(v2, v7, v8, v13, msg[offset + s[j++]]));
|
||||||
|
({ a: v3, b: v4, c: v9, d: v14 } = G1s(v3, v4, v9, v14, msg[offset + s[j++]]));
|
||||||
|
({ a: v3, b: v4, c: v9, d: v14 } = G2s(v3, v4, v9, v14, msg[offset + s[j++]]));
|
||||||
|
}
|
||||||
|
return { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 };
|
||||||
|
}
|
||||||
|
const B2S_IV = SHA256_IV;
|
||||||
|
export class BLAKE2s extends BLAKE2 {
|
||||||
|
constructor(opts = {}) {
|
||||||
|
const olen = opts.dkLen === undefined ? 32 : opts.dkLen;
|
||||||
|
super(64, olen);
|
||||||
|
// Internal state, same as SHA-256
|
||||||
|
this.v0 = B2S_IV[0] | 0;
|
||||||
|
this.v1 = B2S_IV[1] | 0;
|
||||||
|
this.v2 = B2S_IV[2] | 0;
|
||||||
|
this.v3 = B2S_IV[3] | 0;
|
||||||
|
this.v4 = B2S_IV[4] | 0;
|
||||||
|
this.v5 = B2S_IV[5] | 0;
|
||||||
|
this.v6 = B2S_IV[6] | 0;
|
||||||
|
this.v7 = B2S_IV[7] | 0;
|
||||||
|
checkBlake2Opts(olen, opts, 32, 8, 8);
|
||||||
|
let { key, personalization, salt } = opts;
|
||||||
|
let keyLength = 0;
|
||||||
|
if (key !== undefined) {
|
||||||
|
key = toBytes(key);
|
||||||
|
keyLength = key.length;
|
||||||
|
}
|
||||||
|
this.v0 ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
||||||
|
if (salt !== undefined) {
|
||||||
|
salt = toBytes(salt);
|
||||||
|
const slt = u32(salt);
|
||||||
|
this.v4 ^= swap8IfBE(slt[0]);
|
||||||
|
this.v5 ^= swap8IfBE(slt[1]);
|
||||||
|
}
|
||||||
|
if (personalization !== undefined) {
|
||||||
|
personalization = toBytes(personalization);
|
||||||
|
const pers = u32(personalization);
|
||||||
|
this.v6 ^= swap8IfBE(pers[0]);
|
||||||
|
this.v7 ^= swap8IfBE(pers[1]);
|
||||||
|
}
|
||||||
|
if (key !== undefined) {
|
||||||
|
// Pad to blockLen and update
|
||||||
|
abytes(key);
|
||||||
|
const tmp = new Uint8Array(this.blockLen);
|
||||||
|
tmp.set(key);
|
||||||
|
this.update(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get() {
|
||||||
|
const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
|
||||||
|
return [v0, v1, v2, v3, v4, v5, v6, v7];
|
||||||
|
}
|
||||||
|
// prettier-ignore
|
||||||
|
set(v0, v1, v2, v3, v4, v5, v6, v7) {
|
||||||
|
this.v0 = v0 | 0;
|
||||||
|
this.v1 = v1 | 0;
|
||||||
|
this.v2 = v2 | 0;
|
||||||
|
this.v3 = v3 | 0;
|
||||||
|
this.v4 = v4 | 0;
|
||||||
|
this.v5 = v5 | 0;
|
||||||
|
this.v6 = v6 | 0;
|
||||||
|
this.v7 = v7 | 0;
|
||||||
|
}
|
||||||
|
compress(msg, offset, isLast) {
|
||||||
|
const { h, l } = u64.fromBig(BigInt(this.length));
|
||||||
|
// prettier-ignore
|
||||||
|
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = compress(BSIGMA, offset, msg, 10, this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7, B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l ^ B2S_IV[4], h ^ B2S_IV[5], isLast ? ~B2S_IV[6] : B2S_IV[6], B2S_IV[7]);
|
||||||
|
this.v0 ^= v0 ^ v8;
|
||||||
|
this.v1 ^= v1 ^ v9;
|
||||||
|
this.v2 ^= v2 ^ v10;
|
||||||
|
this.v3 ^= v3 ^ v11;
|
||||||
|
this.v4 ^= v4 ^ v12;
|
||||||
|
this.v5 ^= v5 ^ v13;
|
||||||
|
this.v6 ^= v6 ^ v14;
|
||||||
|
this.v7 ^= v7 ^ v15;
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
this.destroyed = true;
|
||||||
|
clean(this.buffer32);
|
||||||
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. 1.5x faster than blake2b in JS.
|
||||||
|
* @param msg - message that would be hashed
|
||||||
|
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
||||||
|
*/
|
||||||
|
export const blake2s = /* @__PURE__ */ createOptHasher((opts) => new BLAKE2s(opts));
|
||||||
|
//# sourceMappingURL=blake2.js.map
|
||||||
+1
File diff suppressed because one or more lines are too long
+11
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* Blake2b hash function. Focuses on 64-bit platforms, but in JS speed different from Blake2s is negligible.
|
||||||
|
* @module
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
import { BLAKE2b as B2B, blake2b as b2b } from './blake2.ts';
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const BLAKE2b: typeof B2B;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export declare const blake2b: typeof b2b;
|
||||||
|
//# sourceMappingURL=blake2b.d.ts.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake2b.d.ts","sourceRoot":"","sources":["../src/blake2b.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,aAAa,CAAC;AAC7D,+DAA+D;AAC/D,eAAO,MAAM,OAAO,EAAE,OAAO,GAAS,CAAC;AACvC,+DAA+D;AAC/D,eAAO,MAAM,OAAO,EAAE,OAAO,GAAS,CAAC"}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* Blake2b hash function. Focuses on 64-bit platforms, but in JS speed different from Blake2s is negligible.
|
||||||
|
* @module
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
import { BLAKE2b as B2B, blake2b as b2b } from "./blake2.js";
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export const BLAKE2b = B2B;
|
||||||
|
/** @deprecated Use import from `noble/hashes/blake2` module */
|
||||||
|
export const blake2b = b2b;
|
||||||
|
//# sourceMappingURL=blake2b.js.map
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"blake2b.js","sourceRoot":"","sources":["../src/blake2b.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,aAAa,CAAC;AAC7D,+DAA+D;AAC/D,MAAM,CAAC,MAAM,OAAO,GAAe,GAAG,CAAC;AACvC,+DAA+D;AAC/D,MAAM,CAAC,MAAM,OAAO,GAAe,GAAG,CAAC"}
|
||||||
Reference in New Issue
Block a user