Search in sources :

Example 6 with KeyTemplate

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

the class AesGcmSivKeyManagerTest method testAes128GcmSivTemplate.

@Test
public void testAes128GcmSivTemplate() throws Exception {
    KeyTemplate template = AesGcmSivKeyManager.aes128GcmSivTemplate();
    assertEquals(new AesGcmSivKeyManager().getKeyType(), template.getTypeUrl());
    assertEquals(KeyTemplate.OutputPrefixType.TINK, template.getOutputPrefixType());
    AesGcmSivKeyFormat format = AesGcmSivKeyFormat.parseFrom(ByteString.copyFrom(template.getValue()), ExtensionRegistryLite.getEmptyRegistry());
    assertEquals(16, format.getKeySize());
}
Also used : AesGcmSivKeyFormat(com.google.crypto.tink.proto.AesGcmSivKeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 7 with KeyTemplate

use of com.google.crypto.tink.KeyTemplate 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 KeyTemplate

use of com.google.crypto.tink.KeyTemplate 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 KeyTemplate

use of com.google.crypto.tink.KeyTemplate 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 KeyTemplate

use of com.google.crypto.tink.KeyTemplate 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

KeyTemplate (com.google.crypto.tink.KeyTemplate)143 Test (org.junit.Test)135 KeysetHandle (com.google.crypto.tink.KeysetHandle)56 ByteString (com.google.protobuf.ByteString)39 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)37 KeyData (com.google.crypto.tink.proto.KeyData)16 Keyset (com.google.crypto.tink.proto.Keyset)12 JsonObject (com.google.gson.JsonObject)8 KeysetManager (com.google.crypto.tink.KeysetManager)7 BigInteger (java.math.BigInteger)7 Instant (java.time.Instant)7 Aead (com.google.crypto.tink.Aead)6 Enums (com.google.crypto.tink.subtle.Enums)6 Clock (java.time.Clock)6 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)5 AesCtrHmacStreamingKeyFormat (com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat)4 AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)4 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)4 AesGcmSivKeyFormat (com.google.crypto.tink.proto.AesGcmSivKeyFormat)4 EciesAeadHkdfKeyFormat (com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat)4