use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class RegistryTest method testRegisterAssymmetricKeyManagers_publicKeyManagerReRegister_getPublicKeyData.
@Test
public void testRegisterAssymmetricKeyManagers_publicKeyManagerReRegister_getPublicKeyData() throws Exception {
Registry.reset();
Registry.registerKeyManager(new TestPublicKeyTypeManager(), false);
Registry.registerAsymmetricKeyManagers(new TestPrivateKeyTypeManager(), new TestPublicKeyTypeManager(), false);
Registry.registerKeyManager(new TestPublicKeyTypeManager(), false);
// Check that getPublicKeyData works now.
Ed25519PrivateKey privateKey = Ed25519PrivateKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(32))).setPublicKey(Ed25519PublicKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(32)))).build();
KeyData publicKeyData = Registry.getPublicKeyData(new TestPrivateKeyTypeManager().getKeyType(), privateKey.toByteString());
assertThat(publicKeyData.getTypeUrl()).isEqualTo(new TestPublicKeyTypeManager().getKeyType());
Ed25519PublicKey publicKey = Ed25519PublicKey.parseFrom(publicKeyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertThat(publicKey.getKeyValue()).isEqualTo(privateKey.getPublicKey().getKeyValue());
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class RegistryTest method testAsymmetricKeyManagers_deriveKey.
@Test
public void testAsymmetricKeyManagers_deriveKey() throws Exception {
Registry.reset();
Registry.registerAsymmetricKeyManagers(new TestPrivateKeyTypeManagerWithKeyFactory(), new TestPublicKeyTypeManager(), true);
com.google.crypto.tink.proto.KeyTemplate template = com.google.crypto.tink.proto.KeyTemplate.newBuilder().setValue(Ed25519KeyFormat.getDefaultInstance().toByteString()).setTypeUrl(new TestPrivateKeyTypeManagerWithKeyFactory().getKeyType()).setOutputPrefixType(OutputPrefixType.TINK).build();
KeyData keyData = Registry.deriveKey(template, new ByteArrayInputStream(new byte[0]));
Ed25519PrivateKey key = Ed25519PrivateKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertThat(key.getKeyValue()).isEqualTo(ByteString.copyFrom("derived", UTF_8));
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class KeysetHandleTest method getPublicKeysetHandle_shouldWork.
@Test
public void getPublicKeysetHandle_shouldWork() throws Exception {
KeysetHandle privateHandle = KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256);
KeyData privateKeyData = privateHandle.getKeyset().getKey(0).getKeyData();
EcdsaPrivateKey privateKey = EcdsaPrivateKey.parseFrom(privateKeyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
KeysetHandle publicHandle = privateHandle.getPublicKeysetHandle();
expect.that(publicHandle.getKeyset().getKeyCount()).isEqualTo(1);
expect.that(privateHandle.getKeyset().getPrimaryKeyId()).isEqualTo(publicHandle.getKeyset().getPrimaryKeyId());
KeyData publicKeyData = publicHandle.getKeyset().getKey(0).getKeyData();
expect.that(publicKeyData.getTypeUrl()).isEqualTo(SignatureConfig.ECDSA_PUBLIC_KEY_TYPE_URL);
expect.that(publicKeyData.getKeyMaterialType()).isEqualTo(KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC);
expect.that(publicKeyData.getValue().toByteArray()).isEqualTo(privateKey.getPublicKey().toByteArray());
PublicKeySign signer = PublicKeySignFactory.getPrimitive(privateHandle);
PublicKeyVerify verifier = PublicKeyVerifyFactory.getPrimitive(publicHandle);
byte[] message = Random.randBytes(20);
verifier.verify(signer.sign(message), message);
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class KeysetHandleTest method getKeys.
@Test
public void getKeys() throws Exception {
KeyTemplate keyTemplate = KeyTemplates.get("AES128_EAX");
KeysetManager keysetManager = KeysetManager.withEmptyKeyset();
final int numKeys = 3;
for (int i = 0; i < numKeys; i++) {
keysetManager.add(keyTemplate);
}
KeysetHandle handle = keysetManager.getKeysetHandle();
Keyset keyset = handle.getKeyset();
List<KeyHandle> keysetKeys = handle.getKeys();
expect.that(keysetKeys).hasSize(numKeys);
Map<Integer, KeyHandle> keysetKeysMap = keysetKeys.stream().collect(Collectors.toMap(KeyHandle::getId, key -> key));
for (Keyset.Key key : keyset.getKeyList()) {
expect.that(keysetKeysMap).containsKey(key.getKeyId());
KeyHandle keysetKey = keysetKeysMap.get(key.getKeyId());
expect.that(KeyStatusTypeProtoConverter.toProto(keysetKey.getStatus())).isEqualTo(key.getStatus());
KeyData keyData = ((ProtoKey) keysetKey.getKey(SecretKeyAccess.insecureSecretAccess())).getProtoKey();
expect.that(keyData).isEqualTo(key.getKeyData());
}
}
use of com.google.crypto.tink.proto.KeyData in project tink by google.
the class PrivateKeyManagerImplTest method getPublicKeyData_works.
@Test
public void getPublicKeyData_works() throws Exception {
TestPrivateKeyTypeManager privateManager = new TestPrivateKeyTypeManager();
TestPublicKeyTypeManager publicManager = new TestPublicKeyTypeManager();
PrivateKeyManager<Void> manager = new PrivateKeyManagerImpl<>(privateManager, publicManager, Void.class);
Ed25519PrivateKey privateKey = Ed25519PrivateKey.newBuilder().setPublicKey(Ed25519PublicKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(32)))).setKeyValue(ByteString.copyFrom(Random.randBytes(32))).build();
KeyData keyData = manager.getPublicKeyData(privateKey.toByteString());
assertThat(keyData.getTypeUrl()).isEqualTo("type.googleapis.com/google.crypto.tink.Ed25519PublicKey");
Ed25519PublicKey publicKey = Ed25519PublicKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertThat(publicKey).isEqualTo(privateKey.getPublicKey());
assertThat(keyData.getKeyMaterialType()).isEqualTo(KeyMaterialType.ASYMMETRIC_PUBLIC);
}
Aggregations