use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AesGcmKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
AesGcmKeyFormat gcmKeyFormat = AesGcmKeyFormat.newBuilder().setKeySize(16).build();
ByteString serialized = ByteString.copyFrom(gcmKeyFormat.toByteArray());
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesGcmKeyManager.TYPE_URL).setValue(serialized).build();
AesGcmKeyManager keyManager = new AesGcmKeyManager();
Set<String> keys = new TreeSet<String>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 27;
for (int i = 0; i < numTests / 3; i++) {
AesGcmKey key = (AesGcmKey) keyManager.newKey(gcmKeyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
key = (AesGcmKey) keyManager.newKey(serialized);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
key = AesGcmKey.parseFrom(keyData.getValue());
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
}
assertEquals(numTests, keys.size());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AesGcmKeyManagerTest method testNewKeyWithCorruptedFormat.
@Test
public void testNewKeyWithCorruptedFormat() throws Exception {
ByteString serialized = ByteString.copyFrom(new byte[128]);
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesGcmKeyManager.TYPE_URL).setValue(serialized).build();
AesGcmKeyManager keyManager = new AesGcmKeyManager();
try {
keyManager.newKey(serialized);
fail("Corrupted format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
try {
keyManager.newKeyData(keyTemplate.getValue());
fail("Corrupted format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
use of com.google.crypto.tink.proto.KeyTemplate 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.KeyTemplate in project tink by google.
the class AeadKeyTemplatesTest method testCreateKmsAeadKeyTemplate.
@Test
public void testCreateKmsAeadKeyTemplate() throws Exception {
// Intentionally using "weird" or invalid values for parameters,
// to test that the function correctly puts them in the resulting template.
String keyUri = "some example URI";
KeyTemplate template = AeadKeyTemplates.createKmsAeadKeyTemplate(keyUri);
assertEquals(KmsAeadKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
KmsAeadKeyFormat format = KmsAeadKeyFormat.parseFrom(template.getValue());
assertEquals(keyUri, format.getKeyUri());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AeadKeyTemplatesTest method testCHACHA20_POLY1305.
@Test
public void testCHACHA20_POLY1305() throws Exception {
KeyTemplate template = AeadKeyTemplates.CHACHA20_POLY1305;
assertEquals(ChaCha20Poly1305KeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
// Empty format.
assertTrue(template.getValue().isEmpty());
}
Aggregations