UNPKG

gnablib

Version:

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

2 lines 3.7 kB
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */ var t,e,s=this&&this.__classPrivateFieldSet||function(t,e,s,r,i){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!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"===r?i.call(t,s):i?i.value=s:e.set(t,s),s},r=this&&this.__classPrivateFieldGet||function(t,e,s,r){if("a"===s&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?r:"a"===s?r.call(t):r?r.value:e.get(t)};import{asLE as i}from"../../endian/platform.js";import{ContentError as o}from"../../error/ContentError.js";import{U32 as n}from"../../primitive/number/U32Static.js";import{sLen as h}from"../../safe/safe.js";const u=64,a=Uint32Array.of(1634760805,857760878,2036477234,1797285236),c=Uint32Array.of(1634760805,824206446,2036477238,1797285236);class f{constructor(t){this.rounds=t,this.u8=new Uint8Array(u),this.u32=new Uint32Array(this.u8.buffer)}quartRound(t,e,s,r){this.u32[t]+=this.u32[e],this.u32[r]=n.lRot(this.u32[r]^this.u32[t],16),this.u32[s]+=this.u32[r],this.u32[e]=n.lRot(this.u32[e]^this.u32[s],12),this.u32[t]+=this.u32[e],this.u32[r]=n.lRot(this.u32[r]^this.u32[t],8),this.u32[s]+=this.u32[r],this.u32[e]=n.lRot(this.u32[e]^this.u32[s],7)}block(){for(let t=this.rounds;t>0;t-=2)this.quartRound(0,4,8,12),this.quartRound(1,5,9,13),this.quartRound(2,6,10,14),this.quartRound(3,7,11,15),this.quartRound(0,5,10,15),this.quartRound(1,6,11,12),this.quartRound(2,7,8,13),this.quartRound(3,4,9,14)}}class l{constructor(n,l,w,d){let y,p;if(this.blockSize=u,t.set(this,new Uint32Array(16)),e.set(this,void 0),s(this,e,new f(d),"f"),16===n.length)y=new Uint8Array(32),y.set(n,0),y.set(n,16),p=c;else{if(32!==n.length)throw new o("should be 16 or 32","key.length",n.length);y=n.slice(),p=a}h("nonce",l).exactly(12).throwNot(),i.i32(y,0,8),r(this,t,"f").set(p);const b=new Uint32Array(y.buffer);r(this,t,"f").set(b,4),r(this,t,"f")[12]=w;const m=l.slice();i.i32(m,0,3);const k=new Uint32Array(m.buffer);r(this,t,"f").set(k,13)}block(){r(this,e,"f").u32.set(r(this,t,"f")),r(this,e,"f").block();for(let s=0;s<16;s++)r(this,e,"f").u32[s]+=r(this,t,"f")[s];r(this,t,"f")[12]+=1}decryptInto(t,s){let i=s.length,o=0;for(t.set(s);i>=u;){this.block();for(let s=0;s<u;s++)t[o++]^=r(this,e,"f").u8[s];i-=u}if(i>0){this.block();for(let s=0;s<i;s++)t[o++]^=r(this,e,"f").u8[s]}}encryptInto(t,s){let i=s.length,o=0;for(s!=t&&t.set(s);i>=u;){this.block();for(let s=0;s<u;s++)t[o++]^=r(this,e,"f").u8[s];i-=u}if(i>0){this.block();for(let s=0;s<i;s++)t[o++]^=r(this,e,"f").u8[s]}}encryptSize(t){return t}}function w(t,e,s,r){h("output",t).atLeast(32).throwNot(),h("key",e).exactly(32).throwNot();const o=new f(r);o.u32.set(a),o.u8.set(e,16),o.u8.set(s.slice(0,16),48),i.i32(o.u8,0,16),o.block();const n=new Uint32Array(t.buffer,t.byteOffset,t.byteLength>>>2);n[0]=o.u32[0],n[1]=o.u32[1],n[2]=o.u32[2],n[3]=o.u32[3],n[4]=o.u32[12],n[5]=o.u32[13],n[6]=o.u32[14],n[7]=o.u32[15],i.i32(t,0,8)}t=new WeakMap,e=new WeakMap;class d extends l{constructor(t,e,s,r){h("nonce",e).exactly(24).throwNot();const i=new Uint8Array(32);w(i,t,e,r);const o=new Uint8Array(12);o.set(e.subarray(16),4),super(i,o,s,r)}}export class ChaCha20 extends l{constructor(t,e,s=0){super(t,e,s,20)}}export function hChaCha20(t,e,s){h("input",s).atLeast(16).throwNot(),w(t,e,s,20)}export class XChaCha20 extends d{constructor(t,e,s=0){super(t,e,s,20)}}