Search in sources :

Example 11 with ECPrivateKey

use of java.security.interfaces.ECPrivateKey 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 12 with ECPrivateKey

use of java.security.interfaces.ECPrivateKey in project wycheproof by google.

the class EciesTest method testKeyGeneration.

/**
   * BouncyCastle has a key generation algorithm "ECIES". This test checks that the result are
   * ECKeys in both cases.
   */
public void testKeyGeneration() throws Exception {
    ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
    KeyPairGenerator kf = KeyPairGenerator.getInstance("ECIES");
    kf.initialize(ecSpec);
    KeyPair keyPair = kf.generateKeyPair();
    ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
    ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) ECPublicKey(java.security.interfaces.ECPublicKey) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator)

Example 13 with ECPrivateKey

use of java.security.interfaces.ECPrivateKey in project chromeview by pwnall.

the class AndroidKeyStore method rawSignDigestWithPrivateKey.

/**
     * Sign a given message with a given PrivateKey object. This method
     * shall only be used to implement signing in the context of SSL
     * client certificate support.
     *
     * The message will actually be a hash, computed and padded by OpenSSL,
     * itself, depending on the type of the key. The result should match
     * exactly what the vanilla implementations of the following OpenSSL
     * function calls do:
     *
     *  - For a RSA private key, this should be equivalent to calling
     *    RSA_sign(NDI_md5_sha1,....), i.e. it must generate a raw RSA
     *    signature. The message must a combined, 36-byte MD5+SHA1 message
     *    digest padded to the length of the modulus using PKCS#1 padding.
     *
     *  - For a DSA and ECDSA private keys, this should be equivalent to
     *    calling DSA_sign(0,...) and ECDSA_sign(0,...) respectively. The
     *    message must be a 20-byte SHA1 hash and the function shall
     *    compute a direct DSA/ECDSA signature for it.
     *
     * @param privateKey The PrivateKey handle.
     * @param message The message to sign.
     * @return signature as a byte buffer.
     *
     * Important: Due to a platform bug, this function will always fail on
     *            Android < 4.2 for RSA PrivateKey objects. See the
     *            getOpenSSLHandleForPrivateKey() below for work-around.
     */
@CalledByNative
public static byte[] rawSignDigestWithPrivateKey(PrivateKey privateKey, byte[] message) {
    // Get the Signature for this key.
    Signature signature = null;
    // http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html
    try {
        if (privateKey instanceof RSAPrivateKey) {
            // IMPORTANT: Due to a platform bug, this will throw NoSuchAlgorithmException
            // on Android 4.0.x and 4.1.x. Fixed in 4.2 and higher.
            // See https://android-review.googlesource.com/#/c/40352/
            signature = Signature.getInstance("NONEwithRSA");
        } else if (privateKey instanceof DSAPrivateKey) {
            signature = Signature.getInstance("NONEwithDSA");
        } else if (privateKey instanceof ECPrivateKey) {
            signature = Signature.getInstance("NONEwithECDSA");
        }
    } catch (NoSuchAlgorithmException e) {
        ;
    }
    if (signature == null) {
        Log.e(TAG, "Unsupported private key algorithm: " + privateKey.getAlgorithm());
        return null;
    }
    // Sign the message.
    try {
        signature.initSign(privateKey);
        signature.update(message);
        return signature.sign();
    } catch (Exception e) {
        Log.e(TAG, "Exception while signing message with " + privateKey.getAlgorithm() + " private key: " + e);
        return null;
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) Signature(java.security.Signature) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CalledByNative(org.chromium.base.CalledByNative)

Example 14 with ECPrivateKey

use of java.security.interfaces.ECPrivateKey in project robovm by robovm.

the class OpenSSLECPrivateKey method equals.

@Override
public boolean equals(Object o) {
    if (o == this) {
        return true;
    }
    if (o instanceof OpenSSLECPrivateKey) {
        OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o;
        return key.equals(other.key);
    }
    if (!(o instanceof ECPrivateKey)) {
        return false;
    }
    final ECPrivateKey other = (ECPrivateKey) o;
    if (!getPrivateKey().equals(other.getS())) {
        return false;
    }
    final ECParameterSpec spec = getParams();
    final ECParameterSpec otherSpec = other.getParams();
    return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor();
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECParameterSpec(java.security.spec.ECParameterSpec)

Example 15 with ECPrivateKey

use of java.security.interfaces.ECPrivateKey in project robovm by robovm.

the class KeyPairGeneratorTest method test_KeyWithAllKeyFactories.

private void test_KeyWithAllKeyFactories(Key k) throws Exception {
    byte[] encoded = k.getEncoded();
    String keyAlgo = k.getAlgorithm();
    Provider[] providers = Security.getProviders();
    for (Provider p : providers) {
        Set<Provider.Service> services = p.getServices();
        for (Provider.Service service : services) {
            if (!"KeyFactory".equals(service.getType())) {
                continue;
            }
            if (!service.getAlgorithm().equals(keyAlgo)) {
                continue;
            }
            if ("PKCS#8".equals(k.getFormat())) {
                PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(encoded);
                KeyFactory kf = KeyFactory.getInstance(k.getAlgorithm(), p);
                PrivateKey privKey = kf.generatePrivate(spec);
                assertNotNull(k.getAlgorithm() + ", provider=" + p.getName(), privKey);
                /*
                     * EC keys are unique because they can have explicit parameters or a curve
                     * name. Check them specially so this test can continue to function.
                     */
                if (k instanceof ECPrivateKey) {
                    assertECPrivateKeyEquals((ECPrivateKey) k, (ECPrivateKey) privKey);
                } else {
                    assertEquals(k.getAlgorithm() + ", provider=" + p.getName(), Arrays.toString(encoded), Arrays.toString(privKey.getEncoded()));
                }
            } else if ("X.509".equals(k.getFormat())) {
                X509EncodedKeySpec spec = new X509EncodedKeySpec(encoded);
                KeyFactory kf = KeyFactory.getInstance(k.getAlgorithm(), p);
                PublicKey pubKey = kf.generatePublic(spec);
                assertNotNull(pubKey);
                assertTrue(Arrays.equals(encoded, pubKey.getEncoded()));
            }
        }
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) Service(java.security.Provider.Service) Service(java.security.Provider.Service) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) Provider(java.security.Provider) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Aggregations

ECPrivateKey (java.security.interfaces.ECPrivateKey)16 KeyPairGenerator (java.security.KeyPairGenerator)9 ECPublicKey (java.security.interfaces.ECPublicKey)8 KeyFactory (java.security.KeyFactory)6 KeyPair (java.security.KeyPair)6 BigInteger (java.math.BigInteger)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Signature (java.security.Signature)4 ECParameterSpec (java.security.spec.ECParameterSpec)4 ECPoint (java.security.spec.ECPoint)4 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)4 GeneralSecurityException (java.security.GeneralSecurityException)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)3 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)3 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)3 KeyAgreement (javax.crypto.KeyAgreement)3 InvalidKeyException (java.security.InvalidKeyException)2 PrivateKey (java.security.PrivateKey)2 Provider (java.security.Provider)2