Search in sources :

Example 1 with AesCtrHmacAeadKeyFormat

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

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

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

the class AeadKeyTemplatesTest method testAES128_CTR_HMAC_SHA256.

@Test
public void testAES128_CTR_HMAC_SHA256() throws Exception {
    KeyTemplate template = AeadKeyTemplates.AES128_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(16, format.getAesCtrKeyFormat().getKeySize());
    assertEquals(16, format.getAesCtrKeyFormat().getParams().getIvSize());
    assertTrue(format.hasHmacKeyFormat());
    assertTrue(format.getHmacKeyFormat().hasParams());
    assertEquals(32, format.getHmacKeyFormat().getKeySize());
    assertEquals(16, 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 4 with AesCtrHmacAeadKeyFormat

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

the class AeadKeyTemplatesTest method testCreateAesCtrHmacAeadKeyTemplate.

@Test
public void testCreateAesCtrHmacAeadKeyTemplate() throws Exception {
    // Intentionally using "weird" or invalid values for parameters,
    // to test that the function correctly puts them in the resulting template.
    int aesKeySize = 42;
    int ivSize = 72;
    int hmacKeySize = 24;
    int tagSize = 27;
    HashType hashType = HashType.SHA224;
    KeyTemplate template = AeadKeyTemplates.createAesCtrHmacAeadKeyTemplate(aesKeySize, ivSize, hmacKeySize, tagSize, hashType);
    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(aesKeySize, format.getAesCtrKeyFormat().getKeySize());
    assertEquals(ivSize, format.getAesCtrKeyFormat().getParams().getIvSize());
    assertTrue(format.hasHmacKeyFormat());
    assertTrue(format.getHmacKeyFormat().hasParams());
    assertEquals(hmacKeySize, format.getHmacKeyFormat().getKeySize());
    assertEquals(tagSize, format.getHmacKeyFormat().getParams().getTagSize());
    assertEquals(hashType, format.getHmacKeyFormat().getParams().getHash());
}
Also used : HashType(com.google.crypto.tink.proto.HashType) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 5 with AesCtrHmacAeadKeyFormat

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

the class AesCtrHmacAeadKeyManager method newKey.

/**
 * @param keyFormat  {@code AesCtrHmacAeadKeyFormat} proto
 * @return new {@code AesCtrHmacAeadKey} proto
 */
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
    if (!(keyFormat instanceof AesCtrHmacAeadKeyFormat)) {
        throw new GeneralSecurityException("expected AesCtrHmacAeadKeyFormat proto");
    }
    AesCtrHmacAeadKeyFormat format = (AesCtrHmacAeadKeyFormat) keyFormat;
    AesCtrKey aesCtrKey = (AesCtrKey) Registry.newKey(AesCtrKeyManager.TYPE_URL, format.getAesCtrKeyFormat());
    HmacKey hmacKey = (HmacKey) Registry.newKey(MacConfig.HMAC_TYPE_URL, format.getHmacKeyFormat());
    return AesCtrHmacAeadKey.newBuilder().setAesCtrKey(aesCtrKey).setHmacKey(hmacKey).setVersion(VERSION).build();
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) GeneralSecurityException(java.security.GeneralSecurityException) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) HmacKey(com.google.crypto.tink.proto.HmacKey)

Aggregations

AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)6 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)4 Test (org.junit.Test)4 AesCtrHmacAeadKey (com.google.crypto.tink.proto.AesCtrHmacAeadKey)1 AesCtrKey (com.google.crypto.tink.proto.AesCtrKey)1 AesCtrKeyFormat (com.google.crypto.tink.proto.AesCtrKeyFormat)1 HashType (com.google.crypto.tink.proto.HashType)1 HmacKey (com.google.crypto.tink.proto.HmacKey)1 HmacKeyFormat (com.google.crypto.tink.proto.HmacKeyFormat)1 KeyData (com.google.crypto.tink.proto.KeyData)1 ByteString (com.google.protobuf.ByteString)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1