Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hpke-js

Package Overview
Dependencies
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hpke-js - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

76

dist/browser/hpke.js

@@ -158,4 +158,4 @@ var hpke = (() => {

var WebCrypto = class {
constructor(crypto2) {
this._crypto = crypto2;
constructor(crypto) {
this._crypto = crypto;
}

@@ -167,4 +167,4 @@ };

try {
const crypto2 = await import("crypto");
Object.defineProperty(global.self, "crypto", { value: crypto2.webcrypto });
const crypto = await import("crypto");
Object.defineProperty(global.self, "crypto", { value: crypto.webcrypto });
} catch (e) {

@@ -206,4 +206,4 @@ throw new NotSupportedError("Web Cryptograph API not supported");

var ExporterContext = class extends WebCrypto {
constructor(crypto2, kdf, exporterSecret) {
super(crypto2);
constructor(crypto, kdf, exporterSecret) {
super(crypto);
this._kdf = kdf;

@@ -230,4 +230,4 @@ this.exporterSecret = exporterSecret;

var SenderExporterContext = class extends ExporterContext {
constructor(crypto2, kdf, exporterSecret, enc) {
super(crypto2, kdf, exporterSecret);
constructor(crypto, kdf, exporterSecret, enc) {
super(crypto, kdf, exporterSecret);
this.enc = enc;

@@ -240,7 +240,7 @@ return;

var KdfCommon = class extends WebCrypto {
constructor(crypto2, suiteId, algHash) {
constructor(crypto, suiteId, algHash) {
if (algHash.length === void 0) {
throw new Error("Unknown hash size");
}
super(crypto2);
super(crypto);
this.suiteId = suiteId;

@@ -301,3 +301,3 @@ this.algHash = algHash;

async extractAndExpand(salt, ikm, info, len) {
const baseKey = await crypto.subtle.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
const baseKey = await this._crypto.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
return await this._crypto.deriveBits({

@@ -320,3 +320,3 @@ name: "HKDF",

var KdfContext = class extends KdfCommon {
constructor(crypto2, params) {
constructor(crypto, params) {
const suiteId = new Uint8Array(10);

@@ -339,3 +339,3 @@ suiteId.set(SUITE_ID_HEADER_HPKE, 0);

}
super(crypto2, suiteId, algHash);
super(crypto, suiteId, algHash);
switch (params.aead) {

@@ -400,3 +400,3 @@ case 1 /* Aes128Gcm */:

return {
key: await crypto.subtle.importKey("raw", key, { name: this._algAead }, true, AEAD_USAGES),
key: await this._crypto.importKey("raw", key, { name: this._algAead }, true, AEAD_USAGES),
baseNonce: new Uint8Array(baseNonce),

@@ -411,3 +411,3 @@ seq: 0,

var KemContext = class extends KdfCommon {
constructor(crypto2, kem) {
constructor(crypto, kem) {
const suiteId = new Uint8Array(5);

@@ -428,3 +428,3 @@ suiteId.set(SUITE_ID_HEADER_KEM, 0);

}
super(crypto2, suiteId, algHash);
super(crypto, suiteId, algHash);
switch (kem) {

@@ -538,4 +538,4 @@ case 16 /* DhkemP256HkdfSha256 */:

var EncryptionContext = class extends ExporterContext {
constructor(crypto2, kdf, params) {
super(crypto2, kdf, params.exporterSecret);
constructor(crypto, kdf, params) {
super(crypto, kdf, params.exporterSecret);
if (params.key === void 0 || params.baseNonce === void 0 || params.seq === void 0) {

@@ -575,3 +575,3 @@ throw new ValidationError("Required parameters are missing");

};
pt = await window.crypto.subtle.decrypt(alg, this.key, data);
pt = await this._crypto.decrypt(alg, this.key, data);
} catch (e) {

@@ -587,4 +587,4 @@ throw new OpenError(e);

var SenderContext = class extends EncryptionContext {
constructor(crypto2, kdf, params, enc) {
super(crypto2, kdf, params);
constructor(crypto, kdf, params, enc) {
super(crypto, kdf, params);
this.enc = enc;

@@ -601,5 +601,5 @@ return;

};
ct = await window.crypto.subtle.encrypt(alg, this.key, data);
ct = await this._crypto.encrypt(alg, this.key, data);
} catch (e) {
throw new OpenError(e);
throw new SealError(e);
}

@@ -626,5 +626,5 @@ this.incrementSeq();

if (this._kem === void 0 || this._kdf === void 0) {
const crypto2 = await loadSubtleCrypto();
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const crypto = await loadSubtleCrypto();
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -634,6 +634,6 @@ return await this._kem.generateKeyPair();

async createSenderContext(params) {
const crypto2 = await loadSubtleCrypto();
const crypto = await loadSubtleCrypto();
if (this._kem === void 0 || this._kdf === void 0) {
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -647,14 +647,14 @@ const dh = await this._kem.encap(params);

}
const kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const aeadParams = await this._kdf.keySchedule(mode, dh.sharedSecret, params);
if (aeadParams.key === void 0) {
return new SenderExporterContext(crypto2, kdf, aeadParams.exporterSecret, dh.enc);
return new SenderExporterContext(crypto, kdf, aeadParams.exporterSecret, dh.enc);
}
return new SenderContext(crypto2, kdf, aeadParams, dh.enc);
return new SenderContext(crypto, kdf, aeadParams, dh.enc);
}
async createRecipientContext(params) {
const crypto2 = await loadSubtleCrypto();
const crypto = await loadSubtleCrypto();
if (this._kem === void 0 || this._kdf === void 0) {
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -668,8 +668,8 @@ const sharedSecret = await this._kem.decap(params);

}
const kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const aeadParams = await this._kdf.keySchedule(mode, sharedSecret, params);
if (aeadParams.key === void 0) {
return new RecipientExporterContext(crypto2, kdf, aeadParams.exporterSecret);
return new RecipientExporterContext(crypto, kdf, aeadParams.exporterSecret);
}
return new RecipientContext(crypto2, kdf, aeadParams);
return new RecipientContext(crypto, kdf, aeadParams);
}

@@ -676,0 +676,0 @@ };

@@ -1,2 +0,2 @@

var hpke=(()=>{var Ae=Object.create;var N=Object.defineProperty;var we=Object.getOwnPropertyDescriptor;var xe=Object.getOwnPropertyNames;var ke=Object.getPrototypeOf,be=Object.prototype.hasOwnProperty;var z=(n=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(n,{get:(i,e)=>(typeof require!="undefined"?require:i)[e]}):n)(function(n){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+n+'" is not supported')});var Pe=(n,i)=>{for(var e in i)N(n,e,{get:i[e],enumerable:!0})},J=(n,i,e,t)=>{if(i&&typeof i=="object"||typeof i=="function")for(let r of xe(i))!be.call(n,r)&&r!==e&&N(n,r,{get:()=>i[r],enumerable:!(t=we(i,r))||t.enumerable});return n};var Q=(n,i,e)=>(e=n!=null?Ae(ke(n)):{},J(i||!n||!n.__esModule?N(e,"default",{value:n,enumerable:!0}):e,n)),_e=n=>J(N({},"__esModule",{value:!0}),n);var Se={};Pe(Se,{Aead:()=>T,CipherSuite:()=>F,DecapError:()=>C,DeriveKeyPairError:()=>H,DeserializeError:()=>j,EncapError:()=>g,ExportError:()=>U,Kdf:()=>R,Kem:()=>M,MessageLimitReachedError:()=>L,NotSupportedError:()=>h,OpenError:()=>m,RecipientContext:()=>E,SealError:()=>B,SenderContext:()=>S,ValidationError:()=>K});var d=class extends Error{constructor(i){let e;i instanceof Error?e=i.message:typeof i=="string"?e=i:e="",super(e),Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor),this.name=this.constructor.name,this.message===""?this.message=this.name:this.message=this.name+": "+this.message}},K=class extends d{},j=class extends d{},g=class extends d{},C=class extends d{},U=class extends d{},B=class extends d{},m=class extends d{},L=class extends d{},H=class extends d{},h=class extends d{};var M=(t=>(t[t.DhkemP256HkdfSha256=16]="DhkemP256HkdfSha256",t[t.DhkemP384HkdfSha384=17]="DhkemP384HkdfSha384",t[t.DhkemP521HkdfSha512=18]="DhkemP521HkdfSha512",t))(M||{}),R=(t=>(t[t.HkdfSha256=1]="HkdfSha256",t[t.HkdfSha384=2]="HkdfSha384",t[t.HkdfSha512=3]="HkdfSha512",t))(R||{}),T=(t=>(t[t.Aes128Gcm=1]="Aes128Gcm",t[t.Aes256Gcm=2]="Aes256Gcm",t[t.ExportOnly=65535]="ExportOnly",t))(T||{});var Z=()=>typeof window!="undefined"&&typeof window.document!="undefined",I=n=>typeof n=="object"&&n!==null&&typeof n.privateKey=="object"&&typeof n.publicKey=="object";function l(n,i){if(i<=0)throw new Error("i2Osp: too small size");if(n>=256**i)throw new Error("i2Osp: too large integer");let e=new Uint8Array(i);for(let t=0;t<i&&n;t++)e[i-(t+1)]=n%256,n=n>>8;return e}function $(n,i){if(n.byteLength!==i.byteLength)throw new Error("xor: different length inputs");let e=new Uint8Array(n.byteLength);for(let t=0;t<n.byteLength;t++)e[t]=n[t]^i[t];return e}function D(n,i){let e=new Uint8Array(n.length+i.length);return e.set(n,0),e.set(i,n.length),e}function ee(n,i,e){let t=new Uint8Array(n.length+i.length+e.length);return t.set(n,0),t.set(i,n.length),t.set(e,n.length+i.length),t}var x=class{constructor(i){this._crypto=i}};async function G(){if(Z()){if(window.crypto===void 0)try{let n=await import("crypto");Object.defineProperty(global.self,"crypto",{value:n.webcrypto})}catch{throw new h("Web Cryptograph API not supported")}return window.crypto.subtle}try{let{webcrypto:n}=await import("node:crypto");return n.subtle}catch{throw new h("Web Cryptograph API not supported")}}var V=["deriveKey","deriveBits"],te=["encrypt","decrypt"],p=new Uint8Array(0),W=new Uint8Array([72,80,75,69,45,118,49]),re=new Uint8Array([72,80,75,69]),ne=new Uint8Array([75,69,77]),Le=new Uint8Array([100,107,112,95,112,114,107]),ie=new Uint8Array([101,97,101,95,112,114,107]),se=new Uint8Array([105,110,102,111,95,104,97,115,104]),oe=new Uint8Array([112,115,107,95,105,100,95,104,97,115,104]),ae=new Uint8Array([115,101,99,114,101,116]),ce=new Uint8Array([115,104,97,114,101,100,95,115,101,99,114,101,116]),ye=new Uint8Array([107,101,121]),pe=new Uint8Array([98,97,115,101,95,110,111,110,99,101]),de=new Uint8Array([101,120,112]),he=new Uint8Array([115,101,99]),He=new Uint8Array([99,97,110,100,105,100,97,116,101]),Ie=new Uint8Array([115,107]);var k=class extends x{constructor(e,t,r){super(e);this._kdf=t,this.exporterSecret=r}async seal(e,t){throw new B("Not available on export-only mode")}async open(e,t){throw new m("Not available on export-only mode")}async export(e,t){try{return await this._kdf.labeledExpand(this.exporterSecret,he,new Uint8Array(e),t)}catch(r){throw new U(r)}}},q=class extends k{},Y=class extends k{constructor(e,t,r,s){super(e,t,r);this.enc=s}};var b=class extends x{constructor(e,t,r){if(r.length===void 0)throw new Error("Unknown hash size");super(e);this.suiteId=t,this.algHash=r,this._nH=r.length/8}buildLabeledIkm(e,t){let r=new Uint8Array(7+this.suiteId.byteLength+e.byteLength+t.byteLength);return r.set(W,0),r.set(this.suiteId,7),r.set(e,7+this.suiteId.byteLength),r.set(t,7+this.suiteId.byteLength+e.byteLength),r}buildLabeledInfo(e,t,r){let s=new Uint8Array(9+this.suiteId.byteLength+e.byteLength+t.byteLength);return s.set(new Uint8Array([0,r]),0),s.set(W,2),s.set(this.suiteId,9),s.set(e,9+this.suiteId.byteLength),s.set(t,9+this.suiteId.byteLength+e.byteLength),s}async extract(e,t){e.byteLength===0&&(e=new ArrayBuffer(this._nH));let r=await this._crypto.importKey("raw",e,this.algHash,!1,["sign"]);return await this._crypto.sign("HMAC",r,t)}async expand(e,t,r){let s=await this._crypto.importKey("raw",e,this.algHash,!1,["sign"]),o=new ArrayBuffer(r),y=new Uint8Array(o),a=p,c=new Uint8Array(t),f=new Uint8Array(1);if(r>255*this._nH)throw new Error("Entropy limit reached");let u=new Uint8Array(this._nH+c.length+1);for(let v=1;y.length>0;v++)f[0]=v,u.set(a,0),u.set(c,a.length),u.set(f,a.length+c.length),a=new Uint8Array(await this._crypto.sign("HMAC",s,u.slice(0,a.length+c.length+1))),y.length>=a.length?y.set(a,0):y.set(a.slice(0,a.length-y.length),0),y=y.slice(this._nH);return o}async extractAndExpand(e,t,r,s){let o=await crypto.subtle.importKey("raw",t,"HKDF",!1,["deriveBits"]);return await this._crypto.deriveBits({name:"HKDF",hash:this.algHash.hash,salt:e,info:r},o,s*8)}async labeledExtract(e,t,r){return await this.extract(e,this.buildLabeledIkm(t,r))}async labeledExpand(e,t,r,s){return await this.expand(e,this.buildLabeledInfo(t,r,s),s)}};var A=class extends b{constructor(e,t){let r=new Uint8Array(10);r.set(re,0),r.set(l(t.kem,2),4),r.set(l(t.kdf,2),6),r.set(l(t.aead,2),8);let s;switch(t.kdf){case 1:s={name:"HMAC",hash:"SHA-256",length:256};break;case 2:s={name:"HMAC",hash:"SHA-384",length:384};break;case 3:s={name:"HMAC",hash:"SHA-512",length:512};break}super(e,r,s);switch(t.aead){case 1:this._nK=16,this._nN=12,this._nT=16,this._algAead="AES-GCM";break;case 2:this._nK=32,this._nN=12,this._nT=16,this._algAead="AES-GCM";break;case 65535:this._nK=0,this._nN=0,this._nT=0,this._algAead="";break}}verifyPskInputs(e,t){let r=t.psk!==void 0,s=t.psk!==void 0&&t.psk.id.byteLength>0;if(r!==s)throw new Error("Inconsistent PSK inputs");if(r&&(e===0||e===2))throw new Error("PSK input provided when not needed");if(!r&&(e===1||e===3))throw new Error("Missing required PSK input")}async keySchedule(e,t,r){let s=r.psk===void 0?p:new Uint8Array(r.psk.id),o=await this.labeledExtract(p,oe,s),y=r.info===void 0?p:new Uint8Array(r.info),a=await this.labeledExtract(p,se,y),c=new Uint8Array(1+o.byteLength+a.byteLength);c.set(new Uint8Array([e]),0),c.set(new Uint8Array(o),1),c.set(new Uint8Array(a),1+o.byteLength);let f=r.psk===void 0?p:new Uint8Array(r.psk.key),u=this.buildLabeledIkm(ae,f),v=this.buildLabeledInfo(de,c,this._nH),X=await this.extractAndExpand(t,u,v,this._nH);if(this._algAead==="")return{exporterSecret:X};let fe=this.buildLabeledInfo(ye,c,this._nK),ue=await this.extractAndExpand(t,u,fe,this._nK),me=this.buildLabeledInfo(pe,c,this._nN),le=await this.extractAndExpand(t,u,me,this._nN);return{key:await crypto.subtle.importKey("raw",ue,{name:this._algAead},!0,te),baseNonce:new Uint8Array(le),seq:0,exporterSecret:X}}};var P=class extends b{constructor(e,t){let r=new Uint8Array(5);r.set(ne,0),r.set(l(t,2),3);let s;switch(t){case 16:s={name:"HMAC",hash:"SHA-256",length:256};break;case 17:s={name:"HMAC",hash:"SHA-384",length:384};break;case 18:s={name:"HMAC",hash:"SHA-512",length:512};break}super(e,r,s);switch(t){case 16:this._algKeyGen={name:"ECDH",namedCurve:"P-256"},this._nSecret=32,this._nPk=65,this._nDh=32;break;case 17:this._algKeyGen={name:"ECDH",namedCurve:"P-384"},this._nSecret=48,this._nPk=97,this._nDh=48;break;case 18:this._algKeyGen={name:"ECDH",namedCurve:"P-521"},this._nSecret=64,this._nPk=133,this._nDh=66;break}}async generateKeyPair(){try{return await this._crypto.generateKey(this._algKeyGen,!0,V)}catch(e){throw new H(e)}}async deriveKeyPair(e){throw new h("deriveKeyPair not supported")}async encap(e){let t=e.nonEphemeralKeyPair===void 0?await this.generateKeyPair():e.nonEphemeralKeyPair,r=await this._crypto.exportKey("raw",t.publicKey),s=await this._crypto.exportKey("raw",e.recipientPublicKey);try{let o;if(e.senderKey===void 0)o=await this.dh(t.privateKey,e.recipientPublicKey);else{let c=I(e.senderKey)?e.senderKey.privateKey:e.senderKey,f=await this.dh(t.privateKey,e.recipientPublicKey),u=await this.dh(c,e.recipientPublicKey);o=D(f,u)}let y;if(e.senderKey===void 0)y=D(new Uint8Array(r),new Uint8Array(s));else{let c=I(e.senderKey)?e.senderKey.publicKey:e.senderKey,f=await this._crypto.exportKey("raw",c);y=ee(new Uint8Array(r),new Uint8Array(s),new Uint8Array(f))}let a=await this.generateSharedSecret(o,y);return{enc:r,sharedSecret:a}}catch(o){throw new g(o)}}async decap(e){let t=await this._crypto.importKey("raw",e.enc,this._algKeyGen,!0,V),r=I(e.recipientKey)?e.recipientKey.privateKey:e.recipientKey,s=I(e.recipientKey)?e.recipientKey.publicKey:e.recipientKey,o=await this._crypto.exportKey("raw",s);try{let y;if(e.senderPublicKey===void 0)y=await this.dh(r,t);else{let c=await this.dh(r,t),f=await this.dh(r,e.senderPublicKey);y=D(c,f)}let a;if(e.senderPublicKey===void 0)a=D(new Uint8Array(e.enc),new Uint8Array(o));else{let c=await this._crypto.exportKey("raw",e.senderPublicKey);a=new Uint8Array(e.enc.byteLength+o.byteLength+c.byteLength),a.set(new Uint8Array(e.enc),0),a.set(new Uint8Array(o),e.enc.byteLength),a.set(new Uint8Array(c),e.enc.byteLength+o.byteLength)}return await this.generateSharedSecret(y,a)}catch(y){throw new C(y)}}async dh(e,t){let r=await this._crypto.deriveBits({name:"ECDH",public:t},e,this._nDh*8);return new Uint8Array(r)}async generateSharedSecret(e,t){let r=this.buildLabeledIkm(ie,e),s=this.buildLabeledInfo(ce,t,this._nSecret);return await this.extractAndExpand(p,r,s,this._nSecret)}};var _=class extends k{constructor(e,t,r){super(e,t,r.exporterSecret);if(r.key===void 0||r.baseNonce===void 0||r.seq===void 0)throw new K("Required parameters are missing");this.key=r.key,this.baseNonce=r.baseNonce,this.seq=r.seq}computeNonce(){let e=l(this.seq,this.baseNonce.byteLength);return $(this.baseNonce,e)}incrementSeq(){if(this.seq>=Number.MAX_SAFE_INTEGER)throw new L("Message limit reached");this.seq+=1}};var E=class extends _{async seal(i,e=p){throw new h("Bidirectional encryption not supported")}async open(i,e=p){let t;try{let r={name:"AES-GCM",iv:this.computeNonce(),additionalData:e};t=await window.crypto.subtle.decrypt(r,this.key,i)}catch(r){throw new m(r)}return this.incrementSeq(),t}};var S=class extends _{constructor(e,t,r,s){super(e,t,r);this.enc=s}async seal(e,t=p){let r;try{let s={name:"AES-GCM",iv:this.computeNonce(),additionalData:t};r=await window.crypto.subtle.encrypt(s,this.key,e)}catch(s){throw new m(s)}return this.incrementSeq(),r}async open(e,t=p){throw new h("Bidirectional encryption not supported")}};var F=class{constructor(i){this.kem=i.kem,this.kdf=i.kdf,this.aead=i.aead,this._kem=void 0,this._kdf=void 0}async generateKeyPair(){if(this._kem===void 0||this._kdf===void 0){let i=await G();this._kem=new P(i,this.kem),this._kdf=new A(i,{kem:this.kem,kdf:this.kdf,aead:this.aead})}return await this._kem.generateKeyPair()}async createSenderContext(i){let e=await G();(this._kem===void 0||this._kdf===void 0)&&(this._kem=new P(e,this.kem),this._kdf=new A(e,{kem:this.kem,kdf:this.kdf,aead:this.aead}));let t=await this._kem.encap(i),r;i.psk!==void 0?r=i.senderKey!==void 0?3:1:r=i.senderKey!==void 0?2:0;let s=new A(e,{kem:this.kem,kdf:this.kdf,aead:this.aead}),o=await this._kdf.keySchedule(r,t.sharedSecret,i);return o.key===void 0?new Y(e,s,o.exporterSecret,t.enc):new S(e,s,o,t.enc)}async createRecipientContext(i){let e=await G();(this._kem===void 0||this._kdf===void 0)&&(this._kem=new P(e,this.kem),this._kdf=new A(e,{kem:this.kem,kdf:this.kdf,aead:this.aead}));let t=await this._kem.decap(i),r;i.psk!==void 0?r=i.senderPublicKey!==void 0?3:1:r=i.senderPublicKey!==void 0?2:0;let s=new A(e,{kem:this.kem,kdf:this.kdf,aead:this.aead}),o=await this._kdf.keySchedule(r,t,i);return o.key===void 0?new q(e,s,o.exporterSecret):new E(e,s,o)}};return _e(Se);})();
var hpke=(()=>{var Ae=Object.create;var N=Object.defineProperty;var we=Object.getOwnPropertyDescriptor;var xe=Object.getOwnPropertyNames;var ke=Object.getPrototypeOf,be=Object.prototype.hasOwnProperty;var z=(n=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(n,{get:(i,e)=>(typeof require!="undefined"?require:i)[e]}):n)(function(n){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+n+'" is not supported')});var Pe=(n,i)=>{for(var e in i)N(n,e,{get:i[e],enumerable:!0})},J=(n,i,e,t)=>{if(i&&typeof i=="object"||typeof i=="function")for(let r of xe(i))!be.call(n,r)&&r!==e&&N(n,r,{get:()=>i[r],enumerable:!(t=we(i,r))||t.enumerable});return n};var Q=(n,i,e)=>(e=n!=null?Ae(ke(n)):{},J(i||!n||!n.__esModule?N(e,"default",{value:n,enumerable:!0}):e,n)),_e=n=>J(N({},"__esModule",{value:!0}),n);var Se={};Pe(Se,{Aead:()=>T,CipherSuite:()=>F,DecapError:()=>U,DeriveKeyPairError:()=>H,DeserializeError:()=>j,EncapError:()=>C,ExportError:()=>B,Kdf:()=>R,Kem:()=>M,MessageLimitReachedError:()=>L,NotSupportedError:()=>h,OpenError:()=>w,RecipientContext:()=>S,SealError:()=>A,SenderContext:()=>K,ValidationError:()=>g});var d=class extends Error{constructor(i){let e;i instanceof Error?e=i.message:typeof i=="string"?e=i:e="",super(e),Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor),this.name=this.constructor.name,this.message===""?this.message=this.name:this.message=this.name+": "+this.message}},g=class extends d{},j=class extends d{},C=class extends d{},U=class extends d{},B=class extends d{},A=class extends d{},w=class extends d{},L=class extends d{},H=class extends d{},h=class extends d{};var M=(t=>(t[t.DhkemP256HkdfSha256=16]="DhkemP256HkdfSha256",t[t.DhkemP384HkdfSha384=17]="DhkemP384HkdfSha384",t[t.DhkemP521HkdfSha512=18]="DhkemP521HkdfSha512",t))(M||{}),R=(t=>(t[t.HkdfSha256=1]="HkdfSha256",t[t.HkdfSha384=2]="HkdfSha384",t[t.HkdfSha512=3]="HkdfSha512",t))(R||{}),T=(t=>(t[t.Aes128Gcm=1]="Aes128Gcm",t[t.Aes256Gcm=2]="Aes256Gcm",t[t.ExportOnly=65535]="ExportOnly",t))(T||{});var Z=()=>typeof window!="undefined"&&typeof window.document!="undefined",I=n=>typeof n=="object"&&n!==null&&typeof n.privateKey=="object"&&typeof n.publicKey=="object";function m(n,i){if(i<=0)throw new Error("i2Osp: too small size");if(n>=256**i)throw new Error("i2Osp: too large integer");let e=new Uint8Array(i);for(let t=0;t<i&&n;t++)e[i-(t+1)]=n%256,n=n>>8;return e}function $(n,i){if(n.byteLength!==i.byteLength)throw new Error("xor: different length inputs");let e=new Uint8Array(n.byteLength);for(let t=0;t<n.byteLength;t++)e[t]=n[t]^i[t];return e}function D(n,i){let e=new Uint8Array(n.length+i.length);return e.set(n,0),e.set(i,n.length),e}function ee(n,i,e){let t=new Uint8Array(n.length+i.length+e.length);return t.set(n,0),t.set(i,n.length),t.set(e,n.length+i.length),t}var k=class{constructor(i){this._crypto=i}};async function G(){if(Z()){if(window.crypto===void 0)try{let n=await import("crypto");Object.defineProperty(global.self,"crypto",{value:n.webcrypto})}catch{throw new h("Web Cryptograph API not supported")}return window.crypto.subtle}try{let{webcrypto:n}=await import("node:crypto");return n.subtle}catch{throw new h("Web Cryptograph API not supported")}}var V=["deriveKey","deriveBits"],te=["encrypt","decrypt"],p=new Uint8Array(0),W=new Uint8Array([72,80,75,69,45,118,49]),re=new Uint8Array([72,80,75,69]),ne=new Uint8Array([75,69,77]),Le=new Uint8Array([100,107,112,95,112,114,107]),ie=new Uint8Array([101,97,101,95,112,114,107]),se=new Uint8Array([105,110,102,111,95,104,97,115,104]),oe=new Uint8Array([112,115,107,95,105,100,95,104,97,115,104]),ae=new Uint8Array([115,101,99,114,101,116]),ce=new Uint8Array([115,104,97,114,101,100,95,115,101,99,114,101,116]),ye=new Uint8Array([107,101,121]),pe=new Uint8Array([98,97,115,101,95,110,111,110,99,101]),de=new Uint8Array([101,120,112]),he=new Uint8Array([115,101,99]),He=new Uint8Array([99,97,110,100,105,100,97,116,101]),Ie=new Uint8Array([115,107]);var b=class extends k{constructor(e,t,r){super(e);this._kdf=t,this.exporterSecret=r}async seal(e,t){throw new A("Not available on export-only mode")}async open(e,t){throw new w("Not available on export-only mode")}async export(e,t){try{return await this._kdf.labeledExpand(this.exporterSecret,he,new Uint8Array(e),t)}catch(r){throw new B(r)}}},q=class extends b{},Y=class extends b{constructor(e,t,r,s){super(e,t,r);this.enc=s}};var P=class extends k{constructor(e,t,r){if(r.length===void 0)throw new Error("Unknown hash size");super(e);this.suiteId=t,this.algHash=r,this._nH=r.length/8}buildLabeledIkm(e,t){let r=new Uint8Array(7+this.suiteId.byteLength+e.byteLength+t.byteLength);return r.set(W,0),r.set(this.suiteId,7),r.set(e,7+this.suiteId.byteLength),r.set(t,7+this.suiteId.byteLength+e.byteLength),r}buildLabeledInfo(e,t,r){let s=new Uint8Array(9+this.suiteId.byteLength+e.byteLength+t.byteLength);return s.set(new Uint8Array([0,r]),0),s.set(W,2),s.set(this.suiteId,9),s.set(e,9+this.suiteId.byteLength),s.set(t,9+this.suiteId.byteLength+e.byteLength),s}async extract(e,t){e.byteLength===0&&(e=new ArrayBuffer(this._nH));let r=await this._crypto.importKey("raw",e,this.algHash,!1,["sign"]);return await this._crypto.sign("HMAC",r,t)}async expand(e,t,r){let s=await this._crypto.importKey("raw",e,this.algHash,!1,["sign"]),o=new ArrayBuffer(r),y=new Uint8Array(o),a=p,c=new Uint8Array(t),f=new Uint8Array(1);if(r>255*this._nH)throw new Error("Entropy limit reached");let u=new Uint8Array(this._nH+c.length+1);for(let v=1;y.length>0;v++)f[0]=v,u.set(a,0),u.set(c,a.length),u.set(f,a.length+c.length),a=new Uint8Array(await this._crypto.sign("HMAC",s,u.slice(0,a.length+c.length+1))),y.length>=a.length?y.set(a,0):y.set(a.slice(0,a.length-y.length),0),y=y.slice(this._nH);return o}async extractAndExpand(e,t,r,s){let o=await this._crypto.importKey("raw",t,"HKDF",!1,["deriveBits"]);return await this._crypto.deriveBits({name:"HKDF",hash:this.algHash.hash,salt:e,info:r},o,s*8)}async labeledExtract(e,t,r){return await this.extract(e,this.buildLabeledIkm(t,r))}async labeledExpand(e,t,r,s){return await this.expand(e,this.buildLabeledInfo(t,r,s),s)}};var l=class extends P{constructor(e,t){let r=new Uint8Array(10);r.set(re,0),r.set(m(t.kem,2),4),r.set(m(t.kdf,2),6),r.set(m(t.aead,2),8);let s;switch(t.kdf){case 1:s={name:"HMAC",hash:"SHA-256",length:256};break;case 2:s={name:"HMAC",hash:"SHA-384",length:384};break;case 3:s={name:"HMAC",hash:"SHA-512",length:512};break}super(e,r,s);switch(t.aead){case 1:this._nK=16,this._nN=12,this._nT=16,this._algAead="AES-GCM";break;case 2:this._nK=32,this._nN=12,this._nT=16,this._algAead="AES-GCM";break;case 65535:this._nK=0,this._nN=0,this._nT=0,this._algAead="";break}}verifyPskInputs(e,t){let r=t.psk!==void 0,s=t.psk!==void 0&&t.psk.id.byteLength>0;if(r!==s)throw new Error("Inconsistent PSK inputs");if(r&&(e===0||e===2))throw new Error("PSK input provided when not needed");if(!r&&(e===1||e===3))throw new Error("Missing required PSK input")}async keySchedule(e,t,r){let s=r.psk===void 0?p:new Uint8Array(r.psk.id),o=await this.labeledExtract(p,oe,s),y=r.info===void 0?p:new Uint8Array(r.info),a=await this.labeledExtract(p,se,y),c=new Uint8Array(1+o.byteLength+a.byteLength);c.set(new Uint8Array([e]),0),c.set(new Uint8Array(o),1),c.set(new Uint8Array(a),1+o.byteLength);let f=r.psk===void 0?p:new Uint8Array(r.psk.key),u=this.buildLabeledIkm(ae,f),v=this.buildLabeledInfo(de,c,this._nH),X=await this.extractAndExpand(t,u,v,this._nH);if(this._algAead==="")return{exporterSecret:X};let fe=this.buildLabeledInfo(ye,c,this._nK),ue=await this.extractAndExpand(t,u,fe,this._nK),me=this.buildLabeledInfo(pe,c,this._nN),le=await this.extractAndExpand(t,u,me,this._nN);return{key:await this._crypto.importKey("raw",ue,{name:this._algAead},!0,te),baseNonce:new Uint8Array(le),seq:0,exporterSecret:X}}};var _=class extends P{constructor(e,t){let r=new Uint8Array(5);r.set(ne,0),r.set(m(t,2),3);let s;switch(t){case 16:s={name:"HMAC",hash:"SHA-256",length:256};break;case 17:s={name:"HMAC",hash:"SHA-384",length:384};break;case 18:s={name:"HMAC",hash:"SHA-512",length:512};break}super(e,r,s);switch(t){case 16:this._algKeyGen={name:"ECDH",namedCurve:"P-256"},this._nSecret=32,this._nPk=65,this._nDh=32;break;case 17:this._algKeyGen={name:"ECDH",namedCurve:"P-384"},this._nSecret=48,this._nPk=97,this._nDh=48;break;case 18:this._algKeyGen={name:"ECDH",namedCurve:"P-521"},this._nSecret=64,this._nPk=133,this._nDh=66;break}}async generateKeyPair(){try{return await this._crypto.generateKey(this._algKeyGen,!0,V)}catch(e){throw new H(e)}}async deriveKeyPair(e){throw new h("deriveKeyPair not supported")}async encap(e){let t=e.nonEphemeralKeyPair===void 0?await this.generateKeyPair():e.nonEphemeralKeyPair,r=await this._crypto.exportKey("raw",t.publicKey),s=await this._crypto.exportKey("raw",e.recipientPublicKey);try{let o;if(e.senderKey===void 0)o=await this.dh(t.privateKey,e.recipientPublicKey);else{let c=I(e.senderKey)?e.senderKey.privateKey:e.senderKey,f=await this.dh(t.privateKey,e.recipientPublicKey),u=await this.dh(c,e.recipientPublicKey);o=D(f,u)}let y;if(e.senderKey===void 0)y=D(new Uint8Array(r),new Uint8Array(s));else{let c=I(e.senderKey)?e.senderKey.publicKey:e.senderKey,f=await this._crypto.exportKey("raw",c);y=ee(new Uint8Array(r),new Uint8Array(s),new Uint8Array(f))}let a=await this.generateSharedSecret(o,y);return{enc:r,sharedSecret:a}}catch(o){throw new C(o)}}async decap(e){let t=await this._crypto.importKey("raw",e.enc,this._algKeyGen,!0,V),r=I(e.recipientKey)?e.recipientKey.privateKey:e.recipientKey,s=I(e.recipientKey)?e.recipientKey.publicKey:e.recipientKey,o=await this._crypto.exportKey("raw",s);try{let y;if(e.senderPublicKey===void 0)y=await this.dh(r,t);else{let c=await this.dh(r,t),f=await this.dh(r,e.senderPublicKey);y=D(c,f)}let a;if(e.senderPublicKey===void 0)a=D(new Uint8Array(e.enc),new Uint8Array(o));else{let c=await this._crypto.exportKey("raw",e.senderPublicKey);a=new Uint8Array(e.enc.byteLength+o.byteLength+c.byteLength),a.set(new Uint8Array(e.enc),0),a.set(new Uint8Array(o),e.enc.byteLength),a.set(new Uint8Array(c),e.enc.byteLength+o.byteLength)}return await this.generateSharedSecret(y,a)}catch(y){throw new U(y)}}async dh(e,t){let r=await this._crypto.deriveBits({name:"ECDH",public:t},e,this._nDh*8);return new Uint8Array(r)}async generateSharedSecret(e,t){let r=this.buildLabeledIkm(ie,e),s=this.buildLabeledInfo(ce,t,this._nSecret);return await this.extractAndExpand(p,r,s,this._nSecret)}};var E=class extends b{constructor(e,t,r){super(e,t,r.exporterSecret);if(r.key===void 0||r.baseNonce===void 0||r.seq===void 0)throw new g("Required parameters are missing");this.key=r.key,this.baseNonce=r.baseNonce,this.seq=r.seq}computeNonce(){let e=m(this.seq,this.baseNonce.byteLength);return $(this.baseNonce,e)}incrementSeq(){if(this.seq>=Number.MAX_SAFE_INTEGER)throw new L("Message limit reached");this.seq+=1}};var S=class extends E{async seal(i,e=p){throw new h("Bidirectional encryption not supported")}async open(i,e=p){let t;try{let r={name:"AES-GCM",iv:this.computeNonce(),additionalData:e};t=await this._crypto.decrypt(r,this.key,i)}catch(r){throw new w(r)}return this.incrementSeq(),t}};var K=class extends E{constructor(e,t,r,s){super(e,t,r);this.enc=s}async seal(e,t=p){let r;try{let s={name:"AES-GCM",iv:this.computeNonce(),additionalData:t};r=await this._crypto.encrypt(s,this.key,e)}catch(s){throw new A(s)}return this.incrementSeq(),r}async open(e,t=p){throw new h("Bidirectional encryption not supported")}};var F=class{constructor(i){this.kem=i.kem,this.kdf=i.kdf,this.aead=i.aead,this._kem=void 0,this._kdf=void 0}async generateKeyPair(){if(this._kem===void 0||this._kdf===void 0){let i=await G();this._kem=new _(i,this.kem),this._kdf=new l(i,{kem:this.kem,kdf:this.kdf,aead:this.aead})}return await this._kem.generateKeyPair()}async createSenderContext(i){let e=await G();(this._kem===void 0||this._kdf===void 0)&&(this._kem=new _(e,this.kem),this._kdf=new l(e,{kem:this.kem,kdf:this.kdf,aead:this.aead}));let t=await this._kem.encap(i),r;i.psk!==void 0?r=i.senderKey!==void 0?3:1:r=i.senderKey!==void 0?2:0;let s=new l(e,{kem:this.kem,kdf:this.kdf,aead:this.aead}),o=await this._kdf.keySchedule(r,t.sharedSecret,i);return o.key===void 0?new Y(e,s,o.exporterSecret,t.enc):new K(e,s,o,t.enc)}async createRecipientContext(i){let e=await G();(this._kem===void 0||this._kdf===void 0)&&(this._kem=new _(e,this.kem),this._kdf=new l(e,{kem:this.kem,kdf:this.kdf,aead:this.aead}));let t=await this._kem.decap(i),r;i.psk!==void 0?r=i.senderPublicKey!==void 0?3:1:r=i.senderPublicKey!==void 0?2:0;let s=new l(e,{kem:this.kem,kdf:this.kdf,aead:this.aead}),o=await this._kdf.keySchedule(r,t,i);return o.key===void 0?new q(e,s,o.exporterSecret):new S(e,s,o)}};return _e(Se);})();
//# sourceMappingURL=hpke.min.js.map

@@ -108,4 +108,4 @@ // src/errors.ts

var WebCrypto = class {
constructor(crypto2) {
this._crypto = crypto2;
constructor(crypto) {
this._crypto = crypto;
}

@@ -117,4 +117,4 @@ };

try {
const crypto2 = await import("crypto");
Object.defineProperty(global.self, "crypto", { value: crypto2.webcrypto });
const crypto = await import("crypto");
Object.defineProperty(global.self, "crypto", { value: crypto.webcrypto });
} catch (e) {

@@ -156,4 +156,4 @@ throw new NotSupportedError("Web Cryptograph API not supported");

var ExporterContext = class extends WebCrypto {
constructor(crypto2, kdf, exporterSecret) {
super(crypto2);
constructor(crypto, kdf, exporterSecret) {
super(crypto);
this._kdf = kdf;

@@ -180,4 +180,4 @@ this.exporterSecret = exporterSecret;

var SenderExporterContext = class extends ExporterContext {
constructor(crypto2, kdf, exporterSecret, enc) {
super(crypto2, kdf, exporterSecret);
constructor(crypto, kdf, exporterSecret, enc) {
super(crypto, kdf, exporterSecret);
this.enc = enc;

@@ -190,7 +190,7 @@ return;

var KdfCommon = class extends WebCrypto {
constructor(crypto2, suiteId, algHash) {
constructor(crypto, suiteId, algHash) {
if (algHash.length === void 0) {
throw new Error("Unknown hash size");
}
super(crypto2);
super(crypto);
this.suiteId = suiteId;

@@ -251,3 +251,3 @@ this.algHash = algHash;

async extractAndExpand(salt, ikm, info, len) {
const baseKey = await crypto.subtle.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
const baseKey = await this._crypto.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
return await this._crypto.deriveBits({

@@ -270,3 +270,3 @@ name: "HKDF",

var KdfContext = class extends KdfCommon {
constructor(crypto2, params) {
constructor(crypto, params) {
const suiteId = new Uint8Array(10);

@@ -289,3 +289,3 @@ suiteId.set(SUITE_ID_HEADER_HPKE, 0);

}
super(crypto2, suiteId, algHash);
super(crypto, suiteId, algHash);
switch (params.aead) {

@@ -350,3 +350,3 @@ case 1 /* Aes128Gcm */:

return {
key: await crypto.subtle.importKey("raw", key, { name: this._algAead }, true, AEAD_USAGES),
key: await this._crypto.importKey("raw", key, { name: this._algAead }, true, AEAD_USAGES),
baseNonce: new Uint8Array(baseNonce),

@@ -361,3 +361,3 @@ seq: 0,

var KemContext = class extends KdfCommon {
constructor(crypto2, kem) {
constructor(crypto, kem) {
const suiteId = new Uint8Array(5);

@@ -378,3 +378,3 @@ suiteId.set(SUITE_ID_HEADER_KEM, 0);

}
super(crypto2, suiteId, algHash);
super(crypto, suiteId, algHash);
switch (kem) {

@@ -488,4 +488,4 @@ case 16 /* DhkemP256HkdfSha256 */:

var EncryptionContext = class extends ExporterContext {
constructor(crypto2, kdf, params) {
super(crypto2, kdf, params.exporterSecret);
constructor(crypto, kdf, params) {
super(crypto, kdf, params.exporterSecret);
if (params.key === void 0 || params.baseNonce === void 0 || params.seq === void 0) {

@@ -525,3 +525,3 @@ throw new ValidationError("Required parameters are missing");

};
pt = await window.crypto.subtle.decrypt(alg, this.key, data);
pt = await this._crypto.decrypt(alg, this.key, data);
} catch (e) {

@@ -537,4 +537,4 @@ throw new OpenError(e);

var SenderContext = class extends EncryptionContext {
constructor(crypto2, kdf, params, enc) {
super(crypto2, kdf, params);
constructor(crypto, kdf, params, enc) {
super(crypto, kdf, params);
this.enc = enc;

@@ -551,5 +551,5 @@ return;

};
ct = await window.crypto.subtle.encrypt(alg, this.key, data);
ct = await this._crypto.encrypt(alg, this.key, data);
} catch (e) {
throw new OpenError(e);
throw new SealError(e);
}

@@ -576,5 +576,5 @@ this.incrementSeq();

if (this._kem === void 0 || this._kdf === void 0) {
const crypto2 = await loadSubtleCrypto();
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const crypto = await loadSubtleCrypto();
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -584,6 +584,6 @@ return await this._kem.generateKeyPair();

async createSenderContext(params) {
const crypto2 = await loadSubtleCrypto();
const crypto = await loadSubtleCrypto();
if (this._kem === void 0 || this._kdf === void 0) {
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -597,14 +597,14 @@ const dh = await this._kem.encap(params);

}
const kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const aeadParams = await this._kdf.keySchedule(mode, dh.sharedSecret, params);
if (aeadParams.key === void 0) {
return new SenderExporterContext(crypto2, kdf, aeadParams.exporterSecret, dh.enc);
return new SenderExporterContext(crypto, kdf, aeadParams.exporterSecret, dh.enc);
}
return new SenderContext(crypto2, kdf, aeadParams, dh.enc);
return new SenderContext(crypto, kdf, aeadParams, dh.enc);
}
async createRecipientContext(params) {
const crypto2 = await loadSubtleCrypto();
const crypto = await loadSubtleCrypto();
if (this._kem === void 0 || this._kdf === void 0) {
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -618,8 +618,8 @@ const sharedSecret = await this._kem.decap(params);

}
const kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const aeadParams = await this._kdf.keySchedule(mode, sharedSecret, params);
if (aeadParams.key === void 0) {
return new RecipientExporterContext(crypto2, kdf, aeadParams.exporterSecret);
return new RecipientExporterContext(crypto, kdf, aeadParams.exporterSecret);
}
return new RecipientContext(crypto2, kdf, aeadParams);
return new RecipientContext(crypto, kdf, aeadParams);
}

@@ -626,0 +626,0 @@ };

@@ -151,4 +151,4 @@ var __create = Object.create;

var WebCrypto = class {
constructor(crypto2) {
this._crypto = crypto2;
constructor(crypto) {
this._crypto = crypto;
}

@@ -160,4 +160,4 @@ };

try {
const crypto2 = await import("crypto");
Object.defineProperty(global.self, "crypto", { value: crypto2.webcrypto });
const crypto = await import("crypto");
Object.defineProperty(global.self, "crypto", { value: crypto.webcrypto });
} catch (e) {

@@ -199,4 +199,4 @@ throw new NotSupportedError("Web Cryptograph API not supported");

var ExporterContext = class extends WebCrypto {
constructor(crypto2, kdf, exporterSecret) {
super(crypto2);
constructor(crypto, kdf, exporterSecret) {
super(crypto);
this._kdf = kdf;

@@ -223,4 +223,4 @@ this.exporterSecret = exporterSecret;

var SenderExporterContext = class extends ExporterContext {
constructor(crypto2, kdf, exporterSecret, enc) {
super(crypto2, kdf, exporterSecret);
constructor(crypto, kdf, exporterSecret, enc) {
super(crypto, kdf, exporterSecret);
this.enc = enc;

@@ -233,7 +233,7 @@ return;

var KdfCommon = class extends WebCrypto {
constructor(crypto2, suiteId, algHash) {
constructor(crypto, suiteId, algHash) {
if (algHash.length === void 0) {
throw new Error("Unknown hash size");
}
super(crypto2);
super(crypto);
this.suiteId = suiteId;

@@ -294,3 +294,3 @@ this.algHash = algHash;

async extractAndExpand(salt, ikm, info, len) {
const baseKey = await crypto.subtle.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
const baseKey = await this._crypto.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
return await this._crypto.deriveBits({

@@ -313,3 +313,3 @@ name: "HKDF",

var KdfContext = class extends KdfCommon {
constructor(crypto2, params) {
constructor(crypto, params) {
const suiteId = new Uint8Array(10);

@@ -332,3 +332,3 @@ suiteId.set(SUITE_ID_HEADER_HPKE, 0);

}
super(crypto2, suiteId, algHash);
super(crypto, suiteId, algHash);
switch (params.aead) {

@@ -393,3 +393,3 @@ case 1 /* Aes128Gcm */:

return {
key: await crypto.subtle.importKey("raw", key, { name: this._algAead }, true, AEAD_USAGES),
key: await this._crypto.importKey("raw", key, { name: this._algAead }, true, AEAD_USAGES),
baseNonce: new Uint8Array(baseNonce),

@@ -404,3 +404,3 @@ seq: 0,

var KemContext = class extends KdfCommon {
constructor(crypto2, kem) {
constructor(crypto, kem) {
const suiteId = new Uint8Array(5);

@@ -421,3 +421,3 @@ suiteId.set(SUITE_ID_HEADER_KEM, 0);

}
super(crypto2, suiteId, algHash);
super(crypto, suiteId, algHash);
switch (kem) {

@@ -531,4 +531,4 @@ case 16 /* DhkemP256HkdfSha256 */:

var EncryptionContext = class extends ExporterContext {
constructor(crypto2, kdf, params) {
super(crypto2, kdf, params.exporterSecret);
constructor(crypto, kdf, params) {
super(crypto, kdf, params.exporterSecret);
if (params.key === void 0 || params.baseNonce === void 0 || params.seq === void 0) {

@@ -568,3 +568,3 @@ throw new ValidationError("Required parameters are missing");

};
pt = await window.crypto.subtle.decrypt(alg, this.key, data);
pt = await this._crypto.decrypt(alg, this.key, data);
} catch (e) {

@@ -580,4 +580,4 @@ throw new OpenError(e);

var SenderContext = class extends EncryptionContext {
constructor(crypto2, kdf, params, enc) {
super(crypto2, kdf, params);
constructor(crypto, kdf, params, enc) {
super(crypto, kdf, params);
this.enc = enc;

@@ -594,5 +594,5 @@ return;

};
ct = await window.crypto.subtle.encrypt(alg, this.key, data);
ct = await this._crypto.encrypt(alg, this.key, data);
} catch (e) {
throw new OpenError(e);
throw new SealError(e);
}

@@ -619,5 +619,5 @@ this.incrementSeq();

if (this._kem === void 0 || this._kdf === void 0) {
const crypto2 = await loadSubtleCrypto();
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const crypto = await loadSubtleCrypto();
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -627,6 +627,6 @@ return await this._kem.generateKeyPair();

async createSenderContext(params) {
const crypto2 = await loadSubtleCrypto();
const crypto = await loadSubtleCrypto();
if (this._kem === void 0 || this._kdf === void 0) {
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -640,14 +640,14 @@ const dh = await this._kem.encap(params);

}
const kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const aeadParams = await this._kdf.keySchedule(mode, dh.sharedSecret, params);
if (aeadParams.key === void 0) {
return new SenderExporterContext(crypto2, kdf, aeadParams.exporterSecret, dh.enc);
return new SenderExporterContext(crypto, kdf, aeadParams.exporterSecret, dh.enc);
}
return new SenderContext(crypto2, kdf, aeadParams, dh.enc);
return new SenderContext(crypto, kdf, aeadParams, dh.enc);
}
async createRecipientContext(params) {
const crypto2 = await loadSubtleCrypto();
const crypto = await loadSubtleCrypto();
if (this._kem === void 0 || this._kdf === void 0) {
this._kem = new KemContext(crypto2, this.kem);
this._kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
this._kem = new KemContext(crypto, this.kem);
this._kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
}

@@ -661,8 +661,8 @@ const sharedSecret = await this._kem.decap(params);

}
const kdf = new KdfContext(crypto2, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const kdf = new KdfContext(crypto, { kem: this.kem, kdf: this.kdf, aead: this.aead });
const aeadParams = await this._kdf.keySchedule(mode, sharedSecret, params);
if (aeadParams.key === void 0) {
return new RecipientExporterContext(crypto2, kdf, aeadParams.exporterSecret);
return new RecipientExporterContext(crypto, kdf, aeadParams.exporterSecret);
}
return new RecipientContext(crypto2, kdf, aeadParams);
return new RecipientContext(crypto, kdf, aeadParams);
}

@@ -669,0 +669,0 @@ };

{
"name": "hpke-js",
"version": "0.2.1",
"version": "0.2.2",
"description": "A Hybrid Public Key Encryption (HPKE) library",

@@ -5,0 +5,0 @@ "repository": {

@@ -68,3 +68,3 @@ <h1 align="center">hpke-js</h1>

```
npm install hpke
npm install hpke-js
```

@@ -78,33 +78,37 @@

On browser:
On Node.js:
```js
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
// The global name is "hpke".
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
// setup
const suite = new hpke.CipherSuite({
kem: hpke.Kem.DhkemP256HkdfSha256,
kdf: hpke.Kdf.HkdfSha256,
aead: hpke.Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// decrypt
const pt = await recipient.open(ct);
// decrypt
const pt = await recipient.open(ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
// new TextDecoder().decode(pt) === "my-secret-message"
doHpke();
```

@@ -114,41 +118,45 @@

On browser:
On Node.js:
```js
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
// The global name is "hpke".
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
// setup
const suite = new hpke.CipherSuite({
kem: hpke.Kem.DhkemP256HkdfSha256,
kdf: hpke.Kdf.HkdfSha256,
aead: hpke.Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const rkp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// decrypt
const pt = await recipient.open(ct);
// decrypt
const pt = await recipient.open(ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
// new TextDecoder().decode(pt) === "my-secret-message"
doHpke();
```

@@ -158,36 +166,40 @@

On browser:
On Node.js:
```js
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
// The global name is "hpke".
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
// setup
const suite = new hpke.CipherSuite({
kem: hpke.Kem.DhkemP256HkdfSha256,
kdf: hpke.Kdf.HkdfSha256,
aead: hpke.Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const skp = await suite.generateKeyPair();
const rkp = await suite.generateKeyPair();
const skp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
senderKey: skp
});
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
senderKey: skp
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
senderPublicKey: skp.publicKey
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
senderPublicKey: skp.publicKey
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// decrypt
const pt = await recipient.open(ct);
// decrypt
const pt = await recipient.open(ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
// new TextDecoder().decode(pt) === "my-secret-message"
doHpke();
```

@@ -197,44 +209,48 @@

On browser:
On Node.js:
```js
const { Kem, Kdf, Aead, CipherSuite } = require("hpke-js");
// The global name is "hpke".
async function doHpke() {
// setup
const suite = new CipherSuite({
kem: Kem.DhkemP256HkdfSha256,
kdf: Kdf.HkdfSha256,
aead: Aead.Aes128Gcm
});
// setup
const suite = new hpke.CipherSuite({
kem: hpke.Kem.DhkemP256HkdfSha256,
kdf: hpke.Kdf.HkdfSha256,
aead: hpke.Aead.Aes128Gcm
});
const rkp = await suite.generateKeyPair();
const skp = await suite.generateKeyPair();
const rkp = await suite.generateKeyPair();
const skp = await suite.generateKeyPair();
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
senderKey: skp,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
const sender = await suite.createSenderContext({
recipientPublicKey: rkp.publicKey,
senderKey: skp,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
senderPublicKey: skp.publicKey,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
const recipient = await suite.createRecipientContext({
recipientKey: rkp,
enc: sender.enc,
senderPublicKey: skp.publicKey,
psk: {
id: new TextEncoder().encode("our-pre-shared-key-id"),
key: new TextEncoder().encode("our-pre-shared-key"),
}
});
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// encrypt
const ct = await sender.seal(new TextEncoder().encode("my-secret-message"));
// decrypt
const pt = await recipient.open(ct);
// decrypt
const pt = await recipient.open(ct);
console.log("decrypted: ", new TextDecoder().decode(pt));
// decripted: my-secret-message
}
// new TextDecoder().decode(pt) === "my-secret-message"
doHpke();
```

@@ -241,0 +257,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc