Search in sources :

Example 6 with KeyAgreement

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

the class KeyAgreementTest method test_getProvider.

public void test_getProvider() throws NoSuchAlgorithmException {
    KeyAgreement keyA;
    for (int i = 0; i < validValues.length; i++) {
        keyA = KeyAgreement.getInstance(validValues[i]);
        assertNotNull(keyA.getProvider());
    }
}
Also used : KeyAgreement(javax.crypto.KeyAgreement)

Example 7 with KeyAgreement

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

the class KeyAgreementTest method testInit04.

/**
     * Test for the methods:
     * <code>init(Key key, AlgorithmParameterSpec params)</code>
     * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
     * <code>generateSecret()</code>
     * Assertions: initializes KeyAgreement and returns byte array
     */
public void testInit04() throws Exception, InvalidAlgorithmParameterException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
    AlgorithmParameterSpec aps = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
    byte[] bbRes1;
    byte[] bbRes2;
    byte[] bbRes3;
    SecureRandom randomNull = null;
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < kAgs.length; i++) {
        kAgs[i].init(privKey, dhPs);
        kAgs[i].doPhase(publKey, true);
        bbRes1 = kAgs[i].generateSecret();
        kAgs[i].init(privKey, dhPs, random);
        kAgs[i].doPhase(publKey, true);
        bbRes2 = kAgs[i].generateSecret();
        assertEquals("Incorrect byte array length", bbRes1.length, bbRes2.length);
        for (int j = 0; j < bbRes1.length; j++) {
            assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
        }
        kAgs[i].init(privKey, dhPs, randomNull);
        kAgs[i].doPhase(publKey, true);
        bbRes3 = kAgs[i].generateSecret();
        assertEquals("Incorrect byte array length", bbRes1.length, bbRes3.length);
        for (int j = 0; j < bbRes1.length; j++) {
            assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
        }
        try {
            kAgs[i].init(publKey, dhPs, random);
            fail("InvalidKeyException expected");
        } catch (InvalidKeyException e) {
        //expected
        }
        try {
            kAgs[i].init(privKey, aps, random);
            fail("InvalidAlgorithmParameterException expected");
        } catch (InvalidAlgorithmParameterException e) {
        //expected
        }
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 8 with KeyAgreement

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

the class KeyAgreementTest method test_initLjava_security_KeyLjava_security_SecureRandom.

public void test_initLjava_security_KeyLjava_security_SecureRandom() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    KeyAgreement ka = KeyAgreement.getInstance("DH");
    ka.init(privKey, new SecureRandom());
    try {
        ka.init(publKey, new SecureRandom());
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
}
Also used : SecureRandom(java.security.SecureRandom) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException)

Example 9 with KeyAgreement

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

the class KeyAgreementTest method test_generateSecretLjava_lang_String.

public void test_generateSecretLjava_lang_String() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    KeyAgreement ka = KeyAgreement.getInstance("DH");
    byte[] bb1 = new byte[1];
    try {
        ka.generateSecret("dh");
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    //expected
    }
    ka.init(privKey);
    ka.doPhase(publKey, true);
    try {
        ka.generateSecret("Wrong alg name");
        fail("NoSuchAlgorithmException expected");
    } catch (NoSuchAlgorithmException e) {
    //expected
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyAgreement(javax.crypto.KeyAgreement)

Example 10 with KeyAgreement

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

the class KeyAgreementTest method testGetInstanceString02.

/**
     * Test for <code> getInstance(String algorithm) </code> method Assertions:
     * returns KeyAgreement object
     */
public void testGetInstanceString02() throws NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    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)

Aggregations

KeyAgreement (javax.crypto.KeyAgreement)55 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