Skip to content

Commit

Permalink
Update: use npm-published node-forge (#103)
Browse files Browse the repository at this point in the history
fixes #96 in collaboration with #104
  • Loading branch information
linuxwolf committed Apr 12, 2017
1 parent c6b30c9 commit 0f4e0ab
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 49 deletions.
2 changes: 1 addition & 1 deletion lib/deps/ecc/curves.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// Named EC curves

var BigInteger = require("jsbn").BigInteger,
var BigInteger = require("../../deps/forge").jsbn.BigInteger,
ec = require("./math.js");

// ----------------
Expand Down
2 changes: 1 addition & 1 deletion lib/deps/ecc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"use strict";

var forge = require("../../deps/forge"),
BigInteger = require("jsbn").BigInteger,
BigInteger = forge.jsbn.BigInteger,
ec = require("./math.js"),
CURVES = require("./curves.js");

Expand Down
71 changes: 59 additions & 12 deletions lib/deps/ecc/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,58 @@
// Ported loosely from BouncyCastle's Java EC code
// Only Fp curves implemented for now

// Requires jsbn.js and jsbn2.js
var jsbn = require("jsbn");
var BigInteger = require("../../deps/forge").jsbn.BigInteger;

var BigInteger = jsbn.BigInteger,
Barrett = BigInteger.prototype.Barrett;
// ----------------
// Helpers

function nbi() {
return new BigInteger(null);
}

// ----------------
// Barrett modular reduction

// constructor
function Barrett(m) {
// setup Barrett
this.r2 = nbi();
this.q3 = nbi();
BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
this.mu = this.r2.divide(m);
this.m = m;
}

function barrettConvert(x) {
if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
else if(x.compareTo(this.m) < 0) return x;
else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
}

function barrettRevert(x) { return x; }

// x = x mod m (HAC 14.42)
function barrettReduce(x) {
x.drShiftTo(this.m.t-1,this.r2);
if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
x.subTo(this.r2,x);
while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
}

// r = x^2 mod m; x != r
function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }

// r = x*y mod m; x,y != r
function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }

Barrett.prototype.convert = barrettConvert;
Barrett.prototype.revert = barrettRevert;
Barrett.prototype.reduce = barrettReduce;
Barrett.prototype.mulTo = barrettMulTo;
Barrett.prototype.sqrTo = barrettSqrTo;

// ----------------
// ECFieldElementFp
Expand Down Expand Up @@ -58,7 +105,7 @@ function feFpMultiply(b) {
}

function feFpSquare() {
return new ECFieldElementFp(this.p, this.x.square().mod(this.p));
return new ECFieldElementFp(this.p, this.x.pow(2).mod(this.p));
}

function feFpDivide(b) {
Expand Down Expand Up @@ -167,10 +214,10 @@ function pointFpAdd(b) {
var x1 = this.x.toBigInteger();
var y1 = this.y.toBigInteger();

var v2 = v.square();
var v2 = v.pow(2);
var v3 = v2.multiply(v);
var x1v2 = x1.multiply(v2);
var zu2 = u.square().multiply(this.z);
var zu2 = u.pow(2).multiply(this.z);

// x3 = v * (z2 * (z1 * u^2 - 2 * x1 * v^2) - v^3)
var x3 = zu2.subtract(x1v2.shiftLeft(1)).multiply(b.z).subtract(v3).multiply(v).mod(this.curve.p);
Expand Down Expand Up @@ -200,18 +247,18 @@ function pointFpTwice() {
var a = this.curve.a.toBigInteger();

// w = 3 * x1^2 + a * z1^2
var w = x1.square().multiply(THREE);
var w = x1.pow(2).multiply(THREE);
if (!BigInteger.ZERO.equals(a)) {
w = w.add(this.z.square().multiply(a));
w = w.add(this.z.pow(2).multiply(a));
}
w = w.mod(this.curve.p);
//this.curve.reduce(w);
// x3 = 2 * y1 * z1 * (w^2 - 8 * x1 * y1^2 * z1)
var x3 = w.square().subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.p);
var x3 = w.pow(2).subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.p);
// y3 = 4 * y1^2 * z1 * (3 * w * x1 - 2 * y1^2 * z1) - w^3
var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.square().multiply(w)).mod(this.curve.p);
var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.pow(2).multiply(w)).mod(this.curve.p);
// z3 = 8 * (y1 * z1)^3
var z3 = y1z1.square().multiply(y1z1).shiftLeft(3).mod(this.curve.p);
var z3 = y1z1.pow(2).multiply(y1z1).shiftLeft(3).mod(this.curve.p);

return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
}
Expand Down
62 changes: 29 additions & 33 deletions lib/deps/forge.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,25 @@
*/
"use strict";

var forge = {
aes: require("node-forge/js/aes"),
asn1: require("node-forge/js/asn1"),
cipher: require("node-forge/js/cipher"),
hmac: require("node-forge/js/hmac"),
jsbn: require("node-forge/js/jsbn"),
md: require("node-forge/js/md"),
mgf: require("node-forge/js/mgf"),
pem: require("node-forge/js/pem"),
pkcs1: require("node-forge/js/pkcs1"),
pkcs5: require("node-forge/js/pkcs5"),
pkcs7: require("node-forge/js/pkcs7"),
pki: require("node-forge/js/x509"),
prime: require("node-forge/js/prime"),
prng: require("node-forge/js/prng"),
pss: require("node-forge/js/pss"),
random: require("node-forge/js/random"),
util: require("node-forge/js/util")
};

// load hash algorithms
require("node-forge/js/sha1");
require("node-forge/js/sha256");
require("node-forge/js/sha512");

// load symmetric cipherModes
require("node-forge/js/cipherModes");

// load AES cipher suites
// TODO: move this to a separate file
require("node-forge/js/aesCipherSuites");
var forge = require("node-forge/lib/forge");
require("node-forge/lib/aes");
require("node-forge/lib/asn1");
require("node-forge/lib/cipher");
require("node-forge/lib/hmac");
require("node-forge/lib/mgf1");
require("node-forge/lib/pbkdf2");
require("node-forge/lib/pem");
require("node-forge/lib/pkcs1");
require("node-forge/lib/pkcs7");
require("node-forge/lib/pki");
require("node-forge/lib/prime");
require("node-forge/lib/prng");
require("node-forge/lib/pss");
require("node-forge/lib/random");
require("node-forge/lib/sha1");
require("node-forge/lib/sha256");
require("node-forge/lib/sha512");
require("node-forge/lib/util");

// Define AES "raw" cipher mode
function modeRaw(options) {
Expand All @@ -50,7 +38,11 @@ function modeRaw(options) {

modeRaw.prototype.start = function() {};

modeRaw.prototype.encrypt = function(input, output) {
modeRaw.prototype.encrypt = function(input, output, finish) {
if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
return true;
}

var i;

// get next block
Expand All @@ -67,7 +59,11 @@ modeRaw.prototype.encrypt = function(input, output) {
}
};

modeRaw.prototype.decrypt = function(input, output) {
modeRaw.prototype.decrypt = function(input, output, finish) {
if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
return true;
}

var i;

// get next block
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"dependencies": {
"base64url": "^2.0.0",
"es6-promise": "^4.0.5",
"jsbn": "^1.1.0",
"lodash.assign": "^4.0.8",
"lodash.clone": "^4.3.2",
"lodash.fill": "^3.2.2",
Expand All @@ -40,7 +39,7 @@
"lodash.pick": "^4.2.0",
"lodash.uniq": "^4.2.1",
"long": "^3.1.0",
"node-forge": "https://github.com/linuxwolf/forge/archive/browserify.tar.gz",
"node-forge": "^0.7.1",
"uuid": "^3.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit 0f4e0ab

Please sign in to comment.