use of com.google.crypto.tink.Aead in project tink by google.
the class AwsKmsAeadTest method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws Exception {
DecryptResult mockDecryptResult = mock(DecryptResult.class);
EncryptResult mockEncryptResult = mock(EncryptResult.class);
when(mockKms.decrypt(isA(DecryptRequest.class))).thenReturn(mockDecryptResult);
when(mockKms.encrypt(isA(EncryptRequest.class))).thenReturn(mockEncryptResult);
Aead aead = new AwsKmsAead(mockKms, KEY_ARN);
byte[] aad = Random.randBytes(20);
for (int messageSize = 0; messageSize < 75; messageSize++) {
byte[] message = Random.randBytes(messageSize);
when(mockDecryptResult.getKeyId()).thenReturn(KEY_ARN);
when(mockDecryptResult.getPlaintext()).thenReturn(ByteBuffer.wrap(message));
when(mockEncryptResult.getCiphertextBlob()).thenReturn(ByteBuffer.wrap(message));
byte[] ciphertext = aead.encrypt(message, aad);
byte[] decrypted = aead.decrypt(ciphertext, aad);
assertArrayEquals(message, decrypted);
}
}
use of com.google.crypto.tink.Aead in project tink by google.
the class AwsKmsAeadTest method testDecryptShouldThrowExceptionIfKeyArnIsDifferent.
@Test
public void testDecryptShouldThrowExceptionIfKeyArnIsDifferent() throws Exception {
DecryptResult mockDecryptResult = mock(DecryptResult.class);
EncryptResult mockEncryptResult = mock(EncryptResult.class);
when(mockKms.decrypt(isA(DecryptRequest.class))).thenReturn(mockDecryptResult);
when(mockKms.encrypt(isA(EncryptRequest.class))).thenReturn(mockEncryptResult);
Aead aead = new AwsKmsAead(mockKms, KEY_ARN);
byte[] aad = Random.randBytes(20);
byte[] message = Random.randBytes(20);
when(mockEncryptResult.getCiphertextBlob()).thenReturn(ByteBuffer.wrap(message));
when(mockDecryptResult.getKeyId()).thenReturn(KEY_ARN + "1");
byte[] ciphertext = aead.encrypt(message, aad);
assertThrows(GeneralSecurityException.class, () -> aead.decrypt(ciphertext, aad));
}
use of com.google.crypto.tink.Aead in project tink by google.
the class EncryptThenAuthenticateTest method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws Exception {
Aead aead = getAead(Random.randBytes(16), Random.randBytes(16), 16, 16, "HMACSHA256");
byte[] plaintext = Random.randBytes(1001);
byte[] aad = Random.randBytes(13);
byte[] ciphertext = aead.encrypt(plaintext, aad);
try {
byte[] plaintext1 = aead.decrypt(ciphertext, aad);
assertArrayEquals(plaintext, plaintext1);
} catch (GeneralSecurityException e) {
fail("Valid ciphertext and aad, should have passed: " + e);
}
}
use of com.google.crypto.tink.Aead in project tink by google.
the class XChaCha20Poly1305Test method testLongMessages.
@Test
public void testLongMessages() throws Exception {
if (TestUtil.isAndroid() || TestUtil.isTsan()) {
System.out.println("testLongMessages doesn't work on Android and under tsan, skipping");
return;
}
int dataSize = 16;
while (dataSize <= (1 << 24)) {
byte[] plaintext = Random.randBytes(dataSize);
byte[] aad = Random.randBytes(dataSize / 3);
byte[] key = Random.randBytes(KEY_SIZE);
Aead aead = createInstance(key);
byte[] ciphertext = aead.encrypt(plaintext, aad);
byte[] decrypted = aead.decrypt(ciphertext, aad);
assertArrayEquals(plaintext, decrypted);
dataSize += 5 * dataSize / 11;
}
}
use of com.google.crypto.tink.Aead in project tink by google.
the class ChaCha20Poly1305Test method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws Exception {
Assume.assumeFalse(TinkFips.useOnlyFips());
Aead aead = createInstance(Random.randBytes(KEY_SIZE));
for (int i = 0; i < 100; i++) {
byte[] message = Random.randBytes(i);
byte[] aad = Random.randBytes(i);
byte[] ciphertext = aead.encrypt(message, aad);
byte[] decrypted = aead.decrypt(ciphertext, aad);
assertArrayEquals(message, decrypted);
}
}
Aggregations