Search in sources :

Example 26 with KeyTemplate

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

the class AesEaxKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    AesEaxKeyFormat eaxKeyFormat = AesEaxKeyFormat.newBuilder().setParams(AesEaxParams.newBuilder().setIvSize(16).build()).setKeySize(16).build();
    ByteString serialized = ByteString.copyFrom(eaxKeyFormat.toByteArray());
    KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesEaxKeyManager.TYPE_URL).setValue(serialized).build();
    AesEaxKeyManager keyManager = new AesEaxKeyManager();
    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++) {
        AesEaxKey key = (AesEaxKey) keyManager.newKey(eaxKeyFormat);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        key = (AesEaxKey) keyManager.newKey(serialized);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = AesEaxKey.parseFrom(keyData.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
    }
    assertEquals(numTests, keys.size());
}
Also used : AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 27 with KeyTemplate

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

the class AesCtrHmacAeadKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    KeyTemplate keyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
    AesCtrHmacAeadKeyFormat aeadKeyFormat = AesCtrHmacAeadKeyFormat.parseFrom(keyTemplate.getValue().toByteArray());
    ByteString serialized = ByteString.copyFrom(aeadKeyFormat.toByteArray());
    AesCtrHmacAeadKeyManager keyManager = new AesCtrHmacAeadKeyManager();
    Set<String> keys = new TreeSet<String>();
    // Calls newKey multiple times and make sure that they generate different keys.
    int numTests = 24;
    for (int i = 0; i < numTests / 6; i++) {
        AesCtrHmacAeadKey key = (AesCtrHmacAeadKey) keyManager.newKey(aeadKeyFormat);
        keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
        keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
        assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
        assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
        key = (AesCtrHmacAeadKey) keyManager.newKey(serialized);
        keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
        keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
        assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
        assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = AesCtrHmacAeadKey.parseFrom(keyData.getValue());
        keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
        keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
        assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
        assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
    }
    assertEquals(numTests, keys.size());
}
Also used : ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) ByteString(com.google.protobuf.ByteString) AesCtrHmacAeadKey(com.google.crypto.tink.proto.AesCtrHmacAeadKey) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 28 with KeyTemplate

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

the class AesCtrHmacAeadKeyManagerTest method testNewKeyWithCorruptedFormat.

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

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

the class AesCtrKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    AesCtrKeyFormat ctrKeyFormat = AesCtrKeyFormat.newBuilder().setParams(AesCtrParams.newBuilder().setIvSize(16).build()).setKeySize(16).build();
    ByteString serialized = ByteString.copyFrom(ctrKeyFormat.toByteArray());
    KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesCtrKeyManager.TYPE_URL).setValue(serialized).build();
    AesCtrKeyManager keyManager = new AesCtrKeyManager();
    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++) {
        AesCtrKey key = (AesCtrKey) keyManager.newKey(ctrKeyFormat);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        key = (AesCtrKey) keyManager.newKey(serialized);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = AesCtrKey.parseFrom(keyData.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
    }
    assertEquals(numTests, keys.size());
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) AesCtrKeyFormat(com.google.crypto.tink.proto.AesCtrKeyFormat) ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 30 with KeyTemplate

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

the class AesCtrKeyManagerTest method testNewKeyWithCorruptedFormat.

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