Search in sources :

Example 6 with ECPrivateKeySpec

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

the class JsonEcdhTest method testEcdhComp.

/**
 * Example for test vector
 * {
 * "algorithm" : "ECDH",
 * "header" : [],
 * "notes" : {
 *   "AddSubChain" : "The private key has a special value....",
 * }
 * "generatorVersion" : "0.7",
 * "numberOfTests" : 308,
 * "testGroups" : [
 *   {
 *     "type" : "EcdhTest",
 *     "tests" : [
 *        {
 *         "comment" : "normal case",
 *         "curve" : "secp224r1",
 *         "private" : "565577a49415ca761a0322ad54e4ad0ae7625174baf372c2816f5328",
 *         "public" : "30...",
 *         "result" : "valid",
 *         "shared" : "b8ecdb552d39228ee332bafe4886dbff272f7109edf933bc7542bd4f",
 *         "tcId" : 1
 *        },
 *     ...
 */
public void testEcdhComp(String filename) throws Exception {
    JsonObject test = JsonUtil.getTestVectors(filename);
    // This test expects test vectors as defined in wycheproof/schemas/ecdh_test_schema.json.
    // In particular, this means that the public keys use X509 encoding.
    // Test vectors with different encodings of the keys have a different schema.
    final String expectedSchema = "ecdh_test_schema.json";
    String schema = test.get("schema").getAsString();
    assertEquals("Unexpected schema in file:" + filename, expectedSchema, schema);
    int numTests = test.get("numberOfTests").getAsInt();
    int passedTests = 0;
    // invalid test vectors leading to exceptions
    int rejectedTests = 0;
    // valid test vectors leading to exceptions
    int skippedTests = 0;
    int errors = 0;
    for (JsonElement g : test.getAsJsonArray("testGroups")) {
        JsonObject group = g.getAsJsonObject();
        String curve = getString(group, "curve");
        for (JsonElement t : group.getAsJsonArray("tests")) {
            JsonObject testcase = t.getAsJsonObject();
            int tcid = testcase.get("tcId").getAsInt();
            String comment = getString(testcase, "comment");
            BigInteger priv = getBigInteger(testcase, "private");
            byte[] publicEncoded = getBytes(testcase, "public");
            String result = getString(testcase, "result");
            String expectedHex = getString(testcase, "shared");
            KeyFactory kf = KeyFactory.getInstance("EC");
            try {
                ECPrivateKeySpec spec = new ECPrivateKeySpec(priv, EcUtil.getCurveSpecRef(curve));
                PrivateKey privKey = kf.generatePrivate(spec);
                X509EncodedKeySpec x509keySpec = new X509EncodedKeySpec(publicEncoded);
                PublicKey pubKey = kf.generatePublic(x509keySpec);
                KeyAgreement ka = KeyAgreement.getInstance("ECDH");
                ka.init(privKey);
                ka.doPhase(pubKey, true);
                String sharedHex = TestUtil.bytesToHex(ka.generateSecret());
                if (result.equals("invalid")) {
                    System.out.println("Computed ECDH with invalid parameters" + " tcId:" + tcid + " comment:" + comment + " shared:" + sharedHex);
                    errors++;
                } else if (!expectedHex.equals(sharedHex)) {
                    System.out.println("Incorrect ECDH computation" + " tcId:" + tcid + " comment:" + comment + "\nshared:" + sharedHex + "\nexpected:" + expectedHex);
                    errors++;
                } else {
                    passedTests++;
                }
            } catch (InvalidKeySpecException | InvalidKeyException | NoSuchAlgorithmException ex) {
                // or when a key is not valid.
                if (result.equals("valid")) {
                    skippedTests++;
                } else {
                    rejectedTests++;
                }
            } catch (Exception ex) {
                // Other exceptions typically indicate that something is wrong with the implementation.
                System.out.println("Test vector with tcId:" + tcid + " comment:" + comment + " throws:" + ex.toString());
                errors++;
            }
        }
    }
    assertEquals(0, errors);
    assertEquals(numTests, passedTests + rejectedTests + skippedTests);
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) JsonObject(com.google.gson.JsonObject) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) JsonElement(com.google.gson.JsonElement) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyAgreement(javax.crypto.KeyAgreement) KeyFactory(java.security.KeyFactory)

Example 7 with ECPrivateKeySpec

use of java.security.spec.ECPrivateKeySpec in project i2p.i2p-bote by i2p.

the class ECDH_ECDSA method createPrivateKeyPair.

@Override
public PrivateKeyPair createPrivateKeyPair(byte[] bytes) throws GeneralSecurityException {
    PrivateKeyPair keyPair = new PrivateKeyPair();
    ECPrivateKeySpec encryptionKeySpec = createPrivateKeySpec(Arrays.copyOf(bytes, keyLengthBytes));
    keyPair.encryptionKey = ecdhKeyFactory.generatePrivate(encryptionKeySpec);
    ECPrivateKeySpec signingKeySpec = createPrivateKeySpec(Arrays.copyOfRange(bytes, keyLengthBytes, 2 * keyLengthBytes));
    keyPair.signingKey = ecdsaKeyFactory.generatePrivate(signingKeySpec);
    return keyPair;
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec)

Example 8 with ECPrivateKeySpec

use of java.security.spec.ECPrivateKeySpec in project j2objc by google.

the class ECPrivateKeySpecTest 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));
    s = BigInteger.valueOf(1);
    ecparams = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
    ecpks = new ECPrivateKeySpec(s, ecparams);
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ECFieldF2m(java.security.spec.ECFieldF2m) ECPoint(java.security.spec.ECPoint)

Example 9 with ECPrivateKeySpec

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

the class NativeCryptoTest method initChannelIdKey.

private static synchronized void initChannelIdKey() throws Exception {
    if (CHANNEL_ID_PRIVATE_KEY != null) {
        return;
    }
    // NIST P-256 aka SECG secp256r1 aka X9.62 prime256v1
    OpenSSLECGroupContext openSslSpec = OpenSSLECGroupContext.getCurveByName("prime256v1");
    BigInteger s = new BigInteger("229cdbbf489aea584828a261a23f9ff8b0f66f7ccac98bf2096ab3aee41497c5", 16);
    CHANNEL_ID_PRIVATE_KEY = new OpenSSLECPrivateKey(new ECPrivateKeySpec(s, openSslSpec.getECParameterSpec())).getOpenSSLKey();
    // Channel ID is the concatenation of the X and Y coordinates of the public key.
    CHANNEL_ID = new BigInteger("702b07871fd7955c320b26f15e244e47eed60272124c92b9ebecf0b42f90069b" + "ab53592ebfeb4f167dbf3ce61513afb0e354c479b1c1b69874fa471293494f77", 16).toByteArray();
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) BigInteger(java.math.BigInteger)

Example 10 with ECPrivateKeySpec

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

the class CryptoUtils method getECPrivateKey.

public static ECPrivateKey getECPrivateKey(String curve, byte[] privateKey) {
    try {
        ECParameterSpec params = getECParameterSpec(curve, true);
        ECPrivateKeySpec keySpec = new ECPrivateKeySpec(toBigInteger(privateKey), params);
        KeyFactory kf = KeyFactory.getInstance("EC");
        return (ECPrivateKey) kf.generatePrivate(keySpec);
    } catch (Exception ex) {
        throw new SecurityException(ex);
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) ECParameterSpec(java.security.spec.ECParameterSpec) KeyFactory(java.security.KeyFactory) DestroyFailedException(javax.security.auth.DestroyFailedException)

Aggregations

ECPrivateKeySpec (java.security.spec.ECPrivateKeySpec)15 BigInteger (java.math.BigInteger)10 ECParameterSpec (java.security.spec.ECParameterSpec)7 ECPoint (java.security.spec.ECPoint)7 KeyFactory (java.security.KeyFactory)6 ECPrivateKey (java.security.interfaces.ECPrivateKey)5 PrivateKey (java.security.PrivateKey)4 PublicKey (java.security.PublicKey)4 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 GeneralSecurityException (java.security.GeneralSecurityException)3 ECPublicKey (java.security.interfaces.ECPublicKey)3 KeyAgreement (javax.crypto.KeyAgreement)3 AlgorithmParameters (java.security.AlgorithmParameters)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ECFieldF2m (java.security.spec.ECFieldF2m)2 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)2 EllipticCurve (java.security.spec.EllipticCurve)2 KeySpec (java.security.spec.KeySpec)2