Search in sources :

Example 31 with Aead

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);
    }
}
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 32 with Aead

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));
}
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 33 with Aead

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);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Aead(com.google.crypto.tink.Aead) Test(org.junit.Test)

Example 34 with Aead

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

Example 35 with Aead

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);
    }
}
Also used : Aead(com.google.crypto.tink.Aead) 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