Search in sources :

Example 1 with AesGcmSivKeyFormat

use of com.google.crypto.tink.proto.AesGcmSivKeyFormat 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 2 with AesGcmSivKeyFormat

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

the class AesGcmSivKeyManagerTest method testDeriveKey_notEnoughKeyMaterial_throws.

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

Example 3 with AesGcmSivKeyFormat

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

the class AesGcmSivKeyManagerTest method testDeriveKey_badVersion_throws.

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

Example 4 with AesGcmSivKeyFormat

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

the class AesGcmSivKeyManagerTest method createKey_multipleTimes.

@Test
public void createKey_multipleTimes() throws Exception {
    AesGcmSivKeyFormat format = AesGcmSivKeyFormat.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 : AesGcmSivKeyFormat(com.google.crypto.tink.proto.AesGcmSivKeyFormat) TreeSet(java.util.TreeSet) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 5 with AesGcmSivKeyFormat

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

the class AesGcmSivKeyManagerTest method testRawAes128GcmSivTemplate.

@Test
public void testRawAes128GcmSivTemplate() throws Exception {
    KeyTemplate template = AesGcmSivKeyManager.rawAes128GcmSivTemplate();
    assertEquals(new AesGcmSivKeyManager().getKeyType(), template.getTypeUrl());
    assertEquals(KeyTemplate.OutputPrefixType.RAW, 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)

Aggregations

AesGcmSivKeyFormat (com.google.crypto.tink.proto.AesGcmSivKeyFormat)8 Test (org.junit.Test)7 KeyTemplate (com.google.crypto.tink.KeyTemplate)4 ByteString (com.google.protobuf.ByteString)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1