Skip to content

Commit

Permalink
Test deterministic keygen in standalone.
Browse files Browse the repository at this point in the history
  • Loading branch information
J08nY committed Aug 7, 2024
1 parent b805493 commit 6e399ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<String> lines1 = out1.lines().collect(Collectors.toList());
List<String> 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")
Expand Down

0 comments on commit 6e399ce

Please sign in to comment.