var SEA = require('./root'); var shim = require('./shim'); var S = require('./settings'); // Derive shared secret from other's pub and my epub/epriv SEA.secret = SEA.secret || (async (key, pair, cb, opt) => { try { opt = opt || {}; if(!pair || !pair.epriv || !pair.epub){ pair = await SEA.I(null, {what: key, how: 'secret', why: opt.why}); } var pub = key.epub || key; var epub = pair.epub; var epriv = pair.epriv; var ecdhSubtle = shim.ossl || shim.subtle; var pubKeyData = keysToEcdhJwk(pub); var props = Object.assign({ public: await ecdhSubtle.importKey(...pubKeyData, true, []) },S.ecdh); var privKeyData = keysToEcdhJwk(epub, epriv); var derived = await ecdhSubtle.importKey(...privKeyData, false, ['deriveKey']).then(async (privKey) => { // privateKey scope doesn't leak out from here! var derivedKey = await ecdhSubtle.deriveKey(props, privKey, { name: 'AES-GCM', length: 256 }, true, [ 'encrypt', 'decrypt' ]); return ecdhSubtle.exportKey('jwk', derivedKey).then(({ k }) => k); }) var r = derived; if(cb){ try{ cb(r) }catch(e){console.log(e)} } return r; } catch(e) { console.log(e); SEA.err = e; if(SEA.throw){ throw e } if(cb){ cb() } return; }}); // can this be replaced with settings.jwk? var keysToEcdhJwk = (pub, d) => { // d === priv //var [ x, y ] = Buffer.from(pub, 'base64').toString('utf8').split(':') // old var [ x, y ] = pub.split('.') // new var jwk = d ? { d: d } : {} return [ // Use with spread returned value... 'jwk', Object.assign( jwk, { x: x, y: y, kty: 'EC', crv: 'P-256', ext: true } ), // ??? refactor S.ecdh ] } module.exports = SEA.secret;