use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AesEaxKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
AesEaxKeyFormat eaxKeyFormat = AesEaxKeyFormat.newBuilder().setParams(AesEaxParams.newBuilder().setIvSize(16).build()).setKeySize(16).build();
ByteString serialized = ByteString.copyFrom(eaxKeyFormat.toByteArray());
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesEaxKeyManager.TYPE_URL).setValue(serialized).build();
AesEaxKeyManager keyManager = new AesEaxKeyManager();
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++) {
AesEaxKey key = (AesEaxKey) keyManager.newKey(eaxKeyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
key = (AesEaxKey) keyManager.newKey(serialized);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
key = AesEaxKey.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 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.KeyTemplate in project tink by google.
the class AesCtrHmacAeadKeyManagerTest method testNewKeyWithCorruptedFormat.
@Test
public void testNewKeyWithCorruptedFormat() throws Exception {
ByteString serialized = ByteString.copyFrom(new byte[128]);
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesCtrHmacAeadKeyManager.TYPE_URL).setValue(serialized).build();
AesCtrHmacAeadKeyManager keyManager = new AesCtrHmacAeadKeyManager();
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 AesCtrKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
AesCtrKeyFormat ctrKeyFormat = AesCtrKeyFormat.newBuilder().setParams(AesCtrParams.newBuilder().setIvSize(16).build()).setKeySize(16).build();
ByteString serialized = ByteString.copyFrom(ctrKeyFormat.toByteArray());
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesCtrKeyManager.TYPE_URL).setValue(serialized).build();
AesCtrKeyManager keyManager = new AesCtrKeyManager();
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++) {
AesCtrKey key = (AesCtrKey) keyManager.newKey(ctrKeyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
key = (AesCtrKey) keyManager.newKey(serialized);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
key = AesCtrKey.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 AesCtrKeyManagerTest method testNewKeyWithCorruptedFormat.
@Test
public void testNewKeyWithCorruptedFormat() throws Exception {
ByteString serialized = ByteString.copyFrom(new byte[128]);
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesCtrKeyManager.TYPE_URL).setValue(serialized).build();
AesCtrKeyManager keyManager = new AesCtrKeyManager();
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
}
}
Aggregations