use of com.google.crypto.tink.proto.HmacKeyFormat in project tink by google.
the class HmacKeyManager method newKey.
/**
* @param keyFormat {@code HmacKeyFormat} proto
* @return new {@code HmacKey} proto
*/
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
if (!(keyFormat instanceof HmacKeyFormat)) {
throw new GeneralSecurityException("expected HmacKeyFormat proto");
}
HmacKeyFormat format = (HmacKeyFormat) keyFormat;
validate(format);
return HmacKey.newBuilder().setVersion(VERSION).setParams(format.getParams()).setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).build();
}
use of com.google.crypto.tink.proto.HmacKeyFormat in project tink by google.
the class TestUtil method assertHmacKey.
/**
* Asserts that {@code key} is generated from {@code keyTemplate}.
*/
public static void assertHmacKey(KeyTemplate keyTemplate, Keyset.Key key) throws Exception {
assertThat(key.getKeyId()).isGreaterThan(0);
assertThat(key.getStatus()).isEqualTo(KeyStatusType.ENABLED);
assertThat(key.getOutputPrefixType()).isEqualTo(OutputPrefixType.TINK);
assertThat(key.hasKeyData()).isTrue();
assertThat(key.getKeyData().getTypeUrl()).isEqualTo(keyTemplate.getTypeUrl());
HmacKeyFormat hmacKeyFormat = HmacKeyFormat.parseFrom(keyTemplate.getValue());
HmacKey hmacKey = HmacKey.parseFrom(key.getKeyData().getValue());
assertThat(hmacKey.getParams()).isEqualTo(hmacKeyFormat.getParams());
assertThat(hmacKey.getKeyValue().size()).isEqualTo(hmacKeyFormat.getKeySize());
}
use of com.google.crypto.tink.proto.HmacKeyFormat in project tink by google.
the class HmacKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
HmacKeyManager keyManager = new HmacKeyManager();
HmacKeyFormat hmacKeyFormat = HmacKeyFormat.newBuilder().setParams(HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(16).build()).setKeySize(32).build();
ByteString serialized = ByteString.copyFrom(hmacKeyFormat.toByteArray());
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(HmacKeyManager.TYPE_URL).setValue(serialized).build();
// Calls newKey multiple times and make sure that we get different HmacKey each time.
Set<String> keys = new TreeSet<String>();
int numTests = 27;
for (int i = 0; i < numTests / 3; i++) {
HmacKey key = (HmacKey) keyManager.newKey(hmacKeyFormat);
assertEquals(32, key.getKeyValue().toByteArray().length);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
key = (HmacKey) keyManager.newKey(serialized);
assertEquals(32, key.getKeyValue().toByteArray().length);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
key = HmacKey.parseFrom(keyManager.newKeyData(keyTemplate.getValue()).getValue());
assertEquals(32, key.getKeyValue().toByteArray().length);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
}
assertEquals(numTests, keys.size());
}
use of com.google.crypto.tink.proto.HmacKeyFormat in project tink by google.
the class MacKeyTemplatesTest method testHMAC_SHA256_256BITTAG.
@Test
public void testHMAC_SHA256_256BITTAG() throws Exception {
KeyTemplate template = MacKeyTemplates.HMAC_SHA256_256BITTAG;
assertEquals(HmacKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
HmacKeyFormat format = HmacKeyFormat.parseFrom(template.getValue());
assertEquals(32, format.getKeySize());
assertEquals(32, format.getParams().getTagSize());
assertEquals(HashType.SHA256, format.getParams().getHash());
}
use of com.google.crypto.tink.proto.HmacKeyFormat in project tink by google.
the class AeadKeyTemplates method createAesCtrHmacAeadKeyTemplate.
/**
* @return a {@link KeyTemplate} containing a {@link AesCtrHmacAeadKeyFormat} with some specific
* parameters.
*/
public static KeyTemplate createAesCtrHmacAeadKeyTemplate(int aesKeySize, int ivSize, int hmacKeySize, int tagSize, HashType hashType) {
AesCtrKeyFormat aesCtrKeyFormat = AesCtrKeyFormat.newBuilder().setParams(AesCtrParams.newBuilder().setIvSize(ivSize).build()).setKeySize(aesKeySize).build();
HmacKeyFormat hmacKeyFormat = HmacKeyFormat.newBuilder().setParams(HmacParams.newBuilder().setHash(hashType).setTagSize(tagSize).build()).setKeySize(hmacKeySize).build();
AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.newBuilder().setAesCtrKeyFormat(aesCtrKeyFormat).setHmacKeyFormat(hmacKeyFormat).build();
return KeyTemplate.newBuilder().setValue(format.toByteString()).setTypeUrl(AesCtrHmacAeadKeyManager.TYPE_URL).setOutputPrefixType(OutputPrefixType.TINK).build();
}
Aggregations