Search in sources :

Example 6 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class ChaCha20Poly1305Test method testModifyCiphertext.

@Test
public void testModifyCiphertext() throws Exception {
    Assume.assumeFalse(TinkFips.useOnlyFips());
    byte[] key = Random.randBytes(KEY_SIZE);
    Aead aead = createInstance(key);
    byte[] aad = Random.randBytes(16);
    byte[] message = Random.randBytes(32);
    byte[] ciphertext = aead.encrypt(message, aad);
    for (BytesMutation mutation : TestUtil.generateMutations(ciphertext)) {
        assertThrows(String.format("Decrypting modified ciphertext should fail : ciphertext = %s, aad = %s," + " description = %s", Hex.encode(mutation.value), Arrays.toString(aad), mutation.description), GeneralSecurityException.class, () -> {
            byte[] unused = aead.decrypt(mutation.value, aad);
        });
    }
    // Modify 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);
            assertThrows(AEADBadTagException.class, () -> {
                byte[] unused = aead.decrypt(ciphertext, modified);
            });
        }
    }
}
Also used : Aead(com.google.crypto.tink.Aead) BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) Test(org.junit.Test)

Example 7 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class AesGcmJceTest method testModifyCiphertext.

@Test
public void testModifyCiphertext() throws Exception {
    Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
    byte[] aad = generateAad();
    byte[] key = Random.randBytes(16);
    byte[] message = Random.randBytes(32);
    AesGcmJce gcm = new AesGcmJce(key);
    byte[] ciphertext = gcm.encrypt(message, aad);
    for (BytesMutation mutation : TestUtil.generateMutations(ciphertext)) {
        assertThrows(String.format("Decrypting modified ciphertext should fail : ciphertext = %s, aad = %s," + " description = %s", Hex.encode(mutation.value), Hex.encode(aad), mutation.description), GeneralSecurityException.class, () -> {
            byte[] unused = gcm.decrypt(mutation.value, aad);
        });
    }
    // Modify AAD
    if (aad != null && aad.length != 0) {
        for (BytesMutation mutation : TestUtil.generateMutations(aad)) {
            assertThrows(String.format("Decrypting with modified aad should fail: ciphertext = %s, aad = %s," + " description = %s", Arrays.toString(ciphertext), Arrays.toString(mutation.value), mutation.description), GeneralSecurityException.class, () -> {
                byte[] unused = gcm.decrypt(ciphertext, mutation.value);
            });
        }
    }
}
Also used : BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) Test(org.junit.Test)

Example 8 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class InsecureNonceAesGcmJceTest method testModifyCiphertext.

@Test
public void testModifyCiphertext() throws Exception {
    Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
    byte[] aad = generateAad();
    byte[] key = Random.randBytes(16);
    byte[] message = Random.randBytes(32);
    InsecureNonceAesGcmJce gcm = new InsecureNonceAesGcmJce(key, /*prependIv=*/
    false);
    byte[] iv = Random.randBytes(InsecureNonceAesGcmJce.IV_SIZE_IN_BYTES);
    byte[] ciphertext = gcm.encrypt(iv, message, aad);
    for (BytesMutation mutation : TestUtil.generateMutations(ciphertext)) {
        assertThrows(String.format("Decrypting modified ciphertext should fail : ciphertext = %s, aad = %s," + " description = %s", Hex.encode(mutation.value), Hex.encode(aad), mutation.description), GeneralSecurityException.class, () -> {
            byte[] unused = gcm.decrypt(iv, mutation.value, aad);
        });
    }
    // Modify AAD
    if (aad != null && aad.length != 0) {
        for (BytesMutation mutation : TestUtil.generateMutations(aad)) {
            assertThrows(String.format("Decrypting with modified aad should fail: ciphertext = %s, aad = %s," + " description = %s", Arrays.toString(ciphertext), Arrays.toString(mutation.value), mutation.description), GeneralSecurityException.class, () -> {
                byte[] unused = gcm.decrypt(iv, ciphertext, mutation.value);
            });
        }
    }
}
Also used : BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) Test(org.junit.Test)

Example 9 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class AesGcmSivTest method testModifyCiphertext.

@Test
public void testModifyCiphertext() throws Exception {
    byte[] aad = Random.randBytes(33);
    byte[] key = Random.randBytes(16);
    byte[] message = Random.randBytes(32);
    AesGcmSiv gcm = new AesGcmSiv(key);
    byte[] ciphertext = gcm.encrypt(message, aad);
    for (BytesMutation mutation : TestUtil.generateMutations(ciphertext)) {
        assertThrows(String.format("Decrypting modified ciphertext should fail : ciphertext = %s, aad = %s," + " description = %s", Hex.encode(mutation.value), Hex.encode(aad), mutation.description), GeneralSecurityException.class, () -> {
            byte[] unused = gcm.decrypt(mutation.value, aad);
        });
    }
    // Modify AAD
    for (BytesMutation mutation : TestUtil.generateMutations(aad)) {
        assertThrows(String.format("Decrypting with modified aad should fail: ciphertext = %s, aad = %s," + " description = %s", Arrays.toString(ciphertext), Arrays.toString(mutation.value), mutation.description), GeneralSecurityException.class, () -> {
            byte[] unused = gcm.decrypt(ciphertext, mutation.value);
        });
    }
}
Also used : BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) Test(org.junit.Test)

Aggregations

BytesMutation (com.google.crypto.tink.testing.TestUtil.BytesMutation)9 Test (org.junit.Test)7 KeyPair (java.security.KeyPair)3 ECPrivateKey (java.security.interfaces.ECPrivateKey)3 ECPublicKey (java.security.interfaces.ECPublicKey)3 HybridDecrypt (com.google.crypto.tink.HybridDecrypt)2 HybridEncrypt (com.google.crypto.tink.HybridEncrypt)2 EciesAeadHkdfHybridDecrypt (com.google.crypto.tink.subtle.EciesAeadHkdfHybridDecrypt)2 EciesAeadHkdfHybridEncrypt (com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)2 Aead (com.google.crypto.tink.Aead)1 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)1 EcdsaEncoding (com.google.crypto.tink.subtle.EllipticCurves.EcdsaEncoding)1 KeyPairGenerator (java.security.KeyPairGenerator)1 ECParameterSpec (java.security.spec.ECParameterSpec)1 ECPoint (java.security.spec.ECPoint)1