Search in sources :

Example 6 with SaltedSecretKey

use of org.syncany.crypto.SaltedSecretKey in project syncany by syncany.

the class CipherSessionTest method testCipherSessionWriteKeyReuseCountOfTwo.

@Test
public void testCipherSessionWriteKeyReuseCountOfTwo() throws Exception {
    SaltedSecretKey masterKey = createDummyMasterKey();
    CipherSession cipherSession = new CipherSession(masterKey, 999, 2);
    CipherSpec cipherSpecAes128 = CipherSpecs.getCipherSpec(CipherSpecs.AES_128_GCM);
    CipherSpec cipherSpecTwofish128 = CipherSpecs.getCipherSpec(CipherSpecs.TWOFISH_128_GCM);
    SaltedSecretKey writeSecretKey1Aes128 = cipherSession.getWriteSecretKey(cipherSpecAes128);
    SaltedSecretKey writeSecretKey2Aes128 = cipherSession.getWriteSecretKey(cipherSpecAes128);
    SaltedSecretKey writeSecretKey3Aes128 = cipherSession.getWriteSecretKey(cipherSpecAes128);
    SaltedSecretKey writeSecretKey1Twofish128 = cipherSession.getWriteSecretKey(cipherSpecTwofish128);
    SaltedSecretKey writeSecretKey2Twofish128 = cipherSession.getWriteSecretKey(cipherSpecTwofish128);
    SaltedSecretKey writeSecretKey3Twofish128 = cipherSession.getWriteSecretKey(cipherSpecTwofish128);
    assertEquals(writeSecretKey1Aes128, writeSecretKey2Aes128);
    assertNotSame(writeSecretKey1Aes128, writeSecretKey3Aes128);
    assertEquals(writeSecretKey1Twofish128, writeSecretKey2Twofish128);
    assertNotSame(writeSecretKey1Twofish128, writeSecretKey3Twofish128);
    assertNotSame(writeSecretKey1Aes128, writeSecretKey1Twofish128);
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) CipherSession(org.syncany.crypto.CipherSession) CipherSpec(org.syncany.crypto.CipherSpec) Test(org.junit.Test)

Example 7 with SaltedSecretKey

use of org.syncany.crypto.SaltedSecretKey in project syncany by syncany.

the class CipherSessionTest method testCipherSessionReadKeyCacheSizeOfThree.

@Test
public void testCipherSessionReadKeyCacheSizeOfThree() throws Exception {
    SaltedSecretKey masterKey = createDummyMasterKey();
    CipherSession cipherSession = new CipherSession(masterKey, 2, 999);
    CipherSpec cipherSpecAes128 = CipherSpecs.getCipherSpec(CipherSpecs.AES_128_GCM);
    byte[] readKeySalt1 = CipherUtil.createRandomArray(cipherSpecAes128.getKeySize());
    byte[] readKeySalt2 = CipherUtil.createRandomArray(cipherSpecAes128.getKeySize());
    byte[] readKeySalt3 = CipherUtil.createRandomArray(cipherSpecAes128.getKeySize());
    SaltedSecretKey readSecretKey1Aes128 = cipherSession.getReadSecretKey(cipherSpecAes128, readKeySalt1);
    SaltedSecretKey readSecretKey2Aes128 = cipherSession.getReadSecretKey(cipherSpecAes128, readKeySalt2);
    SaltedSecretKey readSecretKey3Aes128 = cipherSession.getReadSecretKey(cipherSpecAes128, readKeySalt3);
    assertNotSame(readSecretKey1Aes128, readSecretKey2Aes128);
    assertNotSame(readSecretKey1Aes128, readSecretKey3Aes128);
    assertNotSame(readSecretKey2Aes128, readSecretKey3Aes128);
// TODO [medium] This does NOT TEST the actual read cache. How to test this. The cache is completely hidden/private?!
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) CipherSession(org.syncany.crypto.CipherSession) CipherSpec(org.syncany.crypto.CipherSpec) Test(org.junit.Test)

Example 8 with SaltedSecretKey

use of org.syncany.crypto.SaltedSecretKey in project syncany by syncany.

the class CipherUtilTest method testIntegrityHeaderVersion.

@Test(expected = Exception.class)
public void testIntegrityHeaderVersion() throws Exception {
    SaltedSecretKey masterKey = createDummyMasterKey();
    byte[] originalPlaintext = TestFileUtil.createRandomArray(50);
    byte[] ciphertext = CipherUtil.encrypt(new ByteArrayInputStream(originalPlaintext), Arrays.asList(CipherSpecs.getCipherSpec(CipherSpecs.AES_128_GCM)), masterKey);
    // Alter header VERSION 
    ciphertext[4] = (byte) 0xff;
    byte[] plaintext = CipherUtil.decrypt(new ByteArrayInputStream(ciphertext), masterKey);
    System.out.println(StringUtil.toHex(originalPlaintext));
    System.out.println(StringUtil.toHex(plaintext));
    fail("TEST FAILED: Ciphertext was altered without exception.");
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 9 with SaltedSecretKey

use of org.syncany.crypto.SaltedSecretKey in project syncany by syncany.

the class CipherUtilTest method testIntegrityAesGcmCiphertext.

@Test(expected = CipherException.class)
public void testIntegrityAesGcmCiphertext() throws Exception {
    SaltedSecretKey masterKey = createDummyMasterKey();
    byte[] originalPlaintext = TestFileUtil.createRandomArray(50);
    byte[] ciphertext = CipherUtil.encrypt(new ByteArrayInputStream(originalPlaintext), Arrays.asList(CipherSpecs.getCipherSpec(CipherSpecs.AES_128_GCM)), masterKey);
    // Alter ciphertext (after header!); ciphertext starts after 75 bytes 
    ciphertext[80] = (byte) (ciphertext[80] ^ 0x01);
    ciphertext[81] = (byte) (ciphertext[81] ^ 0x02);
    ciphertext[82] = (byte) (ciphertext[82] ^ 0x03);
    CipherUtil.decrypt(new ByteArrayInputStream(ciphertext), masterKey);
    fail("TEST FAILED: Ciphertext was altered without exception.");
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 10 with SaltedSecretKey

use of org.syncany.crypto.SaltedSecretKey in project syncany by syncany.

the class CipherUtilTest method testIntegrityHeaderCipherIV.

@Test(expected = Exception.class)
public void testIntegrityHeaderCipherIV() throws Exception {
    SaltedSecretKey masterKey = createDummyMasterKey();
    byte[] originalPlaintext = TestFileUtil.createRandomArray(50);
    byte[] ciphertext = CipherUtil.encrypt(new ByteArrayInputStream(originalPlaintext), Arrays.asList(CipherSpecs.getCipherSpec(CipherSpecs.AES_128_GCM)), masterKey);
    // Alter header CIPHER SALT 		
    ciphertext[32] = (byte) 0xff;
    ciphertext[33] = (byte) 0xff;
    ciphertext[34] = (byte) 0xff;
    byte[] plaintext = CipherUtil.decrypt(new ByteArrayInputStream(ciphertext), masterKey);
    System.out.println(StringUtil.toHex(originalPlaintext));
    System.out.println(StringUtil.toHex(plaintext));
    fail("TEST FAILED: Ciphertext was altered without exception.");
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Aggregations

SaltedSecretKey (org.syncany.crypto.SaltedSecretKey)23 Test (org.junit.Test)12 ByteArrayInputStream (java.io.ByteArrayInputStream)8 File (java.io.File)3 ConfigTO (org.syncany.config.to.ConfigTO)3 CipherException (org.syncany.crypto.CipherException)3 CipherSession (org.syncany.crypto.CipherSession)3 CipherSpec (org.syncany.crypto.CipherSpec)3 Persister (org.simpleframework.xml.core.Persister)2 MasterTO (org.syncany.config.to.MasterTO)2 RepoTO (org.syncany.config.to.RepoTO)2 LocalTransferSettings (org.syncany.plugins.local.LocalTransferSettings)2 StorageException (org.syncany.plugins.transfer.StorageException)2 UnreliableLocalTransferSettings (org.syncany.plugins.unreliable_local.UnreliableLocalTransferSettings)2 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 Config (org.syncany.config.Config)1 UserConfig (org.syncany.config.UserConfig)1