mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Read data rounds from the header. Fix some of my types tests that were missing asserts.
This commit is contained in:
@@ -73,7 +73,7 @@ public class PwDatabaseV3 extends PwDatabase {
|
||||
public byte masterKey[] = new byte[32];
|
||||
// Algorithm used to encrypt the database
|
||||
public int algorithm;
|
||||
public int numKeyEncRounds;
|
||||
public int mNumKeyEncRounds;
|
||||
|
||||
// Debugging entries
|
||||
public PwDbHeaderV3 dbHeader;
|
||||
@@ -88,7 +88,7 @@ public class PwDatabaseV3 extends PwDatabase {
|
||||
}
|
||||
|
||||
public int getNumKeyEncRecords() {
|
||||
return numKeyEncRounds;
|
||||
return mNumKeyEncRounds;
|
||||
}
|
||||
|
||||
public void setMasterKey( String key, String keyFileName ) throws InvalidKeyFileException, IOException {
|
||||
|
||||
@@ -103,6 +103,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
|
||||
mTransformSeed = fieldData;
|
||||
|
||||
case PwDbHeaderV4Fields.TransformRounds:
|
||||
setTransformRounds(fieldData);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -130,6 +131,20 @@ public class PwDbHeaderV4 extends PwDbHeader {
|
||||
|
||||
}
|
||||
|
||||
private void setTransformRounds(byte[] rounds) throws IOException {
|
||||
if ( rounds == null || rounds.length != 8 ) {
|
||||
throw new IOException("Invalid rounds.");
|
||||
}
|
||||
|
||||
long rnd = Types.readLong(rounds, 0);
|
||||
|
||||
if ( rnd < 0 ) {
|
||||
//TODO: Actually support really large numbers
|
||||
throw new IOException("Rounds higher than " + Long.MAX_VALUE + " are not currently supported.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Determines if this is a supported version.
|
||||
*
|
||||
* A long is needed here to represent the unsigned int since we perform
|
||||
|
||||
@@ -48,7 +48,7 @@ public class CreateDB extends RunnableOnFinish {
|
||||
// Create the PwDatabaseV3
|
||||
PwDatabaseV3 pm = new PwDatabaseV3();
|
||||
pm.algorithm = PwDbHeaderV3.ALGO_AES;
|
||||
pm.numKeyEncRounds = DEFAULT_ENCRYPTION_ROUNDS;
|
||||
pm.mNumKeyEncRounds = DEFAULT_ENCRYPTION_ROUNDS;
|
||||
pm.name = "KeePass Password Manager";
|
||||
// Build the root group
|
||||
pm.constructTree(null);
|
||||
|
||||
@@ -152,12 +152,12 @@ public class ImporterV3 extends Importer {
|
||||
newManager.dbHeader = hdr;
|
||||
}
|
||||
|
||||
newManager.numKeyEncRounds = hdr.numKeyEncRounds;
|
||||
newManager.mNumKeyEncRounds = hdr.numKeyEncRounds;
|
||||
|
||||
newManager.name = "KeePass Password Manager";
|
||||
|
||||
// Generate transformedMasterKey from masterKey
|
||||
finalKey = makeFinalKey(hdr.mMasterSeed, hdr.mTransformSeed, newManager.masterKey, newManager.numKeyEncRounds);
|
||||
finalKey = makeFinalKey(hdr.mMasterSeed, hdr.mTransformSeed, newManager.masterKey, newManager.mNumKeyEncRounds);
|
||||
newManager.finalKey = new byte[finalKey.length];
|
||||
System.arraycopy(finalKey, 0, newManager.finalKey, 0, finalKey.length);
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ public class PwDbV3Output {
|
||||
|
||||
public byte[] getFinalKey(PwDbHeader header) throws PwDbOutputException {
|
||||
try {
|
||||
return ImporterV3.makeFinalKey(header.mMasterSeed, header.mTransformSeed, mPM.masterKey, mPM.numKeyEncRounds);
|
||||
return ImporterV3.makeFinalKey(header.mMasterSeed, header.mTransformSeed, mPM.masterKey, mPM.mNumKeyEncRounds);
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Key creation failed: " + e.getMessage());
|
||||
}
|
||||
@@ -73,7 +73,7 @@ public class PwDbV3Output {
|
||||
|
||||
public byte[] getFinalKey2(PwDbHeader header) throws PwDbOutputException {
|
||||
try {
|
||||
return ImporterV3.makeFinalKey(header.mMasterSeed, header.mTransformSeed, mPM.masterKey, mPM.numKeyEncRounds);
|
||||
return ImporterV3.makeFinalKey(header.mMasterSeed, header.mTransformSeed, mPM.masterKey, mPM.mNumKeyEncRounds);
|
||||
} catch (IOException e) {
|
||||
throw new PwDbOutputException("Key creation failed: " + e.getMessage());
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ public class AppSettingsActivity extends LockingClosePreferenceActivity {
|
||||
}
|
||||
|
||||
private void setRounds(Database db, Preference rounds) {
|
||||
rounds.setSummary(Integer.toString(db.mPM.numKeyEncRounds));
|
||||
rounds.setSummary(Integer.toString(db.mPM.mNumKeyEncRounds));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ public class RoundsPreference extends DialogPreference {
|
||||
|
||||
Database db = App.getDB();
|
||||
mPM = db.mPM;
|
||||
int numRounds = mPM.numKeyEncRounds;
|
||||
int numRounds = mPM.mNumKeyEncRounds;
|
||||
mRoundsView.setText(Integer.toString(numRounds));
|
||||
|
||||
return view;
|
||||
@@ -82,8 +82,8 @@ public class RoundsPreference extends DialogPreference {
|
||||
rounds = 1;
|
||||
}
|
||||
|
||||
int oldRounds = mPM.numKeyEncRounds;
|
||||
mPM.numKeyEncRounds = rounds;
|
||||
int oldRounds = mPM.mNumKeyEncRounds;
|
||||
mPM.mNumKeyEncRounds = rounds;
|
||||
|
||||
Handler handler = new Handler();
|
||||
SaveDB save = new SaveDB(App.getDB(), new AfterSave(getContext(), handler, oldRounds));
|
||||
@@ -114,7 +114,7 @@ public class RoundsPreference extends DialogPreference {
|
||||
}
|
||||
} else {
|
||||
displayMessage(mCtx);
|
||||
mPM.numKeyEncRounds = mOldRounds;
|
||||
mPM.mNumKeyEncRounds = mOldRounds;
|
||||
}
|
||||
|
||||
super.run();
|
||||
|
||||
@@ -70,6 +70,25 @@ public class Types {
|
||||
return buf;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
public static long readLong( byte buf[], int offset ) {
|
||||
return ((long)buf[offset + 0] & 0xFF) + (((long)buf[offset + 1] & 0xFF) << 8)
|
||||
+ (((long)buf[offset + 2] & 0xFF) << 16) + (((long)buf[offset + 3] & 0xFF) << 24)
|
||||
+ (((long)buf[offset + 4] & 0xFF) << 32) + (((long)buf[offset + 5] & 0xFF) << 40)
|
||||
+ (((long)buf[offset + 6] & 0xFF) << 48) + (((long)buf[offset + 7] & 0xFF) << 56);
|
||||
}
|
||||
|
||||
public static void writeLong( long val, byte[] buf, int offset ) {
|
||||
buf[offset + 0] = (byte)(val & 0xFF);
|
||||
buf[offset + 1] = (byte)((val >>> 8) & 0xFF);
|
||||
buf[offset + 2] = (byte)((val >>> 16) & 0xFF);
|
||||
buf[offset + 3] = (byte)((val >>> 24) & 0xFF);
|
||||
buf[offset + 4] = (byte)((val >>> 32) & 0xFF);
|
||||
buf[offset + 5] = (byte)((val >>> 40) & 0xFF);
|
||||
buf[offset + 6] = (byte)((val >>> 48) & 0xFF);
|
||||
buf[offset + 7] = (byte)((val >>> 56) & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a 32-bit value.
|
||||
@@ -99,6 +118,8 @@ public class Types {
|
||||
return (readInt(is) & INT_TO_LONG_MASK);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Write a 32-bit value.
|
||||
|
||||
@@ -22,6 +22,7 @@ package com.keepassdroid.tests;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -30,6 +31,39 @@ import com.keepassdroid.database.PwDate;
|
||||
import com.keepassdroid.utils.Types;
|
||||
|
||||
public class TypesTest extends TestCase {
|
||||
|
||||
public void testReadWriteLongZero() {
|
||||
testReadWriteLong((byte) 0);
|
||||
}
|
||||
|
||||
public void testReadWriteLongMax() {
|
||||
testReadWriteLong(Byte.MAX_VALUE);
|
||||
}
|
||||
|
||||
public void testReadWriteLongMin() {
|
||||
testReadWriteLong(Byte.MIN_VALUE);
|
||||
}
|
||||
|
||||
public void testReadWriteLongRnd() {
|
||||
Random rnd = new Random();
|
||||
byte[] buf = new byte[1];
|
||||
rnd.nextBytes(buf);
|
||||
|
||||
testReadWriteLong(buf[0]);
|
||||
}
|
||||
|
||||
private void testReadWriteLong(byte value) {
|
||||
byte[] orig = new byte[8];
|
||||
byte[] dest = new byte[8];
|
||||
|
||||
setArray(orig, value, 0, 8);
|
||||
|
||||
long one = Types.readLong(orig, 0);
|
||||
Types.writeLong(one, dest, 0);
|
||||
|
||||
assertArrayEquals(orig, dest);
|
||||
|
||||
}
|
||||
|
||||
public void testReadWriteIntZero() {
|
||||
testReadWriteInt((byte) 0);
|
||||
@@ -97,6 +131,9 @@ public class TypesTest extends TestCase {
|
||||
|
||||
int one = Types.readShort(orig, 0);
|
||||
Types.writeShort(one, dest, 0);
|
||||
|
||||
assertArrayEquals(orig, dest);
|
||||
|
||||
}
|
||||
|
||||
public void testReadWriteByteZero() {
|
||||
@@ -119,6 +156,9 @@ public class TypesTest extends TestCase {
|
||||
|
||||
int one = Types.readUByte(orig, 0);
|
||||
Types.writeUByte(one, dest, 0);
|
||||
|
||||
assertArrayEquals(orig, dest);
|
||||
|
||||
}
|
||||
|
||||
public void testDate() {
|
||||
|
||||
Reference in New Issue
Block a user