use of com.google.crypto.tink.proto.AesCtrKey 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);
}
use of com.google.crypto.tink.proto.AesCtrKey in project tink by google.
the class AesCtrKeyManagerTest method getPrimitive.
@Test
public void getPrimitive() throws Exception {
AesCtrKey key = factory.createKey(createFormat(14, 32));
IndCpaCipher managerCipher = manager.getPrimitive(key, IndCpaCipher.class);
IndCpaCipher directCipher = new AesCtrJceCipher(key.getKeyValue().toByteArray(), 14);
byte[] plaintext = Random.randBytes(20);
assertThat(directCipher.decrypt(managerCipher.encrypt(plaintext))).isEqualTo(plaintext);
}
use of com.google.crypto.tink.proto.AesCtrKey in project tink by google.
the class AesCtrKeyManager method getPrimitive.
/**
* @param key {@code AesCtrKey} proto
*/
@Override
public AesCtrJceCipher getPrimitive(MessageLite key) throws GeneralSecurityException {
if (!(key instanceof AesCtrKey)) {
throw new GeneralSecurityException("expected AesCtrKey proto");
}
AesCtrKey keyProto = (AesCtrKey) key;
validate(keyProto);
return new AesCtrJceCipher(keyProto.getKeyValue().toByteArray(), keyProto.getParams().getIvSize());
}
use of com.google.crypto.tink.proto.AesCtrKey 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.AesCtrKey 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