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();
}
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)));
}
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);
}
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]);
}
}
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)));
}
Aggregations