(()=>{"use strict";let e;async function t(e){let t=new ArrayBuffer,n=new TextEncoder,r=await crypto.subtle.deriveKey({name:"HKDF",salt:n.encode("TalkFrameEncryptionKey"),hash:"SHA-256",info:t},e,{name:"AES-GCM",length:128},!1,["encrypt","decrypt"]);return{material:e,encryptionKey:r}}async function n(e){let t=new TextEncoder;return crypto.subtle.deriveBits({name:"HKDF",salt:t.encode("TalkFrameRatchetKey"),hash:"SHA-256",info:new ArrayBuffer},e,256)}async function r(e){return crypto.subtle.importKey("raw",e,"HKDF",!1,["deriveBits","deriveKey"])}let a={key:10,delta:3,undefined:1};class i{constructor({sharedKey:e=!1}={}){this._cryptoKeyRing=Array(16),this._currentKeyIndex=-1,this._sendCounts=new Map,this._sharedKey=e}async setKey(e,n=-1){let a=!1;if(e)if(this._sharedKey)a=e;else{let n=await r(e);a=await t(n)}this._setKeys(a,n)}_setKeys(e,t=-1){t>=0&&(this._currentKeyIndex=t%this._cryptoKeyRing.length),this._cryptoKeyRing[this._currentKeyIndex]=e,this._sendCount=BigInt(0)}encodeFunction(e,t){let n=this._currentKeyIndex;if(this._cryptoKeyRing[n]){let r=this._makeIV(e.getMetadata().synchronizationSource,e.timestamp),i=new Uint8Array(e.data,0,a[e.type]),s=new Uint8Array(2);return s[0]=12,s[1]=n,crypto.subtle.encrypt({name:"AES-GCM",iv:r,additionalData:new Uint8Array(e.data,0,i.byteLength)},this._cryptoKeyRing[n].encryptionKey,new Uint8Array(e.data,a[e.type])).then(n=>{let a=new ArrayBuffer(i.byteLength+n.byteLength+r.byteLength+s.byteLength),y=new Uint8Array(a);return y.set(i),y.set(new Uint8Array(n),i.byteLength),y.set(new Uint8Array(r),i.byteLength+n.byteLength),y.set(s,i.byteLength+n.byteLength+r.byteLength),e.data=a,t.enqueue(e)},e=>{console.error(e)})}t.enqueue(e)}async decodeFunction(e,t){let n=new Uint8Array(e.data)[e.data.byteLength-1];if(this._cryptoKeyRing[n]){let r=await this._decryptFrame(e,n);r&&t.enqueue(r)}}async _decryptFrame(e,i,s,y=0){let{encryptionKey:o}=this._cryptoKeyRing[i],{material:d}=this._cryptoKeyRing[i];try{let t=new Uint8Array(e.data,0,a[e.type]),n=new Uint8Array(e.data,e.data.byteLength-2,2),r=n[0],i=new Uint8Array(e.data,e.data.byteLength-r-n.byteLength,r),s=t.byteLength,y=e.data.byteLength-(t.byteLength+r+n.byteLength),d=await crypto.subtle.decrypt({name:"AES-GCM",iv:i,additionalData:new Uint8Array(e.data,0,t.byteLength)},o,new Uint8Array(e.data,s,y)),c=new ArrayBuffer(t.byteLength+d.byteLength),h=new Uint8Array(c);return h.set(new Uint8Array(e.data,0,t.byteLength)),h.set(new Uint8Array(d),t.byteLength),e.data=c,e}catch(a){if(this._sharedKey)return;if(y<8){let a=this._cryptoKeyRing[this._currentKeyIndex];d=await r(await n(d));let o=await t(d);return this._setKeys(o),await this._decryptFrame(e,i,s||a,y+1)}this._setKeys(s)}}_makeIV(e,t){let n=new ArrayBuffer(12),r=new DataView(n);this._sendCounts.has(e)||this._sendCounts.set(e,Math.floor(65535*Math.random()));let a=this._sendCounts.get(e);return r.setUint32(0,e),r.setUint32(4,t),r.setUint32(8,a%65535),this._sendCounts.set(e,a+1),n}}let s=new Map;function y(t){return e||(s.has(t)||s.set(t,new i),s.get(t))}function o(e,t,n,r){if("encode"===t||"decode"===t){let a=new TransformStream({transform:("encode"===t?e.encodeFunction:e.decodeFunction).bind(e)});n.pipeThrough(a).pipeTo(r)}else console.error(`Invalid operation: ${t}`)}onmessage=async t=>{let{operation:n}=t.data;if("initialize"===n){let{sharedKey:n}=t.data;n&&(e=new i({sharedKey:n}))}else if("encode"===n||"decode"===n){let{readableStream:e,writableStream:r,participantId:a}=t.data;o(y(a),n,e,r)}else if("setKey"===n){let{participantId:e,key:n,keyIndex:r}=t.data,a=y(e);n?a.setKey(n,r):a.setKey(!1,r)}else if("cleanup"===n){let{participantId:e}=t.data;s.delete(e)}else"cleanupAll"===n?s.clear():console.error("e2ee worker",n)},self.RTCTransformEvent&&(self.onrtctransform=e=>{let t=e.transformer,{operation:n,participantId:r}=t.options;o(y(r),n,t.readable,t.writable)})})();
//# sourceMappingURL=talk-src_utils_e2ee_JitsiEncryptionWorker_worker_js.js.map?v=d1b1c4c98fda4b3e