Search in sources :

Example 1 with AesCtrHmacAeadKey

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

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

the class AesCtrHmacAeadKeyManager method getPrimitive.

/**
 * @param key  {@code AesCtrHmacAeadKey} proto
 */
@Override
public Aead getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof AesCtrHmacAeadKey)) {
        throw new GeneralSecurityException("expected AesCtrHmacAeadKey proto");
    }
    AesCtrHmacAeadKey keyProto = (AesCtrHmacAeadKey) key;
    validate(keyProto);
    return new EncryptThenAuthenticate((IndCpaCipher) Registry.getPrimitive(AesCtrKeyManager.TYPE_URL, keyProto.getAesCtrKey()), (Mac) Registry.getPrimitive(MacConfig.HMAC_TYPE_URL, keyProto.getHmacKey()), keyProto.getHmacKey().getParams().getTagSize());
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) EncryptThenAuthenticate(com.google.crypto.tink.subtle.EncryptThenAuthenticate) AesCtrHmacAeadKey(com.google.crypto.tink.proto.AesCtrHmacAeadKey)

Example 3 with AesCtrHmacAeadKey

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

the class TestUtil method createAesCtrHmacAeadKeyData.

/**
 * @return a {@code KeyData} containing a {@code AesCtrHmacAeadKey}.
 */
public static KeyData createAesCtrHmacAeadKeyData(byte[] aesCtrKeyValue, int ivSize, byte[] hmacKeyValue, int tagSize) throws Exception {
    AesCtrKey aesCtrKey = createAesCtrKey(aesCtrKeyValue, ivSize);
    HmacKey hmacKey = createHmacKey(hmacKeyValue, tagSize);
    AesCtrHmacAeadKey keyProto = AesCtrHmacAeadKey.newBuilder().setAesCtrKey(aesCtrKey).setHmacKey(hmacKey).build();
    return createKeyData(keyProto, AeadConfig.AES_CTR_HMAC_AEAD_TYPE_URL, KeyData.KeyMaterialType.SYMMETRIC);
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) HmacKey(com.google.crypto.tink.proto.HmacKey) AesCtrHmacAeadKey(com.google.crypto.tink.proto.AesCtrHmacAeadKey)

Aggregations

AesCtrHmacAeadKey (com.google.crypto.tink.proto.AesCtrHmacAeadKey)3 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)1 AesCtrKey (com.google.crypto.tink.proto.AesCtrKey)1 HmacKey (com.google.crypto.tink.proto.HmacKey)1 KeyData (com.google.crypto.tink.proto.KeyData)1 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)1 EncryptThenAuthenticate (com.google.crypto.tink.subtle.EncryptThenAuthenticate)1 ByteString (com.google.protobuf.ByteString)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1