Skip to content

Commit

Permalink
#53 stop using optional deps to fix webpack
Browse files Browse the repository at this point in the history
Reviewed by: Cody Peter Mello <cody.mello@joyent.com>
  • Loading branch information
factoidforrest authored and arekinath committed Oct 11, 2018
1 parent 53e23fe commit 026ef47
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 75 deletions.
25 changes: 4 additions & 21 deletions lib/dhe.js
Expand Up @@ -11,14 +11,16 @@ var crypto = require('crypto');
var Buffer = require('safer-buffer').Buffer;
var algs = require('./algs');
var utils = require('./utils');
var nacl;
var nacl = require('tweetnacl');

var Key = require('./key');
var PrivateKey = require('./private-key');

var CRYPTO_HAVE_ECDH = (crypto.createECDH !== undefined);

var ecdh, ec, jsbn;
var ecdh = require('ecc-jsbn');
var ec = require('ecc-jsbn/lib/ec');
var jsbn = require('jsbn').BigInteger;

function DiffieHellman(key) {
utils.assertCompatible(key, Key, [1, 4], 'key');
Expand All @@ -43,13 +45,6 @@ function DiffieHellman(key) {

} else if (key.type === 'ecdsa') {
if (!CRYPTO_HAVE_ECDH) {
if (ecdh === undefined)
ecdh = require('ecc-jsbn');
if (ec === undefined)
ec = require('ecc-jsbn/lib/ec');
if (jsbn === undefined)
jsbn = require('jsbn').BigInteger;

this._ecParams = new X9ECParameters(this._curve);

if (this._isPriv) {
Expand All @@ -76,9 +71,6 @@ function DiffieHellman(key) {
this._dh.setPublicKey(key.part.Q.data);

} else if (key.type === 'curve25519') {
if (nacl === undefined)
nacl = require('tweetnacl');

if (this._isPriv) {
utils.assertCompatible(key, PrivateKey, [1, 5], 'key');
this._priv = key.part.k.data;
Expand Down Expand Up @@ -321,9 +313,6 @@ ECPrivate.prototype.deriveSharedSecret = function (pubKey) {
};

function generateED25519() {
if (nacl === undefined)
nacl = require('tweetnacl');

var pair = nacl.sign.keyPair();
var priv = Buffer.from(pair.secretKey);
var pub = Buffer.from(pair.publicKey);
Expand Down Expand Up @@ -374,12 +363,6 @@ function generateECDSA(curve) {
});
return (key);
} else {
if (ecdh === undefined)
ecdh = require('ecc-jsbn');
if (ec === undefined)
ec = require('ecc-jsbn/lib/ec');
if (jsbn === undefined)
jsbn = require('jsbn').BigInteger;

var ecParams = new X9ECParameters(curve);

Expand Down
8 changes: 1 addition & 7 deletions lib/ed-compat.js
Expand Up @@ -5,17 +5,14 @@ module.exports = {
Signer: Signer
};

var nacl;
var nacl = require('tweetnacl');
var stream = require('stream');
var util = require('util');
var assert = require('assert-plus');
var Buffer = require('safer-buffer').Buffer;
var Signature = require('./signature');

function Verifier(key, hashAlgo) {
if (nacl === undefined)
nacl = require('tweetnacl');

if (hashAlgo.toLowerCase() !== 'sha512')
throw (new Error('ED25519 only supports the use of ' +
'SHA-512 hashes'));
Expand Down Expand Up @@ -61,9 +58,6 @@ Verifier.prototype.verify = function (signature, fmt) {
};

function Signer(key, hashAlgo) {
if (nacl === undefined)
nacl = require('tweetnacl');

if (hashAlgo.toLowerCase() !== 'sha512')
throw (new Error('ED25519 only supports the use of ' +
'SHA-512 hashes'));
Expand Down
16 changes: 2 additions & 14 deletions lib/private-key.js
Expand Up @@ -14,14 +14,8 @@ var utils = require('./utils');
var dhe = require('./dhe');
var generateECDSA = dhe.generateECDSA;
var generateED25519 = dhe.generateED25519;
var edCompat;
var nacl;

try {
edCompat = require('./ed-compat');
} catch (e) {
/* Just continue through, and bail out if we try to use it. */
}
var edCompat = require('./ed-compat');
var nacl = require('tweetnacl');

var Key = require('./key');

Expand Down Expand Up @@ -90,9 +84,6 @@ PrivateKey.prototype.derive = function (newType) {
var priv, pub, pair;

if (this.type === 'ed25519' && newType === 'curve25519') {
if (nacl === undefined)
nacl = require('tweetnacl');

priv = this.part.k.data;
if (priv[0] === 0x00)
priv = priv.slice(1);
Expand All @@ -108,9 +99,6 @@ PrivateKey.prototype.derive = function (newType) {
]
}));
} else if (this.type === 'curve25519' && newType === 'ed25519') {
if (nacl === undefined)
nacl = require('tweetnacl');

priv = this.part.k.data;
if (priv[0] === 0x00)
priv = priv.slice(1);
Expand Down
39 changes: 9 additions & 30 deletions lib/utils.js
Expand Up @@ -28,8 +28,9 @@ var crypto = require('crypto');
var algs = require('./algs');
var asn1 = require('asn1');

var ec, jsbn;
var nacl;
var ec = require('ecc-jsbn/lib/ec');
var jsbn = require('jsbn').BigInteger;
var nacl = require('tweetnacl');

var MAX_CLASS_DEPTH = 3;

Expand Down Expand Up @@ -257,15 +258,9 @@ function calculateDSAPublic(g, p, x) {
assert.buffer(g);
assert.buffer(p);
assert.buffer(x);
try {
var bigInt = require('jsbn').BigInteger;
} catch (e) {
throw (new Error('To load a PKCS#8 format DSA private key, ' +
'the node jsbn library is required.'));
}
g = new bigInt(g);
p = new bigInt(p);
x = new bigInt(x);
g = new jsbn(g);
p = new jsbn(p);
x = new jsbn(x);
var y = g.modPow(x, p);
var ybuf = bigintToMpBuf(y);
return (ybuf);
Expand All @@ -274,46 +269,34 @@ function calculateDSAPublic(g, p, x) {
function calculateED25519Public(k) {
assert.buffer(k);

if (nacl === undefined)
nacl = require('tweetnacl');

var kp = nacl.sign.keyPair.fromSeed(new Uint8Array(k));
return (Buffer.from(kp.publicKey));
}

function calculateX25519Public(k) {
assert.buffer(k);

if (nacl === undefined)
nacl = require('tweetnacl');

var kp = nacl.box.keyPair.fromSeed(new Uint8Array(k));
return (Buffer.from(kp.publicKey));
}

function addRSAMissing(key) {
assert.object(key);
assertCompatible(key, PrivateKey, [1, 1]);
try {
var bigInt = require('jsbn').BigInteger;
} catch (e) {
throw (new Error('To write a PEM private key from ' +
'this source, the node jsbn lib is required.'));
}

var d = new bigInt(key.part.d.data);
var d = new jsbn(key.part.d.data);
var buf;

if (!key.part.dmodp) {
var p = new bigInt(key.part.p.data);
var p = new jsbn(key.part.p.data);
var dmodp = d.mod(p.subtract(1));

buf = bigintToMpBuf(dmodp);
key.part.dmodp = {name: 'dmodp', data: buf};
key.parts.push(key.part.dmodp);
}
if (!key.part.dmodq) {
var q = new bigInt(key.part.q.data);
var q = new jsbn(key.part.q.data);
var dmodq = d.mod(q.subtract(1));

buf = bigintToMpBuf(dmodq);
Expand All @@ -325,10 +308,6 @@ function addRSAMissing(key) {
function publicFromPrivateECDSA(curveName, priv) {
assert.string(curveName, 'curveName');
assert.buffer(priv);
if (ec === undefined)
ec = require('ecc-jsbn/lib/ec');
if (jsbn === undefined)
jsbn = require('jsbn').BigInteger;
var params = algs.curves[curveName];
var p = new jsbn(params.p);
var a = new jsbn(params.a);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -43,14 +43,14 @@
"assert-plus": "^1.0.0",
"dashdash": "^1.12.0",
"getpass": "^0.1.1",
"safer-buffer": "^2.0.2"
},
"optionalDependencies": {
"safer-buffer": "^2.0.2",
"jsbn": "~0.1.0",
"tweetnacl": "~0.14.0",
"ecc-jsbn": "~0.1.1",
"bcrypt-pbkdf": "^1.0.0"
},
"optionalDependencies": {
},
"devDependencies": {
"tape": "^3.5.0",
"benchmark": "^1.0.0",
Expand Down

0 comments on commit 026ef47

Please sign in to comment.