From 060e1438c26d50980bd2d8ec33843310e1891830 Mon Sep 17 00:00:00 2001 From: Timothy Redaelli Date: Wed, 12 Aug 2015 02:32:45 +0200 Subject: [PATCH] Add support for Twofish in kdbx --- .../keepassdroid/crypto/CipherFactory.java | 16 +++- .../crypto/paddings/ZeroBytePadding.java | 73 +++++++++++++++++++ .../jce/provider/JCEBlockCipher.java | 8 +- 3 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 src/org/bouncycastle/crypto/paddings/ZeroBytePadding.java diff --git a/src/com/keepassdroid/crypto/CipherFactory.java b/src/com/keepassdroid/crypto/CipherFactory.java index 68c74135d..57aee96da 100644 --- a/src/com/keepassdroid/crypto/CipherFactory.java +++ b/src/com/keepassdroid/crypto/CipherFactory.java @@ -76,7 +76,10 @@ public class CipherFactory { new byte[]{(byte)0x31, (byte)0xC1, (byte)0xF2, (byte)0xE6, (byte)0xBF, (byte)0x71, (byte)0x43, (byte)0x50, (byte)0xBE, (byte)0x58, (byte)0x05, (byte)0x21, (byte)0x6A, (byte)0xFC, 0x5A, (byte)0xFF }); - + public static final UUID TWOFISH_CIPHER = Types.bytestoUUID( + new byte[]{(byte)0xAD, (byte)0x68, (byte)0xF2, (byte)0x9F, (byte)0x57, (byte)0x6F, (byte)0x4B, (byte)0xB9, + (byte)0xA3, (byte)0x6A, (byte)0xD4, (byte)0x7A, (byte)0xF9, (byte)0x65, (byte)0x34, (byte)0x6C + }); /** Generate appropriate cipher based on KeePass 2.x UUID's * @param uuid @@ -96,6 +99,17 @@ public class CipherFactory { cipher.init(opmode, new SecretKeySpec(key, "AES"), new IvParameterSpec(IV)); + return cipher; + } else if ( uuid.equals(TWOFISH_CIPHER) ) { + Cipher cipher; + if (opmode == Cipher.ENCRYPT_MODE) { + cipher = CipherFactory.getInstance("TWOFISH/CBC/ZeroBytePadding", androidOverride); + } else { + cipher = CipherFactory.getInstance("TWOFISH/CBC/NoPadding", androidOverride); + } + + cipher.init(opmode, new SecretKeySpec(key, "AES"), new IvParameterSpec(IV)); + return cipher; } diff --git a/src/org/bouncycastle/crypto/paddings/ZeroBytePadding.java b/src/org/bouncycastle/crypto/paddings/ZeroBytePadding.java new file mode 100644 index 000000000..c75602863 --- /dev/null +++ b/src/org/bouncycastle/crypto/paddings/ZeroBytePadding.java @@ -0,0 +1,73 @@ +package org.bouncycastle.crypto.paddings; + +import java.security.SecureRandom; + +import org.bouncycastle.crypto.InvalidCipherTextException; + +/** + * A padder that adds NULL byte padding to a block. + */ +public class ZeroBytePadding + implements BlockCipherPadding +{ + /** + * Initialise the padder. + * + * @param random - a SecureRandom if available. + */ + public void init(SecureRandom random) + throws IllegalArgumentException + { + // nothing to do. + } + + /** + * Return the name of the algorithm the padder implements. + * + * @return the name of the algorithm the padder implements. + */ + public String getPaddingName() + { + return "ZeroByte"; + } + + /** + * add the pad bytes to the passed in block, returning the + * number of bytes added. + */ + public int addPadding( + byte[] in, + int inOff) + { + int added = (in.length - inOff); + + while (inOff < in.length) + { + in[inOff] = (byte) 0; + inOff++; + } + + return added; + } + + /** + * return the number of pad bytes present in the block. + */ + public int padCount(byte[] in) + throws InvalidCipherTextException + { + int count = in.length; + + while (count > 0) + { + if (in[count - 1] != 0) + { + break; + } + + count--; + } + + return in.length - count; + } +} diff --git a/src/org/bouncycastle/jce/provider/JCEBlockCipher.java b/src/org/bouncycastle/jce/provider/JCEBlockCipher.java index 73448fd1b..a91080866 100644 --- a/src/org/bouncycastle/jce/provider/JCEBlockCipher.java +++ b/src/org/bouncycastle/jce/provider/JCEBlockCipher.java @@ -40,7 +40,9 @@ import org.bouncycastle.crypto.modes.OFBBlockCipher; import org.bouncycastle.crypto.modes.OpenPGPCFBBlockCipher; import org.bouncycastle.crypto.modes.PGPCFBBlockCipher; import org.bouncycastle.crypto.modes.SICBlockCipher; +*/ import org.bouncycastle.crypto.paddings.BlockCipherPadding; +/* import org.bouncycastle.crypto.paddings.ISO10126d2Padding; import org.bouncycastle.crypto.paddings.ISO7816d4Padding; */ @@ -48,8 +50,8 @@ import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; /* import org.bouncycastle.crypto.paddings.TBCPadding; import org.bouncycastle.crypto.paddings.X923Padding; -import org.bouncycastle.crypto.paddings.ZeroBytePadding; */ +import org.bouncycastle.crypto.paddings.ZeroBytePadding; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithIV; import org.bouncycastle.crypto.params.ParametersWithRandom; @@ -343,11 +345,11 @@ public class JCEBlockCipher extends WrapCipherSpi { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher()); } - /* else if (paddingName.equals("ZEROBYTEPADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ZeroBytePadding()); } + /* else if (paddingName.equals("ISO10126PADDING") || paddingName.equals("ISO10126-2PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO10126d2Padding()); @@ -1290,12 +1292,10 @@ public class JCEBlockCipher extends WrapCipherSpi this.cipher = new PaddedBufferedBlockCipher(cipher); } - /* BufferedGenericBlockCipher(BlockCipher cipher, BlockCipherPadding padding) { this.cipher = new PaddedBufferedBlockCipher(cipher, padding); } - */ public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException