use of com.google.crypto.tink.Aead in project tink by google.
the class AwsKmsAeadTest method testDecryptShouldNotThrowExceptionIfKeyArnIsAlias.
@Test
public void testDecryptShouldNotThrowExceptionIfKeyArnIsAlias() 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);
String aliasArn = "arn:aws:kms:us-west-2:111122223333:alias/ExampleAlias";
Aead aead = new AwsKmsAead(mockKms, aliasArn);
byte[] aad = Random.randBytes(20);
byte[] message = Random.randBytes(20);
when(mockEncryptResult.getCiphertextBlob()).thenReturn(ByteBuffer.wrap(message));
when(mockDecryptResult.getPlaintext()).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 testDecryptShouldThrowExceptionIfRequestFailed.
@Test
public void testDecryptShouldThrowExceptionIfRequestFailed() throws Exception {
EncryptResult mockEncryptResult = mock(EncryptResult.class);
when(mockKms.encrypt(isA(EncryptRequest.class))).thenReturn(mockEncryptResult);
AmazonServiceException exception = mock(AmazonServiceException.class);
when(mockKms.decrypt(isA(DecryptRequest.class))).thenThrow(exception);
Aead aead = new AwsKmsAead(mockKms, KEY_ARN);
byte[] aad = Random.randBytes(20);
byte[] message = Random.randBytes(20);
when(mockEncryptResult.getCiphertextBlob()).thenReturn(ByteBuffer.wrap(message));
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 XChaCha20Poly1305KeyManagerTest method getPrimitive.
@Test
public void getPrimitive() throws Exception {
XChaCha20Poly1305Key key = factory.createKey(XChaCha20Poly1305KeyFormat.getDefaultInstance());
Aead managerAead = manager.getPrimitive(key, Aead.class);
Aead directAead = new XChaCha20Poly1305(key.getKeyValue().toByteArray());
byte[] plaintext = Random.randBytes(20);
byte[] associatedData = Random.randBytes(20);
assertThat(directAead.decrypt(managerAead.encrypt(plaintext, associatedData), associatedData)).isEqualTo(plaintext);
}
use of com.google.crypto.tink.Aead in project tink by google.
the class AeadFactoryTest method testRawKeyAsPrimary.
@Test
public void testRawKeyAsPrimary() throws Exception {
byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE);
byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE);
int ivSize = 12;
int tagSize = 16;
Key primary = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key raw = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key legacy = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, raw, legacy));
Aead aead = keysetHandle.getPrimitive(Aead.class);
byte[] plaintext = Random.randBytes(20);
byte[] associatedData = Random.randBytes(20);
byte[] ciphertext = aead.encrypt(plaintext, associatedData);
assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData));
assertEquals(CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + ivSize + tagSize, ciphertext.length);
}
use of com.google.crypto.tink.Aead in project tink by google.
the class AesEaxKeyManagerTest method getPrimitive.
@Test
public void getPrimitive() throws Exception {
AesEaxKey key = factory.createKey(createKeyFormat(32, 16));
Aead managerAead = manager.getPrimitive(key, Aead.class);
Aead directAead = new AesEaxJce(key.getKeyValue().toByteArray(), key.getParams().getIvSize());
byte[] plaintext = Random.randBytes(20);
byte[] associatedData = Random.randBytes(20);
assertThat(directAead.decrypt(managerAead.encrypt(plaintext, associatedData), associatedData)).isEqualTo(plaintext);
}
Aggregations