use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class RegistryTest method testGetPrimitive_AesGcm_shouldWork.
@Test
public void testGetPrimitive_AesGcm_shouldWork() throws Exception {
KeyTemplate template = AeadKeyTemplates.AES128_EAX;
AesEaxKey aesEaxKey = (AesEaxKey) Registry.newKey(template);
KeyData aesEaxKeyData = Registry.newKeyData(template);
Aead aead = Registry.getPrimitive(aesEaxKeyData);
assertThat(aesEaxKey.getKeyValue().size()).isEqualTo(16);
assertThat(aesEaxKeyData.getTypeUrl()).isEqualTo(AeadConfig.AES_EAX_TYPE_URL);
// This might break when we add native implementations.
assertThat(aead.getClass()).isEqualTo(AesEaxJce.class);
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class EciesAeadHkdfPrivateKeyManagerTest method testNewKey.
@Test
public void testNewKey() throws Exception {
EllipticCurveType curve = EllipticCurveType.NIST_P384;
HashType hashType = HashType.SHA256;
EcPointFormat pointFormat = EcPointFormat.UNCOMPRESSED;
KeyTemplate demKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
byte[] salt = "some salt".getBytes("UTF-8");
EciesAeadHkdfParams params = HybridKeyTemplates.createEciesAeadHkdfParams(curve, hashType, pointFormat, demKeyTemplate, salt);
EciesAeadHkdfPrivateKeyManager manager = new EciesAeadHkdfPrivateKeyManager();
EciesAeadHkdfPrivateKey keyProto = (EciesAeadHkdfPrivateKey) manager.newKey(EciesAeadHkdfKeyFormat.newBuilder().setParams(params).build());
assertEquals(params, keyProto.getPublicKey().getParams());
Key primaryPriv = TestUtil.createKey(TestUtil.createKeyData(keyProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 8, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key primaryPub = TestUtil.createKey(TestUtil.createKeyData(keyProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
KeysetHandle keysetHandlePub = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPub));
KeysetHandle keysetHandlePriv = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPriv));
HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(keysetHandlePub);
HybridDecrypt hybridDecrypt = HybridDecryptFactory.getPrimitive(keysetHandlePriv);
byte[] plaintext = Random.randBytes(20);
byte[] contextInfo = Random.randBytes(20);
byte[] ciphertext = hybridEncrypt.encrypt(plaintext, contextInfo);
assertArrayEquals(plaintext, hybridDecrypt.decrypt(ciphertext, contextInfo));
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AesSivKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
for (KeyTemplate keyTemplate : keyTemplates) {
AesSivKeyManager keyManager = new AesSivKeyManager();
Set<String> keys = new TreeSet<String>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 10;
for (int i = 0; i < numTests; i++) {
AesSivKey key = (AesSivKey) keyManager.newKey(keyTemplate.getValue());
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
key = AesSivKey.parseFrom(keyData.getValue());
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
}
assertEquals(numTests * 2, keys.size());
}
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class ChaCha20Poly1305KeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
KeyTemplate keyTemplate = AeadKeyTemplates.CHACHA20_POLY1305;
ChaCha20Poly1305KeyManager keyManager = new ChaCha20Poly1305KeyManager();
Set<String> keys = new TreeSet<String>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 10;
for (int i = 0; i < numTests; i++) {
ChaCha20Poly1305Key key = (ChaCha20Poly1305Key) keyManager.newKey(keyTemplate.getValue());
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(32, key.getKeyValue().toByteArray().length);
KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
key = ChaCha20Poly1305Key.parseFrom(keyData.getValue());
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(32, key.getKeyValue().toByteArray().length);
}
assertEquals(numTests * 2, keys.size());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class DeterministicAeadKeyTemplatesTest method testCreateAesSivKeyTemplate.
@Test
public void testCreateAesSivKeyTemplate() 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;
KeyTemplate template = DeterministicAeadKeyTemplates.createAesSivKeyTemplate(keySize);
assertEquals(AesSivKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesSivKeyFormat format = AesSivKeyFormat.parseFrom(template.getValue());
assertEquals(keySize, format.getKeySize());
}
Aggregations