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)));
}
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());
}
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");
}
Aggregations