Skip to content

Commit 901d915

Browse files
authored
Merge pull request from GHSA-5h4j-qrvg-9xhw
1 parent e95481a commit 901d915

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/deps/ecc/math.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function barrettRevert(x) { return x; }
4646

4747
// x = x mod m (HAC 14.42)
4848
function barrettReduce(x) {
49+
if (x.s < 0) { throw Error("Barrett reduction on negative input"); }
4950
x.drShiftTo(this.m.t-1,this.r2);
5051
if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
5152
this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);

lib/deps/forge.js

+9
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,13 @@ modeRaw.prototype.decrypt = function(input, output, finish) {
8888
forge.cipher.registerAlgorithm(name, factory);
8989
})();
9090

91+
// Ensure that the jsbn modInverse function always returns a positive result
92+
const originalModInverse = forge.jsbn.BigInteger.prototype.modInverse;
93+
const positiveModInverse = function(m) {
94+
const inv = originalModInverse.apply(this, [m]);
95+
return inv.mod(m);
96+
}
97+
98+
forge.jsbn.BigInteger.prototype.modInverse = positiveModInverse;
99+
91100
module.exports = forge;

test/algorithms/ecc-test.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*!
2+
*
3+
* Copyright (c) 2015 Cisco Systems, Inc. See LICENSE file.
4+
*/
5+
"use strict";
6+
7+
var assert = require("chai").assert;
8+
9+
const CURVES = require('../../lib/deps/ecc/curves.js');
10+
const BigInteger = require('../../lib/deps/forge').jsbn.BigInteger;
11+
12+
describe("ecc/positive", function() {
13+
const negativeModInverseCases = [
14+
'101067240514044546216936289506154965497874315269115226505131909313278720169941',
15+
'47260992668897782856940293132731814279826643476197468731642996160637470667669',
16+
]
17+
18+
const p = CURVES["P-256"].curve.p;
19+
20+
const runner = () => {
21+
for (const kStr of negativeModInverseCases) {
22+
const k = new BigInteger(kStr);
23+
const kinv = k.modInverse(p);
24+
assert.isAtLeast(kinv.s, 0, "Negative mod inverse");
25+
}
26+
};
27+
28+
it('normalizes negative modular inverses', runner);
29+
})

0 commit comments

Comments
 (0)