UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 6.71 kB
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */ var t,s=this&&this.__classPrivateFieldGet||function(t,s,i,r){if("a"===i&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof s?t!==s||!r:!s.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?r:"a"===i?r.call(t):r?r.value:s.get(t)};import{asLE as i}from"../../endian/platform.js";import{ContentError as r}from"../../error/ContentError.js";import{NotEnoughSpaceError as h}from"../../error/NotEnoughSpaceError.js";import{U32 as e}from"../../primitive/number/U32Static.js";const n=16,o=[Uint8Array.of(1,164,85,135,90,88,219,158),Uint8Array.of(164,86,130,243,30,198,104,229),Uint8Array.of(2,161,252,193,71,174,61,25),Uint8Array.of(164,85,135,90,88,219,158,3)],f=Uint8Array.of(169,103,179,232,4,253,163,118,154,146,128,120,228,221,209,56,13,198,53,152,24,247,236,108,67,117,55,38,250,19,148,72,242,208,139,48,132,84,223,35,25,91,61,89,243,174,162,130,99,1,131,46,217,81,155,124,166,235,165,190,22,12,227,97,192,140,58,245,115,44,37,11,187,78,137,107,83,106,180,241,225,230,189,69,226,244,182,102,204,149,3,86,212,28,30,215,251,195,142,181,233,207,191,186,234,119,57,175,51,201,98,113,129,121,9,173,36,205,249,216,229,197,185,77,68,8,134,231,161,29,170,237,6,112,178,210,65,123,160,17,49,194,39,144,32,246,96,255,150,92,177,171,158,156,82,27,95,147,10,239,145,133,73,238,45,79,143,59,71,135,109,70,214,62,105,100,42,206,203,47,252,151,5,122,172,127,213,26,75,14,167,90,40,20,63,41,136,60,76,2,184,218,176,23,85,31,138,125,87,199,141,116,183,196,159,114,126,21,34,18,88,7,153,52,110,80,222,104,101,188,219,248,200,168,43,64,220,254,50,164,202,16,33,240,211,93,15,0,111,157,54,66,74,94,193,224),l=Uint8Array.of(117,243,198,244,219,123,251,200,74,211,230,107,69,125,232,75,214,50,216,253,55,113,241,225,48,15,248,27,135,250,6,63,94,186,174,91,138,0,188,157,109,193,177,14,128,93,210,213,160,132,7,20,181,144,44,163,178,115,76,84,146,116,54,81,56,176,189,90,252,96,98,150,108,66,247,16,124,40,39,140,19,149,156,199,36,70,59,112,202,227,133,203,17,208,147,184,166,131,32,255,159,119,195,204,3,111,8,191,64,231,43,226,121,12,170,130,65,58,234,185,228,154,164,151,126,218,122,23,102,148,161,29,61,240,222,179,11,114,167,28,239,209,83,62,143,51,38,95,236,118,42,73,129,136,238,33,196,26,235,217,197,57,153,205,173,49,139,1,24,35,221,31,78,45,249,72,79,242,101,142,120,92,88,25,141,229,152,87,103,127,5,100,175,99,182,254,245,183,60,165,206,233,104,68,224,77,67,105,41,46,172,21,89,168,10,158,110,71,223,52,53,106,207,220,34,201,192,155,137,212,237,171,18,162,13,82,187,2,47,169,215,97,30,180,80,4,246,194,22,37,134,86,85,9,190,145);function a(t,s,i){const r=Uint32Array.of(0,s),h=Uint32Array.of(0,i);let e=0;for(let s=0;s<7;s++)e^=r[1&t],t>>>=1,r[1]=h[r[1]>>>7]^r[1]<<1;return e^=r[1&t],e}function _(t,s){const i=t,r=a(t,91,361),h=a(t,239,361);switch(s){case 0:return i|r<<8|h<<16|h<<24;case 1:return h|h<<8|r<<16|i<<24;case 2:return r|h<<8|i<<16|h<<24;default:return r|i<<8|h<<16|r<<24}}function c(t,s,i){const r=new Uint8Array(4);switch(r.set(t.subarray(0,4)),s.length){case 32:r[0]=l[r[0]]^s[4*(6+i)+0],r[1]=f[r[1]]^s[4*(6+i)+1],r[2]=f[r[2]]^s[4*(6+i)+2],r[3]=l[r[3]]^s[4*(6+i)+3];case 24:r[0]=l[r[0]]^s[4*(4+i)+0],r[1]=l[r[1]]^s[4*(4+i)+1],r[2]=f[r[2]]^s[4*(4+i)+2],r[3]=f[r[3]]^s[4*(4+i)+3];case 16:r[0]=l[f[f[r[0]]^s[4*(2+i)+0]]^s[4*i+0]],r[1]=f[f[l[r[1]]^s[4*(2+i)+1]]^s[4*i+1]],r[2]=l[l[f[r[2]]^s[4*(2+i)+2]]^s[4*i+2]],r[3]=f[l[l[r[3]]^s[4*(2+i)+3]]^s[4*i+3]]}return _(r[0],0)^_(r[1],1)^_(r[2],2)^_(r[3],3)}export class Twofish{constructor(i){this.blockSize=n,this._s0=new Uint32Array(256),this._s1=new Uint32Array(256),this._s2=new Uint32Array(256),this._s3=new Uint32Array(256),t.set(this,new Uint32Array(40));const h=i.length>>3;if(7&i.length||h<2||h>4)throw new r("should be 16, 24, or 32","key.length",i.length);const w=i.length/8,y=new Uint8Array(16);for(let t=0;t<w;t++)for(let s=0;s<o.length;s++){const r=o[s];for(let h=0;h<r.length;h++)y[4*t+s]^=a(i[8*t+h],r[h],333)}const u=new Uint8Array(4);for(let r=0;r<20;r++){u.fill(2*r);const h=c(u,i,0);u.fill(2*r+1);let n=c(u,i,1);n=e.lRot(n,8),s(this,t,"f")[2*r]=h+n,s(this,t,"f")[2*r+1]=e.lRot(2*n+h,9)}switch(w){case 2:for(let t=0;t<this._s0.length;t++)this._s0[t]=_(l[f[f[t]^y[0]]^y[4]],0),this._s1[t]=_(f[f[l[t]^y[1]]^y[5]],1),this._s2[t]=_(l[l[f[t]^y[2]]^y[6]],2),this._s3[t]=_(f[l[l[t]^y[3]]^y[7]],3);break;case 3:for(let t=0;t<this._s0.length;t++)this._s0[t]=_(l[f[f[l[t]^y[0]]^y[4]]^y[8]],0),this._s1[t]=_(f[f[l[l[t]^y[1]]^y[5]]^y[9]],1),this._s2[t]=_(l[l[f[f[t]^y[2]]^y[6]]^y[10]],2),this._s3[t]=_(f[l[l[f[t]^y[3]]^y[7]]^y[11]],3);break;default:for(let t=0;t<this._s0.length;t++)this._s0[t]=_(l[f[f[l[l[t]^y[0]]^y[4]]^y[8]]^y[12]],0),this._s1[t]=_(f[f[l[l[f[t]^y[1]]^y[5]]^y[9]]^y[13]],1),this._s2[t]=_(l[l[f[f[f[t]^y[2]]^y[6]]^y[10]]^y[14]],2),this._s3[t]=_(f[l[l[f[l[t]^y[3]]^y[7]]^y[11]]^y[15]],3)}}_decBlock(r){i.i32(r,0,4);const h=new Uint32Array(r.buffer);let n=h[2]^s(this,t,"f")[6],o=h[3]^s(this,t,"f")[7],f=h[0]^s(this,t,"f")[4],l=h[1]^s(this,t,"f")[5],a=39;for(let i=8;i>0;i--){let i=this._s0[255&f]^this._s1[f>>>8&255]^this._s2[f>>>16&255]^this._s3[f>>>24],r=this._s1[255&l]^this._s2[l>>>8&255]^this._s3[l>>>16&255]^this._s0[l>>>24];o=e.lRot(o^r+r+i+s(this,t,"f")[a--],-1),n=e.lRot(n,1)^r+i+s(this,t,"f")[a--],i=this._s0[255&n]^this._s1[n>>>8&255]^this._s2[n>>>16&255]^this._s3[n>>>24],r=this._s1[255&o]^this._s2[o>>>8&255]^this._s3[o>>>16&255]^this._s0[o>>>24],l=e.rRot(l^r+r+i+s(this,t,"f")[a--],1),f=e.lRot(f,1)^i+r+s(this,t,"f")[a--]}n^=s(this,t,"f")[0],o^=s(this,t,"f")[1],f^=s(this,t,"f")[2],l^=s(this,t,"f")[3],h[0]=n,h[1]=o,h[2]=f,h[3]=l,i.i32(r,0,4)}_encBlock(r){i.i32(r,0,4);const h=new Uint32Array(r.buffer);let n=h[0]^s(this,t,"f")[0],o=h[1]^s(this,t,"f")[1],f=h[2]^s(this,t,"f")[2],l=h[3]^s(this,t,"f")[3],a=8;for(let i=0;i<8;i++){let i=this._s0[255&n]^this._s1[n>>>8&255]^this._s2[n>>>16&255]^this._s3[n>>>24],r=this._s1[255&o]^this._s2[o>>>8&255]^this._s3[o>>>16&255]^this._s0[o>>>24];f=e.rRot(f^i+r+s(this,t,"f")[a++],1),l=e.lRot(l,1)^r+r+i+s(this,t,"f")[a++],i=this._s0[255&f]^this._s1[f>>>8&255]^this._s2[f>>>16&255]^this._s3[f>>>24],r=this._s1[255&l]^this._s2[l>>>8&255]^this._s3[l>>>16&255]^this._s0[l>>>24],n=e.rRot(n^i+r+s(this,t,"f")[a++],1),o=e.lRot(o,1)^r+r+i+s(this,t,"f")[a++]}h[0]=f^s(this,t,"f")[4],h[1]=l^s(this,t,"f")[5],h[2]=n^s(this,t,"f")[6],h[3]=o^s(this,t,"f")[7],i.i32(r,0,4)}decryptBlock(t,s=0){const i=s*n;if(t.length<i+n)throw new h("block.length",i+n,t.length);this._decBlock(t.subarray(i,i+n))}encryptBlock(t,s=0){const i=s*n;if(t.length<i+n)throw new h("block.length",i+n,t.length);this._encBlock(t.subarray(i,i+n))}}t=new WeakMap;