Search in sources :

Example 21 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class KmsEnvelopeAead method encrypt.

@Override
public byte[] encrypt(final byte[] plaintext, final byte[] associatedData) throws GeneralSecurityException {
    // Generate a new DEK.
    byte[] dek = Registry.newKey(dekTemplate).toByteArray();
    // Wrap it with remote.
    byte[] encryptedDek = remote.encrypt(dek, EMPTY_AAD);
    // Use DEK to encrypt plaintext.
    Aead aead = Registry.getPrimitive(dekTemplate.getTypeUrl(), dek, Aead.class);
    byte[] payload = aead.encrypt(plaintext, associatedData);
    // Build ciphertext protobuf and return result.
    return buildCiphertext(encryptedDek, payload);
}
Also used : Aead(com.google.crypto.tink.Aead)

Example 22 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class KmsEnvelopeAead method decrypt.

@Override
public byte[] decrypt(final byte[] ciphertext, final byte[] associatedData) throws GeneralSecurityException {
    try {
        ByteBuffer buffer = ByteBuffer.wrap(ciphertext);
        int encryptedDekSize = buffer.getInt();
        if (encryptedDekSize <= 0 || encryptedDekSize > (ciphertext.length - LENGTH_ENCRYPTED_DEK)) {
            throw new GeneralSecurityException("invalid ciphertext");
        }
        byte[] encryptedDek = new byte[encryptedDekSize];
        buffer.get(encryptedDek, 0, encryptedDekSize);
        byte[] payload = new byte[buffer.remaining()];
        buffer.get(payload, 0, buffer.remaining());
        // Use remote to decrypt encryptedDek.
        byte[] dek = remote.decrypt(encryptedDek, EMPTY_AAD);
        // Use DEK to decrypt payload.
        Aead aead = Registry.getPrimitive(dekTemplate.getTypeUrl(), dek, Aead.class);
        return aead.decrypt(payload, associatedData);
    } catch (IndexOutOfBoundsException | BufferUnderflowException | NegativeArraySizeException e) {
        throw new GeneralSecurityException("invalid ciphertext", e);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Aead(com.google.crypto.tink.Aead) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 23 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class AeadIntegrationTest method testBasicAesCtrHmacAead.

@Test
public void testBasicAesCtrHmacAead() throws Exception {
    byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE);
    byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE);
    int ivSize = 12;
    int tagSize = 16;
    KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK)));
    Aead aead = keysetHandle.getPrimitive(Aead.class);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    byte[] decrypted = aead.decrypt(ciphertext, associatedData);
    assertArrayEquals(plaintext, decrypted);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) Test(org.junit.Test)

Example 24 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class AeadIntegrationTest method testRawKeyAsPrimary.

@Test
public void testRawKeyAsPrimary() throws Exception {
    byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE);
    byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE);
    int ivSize = 12;
    int tagSize = 16;
    Key primary = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key raw = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key legacy = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY);
    KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, raw, legacy));
    Aead aead = keysetHandle.getPrimitive(Aead.class);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData));
    assertEquals(CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + ivSize + tagSize, ciphertext.length);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) Key(com.google.crypto.tink.proto.Keyset.Key) Test(org.junit.Test)

Example 25 with Aead

use of com.google.crypto.tink.Aead in project tink by google.

the class AeadWrapperTest method testMultipleKeys.

@Test
public void testMultipleKeys() throws Exception {
    byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE);
    byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE);
    int ivSize = 12;
    int tagSize = 16;
    Key primary = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK);
    Key raw = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key legacy = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY);
    Key tink = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 45, KeyStatusType.ENABLED, OutputPrefixType.TINK);
    Aead aead = new AeadWrapper().wrap(TestUtil.createPrimitiveSet(TestUtil.createKeyset(primary, raw, legacy, tink), Aead.class));
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    byte[] prefix = Arrays.copyOfRange(ciphertext, 0, CryptoFormat.NON_RAW_PREFIX_SIZE);
    assertArrayEquals(prefix, CryptoFormat.getOutputPrefix(primary));
    assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData));
    assertEquals(CryptoFormat.NON_RAW_PREFIX_SIZE + plaintext.length + ivSize + tagSize, ciphertext.length);
    // encrypt with a non-primary RAW key and decrypt with the keyset
    Aead aead2 = new AeadWrapper().wrap(TestUtil.createPrimitiveSet(TestUtil.createKeyset(raw, legacy, tink), Aead.class));
    ciphertext = aead2.encrypt(plaintext, associatedData);
    assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData));
    // encrypt with a random key not in the keyset, decrypt with the keyset should fail
    byte[] aesCtrKeyValue2 = Random.randBytes(AES_KEY_SIZE);
    byte[] hmacKeyValue2 = Random.randBytes(HMAC_KEY_SIZE);
    Key random = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue2, ivSize, hmacKeyValue2, tagSize), 44, KeyStatusType.ENABLED, OutputPrefixType.TINK);
    aead2 = new AeadWrapper().wrap(TestUtil.createPrimitiveSet(TestUtil.createKeyset(random), Aead.class));
    final byte[] ciphertext2 = aead2.encrypt(plaintext, associatedData);
    GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> aead.decrypt(ciphertext2, associatedData));
    assertExceptionContains(e, "decryption failed");
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Aead(com.google.crypto.tink.Aead) Key(com.google.crypto.tink.proto.Keyset.Key) Test(org.junit.Test)

Aggregations

Aead (com.google.crypto.tink.Aead)84 Test (org.junit.Test)67 GeneralSecurityException (java.security.GeneralSecurityException)25 KeysetHandle (com.google.crypto.tink.KeysetHandle)21 Key (com.google.crypto.tink.proto.Keyset.Key)9 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)7 IOException (java.io.IOException)7 EncryptRequest (com.amazonaws.services.kms.model.EncryptRequest)6 KeyTemplate (com.google.crypto.tink.KeyTemplate)6 ByteString (com.google.protobuf.ByteString)6 DecryptRequest (com.amazonaws.services.kms.model.DecryptRequest)5 EncryptResult (com.amazonaws.services.kms.model.EncryptResult)5 KmsEnvelopeAeadKey (com.google.crypto.tink.proto.KmsEnvelopeAeadKey)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 DecryptResult (com.amazonaws.services.kms.model.DecryptResult)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBuffer (java.nio.ByteBuffer)4 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)3 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)3