use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class EciesAeadHkdfPrivateKeyManagerTest method testGetPublicKeyData.
/**
* Tests that a public key is extracted properly from a private key.
*/
@Test
public void testGetPublicKeyData() throws Exception {
KeysetHandle privateHandle = KeysetHandle.generateNew(HybridKeyTemplates.ECIES_P256_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256);
KeyData privateKeyData = TestUtil.getKeyset(privateHandle).getKey(0).getKeyData();
EciesAeadHkdfPrivateKeyManager privateManager = new EciesAeadHkdfPrivateKeyManager();
KeyData publicKeyData = privateManager.getPublicKeyData(privateKeyData.getValue());
assertEquals(EciesAeadHkdfPublicKeyManager.TYPE_URL, publicKeyData.getTypeUrl());
assertEquals(KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC, publicKeyData.getKeyMaterialType());
EciesAeadHkdfPrivateKey privateKey = EciesAeadHkdfPrivateKey.parseFrom(privateKeyData.getValue());
assertArrayEquals(privateKey.getPublicKey().toByteArray(), publicKeyData.getValue().toByteArray());
EciesAeadHkdfPublicKeyManager publicManager = new EciesAeadHkdfPublicKeyManager();
HybridEncrypt hybridEncrypt = publicManager.getPrimitive(publicKeyData.getValue());
HybridDecrypt hybridDecrypt = privateManager.getPrimitive(privateKeyData.getValue());
byte[] message = Random.randBytes(20);
byte[] contextInfo = Random.randBytes(20);
assertArrayEquals(message, hybridDecrypt.decrypt(hybridEncrypt.encrypt(message, contextInfo), contextInfo));
}
use of com.google.crypto.tink.proto.KeyData 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.KeyData 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.KeyData 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.KeyData 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());
}
Aggregations