Repository: incubator-pirk Updated Branches: refs/heads/master 8623cb3c3 -> 46ad9ce69
PIRK-2 -- enhanced Pallier acquisition of PRNG provider - closes apache/incubator-pirk#7 Project: http://git-wip-us.apache.org/repos/asf/incubator-pirk/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-pirk/commit/46ad9ce6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-pirk/tree/46ad9ce6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-pirk/diff/46ad9ce6 Branch: refs/heads/master Commit: 46ad9ce695020cef86a8f79d8f5d399427864a4a Parents: 8623cb3 Author: tellison <[email protected]> Authored: Tue Jul 19 08:18:46 2016 -0400 Committer: eawilliams <[email protected]> Committed: Tue Jul 19 08:18:46 2016 -0400 ---------------------------------------------------------------------- .../org/apache/pirk/encryption/Paillier.java | 27 ++++++++++++++------ src/main/resources/pirk.properties | 6 +++++ 2 files changed, 25 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-pirk/blob/46ad9ce6/src/main/java/org/apache/pirk/encryption/Paillier.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/pirk/encryption/Paillier.java b/src/main/java/org/apache/pirk/encryption/Paillier.java index 5a5ac6d..b85962c 100644 --- a/src/main/java/org/apache/pirk/encryption/Paillier.java +++ b/src/main/java/org/apache/pirk/encryption/Paillier.java @@ -20,6 +20,7 @@ package org.apache.pirk.encryption; import java.io.Serializable; import java.math.BigInteger; +import java.security.GeneralSecurityException; import java.security.SecureRandom; import org.apache.log4j.Logger; @@ -70,17 +71,27 @@ public class Paillier implements Serializable private static Logger logger = LogUtils.getLoggerForThisClass(); - private static final SecureRandom nativePRNGSecureRandom; + private static final SecureRandom secureRandom; static { try { - nativePRNGSecureRandom = SecureRandom.getInstance("NativePRNG"); - } catch (Exception e) + String alg = SystemConfiguration.getProperty("pallier.secureRandom.algorithm"); + if (alg == null) + { + secureRandom = new SecureRandom(); + } + else + { + String provider = SystemConfiguration.getProperty("pallier.secureRandom.provider"); + secureRandom = (provider == null) ? SecureRandom.getInstance(alg) : SecureRandom.getInstance(alg, provider); + } + logger.info("Using secure random from " + secureRandom.getProvider().getName() + ":" + secureRandom.getAlgorithm()); + } catch (GeneralSecurityException e) { - logger.error("Unable to instantiate a SecureRandom object with the NativePRNG algorithm.", e); - throw new RuntimeException("Unable to instantiate a SecureRandom object with the NativePRNG algorithm.", e); + logger.error("Unable to instantiate a SecureRandom object with the requested algorithm.", e); + throw new RuntimeException("Unable to instantiate a SecureRandom object with the requested algorithm.", e); } } @@ -237,7 +248,7 @@ public class Paillier implements Serializable private void getKeys(int certainty) { // Generate the primes - BigInteger[] pq = PrimeGenerator.getPrimePair(bitLength, certainty, nativePRNGSecureRandom); + BigInteger[] pq = PrimeGenerator.getPrimePair(bitLength, certainty, secureRandom); p = pq[0]; q = pq[1]; @@ -261,10 +272,10 @@ public class Paillier implements Serializable public BigInteger encrypt(BigInteger m) throws PIRException { // Generate a random value r in (Z/NZ)* - BigInteger r = (new BigInteger(bitLength, nativePRNGSecureRandom)).mod(N); + BigInteger r = (new BigInteger(bitLength, secureRandom)).mod(N); while (r.mod(p).equals(BigInteger.ZERO) || r.mod(q).equals(BigInteger.ZERO) || r.equals(BigInteger.ONE) || r.equals(BigInteger.ZERO)) { - r = (new BigInteger(bitLength, nativePRNGSecureRandom)).mod(N); + r = (new BigInteger(bitLength, secureRandom)).mod(N); } return encrypt(m, r); http://git-wip-us.apache.org/repos/asf/incubator-pirk/blob/46ad9ce6/src/main/resources/pirk.properties ---------------------------------------------------------------------- diff --git a/src/main/resources/pirk.properties b/src/main/resources/pirk.properties index 1b1339c..cf2054f 100755 --- a/src/main/resources/pirk.properties +++ b/src/main/resources/pirk.properties @@ -157,6 +157,12 @@ paillier.GMPConstantTimeMode = false # These checks slow down prime generation considerably pallier.FIPSPrimeGenerationChecks = true +## These properties control the secure random number generator algorithm and provider. +## You can specify just the algorithm, or both algorithm and provider. The system's +## default secure random is used when the algorithm is left unspecified. +pallier.secureRandom.algorithm=NativePRNG +#pallier.secureRandom.provider=SUN + ## ## Properties for PIR query and response ##
