Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
V = result[1];
if (this.modMult(V, V).equals(fourQ))
{
// Integer division by 2, mod q
if (V.testBit(0))
{
V = V.add(q);
}
V = V.shiftRight(1);
return new ECFieldElementFp(q,V);
}
}
while (U.equals(BigInteger.ONE) || U.equals(qMinusOne));
return null;
}
ECFieldElementFp.prototype.lucasSequence = function(P,Q,k)
});
return (key);
} else {
var ecParams = new X9ECParameters(curve);
/* This algorithm taken from FIPS PUB 186-4 (section B.4.1) */
var n = ecParams.getN();
/*
* The crypto.randomBytes() function can only give us whole
* bytes, so taking a nod from X9.62, we round up.
*/
var cByteLen = Math.ceil((n.bitLength() + 64) / 8);
var c = new jsbn(crypto.randomBytes(cByteLen));
var n1 = n.subtract(jsbn.ONE);
var priv = c.mod(n1).add(jsbn.ONE);
var pub = ecParams.getG().multiply(priv);
priv = Buffer.from(priv.toByteArray());
pub = Buffer.from(ecParams.getCurve().
encodePointHex(pub), 'hex');
parts.push({name: 'curve', data: Buffer.from(curve)});
parts.push({name: 'Q', data: pub});
parts.push({name: 'd', data: priv});
key = new PrivateKey({
type: 'ecdsa',
curve: curve,
parts: parts
});
function secp256r1() {
// p = 2^224 (2^32 - 1) + 2^192 + 2^96 - 1
var p = fromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
var a = fromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
var b = fromHex("5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
//byte[] S = Hex.decode("C49D360886E704936A6678E1139D26B7819F7E90");
var n = fromHex("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
var h = BigInteger.ONE;
var curve = new ECCurveFp(p, a, b);
var G = curve.decodePointHex("04"
+ "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
+ "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
return new X9ECParameters(curve, G, n, h);
}
{
if(isPublic)
{
var curve = c.getCurve();
// var x = key.slice(1,bytes+1); // skip the 04 for uncompressed format
// var y = key.slice(bytes+1);
// this.P = new ECPointFp(curve,
// curve.fromBigInteger(new BigInteger(x.toString("hex"), 16)),
// curve.fromBigInteger(new BigInteger(y.toString("hex"), 16)));
this.P = curve.decodePointHex(key.toString("hex"));
}else{
if(key.length != bytes) return false;
priv = new BigInteger(key.toString("hex"), 16);
}
}else{
var n1 = n.subtract(BigInteger.ONE);
var r = new BigInteger(crypto.randomBytes(n.bitLength()));
priv = r.mod(n1).add(BigInteger.ONE);
this.P = c.getG().multiply(priv);
}
if(this.P)
{
// var pubhex = unstupid(this.P.getX().toBigInteger().toString(16),bytes*2)+unstupid(this.P.getY().toBigInteger().toString(16),bytes*2);
// this.PublicKey = new Buffer("04"+pubhex,"hex");
this.PublicKey = new Buffer(c.getCurve().encodeCompressedPointHex(this.P),"hex");
}
if(priv)
{
this.PrivateKey = new Buffer(unstupid(priv.toString(16),bytes*2),"hex");
this.deriveSharedSecret = function(key)
{
if(!key || !key.P) return false;
{
if(isPublic)
{
var curve = c.getCurve();
// var x = key.slice(1,bytes+1); // skip the 04 for uncompressed format
// var y = key.slice(bytes+1);
// this.P = new ECPointFp(curve,
// curve.fromBigInteger(new BigInteger(x.toString("hex"), 16)),
// curve.fromBigInteger(new BigInteger(y.toString("hex"), 16)));
this.P = curve.decodePointHex(key.toString("hex"));
}else{
if(key.length != bytes) return false;
priv = new BigInteger(key.toString("hex"), 16);
}
}else{
var n1 = n.subtract(BigInteger.ONE);
var r = new BigInteger(crypto.randomBytes(n.bitLength()));
priv = r.mod(n1).add(BigInteger.ONE);
this.P = c.getG().multiply(priv);
}
if(this.P)
{
// var pubhex = unstupid(this.P.getX().toBigInteger().toString(16),bytes*2)+unstupid(this.P.getY().toBigInteger().toString(16),bytes*2);
// this.PublicKey = new Buffer("04"+pubhex,"hex");
this.PublicKey = new Buffer(c.getCurve().encodeCompressedPointHex(this.P),"hex");
}
if(priv)
{
this.PrivateKey = new Buffer(unstupid(priv.toString(16),bytes*2),"hex");
this.deriveSharedSecret = function(key)
{
if(!key || !key.P) return false;
function ECPointFp(curve,x,y,z) {
this.curve = curve;
this.x = x;
this.y = y;
// Projective coordinates: either zinv == null or z * zinv == 1
// z and zinv are just BigIntegers, not fieldElements
if(z == null) {
this.z = BigInteger.ONE;
}
else {
this.z = z;
}
this.zinv = null;
//TODO: compression flag
}
ECFieldElementFp.prototype.lucasSequence = function(P,Q,k)
{
var n = k.bitLength();
var s = k.getLowestSetBit();
var Uh = BigInteger.ONE;
var Vl = BigInteger.TWO;
var Vh = P;
var Ql = BigInteger.ONE;
var Qh = BigInteger.ONE;
for (var j = n - 1; j >= s + 1; --j)
{
Ql = this.modMult(Ql, Qh);
if (k.testBit(j))
{
Qh = this.modMult(Ql, Q);
Uh = this.modMult(Uh, Vh);
Vl = this.modReduce(Vh.multiply(Vl).subtract(P.multiply(Ql)));
Vh = this.modReduce(Vh.multiply(Vh).subtract(Qh.shiftLeft(1)));
}
else
{
Qh = Ql;
function RSAGenerate(B, E) {
var rng = new SecureRandom();
var qs = B >> 1;
this.e = parseInt(E, 16);
var ee = new BigInteger(E, 16);
for (;;) {
for (;;) {
this.p = new BigInteger(B - qs, 1, rng);
if (this.p.subtract(BigInteger.ONE)
.gcd(ee)
.compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break
}
for (;;) {
this.q = new BigInteger(qs, 1, rng);
if (this.q.subtract(BigInteger.ONE)
.gcd(ee)
.compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break
}
if (this.p.compareTo(this.q) <= 0) {
var t = this.p;
this.p = this.q;
this.q = t
}
var p1 = this.p.subtract(BigInteger.ONE);
var q1 = this.q.subtract(BigInteger.ONE);
function bex(a) {
return new noun.Atom.Atom(BigInteger.ONE.shiftLeft(a.number.intValue()));
}
ECFieldElementFp.prototype.getR = function()
{
if(this.r != undefined) return this.r;
this.r = null;
var bitLength = this.q.bitLength();
if (bitLength > 128)
{
var firstWord = this.q.shiftRight(bitLength - 64);
if (firstWord.intValue() == -1)
{
this.r = BigInteger.ONE.shiftLeft(bitLength).subtract(this.q);
}
}
return this.r;
}
ECFieldElementFp.prototype.modMult = function(x1,x2)