Search in sources :

Example 11 with AesGcmKeyFormat

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());
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 12 with AesGcmKeyFormat

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)));
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 13 with AesGcmKeyFormat

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);
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) TreeSet(java.util.TreeSet) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 14 with AesGcmKeyFormat

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]);
    }
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) AesGcmKey(com.google.crypto.tink.proto.AesGcmKey) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 15 with AesGcmKeyFormat

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());
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) 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