Search in sources :

Example 1 with NativeBigInteger

use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.

the class ECUtil method addPoint.

private static ECPoint addPoint(ECPoint r, ECPoint s, EllipticCurve curve) {
    if (r.equals(s))
        return doublePoint(r, curve);
    else if (r.equals(ECPoint.POINT_INFINITY))
        return s;
    else if (s.equals(ECPoint.POINT_INFINITY))
        return r;
    BigInteger prime = ((ECFieldFp) curve.getField()).getP();
    // use NBI modInverse();
    BigInteger tmp = r.getAffineX().subtract(s.getAffineX());
    tmp = new NativeBigInteger(tmp);
    BigInteger slope = (r.getAffineY().subtract(s.getAffineY())).multiply(tmp.modInverse(prime)).mod(prime);
    slope = new NativeBigInteger(slope);
    BigInteger xOut = (slope.modPow(TWO, prime).subtract(r.getAffineX())).subtract(s.getAffineX()).mod(prime);
    BigInteger yOut = s.getAffineY().negate().mod(prime);
    yOut = yOut.add(slope.multiply(s.getAffineX().subtract(xOut))).mod(prime);
    ECPoint out = new ECPoint(xOut, yOut);
    return out;
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) ECFieldFp(java.security.spec.ECFieldFp) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPoint(java.security.spec.ECPoint)

Example 2 with NativeBigInteger

use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.

the class ECUtil method doublePoint.

private static ECPoint doublePoint(ECPoint r, EllipticCurve curve) {
    if (r.equals(ECPoint.POINT_INFINITY))
        return r;
    BigInteger slope = (r.getAffineX().pow(2)).multiply(THREE);
    slope = slope.add(curve.getA());
    BigInteger prime = ((ECFieldFp) curve.getField()).getP();
    // use NBI modInverse();
    BigInteger tmp = r.getAffineY().multiply(TWO);
    tmp = new NativeBigInteger(tmp);
    slope = slope.multiply(tmp.modInverse(prime));
    BigInteger xOut = slope.pow(2).subtract(r.getAffineX().multiply(TWO)).mod(prime);
    BigInteger yOut = (r.getAffineY().negate()).add(slope.multiply(r.getAffineX().subtract(xOut))).mod(prime);
    ECPoint out = new ECPoint(xOut, yOut);
    return out;
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) ECFieldFp(java.security.spec.ECFieldFp) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPoint(java.security.spec.ECPoint)

Example 3 with NativeBigInteger

use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.

the class KeyGenerator method generatePKIKeys.

/**
 *  Same as above but different return type
 *  @since 0.8.7
 */
public SimpleDataStructure[] generatePKIKeys() {
    BigInteger a = new NativeBigInteger(getElGamalExponentSize(), _context.random());
    BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
    SimpleDataStructure[] keys = new SimpleDataStructure[2];
    keys[0] = new PublicKey();
    keys[1] = new PrivateKey();
    try {
        keys[0].setData(SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES));
        keys[1].setData(SigUtil.rectify(a, PrivateKey.KEYSIZE_BYTES));
    } catch (InvalidKeyException ike) {
        throw new IllegalArgumentException(ike);
    }
    return keys;
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) PrivateKey(net.i2p.data.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) PublicKey(net.i2p.data.PublicKey) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) InvalidKeyException(java.security.InvalidKeyException) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 4 with NativeBigInteger

use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.

the class KeyGenerator method generateSigningKeys.

/**
 *  DSA-SHA1 only.
 *
 *  Same as above but different return type
 *  @since 0.8.7
 */
public SimpleDataStructure[] generateSigningKeys() {
    SimpleDataStructure[] keys = new SimpleDataStructure[2];
    BigInteger x = null;
    // make sure the random key is less than the DSA q and greater than zero
    do {
        x = new NativeBigInteger(160, _context.random());
    } while (x.compareTo(CryptoConstants.dsaq) >= 0 || x.equals(BigInteger.ZERO));
    BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
    keys[0] = new SigningPublicKey();
    keys[1] = new SigningPrivateKey();
    try {
        keys[0].setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
        keys[1].setData(SigUtil.rectify(x, SigningPrivateKey.KEYSIZE_BYTES));
    } catch (InvalidKeyException ike) {
        throw new IllegalStateException(ike);
    }
    return keys;
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) NativeBigInteger(net.i2p.util.NativeBigInteger) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) InvalidKeyException(java.security.InvalidKeyException) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 5 with NativeBigInteger

use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.

the class SigUtil method toJavaRSAKey.

/**
 *  @deprecated unused
 */
public static RSAPublicKey toJavaRSAKey(SigningPublicKey pk) throws GeneralSecurityException {
    SigType type = pk.getType();
    KeyFactory kf = KeyFactory.getInstance("RSA");
    BigInteger n = new NativeBigInteger(1, pk.getData());
    BigInteger e = ((RSAKeyGenParameterSpec) type.getParams()).getPublicExponent();
    // modulus exponent
    KeySpec ks = new RSAPublicKeySpec(n, e);
    return (RSAPublicKey) kf.generatePublic(ks);
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) RSAPublicKey(java.security.interfaces.RSAPublicKey) 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) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) 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