use of com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat in project tink by google.
the class AesCtrHmacAeadKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
KeyTemplate keyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
AesCtrHmacAeadKeyFormat aeadKeyFormat = AesCtrHmacAeadKeyFormat.parseFrom(keyTemplate.getValue().toByteArray());
ByteString serialized = ByteString.copyFrom(aeadKeyFormat.toByteArray());
AesCtrHmacAeadKeyManager keyManager = new AesCtrHmacAeadKeyManager();
Set<String> keys = new TreeSet<String>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 24;
for (int i = 0; i < numTests / 6; i++) {
AesCtrHmacAeadKey key = (AesCtrHmacAeadKey) keyManager.newKey(aeadKeyFormat);
keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
key = (AesCtrHmacAeadKey) keyManager.newKey(serialized);
keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
key = AesCtrHmacAeadKey.parseFrom(keyData.getValue());
keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
}
assertEquals(numTests, keys.size());
}
use of com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat in project tink by google.
the class AeadKeyTemplatesTest method testAES256_CTR_HMAC_SHA256.
@Test
public void testAES256_CTR_HMAC_SHA256() throws Exception {
KeyTemplate template = AeadKeyTemplates.AES256_CTR_HMAC_SHA256;
assertEquals(AesCtrHmacAeadKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasAesCtrKeyFormat());
assertTrue(format.getAesCtrKeyFormat().hasParams());
assertEquals(32, format.getAesCtrKeyFormat().getKeySize());
assertEquals(16, format.getAesCtrKeyFormat().getParams().getIvSize());
assertTrue(format.hasHmacKeyFormat());
assertTrue(format.getHmacKeyFormat().hasParams());
assertEquals(32, format.getHmacKeyFormat().getKeySize());
assertEquals(32, format.getHmacKeyFormat().getParams().getTagSize());
assertEquals(HashType.SHA256, format.getHmacKeyFormat().getParams().getHash());
}
use of com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat in project tink by google.
the class AeadKeyTemplatesTest method testAES128_CTR_HMAC_SHA256.
@Test
public void testAES128_CTR_HMAC_SHA256() throws Exception {
KeyTemplate template = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
assertEquals(AesCtrHmacAeadKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasAesCtrKeyFormat());
assertTrue(format.getAesCtrKeyFormat().hasParams());
assertEquals(16, format.getAesCtrKeyFormat().getKeySize());
assertEquals(16, format.getAesCtrKeyFormat().getParams().getIvSize());
assertTrue(format.hasHmacKeyFormat());
assertTrue(format.getHmacKeyFormat().hasParams());
assertEquals(32, format.getHmacKeyFormat().getKeySize());
assertEquals(16, format.getHmacKeyFormat().getParams().getTagSize());
assertEquals(HashType.SHA256, format.getHmacKeyFormat().getParams().getHash());
}
use of com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat in project tink by google.
the class AeadKeyTemplatesTest method testCreateAesCtrHmacAeadKeyTemplate.
@Test
public void testCreateAesCtrHmacAeadKeyTemplate() throws Exception {
// Intentionally using "weird" or invalid values for parameters,
// to test that the function correctly puts them in the resulting template.
int aesKeySize = 42;
int ivSize = 72;
int hmacKeySize = 24;
int tagSize = 27;
HashType hashType = HashType.SHA224;
KeyTemplate template = AeadKeyTemplates.createAesCtrHmacAeadKeyTemplate(aesKeySize, ivSize, hmacKeySize, tagSize, hashType);
assertEquals(AesCtrHmacAeadKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasAesCtrKeyFormat());
assertTrue(format.getAesCtrKeyFormat().hasParams());
assertEquals(aesKeySize, format.getAesCtrKeyFormat().getKeySize());
assertEquals(ivSize, format.getAesCtrKeyFormat().getParams().getIvSize());
assertTrue(format.hasHmacKeyFormat());
assertTrue(format.getHmacKeyFormat().hasParams());
assertEquals(hmacKeySize, format.getHmacKeyFormat().getKeySize());
assertEquals(tagSize, format.getHmacKeyFormat().getParams().getTagSize());
assertEquals(hashType, format.getHmacKeyFormat().getParams().getHash());
}
use of com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat 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();
}
Aggregations