wwwtyro / cryptico

An easy-to-use encryption system utilizing RSA and AES for javascript.

Home Page:http://wwwtyro.github.com/cryptico

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bnpFromNumber logical bug

beenotung opened this issue · comments

Bug location

file cryptico/lib/cryptico.js at line 749

function bnpFromNumber(a, b, c) {
    if ("number" == typeof b) {
        // new BigInteger(int,int,RNG)
        if (a < 2) this.fromInt(1);
        else {
            this.fromNumber(a, c);
            if (!this.testBit(a - 1)) // force MSB set
            this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
            if (this.isEven()) this.dAddOffset(1, 0); // force odd
            while (!this.isProbablePrime(b)) {
                this.dAddOffset(2, 0);
                if (this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
            }
        }
    }
    else {
        // new BigInteger(int,RNG)
        var x = new Array(),
            t = a & 7;
        x.length = (a >> 3) + 1;
        b.nextBytes(x);
        if (t > 0) x[0] &= ((1 << t) - 1);
        else x[0] = 0;
        this.fromString(x, 256);
    }
}

Explanation

If the argument b is of type number, it will crash when running line 769 b.nextBytes(x) since number don't have method nextBytes/1

Steps to product the bug

var key = cryptico.generateRSAKey("test",2048);
key.generate(2048,3)

test case output

> key = lib.default.generateRSAKey("test",2048); key.generate(2048,3)
TypeError: Cannot read property 'nextBytes' of undefined
    at BigInteger.bnpFromNumber [as fromNumber] (/home/beenotung/workspace/github.com/beenotung/typestub-cryptico/node_modules/cryptico/lib/cryptico.js:769:10)
    at BigInteger.bnpFromNumber [as fromNumber] (/home/beenotung/workspace/github.com/beenotung/typestub-cryptico/node_modules/cryptico/lib/cryptico.js:754:18)
    at new BigInteger (/home/beenotung/workspace/github.com/beenotung/typestub-cryptico/node_modules/cryptico/lib/cryptico.js:12:51)
    at RSAKey.RSAGenerate [as generate] (/home/beenotung/workspace/github.com/beenotung/typestub-cryptico/node_modules/cryptico/lib/cryptico.js:2228:14)
    at repl:1:52
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:440:10)