use of com.google.crypto.tink.proto.AesEaxKeyFormat in project tink by google.
the class AesEaxKeyManager method newKey.
/**
* @param keyFormat {@code AesEaxKeyFormat} proto
* @return new {@code AesEaxKey} proto
*/
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
if (!(keyFormat instanceof AesEaxKeyFormat)) {
throw new GeneralSecurityException("expected AesEaxKeyFormat proto");
}
AesEaxKeyFormat format = (AesEaxKeyFormat) keyFormat;
validate(format);
return AesEaxKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).setParams(format.getParams()).setVersion(VERSION).build();
}
use of com.google.crypto.tink.proto.AesEaxKeyFormat 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.AesEaxKeyFormat in project tink by google.
the class AeadKeyTemplatesTest method testAES128_EAX.
@Test
public void testAES128_EAX() throws Exception {
KeyTemplate template = AeadKeyTemplates.AES128_EAX;
assertEquals(AesEaxKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesEaxKeyFormat format = AesEaxKeyFormat.parseFrom(template.getValue());
assertEquals(16, format.getKeySize());
assertTrue(format.hasParams());
assertEquals(16, format.getParams().getIvSize());
}
use of com.google.crypto.tink.proto.AesEaxKeyFormat in project tink by google.
the class AeadKeyTemplatesTest method testCreateAesEaxKeyTemplate.
@Test
public void testCreateAesEaxKeyTemplate() throws Exception {
// Intentionally using "weird" or invalid values for parameters,
// to test that the function correctly puts them in the resulting template.
int keySize = 42;
int ivSize = 72;
KeyTemplate template = AeadKeyTemplates.createAesEaxKeyTemplate(keySize, ivSize);
assertEquals(AesEaxKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesEaxKeyFormat format = AesEaxKeyFormat.parseFrom(template.getValue());
assertEquals(keySize, format.getKeySize());
assertTrue(format.hasParams());
assertEquals(ivSize, format.getParams().getIvSize());
}
use of com.google.crypto.tink.proto.AesEaxKeyFormat in project tink by google.
the class AeadKeyTemplatesTest method testAES256_EAX.
@Test
public void testAES256_EAX() throws Exception {
KeyTemplate template = AeadKeyTemplates.AES256_EAX;
assertEquals(AesEaxKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesEaxKeyFormat format = AesEaxKeyFormat.parseFrom(template.getValue());
assertEquals(32, format.getKeySize());
assertTrue(format.hasParams());
assertEquals(16, format.getParams().getIvSize());
}
Aggregations