use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class AesCtrHmacStreamingKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
AesCtrHmacStreamingKeyFormat keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(16).build();
ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
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++) {
AesCtrHmacStreamingKey key = (AesCtrHmacStreamingKey) keyManager.newKey(keyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
key = (AesCtrHmacStreamingKey) keyManager.newKey(serializedKeyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
KeyData keyData = keyManager.newKeyData(serializedKeyFormat);
key = AesCtrHmacStreamingKey.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 KeysetManager method newKey.
@GuardedBy("this")
private synchronized Keyset.Key newKey(KeyTemplate keyTemplate) throws GeneralSecurityException {
KeyData keyData = Registry.newKeyData(keyTemplate);
int keyId = newKeyId();
OutputPrefixType outputPrefixType = keyTemplate.getOutputPrefixType();
if (outputPrefixType == OutputPrefixType.UNKNOWN_PREFIX) {
outputPrefixType = OutputPrefixType.TINK;
}
return Keyset.Key.newBuilder().setKeyData(keyData).setKeyId(keyId).setStatus(KeyStatusType.ENABLED).setOutputPrefixType(outputPrefixType).build();
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class RegistryTest method testGetPrimitives_KeysetWithNoPrimaryKey_shouldThrowException.
@Test
public void testGetPrimitives_KeysetWithNoPrimaryKey_shouldThrowException() throws Exception {
// Create a keyset without a primary key.
KeyData key1 = Registry.newKeyData(MacKeyTemplates.HMAC_SHA256_128BITTAG);
KeysetHandle keysetHandle = KeysetHandle.fromKeyset(Keyset.newBuilder().addKey(Keyset.Key.newBuilder().setKeyData(key1).setKeyId(1).setStatus(KeyStatusType.ENABLED).setOutputPrefixType(OutputPrefixType.TINK).build()).build());
// No primary key.
try {
Registry.getPrimitives(keysetHandle);
fail("Invalid keyset. Expect GeneralSecurityException");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "keyset doesn't contain a valid primary key");
}
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class RegistryTest method testGetPrimitives_KeysetWithMultiplePrimaryKeys_shouldThrowException.
@Test
public void testGetPrimitives_KeysetWithMultiplePrimaryKeys_shouldThrowException() throws Exception {
// Multiple primary keys.
KeyData key1 = Registry.newKeyData(MacKeyTemplates.HMAC_SHA256_128BITTAG);
KeysetHandle keysetHandle = KeysetHandle.fromKeyset(Keyset.newBuilder().addKey(Keyset.Key.newBuilder().setKeyData(key1).setKeyId(1).setStatus(KeyStatusType.ENABLED).setOutputPrefixType(OutputPrefixType.TINK).build()).addKey(Keyset.Key.newBuilder().setKeyData(key1).setKeyId(1).setStatus(KeyStatusType.ENABLED).setOutputPrefixType(OutputPrefixType.TINK).build()).setPrimaryKeyId(1).build());
try {
Registry.getPrimitives(keysetHandle);
fail("Invalid keyset. Expect GeneralSecurityException");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "keyset contains multiple primary keys");
}
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class KeysetHandleTest method testGetPublicKeysetHandle.
/**
* Tests a public keyset is extracted properly from a private keyset.
*/
@Test
public void testGetPublicKeysetHandle() throws Exception {
KeysetHandle privateHandle = KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256);
KeyData privateKeyData = privateHandle.getKeyset().getKey(0).getKeyData();
EcdsaPrivateKey privateKey = EcdsaPrivateKey.parseFrom(privateKeyData.getValue());
KeysetHandle publicHandle = privateHandle.getPublicKeysetHandle();
assertEquals(1, publicHandle.getKeyset().getKeyCount());
assertEquals(privateHandle.getKeyset().getPrimaryKeyId(), publicHandle.getKeyset().getPrimaryKeyId());
KeyData publicKeyData = publicHandle.getKeyset().getKey(0).getKeyData();
assertEquals(SignatureConfig.ECDSA_PUBLIC_KEY_TYPE_URL, publicKeyData.getTypeUrl());
assertEquals(KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC, publicKeyData.getKeyMaterialType());
assertArrayEquals(privateKey.getPublicKey().toByteArray(), publicKeyData.getValue().toByteArray());
PublicKeySign signer = PublicKeySignFactory.getPrimitive(privateHandle);
PublicKeyVerify verifier = PublicKeyVerifyFactory.getPrimitive(publicHandle);
byte[] message = Random.randBytes(20);
try {
verifier.verify(signer.sign(message), message);
} catch (GeneralSecurityException e) {
fail("Should not fail: " + e);
}
}
Aggregations