Search in sources :

Example 16 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testDeriveKey_badVersion_throws.

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

Example 17 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    AesGcmHkdfStreamingKeyFormat keyFormat = createKeyFormat(32, 32, HashType.SHA256, 1024);
    Set<String> keys = new TreeSet<>();
    // Calls newKey multiple times and make sure that they generate different keys.
    int numTests = 100;
    for (int i = 0; i < numTests; i++) {
        keys.add(TestUtil.hexEncode(factory.createKey(keyFormat).getKeyValue().toByteArray()));
    }
    assertThat(keys).hasSize(numTests);
}
Also used : AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Example 18 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testDeriveKey_size32.

@Test
public void testDeriveKey_size32() throws Exception {
    final int keySize = 32;
    final int derivedKeySize = 16;
    AesGcmHkdfStreamingKeyFormat format = createKeyFormat(keySize, derivedKeySize, HashType.SHA256, 1024);
    byte[] keyMaterial = Random.randBytes(100);
    AesGcmHkdfStreamingKey key = factory.deriveKey(format, new ByteArrayInputStream(keyMaterial));
    assertThat(key.getKeyValue()).hasSize(32);
    for (int i = 0; i < keySize; ++i) {
        assertThat(key.getKeyValue().byteAt(i)).isEqualTo(keyMaterial[i]);
    }
    assertThat(key.getParams()).isEqualTo(format.getParams());
}
Also used : AesGcmHkdfStreamingKey(com.google.crypto.tink.proto.AesGcmHkdfStreamingKey) AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 19 with AesGcmHkdfStreamingKeyFormat

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

the class StreamingAeadKeyTemplatesTest method testCreateAesGcmHkdfStreamingKeyTemplate.

@Test
public void testCreateAesGcmHkdfStreamingKeyTemplate() throws Exception {
    // Intentionally using "weird" or invalid values for parameters,
    // to test that the function correctly puts them in the resulting template.
    int mainKeySize = 42;
    int derivedKeySize = 24;
    int ciphertextSegmentSize = 12345;
    HashType hkdfHashType = HashType.SHA512;
    KeyTemplate template = StreamingAeadKeyTemplates.createAesGcmHkdfStreamingKeyTemplate(mainKeySize, hkdfHashType, derivedKeySize, ciphertextSegmentSize);
    assertEquals(new AesGcmHkdfStreamingKeyManager().getKeyType(), template.getTypeUrl());
    assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType());
    AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertEquals(mainKeySize, format.getKeySize());
    assertEquals(derivedKeySize, format.getParams().getDerivedKeySize());
    assertEquals(hkdfHashType, format.getParams().getHkdfHashType());
    assertEquals(ciphertextSegmentSize, format.getParams().getCiphertextSegmentSize());
}
Also used : AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) HashType(com.google.crypto.tink.proto.HashType) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 20 with AesGcmHkdfStreamingKeyFormat

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

the class StreamingAeadKeyTemplatesTest method testAes256GcmHkdf_4KB.

@Test
public void testAes256GcmHkdf_4KB() throws Exception {
    KeyTemplate template = StreamingAeadKeyTemplates.AES256_GCM_HKDF_4KB;
    assertEquals(new AesGcmHkdfStreamingKeyManager().getKeyType(), template.getTypeUrl());
    assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType());
    AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertEquals(32, format.getKeySize());
    assertEquals(32, format.getParams().getDerivedKeySize());
    assertEquals(HashType.SHA256, format.getParams().getHkdfHashType());
    assertEquals(4096, format.getParams().getCiphertextSegmentSize());
}
Also used : AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Aggregations

AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)22 Test (org.junit.Test)19 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)7 AesGcmHkdfStreamingKey (com.google.crypto.tink.proto.AesGcmHkdfStreamingKey)5 KeyTemplate (com.google.crypto.tink.KeyTemplate)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteString (com.google.protobuf.ByteString)3 GeneralSecurityException (java.security.GeneralSecurityException)3 AesGcmHkdfStreamingParams (com.google.crypto.tink.proto.AesGcmHkdfStreamingParams)2 StreamingAead (com.google.crypto.tink.StreamingAead)1 HashType (com.google.crypto.tink.proto.HashType)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1