Search in sources :

Example 6 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class AesGcmSivKeyManagerTest method testCiphertextSize.

@Test
public void testCiphertextSize() throws Exception {
    AesGcmSivKey key = factory.createKey(AesGcmSivKeyFormat.newBuilder().setKeySize(32).build());
    Aead aead = new AesGcmSivKeyManager().getPrimitive(key, Aead.class);
    byte[] plaintext = "plaintext".getBytes(UTF_8);
    byte[] associatedData = "associatedData".getBytes(UTF_8);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertThat(ciphertext.length).isEqualTo(12 + /* IV_SIZE */
    plaintext.length + 16);
}
Also used : Aead(com.google.crypto.tink.Aead) AesGcmSivKey(com.google.crypto.tink.proto.AesGcmSivKey) Test(org.junit.Test)

Example 7 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class KmsAeadKeyManagerTest method createKeyTemplate_multipleKeysWithSameKek.

@Test
public void createKeyTemplate_multipleKeysWithSameKek() throws Exception {
    String keyUri = FakeKmsClient.createFakeKeyUri();
    KeyTemplate template1 = KmsAeadKeyManager.createKeyTemplate(keyUri);
    KeysetHandle handle1 = KeysetHandle.generateNew(template1);
    Aead aead1 = handle1.getPrimitive(Aead.class);
    KeyTemplate template2 = KmsAeadKeyManager.createKeyTemplate(keyUri);
    KeysetHandle handle2 = KeysetHandle.generateNew(template2);
    Aead aead2 = handle2.getPrimitive(Aead.class);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    assertThat(aead1.decrypt(aead2.encrypt(plaintext, associatedData), associatedData)).isEqualTo(plaintext);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 8 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class KmsEnvelopeAeadKeyManagerTest method createKey.

@Test
public void createKey() throws Exception {
    String kekUri = FakeKmsClient.createFakeKeyUri();
    KeyTemplate dekTemplate = AesCtrHmacAeadKeyManager.aes128CtrHmacSha256Template();
    KmsEnvelopeAeadKey key = factory.createKey(KmsEnvelopeAeadKeyManager.createKeyFormat(kekUri, dekTemplate));
    Aead aead = manager.getPrimitive(key, Aead.class);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    assertThat(aead.decrypt(aead.encrypt(plaintext, associatedData), associatedData)).isEqualTo(plaintext);
}
Also used : KmsEnvelopeAeadKey(com.google.crypto.tink.proto.KmsEnvelopeAeadKey) Aead(com.google.crypto.tink.Aead) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 9 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class KmsEnvelopeAeadKeyManagerTest method createKeyTemplate_multipleKeysWithSameKek.

@Test
public void createKeyTemplate_multipleKeysWithSameKek() throws Exception {
    String kekUri = FakeKmsClient.createFakeKeyUri();
    KeyTemplate dekTemplate = AesCtrHmacAeadKeyManager.aes128CtrHmacSha256Template();
    KeyTemplate kt1 = KmsEnvelopeAeadKeyManager.createKeyTemplate(kekUri, dekTemplate);
    KeysetHandle handle1 = KeysetHandle.generateNew(kt1);
    Aead aead1 = handle1.getPrimitive(Aead.class);
    KeyTemplate kt2 = KmsEnvelopeAeadKeyManager.createKeyTemplate(kekUri, dekTemplate);
    KeysetHandle handle2 = KeysetHandle.generateNew(kt2);
    Aead aead2 = handle2.getPrimitive(Aead.class);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    assertThat(aead1.decrypt(aead2.encrypt(plaintext, associatedData), associatedData)).isEqualTo(plaintext);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 10 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class KmsEnvelopeAeadKeyManagerTest method getPrimitive_parsingInvalidCiphetexts.

@Test
public void getPrimitive_parsingInvalidCiphetexts() throws Exception {
    String kekUri = FakeKmsClient.createFakeKeyUri();
    KeyTemplate dekTemplate = AesCtrHmacAeadKeyManager.aes128CtrHmacSha256Template();
    KmsEnvelopeAeadKey key = factory.createKey(KmsEnvelopeAeadKeyManager.createKeyFormat(kekUri, dekTemplate));
    Aead aead = manager.getPrimitive(key, Aead.class);
    byte[] plaintext = Random.randBytes(20);
    byte[] aad = Random.randBytes(20);
    byte[] ciphertext = aead.encrypt(plaintext, aad);
    ByteBuffer buffer = ByteBuffer.wrap(ciphertext);
    int encryptedDekSize = buffer.getInt();
    byte[] encryptedDek = new byte[encryptedDekSize];
    buffer.get(encryptedDek, 0, encryptedDekSize);
    byte[] payload = new byte[buffer.remaining()];
    buffer.get(payload, 0, buffer.remaining());
    // valid, should work
    byte[] ciphertext2 = ByteBuffer.allocate(ciphertext.length).putInt(encryptedDekSize).put(encryptedDek).put(payload).array();
    assertArrayEquals(plaintext, aead.decrypt(ciphertext2, aad));
    // negative length
    byte[] ciphertext3 = ByteBuffer.allocate(ciphertext.length).putInt(-1).put(encryptedDek).put(payload).array();
    assertThrows(GeneralSecurityException.class, () -> aead.decrypt(ciphertext3, aad));
    // length larger than actual value
    byte[] ciphertext4 = ByteBuffer.allocate(ciphertext.length).putInt(encryptedDek.length + 1).put(encryptedDek).put(payload).array();
    assertThrows(GeneralSecurityException.class, () -> aead.decrypt(ciphertext4, aad));
    // length larger than total ciphertext length
    byte[] ciphertext5 = ByteBuffer.allocate(ciphertext.length).putInt(encryptedDek.length + payload.length + 1).put(encryptedDek).put(payload).array();
    assertThrows(GeneralSecurityException.class, () -> aead.decrypt(ciphertext5, aad));
}
Also used : KmsEnvelopeAeadKey(com.google.crypto.tink.proto.KmsEnvelopeAeadKey) Aead(com.google.crypto.tink.Aead) ByteString(com.google.protobuf.ByteString) ByteBuffer(java.nio.ByteBuffer) KeyTemplate(com.google.crypto.tink.KeyTemplate) 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