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
}
}
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) {
}
}
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();
}
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()));
}
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) {
}
}
Aggregations