Search in sources :

Example 16 with KeyData

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());
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519PublicKey(com.google.crypto.tink.proto.Ed25519PublicKey) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 17 with KeyData

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));
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 18 with KeyData

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);
}
Also used : EcdsaPrivateKey(com.google.crypto.tink.proto.EcdsaPrivateKey) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 19 with KeyData

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());
    }
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Keyset(com.google.crypto.tink.proto.Keyset) PublicKeySignFactory(com.google.crypto.tink.signature.PublicKeySignFactory) PublicKeyVerifyFactory(com.google.crypto.tink.signature.PublicKeyVerifyFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BeforeClass(org.junit.BeforeClass) SignatureConfig(com.google.crypto.tink.signature.SignatureConfig) Assert.assertThrows(org.junit.Assert.assertThrows) KeyStatusType(com.google.crypto.tink.proto.KeyStatusType) RunWith(org.junit.runner.RunWith) EcdsaPrivateKey(com.google.crypto.tink.proto.EcdsaPrivateKey) Random(com.google.crypto.tink.subtle.Random) ProtoKey(com.google.crypto.tink.tinkkey.internal.ProtoKey) TreeSet(java.util.TreeSet) GeneralSecurityException(java.security.GeneralSecurityException) SignatureKeyTemplates(com.google.crypto.tink.signature.SignatureKeyTemplates) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyHandle(com.google.crypto.tink.tinkkey.KeyHandle) AesEaxKeyManager(com.google.crypto.tink.aead.AesEaxKeyManager) ExtensionRegistryLite(com.google.protobuf.ExtensionRegistryLite) Map(java.util.Map) TestUtil(com.google.crypto.tink.testing.TestUtil) SecretKeyAccess(com.google.crypto.tink.tinkkey.SecretKeyAccess) Expect(com.google.common.truth.Expect) AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) UTF_8(java.nio.charset.StandardCharsets.UTF_8) TinkConfig(com.google.crypto.tink.config.TinkConfig) OutputPrefixType(com.google.crypto.tink.proto.OutputPrefixType) Set(java.util.Set) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Hex(com.google.crypto.tink.subtle.Hex) Collectors(java.util.stream.Collectors) KeyStatusTypeProtoConverter(com.google.crypto.tink.internal.KeyStatusTypeProtoConverter) List(java.util.List) Rule(org.junit.Rule) KeyAccess(com.google.crypto.tink.tinkkey.KeyAccess) AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat) KeyData(com.google.crypto.tink.proto.KeyData) ProtoKey(com.google.crypto.tink.tinkkey.internal.ProtoKey) KeyHandle(com.google.crypto.tink.tinkkey.KeyHandle) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 20 with KeyData

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);
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519PublicKey(com.google.crypto.tink.proto.Ed25519PublicKey) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Aggregations

KeyData (com.google.crypto.tink.proto.KeyData)66 Test (org.junit.Test)55 Keyset (com.google.crypto.tink.proto.Keyset)17 KeyTemplate (com.google.crypto.tink.KeyTemplate)16 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)11 GeneralSecurityException (java.security.GeneralSecurityException)10 ByteString (com.google.protobuf.ByteString)9 TreeSet (java.util.TreeSet)9 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)7 KeysetReader (com.google.crypto.tink.KeysetReader)6 ProtoKey (com.google.crypto.tink.tinkkey.internal.ProtoKey)6 StringReader (java.io.StringReader)6 KeysetHandle (com.google.crypto.tink.KeysetHandle)5 RsaSsaPssPublicKey (com.google.crypto.tink.proto.RsaSsaPssPublicKey)5 BufferedReader (java.io.BufferedReader)5 RSAPublicKey (java.security.interfaces.RSAPublicKey)5 DummyAead (com.google.crypto.tink.TestUtil.DummyAead)4 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)4 Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)4 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)3