Search in sources :

Example 66 with Aead

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);
}
Also used : DecryptResult(com.amazonaws.services.kms.model.DecryptResult) Aead(com.google.crypto.tink.Aead) EncryptResult(com.amazonaws.services.kms.model.EncryptResult) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest) EncryptRequest(com.amazonaws.services.kms.model.EncryptRequest) Test(org.junit.Test)

Example 67 with Aead

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));
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) Aead(com.google.crypto.tink.Aead) EncryptResult(com.amazonaws.services.kms.model.EncryptResult) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest) EncryptRequest(com.amazonaws.services.kms.model.EncryptRequest) Test(org.junit.Test)

Example 68 with Aead

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);
}
Also used : Aead(com.google.crypto.tink.Aead) XChaCha20Poly1305Key(com.google.crypto.tink.proto.XChaCha20Poly1305Key) XChaCha20Poly1305(com.google.crypto.tink.subtle.XChaCha20Poly1305) Test(org.junit.Test)

Example 69 with Aead

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);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) Key(com.google.crypto.tink.proto.Keyset.Key) Test(org.junit.Test)

Example 70 with Aead

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);
}
Also used : AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) Aead(com.google.crypto.tink.Aead) AesEaxJce(com.google.crypto.tink.subtle.AesEaxJce) Test(org.junit.Test)

Aggregations

Aead (com.google.crypto.tink.Aead)84 Test (org.junit.Test)67 GeneralSecurityException (java.security.GeneralSecurityException)25 KeysetHandle (com.google.crypto.tink.KeysetHandle)21 Key (com.google.crypto.tink.proto.Keyset.Key)9 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)7 IOException (java.io.IOException)7 EncryptRequest (com.amazonaws.services.kms.model.EncryptRequest)6 KeyTemplate (com.google.crypto.tink.KeyTemplate)6 ByteString (com.google.protobuf.ByteString)6 DecryptRequest (com.amazonaws.services.kms.model.DecryptRequest)5 EncryptResult (com.amazonaws.services.kms.model.EncryptResult)5 KmsEnvelopeAeadKey (com.google.crypto.tink.proto.KmsEnvelopeAeadKey)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 DecryptResult (com.amazonaws.services.kms.model.DecryptResult)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBuffer (java.nio.ByteBuffer)4 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)3 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)3