Skip to content

Commit 0f4e0ab

Browse files
authored
Update: use npm-published node-forge (#103)
fixes #96 in collaboration with #104
1 parent c6b30c9 commit 0f4e0ab

File tree

5 files changed

+91
-49
lines changed

5 files changed

+91
-49
lines changed

lib/deps/ecc/curves.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// Named EC curves
1414

15-
var BigInteger = require("jsbn").BigInteger,
15+
var BigInteger = require("../../deps/forge").jsbn.BigInteger,
1616
ec = require("./math.js");
1717

1818
// ----------------

lib/deps/ecc/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"use strict";
77

88
var forge = require("../../deps/forge"),
9-
BigInteger = require("jsbn").BigInteger,
9+
BigInteger = forge.jsbn.BigInteger,
1010
ec = require("./math.js"),
1111
CURVES = require("./curves.js");
1212

lib/deps/ecc/math.js

+59-12
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,58 @@
1414
// Ported loosely from BouncyCastle's Java EC code
1515
// Only Fp curves implemented for now
1616

17-
// Requires jsbn.js and jsbn2.js
18-
var jsbn = require("jsbn");
17+
var BigInteger = require("../../deps/forge").jsbn.BigInteger;
1918

20-
var BigInteger = jsbn.BigInteger,
21-
Barrett = BigInteger.prototype.Barrett;
19+
// ----------------
20+
// Helpers
21+
22+
function nbi() {
23+
return new BigInteger(null);
24+
}
25+
26+
// ----------------
27+
// Barrett modular reduction
28+
29+
// constructor
30+
function Barrett(m) {
31+
// setup Barrett
32+
this.r2 = nbi();
33+
this.q3 = nbi();
34+
BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
35+
this.mu = this.r2.divide(m);
36+
this.m = m;
37+
}
38+
39+
function barrettConvert(x) {
40+
if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
41+
else if(x.compareTo(this.m) < 0) return x;
42+
else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
43+
}
44+
45+
function barrettRevert(x) { return x; }
46+
47+
// x = x mod m (HAC 14.42)
48+
function barrettReduce(x) {
49+
x.drShiftTo(this.m.t-1,this.r2);
50+
if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
51+
this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
52+
this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
53+
while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
54+
x.subTo(this.r2,x);
55+
while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
56+
}
57+
58+
// r = x^2 mod m; x != r
59+
function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
60+
61+
// r = x*y mod m; x,y != r
62+
function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
63+
64+
Barrett.prototype.convert = barrettConvert;
65+
Barrett.prototype.revert = barrettRevert;
66+
Barrett.prototype.reduce = barrettReduce;
67+
Barrett.prototype.mulTo = barrettMulTo;
68+
Barrett.prototype.sqrTo = barrettSqrTo;
2269

2370
// ----------------
2471
// ECFieldElementFp
@@ -58,7 +105,7 @@ function feFpMultiply(b) {
58105
}
59106

60107
function feFpSquare() {
61-
return new ECFieldElementFp(this.p, this.x.square().mod(this.p));
108+
return new ECFieldElementFp(this.p, this.x.pow(2).mod(this.p));
62109
}
63110

64111
function feFpDivide(b) {
@@ -167,10 +214,10 @@ function pointFpAdd(b) {
167214
var x1 = this.x.toBigInteger();
168215
var y1 = this.y.toBigInteger();
169216

170-
var v2 = v.square();
217+
var v2 = v.pow(2);
171218
var v3 = v2.multiply(v);
172219
var x1v2 = x1.multiply(v2);
173-
var zu2 = u.square().multiply(this.z);
220+
var zu2 = u.pow(2).multiply(this.z);
174221

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

202249
// w = 3 * x1^2 + a * z1^2
203-
var w = x1.square().multiply(THREE);
250+
var w = x1.pow(2).multiply(THREE);
204251
if (!BigInteger.ZERO.equals(a)) {
205-
w = w.add(this.z.square().multiply(a));
252+
w = w.add(this.z.pow(2).multiply(a));
206253
}
207254
w = w.mod(this.curve.p);
208255
//this.curve.reduce(w);
209256
// x3 = 2 * y1 * z1 * (w^2 - 8 * x1 * y1^2 * z1)
210-
var x3 = w.square().subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.p);
257+
var x3 = w.pow(2).subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.p);
211258
// y3 = 4 * y1^2 * z1 * (3 * w * x1 - 2 * y1^2 * z1) - w^3
212-
var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.square().multiply(w)).mod(this.curve.p);
259+
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);
213260
// z3 = 8 * (y1 * z1)^3
214-
var z3 = y1z1.square().multiply(y1z1).shiftLeft(3).mod(this.curve.p);
261+
var z3 = y1z1.pow(2).multiply(y1z1).shiftLeft(3).mod(this.curve.p);
215262

216263
return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
217264
}

lib/deps/forge.js

+29-33
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,25 @@
55
*/
66
"use strict";
77

8-
var forge = {
9-
aes: require("node-forge/js/aes"),
10-
asn1: require("node-forge/js/asn1"),
11-
cipher: require("node-forge/js/cipher"),
12-
hmac: require("node-forge/js/hmac"),
13-
jsbn: require("node-forge/js/jsbn"),
14-
md: require("node-forge/js/md"),
15-
mgf: require("node-forge/js/mgf"),
16-
pem: require("node-forge/js/pem"),
17-
pkcs1: require("node-forge/js/pkcs1"),
18-
pkcs5: require("node-forge/js/pkcs5"),
19-
pkcs7: require("node-forge/js/pkcs7"),
20-
pki: require("node-forge/js/x509"),
21-
prime: require("node-forge/js/prime"),
22-
prng: require("node-forge/js/prng"),
23-
pss: require("node-forge/js/pss"),
24-
random: require("node-forge/js/random"),
25-
util: require("node-forge/js/util")
26-
};
27-
28-
// load hash algorithms
29-
require("node-forge/js/sha1");
30-
require("node-forge/js/sha256");
31-
require("node-forge/js/sha512");
32-
33-
// load symmetric cipherModes
34-
require("node-forge/js/cipherModes");
35-
36-
// load AES cipher suites
37-
// TODO: move this to a separate file
38-
require("node-forge/js/aesCipherSuites");
8+
var forge = require("node-forge/lib/forge");
9+
require("node-forge/lib/aes");
10+
require("node-forge/lib/asn1");
11+
require("node-forge/lib/cipher");
12+
require("node-forge/lib/hmac");
13+
require("node-forge/lib/mgf1");
14+
require("node-forge/lib/pbkdf2");
15+
require("node-forge/lib/pem");
16+
require("node-forge/lib/pkcs1");
17+
require("node-forge/lib/pkcs7");
18+
require("node-forge/lib/pki");
19+
require("node-forge/lib/prime");
20+
require("node-forge/lib/prng");
21+
require("node-forge/lib/pss");
22+
require("node-forge/lib/random");
23+
require("node-forge/lib/sha1");
24+
require("node-forge/lib/sha256");
25+
require("node-forge/lib/sha512");
26+
require("node-forge/lib/util");
3927

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

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

53-
modeRaw.prototype.encrypt = function(input, output) {
41+
modeRaw.prototype.encrypt = function(input, output, finish) {
42+
if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
43+
return true;
44+
}
45+
5446
var i;
5547

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

70-
modeRaw.prototype.decrypt = function(input, output) {
62+
modeRaw.prototype.decrypt = function(input, output, finish) {
63+
if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
64+
return true;
65+
}
66+
7167
var i;
7268

7369
// get next block

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"dependencies": {
2929
"base64url": "^2.0.0",
3030
"es6-promise": "^4.0.5",
31-
"jsbn": "^1.1.0",
3231
"lodash.assign": "^4.0.8",
3332
"lodash.clone": "^4.3.2",
3433
"lodash.fill": "^3.2.2",
@@ -40,7 +39,7 @@
4039
"lodash.pick": "^4.2.0",
4140
"lodash.uniq": "^4.2.1",
4241
"long": "^3.1.0",
43-
"node-forge": "https://github.com/linuxwolf/forge/archive/browserify.tar.gz",
42+
"node-forge": "^0.7.1",
4443
"uuid": "^3.0.1"
4544
},
4645
"devDependencies": {

0 commit comments

Comments
 (0)