Search in sources :

Example 11 with KeyAgreement

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

the class KeyAgreementTest method test_generateSecret$BI.

public void test_generateSecret$BI() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    KeyAgreement ka = KeyAgreement.getInstance("DH");
    byte[] bb1 = new byte[1];
    try {
        ka.generateSecret(bb1, 0);
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    //expected
    }
    ka.init(privKey);
    ka.doPhase(publKey, true);
    try {
        ka.generateSecret(bb1, 0);
        fail("ShortBufferException expected");
    } catch (ShortBufferException e) {
    //expected
    }
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) KeyAgreement(javax.crypto.KeyAgreement)

Example 12 with KeyAgreement

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

the class ECDHKeyAgreementTest method testGenerateSecret_withSmallerThanNeededBuffer.

void testGenerateSecret_withSmallerThanNeededBuffer(Provider provider) throws Exception {
    KeyAgreement keyAgreement = getKeyAgreement(provider);
    keyAgreement.init(KAT_PRIVATE_KEY1);
    keyAgreement.doPhase(KAT_PUBLIC_KEY2, true);
    try {
        // Although the buffer is big enough (1024 bytes) the shared secret should be placed
        // at offset 1020 thus leaving only 4 bytes for the secret, which is not enough.
        keyAgreement.generateSecret(new byte[1024], 1020);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) KeyAgreement(javax.crypto.KeyAgreement)

Example 13 with KeyAgreement

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

the class ECDHKeyAgreementTest method generateSecret.

private static byte[] generateSecret(Provider keyAgreementProvider, PrivateKey privateKey, PublicKey publicKey) throws GeneralSecurityException {
    KeyAgreement keyAgreement = getKeyAgreement(keyAgreementProvider);
    keyAgreement.init(privateKey);
    keyAgreement.doPhase(publicKey, true);
    return keyAgreement.generateSecret();
}
Also used : KeyAgreement(javax.crypto.KeyAgreement)

Example 14 with KeyAgreement

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

the class ECDHKeyAgreementTest method testGenerateSecret_withAlgorithm.

void testGenerateSecret_withAlgorithm(Provider provider) throws Exception {
    KeyAgreement keyAgreement = getKeyAgreement(provider);
    keyAgreement.init(KAT_PRIVATE_KEY2);
    keyAgreement.doPhase(KAT_PUBLIC_KEY1, true);
    SecretKey key = keyAgreement.generateSecret("AES");
    assertEquals("AES", key.getAlgorithm());
    // The check below will need to change if it's a hardware-backed key.
    // We'll have to encrypt a known plaintext and check that the ciphertext is as
    // expected.
    assertTrue(Arrays.equals(KAT_SECRET, key.getEncoded()));
}
Also used : SecretKey(javax.crypto.SecretKey) KeyAgreement(javax.crypto.KeyAgreement)

Example 15 with KeyAgreement

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

the class ECDHKeyAgreementTest method testDoPhase_withInvalidKeyType.

void testDoPhase_withInvalidKeyType(Provider provider) throws Exception {
    KeyAgreement keyAgreement = getKeyAgreement(provider);
    keyAgreement.init(KAT_PRIVATE_KEY1);
    try {
        keyAgreement.doPhase(KAT_PRIVATE_KEY1, true);
        fail();
    } catch (InvalidKeyException expected) {
    }
}
Also used : KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException)

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