use of com.google.crypto.tink.proto.AesGcmKeyFormat in project tink by google.
the class AesGcmKeyManagerTest method testAes128GcmTemplate.
@Test
public void testAes128GcmTemplate() throws Exception {
KeyTemplate template = AesGcmKeyManager.aes128GcmTemplate();
assertEquals(new AesGcmKeyManager().getKeyType(), template.getTypeUrl());
assertEquals(KeyTemplate.OutputPrefixType.TINK, template.getOutputPrefixType());
AesGcmKeyFormat format = AesGcmKeyFormat.parseFrom(ByteString.copyFrom(template.getValue()), ExtensionRegistryLite.getEmptyRegistry());
assertEquals(16, format.getKeySize());
}
use of com.google.crypto.tink.proto.AesGcmKeyFormat in project tink by google.
the class AesGcmKeyManagerTest method testDeriveKey_notEnoughKeyMaterial_throws.
@Test
public void testDeriveKey_notEnoughKeyMaterial_throws() throws Exception {
byte[] keyMaterial = Random.randBytes(31);
AesGcmKeyFormat format = AesGcmKeyFormat.newBuilder().setVersion(0).setKeySize(32).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 AesGcmKeyManagerTest method createKey_multipleTimes.
@Test
public void createKey_multipleTimes() throws Exception {
AesGcmKeyFormat format = AesGcmKeyFormat.newBuilder().setKeySize(16).build();
Set<String> keys = new TreeSet<>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 50;
for (int i = 0; i < numTests; i++) {
keys.add(TestUtil.hexEncode(factory.createKey(format).getKeyValue().toByteArray()));
}
assertThat(keys).hasSize(numTests);
}
use of com.google.crypto.tink.proto.AesGcmKeyFormat in project tink by google.
the class RegistryTest method testDeriveKey_succeeds.
@Test
public void testDeriveKey_succeeds() throws Exception {
Registry.reset();
Registry.registerKeyManager(new TestKeyTypeManager(), true);
AesGcmKeyFormat format = AesGcmKeyFormat.newBuilder().setKeySize(16).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();
byte[] keyMaterial = Random.randBytes(100);
KeyData keyData = Registry.deriveKey(template, new ByteArrayInputStream(keyMaterial));
assertThat(keyData.getKeyMaterialType()).isEqualTo(new TestKeyTypeManager().keyMaterialType());
assertThat(keyData.getTypeUrl()).isEqualTo(new TestKeyTypeManager().getKeyType());
AesGcmKey key = AesGcmKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
for (int i = 0; i < 16; ++i) {
assertThat(key.getKeyValue().byteAt(i)).isEqualTo(keyMaterial[i]);
}
}
use of com.google.crypto.tink.proto.AesGcmKeyFormat in project tink by google.
the class AeadKeyTemplatesTest method testAES128_GCM.
@Test
public void testAES128_GCM() throws Exception {
KeyTemplate template = AeadKeyTemplates.AES128_GCM;
assertEquals(AesGcmKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesGcmKeyFormat format = AesGcmKeyFormat.parseFrom(template.getValue());
assertEquals(16, format.getKeySize());
}
Aggregations