use of com.google.crypto.tink.proto.AesCtrHmacAeadKey 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.AesCtrHmacAeadKey in project tink by google.
the class AesCtrHmacAeadKeyManager method getPrimitive.
/**
* @param key {@code AesCtrHmacAeadKey} proto
*/
@Override
public Aead getPrimitive(MessageLite key) throws GeneralSecurityException {
if (!(key instanceof AesCtrHmacAeadKey)) {
throw new GeneralSecurityException("expected AesCtrHmacAeadKey proto");
}
AesCtrHmacAeadKey keyProto = (AesCtrHmacAeadKey) key;
validate(keyProto);
return new EncryptThenAuthenticate((IndCpaCipher) Registry.getPrimitive(AesCtrKeyManager.TYPE_URL, keyProto.getAesCtrKey()), (Mac) Registry.getPrimitive(MacConfig.HMAC_TYPE_URL, keyProto.getHmacKey()), keyProto.getHmacKey().getParams().getTagSize());
}
use of com.google.crypto.tink.proto.AesCtrHmacAeadKey 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