mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-12-03 23:32:28 -08:00
Refactor rounds
This commit is contained in:
1 parent
d5acd04a19
commit
1e4deb2182
10 files changed
+38
-63
No files matched your search
@@ -19,17 +19,16 @@
|
||||
*/
|
||||
package com.keepassdroid.tests.database;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
import com.keepassdroid.crypto.CipherFactory;
|
||||
import com.keepassdroid.crypto.engine.AesEngine;
|
||||
import com.keepassdroid.database.PwDatabaseV4;
|
||||
import com.keepassdroid.database.load.ImporterV4;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class Kdb4Header extends AndroidTestCase {
|
||||
public void testReadHeader() throws Exception {
|
||||
Context ctx = getContext();
|
||||
@@ -41,7 +40,7 @@ public class Kdb4Header extends AndroidTestCase {
|
||||
|
||||
PwDatabaseV4 db = importer.openDatabase(is, "12345", null);
|
||||
|
||||
assertEquals(6000, db.getNumKeyEncRounds());
|
||||
assertEquals(6000, db.getNumberKeyEncryptionRounds());
|
||||
|
||||
assertTrue(db.getDataCipher().equals(AesEngine.CIPHER_UUID));
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ package com.keepassdroid.database;
|
||||
|
||||
import com.keepassdroid.crypto.finalkey.FinalKey;
|
||||
import com.keepassdroid.crypto.finalkey.FinalKeyFactory;
|
||||
import com.keepassdroid.crypto.keyDerivation.KdfEngine;
|
||||
import com.keepassdroid.database.exception.InvalidKeyFileException;
|
||||
import com.keepassdroid.database.exception.KeyFileEmptyException;
|
||||
import com.keepassdroid.stream.NullOutputStream;
|
||||
@@ -110,7 +109,7 @@ public abstract class PwDatabase<PwGroupDB extends PwGroup<PwGroupDB, PwGroupDB,
|
||||
return finalKey;
|
||||
}
|
||||
|
||||
public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, int numRounds) throws IOException {
|
||||
public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, long numRounds) throws IOException {
|
||||
|
||||
// Write checksum Checksum
|
||||
MessageDigest md;
|
||||
@@ -133,7 +132,7 @@ public abstract class PwDatabase<PwGroupDB extends PwGroup<PwGroupDB, PwGroupDB,
|
||||
* Encrypt the master key a few times to make brute-force key-search harder
|
||||
* @throws IOException
|
||||
*/
|
||||
protected static byte[] transformMasterKey( byte[] pKeySeed, byte[] pKey, int rounds ) throws IOException {
|
||||
protected static byte[] transformMasterKey( byte[] pKeySeed, byte[] pKey, long rounds ) throws IOException {
|
||||
FinalKey key = FinalKeyFactory.createFinalKey();
|
||||
|
||||
return key.transformMasterKey(pKeySeed, pKey, rounds);
|
||||
@@ -277,9 +276,9 @@ public abstract class PwDatabase<PwGroupDB extends PwGroup<PwGroupDB, PwGroupDB,
|
||||
return md.digest();
|
||||
}
|
||||
|
||||
public abstract long getNumRounds();
|
||||
public abstract long getNumberKeyEncryptionRounds();
|
||||
|
||||
public abstract void setNumRounds(long rounds) throws NumberFormatException;
|
||||
public abstract void setNumberKeyEncryptionRounds(long rounds) throws NumberFormatException;
|
||||
|
||||
public PwEncryptionAlgorithm getEncryptionAlgorithm() {
|
||||
if (algorithm != null)
|
||||
|
||||
@@ -95,14 +95,6 @@ public class PwDatabaseV3 extends PwDatabase<PwGroupV3, PwEntryV3> {
|
||||
return PwVersion.V3;
|
||||
}
|
||||
|
||||
public int getNumKeyEncRounds() {
|
||||
return numKeyEncRounds;
|
||||
}
|
||||
|
||||
public void setNumKeyEncRounds(int numKeyEncRounds) {
|
||||
this.numKeyEncRounds = numKeyEncRounds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKeyDerivationName() {
|
||||
return "AES";
|
||||
@@ -273,12 +265,12 @@ public class PwDatabaseV3 extends PwDatabase<PwGroupV3, PwEntryV3> {
|
||||
|
||||
|
||||
@Override
|
||||
public long getNumRounds() {
|
||||
public long getNumberKeyEncryptionRounds() {
|
||||
return numKeyEncRounds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumRounds(long rounds) throws NumberFormatException {
|
||||
public void setNumberKeyEncryptionRounds(long rounds) throws NumberFormatException {
|
||||
if (rounds > Integer.MAX_VALUE || rounds < Integer.MIN_VALUE) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
|
||||
@@ -135,12 +135,14 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
|
||||
this.compressionAlgorithm = compressionAlgorithm;
|
||||
}
|
||||
|
||||
public long getNumKeyEncRounds() {
|
||||
@Override
|
||||
public long getNumberKeyEncryptionRounds() {
|
||||
return numKeyEncRounds;
|
||||
}
|
||||
|
||||
public void setNumKeyEncRounds(long numKeyEncRounds) {
|
||||
this.numKeyEncRounds = numKeyEncRounds;
|
||||
@Override
|
||||
public void setNumberKeyEncryptionRounds(long rounds) throws NumberFormatException {
|
||||
numKeyEncRounds = rounds;
|
||||
}
|
||||
|
||||
public PwDate getNameChanged() {
|
||||
@@ -346,7 +348,7 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, int numRounds) throws IOException {
|
||||
public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, long numRounds) throws IOException {
|
||||
|
||||
byte[] transformedMasterKey = transformMasterKey(masterSeed2, masterKey, numRounds);
|
||||
|
||||
@@ -501,17 +503,6 @@ public class PwDatabaseV4 extends PwDatabase<PwGroupV4, PwEntryV4> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getNumRounds() {
|
||||
return numKeyEncRounds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumRounds(long rounds) throws NumberFormatException {
|
||||
numKeyEncRounds = rounds;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public PwGroupIdV4 newGroupId() {
|
||||
PwGroupIdV4 id;
|
||||
|
||||
@@ -19,26 +19,22 @@
|
||||
*/
|
||||
package com.keepassdroid.database;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.keepassdroid.crypto.keyDerivation.AesKdf;
|
||||
import com.keepassdroid.crypto.keyDerivation.KdfParameters;
|
||||
import com.keepassdroid.database.exception.InvalidDBVersionException;
|
||||
import com.keepassdroid.database.security.ProtectedBinary;
|
||||
import com.keepassdroid.stream.CopyInputStream;
|
||||
import com.keepassdroid.stream.HmacBlockStream;
|
||||
import com.keepassdroid.stream.LEDataInputStream;
|
||||
import com.keepassdroid.utils.Types;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
@@ -65,7 +61,6 @@ public class PwDbHeaderV4 extends PwDbHeader {
|
||||
public static final byte InnerRandomStreamID = 10;
|
||||
public static final byte KdfParameters = 11;
|
||||
public static final byte PublicCustomData = 12;
|
||||
|
||||
}
|
||||
|
||||
public class PwDbInnerHeaderV4Fields {
|
||||
@@ -178,7 +173,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
|
||||
break;
|
||||
|
||||
case PwDbHeaderV4Fields.TransformSeed:
|
||||
assert(version < PwDbHeaderV4.FILE_VERSION_32_4);
|
||||
assert(version < PwDbHeaderV4.FILE_VERSION_32_4); // TODO file > FILEVERSION
|
||||
AesKdf kdfS = new AesKdf();
|
||||
if (!db.getKdfParameters().kdfUUID.equals(kdfS.uuid)) {
|
||||
db.setKdfParameters(kdfS.getDefaultParameters());
|
||||
@@ -195,7 +190,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
|
||||
}
|
||||
long rounds = LEDataInputStream.readLong(fieldData, 0);
|
||||
db.getKdfParameters().setUInt64(AesKdf.ParamRounds, rounds);
|
||||
db.setNumKeyEncRounds(rounds);
|
||||
db.setNumberKeyEncryptionRounds(rounds);
|
||||
break;
|
||||
|
||||
case PwDbHeaderV4Fields.EncryptionIV:
|
||||
@@ -264,7 +259,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
|
||||
throw new IOException("Rounds higher than " + Integer.MAX_VALUE + " are not currently supported.");
|
||||
}
|
||||
|
||||
db.setNumKeyEncRounds(rnd);
|
||||
db.setNumberKeyEncryptionRounds(rnd);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -170,12 +170,12 @@ public class ImporterV3 extends Importer {
|
||||
// Copy for testing
|
||||
newManager.copyHeader(hdr);
|
||||
|
||||
newManager.setNumKeyEncRounds(hdr.numKeyEncRounds);
|
||||
newManager.setNumberKeyEncryptionRounds(hdr.numKeyEncRounds);
|
||||
|
||||
newManager.setName("KeePass Password Manager"); // TODO as resource;
|
||||
|
||||
// Generate transformedMasterKey from masterKey
|
||||
newManager.makeFinalKey(hdr.masterSeed, hdr.transformSeed, newManager.getNumKeyEncRounds());
|
||||
newManager.makeFinalKey(hdr.masterSeed, hdr.transformSeed, newManager.getNumberKeyEncryptionRounds());
|
||||
|
||||
status.updateMessage(R.string.decrypting_db);
|
||||
// Initialize Rijndael algorithm
|
||||
|
||||
@@ -98,7 +98,7 @@ public class PwDbHeaderOutputV4 extends PwDbHeaderOutput {
|
||||
|
||||
if (header.version < PwDbHeaderV4.FILE_VERSION_32_4) {
|
||||
writeHeaderField(PwDbHeaderV4Fields.TransformSeed, header.getTransformSeed());
|
||||
writeHeaderField(PwDbHeaderV4Fields.TransformRounds, LEDataOutputStream.writeLongBuf(db.getNumKeyEncRounds()));
|
||||
writeHeaderField(PwDbHeaderV4Fields.TransformRounds, LEDataOutputStream.writeLongBuf(db.getNumberKeyEncryptionRounds()));
|
||||
} else {
|
||||
writeHeaderField(PwDbHeaderV4Fields.KdfParameters, KdfParameters.serialize(db.getKdfParameters()));
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class PwDbV3Output extends PwDbOutput {
|
||||
public byte[] getFinalKey(PwDbHeader header) throws PwDbOutputException {
|
||||
try {
|
||||
PwDbHeaderV3 h3 = (PwDbHeaderV3) header;
|
||||
mPM.makeFinalKey(h3.masterSeed, h3.transformSeed, mPM.getNumKeyEncRounds());
|
||||
mPM.makeFinalKey(h3.masterSeed, h3.transformSeed, mPM.getNumberKeyEncryptionRounds());
|
||||
return mPM.getFinalKey();
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Key creation failed: " + e.getMessage());
|
||||
@@ -138,7 +138,7 @@ public class PwDbV3Output extends PwDbOutput {
|
||||
header.version = PwDbHeaderV3.DBVER_DW;
|
||||
header.numGroups = mPM.numberOfGroups();
|
||||
header.numEntries = mPM.numberOfEntries();
|
||||
header.numKeyEncRounds = mPM.getNumKeyEncRounds();
|
||||
header.numKeyEncRounds = (int) mPM.getNumberKeyEncryptionRounds();
|
||||
|
||||
setIVs(header);
|
||||
|
||||
|
||||
@@ -272,7 +272,6 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
|
||||
|
||||
PwDatabase pwDatabase = db.getPwDatabase(); // Transit methods in db
|
||||
|
||||
|
||||
// Encryption Algorithme
|
||||
Preference algorithmPref = findPreference(getString(R.string.encryption_algorithm_key));
|
||||
algorithmPref.setSummary(pwDatabase
|
||||
@@ -285,10 +284,10 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
|
||||
|
||||
// Round encryption
|
||||
Preference roundPref = findPreference(getString(R.string.rounds_key));
|
||||
roundPref.setSummary(Long.toString(pwDatabase.getNumRounds()));
|
||||
roundPref.setSummary(Long.toString(pwDatabase.getNumberKeyEncryptionRounds()));
|
||||
roundPref.setEnabled(false); //TODO refactor round pref
|
||||
roundPref.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
preference.setSummary(Long.toString(pwDatabase.getNumRounds()));
|
||||
preference.setSummary(Long.toString(pwDatabase.getNumberKeyEncryptionRounds()));
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
+5
-5
@@ -50,12 +50,12 @@ public class RoundsPreferenceDialogFragmentCompat extends PreferenceDialogFragme
|
||||
rounds = 1;
|
||||
}
|
||||
|
||||
long oldRounds = mPM.getNumRounds();
|
||||
long oldRounds = mPM.getNumberKeyEncryptionRounds();
|
||||
try {
|
||||
mPM.setNumRounds(rounds);
|
||||
mPM.setNumberKeyEncryptionRounds(rounds);
|
||||
} catch (NumberFormatException e) {
|
||||
Toast.makeText(getContext(), R.string.error_rounds_too_large, Toast.LENGTH_LONG).show();
|
||||
mPM.setNumRounds(Integer.MAX_VALUE);
|
||||
mPM.setNumberKeyEncryptionRounds(Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
Handler handler = new Handler();
|
||||
@@ -75,7 +75,7 @@ public class RoundsPreferenceDialogFragmentCompat extends PreferenceDialogFragme
|
||||
// Get the time from the related Preference
|
||||
Database db = App.getDB();
|
||||
mPM = db.getPwDatabase();
|
||||
long numRounds = mPM.getNumRounds();
|
||||
long numRounds = mPM.getNumberKeyEncryptionRounds();
|
||||
|
||||
DialogPreference preference = getPreference();
|
||||
if (preference instanceof RoundsPreference) {
|
||||
@@ -100,7 +100,7 @@ public class RoundsPreferenceDialogFragmentCompat extends PreferenceDialogFragme
|
||||
public void run() {
|
||||
if (!mSuccess) {
|
||||
displayMessage(mCtx);
|
||||
mPM.setNumRounds(mOldRounds);
|
||||
mPM.setNumberKeyEncryptionRounds(mOldRounds);
|
||||
}
|
||||
|
||||
super.run();
|
||||
|
||||
Reference in new issue
Block a user