/opt/canhelp/node_modules/@noble/ciphers
NameSizeModeActions
src/-0755rm
aes.d.ts83070644editdlrm
aes.d.ts.map47720644editdlrm
aes.js503500644editdlrm
aes.js.map546380644editdlrm
chacha.d.ts32960644editdlrm
chacha.d.ts.map9430644editdlrm
chacha.js142130644editdlrm
chacha.js.map184060644editdlrm
ff1.d.ts4200644editdlrm
ff1.d.ts.map4520644editdlrm
ff1.js61720644editdlrm
ff1.js.map78510644editdlrm
index.d.ts460644editdlrm
index.d.ts.map1010644editdlrm
index.js11020644editdlrm
index.js.map1730644editdlrm
LICENSE11610644editdlrm
package.json21700644editdlrm
README.md268260644editdlrm
salsa.d.ts13270644editdlrm
salsa.d.ts.map6790644editdlrm
salsa.js112710644editdlrm
salsa.js.map137690644editdlrm
utils.d.ts81160644editdlrm
utils.d.ts.map50240644editdlrm
utils.js134580644editdlrm
utils.js.map115820644editdlrm
webcrypto.d.ts10960644editdlrm
webcrypto.d.ts.map9350644editdlrm
webcrypto.js39170644editdlrm
webcrypto.js.map33330644editdlrm
_arx.d.ts34170644editdlrm
_arx.d.ts.map16530644editdlrm
_arx.js97280644editdlrm
_arx.js.map90970644editdlrm
_poly1305.d.ts19470644editdlrm
_poly1305.d.ts.map9970644editdlrm
_poly1305.js120350644editdlrm
_poly1305.js.map178090644editdlrm
_polyval.d.ts20350644editdlrm
_polyval.d.ts.map16080644editdlrm
_polyval.js79220644editdlrm
_polyval.js.map101640644editdlrm
Edit: /opt/canhelp/node_modules/@noble/ciphers/_polyval.js (7922B)
/** * GHash from AES-GCM and its little-endian "mirror image" Polyval from AES-SIV. * * Implemented in terms of GHash with conversion function for keys * GCM GHASH from * [NIST SP800-38d](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf), * SIV from * [RFC 8452](https://www.rfc-editor.org/rfc/rfc8452). * * GHASH modulo: x^128 + x^7 + x^2 + x + 1 * POLYVAL modulo: x^128 + x^127 + x^126 + x^121 + 1 * * @module */ import { abytes, aexists, aoutput, clean, copyBytes, createView, u32, } from "./utils.js"; const BLOCK_SIZE = 16; // TODO: rewrite // temporary padding buffer const ZEROS16 = /* @__PURE__ */ new Uint8Array(16); const ZEROS32 = u32(ZEROS16); const POLY = 0xe1; // v = 2*v % POLY // v = 2*v % POLY // NOTE: because x + x = 0 (add/sub is same), mul2(x) != x+x // We can multiply any number using montgomery ladder and this function (works as double, add is simple xor) const mul2 = (s0, s1, s2, s3) => { const hiBit = s3 & 1; return { s3: (s2 << 31) | (s3 >>> 1), s2: (s1 << 31) | (s2 >>> 1), s1: (s0 << 31) | (s1 >>> 1), s0: (s0 >>> 1) ^ ((POLY << 24) & -(hiBit & 1)), // reduce % poly }; }; const swapLE = (n) => (((n >>> 0) & 0xff) << 24) | (((n >>> 8) & 0xff) << 16) | (((n >>> 16) & 0xff) << 8) | ((n >>> 24) & 0xff) | 0; /** * `mulX_POLYVAL(ByteReverse(H))` from spec * @param k mutated in place */ export function _toGHASHKey(k) { k.reverse(); const hiBit = k[15] & 1; // k >>= 1 let carry = 0; for (let i = 0; i < k.length; i++) { const t = k[i]; k[i] = (t >>> 1) | carry; carry = (t & 1) << 7; } k[0] ^= -hiBit & 0xe1; // if (hiBit) n ^= 0xe1000000000000000000000000000000; return k; } const estimateWindow = (bytes) => { if (bytes > 64 * 1024) return 8; if (bytes > 1024) return 4; return 2; }; export class GHASH { blockLen = BLOCK_SIZE; outputLen = BLOCK_SIZE; s0 = 0; s1 = 0; s2 = 0; s3 = 0; finished = false; t; W; windowSize; // We select bits per window adaptively based on expectedLength constructor(key, expectedLength) { abytes(key, 16, 'key'); key = copyBytes(key); const kView = createView(key); let k0 = kView.getUint32(0, false); let k1 = kView.getUint32(4, false); let k2 = kView.getUint32(8, false); let k3 = kView.getUint32(12, false); // generate table of doubled keys (half of montgomery ladder) const doubles = []; for (let i = 0; i < 128; i++) { doubles.push({ s0: swapLE(k0), s1: swapLE(k1), s2: swapLE(k2), s3: swapLE(k3) }); ({ s0: k0, s1: k1, s2: k2, s3: k3 } = mul2(k0, k1, k2, k3)); } const W = estimateWindow(expectedLength || 1024); if (![1, 2, 4, 8].includes(W)) throw new Error('ghash: invalid window size, expected 2, 4 or 8'); this.W = W; const bits = 128; // always 128 bits; const windows = bits / W; const windowSize = (this.windowSize = 2 ** W); const items = []; // Create precompute table for window of W bits for (let w = 0; w < windows; w++) { // truth table: 00, 01, 10, 11 for (let byte = 0; byte < windowSize; byte++) { // prettier-ignore let s0 = 0, s1 = 0, s2 = 0, s3 = 0; for (let j = 0; j < W; j++) { const bit = (byte >>> (W - j - 1)) & 1; if (!bit) continue; const { s0: d0, s1: d1, s2: d2, s3: d3 } = doubles[W * w + j]; ((s0 ^= d0), (s1 ^= d1), (s2 ^= d2), (s3 ^= d3)); } items.push({ s0, s1, s2, s3 }); } } this.t = items; } _updateBlock(s0, s1, s2, s3) { ((s0 ^= this.s0), (s1 ^= this.s1), (s2 ^= this.s2), (s3 ^= this.s3)); const { W, t, windowSize } = this; // prettier-ignore let o0 = 0, o1 = 0, o2 = 0, o3 = 0; const mask = (1 << W) - 1; // 2**W will kill performance. let w = 0; for (const num of [s0, s1, s2, s3]) { for (let bytePos = 0; bytePos < 4; bytePos++) { const byte = (num >>> (8 * bytePos)) & 0xff; for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) { const bit = (byte >>> (W * bitPos)) & mask; const { s0: e0, s1: e1, s2: e2, s3: e3 } = t[w * windowSize + bit]; ((o0 ^= e0), (o1 ^= e1), (o2 ^= e2), (o3 ^= e3)); w += 1; } } } this.s0 = o0; this.s1 = o1; this.s2 = o2; this.s3 = o3; } update(data) { aexists(this); abytes(data); data = copyBytes(data); const b32 = u32(data); const blocks = Math.floor(data.length / BLOCK_SIZE); const left = data.length % BLOCK_SIZE; for (let i = 0; i < blocks; i++) { this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]); } if (left) { ZEROS16.set(data.subarray(blocks * BLOCK_SIZE)); this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]); clean(ZEROS32); // clean tmp buffer } return this; } destroy() { const { t } = this; // clean precompute table for (const elm of t) { ((elm.s0 = 0), (elm.s1 = 0), (elm.s2 = 0), (elm.s3 = 0)); } } digestInto(out) { aexists(this); aoutput(out, this); this.finished = true; const { s0, s1, s2, s3 } = this; const o32 = u32(out); o32[0] = s0; o32[1] = s1; o32[2] = s2; o32[3] = s3; return out; } digest() { const res = new Uint8Array(BLOCK_SIZE); this.digestInto(res); this.destroy(); return res; } } export class Polyval extends GHASH { constructor(key, expectedLength) { abytes(key); const ghKey = _toGHASHKey(copyBytes(key)); super(ghKey, expectedLength); clean(ghKey); } update(data) { aexists(this); abytes(data); data = copyBytes(data); const b32 = u32(data); const left = data.length % BLOCK_SIZE; const blocks = Math.floor(data.length / BLOCK_SIZE); for (let i = 0; i < blocks; i++) { this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0])); } if (left) { ZEROS16.set(data.subarray(blocks * BLOCK_SIZE)); this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0])); clean(ZEROS32); } return this; } digestInto(out) { aexists(this); aoutput(out, this); this.finished = true; // tmp ugly hack const { s0, s1, s2, s3 } = this; const o32 = u32(out); o32[0] = s0; o32[1] = s1; o32[2] = s2; o32[3] = s3; return out.reverse(); } } function wrapConstructorWithKey(hashCons) { const hashC = (msg, key) => hashCons(key, msg.length).update(msg).digest(); const tmp = hashCons(new Uint8Array(16), 0); hashC.outputLen = tmp.outputLen; hashC.blockLen = tmp.blockLen; hashC.create = (key, expectedLength) => hashCons(key, expectedLength); return hashC; } /** GHash MAC for AES-GCM. */ export const ghash = wrapConstructorWithKey((key, expectedLength) => new GHASH(key, expectedLength)); /** Polyval MAC for AES-SIV. */ export const polyval = wrapConstructorWithKey((key, expectedLength) => new Polyval(key, expectedLength)); //# sourceMappingURL=_polyval.js.map