Search in sources :

Example 1 with HmacPrfParams

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

the class PrfKeyTemplates method createHmacTemplate.

private static KeyTemplate createHmacTemplate(int keySize, HashType hashType) {
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(hashType).build();
    HmacPrfKeyFormat format = HmacPrfKeyFormat.newBuilder().setParams(params).setKeySize(keySize).build();
    return KeyTemplate.newBuilder().setTypeUrl(new HmacPrfKeyManager().getKeyType()).setValue(format.toByteString()).setOutputPrefixType(OutputPrefixType.RAW).build();
}
Also used : HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams)

Example 2 with HmacPrfParams

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

the class HmacPrfKeyManagerTest method testDeriveKey_notEnoughKeyMaterial_throws.

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

Example 3 with HmacPrfParams

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

the class HmacPrfKeyManagerTest method testDeriveKey_size27.

@Test
public void testDeriveKey_size27() throws Exception {
    final int keySize = 27;
    byte[] keyMaterial = Random.randBytes(100);
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(HashType.SHA256).build();
    HmacPrfKey key = factory.deriveKey(HmacPrfKeyFormat.newBuilder().setVersion(0).setParams(params).setKeySize(keySize).build(), new ByteArrayInputStream(keyMaterial));
    assertThat(key.getKeyValue()).hasSize(keySize);
    for (int i = 0; i < keySize; ++i) {
        assertThat(key.getKeyValue().byteAt(i)).isEqualTo(keyMaterial[i]);
    }
    assertThat(key.getParams()).isEqualTo(params);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams) HmacPrfKey(com.google.crypto.tink.proto.HmacPrfKey) Test(org.junit.Test)

Example 4 with HmacPrfParams

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

the class HmacPrfKeyManagerTest method testDeriveKey_justEnoughKeyMaterial.

@Test
public void testDeriveKey_justEnoughKeyMaterial() throws Exception {
    final int keySize = 32;
    byte[] keyMaterial = Random.randBytes(keySize);
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(HashType.SHA256).build();
    HmacPrfKey key = factory.deriveKey(HmacPrfKeyFormat.newBuilder().setVersion(0).setParams(params).setKeySize(keySize).build(), new ByteArrayInputStream(keyMaterial));
    assertThat(key.getKeyValue()).hasSize(keySize);
    for (int i = 0; i < keySize; ++i) {
        assertThat(key.getKeyValue().byteAt(i)).isEqualTo(keyMaterial[i]);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams) HmacPrfKey(com.google.crypto.tink.proto.HmacPrfKey) Test(org.junit.Test)

Example 5 with HmacPrfParams

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

the class HmacPrfKeyManagerTest method testDeriveKey_badVersion_throws.

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

Aggregations

HmacPrfParams (com.google.crypto.tink.proto.HmacPrfParams)6 HmacPrfKeyFormat (com.google.crypto.tink.proto.HmacPrfKeyFormat)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Test (org.junit.Test)4 HmacPrfKey (com.google.crypto.tink.proto.HmacPrfKey)2