From 11d8fde12ea45f7debcd6fc5a448140b83a4c19a Mon Sep 17 00:00:00 2001 From: David Hook Date: Mon, 21 Jul 2014 17:35:37 +1000 Subject: compatibility updates --- .../bouncycastle/crypto/digests/SHA512tDigest.java | 2 +- .../crypto/engines/CramerShoupCoreEngine.java | 3 +- .../generators/CramerShoupParametersGenerator.java | 197 +++++++++++---------- .../bouncycastle/crypto/io/CipherInputStream.java | 4 +- .../crypto/params/ECNamedDomainParameters.java | 2 +- .../crypto/params/KDFCounterParameters.java | 8 +- .../crypto/signers/DSTU4145Signer.java | 2 +- .../math/ec/tools/DiscoverEndomorphisms.java | 2 +- 8 files changed, 115 insertions(+), 105 deletions(-) (limited to 'core/src/main/java') diff --git a/core/src/main/java/org/bouncycastle/crypto/digests/SHA512tDigest.java b/core/src/main/java/org/bouncycastle/crypto/digests/SHA512tDigest.java index 9abf73b4..d5848b17 100644 --- a/core/src/main/java/org/bouncycastle/crypto/digests/SHA512tDigest.java +++ b/core/src/main/java/org/bouncycastle/crypto/digests/SHA512tDigest.java @@ -10,7 +10,7 @@ import org.bouncycastle.util.Pack; public class SHA512tDigest extends LongDigest { - private final int digestLength; + private int digestLength; // non-final due to old flow analyser. private long H1t, H2t, H3t, H4t, H5t, H6t, H7t, H8t; 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 5fcfff9b..4c0db1b5 100644 --- a/core/src/main/java/org/bouncycastle/crypto/engines/CramerShoupCoreEngine.java +++ b/core/src/main/java/org/bouncycastle/crypto/engines/CramerShoupCoreEngine.java @@ -299,10 +299,9 @@ public class CramerShoupCoreEngine /** * CS exception for wrong cipher-texts */ - public class CramerShoupCiphertextException + public static class CramerShoupCiphertextException extends Exception { - private static final long serialVersionUID = -6360977166495345076L; public CramerShoupCiphertextException(String msg) diff --git a/core/src/main/java/org/bouncycastle/crypto/generators/CramerShoupParametersGenerator.java b/core/src/main/java/org/bouncycastle/crypto/generators/CramerShoupParametersGenerator.java index 704b1de4..4788a8ca 100644 --- a/core/src/main/java/org/bouncycastle/crypto/generators/CramerShoupParametersGenerator.java +++ b/core/src/main/java/org/bouncycastle/crypto/generators/CramerShoupParametersGenerator.java @@ -8,105 +8,116 @@ import org.bouncycastle.crypto.params.CramerShoupParameters; import org.bouncycastle.crypto.params.DHParameters; import org.bouncycastle.util.BigIntegers; -public class CramerShoupParametersGenerator { - - private int size; - private int certainty; - private SecureRandom random; - - /** - * Initialise the parameters generator. - * - * @param size - * bit length for the prime p - * @param certainty - * a measure of the uncertainty that the caller is willing to tolerate: - * the probability that the generated modulus is prime exceeds (1 - 1/2^certainty). - * The execution time of this method is proportional to the value of this parameter. - * @param random - * a source of randomness - */ - public void init(int size, int certainty, SecureRandom random) { - this.size = size; - this.certainty = certainty; - this.random = random; - } - - /** - * which generates the p and g values from the given parameters, returning - * the CramerShoupParameters object. - *

- * Note: can take a while... - */ - public CramerShoupParameters generateParameters() { - // - // find a safe prime p where p = 2*q + 1, where p and q are prime. - // - BigInteger[] safePrimes = ParametersHelper.generateSafePrimes(size, certainty, random); +public class CramerShoupParametersGenerator +{ + private static final BigInteger ONE = BigInteger.valueOf(1); + + private int size; + private int certainty; + private SecureRandom random; + + /** + * Initialise the parameters generator. + * + * @param size bit length for the prime p + * @param certainty a measure of the uncertainty that the caller is willing to tolerate: + * the probability that the generated modulus is prime exceeds (1 - 1/2^certainty). + * The execution time of this method is proportional to the value of this parameter. + * @param random a source of randomness + */ + public void init(int size, int certainty, SecureRandom random) + { + this.size = size; + this.certainty = certainty; + this.random = random; + } + + /** + * which generates the p and g values from the given parameters, returning + * the CramerShoupParameters object. + *

+ * Note: can take a while... + */ + public CramerShoupParameters generateParameters() + { + // + // find a safe prime p where p = 2*q + 1, where p and q are prime. + // + BigInteger[] safePrimes = ParametersHelper.generateSafePrimes(size, certainty, random); // BigInteger p = safePrimes[0]; - BigInteger q = safePrimes[1]; - BigInteger g1 = ParametersHelper.selectGenerator(q, random); - BigInteger g2 = ParametersHelper.selectGenerator(q, random); - while(g1.equals(g2)){ - g2 = ParametersHelper.selectGenerator(q, random); - } - - return new CramerShoupParameters(q, g1, g2, new SHA256Digest()); - } - - public CramerShoupParameters generateParameters(DHParameters dhParams){ - BigInteger p = dhParams.getP(); - BigInteger g1 = dhParams.getG(); - - // now we just need a second generator - BigInteger g2 = ParametersHelper.selectGenerator(p, random); - while(g1.equals(g2)){ - g2 = ParametersHelper.selectGenerator(p, random); - } - - return new CramerShoupParameters(p, g1, g2, new SHA256Digest()); - } - - private static class ParametersHelper { - - private static final BigInteger TWO = BigInteger.valueOf(2); - - /* - * Finds a pair of prime BigInteger's {p, q: p = 2q + 1} - * - * (see: Handbook of Applied Cryptography 4.86) - */ - static BigInteger[] generateSafePrimes(int size, int certainty, SecureRandom random) { - BigInteger p, q; - int qLength = size - 1; - - for (;;) { - q = new BigInteger(qLength, 2, random); - p = q.shiftLeft(1).add(BigInteger.ONE); - if (p.isProbablePrime(certainty) && (certainty <= 2 || q.isProbablePrime(certainty))) { - break; - } - } - - return new BigInteger[] { p, q }; - } - - static BigInteger selectGenerator(BigInteger p, SecureRandom random) { - BigInteger pMinusTwo = p.subtract(TWO); - BigInteger g; + BigInteger q = safePrimes[1]; + BigInteger g1 = ParametersHelper.selectGenerator(q, random); + BigInteger g2 = ParametersHelper.selectGenerator(q, random); + while (g1.equals(g2)) + { + g2 = ParametersHelper.selectGenerator(q, random); + } + + return new CramerShoupParameters(q, g1, g2, new SHA256Digest()); + } + + public CramerShoupParameters generateParameters(DHParameters dhParams) + { + BigInteger p = dhParams.getP(); + BigInteger g1 = dhParams.getG(); + + // now we just need a second generator + BigInteger g2 = ParametersHelper.selectGenerator(p, random); + while (g1.equals(g2)) + { + g2 = ParametersHelper.selectGenerator(p, random); + } + + return new CramerShoupParameters(p, g1, g2, new SHA256Digest()); + } + + private static class ParametersHelper + { + + private static final BigInteger TWO = BigInteger.valueOf(2); + + /* + * Finds a pair of prime BigInteger's {p, q: p = 2q + 1} + * + * (see: Handbook of Applied Cryptography 4.86) + */ + static BigInteger[] generateSafePrimes(int size, int certainty, SecureRandom random) + { + BigInteger p, q; + int qLength = size - 1; + + for (; ; ) + { + q = new BigInteger(qLength, 2, random); + p = q.shiftLeft(1).add(ONE); + if (p.isProbablePrime(certainty) && (certainty <= 2 || q.isProbablePrime(certainty))) + { + break; + } + } + + return new BigInteger[]{p, q}; + } + + static BigInteger selectGenerator(BigInteger p, SecureRandom random) + { + BigInteger pMinusTwo = p.subtract(TWO); + BigInteger g; /* - * RFC 2631 2.2.1.2 (and see: Handbook of Applied Cryptography 4.81) + * RFC 2631 2.2.1.2 (and see: Handbook of Applied Cryptography 4.81) */ - do { - BigInteger h = BigIntegers.createRandomInRange(TWO, pMinusTwo, random); + do + { + BigInteger h = BigIntegers.createRandomInRange(TWO, pMinusTwo, random); - g = h.modPow(TWO, p); - } while (g.equals(BigInteger.ONE)); + g = h.modPow(TWO, p); + } + while (g.equals(ONE)); - return g; - } - } + return g; + } + } } diff --git a/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java b/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java index 8d5b99b2..b06d1f53 100644 --- a/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java +++ b/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java @@ -25,8 +25,8 @@ public class CipherInputStream { private static final int INPUT_BUF_SIZE = 2048; - private final SkippingCipher skippingCipher; - private final byte[] inBuf; + private SkippingCipher skippingCipher; + private byte[] inBuf; private BufferedBlockCipher bufferedBlockCipher; private StreamCipher streamCipher; diff --git a/core/src/main/java/org/bouncycastle/crypto/params/ECNamedDomainParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/ECNamedDomainParameters.java index 6350806f..5b694bec 100644 --- a/core/src/main/java/org/bouncycastle/crypto/params/ECNamedDomainParameters.java +++ b/core/src/main/java/org/bouncycastle/crypto/params/ECNamedDomainParameters.java @@ -9,7 +9,7 @@ import org.bouncycastle.math.ec.ECPoint; public class ECNamedDomainParameters extends ECDomainParameters { - private final ASN1ObjectIdentifier name; + private ASN1ObjectIdentifier name; public ECNamedDomainParameters(ASN1ObjectIdentifier name, ECCurve curve, ECPoint G, BigInteger n) { diff --git a/core/src/main/java/org/bouncycastle/crypto/params/KDFCounterParameters.java b/core/src/main/java/org/bouncycastle/crypto/params/KDFCounterParameters.java index 8ff637da..29d8b369 100644 --- a/core/src/main/java/org/bouncycastle/crypto/params/KDFCounterParameters.java +++ b/core/src/main/java/org/bouncycastle/crypto/params/KDFCounterParameters.java @@ -34,10 +34,10 @@ public final class KDFCounterParameters implements DerivationParameters { - private final byte[] ki; - private final byte[] fixedInputDataCounterPrefix; - private final byte[] fixedInputDataCounterSuffix; - private final int r; + private byte[] ki; + private byte[] fixedInputDataCounterPrefix; + private byte[] fixedInputDataCounterSuffix; + private int r; /** * Base constructor - suffix fixed input data only. diff --git a/core/src/main/java/org/bouncycastle/crypto/signers/DSTU4145Signer.java b/core/src/main/java/org/bouncycastle/crypto/signers/DSTU4145Signer.java index d0b893a6..bceb8220 100644 --- a/core/src/main/java/org/bouncycastle/crypto/signers/DSTU4145Signer.java +++ b/core/src/main/java/org/bouncycastle/crypto/signers/DSTU4145Signer.java @@ -163,7 +163,7 @@ public class DSTU4145Signer { if (x.bitLength() > bitLength) { - x = x.mod(BigInteger.ONE.shiftLeft(bitLength)); + x = x.mod(ONE.shiftLeft(bitLength)); } return x; } diff --git a/core/src/main/java/org/bouncycastle/math/ec/tools/DiscoverEndomorphisms.java b/core/src/main/java/org/bouncycastle/math/ec/tools/DiscoverEndomorphisms.java index 4292da31..4ee2de60 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/tools/DiscoverEndomorphisms.java +++ b/core/src/main/java/org/bouncycastle/math/ec/tools/DiscoverEndomorphisms.java @@ -213,7 +213,7 @@ public class DiscoverEndomorphisms BigInteger s0 = ECConstants.ONE, s1 = ECConstants.ZERO; BigInteger t0 = ECConstants.ZERO, t1 = ECConstants.ONE; - while (r1.compareTo(BigInteger.ONE) > 0) + while (r1.compareTo(ECConstants.ONE) > 0) { BigInteger[] qr = r0.divideAndRemainder(r1); BigInteger q = qr[0], r2 = qr[1]; -- cgit v1.2.3