Search in sources :

Example 16 with SaltedSecretKey

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

the class CipherUtilMasterKeyCreationTest method testCreateMasterKeyWithSalt.

@Test
public void testCreateMasterKeyWithSalt() throws CipherException {
    long timeStart = System.currentTimeMillis();
    SaltedSecretKey masterKeyForPasswordTestAndSalt123 = CipherUtil.createMasterKey("Test", new byte[] { 1, 2, 3 });
    long timeEnd = System.currentTimeMillis();
    long timeDuration = timeEnd - timeStart;
    logger.log(Level.INFO, "Creating master key took " + timeDuration + "ms:");
    logger.log(Level.INFO, " - Key:  " + StringUtil.toHex(masterKeyForPasswordTestAndSalt123.getEncoded()));
    logger.log(Level.INFO, " - Salt: " + StringUtil.toHex(masterKeyForPasswordTestAndSalt123.getSalt()));
    assertEquals("010203", StringUtil.toHex(masterKeyForPasswordTestAndSalt123.getSalt()));
    assertEquals("44fda24d53b29828b62c362529bd9df5c8a92c2736bcae3a28b3d7b44488e36e246106aa5334813028abb2048eeb5e177df1c702d93cf82aeb7b6d59a8534ff0", StringUtil.toHex(masterKeyForPasswordTestAndSalt123.getEncoded()));
    assertEquals(CipherParams.MASTER_KEY_SIZE / 8, masterKeyForPasswordTestAndSalt123.getEncoded().length);
    assertEquals("PBKDF2WithHmacSHA1", masterKeyForPasswordTestAndSalt123.getAlgorithm());
    assertEquals("RAW", masterKeyForPasswordTestAndSalt123.getFormat());
    assertTrue(timeDuration > 5000);
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) Test(org.junit.Test)

Example 17 with SaltedSecretKey

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

the class CipherUtilMasterKeyCreationTest method testCreateMasterKeyNoSalt.

@Test
public void testCreateMasterKeyNoSalt() throws CipherException {
    SaltedSecretKey masterKeyForPasswordTestNoSalt1 = CipherUtil.createMasterKey("Test");
    SaltedSecretKey masterKeyForPasswordTestNoSalt2 = CipherUtil.createMasterKey("Test");
    logger.log(Level.INFO, "Key comparison for password 'Test':");
    logger.log(Level.INFO, "- Master key 1: " + StringUtil.toHex(masterKeyForPasswordTestNoSalt1.getEncoded()));
    logger.log(Level.INFO, "     with salt: " + StringUtil.toHex(masterKeyForPasswordTestNoSalt1.getSalt()));
    logger.log(Level.INFO, "- Master key 2: " + StringUtil.toHex(masterKeyForPasswordTestNoSalt2.getEncoded()));
    logger.log(Level.INFO, "     with salt: " + StringUtil.toHex(masterKeyForPasswordTestNoSalt2.getSalt()));
    assertFalse(Arrays.equals(masterKeyForPasswordTestNoSalt1.getSalt(), masterKeyForPasswordTestNoSalt2.getSalt()));
    assertFalse(Arrays.equals(masterKeyForPasswordTestNoSalt1.getEncoded(), masterKeyForPasswordTestNoSalt2.getEncoded()));
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) Test(org.junit.Test)

Example 18 with SaltedSecretKey

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

the class CipherUtilTest method testIntegrityTwofishGcmCiphertext.

@Test(expected = Exception.class)
public void testIntegrityTwofishGcmCiphertext() throws Exception {
    SaltedSecretKey masterKey = createDummyMasterKey();
    byte[] originalPlaintext = TestFileUtil.createRandomArray(50);
    byte[] ciphertext = CipherUtil.encrypt(new ByteArrayInputStream(originalPlaintext), Arrays.asList(CipherSpecs.getCipherSpec(CipherSpecs.TWOFISH_128_GCM)), masterKey);
    // Alter ciphertext (after header!); ciphertext starts after 75 bytes 
    ciphertext[80] = (byte) (ciphertext[80] ^ 0x01);
    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 19 with SaltedSecretKey

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

the class CipherUtilTest method testIntegrityHeaderMagic.

@Test(expected = Exception.class)
public void testIntegrityHeaderMagic() 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 MAGIC BYTES 
    ciphertext[0] = 0x12;
    ciphertext[1] = 0x34;
    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 20 with SaltedSecretKey

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

the class CipherUtilTest method testIntegrityHeaderCipherSpecId.

@Test(expected = Exception.class)
public void testIntegrityHeaderCipherSpecId() 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);
    // If this fails, fix test!
    assertEquals(CipherSpecs.AES_128_GCM, ciphertext[18]);
    // Alter header CIPHER SPEC ID 		
    ciphertext[18] = (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