use of com.google.crypto.tink.DeterministicAead in project tink by google.
the class DeterministicAeadCatalogueTest method testBasic.
@Test
public void testBasic() throws Exception {
DeterministicAeadCatalogue catalogue = new DeterministicAeadCatalogue();
// Check a single key type, incl. case-insensitve primitive name.
String keyType = "type.googleapis.com/google.crypto.tink.AesSivKey";
{
KeyManager<DeterministicAead> manager = catalogue.getKeyManager(keyType, "DeterministicAead", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
{
KeyManager<DeterministicAead> manager = catalogue.getKeyManager(keyType, "DeterministicAEaD", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
{
KeyManager<DeterministicAead> manager = catalogue.getKeyManager(keyType, "dEtermInisticaeAD", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
// Check all entries from the current DeterministicAeadConfig.
RegistryConfig config = DeterministicAeadConfig.TINK_1_1_0;
int count = 0;
for (KeyTypeEntry entry : config.getEntryList()) {
if ("DeterministicAead".equals(entry.getPrimitiveName())) {
count = count + 1;
KeyManager<DeterministicAead> manager = catalogue.getKeyManager(entry.getTypeUrl(), "deterministicaead", entry.getKeyManagerVersion());
assertThat(manager.doesSupport(entry.getTypeUrl())).isTrue();
}
}
assertEquals(1, count);
}
use of com.google.crypto.tink.DeterministicAead in project tink by google.
the class AesSivTest method testModifiedCiphertext.
private static void testModifiedCiphertext(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 ciphertext.
for (int b = 0; b < ciphertext.length; b++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modified = Arrays.copyOf(ciphertext, ciphertext.length);
modified[b] ^= (byte) (1 << bit);
try {
byte[] unused = crypter.decryptDeterministically(modified, aad);
fail("Decrypting modified ciphertext should fail");
} catch (AEADBadTagException ex) {
// This is expected.
}
}
}
// Truncate the message.
for (int length = 0; length < ciphertext.length; length++) {
byte[] modified = Arrays.copyOf(ciphertext, length);
try {
byte[] unused = crypter.decryptDeterministically(modified, aad);
fail("Decrypting modified ciphertext should fail");
} catch (GeneralSecurityException ex) {
// This is expected.
// This could be a AeadBadTagException when the tag verification
// fails or some not yet specified Exception when the ciphertext is too short.
// In all cases a GeneralSecurityException or a subclass of it must be thrown.
}
}
}
use of com.google.crypto.tink.DeterministicAead in project tink by google.
the class AesSivTest method testEncryptDecryptWithEmptyPlaintextAndEmptyAssociatedData.
@Test
public void testEncryptDecryptWithEmptyPlaintextAndEmptyAssociatedData() throws GeneralSecurityException {
for (int keySize : keySizeInBytes) {
DeterministicAead dead = new AesSiv(Random.randBytes(keySize));
for (int triesPlaintext = 0; triesPlaintext < 100; triesPlaintext++) {
byte[] plaintext = new byte[0];
byte[] aad = new byte[0];
byte[] rebuiltPlaintext = dead.decryptDeterministically(dead.encryptDeterministically(plaintext, aad), aad);
assertEquals(Hex.encode(plaintext), Hex.encode(rebuiltPlaintext));
}
}
}
use of com.google.crypto.tink.DeterministicAead in project tink by google.
the class AesSivTest method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws GeneralSecurityException {
for (int keySize : keySizeInBytes) {
DeterministicAead dead = new AesSiv(Random.randBytes(keySize));
for (int triesPlaintext = 0; triesPlaintext < 100; triesPlaintext++) {
byte[] plaintext = Random.randBytes(Random.randInt(1024) + 1);
byte[] aad = Random.randBytes(Random.randInt(128) + 1);
byte[] rebuiltPlaintext = dead.decryptDeterministically(dead.encryptDeterministically(plaintext, aad), aad);
assertEquals(Hex.encode(plaintext), Hex.encode(rebuiltPlaintext));
}
}
}
use of com.google.crypto.tink.DeterministicAead in project tink by google.
the class AesSivKeyManagerTest method testCiphertextSize.
@Test
public void testCiphertextSize() throws Exception {
for (KeyTemplate template : keyTemplates) {
KeysetHandle keysetHandle = KeysetHandle.generateNew(template);
DeterministicAead daead = DeterministicAeadFactory.getPrimitive(keysetHandle);
byte[] plaintext = "plaintext".getBytes("UTF-8");
byte[] associatedData = "associatedData".getBytes("UTF-8");
byte[] ciphertext = daead.encryptDeterministically(plaintext, associatedData);
assertEquals(CryptoFormat.NON_RAW_PREFIX_SIZE + plaintext.length + 16, /* IV_SIZE */
ciphertext.length);
}
}
Aggregations