Search in sources :

Example 21 with AesGcmKeyFormat

use of com.google.crypto.tink.proto.AesGcmKeyFormat in project tink by google.

the class AesGcmKeyManagerTest method testDeriveKey_badVersion_throws.

@Test
public void testDeriveKey_badVersion_throws() throws Exception {
    final int keySize = 32;
    byte[] keyMaterial = Random.randBytes(100);
    AesGcmKeyFormat format = AesGcmKeyFormat.newBuilder().setVersion(1).setKeySize(keySize).build();
    assertThrows(GeneralSecurityException.class, () -> factory.deriveKey(format, new ByteArrayInputStream(keyMaterial)));
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 22 with AesGcmKeyFormat

use of com.google.crypto.tink.proto.AesGcmKeyFormat in project tink by google.

the class KeysetManagerTest method testAdd_existingKeySet_shouldAddNewKey.

@Test
public void testAdd_existingKeySet_shouldAddNewKey() throws Exception {
    KeyTemplate kt1 = AesGcmKeyManager.aes128GcmTemplate();
    KeysetHandle existing = KeysetManager.withEmptyKeyset().add(kt1).getKeysetHandle();
    KeyTemplate kt2 = AesGcmKeyManager.aes256GcmTemplate();
    Keyset keyset = KeysetManager.withKeysetHandle(existing).add(kt2).getKeysetHandle().getKeyset();
    assertThat(keyset.getKeyCount()).isEqualTo(2);
    // None of the keys are primary.
    assertThat(keyset.getPrimaryKeyId()).isEqualTo(0);
    Keyset.Key key1 = keyset.getKey(0);
    assertThat(key1.getStatus()).isEqualTo(KeyStatusType.ENABLED);
    assertThat(key1.getOutputPrefixType()).isEqualTo(OutputPrefixType.TINK);
    assertThat(key1.hasKeyData()).isTrue();
    assertThat(key1.getKeyData().getTypeUrl()).isEqualTo(kt1.getTypeUrl());
    AesGcmKeyFormat aesGcmKeyFormat1 = AesGcmKeyFormat.parseFrom(kt1.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    AesGcmKey aesGcmKey1 = AesGcmKey.parseFrom(key1.getKeyData().getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertThat(aesGcmKey1.getKeyValue().size()).isEqualTo(aesGcmKeyFormat1.getKeySize());
    Keyset.Key key2 = keyset.getKey(1);
    assertThat(key2.getStatus()).isEqualTo(KeyStatusType.ENABLED);
    assertThat(key2.getOutputPrefixType()).isEqualTo(OutputPrefixType.TINK);
    assertThat(key2.hasKeyData()).isTrue();
    assertThat(key2.getKeyData().getTypeUrl()).isEqualTo(kt2.getTypeUrl());
    AesGcmKeyFormat aesGcmKeyFormat2 = AesGcmKeyFormat.parseFrom(kt2.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    AesGcmKey aesGcmKey2 = AesGcmKey.parseFrom(key2.getKeyData().getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertThat(aesGcmKey2.getKeyValue().size()).isEqualTo(aesGcmKeyFormat2.getKeySize());
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) Key(com.google.crypto.tink.proto.Keyset.Key) AesGcmKey(com.google.crypto.tink.proto.AesGcmKey) Test(org.junit.Test)

Example 23 with AesGcmKeyFormat

use of com.google.crypto.tink.proto.AesGcmKeyFormat in project tink by google.

the class RegistryTest method testDeriveKey_wrongKeySize_validateThrows.

// Tests that validate is called.
@Test
public void testDeriveKey_wrongKeySize_validateThrows() throws Exception {
    Registry.reset();
    Registry.registerKeyManager(new TestKeyTypeManager(), true);
    AesGcmKeyFormat format = AesGcmKeyFormat.newBuilder().setKeySize(32).build();
    com.google.crypto.tink.proto.KeyTemplate template = com.google.crypto.tink.proto.KeyTemplate.newBuilder().setValue(format.toByteString()).setTypeUrl(new TestKeyTypeManager().getKeyType()).setOutputPrefixType(OutputPrefixType.TINK).build();
    ByteArrayInputStream emptyInput = new ByteArrayInputStream(new byte[0]);
    GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> Registry.deriveKey(template, emptyInput));
    assertExceptionContains(e, "validateKeyFormat");
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) GeneralSecurityException(java.security.GeneralSecurityException) Test(org.junit.Test)

Aggregations

AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)23 Test (org.junit.Test)21 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)8 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)6 Keyset (com.google.crypto.tink.proto.Keyset)6 Key (com.google.crypto.tink.proto.Keyset.Key)6 KeyTemplate (com.google.crypto.tink.KeyTemplate)4 KeyHandle (com.google.crypto.tink.tinkkey.KeyHandle)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteString (com.google.protobuf.ByteString)3 GeneralSecurityException (java.security.GeneralSecurityException)3 KeyData (com.google.crypto.tink.proto.KeyData)2 KeyAccess (com.google.crypto.tink.tinkkey.KeyAccess)2 SecretKeyAccess (com.google.crypto.tink.tinkkey.SecretKeyAccess)2 TreeSet (java.util.TreeSet)2 ProtoKey (com.google.crypto.tink.tinkkey.internal.ProtoKey)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1