Search in sources :

Example 41 with KeyAgreement

use of javax.crypto.KeyAgreement in project error-prone by google.

the class InsecureCipherModeNegativeCases method ellipticCurveDiffieHellman.

public void ellipticCurveDiffieHellman() {
    KeyFactory keyFactory;
    KeyAgreement keyAgreement;
    KeyPairGenerator keyPairGenerator;
    final String ecdh = "ECDH";
    try {
        keyFactory = KeyFactory.getInstance(ecdh);
        keyAgreement = KeyAgreement.getInstance("ECDH");
        keyPairGenerator = KeyPairGenerator.getInstance("EC" + "DH");
    } catch (NoSuchAlgorithmException e) {
    // We don't handle any exception as this code is not meant to be executed.
    }
}
Also used : KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyAgreement(javax.crypto.KeyAgreement) KeyFactory(java.security.KeyFactory)

Example 42 with KeyAgreement

use of javax.crypto.KeyAgreement in project robovm by robovm.

the class KeyAgreementTest method testInit02.

/**
     * Test for the methods
     * <code>init(Key key, AlgorithmParameterSpec params)</code>
     * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
     * Assertion: throws AlgorithmParameterException when params are
     * inappropriate
     */
public void testInit02() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    SecureRandom random = null;
    DSAParameterSpec dsa = new DSAParameterSpec(new BigInteger("56"), new BigInteger("56"), new BigInteger("56"));
    for (int i = 0; i < kAgs.length; i++) {
        try {
            kAgs[i].init(privKey, dsa);
            fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
        } catch (InvalidAlgorithmParameterException e) {
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(privKey, dsa, new SecureRandom());
            fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
        } catch (InvalidAlgorithmParameterException e) {
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(privKey, dsa, random);
            fail("InvalidAlgorithmParameterException or InvalidKeyException must be throw");
        } catch (InvalidAlgorithmParameterException e) {
        } catch (InvalidKeyException e) {
        }
    }
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) BigInteger(java.math.BigInteger) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException)

Example 43 with KeyAgreement

use of javax.crypto.KeyAgreement in project robovm by robovm.

the class KeyAgreementTest method testGetInstanceStringProvider03.

/**
     * Test for <code> getInstance(String algorithm, Provider provider)</code>
     * method Assertions: returns KeyAgreement object
     */
public void testGetInstanceStringProvider03() throws IllegalArgumentException, NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyAgreement keyA;
    for (int i = 0; i < validValues.length; i++) {
        keyA = KeyAgreement.getInstance(validValues[i], defaultProvider);
        assertEquals("Incorrect algorithm", keyA.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", keyA.getProvider(), defaultProvider);
    }
}
Also used : KeyAgreement(javax.crypto.KeyAgreement)

Example 44 with KeyAgreement

use of javax.crypto.KeyAgreement in project robovm by robovm.

the class KeyAgreementTest method test_getAlgorithm.

public void test_getAlgorithm() throws NoSuchAlgorithmException {
    Mock_KeyAgreement mka = new Mock_KeyAgreement(null, null, null);
    assertNull(mka.getAlgorithm());
    KeyAgreement keyA;
    for (int i = 0; i < validValues.length; i++) {
        keyA = KeyAgreement.getInstance(validValues[i]);
        assertEquals("Incorrect algorithm", keyA.getAlgorithm(), validValues[i]);
    }
}
Also used : KeyAgreement(javax.crypto.KeyAgreement)

Example 45 with KeyAgreement

use of javax.crypto.KeyAgreement in project robovm by robovm.

the class KeyAgreementTest method testInit01.

/**
     * Test for the methods <code>init(Key key)</code>
     * <code>init(Key key, SecureRandom random)</code>
     * <code>init(Key key, AlgorithmParameterSpec params)</code>
     * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
     * Assertion: throws InvalidKeyException when key is inappropriate
     */
public void testInit01() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    SecureRandom random = null;
    AlgorithmParameterSpec aps = null;
    DHParameterSpec dhPs = new DHParameterSpec(new BigInteger("56"), new BigInteger("56"));
    for (int i = 0; i < kAgs.length; i++) {
        try {
            kAgs[i].init(publKey);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, new SecureRandom());
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, random);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, dhPs);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, aps);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].init(publKey, dhPs, new SecureRandom());
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
    }
}
Also used : SecureRandom(java.security.SecureRandom) BigInteger(java.math.BigInteger) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

KeyAgreement (javax.crypto.KeyAgreement)56 KeyPairGenerator (java.security.KeyPairGenerator)15 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 InvalidKeyException (java.security.InvalidKeyException)12 KeyFactory (java.security.KeyFactory)12 SecretKey (javax.crypto.SecretKey)10 DHParameterSpec (javax.crypto.spec.DHParameterSpec)10 KeyPair (java.security.KeyPair)9 Cipher (javax.crypto.Cipher)9 IOException (java.io.IOException)7 PublicKey (java.security.PublicKey)7 BigInteger (java.math.BigInteger)6 SecureRandom (java.security.SecureRandom)6 DHPublicKey (javax.crypto.interfaces.DHPublicKey)6 SecretKeySpec (javax.crypto.spec.SecretKeySpec)6 GeneralSecurityException (java.security.GeneralSecurityException)5 DHPublicKeySpec (javax.crypto.spec.DHPublicKeySpec)5 IvParameterSpec (javax.crypto.spec.IvParameterSpec)5 PrivateKey (java.security.PrivateKey)4 CertificateException (java.security.cert.CertificateException)4