Skip to content

Commit 77f1776

Browse files
authored
Merge pull request #651 from Kr0emer/fix/bug-007-isprobableprime-negative
fix(jsbn2): reject non-positive values in primality checks
2 parents 5ea1c32 + 02fa75d commit 77f1776

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

ext/jsbn2.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ var lplim = (1<<26)/lowprimes[lowprimes.length-1];
558558

559559
// (public) test primality with certainty >= 1-.5^t
560560
function bnIsProbablePrime(t) {
561+
if(this.signum() <= 0) return false;
561562
var i, x = this.abs();
562563
if(x.t == 1 && x[0] <= lowprimes[lowprimes.length-1]) {
563564
for(i = 0; i < lowprimes.length; ++i)
@@ -577,6 +578,7 @@ function bnIsProbablePrime(t) {
577578

578579
// (protected) true if probably prime (HAC 4.24, Miller-Rabin)
579580
function bnpMillerRabin(t) {
581+
if(this.signum() <= 0) return false;
580582
var n1 = this.subtract(BigInteger.ONE);
581583
var k = n1.getLowestSetBit();
582584
if(k <= 0) return false;

test/qunit-do-crypto.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@
248248
equal(n, 100, "100 times success:" + n0 + ":" + n1 + ":" + n2 + ":" + n3);
249249
});
250250

251+
test("BigInteger.isProbablePrime rejects negative values", function() {
252+
equal(new BigInteger("-2", 10).isProbablePrime(20), false, "-2 is not prime");
253+
equal(new BigInteger("-7", 10).isProbablePrime(20), false, "-7 is not prime");
254+
equal(new BigInteger("-97", 10).isProbablePrime(20), false, "-97 is not prime");
255+
});
256+
257+
test("BigInteger.isProbablePrime control values", function() {
258+
equal(new BigInteger("7", 10).isProbablePrime(20), true, "7 is prime");
259+
equal(new BigInteger("4", 10).isProbablePrime(20), false, "4 is composite");
260+
});
261+
251262
test("RSASetPublic rejects zero modulus", function() {
252263
throws(function() {
253264
var pub = new RSAKey();

0 commit comments

Comments
 (0)