Search in sources :

Example 21 with EllipticCurve

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

the class EcUtil method getPoint.

/**
 * Decompress a point
 *
 * @param x The x-coordinate of the point
 * @param bit0 true if the least significant bit of y is set.
 * @param ecParams contains the curve of the point. This must be over a prime order field.
 */
public static ECPoint getPoint(BigInteger x, boolean bit0, 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();
    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 (bit0 != 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)

Example 22 with EllipticCurve

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

the class EcUtil method printParameters.

public static void printParameters(ECParameterSpec spec) {
    System.out.println("cofactor:" + spec.getCofactor());
    EllipticCurve curve = spec.getCurve();
    System.out.println("A:" + curve.getA());
    System.out.println("B:" + curve.getB());
    ECField field = curve.getField();
    System.out.println("field size:" + field.getFieldSize());
    if (field instanceof ECFieldFp) {
        ECFieldFp fp = (ECFieldFp) field;
        System.out.println("P:" + fp.getP());
    }
    ECPoint generator = spec.getGenerator();
    System.out.println("Gx:" + generator.getAffineX());
    System.out.println("Gy:" + generator.getAffineY());
    System.out.println("order:" + spec.getOrder());
}
Also used : ECField(java.security.spec.ECField) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) ECPoint(java.security.spec.ECPoint)

Example 23 with EllipticCurve

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

the class EcUtil method getWeakPublicKey.

/**
 * Returns a weak public key of order 3 such that the public key point is on the curve specified
 * in ecParams. This method is used to check ECC implementations for missing step in the
 * verification of the public key. E.g. implementations of ECDH must verify that the public key
 * contains a point on the curve as well as public and secret key are using the same curve.
 *
 * @param ecParams the parameters of the key to attack. This must be a curve in Weierstrass form
 *     over a prime order field.
 * @return a weak EC group with a genrator of order 3.
 */
public static ECPublicKeySpec getWeakPublicKey(ECParameterSpec ecParams) throws GeneralSecurityException {
    EllipticCurve curve = ecParams.getCurve();
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
    keyGen.initialize(ecParams);
    BigInteger p = getModulus(curve);
    BigInteger three = new BigInteger("3");
    while (true) {
        // Generate a point on the original curve
        KeyPair keyPair = keyGen.generateKeyPair();
        ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
        ECPoint w = pub.getW();
        BigInteger x = w.getAffineX();
        BigInteger y = w.getAffineY();
        // Find the curve parameters a,b such that 3*w = infinity.
        // This is the case if the following equations are satisfied:
        // 3x == l^2 (mod p)
        // l == (3x^2 + a) / 2*y (mod p)
        // y^2 == x^3 + ax + b (mod p)
        BigInteger l;
        try {
            l = modSqrt(x.multiply(three), p);
        } catch (GeneralSecurityException ex) {
            continue;
        }
        BigInteger xSqr = x.multiply(x).mod(p);
        BigInteger a = l.multiply(y.add(y)).subtract(xSqr.multiply(three)).mod(p);
        BigInteger b = y.multiply(y).subtract(x.multiply(xSqr.add(a))).mod(p);
        EllipticCurve newCurve = new EllipticCurve(curve.getField(), a, b);
        // Just a sanity check.
        checkPointOnCurve(w, newCurve);
        // Cofactor and order are of course wrong.
        ECParameterSpec spec = new ECParameterSpec(newCurve, w, p, 1);
        return new ECPublicKeySpec(w, spec);
    }
}
Also used : KeyPair(java.security.KeyPair) ECPublicKey(java.security.interfaces.ECPublicKey) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) KeyPairGenerator(java.security.KeyPairGenerator) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec)

Example 24 with EllipticCurve

use of java.security.spec.EllipticCurve in project spring-security by spring-projects.

the class TestKeys method generateEcKeyPair.

static KeyPair generateEcKeyPair() {
    EllipticCurve ellipticCurve = new EllipticCurve(new ECFieldFp(new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853951")), new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853948"), new BigInteger("41058363725152142129326129780047268409114441015993725554835256314039467401291"));
    ECPoint ecPoint = new ECPoint(new BigInteger("48439561293906451759052585252797914202762949526041747995844080717082404635286"), new BigInteger("36134250956749795798585127919587881956611106672985015071877198253568414405109"));
    ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, ecPoint, new BigInteger("115792089210356248762697446949407573529996955224135760342422259061068512044369"), 1);
    KeyPair keyPair;
    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
        keyPairGenerator.initialize(ecParameterSpec);
        keyPair = keyPairGenerator.generateKeyPair();
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
    return keyPair;
}
Also used : KeyPair(java.security.KeyPair) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) KeyPairGenerator(java.security.KeyPairGenerator) ECPoint(java.security.spec.ECPoint) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 25 with EllipticCurve

use of java.security.spec.EllipticCurve in project dhis2-core by dhis2.

the class KeyGeneratorUtils method generateEcKey.

static KeyPair generateEcKey() {
    EllipticCurve ellipticCurve = new EllipticCurve(new ECFieldFp(new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853951")), new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853948"), new BigInteger("41058363725152142129326129780047268409114441015993725554835256314039467401291"));
    ECPoint ecPoint = new ECPoint(new BigInteger("48439561293906451759052585252797914202762949526041747995844080717082404635286"), new BigInteger("36134250956749795798585127919587881956611106672985015071877198253568414405109"));
    ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, ecPoint, new BigInteger("115792089210356248762697446949407573529996955224135760342422259061068512044369"), 1);
    KeyPair keyPair;
    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
        keyPairGenerator.initialize(ecParameterSpec);
        keyPair = keyPairGenerator.generateKeyPair();
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
    return keyPair;
}
Also used : KeyPair(java.security.KeyPair) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) KeyPairGenerator(java.security.KeyPairGenerator) 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