Search in sources :

Example 1 with AesEaxKey

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

the class RegistryTest method testGetPrimitive_AesGcm_shouldWork.

@Test
public void testGetPrimitive_AesGcm_shouldWork() throws Exception {
    KeyTemplate template = AeadKeyTemplates.AES128_EAX;
    AesEaxKey aesEaxKey = (AesEaxKey) Registry.newKey(template);
    KeyData aesEaxKeyData = Registry.newKeyData(template);
    Aead aead = Registry.getPrimitive(aesEaxKeyData);
    assertThat(aesEaxKey.getKeyValue().size()).isEqualTo(16);
    assertThat(aesEaxKeyData.getTypeUrl()).isEqualTo(AeadConfig.AES_EAX_TYPE_URL);
    // This might break when we add native implementations.
    assertThat(aead.getClass()).isEqualTo(AesEaxJce.class);
}
Also used : AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) DummyAead(com.google.crypto.tink.TestUtil.DummyAead) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 2 with AesEaxKey

use of com.google.crypto.tink.proto.AesEaxKey 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 3 with AesEaxKey

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

the class AesEaxKeyManager method getPrimitive.

/**
 * @param key {@code AesEaxKey} proto
 */
@Override
public Aead getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof AesEaxKey)) {
        throw new GeneralSecurityException("expected AesEaxKey proto");
    }
    AesEaxKey keyProto = (AesEaxKey) key;
    validate(keyProto);
    return new AesEaxJce(keyProto.getKeyValue().toByteArray(), keyProto.getParams().getIvSize());
}
Also used : AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) GeneralSecurityException(java.security.GeneralSecurityException) AesEaxJce(com.google.crypto.tink.subtle.AesEaxJce)

Aggregations

AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)3 KeyData (com.google.crypto.tink.proto.KeyData)2 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)2 Test (org.junit.Test)2 DummyAead (com.google.crypto.tink.TestUtil.DummyAead)1 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)1 AesEaxJce (com.google.crypto.tink.subtle.AesEaxJce)1 ByteString (com.google.protobuf.ByteString)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1