Search in sources :

Example 6 with NativeBigInteger

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);
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 7 with NativeBigInteger

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;
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger)

Example 8 with NativeBigInteger

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);
    }
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) SignatureException(java.security.SignatureException)

Example 9 with NativeBigInteger

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;
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) SignatureException(java.security.SignatureException)

Example 10 with NativeBigInteger

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);
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) KeyFactory(java.security.KeyFactory)

Aggregations

NativeBigInteger (net.i2p.util.NativeBigInteger)19 BigInteger (java.math.BigInteger)15 KeyFactory (java.security.KeyFactory)5 RSAPublicKey (java.security.interfaces.RSAPublicKey)4 ECPoint (java.security.spec.ECPoint)4 ECPrivateKeySpec (java.security.spec.ECPrivateKeySpec)4 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)4 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)4 EdDSAPublicKey (net.i2p.crypto.eddsa.EdDSAPublicKey)4 EdDSAPublicKeySpec (net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec)4 SigningPublicKey (net.i2p.data.SigningPublicKey)4 InvalidKeyException (java.security.InvalidKeyException)3 ECPrivateKey (java.security.interfaces.ECPrivateKey)3 ECPublicKey (java.security.interfaces.ECPublicKey)3 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)3 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)3 KeySpec (java.security.spec.KeySpec)3 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)3 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)3 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)3