pax_global_header00006660000000000000000000000064132740606210014513gustar00rootroot0000000000000052 comment=3e7d3d301bfa4c332e6124db609743718cf1ffae createECDH-4.0.3/000077500000000000000000000000001327406062100134065ustar00rootroot00000000000000createECDH-4.0.3/.npmignore000066400000000000000000000000101327406062100153740ustar00rootroot00000000000000test.js createECDH-4.0.3/.travis.yml000066400000000000000000000001001327406062100155060ustar00rootroot00000000000000language: node_js sudo: false node_js: - 6 - 8 - 9 - 10 createECDH-4.0.3/LICENSE000066400000000000000000000021071327406062100144130ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014-2017 createECDH contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. createECDH-4.0.3/browser.js000066400000000000000000000052101327406062100154250ustar00rootroot00000000000000var elliptic = require('elliptic') var BN = require('bn.js') module.exports = function createECDH (curve) { return new ECDH(curve) } var aliases = { secp256k1: { name: 'secp256k1', byteLength: 32 }, secp224r1: { name: 'p224', byteLength: 28 }, prime256v1: { name: 'p256', byteLength: 32 }, prime192v1: { name: 'p192', byteLength: 24 }, ed25519: { name: 'ed25519', byteLength: 32 }, secp384r1: { name: 'p384', byteLength: 48 }, secp521r1: { name: 'p521', byteLength: 66 } } aliases.p224 = aliases.secp224r1 aliases.p256 = aliases.secp256r1 = aliases.prime256v1 aliases.p192 = aliases.secp192r1 = aliases.prime192v1 aliases.p384 = aliases.secp384r1 aliases.p521 = aliases.secp521r1 function ECDH (curve) { this.curveType = aliases[curve] if (!this.curveType) { this.curveType = { name: curve } } this.curve = new elliptic.ec(this.curveType.name) // eslint-disable-line new-cap this.keys = void 0 } ECDH.prototype.generateKeys = function (enc, format) { this.keys = this.curve.genKeyPair() return this.getPublicKey(enc, format) } ECDH.prototype.computeSecret = function (other, inenc, enc) { inenc = inenc || 'utf8' if (!Buffer.isBuffer(other)) { other = new Buffer(other, inenc) } var otherPub = this.curve.keyFromPublic(other).getPublic() var out = otherPub.mul(this.keys.getPrivate()).getX() return formatReturnValue(out, enc, this.curveType.byteLength) } ECDH.prototype.getPublicKey = function (enc, format) { var key = this.keys.getPublic(format === 'compressed', true) if (format === 'hybrid') { if (key[key.length - 1] % 2) { key[0] = 7 } else { key[0] = 6 } } return formatReturnValue(key, enc) } ECDH.prototype.getPrivateKey = function (enc) { return formatReturnValue(this.keys.getPrivate(), enc) } ECDH.prototype.setPublicKey = function (pub, enc) { enc = enc || 'utf8' if (!Buffer.isBuffer(pub)) { pub = new Buffer(pub, enc) } this.keys._importPublic(pub) return this } ECDH.prototype.setPrivateKey = function (priv, enc) { enc = enc || 'utf8' if (!Buffer.isBuffer(priv)) { priv = new Buffer(priv, enc) } var _priv = new BN(priv) _priv = _priv.toString(16) this.keys = this.curve.genKeyPair() this.keys._importPrivate(_priv) return this } function formatReturnValue (bn, enc, len) { if (!Array.isArray(bn)) { bn = bn.toArray() } var buf = new Buffer(bn) if (len && buf.length < len) { var zeros = new Buffer(len - buf.length) zeros.fill(0) buf = Buffer.concat([zeros, buf]) } if (!enc) { return buf } else { return buf.toString(enc) } } createECDH-4.0.3/index.js000066400000000000000000000001431327406062100150510ustar00rootroot00000000000000var createECDH = require('crypto').createECDH module.exports = createECDH || require('./browser') createECDH-4.0.3/package.json000066400000000000000000000014131327406062100156730ustar00rootroot00000000000000{ "name": "create-ecdh", "version": "4.0.3", "description": "createECDH but browserifiable", "main": "index.js", "browser": "browser.js", "scripts": { "test": "standard && node test.js | tspec" }, "repository": { "type": "git", "url": "https://github.com/crypto-browserify/createECDH.git" }, "keywords": [ "diffie", "hellman", "diffiehellman", "ECDH" ], "author": "Calvin Metcalf", "license": "MIT", "bugs": { "url": "https://github.com/crypto-browserify/createECDH/issues" }, "homepage": "https://github.com/crypto-browserify/createECDH", "dependencies": { "bn.js": "^4.1.0", "elliptic": "^6.0.0" }, "devDependencies": { "tap-spec": "^1.0.1", "tape": "^3.0.1", "standard": "^5.4.1" } } createECDH-4.0.3/readme.md000066400000000000000000000012351327406062100151660ustar00rootroot00000000000000createECDH [![Build Status](https://travis-ci.org/crypto-browserify/createECDH.svg)](https://travis-ci.org/crypto-browserify/createECDH) ==== In io.js or node >= 0.11 this module is just a shortcut to crypto.createECDH. In node <= 0.11 or the browser this is a pure JavaScript implimentation, more specifically a wrapper around [elliptic](https://github.com/indutny/elliptic), to give it the same API as node. `secp256k1`, `secp224r1` (aka p224), `prime256v1` (aka p256, secp256r1), `prime192v1` (aka p192, secp192r1), `secp384r1` (aka p384), `secp521r1` (aka p521) curves all work in both this library and node (though only the highlighted name will work in node). createECDH-4.0.3/test.js000066400000000000000000000102121327406062100147170ustar00rootroot00000000000000var test = require('tape') var nodeCrypto = require('./') var myCrypto = require('./browser') var mods = [ 'secp256k1', 'secp224r1', 'prime256v1', 'prime192v1', 'secp384r1', 'secp521r1' ] function run (i) { mods.forEach(function (mod) { test(mod + ' run ' + i + ' uncompressed', function (t) { t.plan(2) var dh1 = nodeCrypto(mod) dh1.generateKeys() var dh2 = myCrypto(mod) dh2.generateKeys() var pubk1 = dh1.getPublicKey() var pubk2 = dh2.getPublicKey() t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys') var pub1 = dh1.computeSecret(pubk2).toString('hex') var pub2 = dh2.computeSecret(pubk1).toString('hex') t.equals(pub1, pub2, 'equal secrets') }) test(mod + ' run ' + i + ' compressed', function (t) { t.plan(2) var dh1 = nodeCrypto(mod) dh1.generateKeys() var dh2 = myCrypto(mod) dh2.generateKeys() var pubk1 = dh1.getPublicKey(null, 'compressed') var pubk2 = dh2.getPublicKey(null, 'compressed') t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys') var pub1 = dh1.computeSecret(pubk2).toString('hex') var pub2 = dh2.computeSecret(pubk1).toString('hex') t.equals(pub1, pub2, 'equal secrets') }) test(mod + ' run ' + i + ' set stuff', function (t) { t.plan(5) var dh1 = nodeCrypto(mod) var dh2 = myCrypto(mod) dh1.generateKeys() dh2.generateKeys() dh1.setPrivateKey(dh2.getPrivateKey()) dh1.setPublicKey(dh2.getPublicKey()) var priv1 = dh1.getPrivateKey('hex') var priv2 = dh2.getPrivateKey('hex') t.equals(priv1, priv2, 'same private key') var pubk1 = dh1.getPublicKey() var pubk2 = dh2.getPublicKey() t.equals(pubk1.toString('hex'), pubk2.toString('hex'), 'same public keys, uncompressed') t.equals(dh1.getPublicKey('hex', 'compressed'), dh2.getPublicKey('hex', 'compressed'), 'same public keys compressed') t.equals(dh1.getPublicKey('hex', 'hybrid'), dh2.getPublicKey('hex', 'hybrid'), 'same public keys hybrid') var pub1 = dh1.computeSecret(pubk2).toString('hex') var pub2 = dh2.computeSecret(pubk1).toString('hex') t.equals(pub1, pub2, 'equal secrets') }) test(mod + ' run ' + i + ' new way to set stuff', function (t) { t.plan(5) var dh1 = myCrypto(mod) var dh2 = nodeCrypto(mod) dh2.generateKeys() dh1.setPrivateKey(dh2.getPrivateKey()) var priv1 = dh1.getPrivateKey('hex') var priv2 = dh2.getPrivateKey('hex') t.equals(priv1, priv2, 'same private key') var pubk1 = dh1.getPublicKey() var pubk2 = dh2.getPublicKey() t.equals(pubk1.toString('hex'), pubk2.toString('hex'), 'same public keys, uncompressed') t.equals(dh1.getPublicKey('hex', 'compressed'), dh2.getPublicKey('hex', 'compressed'), 'same public keys compressed') t.equals(dh1.getPublicKey('hex', 'hybrid'), dh2.getPublicKey('hex', 'hybrid'), 'same public keys hybrid') var pub1 = dh1.computeSecret(pubk2).toString('hex') var pub2 = dh2.computeSecret(pubk1).toString('hex') t.equals(pub1, pub2, 'equal secrets') }) }) } apitests('api tests for my crypto', myCrypto) apitests('api tests for node crypto', nodeCrypto) function apitests (name, crypto) { test(name, function (t) { t.test('check about regenerating keys', function (t) { t.plan(2) var dh1 = crypto('secp256k1') var dh2 = crypto('secp256k1') dh1.generateKeys() dh2.generateKeys() var pub = dh1.getPublicKey('hex') var priv = dh1.getPrivateKey('hex') dh1.setPrivateKey(dh2.getPrivateKey()) t.notEquals(dh1.getPrivateKey('hex'), priv, 'private keys not equal') t.notEquals(dh1.getPublicKey('hex'), pub, 'public keys not equal') }) t.test('set private keys without generating them', function (t) { t.plan(1) var dh1 = crypto('secp256k1') var dh2 = crypto('secp256k1') dh2.generateKeys() dh1.setPrivateKey(dh2.getPrivateKey()) t.equals(dh1.getPublicKey('hex'), dh1.getPublicKey('hex'), 'equal public keys') }) }) } var i = 0 while (++i < 20) { run(i) }