Search in sources :

Example 31 with KeyTemplate

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

the class AesGcmKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    AesGcmKeyFormat gcmKeyFormat = AesGcmKeyFormat.newBuilder().setKeySize(16).build();
    ByteString serialized = ByteString.copyFrom(gcmKeyFormat.toByteArray());
    KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesGcmKeyManager.TYPE_URL).setValue(serialized).build();
    AesGcmKeyManager keyManager = new AesGcmKeyManager();
    Set<String> keys = new TreeSet<String>();
    // Calls newKey multiple times and make sure that they generate different keys.
    int numTests = 27;
    for (int i = 0; i < numTests / 3; i++) {
        AesGcmKey key = (AesGcmKey) keyManager.newKey(gcmKeyFormat);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        key = (AesGcmKey) keyManager.newKey(serialized);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = AesGcmKey.parseFrom(keyData.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
    }
    assertEquals(numTests, keys.size());
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) ByteString(com.google.protobuf.ByteString) AesGcmKey(com.google.crypto.tink.proto.AesGcmKey) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 32 with KeyTemplate

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

the class AesGcmKeyManagerTest method testNewKeyWithCorruptedFormat.

@Test
public void testNewKeyWithCorruptedFormat() throws Exception {
    ByteString serialized = ByteString.copyFrom(new byte[128]);
    KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesGcmKeyManager.TYPE_URL).setValue(serialized).build();
    AesGcmKeyManager keyManager = new AesGcmKeyManager();
    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)

Example 33 with KeyTemplate

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

the class AeadKeyTemplatesTest method testAES256_CTR_HMAC_SHA256.

@Test
public void testAES256_CTR_HMAC_SHA256() throws Exception {
    KeyTemplate template = AeadKeyTemplates.AES256_CTR_HMAC_SHA256;
    assertEquals(AesCtrHmacAeadKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.parseFrom(template.getValue());
    assertTrue(format.hasAesCtrKeyFormat());
    assertTrue(format.getAesCtrKeyFormat().hasParams());
    assertEquals(32, format.getAesCtrKeyFormat().getKeySize());
    assertEquals(16, format.getAesCtrKeyFormat().getParams().getIvSize());
    assertTrue(format.hasHmacKeyFormat());
    assertTrue(format.getHmacKeyFormat().hasParams());
    assertEquals(32, format.getHmacKeyFormat().getKeySize());
    assertEquals(32, format.getHmacKeyFormat().getParams().getTagSize());
    assertEquals(HashType.SHA256, format.getHmacKeyFormat().getParams().getHash());
}
Also used : AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 34 with KeyTemplate

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

the class AeadKeyTemplatesTest method testCreateKmsAeadKeyTemplate.

@Test
public void testCreateKmsAeadKeyTemplate() throws Exception {
    // Intentionally using "weird" or invalid values for parameters,
    // to test that the function correctly puts them in the resulting template.
    String keyUri = "some example URI";
    KeyTemplate template = AeadKeyTemplates.createKmsAeadKeyTemplate(keyUri);
    assertEquals(KmsAeadKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    KmsAeadKeyFormat format = KmsAeadKeyFormat.parseFrom(template.getValue());
    assertEquals(keyUri, format.getKeyUri());
}
Also used : KmsAeadKeyFormat(com.google.crypto.tink.proto.KmsAeadKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 35 with KeyTemplate

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

the class AeadKeyTemplatesTest method testCHACHA20_POLY1305.

@Test
public void testCHACHA20_POLY1305() throws Exception {
    KeyTemplate template = AeadKeyTemplates.CHACHA20_POLY1305;
    assertEquals(ChaCha20Poly1305KeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    // Empty format.
    assertTrue(template.getValue().isEmpty());
}
Also used : 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