diff options
| author | J08nY | 2024-08-02 14:32:31 +0200 |
|---|---|---|
| committer | J08nY | 2024-08-02 14:32:31 +0200 |
| commit | 386e01e2a47557acc9440001d40314374e5d982f (patch) | |
| tree | 7ee68686b86b953ea4fc76de9b0560fb0ef6f1bc | |
| parent | 70b95b66ba8410c6e18c3f19449d409ab7f48c1e (diff) | |
| download | ECTester-386e01e2a47557acc9440001d40314374e5d982f.tar.gz ECTester-386e01e2a47557acc9440001d40314374e5d982f.tar.zst ECTester-386e01e2a47557acc9440001d40314374e5d982f.zip | |
3 files changed, 60 insertions, 26 deletions
diff --git a/common/src/main/java/cz/crcs/ectester/common/test/CompoundTest.java b/common/src/main/java/cz/crcs/ectester/common/test/CompoundTest.java index ba4ad4f..ccb0f21 100644 --- a/common/src/main/java/cz/crcs/ectester/common/test/CompoundTest.java +++ b/common/src/main/java/cz/crcs/ectester/common/test/CompoundTest.java @@ -2,6 +2,7 @@ package cz.crcs.ectester.common.test; import java.util.Arrays; import java.util.Objects; +import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; @@ -16,13 +17,22 @@ public class CompoundTest extends Test implements Cloneable { private Test[] tests; private String description = ""; - private final static Consumer<Test[]> RUN_ALL = tests -> { + public final static BiFunction<Result.ExpectedValue, Test[], Result> EXPECT_ALL = (what, tests) -> { + for (Test test : tests) { + if (!Result.Value.fromExpected(what, test.ok()).ok()) { + return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result."); + } + } + return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result."); + }; + + public final static Consumer<Test[]> RUN_ALL = tests -> { for (Test t : tests) { t.run(); } }; - private final static Consumer<Test[]> RUN_GREEDY_ALL = tests -> { + public final static Consumer<Test[]> RUN_GREEDY_ALL = tests -> { for (Test t : tests) { t.run(); if (!t.ok()) { @@ -31,7 +41,7 @@ public class CompoundTest extends Test implements Cloneable { } }; - private final static Consumer<Test[]> RUN_GREEDY_ANY = tests -> { + public final static Consumer<Test[]> RUN_GREEDY_ANY = tests -> { for (Test t : tests) { t.run(); if (t.ok()) { @@ -68,14 +78,7 @@ public class CompoundTest extends Test implements Cloneable { } private static CompoundTest expectAll(Result.ExpectedValue what, Consumer<Test[]> runCallback, Test[] all) { - return new CompoundTest((tests) -> { - for (Test test : tests) { - if (!Result.Value.fromExpected(what, test.ok()).ok()) { - return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result."); - } - } - return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result."); - }, runCallback, all); + return new CompoundTest((tests) -> EXPECT_ALL.apply(what, tests), runCallback, all); } public static CompoundTest all(Result.ExpectedValue what, Test... all) { diff --git a/standalone/src/main/java/cz/crcs/ectester/standalone/test/base/KeyAgreementTestable.java b/standalone/src/main/java/cz/crcs/ectester/standalone/test/base/KeyAgreementTestable.java index 7fd1c5a..24c25c5 100644 --- a/standalone/src/main/java/cz/crcs/ectester/standalone/test/base/KeyAgreementTestable.java +++ b/standalone/src/main/java/cz/crcs/ectester/standalone/test/base/KeyAgreementTestable.java @@ -46,7 +46,7 @@ public class KeyAgreementTestable extends StandaloneTestable<KeyAgreementTestabl } public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable kgt, ECPrivateKey privateKey, ECParameterSpec spec) { - this(ka, privateKey, null, spec); + this(ka, privateKey, (ECPublicKey) null, spec); this.kgtPublic = kgt; } @@ -56,7 +56,7 @@ public class KeyAgreementTestable extends StandaloneTestable<KeyAgreementTestabl } public KeyAgreementTestable(KeyAgreement ka, ECPublicKey publicKey, KeyGeneratorTestable kgt, ECParameterSpec spec) { - this(ka, null, publicKey, spec); + this(ka, (ECPrivateKey) null, publicKey, spec); this.kgtPrivate = kgt; } @@ -66,7 +66,7 @@ public class KeyAgreementTestable extends StandaloneTestable<KeyAgreementTestabl } public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable privKgt, KeyGeneratorTestable pubKgt, ECParameterSpec spec) { - this(ka, (ECPrivateKey) null, null, spec); + this(ka, (ECPrivateKey) null, (ECPublicKey) null, spec); this.kgtPrivate = privKgt; this.kgtPublic = pubKgt; } @@ -76,6 +76,37 @@ public class KeyAgreementTestable extends StandaloneTestable<KeyAgreementTestabl this.keyAlgo = keyAlgo; } + public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable kgt, ECPrivateKey privateKey) { + this(ka, privateKey, null, (ECParameterSpec) null); + this.kgtPublic = kgt; + } + + public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable kgt, ECPrivateKey privateKey, String keyAlgo) { + this(ka, kgt, privateKey, (ECParameterSpec) null); + this.keyAlgo = keyAlgo; + } + + public KeyAgreementTestable(KeyAgreement ka, ECPublicKey publicKey, KeyGeneratorTestable kgt) { + this(ka, null, publicKey, (ECParameterSpec) null); + this.kgtPrivate = kgt; + } + + public KeyAgreementTestable(KeyAgreement ka, ECPublicKey publicKey, KeyGeneratorTestable kgt, String keyAlgo) { + this(ka, publicKey, kgt, (ECParameterSpec) null); + this.keyAlgo = keyAlgo; + } + + public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable privKgt, KeyGeneratorTestable pubKgt) { + this(ka, (ECPrivateKey) null, (ECPublicKey) null, (ECParameterSpec) null); + this.kgtPrivate = privKgt; + this.kgtPublic = pubKgt; + } + + public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable privKgt, KeyGeneratorTestable pubKgt, String keyAlgo) { + this(ka, privKgt, pubKgt, (ECParameterSpec) null); + this.keyAlgo = keyAlgo; + } + public String getKeyAlgorithm() { return keyAlgo; } diff --git a/standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneCofactorSuite.java b/standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneCofactorSuite.java index bb9a509..f36bc98 100644 --- a/standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneCofactorSuite.java +++ b/standalone/src/main/java/cz/crcs/ectester/standalone/test/suites/StandaloneCofactorSuite.java @@ -24,6 +24,8 @@ import java.security.interfaces.ECPrivateKey; import java.security.interfaces.ECPublicKey; import java.security.spec.ECParameterSpec; import java.util.*; +import java.util.function.Consumer; +import java.util.function.Function; /** * @author David Hofman @@ -77,15 +79,6 @@ public class StandaloneCofactorSuite extends StandaloneTestSuite { KeyGeneratorTestable kgt = new KeyGeneratorTestable(kpg, spec); Test generate = KeyGeneratorTest.expectError(kgt, Result.ExpectedValue.ANY); - runTest(generate); - KeyPair kp = kgt.getKeyPair(); - if (kp == null) { - Test generateFail = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Generating KeyPair has failed on " + curve.getId() + ". " + "KeyAgreement tests will be skipped.", generate); - doTest(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Cofactor test of " + curve.getId() + ".", generateFail)); - continue; - } - Test generateSuccess = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Generate keypair.", generate); - ECPrivateKey ecpriv = (ECPrivateKey) kp.getPrivate(); List<Test> allKaTests = new LinkedList<>(); for (KeyAgreementIdent kaIdent : cfg.selected.getKAs()) { @@ -94,7 +87,7 @@ public class StandaloneCofactorSuite extends StandaloneTestSuite { for (EC_Key.Public pub : keys) { ECPublicKey ecpub = ECUtil.toPublicKey(pub); KeyAgreement ka = kaIdent.getInstance(cfg.selected.getProvider()); - KeyAgreementTestable testable = new KeyAgreementTestable(ka, ecpriv, ecpub); + KeyAgreementTestable testable = new KeyAgreementTestable(ka, ecpub, kgt); Test keyAgreement = KeyAgreementTest.expectError(testable, Result.ExpectedValue.FAILURE); specificKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, pub.getId() + " cofactor key test.", keyAgreement)); } @@ -104,8 +97,15 @@ public class StandaloneCofactorSuite extends StandaloneTestSuite { if (allKaTests.isEmpty()) { allKaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "None of the specified key agreement types is supported by the library.")); } - Test tests = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Do tests.", allKaTests.toArray(new Test[0])); - doTest(CompoundTest.greedyAllTry(Result.ExpectedValue.SUCCESS, "Cofactor test of " + curve.getId() + ".", generateSuccess, tests)); + Test kaTests = CompoundTest.all(Result.ExpectedValue.SUCCESS, "Do tests.", allKaTests.toArray(new Test[0])); + Function<Test[], Result> callback = (tests) -> CompoundTest.EXPECT_ALL.apply(Result.ExpectedValue.SUCCESS, tests); + Consumer<Test[]> runCallback = (tests) -> { + tests[0].run(); + if (tests[0].getResult().getValue().equals(Result.Value.SUCCESS)) { + tests[1].run(); + } + }; + doTest(CompoundTest.function(callback, runCallback, "Cofactor test of " + curve.getId() + ".", generate, kaTests)); } } } |
