Skip to content

Commit ee4b013

Browse files
authored
Merge pull request #647 from Kr0emer/fix/bug-003-dsa-nonce-compareto
fix(crypto): correct compareTo checks in BigInteger RNG helpers
2 parents 37b4c06 + d89f0ec commit ee4b013

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/crypto-1.1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ KJUR.crypto.Util.getRandomBigIntegerZeroToMax = function(biMax) {
391391
var bitLenMax = biMax.bitLength();
392392
while (1) {
393393
var biRand = KJUR.crypto.Util.getRandomBigIntegerOfNbits(bitLenMax);
394-
if (biMax.compareTo(biRand) != -1) return biRand;
394+
if (biMax.compareTo(biRand) >= 0) return biRand;
395395
}
396396
};
397397

@@ -415,7 +415,7 @@ KJUR.crypto.Util.getRandomBigIntegerZeroToMax = function(biMax) {
415415
*/
416416
KJUR.crypto.Util.getRandomBigIntegerMinToMax = function(biMin, biMax) {
417417
var flagCompare = biMin.compareTo(biMax);
418-
if (flagCompare == 1) throw "biMin is greater than biMax";
418+
if (flagCompare > 0) throw "biMin is greater than biMax";
419419
if (flagCompare == 0) return biMin;
420420

421421
var biDiff = biMax.subtract(biMin);

test/qunit-do-crypto.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,22 @@
211211
equal(n, 1000, "1000 times success:" + n0 + ":" + n1 + ":" + n2 + ":" + n3);
212212
});
213213

214+
test("Util.getRandomBigIntegerZeroToMax rejects out-of-range candidate", function() {
215+
var fOrig = KJUR.crypto.Util.getRandomBigIntegerOfNbits;
216+
var aBiRand = [new BigInteger("200", 10), new BigInteger("42", 10)];
217+
var idx = 0;
218+
KJUR.crypto.Util.getRandomBigIntegerOfNbits = function(n) {
219+
return aBiRand[idx++];
220+
};
221+
try {
222+
var bi = KJUR.crypto.Util.getRandomBigIntegerZeroToMax(new BigInteger("100", 10));
223+
equal(bi.toString(10), "42", "returns in-range value");
224+
equal(idx >= 2, true, "retries when out-of-range candidate appears");
225+
} finally {
226+
KJUR.crypto.Util.getRandomBigIntegerOfNbits = fOrig;
227+
}
228+
});
229+
214230
test("Util.getRandomBigIntegerMinToMax", function() {
215231
var bi15 = new BigInteger("15", 10);
216232
var bi18 = new BigInteger("18", 10);
@@ -270,6 +286,22 @@
270286
equal(new BigInteger("-13", 10).modInverse(biM2).toString(10), "82", "-13 mod 97");
271287
});
272288

289+
test("Util.getRandomBigIntegerMinToMax throws for any positive compareTo", function() {
290+
var biMin = {
291+
compareTo: function(a) { return 2; }
292+
};
293+
var biMax = {
294+
subtract: function(a) { throw "subtract must not be called"; }
295+
};
296+
var ex = null;
297+
try {
298+
KJUR.crypto.Util.getRandomBigIntegerMinToMax(biMin, biMax);
299+
} catch (err) {
300+
ex = err;
301+
}
302+
equal(ex, "biMin is greater than biMax", "throws before subtraction");
303+
});
304+
273305
test("MessageDigest test", function() {
274306
expect(10);
275307
var md1 = new KJUR.crypto.MessageDigest({"alg": "sha1", "prov": "cryptojs"});

0 commit comments

Comments
 (0)