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