gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 3.44 kB
JavaScript
/*! Copyright 2025 the gnablib contributors MPL-1.1 */
var t,e=this&&this.__classPrivateFieldSet||function(t,e,r,o,i){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===o?i.call(t,r):i?i.value=r:e.set(t,r),r},r=this&&this.__classPrivateFieldGet||function(t,e,r,o){if("a"===r&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!o:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?o:"a"===r?o.call(t):o?o.value:e.get(t)};import{asBE as o}from"../../endian/platform.js";import{U32 as i}from"../../primitive/number/U32Static.js";import{sLen as s}from"../../safe/safe.js";const a=Uint32Array.of(2746333894,1453994832,1736282519,2993693404),n=Uint32Array.of(462357,472066609,943670861,1415275113,1886879365,2358483617,2830087869,3301692121,3773296373,4228057617,404694573,876298825,1347903077,1819507329,2291111581,2762715833,3234320085,3705924337,4177462797,337322537,808926789,1280531041,1752135293,2223739545,2695343797,3166948049,3638552301,4110090761,269950501,741554753,1213159005,1684763257),c=Uint8Array.of(214,144,233,254,204,225,61,183,22,182,20,194,40,251,44,5,43,103,154,118,42,190,4,195,170,68,19,38,73,134,6,153,156,66,80,244,145,239,152,122,51,84,11,67,237,207,172,98,228,179,28,169,201,8,232,149,128,223,148,250,117,143,63,166,71,7,167,252,243,115,23,186,131,89,60,25,230,133,79,168,104,107,129,178,113,100,218,139,248,235,15,75,112,86,157,53,30,36,14,94,99,88,209,162,37,34,124,59,1,33,120,135,212,0,70,87,159,211,39,82,76,54,2,231,160,196,200,158,234,191,138,210,64,199,56,181,163,247,242,206,249,97,21,161,224,174,93,164,155,52,26,85,173,147,50,48,245,140,177,227,29,246,226,46,130,102,202,96,192,41,35,171,13,83,78,111,213,219,55,69,222,253,142,47,3,255,106,114,109,108,91,81,141,27,175,146,187,221,188,127,17,217,92,65,31,16,90,216,10,193,49,136,165,205,123,189,45,116,208,18,184,229,180,176,137,105,151,74,12,150,119,126,101,185,241,9,197,110,198,132,24,240,125,236,58,220,77,32,121,238,95,62,215,203,57,72),f=t=>c[255&t]|c[255&t>>>8]<<8|c[255&t>>>16]<<16|c[t>>>24]<<24,l=t=>t^i.lRot(t,2)^i.lRot(t,10)^i.lRot(t,18)^i.lRot(t,24);export class Sm4{constructor(r){this.blockSize=16,t.set(this,void 0);const s=r.slice(),c=new Uint32Array(s.buffer,s.byteOffset,s.length/4);o.i32(s,0,4);const l=new Uint32Array(32);c[0]^=a[0],c[1]^=a[1],c[2]^=a[2],c[3]^=a[3];for(let t=0;t<32;t++){const e=f(c[1]^c[2]^c[3]^n[t]),r=e^i.lRot(e,13)^i.lRot(e,23);l[t]=c[0]^r,c[0]=c[1],c[1]=c[2],c[2]=c[3],c[3]=l[t]}e(this,t,l,"f")}_encBlock(e){const i=new Uint32Array(e.buffer,e.byteOffset,e.length/4);o.i32(e,0,4);for(let e=0;e<32;e++){const o=i[0]^l(f(i[1]^i[2]^i[3]^r(this,t,"f")[e]));i[0]=i[1],i[1]=i[2],i[2]=i[3],i[3]=o}e.reverse()}_decBlock(e){const i=new Uint32Array(e.buffer,e.byteOffset,e.length/4);o.i32(e,0,4);for(let e=31;e>=0;e--){const o=i[0]^l(f(i[1]^i[2]^i[3]^r(this,t,"f")[e]));i[0]=i[1],i[1]=i[2],i[2]=i[3],i[3]=o}o.i32(e,0,4),i.reverse()}encryptBlock(t,e=0){const r=8*e;s("block",t).atLeast(r+16).throwNot(),this._encBlock(t.subarray(r,r+16))}decryptBlock(t,e=0){const r=8*e;s("block",t).atLeast(r+16).throwNot(),this._decBlock(t.subarray(r,r+16))}}t=new WeakMap;