Search in sources :

Example 51 with ECPoint

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

the class EllipticCurveKeyHelper method createPublicKey.

static ECPublicKey createPublicKey(final BigInteger x, final BigInteger y, final String curve) {
    ECNamedCurveParameterSpec curveParameterSpec;
    if ((curveParameterSpec = ECNamedCurveTable.getParameterSpec(curve)) == null) {
        throw new IllegalArgumentException("Unsupported named curve: " + curve);
    }
    ECParameterSpec parameterSpec = new ECNamedCurveSpec(curveParameterSpec.getName(), curveParameterSpec.getCurve(), curveParameterSpec.getG(), curveParameterSpec.getN());
    ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(x, y), parameterSpec);
    try {
        return (ECPublicKey) KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) ECNamedCurveParameterSpec(org.bouncycastle.jce.spec.ECNamedCurveParameterSpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECNamedCurveSpec(org.bouncycastle.jce.spec.ECNamedCurveSpec)

Example 52 with ECPoint

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

the class EcdsaTest method testBasic.

/**
   * This test checks the basic functionality of ECDSA. It can also be used to generate simple test
   * vectors.
   */
public void testBasic() throws Exception {
    String algorithm = "SHA256WithECDSA";
    String hashAlgorithm = "SHA-256";
    String message = "Hello";
    String curve = "secp256r1";
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
    ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
    keyGen.initialize(ecSpec);
    KeyPair keyPair = keyGen.generateKeyPair();
    ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
    ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
    byte[] messageBytes = message.getBytes("UTF-8");
    Signature signer = Signature.getInstance(algorithm);
    Signature verifier = Signature.getInstance(algorithm);
    signer.initSign(priv);
    signer.update(messageBytes);
    byte[] signature = signer.sign();
    verifier.initVerify(pub);
    verifier.update(messageBytes);
    assertTrue(verifier.verify(signature));
    // Extract some parameters.
    byte[] rawHash = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
    ECParameterSpec params = priv.getParams();
    // Print keys and signature, so that it can be used to generate new test vectors.
    System.out.println("Message:" + message);
    System.out.println("Hash:" + TestUtil.bytesToHex(rawHash));
    System.out.println("Curve:" + curve);
    System.out.println("Order:" + params.getOrder().toString());
    System.out.println("Private key:");
    System.out.println("S:" + priv.getS().toString());
    System.out.println("encoded:" + TestUtil.bytesToHex(priv.getEncoded()));
    System.out.println("Public key:");
    ECPoint w = pub.getW();
    System.out.println("X:" + w.getAffineX().toString());
    System.out.println("Y:" + w.getAffineY().toString());
    System.out.println("encoded:" + TestUtil.bytesToHex(pub.getEncoded()));
    System.out.println("Signature:" + TestUtil.bytesToHex(signature));
    System.out.println("r:" + extractR(signature).toString());
    System.out.println("s:" + extractS(signature).toString());
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) Signature(java.security.Signature) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) ECPoint(java.security.spec.ECPoint)

Example 53 with ECPoint

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

the class ECPublicKeySpecTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
    EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1), BigInteger.valueOf(1));
    w = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
    params = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
    ecpks = new ECPublicKeySpec(w, params);
}
Also used : EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ECFieldF2m(java.security.spec.ECFieldF2m) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec)

Example 54 with ECPoint

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

the class ECParameterSpecTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1), BigInteger.valueOf(1));
    ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
    ecps = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
}
Also used : EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ECFieldF2m(java.security.spec.ECFieldF2m) ECPoint(java.security.spec.ECPoint)

Example 55 with ECPoint

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

the class ECPointTest method testEqualsObject02.

/**
     * Test #2 for <code>equals(Object other)</code> method<br>
     * Assertion: return false if this and other objects are not equal<br>
     * Test preconditions: see test comments<br>
     * Expected: all objects in this test must be not equal
     */
public final void testEqualsObject02() {
    // test case 1: must be not equal to null
    ECPoint p2 = null, p1 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE);
    assertFalse(p1.equals(p2));
    // test case 2: not equal objects - x
    p1 = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE);
    p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(1L));
    assertFalse(p1.equals(p2) || p2.equals(p1));
    // test case 3: not equal objects - y
    p1 = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE);
    p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ZERO);
    assertFalse(p1.equals(p2) || p2.equals(p1));
    // test case 4: not equal - some point and POINT_INFINITY
    p1 = ECPoint.POINT_INFINITY;
    p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ZERO);
    assertFalse(p1.equals(p2) || p2.equals(p1));
}
Also used : ECPoint(java.security.spec.ECPoint)

Aggregations

ECPoint (java.security.spec.ECPoint)111 ECParameterSpec (java.security.spec.ECParameterSpec)56 BigInteger (java.math.BigInteger)54 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)36 ECPublicKey (java.security.interfaces.ECPublicKey)31 EllipticCurve (java.security.spec.EllipticCurve)31 KeyPair (java.security.KeyPair)20 ECPrivateKey (java.security.interfaces.ECPrivateKey)20 PublicKey (java.security.PublicKey)17 ECFieldFp (java.security.spec.ECFieldFp)17 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)16 Test (org.junit.Test)16 KeyFactory (java.security.KeyFactory)15 KeyPairGenerator (java.security.KeyPairGenerator)14 AlgorithmParameters (java.security.AlgorithmParameters)13 GeneralSecurityException (java.security.GeneralSecurityException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 KeySpec (java.security.spec.KeySpec)11 IOException (java.io.IOException)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9