diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-06-21 12:50:18 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-06-21 12:50:18 +0400 |
commit | 63438700f77717abc0c5911eb92257757f5b9410 (patch) | |
tree | d236dee305a512f597260f4f8e75764ec421962a /core/src | |
parent | b002c53f2f34d6ee6d36147a18c23cfc8746353b (diff) |
Reverse the sense of 'forEncryption'
Remove debug output from test
Improve SecureRandom usage
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/engines/CramerShoupCoreEngine.java | 35 | ||||
-rw-r--r-- | core/src/test/java/org/bouncycastle/crypto/test/CramerShoupTest.java | 36 |
2 files changed, 35 insertions, 36 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/engines/CramerShoupCoreEngine.java b/core/src/main/java/org/bouncycastle/crypto/engines/CramerShoupCoreEngine.java index 71cab278..cc6d96be 100644 --- a/core/src/main/java/org/bouncycastle/crypto/engines/CramerShoupCoreEngine.java +++ b/core/src/main/java/org/bouncycastle/crypto/engines/CramerShoupCoreEngine.java @@ -23,43 +23,48 @@ public class CramerShoupCoreEngine { private static final BigInteger ONE = BigInteger.ONE; private CramerShoupKeyParameters key; + private SecureRandom random; private boolean forEncryption; private String label = null; /** * initialise the CramerShoup engine. * - * @param enc + * @param forEncryption * whether this engine should encrypt or decrypt * @param param * the necessary CramerShoup key parameters. * @param label * the label for labelled CS as {@link String} */ - public void init(boolean enc, CipherParameters param, String label) { - init(enc, param); + public void init(boolean forEncryption, CipherParameters param, String label) { + init(forEncryption, param); this.label = label; } - + /** * initialise the CramerShoup engine. * - * @param enc + * @param forEncryption * whether this engine should encrypt or decrypt * @param param * the necessary CramerShoup key parameters. */ - public void init(boolean enc, CipherParameters param) { + public void init(boolean forEncryption, CipherParameters param) { + SecureRandom providedRandom = null; + if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom) param; key = (CramerShoupKeyParameters) rParam.getParameters(); + providedRandom = rParam.getRandom(); } else { key = (CramerShoupKeyParameters) param; } - this.forEncryption = enc; + this.random = initSecureRandom(forEncryption, providedRandom); + this.forEncryption = forEncryption; } /** @@ -103,7 +108,7 @@ public class CramerShoupCoreEngine { public BigInteger convertInput(byte[] in, int inOff, int inLen) { if (inLen > (getInputBlockSize() + 1)) { throw new DataLengthException("input too large for Cramer Shoup cipher."); - } else if (inLen == (getInputBlockSize() + 1) && !forEncryption) { + } else if (inLen == (getInputBlockSize() + 1) && forEncryption) { throw new DataLengthException("input too large for Cramer Shoup cipher."); } @@ -128,7 +133,7 @@ public class CramerShoupCoreEngine { public byte[] convertOutput(BigInteger result) { byte[] output = result.toByteArray(); - if (forEncryption) { + if (!forEncryption) { if (output[0] == 0 && output.length > getOutputBlockSize()) { // have ended up with an extra zero byte, copy down. byte[] tmp = new byte[output.length - 1]; @@ -160,9 +165,8 @@ public class CramerShoupCoreEngine { public CramerShoupCiphertext encryptBlock(BigInteger input) { CramerShoupCiphertext result = null; - - if (!key.isPrivate() && !this.forEncryption && key instanceof CramerShoupPublicKeyParameters) { + if (!key.isPrivate() && this.forEncryption && key instanceof CramerShoupPublicKeyParameters) { CramerShoupPublicKeyParameters pk = (CramerShoupPublicKeyParameters)key; BigInteger p = pk.getParameters().getP(); BigInteger g1 = pk.getParameters().getG1(); @@ -173,7 +177,7 @@ public class CramerShoupCoreEngine { if (!isValidMessage(input, p)) return result; - BigInteger r = generateRandomElement(p, new SecureRandom()); + BigInteger r = generateRandomElement(p, random); BigInteger u1, u2, v, e, a; @@ -207,7 +211,7 @@ public class CramerShoupCoreEngine { BigInteger result = null; - if (key.isPrivate() && this.forEncryption && key instanceof CramerShoupPrivateKeyParameters) { + if (key.isPrivate() && !this.forEncryption && key instanceof CramerShoupPrivateKeyParameters) { CramerShoupPrivateKeyParameters sk = (CramerShoupPrivateKeyParameters)key; BigInteger p = sk.getParameters().getP(); @@ -251,6 +255,11 @@ public class CramerShoupCoreEngine { return m.compareTo(p) < 0; } + protected SecureRandom initSecureRandom(boolean needed, SecureRandom provided) + { + return !needed ? null : (provided != null) ? provided : new SecureRandom(); + } + /** * CS exception for wrong cipher-texts */ diff --git a/core/src/test/java/org/bouncycastle/crypto/test/CramerShoupTest.java b/core/src/test/java/org/bouncycastle/crypto/test/CramerShoupTest.java index 5b288284..2eb90152 100644 --- a/core/src/test/java/org/bouncycastle/crypto/test/CramerShoupTest.java +++ b/core/src/test/java/org/bouncycastle/crypto/test/CramerShoupTest.java @@ -16,7 +16,9 @@ import org.bouncycastle.util.BigIntegers; import org.bouncycastle.util.test.SimpleTest; public class CramerShoupTest extends SimpleTest { - + + private static final SecureRandom RND = new SecureRandom(); + private AsymmetricCipherKeyPair keyPair; public static void main(String[] args) { @@ -30,29 +32,18 @@ public class CramerShoupTest extends SimpleTest { @Override public void performTest() throws Exception { + BigInteger pSubOne = DHStandardGroups.rfc3526_2048.getP().subtract(BigInteger.ONE); + for (int i = 0; i < 10; ++i) { + BigInteger message = BigIntegers.createRandomInRange(BigInteger.ONE, pSubOne, RND); - int i = 0; - int numTests = 10; - BigInteger message = BigInteger.ONE; - - while (i < numTests){ - - message = BigIntegers.createRandomInRange(BigInteger.ONE, - DHStandardGroups.rfc3526_2048.getP().subtract(BigInteger.ONE), - new SecureRandom()); - BigInteger m1 = encDecTest(message); BigInteger m2 = labelledEncDecTest(message, "myRandomLabel"); BigInteger m3 = encDecEncodingTest(message); BigInteger m4 = labelledEncDecEncodingTest(message, "myOtherCoolLabel"); - - System.out.println(message); - + if (!message.equals(m1) || !message.equals(m2) || !message.equals(m3) || !message.equals(m4)){ fail("decrypted message != original message"); } - - ++i; } } @@ -87,9 +78,9 @@ public class CramerShoupTest extends SimpleTest { CramerShoupCoreEngine engine = new CramerShoupCoreEngine(); if (label != null) - engine.init(true, keyPair.getPrivate(), label); + engine.init(false, keyPair.getPrivate(), label); else - engine.init(true, keyPair.getPrivate()); + engine.init(false, keyPair.getPrivate()); try { BigInteger m = engine.decryptBlock(ciphertext); @@ -109,22 +100,21 @@ public class CramerShoupTest extends SimpleTest { CramerShoupKeyPairGenerator kpGen = new CramerShoupKeyPairGenerator(); CramerShoupParametersGenerator pGen = new CramerShoupParametersGenerator(); - pGen.init(2048, 1, new SecureRandom()); + pGen.init(2048, 1, RND); CramerShoupParameters params = pGen.generateParameters(DHStandardGroups.rfc3526_2048); - CramerShoupKeyGenerationParameters param = new CramerShoupKeyGenerationParameters(new SecureRandom(), params); + CramerShoupKeyGenerationParameters param = new CramerShoupKeyGenerationParameters(RND, params); kpGen.init(param); keyPair = kpGen.generateKeyPair(); CramerShoupCoreEngine engine = new CramerShoupCoreEngine(); if (label != null) - engine.init(false, keyPair.getPublic(), label); + engine.init(true, keyPair.getPublic(), label); else - engine.init(false, keyPair.getPublic()); + engine.init(true, keyPair.getPublic()); CramerShoupCiphertext ciphertext = engine.encryptBlock(message); return ciphertext; } - } |