Search in sources :

Example 1 with AesGcmKey

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

the class AesGcmKeyManager method getPrimitive.

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

Example 2 with AesGcmKey

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

the class AesGcmKeyManagerTest method testNewKeyMultipleTimes.

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

Aggregations

AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)2 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)1 KeyData (com.google.crypto.tink.proto.KeyData)1 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)1 AesGcmJce (com.google.crypto.tink.subtle.AesGcmJce)1 ByteString (com.google.protobuf.ByteString)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1