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);
});
}
}
}
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);
});
}
}
}
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);
});
}
}
}
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);
});
}
}
Aggregations