use of com.google.crypto.tink.proto.HmacPrfKeyFormat in project tink by google.
the class HmacPrfKeyManager method keyFactory.
@Override
public KeyFactory<HmacPrfKeyFormat, HmacPrfKey> keyFactory() {
return new KeyFactory<HmacPrfKeyFormat, HmacPrfKey>(HmacPrfKeyFormat.class) {
@Override
public void validateKeyFormat(HmacPrfKeyFormat format) throws GeneralSecurityException {
if (format.getKeySize() < MIN_KEY_SIZE_IN_BYTES) {
throw new GeneralSecurityException("key too short");
}
validateParams(format.getParams());
}
@Override
public HmacPrfKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException {
return HmacPrfKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
}
@Override
public HmacPrfKey createKey(HmacPrfKeyFormat format) {
return HmacPrfKey.newBuilder().setVersion(getVersion()).setParams(format.getParams()).setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).build();
}
@Override
public HmacPrfKey deriveKey(HmacPrfKeyFormat format, InputStream inputStream) throws GeneralSecurityException {
Validators.validateVersion(format.getVersion(), getVersion());
byte[] pseudorandomness = new byte[format.getKeySize()];
try {
int read = inputStream.read(pseudorandomness);
if (read != format.getKeySize()) {
throw new GeneralSecurityException("Not enough pseudorandomness given");
}
return HmacPrfKey.newBuilder().setVersion(getVersion()).setParams(format.getParams()).setKeyValue(ByteString.copyFrom(pseudorandomness)).build();
} catch (IOException e) {
throw new GeneralSecurityException("Reading pseudorandomness failed", e);
}
}
@Override
public Map<String, KeyFactory.KeyFormat<HmacPrfKeyFormat>> keyFormats() throws GeneralSecurityException {
Map<String, KeyFactory.KeyFormat<HmacPrfKeyFormat>> result = new HashMap<>();
result.put("HMAC_SHA256_PRF", new KeyFactory.KeyFormat<>(HmacPrfKeyFormat.newBuilder().setParams(HmacPrfParams.newBuilder().setHash(HashType.SHA256).build()).setKeySize(32).build(), KeyTemplate.OutputPrefixType.RAW));
result.put("HMAC_SHA512_PRF", new KeyFactory.KeyFormat<>(HmacPrfKeyFormat.newBuilder().setParams(HmacPrfParams.newBuilder().setHash(HashType.SHA512).build()).setKeySize(64).build(), KeyTemplate.OutputPrefixType.RAW));
return Collections.unmodifiableMap(result);
}
};
}
use of com.google.crypto.tink.proto.HmacPrfKeyFormat 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.HmacPrfKeyFormat in project tink by google.
the class HmacPrfKeyManagerTest method createKey_multipleTimes.
@Test
public void createKey_multipleTimes() throws Exception {
HmacPrfKeyFormat keyFormat = makeHmacPrfKeyFormat(16, HashType.SHA256);
int numKeys = 100;
Set<String> keys = new TreeSet<String>();
for (int i = 0; i < numKeys; ++i) {
keys.add(TestUtil.hexEncode(factory.createKey(keyFormat).getKeyValue().toByteArray()));
}
assertThat(keys).hasSize(numKeys);
}
use of com.google.crypto.tink.proto.HmacPrfKeyFormat 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.HmacPrfKeyFormat in project tink by google.
the class HmacPrfKeyManagerTest method testHmacSha512Template.
@Test
public void testHmacSha512Template() throws Exception {
KeyTemplate template = HmacPrfKeyManager.hmacSha512Template();
assertThat(template.getTypeUrl()).isEqualTo(new HmacPrfKeyManager().getKeyType());
assertThat(template.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.RAW);
HmacPrfKeyFormat format = HmacPrfKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertThat(format.getKeySize()).isEqualTo(64);
assertThat(format.getParams().getHash()).isEqualTo(HashType.SHA512);
}
Aggregations