Search in sources :

Example 1 with DeterministicAead

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);
}
Also used : RegistryConfig(com.google.crypto.tink.proto.RegistryConfig) DeterministicAead(com.google.crypto.tink.DeterministicAead) KeyTypeEntry(com.google.crypto.tink.proto.KeyTypeEntry) KeyManager(com.google.crypto.tink.KeyManager) Test(org.junit.Test)

Example 2 with DeterministicAead

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.
        }
    }
}
Also used : DeterministicAead(com.google.crypto.tink.DeterministicAead) GeneralSecurityException(java.security.GeneralSecurityException) AEADBadTagException(javax.crypto.AEADBadTagException)

Example 3 with DeterministicAead

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));
        }
    }
}
Also used : DeterministicAead(com.google.crypto.tink.DeterministicAead) Test(org.junit.Test)

Example 4 with DeterministicAead

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));
        }
    }
}
Also used : DeterministicAead(com.google.crypto.tink.DeterministicAead) Test(org.junit.Test)

Example 5 with DeterministicAead

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);
    }
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) DeterministicAead(com.google.crypto.tink.DeterministicAead) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

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