Search in sources :

Example 11 with EllipticCurve

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

the class EllipticCurveTest method testEqualsObject01.

/**
     * Test #1 for <code>equals(Object other)</code> method<br>
     * Assertion: return true if this and other objects are equal<br>
     * Test preconditions: see test comments<br>
     * Expected: all objects in this test must be equal
     */
public final void testEqualsObject01() {
    // test case 1: must be equal to itself
    EllipticCurve c2 = null, c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L));
    assertTrue(c1.equals(c1));
    // test case 2: equal objects
    c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L));
    c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.valueOf(1L), BigInteger.valueOf(19L));
    assertTrue(c1.equals(c2) && c2.equals(c1));
    // test case 3: equal objects with seed not null
    c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L), new byte[24]);
    c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.valueOf(1L), BigInteger.valueOf(19L), new byte[24]);
    assertTrue(c1.equals(c2) && c2.equals(c1));
    // test case 4: equal object and subclass object
    c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L), new byte[24]);
    MyEllipticCurve c3 = new MyEllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L), new byte[24]);
    assertTrue(c1.equals(c3) && c3.equals(c1));
    // test case 5: equal objects
    c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L));
    c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)), BigInteger.valueOf(1L), BigInteger.valueOf(19L), null);
    assertTrue(c1.equals(c2) && c2.equals(c1));
}
Also used : ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve)

Example 12 with EllipticCurve

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

the class EllipticCurveTest method testGetField.

/**
     * Test for <code>getField()</code> method<br>
     * Assertion: returns <code>field</code><br>
     * Test preconditions: <code>ECFieldF2m</code> instance
     * created using valid parameters<br>
     * Expected: must return <code>field</code> which is equal
     * to the one passed to the constructor; (both must refer
     * the same object)
     */
public final void testGetField() {
    ECFieldF2m f = new ECFieldF2m(5);
    BigInteger a = BigInteger.valueOf(5L);
    BigInteger b = BigInteger.valueOf(19L);
    EllipticCurve c = new EllipticCurve(f, a, b);
    assertEquals(f, c.getField());
    assertSame(f, c.getField());
}
Also used : EllipticCurve(java.security.spec.EllipticCurve) BigInteger(java.math.BigInteger) ECFieldF2m(java.security.spec.ECFieldF2m)

Example 13 with EllipticCurve

use of java.security.spec.EllipticCurve in project XobotOS by xamarin.

the class EC5Util method convertCurve.

public static EllipticCurve convertCurve(ECCurve curve, byte[] seed) {
    // so at the moment it's set to null. Should probably look at making this configurable
    if (curve instanceof ECCurve.Fp) {
        return new EllipticCurve(new ECFieldFp(((ECCurve.Fp) curve).getQ()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
    } else {
        ECCurve.F2m curveF2m = (ECCurve.F2m) curve;
        int[] ks;
        if (curveF2m.isTrinomial()) {
            ks = new int[] { curveF2m.getK1() };
            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
        } else {
            ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
            return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
        }
    }
}
Also used : ECFieldF2m(java.security.spec.ECFieldF2m) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) ECCurve(org.bouncycastle.math.ec.ECCurve) ECFieldFp(java.security.spec.ECFieldFp) ECFieldF2m(java.security.spec.ECFieldF2m)

Example 14 with EllipticCurve

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

the class JCEECPrivateKey method populateFromPrivKeyInfo.

private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
    X962Parameters params = new X962Parameters((ASN1Primitive) info.getPrivateKeyAlgorithm().getParameters());
    if (params.isNamedCurve()) {
        ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
        // BEGIN android-removed
        // if (ecP == null) // GOST Curve
        // {
        //     ECDomainParameters gParam = ECGOST3410NamedCurves.getByOID(oid);
        //     EllipticCurve ellipticCurve = EC5Util.convertCurve(gParam.getCurve(), gParam.getSeed());
        //
        //     ecSpec = new ECNamedCurveSpec(
        //             ECGOST3410NamedCurves.getName(oid),
        //             ellipticCurve,
        //             new ECPoint(
        //                     gParam.getG().getX().toBigInteger(),
        //                     gParam.getG().getY().toBigInteger()),
        //             gParam.getN(),
        //             gParam.getH());
        // }
        // else
        // END android-removed
        {
            EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
            ecSpec = new ECNamedCurveSpec(ECUtil.getCurveName(oid), ellipticCurve, new ECPoint(ecP.getG().getX().toBigInteger(), ecP.getG().getY().toBigInteger()), ecP.getN(), ecP.getH());
        }
    } else if (params.isImplicitlyCA()) {
        ecSpec = null;
    } else {
        X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
        EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
        this.ecSpec = new ECParameterSpec(ellipticCurve, new ECPoint(ecP.getG().getX().toBigInteger(), ecP.getG().getY().toBigInteger()), ecP.getN(), ecP.getH().intValue());
    }
    ASN1Encodable privKey = info.parsePrivateKey();
    if (privKey instanceof DERInteger) {
        DERInteger derD = DERInteger.getInstance(privKey);
        this.d = derD.getValue();
    } else {
        ECPrivateKeyStructure ec = new ECPrivateKeyStructure((ASN1Sequence) privKey);
        this.d = ec.getKey();
        this.publicKey = ec.getPublicKey();
    }
}
Also used : X962Parameters(org.bouncycastle.asn1.x9.X962Parameters) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ECPrivateKeyStructure(org.bouncycastle.asn1.sec.ECPrivateKeyStructure) ECPoint(java.security.spec.ECPoint) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(org.bouncycastle.jce.spec.ECNamedCurveSpec) DERInteger(org.bouncycastle.asn1.DERInteger)

Example 15 with EllipticCurve

use of java.security.spec.EllipticCurve in project cxf by apache.

the class JweUtils method getECDHKey.

public static byte[] getECDHKey(ECPrivateKey privateKey, ECPublicKey peerPublicKey, byte[] partyUInfo, byte[] partyVInfo, String algoName, int algoKeyBitLen) {
    // Step 1: Verify public key is not point at infinity.
    if (ECPoint.POINT_INFINITY.equals(peerPublicKey.getW())) {
        throw new JweException(JweException.Error.KEY_ENCRYPTION_FAILURE);
    }
    EllipticCurve curve = peerPublicKey.getParams().getCurve();
    final BigInteger x = peerPublicKey.getW().getAffineX();
    final BigInteger y = peerPublicKey.getW().getAffineY();
    final BigInteger p = ((ECFieldFp) curve.getField()).getP();
    // Step 2: Verify x and y are in range [0,p-1]
    if (x.compareTo(BigInteger.ZERO) < 0 || x.compareTo(p) >= 0 || y.compareTo(BigInteger.ZERO) < 0 || y.compareTo(p) >= 0) {
        throw new JweException(JweException.Error.KEY_ENCRYPTION_FAILURE);
    }
    final BigInteger a = curve.getA();
    final BigInteger b = curve.getB();
    // Step 3: Verify that y^2 == x^3 + ax + b (mod p)
    final BigInteger ySquared = y.modPow(BigInteger.valueOf(2), p);
    final BigInteger xCubedPlusAXPlusB = x.modPow(BigInteger.valueOf(3), p).add(a.multiply(x)).add(b).mod(p);
    if (!ySquared.equals(xCubedPlusAXPlusB)) {
        throw new JweException(JweException.Error.KEY_ENCRYPTION_FAILURE);
    }
    // All the NIST curves used here define h = 1.
    if (peerPublicKey.getParams().getCofactor() != 1) {
        throw new JweException(JweException.Error.KEY_ENCRYPTION_FAILURE);
    }
    // Finally calculate the derived key
    byte[] keyZ = generateKeyZ(privateKey, peerPublicKey);
    return calculateDerivedKey(keyZ, algoName, partyUInfo, partyVInfo, algoKeyBitLen);
}
Also used : ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) BigInteger(java.math.BigInteger)

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