use of com.google.crypto.tink.proto.HmacKey in project tink by google.
the class RegistryTest method testGetPrimitive_Hmac_shouldWork.
@Test
public void testGetPrimitive_Hmac_shouldWork() throws Exception {
KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
HmacKey hmacKey = (HmacKey) Registry.newKey(template);
KeyData hmacKeyData = Registry.newKeyData(template);
Mac mac = Registry.getPrimitive(hmacKeyData);
assertThat(hmacKey.getKeyValue().size()).isEqualTo(32);
assertThat(hmacKey.getParams().getTagSize()).isEqualTo(16);
assertThat(hmacKey.getParams().getHash()).isEqualTo(HashType.SHA256);
assertThat(hmacKeyData.getTypeUrl()).isEqualTo(MacConfig.HMAC_TYPE_URL);
// This might break when we add native implementations.
assertThat(mac.getClass()).isEqualTo(MacJce.class);
}
use of com.google.crypto.tink.proto.HmacKey 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.HmacKey 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.HmacKey in project tink by google.
the class AesCtrHmacAeadKeyManager method newKey.
/**
* @param keyFormat {@code AesCtrHmacAeadKeyFormat} proto
* @return new {@code AesCtrHmacAeadKey} proto
*/
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
if (!(keyFormat instanceof AesCtrHmacAeadKeyFormat)) {
throw new GeneralSecurityException("expected AesCtrHmacAeadKeyFormat proto");
}
AesCtrHmacAeadKeyFormat format = (AesCtrHmacAeadKeyFormat) keyFormat;
AesCtrKey aesCtrKey = (AesCtrKey) Registry.newKey(AesCtrKeyManager.TYPE_URL, format.getAesCtrKeyFormat());
HmacKey hmacKey = (HmacKey) Registry.newKey(MacConfig.HMAC_TYPE_URL, format.getHmacKeyFormat());
return AesCtrHmacAeadKey.newBuilder().setAesCtrKey(aesCtrKey).setHmacKey(hmacKey).setVersion(VERSION).build();
}
use of com.google.crypto.tink.proto.HmacKey in project tink by google.
the class TestUtil method createAesCtrHmacAeadKeyData.
/**
* @return a {@code KeyData} containing a {@code AesCtrHmacAeadKey}.
*/
public static KeyData createAesCtrHmacAeadKeyData(byte[] aesCtrKeyValue, int ivSize, byte[] hmacKeyValue, int tagSize) throws Exception {
AesCtrKey aesCtrKey = createAesCtrKey(aesCtrKeyValue, ivSize);
HmacKey hmacKey = createHmacKey(hmacKeyValue, tagSize);
AesCtrHmacAeadKey keyProto = AesCtrHmacAeadKey.newBuilder().setAesCtrKey(aesCtrKey).setHmacKey(hmacKey).build();
return createKeyData(keyProto, AeadConfig.AES_CTR_HMAC_AEAD_TYPE_URL, KeyData.KeyMaterialType.SYMMETRIC);
}
Aggregations