Search in sources :

Example 11 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testAes128GcmHkdf4KBTemplate.

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

Example 12 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method createKey_checkValues.

@Test
public void createKey_checkValues() throws Exception {
    AesGcmHkdfStreamingKeyFormat format = createKeyFormat(32, 32, HashType.SHA256, 1024);
    AesGcmHkdfStreamingKey key = factory.createKey(format);
    assertThat(key.getParams()).isEqualTo(format.getParams());
    assertThat(key.getVersion()).isEqualTo(0);
    assertThat(key.getKeyValue()).hasSize(format.getKeySize());
}
Also used : AesGcmHkdfStreamingKey(com.google.crypto.tink.proto.AesGcmHkdfStreamingKey) AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) Test(org.junit.Test)

Example 13 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testAes256GcmHkdf4KBTemplate.

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

Example 14 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testDeriveKey_justEnoughKeyMaterial_works.

@Test
public void testDeriveKey_justEnoughKeyMaterial_works() throws Exception {
    final int keySize = 32;
    final int derivedKeySize = 16;
    AesGcmHkdfStreamingKeyFormat format = createKeyFormat(keySize, derivedKeySize, HashType.SHA256, 1024);
    byte[] keyMaterial = Random.randBytes(32);
    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 15 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testDeriveKey_notEnoughKeyMaterial_throws.

@Test
public void testDeriveKey_notEnoughKeyMaterial_throws() throws Exception {
    final int keySize = 32;
    final int derivedKeySize = 16;
    AesGcmHkdfStreamingKeyFormat format = createKeyFormat(keySize, derivedKeySize, HashType.SHA256, 1024);
    byte[] keyMaterial = Random.randBytes(31);
    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)

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