Search in sources :

Example 21 with KeyTemplate

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

the class JsonKeysetWriterTest method testWriteEncrypted_shouldWork.

private void testWriteEncrypted_shouldWork(KeysetHandle handle1) throws Exception {
    // Encrypt the keyset with an AeadKey.
    KeyTemplate masterKeyTemplate = AeadKeyTemplates.AES128_EAX;
    Aead masterKey = Registry.getPrimitive(Registry.newKeyData(masterKeyTemplate));
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    handle1.write(JsonKeysetWriter.withOutputStream(outputStream), masterKey);
    KeysetHandle handle2 = KeysetHandle.read(JsonKeysetReader.withInputStream(new ByteArrayInputStream(outputStream.toByteArray())), masterKey);
    assertKeysetHandle(handle1, handle2);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate)

Example 22 with KeyTemplate

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

the class KeysetHandleTest method testWriteEncrypted.

@Test
public void testWriteEncrypted() throws Exception {
    KeysetHandle handle = KeysetHandle.generateNew(MacKeyTemplates.HMAC_SHA256_128BITTAG);
    // Encrypt the keyset with an AeadKey.
    KeyTemplate masterKeyTemplate = AeadKeyTemplates.AES128_EAX;
    Aead masterKey = Registry.getPrimitive(Registry.newKeyData(masterKeyTemplate));
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    KeysetWriter writer = BinaryKeysetWriter.withOutputStream(outputStream);
    handle.write(writer, masterKey);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    KeysetReader reader = BinaryKeysetReader.withInputStream(inputStream);
    KeysetHandle handle2 = KeysetHandle.read(reader, masterKey);
    assertEquals(handle.getKeyset(), handle2.getKeyset());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 23 with KeyTemplate

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

the class JsonKeysetReaderTest method testRead_singleKey_shouldWork.

@Test
public void testRead_singleKey_shouldWork() throws Exception {
    KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
    KeysetHandle handle1 = KeysetHandle.generateNew(template);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CleartextKeysetHandle.write(handle1, JsonKeysetWriter.withOutputStream(outputStream));
    KeysetHandle handle2 = CleartextKeysetHandle.read(JsonKeysetReader.withInputStream(new ByteArrayInputStream(outputStream.toByteArray())));
    assertKeysetHandle(handle1, handle2);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 24 with KeyTemplate

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

the class KeysetManagerTest method testRotate_bogusKeyTemplate_shouldThrowException.

@Test
public void testRotate_bogusKeyTemplate_shouldThrowException() throws Exception {
    KeyTemplate bogus = TestUtil.createKeyTemplateWithNonExistingTypeUrl();
    try {
        KeysetManager.withEmptyKeyset().rotate(bogus);
        fail("Expected GeneralSecurityException");
    } catch (GeneralSecurityException e) {
        TestUtil.assertExceptionContains(e, "No key manager found for key type");
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 25 with KeyTemplate

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

the class AesEaxKeyManagerTest method testNewKeyWithCorruptedFormat.

@Test
public void testNewKeyWithCorruptedFormat() throws Exception {
    ByteString serialized = ByteString.copyFrom(new byte[128]);
    KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesEaxKeyManager.TYPE_URL).setValue(serialized).build();
    AesEaxKeyManager keyManager = new AesEaxKeyManager();
    try {
        keyManager.newKey(serialized);
        fail("Corrupted format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    try {
        keyManager.newKeyData(keyTemplate.getValue());
        fail("Corrupted format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
}
Also used : ByteString(com.google.protobuf.ByteString) GeneralSecurityException(java.security.GeneralSecurityException) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Aggregations

KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)82 Test (org.junit.Test)79 GeneralSecurityException (java.security.GeneralSecurityException)18 ByteString (com.google.protobuf.ByteString)12 KeyData (com.google.crypto.tink.proto.KeyData)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 HashType (com.google.crypto.tink.proto.HashType)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 TreeSet (java.util.TreeSet)7 Keyset (com.google.crypto.tink.proto.Keyset)6 KeysetHandle (com.google.crypto.tink.KeysetHandle)5 DummyAead (com.google.crypto.tink.TestUtil.DummyAead)5 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)4 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)4 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)4 EcdsaKeyFormat (com.google.crypto.tink.proto.EcdsaKeyFormat)4 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)4 HmacKeyFormat (com.google.crypto.tink.proto.HmacKeyFormat)4 AesCtrHmacStreamingKeyFormat (com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat)3 AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)3