Search in sources :

Example 1 with AesEaxKeyFormat

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

the class AesEaxKeyManager method newKey.

/**
 * @param keyFormat {@code AesEaxKeyFormat} proto
 * @return new {@code AesEaxKey} proto
 */
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
    if (!(keyFormat instanceof AesEaxKeyFormat)) {
        throw new GeneralSecurityException("expected AesEaxKeyFormat proto");
    }
    AesEaxKeyFormat format = (AesEaxKeyFormat) keyFormat;
    validate(format);
    return AesEaxKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).setParams(format.getParams()).setVersion(VERSION).build();
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat)

Example 2 with AesEaxKeyFormat

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

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

the class AeadKeyTemplatesTest method testAES128_EAX.

@Test
public void testAES128_EAX() throws Exception {
    KeyTemplate template = AeadKeyTemplates.AES128_EAX;
    assertEquals(AesEaxKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    AesEaxKeyFormat format = AesEaxKeyFormat.parseFrom(template.getValue());
    assertEquals(16, format.getKeySize());
    assertTrue(format.hasParams());
    assertEquals(16, format.getParams().getIvSize());
}
Also used : AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 4 with AesEaxKeyFormat

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

the class AeadKeyTemplatesTest method testCreateAesEaxKeyTemplate.

@Test
public void testCreateAesEaxKeyTemplate() throws Exception {
    // Intentionally using "weird" or invalid values for parameters,
    // to test that the function correctly puts them in the resulting template.
    int keySize = 42;
    int ivSize = 72;
    KeyTemplate template = AeadKeyTemplates.createAesEaxKeyTemplate(keySize, ivSize);
    assertEquals(AesEaxKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    AesEaxKeyFormat format = AesEaxKeyFormat.parseFrom(template.getValue());
    assertEquals(keySize, format.getKeySize());
    assertTrue(format.hasParams());
    assertEquals(ivSize, format.getParams().getIvSize());
}
Also used : AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 5 with AesEaxKeyFormat

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

the class AeadKeyTemplatesTest method testAES256_EAX.

@Test
public void testAES256_EAX() throws Exception {
    KeyTemplate template = AeadKeyTemplates.AES256_EAX;
    assertEquals(AesEaxKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    AesEaxKeyFormat format = AesEaxKeyFormat.parseFrom(template.getValue());
    assertEquals(32, format.getKeySize());
    assertTrue(format.hasParams());
    assertEquals(16, format.getParams().getIvSize());
}
Also used : AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Aggregations

AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)5 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)4 Test (org.junit.Test)4 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)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