Search in sources :

Example 21 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandleTest method toString_containsNoKeyMaterial.

@Test
public void toString_containsNoKeyMaterial() throws Exception {
    String keyValue = "01234567890123456";
    Keyset keyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK));
    KeysetHandle handle = KeysetHandle.fromKeyset(keyset);
    String keysetInfo = handle.toString();
    expect.that(keysetInfo).doesNotContain(keyValue);
    expect.that(handle.getKeyset().toString()).contains(keyValue);
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Test(org.junit.Test)

Example 22 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandleTest method readNoSecret_withTypeSymmetric_shouldThrow.

@Test
public void readNoSecret_withTypeSymmetric_shouldThrow() throws Exception {
    String keyValue = "01234567890123456";
    Keyset keyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK));
    assertThrows(GeneralSecurityException.class, () -> KeysetHandle.readNoSecret(keyset.toByteArray()));
    assertThrows(GeneralSecurityException.class, () -> KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(keyset.toByteArray())));
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Test(org.junit.Test)

Example 23 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandleTest method writeNoSecret_withTypeSymmetric_shouldThrow.

@Test
public void writeNoSecret_withTypeSymmetric_shouldThrow() throws Exception {
    String keyValue = "01234567890123456";
    Keyset keyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK));
    KeysetHandle handle = KeysetHandle.fromKeyset(keyset);
    assertThrows(GeneralSecurityException.class, () -> handle.writeNoSecret(/* writer= */
    null));
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Test(org.junit.Test)

Example 24 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandleTest method getPrimitive_wrappingDoneCorrectly.

// Tests that getPrimitive does correct wrapping and not just return the primary. For this, we
// simply add a raw, non-primary key and encrypt directly with it.
@Test
public void getPrimitive_wrappingDoneCorrectly() throws Exception {
    KeyData rawKeyData = Registry.newKeyData(KeyTemplates.get("AES128_EAX"));
    Keyset keyset = TestUtil.createKeyset(TestUtil.createKey(Registry.newKeyData(KeyTemplates.get("AES128_EAX").getProto()), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK), TestUtil.createKey(rawKeyData, 43, KeyStatusType.ENABLED, OutputPrefixType.RAW));
    KeysetHandle handle = KeysetHandle.fromKeyset(keyset);
    byte[] message = Random.randBytes(20);
    byte[] aad = Random.randBytes(20);
    Aead aeadToEncrypt = Registry.getPrimitive(rawKeyData, Aead.class);
    Aead aead = handle.getPrimitive(Aead.class);
    assertThat(aead.decrypt(aeadToEncrypt.encrypt(message, aad), aad)).isEqualTo(message);
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 25 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandleTest method readNoSecret_shouldWork.

@Test
public void readNoSecret_shouldWork() throws Exception {
    KeysetHandle privateHandle = KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256);
    Keyset keyset = privateHandle.getPublicKeysetHandle().getKeyset();
    Keyset keyset2 = KeysetHandle.readNoSecret(keyset.toByteArray()).getKeyset();
    Keyset keyset3 = KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(keyset.toByteArray())).getKeyset();
    expect.that(keyset).isEqualTo(keyset2);
    expect.that(keyset).isEqualTo(keyset3);
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Test(org.junit.Test)

Aggregations

Keyset (com.google.crypto.tink.proto.Keyset)108 Test (org.junit.Test)81 GeneralSecurityException (java.security.GeneralSecurityException)22 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)17 KeysetHandle (com.google.crypto.tink.KeysetHandle)17 KeyData (com.google.crypto.tink.proto.KeyData)17 KeyTemplate (com.google.crypto.tink.KeyTemplate)12 EncryptedKeyset (com.google.crypto.tink.proto.EncryptedKeyset)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteString (com.google.protobuf.ByteString)10 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)10 Key (com.google.crypto.tink.proto.Keyset.Key)9 JsonObject (com.google.gson.JsonObject)9 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)8 KeysetReader (com.google.crypto.tink.KeysetReader)7 IOException (java.io.IOException)7 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)6 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)6 Enums (com.google.crypto.tink.subtle.Enums)6 KeyHandle (com.google.crypto.tink.tinkkey.KeyHandle)6