From 6e399ce3dec913be7728c2892301dd3eceb2cd83 Mon Sep 17 00:00:00 2001 From: J08nY Date: Wed, 7 Aug 2024 13:31:39 +0200 Subject: [PATCH] Test deterministic keygen in standalone. --- .../cz/crcs/ectester/common/util/Util.java | 2 +- .../standalone/ECTesterStandalone.java | 10 ++--- .../cz/crcs/ectester/standalone/AppTests.java | 39 +++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/cz/crcs/ectester/common/util/Util.java b/common/src/main/java/cz/crcs/ectester/common/util/Util.java index 9e6daa27..1d9bcf4d 100644 --- a/common/src/main/java/cz/crcs/ectester/common/util/Util.java +++ b/common/src/main/java/cz/crcs/ectester/common/util/Util.java @@ -35,7 +35,7 @@ public static int getVersion() { public static SecureRandom getRandom(byte[] seed) { SecureRandom random; try { - random = SecureRandom.getInstance("DRBG"); + random = SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException ignored) { return null; } diff --git a/standalone/src/main/java/cz/crcs/ectester/standalone/ECTesterStandalone.java b/standalone/src/main/java/cz/crcs/ectester/standalone/ECTesterStandalone.java index f358662f..44fc1b48 100644 --- a/standalone/src/main/java/cz/crcs/ectester/standalone/ECTesterStandalone.java +++ b/standalone/src/main/java/cz/crcs/ectester/standalone/ECTesterStandalone.java @@ -31,6 +31,7 @@ import cz.crcs.ectester.common.util.ByteUtil; import cz.crcs.ectester.common.util.ECUtil; import cz.crcs.ectester.common.util.FileUtil; +import cz.crcs.ectester.common.util.Util; import cz.crcs.ectester.data.EC_Store; import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; @@ -419,8 +420,7 @@ private void ecdh() throws NoSuchAlgorithmException, InvalidAlgorithmParameterEx if (cli.hasOption("ecdh.prng-seed")) { String seedString = cli.getOptionValue("ecdh.prng-seed"); byte[] seed = ByteUtil.hexToBytes(seedString, true); - random = SecureRandom.getInstance("DRBG"); - random.setSeed(seed); + random = Util.getRandom(seed); if (!lib.setupDeterministicPRNG(seed)) { System.err.println("Couldn't set PRNG seed."); return; @@ -549,8 +549,7 @@ private void ecdsa() throws NoSuchAlgorithmException, InvalidAlgorithmParameterE if (cli.hasOption("ecdsa.prng-seed")) { String seedString = cli.getOptionValue("ecdsa.prng-seed"); byte[] seed = ByteUtil.hexToBytes(seedString, true); - random = SecureRandom.getInstance("DRBG"); - random.setSeed(seed); + random = Util.getRandom(seed); if (!lib.setupDeterministicPRNG(seed)) { System.err.println("Couldn't set PRNG seed."); return; @@ -754,8 +753,7 @@ private void generate() throws NoSuchAlgorithmException, InvalidAlgorithmParamet if (cli.hasOption("generate.prng-seed")) { String seedString = cli.getOptionValue("generate.prng-seed"); byte[] seed = ByteUtil.hexToBytes(seedString, true); - random = SecureRandom.getInstance("DRBG"); - random.setSeed(seed); + random = Util.getRandom(seed); if (!lib.setupDeterministicPRNG(seed)) { System.err.println("Couldn't set PRNG seed."); return; diff --git a/standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java b/standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java index d401e58d..6072e68c 100644 --- a/standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java +++ b/standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java @@ -10,9 +10,11 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStream; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; @@ -85,6 +87,43 @@ String[] buildCLIArgs(String libName, String suite, String... additional) { return args.toArray(new String[]{}); } + @SuppressWarnings("JUnitMalformedDeclaration") + @ParameterizedTest + @MethodSource("libs") + @StdIo() + public void deterministicGenerate(String libName, StdOut out) { + String[] args = new String[]{"generate", "-ps", "123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234", "-n", "10", "-nc", "secg/secp256r1", libName}; + switch (libName) { + case "Botan": + case "Crypto++": + args = new String[]{"generate", "-ps", "123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234", "-n", "10", "-nc", "secg/secp256r1", "-t", "ECDH", libName}; + break; + case "Nettle": + case "libgcrypt": + case "wolfCrypt": + args = new String[]{"generate", "-ps", "123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234", "-n", "10", "-cn", "secp256r1", libName}; + break; + case "BoringSSL": + args = new String[]{"generate", "-ps", "123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234", "-n", "10", "-cn", "prime256v1", libName}; + break; + } + ECTesterStandalone.main(args); + String out1 = out.capturedString(); + ECTesterStandalone.main(args); + String out2 = out.capturedString().substring(out1.length()); + if (!out1.contains(";")) + return; + List lines1 = out1.lines().collect(Collectors.toList()); + List lines2 = out2.lines().collect(Collectors.toList()); + assertEquals(lines1.size(), lines2.size()); + for (int i = 0; i < lines1.size(); ++i) { + String[] parts1 = lines1.get(i).split(";"); + String[] parts2 = lines2.get(i).split(";"); + assertEquals(parts1[2], parts2[2]); + assertEquals(parts1[3], parts2[3]); + } + } + @SuppressWarnings("JUnitMalformedDeclaration") @ParameterizedTest @MethodSource("libs")