Search in sources :

Example 11 with DeterministicAead

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

the class AesSivTest method testModifiedAssociatedData.

private static void testModifiedAssociatedData(int keySize) throws GeneralSecurityException {
    byte[] key = Random.randBytes(keySize);
    DeterministicAead crypter = new AesSiv(key);
    byte[] plaintext = Random.randBytes(10);
    byte[] aad = Random.randBytes(10);
    byte[] ciphertext = crypter.encryptDeterministically(plaintext, aad);
    // Flipping bits of aad.
    for (int b = 0; b < aad.length; b++) {
        for (int bit = 0; bit < 8; bit++) {
            byte[] modified = Arrays.copyOf(aad, aad.length);
            modified[b] ^= (byte) (1 << bit);
            try {
                byte[] unused = crypter.decryptDeterministically(ciphertext, modified);
                fail("Decrypting modified aad should fail");
            } catch (AEADBadTagException ex) {
            // This is expected.
            }
        }
    }
}
Also used : DeterministicAead(com.google.crypto.tink.DeterministicAead) AEADBadTagException(javax.crypto.AEADBadTagException)

Example 12 with DeterministicAead

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

the class DeterministicAeadFactoryTest method testEncrytDecrypt.

@Test
public void testEncrytDecrypt() throws Exception {
    KeysetHandle keysetHandle = KeysetHandle.generateNew(DeterministicAeadKeyTemplates.AES256_SIV);
    DeterministicAead aead = DeterministicAeadFactory.getPrimitive(keysetHandle);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    byte[] ciphertext = aead.encryptDeterministically(plaintext, associatedData);
    byte[] ciphertext2 = aead.encryptDeterministically(plaintext, associatedData);
    byte[] decrypted = aead.decryptDeterministically(ciphertext, associatedData);
    byte[] decrypted2 = aead.decryptDeterministically(ciphertext2, associatedData);
    assertArrayEquals(ciphertext, ciphertext2);
    assertArrayEquals(plaintext, decrypted);
    assertArrayEquals(plaintext, decrypted2);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) DeterministicAead(com.google.crypto.tink.DeterministicAead) Test(org.junit.Test)

Example 13 with DeterministicAead

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

the class DeterministicAeadFactoryTest method testSmallPlaintextWithRawKey.

private static void testSmallPlaintextWithRawKey(int keySize) throws Exception {
    Key primary = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary));
    DeterministicAead daead = DeterministicAeadFactory.getPrimitive(keysetHandle);
    byte[] plaintext = Random.randBytes(1);
    byte[] associatedData = Random.randBytes(20);
    byte[] ciphertext = daead.encryptDeterministically(plaintext, associatedData);
    assertArrayEquals(plaintext, daead.decryptDeterministically(ciphertext, associatedData));
    assertEquals(CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + 16, ciphertext.length);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) DeterministicAead(com.google.crypto.tink.DeterministicAead) Key(com.google.crypto.tink.proto.Keyset.Key)

Aggregations

DeterministicAead (com.google.crypto.tink.DeterministicAead)13 Test (org.junit.Test)8 KeysetHandle (com.google.crypto.tink.KeysetHandle)5 Key (com.google.crypto.tink.proto.Keyset.Key)3 GeneralSecurityException (java.security.GeneralSecurityException)3 AEADBadTagException (javax.crypto.AEADBadTagException)2 KeyManager (com.google.crypto.tink.KeyManager)1 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)1 KeyTypeEntry (com.google.crypto.tink.proto.KeyTypeEntry)1 RegistryConfig (com.google.crypto.tink.proto.RegistryConfig)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1