Search in sources :

Example 61 with EllipticCurve

use of java.security.spec.EllipticCurve in project jmulticard by ctt-gob-es.

the class JseCryptoHelper method toSpongyCastleECCurve.

private static ECCurve toSpongyCastleECCurve(final ECParameterSpec params) {
    final EllipticCurve curve = params.getCurve();
    final ECField field = curve.getField();
    if (!(field instanceof ECFieldFp)) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "Solo se soporta 'ECFieldFp' y se proporciono  " + field.getClass().getCanonicalName());
    }
    final int coFactor = params.getCofactor();
    final BigInteger order = params.getOrder();
    final BigInteger a = curve.getA();
    final BigInteger b = curve.getB();
    final BigInteger p = getPrime(params);
    return new ECCurve.Fp(p, a, b, order, BigInteger.valueOf(coFactor));
}
Also used : ECField(java.security.spec.ECField) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) BigInteger(java.math.BigInteger) ECFieldFp(java.security.spec.ECFieldFp) ECPoint(java.security.spec.ECPoint)

Example 62 with EllipticCurve

use of java.security.spec.EllipticCurve in project jmulticard by ctt-gob-es.

the class JseCryptoHelper method mapNonceGMWithECDH.

private static ECParameterSpec mapNonceGMWithECDH(final BigInteger nonceS, final ECPoint sharedSecretPointH, final ECParameterSpec params) {
    // D~ = (p, a, b, G~, n, h) where G~ = [s]G + H
    final ECPoint generator = params.getGenerator();
    final EllipticCurve curve = params.getCurve();
    final BigInteger a = curve.getA();
    final BigInteger b = curve.getB();
    final ECFieldFp field = (ECFieldFp) curve.getField();
    final BigInteger p = field.getP();
    final BigInteger order = params.getOrder();
    final int cofactor = params.getCofactor();
    final ECPoint ephemeralGenerator = add(multiply(nonceS, generator, params), sharedSecretPointH, params);
    if (!toSpongyCastleECPoint(ephemeralGenerator, params).isValid()) {
        // $NON-NLS-1$
        LOGGER.warning("Se ha generado un punto invalido");
    }
    return new ECParameterSpec(new EllipticCurve(new ECFieldFp(p), a, b), ephemeralGenerator, order, cofactor);
}
Also used : ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) ECPoint(java.security.spec.ECPoint)

Example 63 with EllipticCurve

use of java.security.spec.EllipticCurve in project i2p.i2p by i2p.

the class KeyGenerator method getSigningPublicKey.

/**
 * Convert a SigningPrivateKey to a SigningPublicKey.
 *  As of 0.9.16, supports all key types.
 *
 * @param priv a SigningPrivateKey object
 * @return a SigningPublicKey object
 * @throws IllegalArgumentException on bad key or unknown type
 */
public static SigningPublicKey getSigningPublicKey(SigningPrivateKey priv) {
    SigType type = priv.getType();
    if (type == null)
        throw new IllegalArgumentException("Unknown type");
    try {
        switch(type.getBaseAlgorithm()) {
            case DSA:
                BigInteger x = new NativeBigInteger(1, priv.toByteArray());
                BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
                SigningPublicKey pub = new SigningPublicKey();
                pub.setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
                return pub;
            case EC:
                ECPrivateKey ecpriv = SigUtil.toJavaECKey(priv);
                BigInteger s = ecpriv.getS();
                ECParameterSpec spec = (ECParameterSpec) type.getParams();
                EllipticCurve curve = spec.getCurve();
                ECPoint g = spec.getGenerator();
                ECPoint w = ECUtil.scalarMult(g, s, curve);
                ECPublicKeySpec ecks = new ECPublicKeySpec(w, ecpriv.getParams());
                KeyFactory eckf = KeyFactory.getInstance("EC");
                ECPublicKey ecpub = (ECPublicKey) eckf.generatePublic(ecks);
                return SigUtil.fromJavaKey(ecpub, type);
            case RSA:
                RSAPrivateKey rsapriv = SigUtil.toJavaRSAKey(priv);
                BigInteger exp = ((RSAKeyGenParameterSpec) type.getParams()).getPublicExponent();
                RSAPublicKeySpec rsaks = new RSAPublicKeySpec(rsapriv.getModulus(), exp);
                KeyFactory rsakf = KeyFactory.getInstance("RSA");
                RSAPublicKey rsapub = (RSAPublicKey) rsakf.generatePublic(rsaks);
                return SigUtil.fromJavaKey(rsapub, type);
            case EdDSA:
                EdDSAPrivateKey epriv = SigUtil.toJavaEdDSAKey(priv);
                EdDSAPublicKey epub = new EdDSAPublicKey(new EdDSAPublicKeySpec(epriv.getA(), epriv.getParams()));
                return SigUtil.fromJavaKey(epub, type);
            default:
                throw new IllegalArgumentException("Unsupported algorithm");
        }
    } catch (GeneralSecurityException gse) {
        throw new IllegalArgumentException("Conversion failed", gse);
    }
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) NativeBigInteger(net.i2p.util.NativeBigInteger) GeneralSecurityException(java.security.GeneralSecurityException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) EdDSAPublicKeySpec(net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) EllipticCurve(java.security.spec.EllipticCurve) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 64 with EllipticCurve

use of java.security.spec.EllipticCurve in project wycheproof by google.

the class EcUtil method getBrainpoolP224r1Params.

public static ECParameterSpec getBrainpoolP224r1Params() {
    // name = "brainpoolP224r1",
    // oid = '2b2403030208010105',
    // ref = "RFC 5639",
    BigInteger p = new BigInteger("D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF", 16);
    BigInteger a = new BigInteger("68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43", 16);
    BigInteger b = new BigInteger("2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B", 16);
    BigInteger x = new BigInteger("0D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D", 16);
    BigInteger y = new BigInteger("58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD", 16);
    BigInteger n = new BigInteger("D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F", 16);
    final int h = 1;
    ECFieldFp fp = new ECFieldFp(p);
    EllipticCurve curve = new EllipticCurve(fp, a, b);
    ECPoint g = new ECPoint(x, y);
    return new ECParameterSpec(curve, g, n, h);
}
Also used : ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) ECPoint(java.security.spec.ECPoint)

Example 65 with EllipticCurve

use of java.security.spec.EllipticCurve in project wycheproof by google.

the class EcUtil method decompressPoint.

/**
 * Decompress a point on an elliptic curve.
 *
 * @param bytes The compressed point. Its representation is z || x where z is 2+lsb(y) and x is
 *     using a unsigned fixed length big-endian representation.
 * @param ecParams the specification of the curve. Only Weierstrass curves over prime order fields
 *     are implemented.
 */
public static ECPoint decompressPoint(byte[] bytes, ECParameterSpec ecParams) throws GeneralSecurityException {
    EllipticCurve ec = ecParams.getCurve();
    ECField field = ec.getField();
    if (!(field instanceof ECFieldFp)) {
        throw new GeneralSecurityException("Only curves over prime order fields are supported");
    }
    BigInteger p = ((java.security.spec.ECFieldFp) field).getP();
    int expectedLength = 1 + (p.bitLength() + 7) / 8;
    if (bytes.length != expectedLength) {
        throw new GeneralSecurityException("compressed point has wrong length");
    }
    boolean lsb;
    switch(bytes[0]) {
        case 2:
            lsb = false;
            break;
        case 3:
            lsb = true;
            break;
        default:
            throw new GeneralSecurityException("Invalid format");
    }
    BigInteger x = new BigInteger(1, Arrays.copyOfRange(bytes, 1, bytes.length));
    if (x.compareTo(BigInteger.ZERO) == -1 || x.compareTo(p) != -1) {
        throw new GeneralSecurityException("x is out of range");
    }
    // Compute rhs == x^3 + a x + b (mod p)
    BigInteger rhs = x.multiply(x).add(ec.getA()).multiply(x).add(ec.getB()).mod(p);
    BigInteger y = modSqrt(rhs, p);
    if (lsb != y.testBit(0)) {
        y = p.subtract(y).mod(p);
    }
    return new ECPoint(x, y);
}
Also used : ECField(java.security.spec.ECField) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) ECPoint(java.security.spec.ECPoint) ECPoint(java.security.spec.ECPoint)

Aggregations

EllipticCurve (java.security.spec.EllipticCurve)78 BigInteger (java.math.BigInteger)48 ECFieldFp (java.security.spec.ECFieldFp)43 ECPoint (java.security.spec.ECPoint)30 ECFieldF2m (java.security.spec.ECFieldF2m)28 ECParameterSpec (java.security.spec.ECParameterSpec)23 GeneralSecurityException (java.security.GeneralSecurityException)7 ECField (java.security.spec.ECField)7 ECCurve (org.bouncycastle.math.ec.ECCurve)7 X962Parameters (org.bouncycastle.asn1.x9.X962Parameters)6 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)6 ECNamedCurveSpec (org.bouncycastle.jce.spec.ECNamedCurveSpec)6 KeyPair (java.security.KeyPair)5 KeyPairGenerator (java.security.KeyPairGenerator)5 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)5 IOException (java.io.IOException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 ECPublicKey (java.security.interfaces.ECPublicKey)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 ECPrivateKey (java.security.interfaces.ECPrivateKey)3