gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.69 kB
JavaScript
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */
var t,s=this&&this.__classPrivateFieldGet||function(t,s,e,i){if("a"===e&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof s?t!==s||!i:!s.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===e?i:"a"===e?i.call(t):i?i.value:s.get(t)};import{Aes as e}from"../sym/Aes.js";import{lShiftEq as i,xorEq as r}from"../../primitive/xtUint8Array.js";const n=16;export class Cmac{constructor(s){this.blockSize=n,this.size=n,this._k1=new Uint8Array(n),this._k2=new Uint8Array(n),t.set(this,new Uint8Array(n)),this._bPos=0,s instanceof e?(this._aes=s,this._k1=new Uint8Array(n),this._k2=new Uint8Array(n)):(this._aes=new e(s),function(t,s,e){const r=128,h=new Uint8Array(n);t.encryptBlock(h);let o=(h[0]&r)===r;i(h,1),o&&(h[h.length-1]^=135),s.set(h),o=(h[0]&r)===r,i(h,1),o&&(h[h.length-1]^=135),e.set(h)}(this._aes,this._k1,this._k2))}write(e){let i=e.length,r=0;for(;i>n;){for(let i=0;i<n;i++)s(this,t,"f")[this._bPos++]^=e[r++];this._aes.encryptBlock(s(this,t,"f")),this._bPos=0,i-=n}for(;r<e.length;)s(this,t,"f")[this._bPos++]^=e[r++]}sum(){return this.clone().sumIn()}sumIn(){return 16===this._bPos?r(s(this,t,"f"),this._k1):(s(this,t,"f")[this._bPos++]^=128,r(s(this,t,"f"),this._k2)),this._aes.encryptBlock(s(this,t,"f")),this._bPos=0,s(this,t,"f")}reset(){this._bPos=0,s(this,t,"f").fill(0)}newEmpty(){const t=new Cmac(this._aes);return t._k1.set(this._k1),t._k2.set(this._k2),t}clone(){const e=new Cmac(this._aes);return e._k1.set(this._k1),e._k2.set(this._k2),s(e,t,"f").set(s(this,t,"f")),e._bPos=this._bPos,e}}t=new WeakMap;