Update bouncycastle library names.

This commit is contained in:
Brian Pellin
2009-05-02 16:07:32 -05:00
parent 76b68ef2c4
commit b1ead76d3c
43 changed files with 236 additions and 144 deletions

View File

@@ -27,7 +27,7 @@ import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import java.util.Vector; import java.util.Vector;
import org.bouncycastle1.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.InvalidCipherTextException;
import org.phoneid.keepassj2me.ImporterV3; import org.phoneid.keepassj2me.ImporterV3;
import org.phoneid.keepassj2me.PwEntry; import org.phoneid.keepassj2me.PwEntry;
import org.phoneid.keepassj2me.PwGroup; import org.phoneid.keepassj2me.PwGroup;

View File

@@ -23,7 +23,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import org.bouncycastle1.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.InvalidCipherTextException;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;

View File

@@ -48,4 +48,8 @@ public class PwDbHeaderOutput {
mOS.write(Types.writeInt(mHeader.numKeyEncRounds)); mOS.write(Types.writeInt(mHeader.numKeyEncRounds));
} }
public void close() throws IOException {
mOS.close();
}
} }

View File

@@ -19,8 +19,6 @@
*/ */
package com.android.keepass.keepasslib; package com.android.keepass.keepasslib;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.DigestOutputStream; import java.security.DigestOutputStream;
@@ -28,6 +26,13 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.phoneid.keepassj2me.ImporterV3;
import org.phoneid.keepassj2me.PwDbHeader; import org.phoneid.keepassj2me.PwDbHeader;
import org.phoneid.keepassj2me.PwEntry; import org.phoneid.keepassj2me.PwEntry;
import org.phoneid.keepassj2me.PwGroup; import org.phoneid.keepassj2me.PwGroup;
@@ -51,22 +56,79 @@ public class PwManagerOutput {
mDebug = debug; mDebug = debug;
} }
public void output() throws IOException, PwManagerOutputException { public void close() throws PwManagerOutputException {
try {
mOS.close();
} catch (IOException e) {
throw new PwManagerOutputException("Failed to close stream.");
/*
int filePadding = (int)(16 - (fileSize % 16)); // Pad file to 16-byte boundary
if ( filePadding > 0 ) {
byte[] padding = new byte[filePadding];
fos.write(padding);
} }
*/
} }
public void outputHeader(OutputStream os) throws PwManagerOutputException { public void output() throws PwManagerOutputException, IOException {
PwDbHeader header = outputHeader(mOS);
// Write checksum Checksum
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
assert true;
throw new PwManagerOutputException("SHA-256 not implemented here.");
}
NullOutputStream nos = new NullOutputStream();
DigestOutputStream dos = new DigestOutputStream(nos, md);
byte[] transformedMasterKey = ImporterV3.transformMasterKey(header.masterSeed2, mPM.masterKey, header.numKeyEncRounds);
try {
dos.write(header.masterSeed);
dos.write(transformedMasterKey);
dos.close();
nos.close();
} catch ( IOException e ) {
throw new PwManagerOutputException("Failed to build final key.");
}
byte[] finalKey = md.digest();
// Bouncy Castle implementation
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.init(true, new ParametersWithIV(new KeyParameter(finalKey), header.encryptionIV));
BufferedBlockCipherOutputStream bbcos = new BufferedBlockCipherOutputStream(mOS, cipher);
outputPlanGroupAndEntries(bbcos);
bbcos.close();
/*
try {
bbcos.close();
} catch (IOException e) {
throw new PwManagerOutputException("Failed to close encryption stream.");
}
*/
/* Why doesn't java native work?
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (Exception e) {
throw new PwManagerOutputException("Algorithm not supported.");
}
try {
cipher.init( Cipher.ENCRYPT_MODE, new SecretKeySpec(finalKey, "AES" ), new IvParameterSpec(header.encryptionIV) );
CipherOutputStream cos = new CipherOutputStream(mOS, cipher);
outputPlanGroupAndEntries(cos);
cos.close();
} catch (InvalidKeyException e) {
throw new PwManagerOutputException("Invalid key");
} catch (InvalidAlgorithmParameterException e) {
throw new PwManagerOutputException("Invalid algorithm parameter.");
} catch (IOException e) {
throw new PwManagerOutputException("Failed to output final encrypted part.");
}
*/
}
public PwDbHeader outputHeader(OutputStream os) throws PwManagerOutputException {
// Build header // Build header
PwDbHeader header = new PwDbHeader(); PwDbHeader header = new PwDbHeader();
header.signature1 = PwDbHeader.PWM_DBSIG_1; header.signature1 = PwDbHeader.PWM_DBSIG_1;
@@ -113,16 +175,13 @@ public class PwManagerOutput {
throw new PwManagerOutputException("SHA-256 not implemented here."); throw new PwManagerOutputException("SHA-256 not implemented here.");
} }
FileOutputStream fos; NullOutputStream nos;
try { nos = new NullOutputStream();
fos = new FileOutputStream("/dev/null"); DigestOutputStream dos = new DigestOutputStream(nos, md);
} catch (FileNotFoundException e1) {
throw new PwManagerOutputException("Could not open /dev/null");
}
DigestOutputStream dos = new DigestOutputStream(fos, md);
try { try {
outputPlanGroupAndEntries(dos); outputPlanGroupAndEntries(dos);
dos.close(); dos.close();
nos.close();
} catch (IOException e) { } catch (IOException e) {
throw new PwManagerOutputException("Failed to generate checksum."); throw new PwManagerOutputException("Failed to generate checksum.");
} }
@@ -134,36 +193,41 @@ public class PwManagerOutput {
PwDbHeaderOutput pho = new PwDbHeaderOutput(header, os); PwDbHeaderOutput pho = new PwDbHeaderOutput(header, os);
try { try {
pho.output(); pho.output();
pho.close();
} catch (IOException e) { } catch (IOException e) {
throw new PwManagerOutputException("Failed to output the header."); throw new PwManagerOutputException("Failed to output the header.");
} }
return header;
} }
public void outputPlanGroupAndEntries(OutputStream os) throws IOException { public void outputPlanGroupAndEntries(OutputStream os) throws PwManagerOutputException {
//long size = 0; //long size = 0;
// Groups // Groups
for (int i = 0; i < mPM.groups.size(); i++ ) { for (int i = 0; i < mPM.groups.size(); i++ ) {
PwGroup pg = mPM.groups.get(i); PwGroup pg = mPM.groups.get(i);
PwGroupOutput pgo = new PwGroupOutput(pg, os); PwGroupOutput pgo = new PwGroupOutput(pg, os);
pgo.output(); try {
//size += pgo.getLength(); pgo.output();
} catch (IOException e) {
throw new PwManagerOutputException("Failed to output a group.");
}
} }
// Entries // Entries
for (int i = 0; i < mPM.entries.size(); i++ ) { for (int i = 0; i < mPM.entries.size(); i++ ) {
PwEntry pe = mPM.entries.get(i); PwEntry pe = mPM.entries.get(i);
PwEntryOutput peo = new PwEntryOutput(pe, os); PwEntryOutput peo = new PwEntryOutput(pe, os);
peo.output(); try {
//size += peo.getLength(); peo.output();
} catch (IOException e) {
throw new PwManagerOutputException("Failed to output an entry.");
}
} }
//return size;
} }
class PwManagerOutputException extends Exception { public class PwManagerOutputException extends Exception {
public PwManagerOutputException(String string) { public PwManagerOutputException(String string) {
super(string); super(string);

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**
* all parameter classes implement this. * all parameter classes implement this.

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**
* the foundation class for the hard exceptions thrown by the crypto packages. * the foundation class for the hard exceptions thrown by the crypto packages.

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**
* this exception is thrown if a buffer that is meant to have output * this exception is thrown if a buffer that is meant to have output

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**
* interface that a message digest conforms to. * interface that a message digest conforms to.

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
public interface ExtendedDigest public interface ExtendedDigest
extends Digest extends Digest

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**
* this exception is thrown whenever we find something we don't expect in a * this exception is thrown whenever we find something we don't expect in a

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**
* super class for all Password Based Encryption (PBE) parameter generator classes. * super class for all Password Based Encryption (PBE) parameter generator classes.

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
/** /**
* the foundation class for the exceptions thrown by the crypto packages. * the foundation class for the exceptions thrown by the crypto packages.

View File

@@ -1,12 +1,12 @@
//package java.security; //package java.security;
package org.bouncycastle1.crypto; package org.bouncycastle.crypto;
import java.util.Random; import java.util.Random;
import org.bouncycastle1.crypto.digests.SHA1Digest; import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle1.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle1.crypto.prng.RandomGenerator; import org.bouncycastle.crypto.prng.DigestRandomGenerator;
import org.bouncycastle1.crypto.prng.DigestRandomGenerator; import org.bouncycastle.crypto.prng.RandomGenerator;
/** /**
* An implementation of SecureRandom specifically for the light-weight API, JDK * An implementation of SecureRandom specifically for the light-weight API, JDK

View File

@@ -1,6 +1,6 @@
package org.bouncycastle1.crypto.digests; package org.bouncycastle.crypto.digests;
import org.bouncycastle1.crypto.ExtendedDigest; import org.bouncycastle.crypto.ExtendedDigest;
/** /**
* base implementation of MD4 family style digest as outlined in * base implementation of MD4 family style digest as outlined in

View File

@@ -1,6 +1,6 @@
package org.bouncycastle1.crypto.digests; package org.bouncycastle.crypto.digests;
import org.bouncycastle1.crypto.ExtendedDigest; import org.bouncycastle.crypto.ExtendedDigest;
/** /**
* Base class for SHA-384 and SHA-512. * Base class for SHA-384 and SHA-512.

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto.digests; package org.bouncycastle.crypto.digests;
/** /**
* implementation of SHA-1 as outlined in "Handbook of Applied Cryptography", pages 346 - 349. * implementation of SHA-1 as outlined in "Handbook of Applied Cryptography", pages 346 - 349.
@@ -7,7 +7,7 @@ package org.bouncycastle1.crypto.digests;
* is the "endienness" of the word processing! * is the "endienness" of the word processing!
*/ */
public class SHA1Digest public class SHA1Digest
extends org.bouncycastle1.crypto.digests.GeneralDigest extends org.bouncycastle.crypto.digests.GeneralDigest
{ {
private static final int DIGEST_LENGTH = 20; private static final int DIGEST_LENGTH = 20;

View File

@@ -1,7 +1,7 @@
package org.bouncycastle1.crypto.digests; package org.bouncycastle.crypto.digests;
import org.bouncycastle1.crypto.digests.GeneralDigest; import org.bouncycastle.crypto.digests.GeneralDigest;
/** /**
@@ -16,7 +16,7 @@ import org.bouncycastle1.crypto.digests.GeneralDigest;
* </pre> * </pre>
*/ */
public class SHA256Digest public class SHA256Digest
extends org.bouncycastle1.crypto.digests.GeneralDigest extends org.bouncycastle.crypto.digests.GeneralDigest
{ {
private static final int DIGEST_LENGTH = 32; private static final int DIGEST_LENGTH = 32;

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto.digests; package org.bouncycastle.crypto.digests;
/** /**

View File

@@ -1,9 +1,9 @@
package org.bouncycastle1.crypto.engines; package org.bouncycastle.crypto.engines;
import org.bouncycastle1.crypto.BlockCipher; import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle1.crypto.DataLengthException; import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle1.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.KeyParameter;
/** /**
* an implementation of the AES (Rijndael), from FIPS-197. * an implementation of the AES (Rijndael), from FIPS-197.

View File

@@ -1,9 +1,9 @@
package org.bouncycastle1.crypto.engines; package org.bouncycastle.crypto.engines;
import org.bouncycastle1.crypto.BlockCipher; import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle1.crypto.DataLengthException; import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle1.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.KeyParameter;
/** /**
* A class that provides Twofish encryption operations. * A class that provides Twofish encryption operations.

View File

@@ -1,10 +1,10 @@
package org.bouncycastle1.crypto.generators; package org.bouncycastle.crypto.generators;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle1.crypto.Digest; import org.bouncycastle.crypto.Digest;
import org.bouncycastle1.crypto.PBEParametersGenerator; import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle1.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle1.crypto.params.ParametersWithIV; import org.bouncycastle.crypto.params.ParametersWithIV;
/** /**
* Generator for PBE derived keys and ivs as defined by PKCS 5 V2.0 Scheme 1. * Generator for PBE derived keys and ivs as defined by PKCS 5 V2.0 Scheme 1.

View File

@@ -1,12 +1,12 @@
package org.bouncycastle1.crypto.generators; package org.bouncycastle.crypto.generators;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle1.crypto.Mac; import org.bouncycastle.crypto.Mac;
import org.bouncycastle1.crypto.PBEParametersGenerator; import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle1.crypto.digests.SHA1Digest; import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle1.crypto.macs.HMac; import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle1.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle1.crypto.params.ParametersWithIV; import org.bouncycastle.crypto.params.ParametersWithIV;
/** /**
* Generator for PBE derived keys and ivs as defined by PKCS 5 V2.0 Scheme 2. * Generator for PBE derived keys and ivs as defined by PKCS 5 V2.0 Scheme 2.

View File

@@ -1,12 +1,12 @@
package org.bouncycastle1.crypto.macs; package org.bouncycastle.crypto.macs;
import java.util.Hashtable; import java.util.Hashtable;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle1.crypto.Digest; import org.bouncycastle.crypto.Digest;
import org.bouncycastle1.crypto.ExtendedDigest; import org.bouncycastle.crypto.ExtendedDigest;
import org.bouncycastle1.crypto.Mac; import org.bouncycastle.crypto.Mac;
import org.bouncycastle1.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.KeyParameter;
/** /**
* HMAC implementation based on RFC2104 * HMAC implementation based on RFC2104

View File

@@ -1,9 +1,9 @@
package org.bouncycastle1.crypto.modes; package org.bouncycastle.crypto.modes;
import org.bouncycastle1.crypto.BlockCipher; import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle1.crypto.DataLengthException; import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle1.crypto.params.ParametersWithIV; import org.bouncycastle.crypto.params.ParametersWithIV;
/** /**
* implements Cipher-Block-Chaining (CBC) mode on top of a simple cipher. * implements Cipher-Block-Chaining (CBC) mode on top of a simple cipher.

View File

@@ -1,8 +1,8 @@
package org.bouncycastle1.crypto.paddings; package org.bouncycastle.crypto.paddings;
import org.bouncycastle1.crypto.SecureRandom; import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.SecureRandom;
import org.bouncycastle1.crypto.InvalidCipherTextException;
/** /**
* Block cipher padders are expected to conform to this interface * Block cipher padders are expected to conform to this interface

View File

@@ -1,6 +1,6 @@
package org.bouncycastle1.crypto.paddings; package org.bouncycastle.crypto.paddings;
import org.bouncycastle1.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.InvalidCipherTextException;
/** /**
* A padder that adds PKCS7/PKCS5 padding to a block. * A padder that adds PKCS7/PKCS5 padding to a block.
@@ -13,7 +13,7 @@ public class PKCS7Padding
* *
* @param random - a SecureRandom if available. * @param random - a SecureRandom if available.
*/ */
public void init(org.bouncycastle1.crypto.SecureRandom random) public void init(org.bouncycastle.crypto.SecureRandom random)
throws IllegalArgumentException throws IllegalArgumentException
{ {
// nothing to do. // nothing to do.

View File

@@ -1,11 +1,11 @@
package org.bouncycastle1.crypto.paddings; package org.bouncycastle.crypto.paddings;
import org.bouncycastle1.crypto.BlockCipher; import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle1.crypto.BufferedBlockCipher; import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle1.crypto.DataLengthException; import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle1.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle1.crypto.params.ParametersWithRandom; import org.bouncycastle.crypto.params.ParametersWithRandom;
/** /**
* A wrapper class that allows block ciphers to be used to process data in * A wrapper class that allows block ciphers to be used to process data in

View File

@@ -1,6 +1,6 @@
package org.bouncycastle1.crypto.params; package org.bouncycastle.crypto.params;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
public class KeyParameter public class KeyParameter
implements CipherParameters implements CipherParameters

View File

@@ -1,6 +1,6 @@
package org.bouncycastle1.crypto.params; package org.bouncycastle.crypto.params;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
public class ParametersWithIV public class ParametersWithIV
implements CipherParameters implements CipherParameters

View File

@@ -1,8 +1,8 @@
package org.bouncycastle1.crypto.params; package org.bouncycastle.crypto.params;
import org.bouncycastle1.crypto.CipherParameters; import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.SecureRandom;
import org.bouncycastle1.crypto.SecureRandom;
public class ParametersWithRandom public class ParametersWithRandom
implements CipherParameters implements CipherParameters

View File

@@ -1,6 +1,6 @@
package org.bouncycastle1.crypto.prng; package org.bouncycastle.crypto.prng;
import org.bouncycastle1.crypto.Digest; import org.bouncycastle.crypto.Digest;
/** /**
* Random generation based on the digest with counter. Calling addSeedMaterial will * Random generation based on the digest with counter. Calling addSeedMaterial will

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.crypto.prng; package org.bouncycastle.crypto.prng;
/** /**
* Generic interface for objects generating random bytes. * Generic interface for objects generating random bytes.

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.util; package org.bouncycastle.util;
/** /**
* General array utilities. * General array utilities.

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.util.encoders; package org.bouncycastle.util.encoders;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.util.encoders; package org.bouncycastle.util.encoders;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;

View File

@@ -1,4 +1,4 @@
package org.bouncycastle1.util.encoders; package org.bouncycastle.util.encoders;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;

View File

@@ -36,14 +36,14 @@ import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException; import javax.crypto.ShortBufferException;
import org.bouncycastle1.crypto.BufferedBlockCipher; import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle1.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle1.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle1.crypto.engines.AESEngine; import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle1.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle1.crypto.paddings.PKCS7Padding; import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle1.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle1.crypto.params.ParametersWithIV; import org.bouncycastle.crypto.params.ParametersWithIV;
import org.phoneid.PhoneIDUtil; import org.phoneid.PhoneIDUtil;
import android.util.Log; import android.util.Log;
@@ -181,7 +181,12 @@ public class ImporterV3 {
int encryptedPartSize = 0; int encryptedPartSize = 0;
//try { //try {
PKCS7Padding padding = new PKCS7Padding(); PKCS7Padding padding = new PKCS7Padding();
encryptedPartSize = paddedEncryptedPartSize - padding.padCount(filebuf); int paddingSize = padding.padCount(filebuf);
encryptedPartSize = paddedEncryptedPartSize - paddingSize;
if ( mDebug ) {
newManager.paddingBytes = paddingSize;
}
//} catch (Exception e) { //} catch (Exception e) {
//} //}
// NI // NI
@@ -320,7 +325,7 @@ public class ImporterV3 {
* @throws ShortBufferException * @throws ShortBufferException
*/ */
static byte[] transformMasterKey( byte[] pKeySeed, byte[] pKey, int rounds ) public static byte[] transformMasterKey( byte[] pKeySeed, byte[] pKey, int rounds )
/*throws InvalidKeyException, /*throws InvalidKeyException,
IllegalBlockSizeException, IllegalBlockSizeException,
BadPaddingException, BadPaddingException,

View File

@@ -32,7 +32,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Vector; import java.util.Vector;
import org.bouncycastle1.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.digests.SHA256Digest;
import com.android.keepass.keepasslib.InvalidKeyFileException; import com.android.keepass.keepasslib.InvalidKeyFileException;
@@ -65,12 +65,14 @@ public class PwManager {
// Used for in-memory encryption of passwords // Used for in-memory encryption of passwords
// private byte sessionKey[] = new byte[PWM_SESSION_KEY_SIZE]; // private byte sessionKey[] = new byte[PWM_SESSION_KEY_SIZE];
// Master key used to encrypt the whole database // Master key used to encrypt the whole database
byte masterKey[] = new byte[32]; public byte masterKey[] = new byte[32];
// Algorithm used to encrypt the database // Algorithm used to encrypt the database
int algorithm; int algorithm;
int numKeyEncRounds; int numKeyEncRounds;
// Debugging entries
public PwDbHeader dbHeader; public PwDbHeader dbHeader;
public long paddingBytes;
// root group // root group
PwGroup rootGroup; PwGroup rootGroup;

View File

@@ -22,6 +22,9 @@ package com.android.keepass.tests;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.security.DigestOutputStream; import java.security.DigestOutputStream;
@@ -30,10 +33,12 @@ import java.security.NoSuchAlgorithmException;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.phoneid.keepassj2me.PwDbHeader;
import org.phoneid.keepassj2me.PwManager; import org.phoneid.keepassj2me.PwManager;
import com.android.keepass.keepasslib.PwDbHeaderOutput; import com.android.keepass.keepasslib.PwDbHeaderOutput;
import com.android.keepass.keepasslib.PwManagerOutput; import com.android.keepass.keepasslib.PwManagerOutput;
import com.android.keepass.keepasslib.PwManagerOutput.PwManagerOutputException;
public class PwManagerOutputTest extends TestCase { public class PwManagerOutputTest extends TestCase {
PwManager mPM; PwManager mPM;
@@ -46,7 +51,7 @@ public class PwManagerOutputTest extends TestCase {
} }
public void testPlainContent() throws IOException { public void testPlainContent() throws IOException, PwManagerOutputException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
PwManagerOutput pos = new PwManagerOutput(mPM, bos, PwManagerOutput.DEBUG); PwManagerOutput pos = new PwManagerOutput(mPM, bos, PwManagerOutput.DEBUG);
@@ -56,7 +61,7 @@ public class PwManagerOutputTest extends TestCase {
} }
public void testChecksum() throws NoSuchAlgorithmException, IOException { public void testChecksum() throws NoSuchAlgorithmException, IOException, PwManagerOutputException {
FileOutputStream fos = new FileOutputStream("/dev/null"); FileOutputStream fos = new FileOutputStream("/dev/null");
MessageDigest md = MessageDigest.getInstance("SHA-256"); MessageDigest md = MessageDigest.getInstance("SHA-256");
@@ -68,7 +73,7 @@ public class PwManagerOutputTest extends TestCase {
assertArrayEquals("Hash of groups and entries failed.", md.digest(), mPM.dbHeader.contentsHash); assertArrayEquals("Hash of groups and entries failed.", md.digest(), mPM.dbHeader.contentsHash);
} }
public void testHeader() throws Exception { public void testHeader() throws PwManagerOutputException, IOException {
ByteArrayOutputStream bActual = new ByteArrayOutputStream(); ByteArrayOutputStream bActual = new ByteArrayOutputStream();
PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, PwManagerOutput.DEBUG); PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, PwManagerOutput.DEBUG);
pActual.outputHeader(bActual); pActual.outputHeader(bActual);
@@ -80,17 +85,29 @@ public class PwManagerOutputTest extends TestCase {
assertArrayEquals("Header does not match.", bExpected.toByteArray(), bActual.toByteArray()); assertArrayEquals("Header does not match.", bExpected.toByteArray(), bActual.toByteArray());
} }
/* public void testFullWrite() throws IOException, PwManagerOutputException {
public void testEncryptedPart() throws Exception {
File file = new File("/sdcard/test1.kdb"); File file = new File("/sdcard/test1.kdb");
long length = file.length();
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
byte[] expected = new byte[(int)(length-PwDbHeader.BUF_SIZE)];
// Pull file into byte array (for streaming fun)
ByteArrayOutputStream bExpected = new ByteArrayOutputStream();
while (true) {
int data = fis.read();
if ( data == -1 ) {
break;
}
bExpected.write(data);
}
fis.skip(PwDbHeader.BUF_SIZE); ByteArrayOutputStream bActual = new ByteArrayOutputStream();
fis.read(expected); PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, PwManagerOutput.DEBUG);
pActual.output();
pActual.close();
bActual.close();
assertArrayEquals("Databases do not match.", bExpected.toByteArray(), bActual.toByteArray());
} }
*/
} }

View File

@@ -23,7 +23,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import org.bouncycastle1.crypto.InvalidCipherTextException; import org.bouncycastle.crypto.InvalidCipherTextException;
import org.phoneid.keepassj2me.ImporterV3; import org.phoneid.keepassj2me.ImporterV3;
import org.phoneid.keepassj2me.PwManager; import org.phoneid.keepassj2me.PwManager;