wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 2.75 kB
JavaScript
import{generateX25519KeyPair,x25519DH,falcon512Sign,falcon512Verify,generateMLKEMKeyPair,mlkemEncapsulate,mlkemDecapsulate,hkdfDerive,concatBytes,deriveRegistrationId}from'./crypto.js';import{ROOT_KEY_SIZE,CHAIN_KEY_SIZE}from'./types.js';export function pqxdhInitiate(a,b){const c=generateX25519KeyPair(),d=x25519DH(c['privateKey'],b['identityKey']),e=x25519DH(a['privateKey'],b['signedPreKey']['publicKey']),f=x25519DH(c['privateKey'],b['signedPreKey']['publicKey']),{ciphertext:g,sharedSecret:h}=mlkemEncapsulate(b['pqPreKey']['publicKey']);let i;if(b['oneTimePreKey']){const m=x25519DH(c['privateKey'],b['oneTimePreKey']['publicKey']);i=concatBytes(d,e,f,m,h);}else i=concatBytes(d,e,f,h);const j=concatBytes(new TextEncoder()['encode']('WowokPQXDH'),a['publicKey'],b['identityKey']),k=hkdfDerive(i,j,ROOT_KEY_SIZE+CHAIN_KEY_SIZE),l={'rootKey':k['slice'](0x0,ROOT_KEY_SIZE),'chainKey':k['slice'](ROOT_KEY_SIZE,ROOT_KEY_SIZE+CHAIN_KEY_SIZE),'ourDHKeyPair':c,'peerDHPublicKey':b['signedPreKey']['publicKey'],'peerIdentityKey':b['identityKey'],'pqSharedSecret':h};return{'result':l,'initialMessage':{'ephemeralPublicKey':c['publicKey'],'mlkemCiphertext':g,'identityKey':a['publicKey']}};}export function pqxdhReceive(a,b,c,d,e,f,g){const h=x25519DH(a['privateKey'],e),i=x25519DH(b['privateKey'],f),j=x25519DH(b['privateKey'],e),k=mlkemDecapsulate(g,d['secretKey']);let l;if(c){const o=x25519DH(c['privateKey'],e);l=concatBytes(h,i,j,o,k);}else l=concatBytes(h,i,j,k);const m=concatBytes(new TextEncoder()['encode']('WowokPQXDH'),f,a['publicKey']),n=hkdfDerive(l,m,ROOT_KEY_SIZE+CHAIN_KEY_SIZE);return{'rootKey':n['slice'](0x0,ROOT_KEY_SIZE),'chainKey':n['slice'](ROOT_KEY_SIZE,ROOT_KEY_SIZE+CHAIN_KEY_SIZE),'ourDHKeyPair':b,'peerDHPublicKey':e,'peerIdentityKey':f,'pqSharedSecret':k};}export function generatePreKeyBundle(a,b,c,d,e){const f=deriveRegistrationId(a['publicKey']),g=generateX25519KeyPair(),h=falcon512Sign(b['privateKey'],g['publicKey']),j=[];for(let m=0x0;m<e;m++){j['push']({'keyId':m+0x1,'keyPair':generateX25519KeyPair()});}const k=generateMLKEMKeyPair(),l=falcon512Sign(b['privateKey'],k['publicKey']);return{'identityKeyPair':a,'falcon512KeyPair':b,'registrationId':f,'signedPreKey':{'keyId':c,'keyPair':g,'signature':h},'oneTimePreKeys':j,'pqPreKey':{'keyId':d,'keyPair':k,'signature':l}};}export function verifyPreKeyBundle(a){if(!a['falcon512PublicKey']||a['falcon512PublicKey']['length']!==0x381)return![];if(!a['signedPreKey'])return![];const b=falcon512Verify(a['falcon512PublicKey'],a['signedPreKey']['publicKey'],a['signedPreKey']['signature']);if(!b)return![];if(!a['pqPreKey'])return![];const c=falcon512Verify(a['falcon512PublicKey'],a['pqPreKey']['publicKey'],a['pqPreKey']['signature']);if(!c)return![];return!![];}