use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class SigUtil method toJavaDSAKey.
public static DSAPrivateKey toJavaDSAKey(SigningPrivateKey pk) throws GeneralSecurityException {
KeyFactory kf = KeyFactory.getInstance("DSA");
// x p q g
KeySpec ks = new DSAPrivateKeySpec(new NativeBigInteger(1, pk.getData()), CryptoConstants.dsap, CryptoConstants.dsaq, CryptoConstants.dsag);
return (DSAPrivateKey) kf.generatePrivate(ks);
}
use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class YKGenerator method generateYK.
/**
* @return rv[0] = Y; rv[1] = K
*/
private final BigInteger[] generateYK() {
NativeBigInteger k = null;
BigInteger y = null;
// long t1 = 0;
while (k == null) {
// t0 = Clock.getInstance().now();
k = new NativeBigInteger(ctx.keyGenerator().getElGamalExponentSize(), ctx.random());
// t1 = Clock.getInstance().now();
if (BigInteger.ZERO.compareTo(k) == 0) {
k = null;
continue;
}
BigInteger kPlus2 = k.add(_two);
if (kPlus2.compareTo(CryptoConstants.elgp) > 0)
k = null;
}
// long t2 = Clock.getInstance().now();
y = CryptoConstants.elgg.modPow(k, CryptoConstants.elgp);
BigInteger[] yk = new BigInteger[2];
yk[0] = y;
yk[1] = k;
return yk;
}
use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class ElGamalSigEngine method engineVerify.
/**
* @param sigBytes ASN.1 R,S
*/
@Override
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
BigInteger elgp = key.getParams().getP();
BigInteger pm1 = elgp.subtract(BigInteger.ONE);
BigInteger elgg = key.getParams().getG();
BigInteger y = ((ElGamalPublicKey) key).getY();
if (!(y instanceof NativeBigInteger))
y = new NativeBigInteger(y);
byte[] data = digest.digest();
try {
BigInteger[] rs = SigUtil.aSN1ToBigInteger(sigBytes, 256);
BigInteger r = rs[0];
BigInteger s = rs[1];
if (r.signum() != 1 || s.signum() != 1 || r.compareTo(elgp) != -1 || s.compareTo(pm1) != -1)
return false;
NativeBigInteger h = new NativeBigInteger(1, data);
BigInteger modvalr = r.modPow(s, elgp);
BigInteger modvaly = y.modPow(r, elgp);
BigInteger modmulval = modvalr.multiply(modvaly).mod(elgp);
BigInteger v = elgg.modPow(h, elgp);
boolean ok = v.compareTo(modmulval) == 0;
return ok;
} catch (RuntimeException e) {
throw new SignatureException("verify", e);
}
}
use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class ElGamalSigEngine method engineSign.
/**
* @return ASN.1 R,S
*/
@Override
protected byte[] engineSign() throws SignatureException {
BigInteger elgp = key.getParams().getP();
BigInteger pm1 = elgp.subtract(BigInteger.ONE);
BigInteger elgg = key.getParams().getG();
BigInteger x = ((ElGamalPrivateKey) key).getX();
if (!(x instanceof NativeBigInteger))
x = new NativeBigInteger(x);
byte[] data = digest.digest();
BigInteger k;
boolean ok;
do {
k = new BigInteger(2048, RandomSource.getInstance());
ok = k.compareTo(pm1) == -1;
ok = ok && k.compareTo(BigInteger.ONE) == 1;
ok = ok && k.gcd(pm1).equals(BigInteger.ONE);
} while (!ok);
BigInteger r = elgg.modPow(k, elgp);
BigInteger kinv = k.modInverse(pm1);
BigInteger h = new NativeBigInteger(1, data);
BigInteger s = (kinv.multiply(h.subtract(x.multiply(r)))).mod(pm1);
// todo if s == 0 go around again
byte[] rv;
try {
rv = SigUtil.sigBytesToASN1(r, s);
} catch (IllegalArgumentException iae) {
throw new SignatureException("ASN1", iae);
}
return rv;
}
use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class SigUtil method cvtToJavaECKey.
private static ECPrivateKey cvtToJavaECKey(SigningPrivateKey pk) throws GeneralSecurityException {
SigType type = pk.getType();
byte[] b = pk.getData();
BigInteger s = new NativeBigInteger(1, b);
// see ECConstants re: casting
ECPrivateKeySpec ks = new ECPrivateKeySpec(s, (ECParameterSpec) type.getParams());
KeyFactory kf = KeyFactory.getInstance("EC");
return (ECPrivateKey) kf.generatePrivate(ks);
}
Aggregations