From bccbb8b0a9df3ea6d0e2246907a40c1be665a385 Mon Sep 17 00:00:00 2001 From: Dusan Klinec Date: Tue, 19 May 2020 17:14:04 +0200 Subject: [PATCH] add option to randomize RandomData seed - if the System property `com.licel.jcardsim.randomdata.seed` is set, the hex-decoded value of the property is added as a seed material to the RandomData on initialization - else if the System property `com.licel.jcardsim.randomdata.secure` is set to `1`, the SecureRandom is used to generate 32 random bytes that are added as a seed material to the RandomData - else the original behavior is preserved to be consistent with previous versions (some tests might rely on the fixed randomness) --- .../com/licel/jcardsim/crypto/RandomDataImpl.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/com/licel/jcardsim/crypto/RandomDataImpl.java b/src/main/java/com/licel/jcardsim/crypto/RandomDataImpl.java index 4a5165d2..983adffb 100644 --- a/src/main/java/com/licel/jcardsim/crypto/RandomDataImpl.java +++ b/src/main/java/com/licel/jcardsim/crypto/RandomDataImpl.java @@ -21,6 +21,9 @@ import org.bouncycastle.crypto.digests.SHA1Digest; import org.bouncycastle.crypto.prng.DigestRandomGenerator; import org.bouncycastle.crypto.prng.RandomGenerator; +import org.bouncycastle.util.encoders.Hex; + +import java.security.SecureRandom; /** * Implementation RandomData based @@ -34,6 +37,18 @@ public class RandomDataImpl extends RandomData { public RandomDataImpl(byte algorithm) { this.algorithm = algorithm; this.engine = new DigestRandomGenerator(new SHA1Digest()); + + final String randomSeed = System.getProperty("com.licel.jcardsim.randomdata.seed"); + final String doSecureRandom = System.getProperty("com.licel.jcardsim.randomdata.secure", "0"); + if (randomSeed != null){ + this.engine.addSeedMaterial(Hex.decode(randomSeed)); + } + else if ("1".equals(doSecureRandom)){ + byte[] seed = new byte[32]; + SecureRandom randomGenerator = new SecureRandom(); + randomGenerator.nextBytes(seed); + this.engine.addSeedMaterial(seed); + } } public void generateData(byte[] buffer, short offset, short length) throws CryptoException {