diff --git a/assets/twofish.kdb b/assets/twofish.kdb new file mode 100644 index 000000000..fb08f0466 Binary files /dev/null and b/assets/twofish.kdb differ diff --git a/res/values/config.xml b/res/values/config.xml index 003d430ab..e04c29fd7 100644 --- a/res/values/config.xml +++ b/res/values/config.xml @@ -22,6 +22,7 @@ http://www.keepassdroid.com + algorithm app clip_timeout_key db diff --git a/res/values/strings.xml b/res/values/strings.xml index 4708d2ace..03314bc98 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -137,6 +137,7 @@ Entry title/description Search Show password + Twofish Unsupported database version. diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 9484b93b0..5441181e1 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -24,8 +24,8 @@ android:title="@string/database" android:summary="@string/menu_db_settings"> groups = new ArrayList(); // Algorithm used to encrypt the database - public int algorithm; + public PwEncryptionAlgorithm algorithm; public int numKeyEncRounds; // Debugging entries public PwDbHeaderV3 dbHeader; - public int getAlgorithm() { + @Override + public PwEncryptionAlgorithm getEncAlgorithm() { return algorithm; } diff --git a/src/com/keepassdroid/database/PwDatabaseV4.java b/src/com/keepassdroid/database/PwDatabaseV4.java index 97b4e6454..67799bfac 100644 --- a/src/com/keepassdroid/database/PwDatabaseV4.java +++ b/src/com/keepassdroid/database/PwDatabaseV4.java @@ -148,4 +148,9 @@ public class PwDatabaseV4 extends PwDatabase { return false; } + @Override + public PwEncryptionAlgorithm getEncAlgorithm() { + return PwEncryptionAlgorithm.Rjindal; + } + } diff --git a/src/com/keepassdroid/database/PwDbHeaderV3.java b/src/com/keepassdroid/database/PwDbHeaderV3.java index 52bc9b606..e47627fe2 100644 --- a/src/com/keepassdroid/database/PwDbHeaderV3.java +++ b/src/com/keepassdroid/database/PwDbHeaderV3.java @@ -40,9 +40,6 @@ public class PwDbHeaderV3 extends PwDbHeader { public static final int FLAG_ARCFOUR = 4; public static final int FLAG_TWOFISH = 8; - public static final int ALGO_AES = 0; - public static final int ALGO_TWOFISH = 1; - /** Size of byte buffer needed to hold this struct. */ public static final int BUF_SIZE = 124; diff --git a/src/com/keepassdroid/database/PwEncryptionAlgorithm.java b/src/com/keepassdroid/database/PwEncryptionAlgorithm.java new file mode 100644 index 000000000..72175f78b --- /dev/null +++ b/src/com/keepassdroid/database/PwEncryptionAlgorithm.java @@ -0,0 +1,26 @@ +/* + * Copyright 2010 Brian Pellin. + * + * This file is part of KeePassDroid. + * + * KeePassDroid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * KeePassDroid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePassDroid. If not, see . + * + */ +package com.keepassdroid.database; + +public enum PwEncryptionAlgorithm { + + Rjindal, + Twofish; +} diff --git a/src/com/keepassdroid/database/edit/CreateDB.java b/src/com/keepassdroid/database/edit/CreateDB.java index aa403dbd2..57b092505 100644 --- a/src/com/keepassdroid/database/edit/CreateDB.java +++ b/src/com/keepassdroid/database/edit/CreateDB.java @@ -23,7 +23,7 @@ package com.keepassdroid.database.edit; import com.keepassdroid.Database; import com.keepassdroid.app.App; import com.keepassdroid.database.PwDatabaseV3; -import com.keepassdroid.database.PwDbHeaderV3; +import com.keepassdroid.database.PwEncryptionAlgorithm; public class CreateDB extends RunnableOnFinish { @@ -47,7 +47,7 @@ public class CreateDB extends RunnableOnFinish { // Create the PwDatabaseV3 PwDatabaseV3 pm = new PwDatabaseV3(); - pm.algorithm = PwDbHeaderV3.ALGO_AES; + pm.algorithm = PwEncryptionAlgorithm.Rjindal; pm.numKeyEncRounds = DEFAULT_ENCRYPTION_ROUNDS; pm.name = "KeePass Password Manager"; // Build the root group diff --git a/src/com/keepassdroid/database/load/ImporterV3.java b/src/com/keepassdroid/database/load/ImporterV3.java index c861f4855..9988f631f 100644 --- a/src/com/keepassdroid/database/load/ImporterV3.java +++ b/src/com/keepassdroid/database/load/ImporterV3.java @@ -51,6 +51,7 @@ import com.keepassdroid.database.PwDatabaseV3; import com.keepassdroid.database.PwDate; import com.keepassdroid.database.PwDbHeader; import com.keepassdroid.database.PwDbHeaderV3; +import com.keepassdroid.database.PwEncryptionAlgorithm; import com.keepassdroid.database.PwEntryV3; import com.keepassdroid.database.PwGroupV3; import com.keepassdroid.database.exception.InvalidDBException; @@ -138,9 +139,9 @@ public class ImporterV3 extends Importer { // Select algorithm if( (hdr.flags & PwDbHeaderV3.FLAG_RIJNDAEL) != 0 ) { - newManager.algorithm = PwDbHeaderV3.ALGO_AES; + newManager.algorithm = PwEncryptionAlgorithm.Rjindal; } else if( (hdr.flags & PwDbHeaderV3.FLAG_TWOFISH) != 0 ) { - newManager.algorithm = PwDbHeaderV3.ALGO_TWOFISH; + newManager.algorithm = PwEncryptionAlgorithm.Twofish; } else { throw new IOException( "Unknown algorithm." ); } @@ -161,9 +162,9 @@ public class ImporterV3 extends Importer { // Initialize Rijndael algorithm Cipher cipher; try { - if ( newManager.algorithm == PwDbHeaderV3.ALGO_AES ) { + if ( newManager.algorithm == PwEncryptionAlgorithm.Rjindal ) { cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding"); - } else if ( newManager.algorithm == PwDbHeaderV3.ALGO_TWOFISH ) { + } else if ( newManager.algorithm == PwEncryptionAlgorithm.Twofish ) { cipher = CipherFactory.getInstance("TWOFISH/CBC/PKCS7PADDING"); } else { throw new IOException( "Encryption algorithm is not supported" ); diff --git a/src/com/keepassdroid/database/save/PwDbV3Output.java b/src/com/keepassdroid/database/save/PwDbV3Output.java index d17ca1581..903bce449 100644 --- a/src/com/keepassdroid/database/save/PwDbV3Output.java +++ b/src/com/keepassdroid/database/save/PwDbV3Output.java @@ -40,6 +40,7 @@ import com.keepassdroid.crypto.CipherFactory; import com.keepassdroid.database.PwDatabaseV3; import com.keepassdroid.database.PwDbHeader; import com.keepassdroid.database.PwDbHeaderV3; +import com.keepassdroid.database.PwEncryptionAlgorithm; import com.keepassdroid.database.PwEntryV3; import com.keepassdroid.database.PwGroup; import com.keepassdroid.database.PwGroupV3; @@ -84,9 +85,9 @@ public class PwDbV3Output extends PwDbOutput { Cipher cipher; try { - if (mPM.algorithm == PwDbHeaderV3.ALGO_AES) { + if (mPM.algorithm == PwEncryptionAlgorithm.Rjindal) { cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding"); - } else if (mPM.algorithm == PwDbHeaderV3.ALGO_TWOFISH){ + } else if (mPM.algorithm == PwEncryptionAlgorithm.Twofish){ cipher = CipherFactory.getInstance("TWOFISH/CBC/PKCS7PADDING"); } else { throw new Exception(); @@ -119,9 +120,9 @@ public class PwDbV3Output extends PwDbOutput { header.signature2 = PwDbHeaderV3.DBSIG_2; header.flags = PwDbHeaderV3.FLAG_SHA2; - if ( mPM.getAlgorithm() == PwDbHeaderV3.ALGO_AES ) { + if ( mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal ) { header.flags |= PwDbHeaderV3.FLAG_RIJNDAEL; - } else if ( mPM.getAlgorithm() == PwDbHeaderV3.ALGO_TWOFISH ) { + } else if ( mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Twofish ) { header.flags |= PwDbHeaderV3.FLAG_TWOFISH; } else { throw new PwDbOutputException("Unsupported algorithm."); diff --git a/src/com/keepassdroid/settings/AppSettingsActivity.java b/src/com/keepassdroid/settings/AppSettingsActivity.java index 2173f2509..fcb07a1a3 100644 --- a/src/com/keepassdroid/settings/AppSettingsActivity.java +++ b/src/com/keepassdroid/settings/AppSettingsActivity.java @@ -29,6 +29,7 @@ import com.android.keepass.R; import com.keepassdroid.Database; import com.keepassdroid.LockingClosePreferenceActivity; import com.keepassdroid.app.App; +import com.keepassdroid.database.PwEncryptionAlgorithm; import com.keepassdroid.fileselect.FileDbHelper; public class AppSettingsActivity extends LockingClosePreferenceActivity { @@ -76,6 +77,9 @@ public class AppSettingsActivity extends LockingClosePreferenceActivity { }); setRounds(db, rounds); + + Preference algorithm = findPreference(getString(R.string.algorithm_key)); + setAlgorithm(db, algorithm); } else { Preference dbSettings = findPreference(getString(R.string.db_key)); dbSettings.setEnabled(false); @@ -84,7 +88,17 @@ public class AppSettingsActivity extends LockingClosePreferenceActivity { private void setRounds(Database db, Preference rounds) { rounds.setSummary(Long.toString(db.pm.getNumRounds())); + } + + private void setAlgorithm(Database db, Preference algorithm) { + int resId; + if ( db.pm.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal ) { + resId = R.string.rijndael; + } else { + resId = R.string.twofish; + } + algorithm.setSummary(resId); } diff --git a/tests/src/com/keepassdroid/tests/database/Kdb4Twofish.java b/tests/src/com/keepassdroid/tests/database/Kdb4Twofish.java index a7981e6e2..24387101d 100644 --- a/tests/src/com/keepassdroid/tests/database/Kdb4Twofish.java +++ b/tests/src/com/keepassdroid/tests/database/Kdb4Twofish.java @@ -26,7 +26,7 @@ import android.content.res.AssetManager; import android.test.AndroidTestCase; import com.keepassdroid.database.PwDatabaseV3; -import com.keepassdroid.database.PwDbHeaderV3; +import com.keepassdroid.database.PwEncryptionAlgorithm; import com.keepassdroid.database.load.ImporterV3; public class Kdb4Twofish extends AndroidTestCase { @@ -40,7 +40,7 @@ public class Kdb4Twofish extends AndroidTestCase { PwDatabaseV3 db = importer.openDatabase(is, "12345", ""); - assertTrue(db.algorithm == PwDbHeaderV3.ALGO_TWOFISH); + assertTrue(db.algorithm == PwEncryptionAlgorithm.Twofish); is.close();