[Pkg-javascript-commits] [node-diffie-hellman] 10/88: gen primes

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:19:12 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-diffie-hellman.

commit 32b0cd0b6c3bb8a52161a4776970d2950ac36008
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Tue Nov 4 16:19:21 2014 -0500

    gen primes
---
 dh.js     | 18 ++++++++++++++---
 inject.js |  3 ++-
 test.js   | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 82 insertions(+), 6 deletions(-)

diff --git a/dh.js b/dh.js
index 69bff59..812c785 100644
--- a/dh.js
+++ b/dh.js
@@ -4,8 +4,13 @@ module.exports = DH;
 
 function DH(prime, crypto) {
 	this.setGenerator(new Buffer([2]));
-	this.__prime = new BN(prime);
-	this._prime = BN.mont(this.__prime);
+	if (typeof prime === 'string') {
+		this.__prime = BN._prime(prime).p;
+		this._prime = BN.red(prime);
+	} else {
+		this.__prime = new BN(prime);
+		this._prime = BN.mont(this.__prime);
+	}
 	this._pub = void 0;
 	this._priv = void 0;
 	this._makeNum = function makeNum() {
@@ -22,7 +27,14 @@ DH.prototype.computeSecret = function (other) {
 	other = new BN(other);
 	other = other.toRed(this._prime);
 	var secret = other.redPow(this._priv).fromRed();
-	return new Buffer(secret.toArray());
+	var out = new Buffer(secret.toArray());
+	var prime = this.getPrime();
+	if (out.length < prime.length) {
+		var front = new Buffer(prime.length - out.length);
+		front.fill(0);
+		out = Buffer.concat([front, out]);
+	}
+	return out;
 };
 DH.prototype.getPublicKey = function (enc) {
 	return returnValue(this._pub, enc);
diff --git a/inject.js b/inject.js
index 149c285..96e5302 100644
--- a/inject.js
+++ b/inject.js
@@ -1,5 +1,6 @@
 var primes = require('./primes.json');
 var DH = require('./dh');
+var generatePrime = require('./generatePrime');
 module.exports = function (crypto, exports) {
 	exports.getDiffieHellman = function (mod) {
 		if (mod === 'modp17' || mod === 'modp18') {
@@ -9,7 +10,7 @@ module.exports = function (crypto, exports) {
 	};
 	exports.createDiffieHellman = function (prime, enc) {
 		if (typeof prime === 'number') {
-			throw new Error('generating primes not implimented');
+			return new DH(generatePrime(prime, crypto), crypto);
 		}
 		enc = enc || 'utf8';
 		if (!Buffer.isBuffer(prime)) {
diff --git a/test.js b/test.js
index 62e482a..99f4ca7 100644
--- a/test.js
+++ b/test.js
@@ -5,7 +5,16 @@ var myCrypto = require('./');
 var mods = [
    'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16'/*, 'modp17', 'modp18'*/
 ];
-
+var lens = [128, 64, 256,
+  224,
+  192,
+  25519];
+  var lens2 = [256,
+  224,
+  192,
+  512,
+  384,
+  1024];
 function run(i) {
 	mods.forEach(function (mod) {
 		test(mod + ' run ' + i, function (t){
@@ -21,12 +30,66 @@ function run(i) {
 			var pubk2 = dh2.getPublicKey();
 			t.notEquals(pubk1, pubk2, 'diff public keys');
 			var pub1 = dh1.computeSecret(pubk2).toString('hex');
+			var pub2 = dh2.computeSecret(pubk1).toString('hex');
+			t.equals(pub1, pub2, 'equal secrets');
+		});
+	});
+}
+
+
+function bylen(t) {
+	return function (len){
+		t.test('' + len, function (t) {
+			t.plan(3);
+			var dh2 = myCrypto.createDiffieHellman(len);
+			var prime2 = dh2.getPrime();
+			var p2 = prime2.toString('hex');
+			var dh1 = nodeCrypto.createDiffieHellman(prime2);
+			var p1 = dh1.getPrime().toString('hex');
+			dh1.generateKeys();
+			dh2.generateKeys();
+			t.equals(p1, p2, 'equal primes');
+			var pubk1 = dh1.getPublicKey();
+			var pubk2 = dh2.getPublicKey();
+			t.notEquals(pubk1, pubk2, 'diff public keys');
+			var pub1 = dh1.computeSecret(pubk2).toString('hex');
 			var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
 			t.equals(pub1, pub2, 'equal secrets');
 		});
+	};
+}
+function bylen2(t) {
+	return function (len){
+		t.test('' + len, function (t) {
+			t.plan(3);
+			var dh2 = nodeCrypto.createDiffieHellman(len);
+			var prime2 = dh2.getPrime();
+			var p2 = prime2.toString('hex');
+			var dh1 = myCrypto.createDiffieHellman(prime2);
+			var p1 = dh1.getPrime().toString('hex');
+			dh1.generateKeys();
+			dh2.generateKeys();
+			t.equals(p1, p2, 'equal primes');
+			var pubk1 = dh1.getPublicKey();
+			var pubk2 = dh2.getPublicKey();
+			t.notEquals(pubk1, pubk2, 'diff public keys');
+			var pub1 = dh1.computeSecret(pubk2).toString('hex');
+			var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
+			t.equals(pub1, pub2, 'equal secrets');
+		});
+	};
+}
+if (process.version && process.version.split('.').length === 3 && parseInt(process.version.split('.')[1], 10) > 10) {
+	test('create primes', function (t) {
+		var f = bylen(t);
+		lens.forEach(f);
 	});
 }
+test('create primes other way', function (t) {
+		var f = bylen2(t);
+		lens2.forEach(f);
+	});
 var i = 0;
-while (++i < 3) {
+while (++i < 2) {
 	run(i);
 }
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-diffie-hellman.git



More information about the Pkg-javascript-commits mailing list