Skip to content

Commit 51f1186

Browse files
committed
Add number and shuffle params to standalone.
1 parent 9946667 commit 51f1186

8 files changed

+108
-103
lines changed

standalone/src/main/java/cz/crcs/ectester/standalone/ECTesterStandalone.java

+2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ private TreeCommandLine parseArgs(String[] args) throws ParseException {
258258
testOpts.addOption(Option.builder("kt").longOpt("ka-type").desc("Set the KeyAgreement object [type].").hasArg().argName("type").optionalArg(false).build());
259259
testOpts.addOption(Option.builder("st").longOpt("sig-type").desc("Set the Signature object [type].").hasArg().argName("type").optionalArg(false).build());
260260
testOpts.addOption(Option.builder("f").longOpt("format").desc("Set the output format, one of text,yaml,xml.").hasArg().argName("format").optionalArg(false).build());
261+
testOpts.addOption(Option.builder("n").longOpt("number").desc("Number of repeats during testing.").hasArg().argName("number").optionalArg(false).build());
262+
testOpts.addOption(Option.builder("s").longOpt("shuffle").desc("Shuffle the test suite before running it.").build());
261263
testOpts.addOption(Option.builder().longOpt("key-type").desc("Set the key [algorithm] for which the key should be derived in KeyAgreements with KDF. Default is \"AES\".").hasArg().argName("algorithm").optionalArg(false).build());
262264
List<Argument> testArgs = new LinkedList<>();
263265
testArgs.add(new Argument("test-suite", "The test suite to run.", true));

standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneCofactorSuite.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,14 @@ protected void runTests() throws Exception {
7171
Test keyAgreement = KeyAgreementTest.expectError(testable, Result.ExpectedValue.FAILURE);
7272
specificKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, pub.getId() + " cofactor key test (" + pub.getDesc() + ").", keyAgreement));
7373
}
74-
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform " + kaIdent.getName() + " with public points on non-generator subgroup.", specificKaTests.toArray(new Test[0])));
74+
for (int i = 0; i < getNumRepeats(); i++) {
75+
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform " + kaIdent.getName() + " with public points on non-generator subgroup.", specificKaTests.toArray(new Test[0])));
76+
}
7577
}
7678
}
79+
if (cli.hasOption("test.shuffle")) {
80+
Collections.shuffle(allKaTests);
81+
}
7782
if (allKaTests.isEmpty()) {
7883
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "None of the specified key agreement types is supported by the library."));
7984
}

standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneCompositeSuite.java

+8-41
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212
import cz.crcs.ectester.standalone.ECTesterStandalone;
1313
import cz.crcs.ectester.standalone.consts.KeyAgreementIdent;
1414
import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent;
15-
import cz.crcs.ectester.standalone.consts.SignatureIdent;
1615
import cz.crcs.ectester.standalone.test.base.*;
1716

1817
import javax.crypto.KeyAgreement;
19-
import java.security.KeyPair;
2018
import java.security.KeyPairGenerator;
21-
import java.security.Signature;
22-
import java.security.interfaces.ECPrivateKey;
2319
import java.security.interfaces.ECPublicKey;
2420
import java.security.spec.ECParameterSpec;
2521
import java.util.*;
@@ -79,9 +75,14 @@ protected void runTests() throws Exception {
7975
Test keyAgreement = KeyAgreementTest.expectError(testable, Result.ExpectedValue.FAILURE);
8076
specificKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Composite test of " + curve.getId() + ", with generated private key, " + pub.getDesc(), keyAgreement));
8177
}
82-
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform " + kaIdent.getName() + " with various public points.", specificKaTests.toArray(new Test[0])));
78+
for (int i = 0; i < getNumRepeats(); i++) {
79+
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform " + kaIdent.getName() + " with various public points.", specificKaTests.toArray(new Test[0])));
80+
}
8381
}
8482
}
83+
if (cli.hasOption("test.shuffle")) {
84+
Collections.shuffle(allKaTests);
85+
}
8586
if (allKaTests.isEmpty()) {
8687
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "None of the specified key agreement types is supported by the library."));
8788
}
@@ -123,49 +124,15 @@ protected void runTests() throws Exception {
123124
testGroup(rg0Curves, kpg, null, Result.ExpectedValue.ANY);
124125
}
125126

126-
private void testGroup(List<EC_Curve> curves, KeyPairGenerator kpg, String testName, Result.ExpectedValue dhValue) throws Exception {
127+
private void testGroup(List<EC_Curve> curves, KeyPairGenerator kpg, String testName, Result.ExpectedValue expected) throws Exception {
127128
for (EC_Curve curve : curves) {
128129
String description;
129130
if (testName == null) {
130131
description = curve.getDesc() + " test of " + curve.getId() + ".";
131132
} else {
132133
description = testName + " test of " + curve.getId() + ".";
133134
}
134-
135-
//generate KeyPair
136-
KeyGeneratorTestable kgt = KeyGeneratorTestable.builder().keyPairGenerator(kpg).spec(curve.toSpec()).build();
137-
Test generate = KeyGeneratorTest.expectError(kgt, Result.ExpectedValue.ANY);
138-
139-
//perform KeyAgreement tests
140-
List<Test> kaTests = new LinkedList<>();
141-
for (KeyAgreementIdent kaIdent : cfg.selected.getKAs()) {
142-
if (kaAlgo == null || kaIdent.containsAny(kaTypes)) {
143-
KeyAgreement ka = kaIdent.getInstance(cfg.selected.getProvider());
144-
KeyAgreementTestable testable = KeyAgreementTestable.builder().ka(ka).publicKgt(kgt).privateKgt(kgt).random(getRandom()).build();
145-
kaTests.add(KeyAgreementTest.expectError(testable, dhValue));
146-
}
147-
}
148-
if (kaTests.isEmpty()) {
149-
kaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "None of the specified KeyAgreement types is supported by the library."));
150-
}
151-
152-
//perform Signature tests
153-
List<Test> sigTests = new LinkedList<>();
154-
for (SignatureIdent sigIdent : cfg.selected.getSigs()) {
155-
if (sigAlgo == null || sigIdent.containsAny(sigTypes)) {
156-
Signature sig = sigIdent.getInstance(cfg.selected.getProvider());
157-
byte[] data = sigIdent.toString().getBytes();
158-
SignatureTestable testable = new SignatureTestable(sig, kgt, data, getRandom());
159-
sigTests.add(SignatureTest.expectError(testable, dhValue));
160-
}
161-
}
162-
if (sigTests.isEmpty()) {
163-
sigTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "None of the specified Signature types is supported by the library."));
164-
}
165-
166-
Test performKeyAgreements = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform specified KeyAgreements.", kaTests.toArray(new Test[0]));
167-
Test performSignatures = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform specified Signatures.", sigTests.toArray(new Test[0]));
168-
doTest(CompoundTest.function(CompoundTest.EXPECT_ALL_SUCCESS, CompoundTest.RUN_ALL_IF_FIRST, description, generate, performKeyAgreements, performSignatures));
135+
testCurve(curve, kpg, expected, description, kaAlgo, sigAlgo, kaTypes, sigTypes);
169136
}
170137
}
171138
}

standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneEdgeCasesSuite.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public Result apply(KeyAgreementTestable testable) {
163163
Test fullS = ecdhTest(kgt, full, spec, "ECDH with S = 111111111...11111 (but < r).", Result.ExpectedValue.SUCCESS);
164164
Test smallerS = ecdhTest(kgt, smaller, spec, "ECDH with S < r.", Result.ExpectedValue.SUCCESS);
165165
Test exactS = ecdhTest(kgt, R, spec, "ECDH with S = r.", Result.ExpectedValue.FAILURE);
166-
Test largeS = ecdhTest(kgt, larger, spec, "ECDH with S > r.", Result.ExpectedValue.ANY);
166+
Test largerS = ecdhTest(kgt, larger, spec, "ECDH with S > r.", Result.ExpectedValue.ANY);
167167
Test rm1S = ecdhTest(kgt, rm1, spec, "ECDH with S = r - 1.", Result.ExpectedValue.SUCCESS);
168168
Test rp1S = ecdhTest(kgt, rp1, spec, "ECDH with S = r + 1.", Result.ExpectedValue.ANY);
169169

@@ -179,8 +179,15 @@ public Result apply(KeyAgreementTestable testable) {
179179
Test krm1S = ecdhTest(kgt, krm1, spec, "ECDH with S = (k * r) - 1.", kExpected);
180180
Test krp1S = ecdhTest(kgt, krp1, spec, "ECDH with S = (k * r) + 1.", Result.ExpectedValue.ANY);
181181

182+
List<Test> tests = new LinkedList<>();
183+
for (int i = 0; i < getNumRepeats(); ++i) {
184+
tests.addAll(Arrays.asList(zeroS, oneS, alternateS, alternateOtherS, fullS, smallerS, exactS, largerS, rm1S, rp1S, krS, krm1S, krp1S));
185+
}
186+
if (cli.hasOption("test.shuffle"))
187+
Collections.shuffle(tests);
188+
tests.add(0, generate);
182189
doTest(CompoundTest.function(CompoundTest.EXPECT_ALL_SUCCESS, CompoundTest.RUN_ALL_IF_FIRST, "Tests with edge-case private key values over " + curve.getId() + ".",
183-
generate, zeroS, oneS, alternateS, alternateOtherS, fullS, smallerS, exactS, largeS, rm1S, rp1S, krS, krm1S, krp1S));
190+
generate, zeroS, oneS, alternateS, alternateOtherS, fullS, smallerS, exactS, largerS, rm1S, rp1S, krS, krm1S, krp1S));
184191
}
185192

186193
EC_Curve secp160r1 = EC_Store.getInstance().getObject(EC_Curve.class, "secg/secp160r1");
@@ -241,7 +248,16 @@ public Result apply(KeyAgreementTestable testable) {
241248
}
242249
}
243250
Test rTest = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Near r.", rTests);
244-
doTest(CompoundTest.function(CompoundTest.EXPECT_ALL_SUCCESS, CompoundTest.RUN_ALL_IF_FIRST, "Test private key values near zero, near p and near/larger than the order.", generate, zeroTest, pTest, rTest));
251+
252+
List<Test> tests160 = new LinkedList<>();
253+
for (int j = 0; j < getNumRepeats(); ++j) {
254+
tests160.addAll(Arrays.asList(zeroTest, pTest, rTest));
255+
}
256+
if (cli.hasOption("test.shuffle"))
257+
Collections.shuffle(tests160);
258+
tests160.add(0, generate);
259+
260+
doTest(CompoundTest.function(CompoundTest.EXPECT_ALL_SUCCESS, CompoundTest.RUN_ALL_IF_FIRST, "Test private key values near zero, near p and near/larger than the order.", tests160.toArray(new Test[0])));
245261
}
246262

247263
private Test ecdhTest(KeyGeneratorTestable kgt, BigInteger SParam, ECParameterSpec spec, String desc, Result.ExpectedValue expect) throws NoSuchAlgorithmException {

standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneForeignSuite.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public StandaloneForeignSuite(TestWriter writer, ECTesterStandalone.Config cfg,
3939
protected void runTests() throws Exception {
4040
String kpgAlgo = cli.getOptionValue("test.kpg-type");
4141
String kaAlgo = cli.getOptionValue("test.ka-type");
42+
4243
List<String> kaTypes = kaAlgo != null ? Arrays.asList(kaAlgo.split(",")) : new ArrayList<>();
4344

4445
KeyPairGeneratorIdent kpgIdent = getKeyPairGeneratorIdent(kpgAlgo);
@@ -160,9 +161,14 @@ public int getKeysize() {
160161
Test keyAgreement = KeyAgreementTest.expectError(testable, Result.ExpectedValue.FAILURE);
161162
specificKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, pub.getId() + " invalid key test.", keyAgreement));
162163
}
163-
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform " + kaIdent.getName() + " with invalid public points.", specificKaTests.toArray(new Test[0])));
164+
for (int i = 0; i < getNumRepeats(); i++) {
165+
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform " + kaIdent.getName() + " with invalid public points.", specificKaTests.toArray(new Test[0])));
166+
}
164167
}
165168
}
169+
if (cli.hasOption("test.shuffle")) {
170+
Collections.shuffle(allKaTests);
171+
}
166172
if (allKaTests.isEmpty()) {
167173
allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "None of the specified key agreement types is supported by the library."));
168174
}

standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneMiscSuite.java

+2-53
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,15 @@
33
import cz.crcs.ectester.common.cli.TreeCommandLine;
44
import cz.crcs.ectester.common.ec.EC_Curve;
55
import cz.crcs.ectester.common.output.TestWriter;
6-
import cz.crcs.ectester.common.test.CompoundTest;
76
import cz.crcs.ectester.common.test.Result;
8-
import cz.crcs.ectester.common.test.Test;
9-
import cz.crcs.ectester.common.util.ByteUtil;
10-
import cz.crcs.ectester.common.util.ECUtil;
117
import cz.crcs.ectester.data.EC_Store;
128
import cz.crcs.ectester.standalone.ECTesterStandalone;
13-
import cz.crcs.ectester.standalone.consts.KeyAgreementIdent;
149
import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent;
15-
import cz.crcs.ectester.standalone.consts.SignatureIdent;
16-
import cz.crcs.ectester.standalone.test.base.*;
1710

18-
import javax.crypto.KeyAgreement;
19-
import java.security.KeyPair;
2011
import java.security.KeyPairGenerator;
2112
import java.security.NoSuchAlgorithmException;
22-
import java.security.Signature;
23-
import java.security.interfaces.ECPrivateKey;
24-
import java.security.interfaces.ECPublicKey;
2513
import java.util.*;
2614

27-
import static cz.crcs.ectester.common.util.ECUtil.hashCurve;
28-
2915
/**
3016
* @author David Hofman
3117
*/
@@ -76,49 +62,12 @@ protected void runTests() throws Exception {
7662
testCurves(bnCurves.values(), "Barreto-Naehrig", kpg, Result.ExpectedValue.SUCCESS);
7763
testCurves(mntCurves.values(), "MNT", kpg, Result.ExpectedValue.SUCCESS);
7864
testCurves(mCurves, "Montgomery", kpg, Result.ExpectedValue.SUCCESS);
79-
testCurve(curve25519, "Montgomery", kpg, Result.ExpectedValue.SUCCESS);
80-
}
81-
82-
private void testCurve(EC_Curve curve, String catName, KeyPairGenerator kpg, Result.ExpectedValue expected) throws NoSuchAlgorithmException {
83-
//generate KeyPair
84-
KeyGeneratorTestable kgt = KeyGeneratorTestable.builder().keyPairGenerator(kpg).spec(curve.toSpec()).random(getRandom()).build();
85-
Test generate = KeyGeneratorTest.expectError(kgt, Result.ExpectedValue.ANY);
86-
87-
//perform KeyAgreement tests
88-
List<Test> kaTests = new LinkedList<>();
89-
for (KeyAgreementIdent kaIdent : cfg.selected.getKAs()) {
90-
if (kaAlgo == null || kaIdent.containsAny(kaTypes)) {
91-
KeyAgreement ka = kaIdent.getInstance(cfg.selected.getProvider());
92-
KeyAgreementTestable testable = KeyAgreementTestable.builder().ka(ka).publicKgt(kgt).privateKgt(kgt).random(getRandom()).build();
93-
kaTests.add(KeyAgreementTest.expectError(testable, expected));
94-
}
95-
}
96-
if (kaTests.isEmpty()) {
97-
kaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "None of the specified KeyAgreement types is supported by the library."));
98-
}
99-
100-
//perform Signature tests
101-
List<Test> sigTests = new LinkedList<>();
102-
for (SignatureIdent sigIdent : cfg.selected.getSigs()) {
103-
if (sigAlgo == null || sigIdent.containsAny(sigTypes)) {
104-
Signature sig = sigIdent.getInstance(cfg.selected.getProvider());
105-
byte[] data = sigIdent.toString().getBytes();
106-
SignatureTestable testable = new SignatureTestable(sig, kgt, data, getRandom());
107-
sigTests.add(SignatureTest.expectError(testable, expected));
108-
}
109-
}
110-
if (sigTests.isEmpty()) {
111-
sigTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "None of the specified Signature types is supported by the library."));
112-
}
113-
114-
Test performKeyAgreements = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform specified KeyAgreements.", kaTests.toArray(new Test[0]));
115-
Test performSignatures = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Perform specified Signatures.", sigTests.toArray(new Test[0]));
116-
doTest(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Tests over " + curve.getBits() + "b " + catName + " curve: " + curve.getId() + ".", generate, performKeyAgreements, performSignatures));
65+
testCurve(curve25519, kpg, Result.ExpectedValue.SUCCESS, "Tests over Curve25519.", kaAlgo, sigAlgo, kaTypes, sigTypes);
11766
}
11867

11968
private void testCurves(Collection<EC_Curve> curves, String catName, KeyPairGenerator kpg, Result.ExpectedValue expected) throws NoSuchAlgorithmException {
12069
for (EC_Curve curve : curves) {
121-
testCurve(curve, catName, kpg, expected);
70+
testCurve(curve, kpg, expected, "Tests over " + curve.getBits() + "b " + catName + " curve: " + curve.getId() + ".", kaAlgo, sigAlgo, kaTypes, sigTypes);
12271
}
12372
}
12473
}

standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneSignatureSuite.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,21 @@ protected void runTests() throws Exception {
4242
List<EC_SigResult> nok = groups.entrySet().stream().filter((e) -> e.getKey().equals("nok")).findFirst().get().getValue();
4343

4444
byte[] data = "Some stuff that is not the actual data".getBytes();
45-
for (EC_SigResult sig : nok) {
46-
ecdsaTest(sig, sigIdent, Result.ExpectedValue.FAILURE, data);
45+
for (int i = 0; i < getNumRepeats(); ++i) {
46+
if (cli.hasOption("test.shuffle"))
47+
Collections.shuffle(nok);
48+
for (EC_SigResult sig : nok) {
49+
ecdsaTest(sig, sigIdent, Result.ExpectedValue.FAILURE, data);
50+
}
4751
}
4852

4953
List<EC_SigResult> ok = groups.entrySet().stream().filter((e) -> e.getKey().equals("ok")).findFirst().get().getValue();
50-
for (EC_SigResult sig : ok) {
51-
ecdsaTest(sig, sigIdent, Result.ExpectedValue.SUCCESS, null);
54+
for (int i = 0; i < getNumRepeats(); ++i) {
55+
if (cli.hasOption("test.shuffle"))
56+
Collections.shuffle(ok);
57+
for (EC_SigResult sig : ok) {
58+
ecdsaTest(sig, sigIdent, Result.ExpectedValue.SUCCESS, null);
59+
}
5260
}
5361
}
5462

0 commit comments

Comments
 (0)