Search in sources :

Example 26 with KeyTemplate

use of com.google.crypto.tink.KeyTemplate in project tink by google.

the class KeyHandleTest method generateNew_shouldWork.

@Test
public void generateNew_shouldWork() throws Exception {
    KeyTemplate template = KeyTemplates.get("AES128_EAX");
    KeyHandle handle = KeyHandle.generateNew(template);
    ProtoKey protoKey = (ProtoKey) handle.getKey(SecretKeyAccess.insecureSecretAccess());
    expect.that(protoKey.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.TINK);
    expect.that(protoKey.hasSecret()).isTrue();
    KeyData keyData = protoKey.getProtoKey();
    expect.that(keyData.getTypeUrl()).isEqualTo(template.getTypeUrl());
    AesEaxKeyFormat aesEaxKeyFormat = AesEaxKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    AesEaxKey aesEaxKey = AesEaxKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    expect.that(aesEaxKey.getKeyValue().size()).isEqualTo(aesEaxKeyFormat.getKeySize());
}
Also used : AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) ProtoKey(com.google.crypto.tink.tinkkey.internal.ProtoKey) AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 27 with KeyTemplate

use of com.google.crypto.tink.KeyTemplate in project tink by google.

the class KeyHandleTest method createFromKey_keyDataRemote_shouldNotHaveSecret.

@Test
public void createFromKey_keyDataRemote_shouldNotHaveSecret() throws Exception {
    KeyTemplate kt = KeyTemplates.get("ED25519");
    KeyData kd = KeyData.newBuilder().mergeFrom(Registry.newKeyData(kt)).setKeyMaterialType(KeyData.KeyMaterialType.REMOTE).build();
    KeyHandle kh = KeyHandle.createFromKey(kd, kt.getOutputPrefixType());
    assertThat(kh.hasSecret()).isFalse();
}
Also used : KeyTemplate(com.google.crypto.tink.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 28 with KeyTemplate

use of com.google.crypto.tink.KeyTemplate in project tink by google.

the class KeyHandleTest method generateNew_compareWith_createFromKeyViaProtoKey_shouldBeEqual.

@Test
public void generateNew_compareWith_createFromKeyViaProtoKey_shouldBeEqual() throws Exception {
    KeyTemplate template = KeyTemplates.get("AES128_EAX");
    KeyData keyData = Registry.newKeyData(template);
    ProtoKey protoKey = new ProtoKey(keyData, template.getOutputPrefixType());
    KeyHandle handle1 = KeyHandle.generateNew(template);
    KeyHandle handle2 = KeyHandle.createFromKey(protoKey, SecretKeyAccess.insecureSecretAccess());
    expect.that(handle1.getStatus()).isEqualTo(handle2.getStatus());
    ProtoKey outputProtoKey1 = (ProtoKey) handle1.getKey(SecretKeyAccess.insecureSecretAccess());
    ProtoKey outputProtoKey2 = (ProtoKey) handle2.getKey(SecretKeyAccess.insecureSecretAccess());
    expect.that(outputProtoKey1.getOutputPrefixType()).isEqualTo(outputProtoKey2.getOutputPrefixType());
    expect.that(handle1.hasSecret()).isEqualTo(handle2.hasSecret());
}
Also used : ProtoKey(com.google.crypto.tink.tinkkey.internal.ProtoKey) KeyTemplate(com.google.crypto.tink.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 29 with KeyTemplate

use of com.google.crypto.tink.KeyTemplate in project tink by google.

the class KeyHandleTest method generateNew_unregisteredTypeUrl_shouldThrow.

@Test
public void generateNew_unregisteredTypeUrl_shouldThrow() throws Exception {
    String typeUrl = "testNewKeyDataTypeUrl";
    ByteString keyformat = ByteString.copyFromUtf8("testNewKeyDataKeyFormat");
    com.google.crypto.tink.KeyTemplate keyTemplate = com.google.crypto.tink.KeyTemplate.create(typeUrl, keyformat.toByteArray(), OutputPrefixType.TINK);
    assertThrows(GeneralSecurityException.class, () -> KeyHandle.generateNew(keyTemplate));
}
Also used : ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 30 with KeyTemplate

use of com.google.crypto.tink.KeyTemplate in project tink by google.

the class ProtoKeyTest method testProtoKey_keyDataUNKNOWN_shouldHaveSecret.

@Test
public void testProtoKey_keyDataUNKNOWN_shouldHaveSecret() throws GeneralSecurityException {
    KeyTemplate kt = KeyTemplates.get("ED25519");
    KeyData kd = KeyData.newBuilder().mergeFrom(Registry.newKeyData(kt)).setKeyMaterialType(KeyData.KeyMaterialType.UNKNOWN_KEYMATERIAL).build();
    ProtoKey pk = new ProtoKey(kd, kt.getOutputPrefixType());
    assertThat(pk.getProtoKey()).isEqualTo(kd);
    assertThat(pk.getOutputPrefixType()).isEqualTo(kt.getOutputPrefixType());
    assertThat(pk.hasSecret()).isTrue();
}
Also used : KeyTemplate(com.google.crypto.tink.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Aggregations

KeyTemplate (com.google.crypto.tink.KeyTemplate)143 Test (org.junit.Test)135 KeysetHandle (com.google.crypto.tink.KeysetHandle)56 ByteString (com.google.protobuf.ByteString)39 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)37 KeyData (com.google.crypto.tink.proto.KeyData)16 Keyset (com.google.crypto.tink.proto.Keyset)12 JsonObject (com.google.gson.JsonObject)8 KeysetManager (com.google.crypto.tink.KeysetManager)7 BigInteger (java.math.BigInteger)7 Instant (java.time.Instant)7 Aead (com.google.crypto.tink.Aead)6 Enums (com.google.crypto.tink.subtle.Enums)6 Clock (java.time.Clock)6 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)5 AesCtrHmacStreamingKeyFormat (com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat)4 AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)4 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)4 AesGcmSivKeyFormat (com.google.crypto.tink.proto.AesGcmSivKeyFormat)4 EciesAeadHkdfKeyFormat (com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat)4