Search in sources :

Example 76 with Aead

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

the class ChaCha20Poly1305KeyManagerTest method testCiphertextSize.

@Test
public void testCiphertextSize() throws Exception {
    Aead aead = new ChaCha20Poly1305KeyManager().getPrimitive(createChaCha20Poly1305Key(32), Aead.class);
    byte[] plaintext = "plaintext".getBytes(UTF_8);
    byte[] associatedData = "associatedData".getBytes(UTF_8);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertEquals(12 + /* IV_SIZE */
    plaintext.length + 16, /* TAG_SIZE */
    ciphertext.length);
}
Also used : Aead(com.google.crypto.tink.Aead) Test(org.junit.Test)

Example 77 with Aead

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

the class AesGcmSivKeyManagerTest method getPrimitive.

@Test
public void getPrimitive() throws Exception {
    AesGcmSivKey key = factory.createKey(AesGcmSivKeyFormat.newBuilder().setKeySize(16).build());
    Aead managerAead = manager.getPrimitive(key, Aead.class);
    Aead directAead = new AesGcmSiv(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) AesGcmSivKey(com.google.crypto.tink.proto.AesGcmSivKey) AesGcmSiv(com.google.crypto.tink.aead.subtle.AesGcmSiv) Test(org.junit.Test)

Example 78 with Aead

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

the class KmsEnvelopeAeadKeyManagerTest method createKey_multipleKeysWithSameKek.

@Test
public void createKey_multipleKeysWithSameKek() throws Exception {
    String kekUri = FakeKmsClient.createFakeKeyUri();
    KeyTemplate dekTemplate = AesCtrHmacAeadKeyManager.aes128CtrHmacSha256Template();
    KmsEnvelopeAeadKey key1 = factory.createKey(KmsEnvelopeAeadKeyManager.createKeyFormat(kekUri, dekTemplate));
    Aead aead1 = manager.getPrimitive(key1, Aead.class);
    KmsEnvelopeAeadKey key2 = factory.createKey(KmsEnvelopeAeadKeyManager.createKeyFormat(kekUri, dekTemplate));
    Aead aead2 = manager.getPrimitive(key2, Aead.class);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    assertThat(aead1.decrypt(aead2.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 79 with Aead

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

the class KmsEnvelopeAeadKeyManagerTest method getPrimitive.

@Test
public void getPrimitive() 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);
    TestUtil.runBasicAeadTests(aead);
}
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 80 with Aead

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

the class KmsEnvelopeAeadTest method encryptDecryptMissingAd_fails.

@Test
public void encryptDecryptMissingAd_fails() throws GeneralSecurityException {
    Aead remoteAead = this.generateNewRemoteAead();
    KmsEnvelopeAead envAead = new KmsEnvelopeAead(KeyTemplateProtoConverter.toProto(KeyTemplates.get("AES128_EAX")), remoteAead);
    byte[] plaintext = "helloworld".getBytes(UTF_8);
    byte[] associatedData = "envelope_ad".getBytes(UTF_8);
    byte[] ciphertext = envAead.encrypt(plaintext, associatedData);
    assertThrows(GeneralSecurityException.class, () -> envAead.decrypt(ciphertext, EMPTY_ADD));
}
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