From 35bdac1fff6e98485d5fbef870d6438fdbbd00c3 Mon Sep 17 00:00:00 2001 From: J08nY Date: Mon, 13 Nov 2017 22:49:41 +0100 Subject: Implement basic lib tests. --- .../standalone/test/SignatureTestable.java | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/cz/crcs/ectester/standalone/test/SignatureTestable.java (limited to 'src/cz/crcs/ectester/standalone/test/SignatureTestable.java') diff --git a/src/cz/crcs/ectester/standalone/test/SignatureTestable.java b/src/cz/crcs/ectester/standalone/test/SignatureTestable.java new file mode 100644 index 0000000..5f58b4a --- /dev/null +++ b/src/cz/crcs/ectester/standalone/test/SignatureTestable.java @@ -0,0 +1,102 @@ +package cz.crcs.ectester.standalone.test; + +import cz.crcs.ectester.common.test.TestException; +import cz.crcs.ectester.common.test.Testable; + +import java.security.InvalidKeyException; +import java.security.Signature; +import java.security.SignatureException; +import java.security.interfaces.ECPrivateKey; +import java.security.interfaces.ECPublicKey; + +public class SignatureTestable implements Testable { + + private Signature sig; + private ECPrivateKey signKey; + private ECPublicKey verifyKey; + private byte[] data; + private byte[] signature; + private boolean verified; + + private boolean hasRun; + private boolean error; + private boolean ok; + + public SignatureTestable(Signature sig, ECPrivateKey signKey, ECPublicKey verifyKey, byte[] data) { + this.sig = sig; + this.signKey = signKey; + this.verifyKey = verifyKey; + this.data = data; + } + + public byte[] getSignature() { + return signature; + } + + public boolean getVerified() { + return verified; + } + + @Override + public boolean hasRun() { + return hasRun; + } + + @Override + public void run() throws TestException { + try { + sig.initSign(signKey); + } catch (InvalidKeyException e) { + throw new TestException(e); + } + + try { + sig.update(data); + } catch (SignatureException e) { + ok = false; + hasRun = true; + return; + } + + try { + signature = sig.sign(); + } catch (SignatureException e) { + ok = false; + hasRun = true; + return; + } + + try { + sig.initVerify(verifyKey); + } catch (InvalidKeyException e) { + throw new TestException(e); + } + + try { + sig.update(data); + } catch (SignatureException e) { + ok = false; + hasRun = true; + return; + } + + try { + verified = sig.verify(signature); + } catch (SignatureException e) { + ok = false; + hasRun = true; + } + ok = true; + hasRun = true; + } + + @Override + public boolean ok() { + return ok; + } + + @Override + public boolean error() { + return error; + } +} -- cgit v1.2.3-70-g09d2 From da09715d9ec955b4b139b45a1b2c5270ca0ebf2d Mon Sep 17 00:00:00 2001 From: J08nY Date: Sun, 3 Dec 2017 20:59:41 +0100 Subject: Introduce BaseTestable and Testable Metadata. --- src/cz/crcs/ectester/common/output/TestWriter.java | 15 +++++++ .../ectester/common/output/TestableWriter.java | 39 ++++++++++++++++++ src/cz/crcs/ectester/common/test/BaseTestable.java | 35 ++++++++++++++++ src/cz/crcs/ectester/common/test/Test.java | 18 +++++++-- src/cz/crcs/ectester/common/test/Testable.java | 23 +++++++++++ src/cz/crcs/ectester/reader/ECTesterReader.java | 1 + src/cz/crcs/ectester/reader/output/TestWriter.java | 15 ------- .../ectester/reader/output/TextTestWriter.java | 1 + .../crcs/ectester/reader/output/XMLTestWriter.java | 1 + .../ectester/reader/output/YAMLTestWriter.java | 1 + src/cz/crcs/ectester/reader/response/Response.java | 5 ++- .../crcs/ectester/reader/test/CommandTestable.java | 47 ++++++++++++++++++++++ src/cz/crcs/ectester/reader/test/TestRunner.java | 2 +- .../standalone/libs/jni/NativeSignatureSpi.java | 2 + .../standalone/test/KeyAgreementTestable.java | 21 +--------- .../standalone/test/KeyGenerationTestable.java | 23 +---------- .../standalone/test/SignatureTestable.java | 23 +---------- 17 files changed, 191 insertions(+), 81 deletions(-) create mode 100644 src/cz/crcs/ectester/common/output/TestWriter.java create mode 100644 src/cz/crcs/ectester/common/output/TestableWriter.java create mode 100644 src/cz/crcs/ectester/common/test/BaseTestable.java delete mode 100644 src/cz/crcs/ectester/reader/output/TestWriter.java create mode 100644 src/cz/crcs/ectester/reader/test/CommandTestable.java (limited to 'src/cz/crcs/ectester/standalone/test/SignatureTestable.java') diff --git a/src/cz/crcs/ectester/common/output/TestWriter.java b/src/cz/crcs/ectester/common/output/TestWriter.java new file mode 100644 index 0000000..0f3c4e6 --- /dev/null +++ b/src/cz/crcs/ectester/common/output/TestWriter.java @@ -0,0 +1,15 @@ +package cz.crcs.ectester.common.output; + +import cz.crcs.ectester.common.test.Test; +import cz.crcs.ectester.reader.test.TestSuite; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public interface TestWriter { + void begin(TestSuite suite); + + void outputTest(Test t); + + void end(); +} diff --git a/src/cz/crcs/ectester/common/output/TestableWriter.java b/src/cz/crcs/ectester/common/output/TestableWriter.java new file mode 100644 index 0000000..2097254 --- /dev/null +++ b/src/cz/crcs/ectester/common/output/TestableWriter.java @@ -0,0 +1,39 @@ +package cz.crcs.ectester.common.output; + +import cz.crcs.ectester.common.test.BaseTestable; +import cz.crcs.ectester.common.test.Testable; + +import java.io.OutputStream; +import java.io.PrintStream; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class TestableWriter { + private PrintStream output; + + public TestableWriter(PrintStream output) { + this.output = output; + } + + public TestableWriter(OutputStream output) { + this(new PrintStream(output)); + } + + + public String outputTestableMeta(BaseTestable t) { + return null; + } + + public void writeTestableMeta(BaseTestable t) { + + } + + public String outputTestable(BaseTestable t) { + return null; + } + + public void writeTestable(BaseTestable t) { + + } +} diff --git a/src/cz/crcs/ectester/common/test/BaseTestable.java b/src/cz/crcs/ectester/common/test/BaseTestable.java new file mode 100644 index 0000000..6a63b13 --- /dev/null +++ b/src/cz/crcs/ectester/common/test/BaseTestable.java @@ -0,0 +1,35 @@ +package cz.crcs.ectester.common.test; + +import java.util.Collections; +import java.util.Map; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public abstract class BaseTestable implements Testable { + protected boolean hasRun; + protected boolean ok; + protected boolean error; + + protected Map meta; + + @Override + public boolean hasRun() { + return hasRun; + } + + @Override + public boolean ok() { + return ok; + } + + @Override + public boolean error() { + return error; + } + + @Override + public Map meta() { + return Collections.unmodifiableMap(meta); + } +} diff --git a/src/cz/crcs/ectester/common/test/Test.java b/src/cz/crcs/ectester/common/test/Test.java index 750a410..5f55337 100644 --- a/src/cz/crcs/ectester/common/test/Test.java +++ b/src/cz/crcs/ectester/common/test/Test.java @@ -1,5 +1,8 @@ package cz.crcs.ectester.common.test; +import java.util.Collections; +import java.util.Map; + import static cz.crcs.ectester.common.test.Result.Value; /** @@ -8,8 +11,9 @@ import static cz.crcs.ectester.common.test.Result.Value; * @author Jan Jancar johny@neuromancer.sk */ public abstract class Test implements Testable { - protected boolean hasRun = false; + protected boolean hasRun; protected Result result; + protected Map meta; public Result getResult() { if (!hasRun) { @@ -39,6 +43,7 @@ public abstract class Test implements Testable { return result.ok(); } + @Override public boolean error() { if (!hasRun) { return false; @@ -46,12 +51,19 @@ public abstract class Test implements Testable { return result.compareTo(Value.ERROR); } - public abstract String getDescription(); - + @Override public boolean hasRun() { return hasRun; } + @Override + public Map meta() { + return Collections.unmodifiableMap(meta); + } + + public abstract String getDescription(); + + @Override public abstract void run() throws TestException; } diff --git a/src/cz/crcs/ectester/common/test/Testable.java b/src/cz/crcs/ectester/common/test/Testable.java index d05d31e..e8eb321 100644 --- a/src/cz/crcs/ectester/common/test/Testable.java +++ b/src/cz/crcs/ectester/common/test/Testable.java @@ -1,15 +1,38 @@ package cz.crcs.ectester.common.test; +import java.util.Map; + /** * @author Jan Jancar johny@neuromancer.sk */ public interface Testable { + /** + * @return Whether this testable was run. + */ boolean hasRun(); + /** + * Run this Testable. + * + * @throws TestException + */ void run() throws TestException; + /** + * @return Whether this Testable was OK. + */ boolean ok(); + /** + * @return Whether an error happened. + */ boolean error(); + + /** + * Get the metadata of this Testable. + * + * @return The metadata of the testable. + */ + Map meta(); } diff --git a/src/cz/crcs/ectester/reader/ECTesterReader.java b/src/cz/crcs/ectester/reader/ECTesterReader.java index d32d9d8..e627099 100644 --- a/src/cz/crcs/ectester/reader/ECTesterReader.java +++ b/src/cz/crcs/ectester/reader/ECTesterReader.java @@ -26,6 +26,7 @@ import cz.crcs.ectester.applet.EC_Consts; import cz.crcs.ectester.common.cli.CLITools; import cz.crcs.ectester.common.ec.EC_Params; import cz.crcs.ectester.common.output.OutputLogger; +import cz.crcs.ectester.common.output.TestWriter; import cz.crcs.ectester.common.test.TestException; import cz.crcs.ectester.common.util.ByteUtil; import cz.crcs.ectester.data.EC_Store; diff --git a/src/cz/crcs/ectester/reader/output/TestWriter.java b/src/cz/crcs/ectester/reader/output/TestWriter.java deleted file mode 100644 index d79252d..0000000 --- a/src/cz/crcs/ectester/reader/output/TestWriter.java +++ /dev/null @@ -1,15 +0,0 @@ -package cz.crcs.ectester.reader.output; - -import cz.crcs.ectester.common.test.Test; -import cz.crcs.ectester.reader.test.TestSuite; - -/** - * @author Jan Jancar johny@neuromancer.sk - */ -public interface TestWriter { - void begin(TestSuite suite); - - void outputTest(Test t); - - void end(); -} diff --git a/src/cz/crcs/ectester/reader/output/TextTestWriter.java b/src/cz/crcs/ectester/reader/output/TextTestWriter.java index dc285b5..a4bb33e 100644 --- a/src/cz/crcs/ectester/reader/output/TextTestWriter.java +++ b/src/cz/crcs/ectester/reader/output/TextTestWriter.java @@ -1,5 +1,6 @@ package cz.crcs.ectester.reader.output; +import cz.crcs.ectester.common.output.TestWriter; import cz.crcs.ectester.common.test.CompoundTest; import cz.crcs.ectester.common.test.Test; import cz.crcs.ectester.reader.test.CommandTest; diff --git a/src/cz/crcs/ectester/reader/output/XMLTestWriter.java b/src/cz/crcs/ectester/reader/output/XMLTestWriter.java index ebc93ac..1a94984 100644 --- a/src/cz/crcs/ectester/reader/output/XMLTestWriter.java +++ b/src/cz/crcs/ectester/reader/output/XMLTestWriter.java @@ -1,5 +1,6 @@ package cz.crcs.ectester.reader.output; +import cz.crcs.ectester.common.output.TestWriter; import cz.crcs.ectester.common.test.CompoundTest; import cz.crcs.ectester.common.util.ByteUtil; import cz.crcs.ectester.reader.command.Command; diff --git a/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java b/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java index d8350ac..e0038fd 100644 --- a/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java +++ b/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java @@ -1,5 +1,6 @@ package cz.crcs.ectester.reader.output; +import cz.crcs.ectester.common.output.TestWriter; import cz.crcs.ectester.common.test.CompoundTest; import cz.crcs.ectester.common.util.ByteUtil; import cz.crcs.ectester.reader.command.Command; diff --git a/src/cz/crcs/ectester/reader/response/Response.java b/src/cz/crcs/ectester/reader/response/Response.java index d8edf9e..3b5d98b 100644 --- a/src/cz/crcs/ectester/reader/response/Response.java +++ b/src/cz/crcs/ectester/reader/response/Response.java @@ -13,7 +13,6 @@ import javax.smartcardio.ResponseAPDU; * @author Jan Jancar johny@neuromancer.sk */ public abstract class Response { - private ResponseAPDU resp; private long time; private short[] sws; @@ -88,6 +87,10 @@ public abstract class Response { return (short) resp.getSW(); } + public short[] getSWs() { + return sws; + } + public short getSW(int index) { return sws[index]; } diff --git a/src/cz/crcs/ectester/reader/test/CommandTestable.java b/src/cz/crcs/ectester/reader/test/CommandTestable.java new file mode 100644 index 0000000..f09a736 --- /dev/null +++ b/src/cz/crcs/ectester/reader/test/CommandTestable.java @@ -0,0 +1,47 @@ +package cz.crcs.ectester.reader.test; + +import cz.crcs.ectester.common.test.BaseTestable; +import cz.crcs.ectester.common.test.TestException; +import cz.crcs.ectester.reader.command.Command; +import cz.crcs.ectester.reader.response.Response; + +import javax.smartcardio.CardException; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class CommandTestable extends BaseTestable { + private Command command; + private Response response; + + public CommandTestable(Command command) { + this.command = command; + } + + public Command getCommand() { + return command; + } + + public Response getResponse() { + return response; + } + + @Override + public void run() throws TestException { + try { + response = command.send(); + } catch (CardException e) { + throw new TestException(e); + } + + hasRun = true; + if (response.error()) { + error = true; + } else if (response.successful()) { + ok = true; + } + meta.clear(); + meta.put("Natural SW", response.getNaturalSW()); + meta.put("SWs", response.getSWs()); + } +} diff --git a/src/cz/crcs/ectester/reader/test/TestRunner.java b/src/cz/crcs/ectester/reader/test/TestRunner.java index dcc78db..e581aaa 100644 --- a/src/cz/crcs/ectester/reader/test/TestRunner.java +++ b/src/cz/crcs/ectester/reader/test/TestRunner.java @@ -2,7 +2,7 @@ package cz.crcs.ectester.reader.test; import cz.crcs.ectester.common.test.Test; import cz.crcs.ectester.common.test.TestException; -import cz.crcs.ectester.reader.output.TestWriter; +import cz.crcs.ectester.common.output.TestWriter; /** * @author Jan Jancar johny@neuromancer.sk diff --git a/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java b/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java index a9ec8a6..76f01f0 100644 --- a/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java +++ b/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java @@ -61,11 +61,13 @@ public abstract class NativeSignatureSpi extends SignatureSpi { } @Override + @Deprecated protected void engineSetParameter(String param, Object value) throws InvalidParameterException { throw new UnsupportedOperationException("setParameter() not supported"); } @Override + @Deprecated protected Object engineGetParameter(String param) throws InvalidParameterException { throw new UnsupportedOperationException("getParameter() not supported"); } diff --git a/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java b/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java index ef363c3..fedf519 100644 --- a/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java +++ b/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java @@ -1,5 +1,6 @@ package cz.crcs.ectester.standalone.test; +import cz.crcs.ectester.common.test.BaseTestable; import cz.crcs.ectester.common.test.TestException; import cz.crcs.ectester.common.test.Testable; @@ -11,14 +12,11 @@ import java.security.interfaces.ECPublicKey; /** * @author Jan Jancar johny@neuromancer.sk */ -public class KeyAgreementTestable implements Testable { +public class KeyAgreementTestable extends BaseTestable { private KeyAgreement ka; private ECPrivateKey privateKey; private ECPublicKey publicKey; private byte[] secret; - private boolean hasRun; - private boolean error = false; - private boolean ok; public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey) { this.ka = ka; @@ -33,11 +31,6 @@ public class KeyAgreementTestable implements Testable { return secret; } - @Override - public boolean hasRun() { - return hasRun; - } - @Override public void run() throws TestException { try { @@ -66,14 +59,4 @@ public class KeyAgreementTestable implements Testable { ok = true; hasRun = true; } - - @Override - public boolean ok() { - return ok; - } - - @Override - public boolean error() { - return error; - } } diff --git a/src/cz/crcs/ectester/standalone/test/KeyGenerationTestable.java b/src/cz/crcs/ectester/standalone/test/KeyGenerationTestable.java index 381ce70..5a891b7 100644 --- a/src/cz/crcs/ectester/standalone/test/KeyGenerationTestable.java +++ b/src/cz/crcs/ectester/standalone/test/KeyGenerationTestable.java @@ -1,22 +1,18 @@ package cz.crcs.ectester.standalone.test; +import cz.crcs.ectester.common.test.BaseTestable; import cz.crcs.ectester.common.test.TestException; -import cz.crcs.ectester.common.test.Testable; import java.security.InvalidAlgorithmParameterException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.spec.ECParameterSpec; -public class KeyGenerationTestable implements Testable { - +public class KeyGenerationTestable extends BaseTestable { private KeyPair kp; private KeyPairGenerator kpg; private int keysize = 0; private ECParameterSpec spec = null; - private boolean hasRun; - private boolean error = false; - private boolean ok; public KeyGenerationTestable(KeyPairGenerator kpg) { this.kpg = kpg; @@ -36,11 +32,6 @@ public class KeyGenerationTestable implements Testable { return kp; } - @Override - public boolean hasRun() { - return hasRun; - } - @Override public void run() throws TestException { try { @@ -58,14 +49,4 @@ public class KeyGenerationTestable implements Testable { hasRun = true; ok = true; } - - @Override - public boolean ok() { - return ok; - } - - @Override - public boolean error() { - return error; - } } diff --git a/src/cz/crcs/ectester/standalone/test/SignatureTestable.java b/src/cz/crcs/ectester/standalone/test/SignatureTestable.java index 5f58b4a..8b6815b 100644 --- a/src/cz/crcs/ectester/standalone/test/SignatureTestable.java +++ b/src/cz/crcs/ectester/standalone/test/SignatureTestable.java @@ -1,5 +1,6 @@ package cz.crcs.ectester.standalone.test; +import cz.crcs.ectester.common.test.BaseTestable; import cz.crcs.ectester.common.test.TestException; import cz.crcs.ectester.common.test.Testable; @@ -9,8 +10,7 @@ import java.security.SignatureException; import java.security.interfaces.ECPrivateKey; import java.security.interfaces.ECPublicKey; -public class SignatureTestable implements Testable { - +public class SignatureTestable extends BaseTestable { private Signature sig; private ECPrivateKey signKey; private ECPublicKey verifyKey; @@ -18,10 +18,6 @@ public class SignatureTestable implements Testable { private byte[] signature; private boolean verified; - private boolean hasRun; - private boolean error; - private boolean ok; - public SignatureTestable(Signature sig, ECPrivateKey signKey, ECPublicKey verifyKey, byte[] data) { this.sig = sig; this.signKey = signKey; @@ -37,11 +33,6 @@ public class SignatureTestable implements Testable { return verified; } - @Override - public boolean hasRun() { - return hasRun; - } - @Override public void run() throws TestException { try { @@ -89,14 +80,4 @@ public class SignatureTestable implements Testable { ok = true; hasRun = true; } - - @Override - public boolean ok() { - return ok; - } - - @Override - public boolean error() { - return error; - } } -- cgit v1.2.3-70-g09d2 From 21da331c7a6e5db64ec5f1b59ce0ece624f6b760 Mon Sep 17 00:00:00 2001 From: J08nY Date: Wed, 6 Dec 2017 19:07:44 +0100 Subject: Implement basic standalone testing. --- .../crcs/ectester/reader/test/CardTestSuite.java | 1 - .../ectester/standalone/ECTesterStandalone.java | 373 +++++++++++---------- src/cz/crcs/ectester/standalone/consts/Ident.java | 3 + .../ectester/standalone/test/KeyAgreementTest.java | 2 +- .../standalone/test/KeyAgreementTestable.java | 64 +++- .../ectester/standalone/test/KeyGeneratorTest.java | 2 +- .../ectester/standalone/test/SignatureTest.java | 16 +- .../standalone/test/SignatureTestable.java | 22 +- .../standalone/test/StandaloneDefaultSuite.java | 76 +++++ .../standalone/test/StandaloneTestSuite.java | 24 ++ 10 files changed, 386 insertions(+), 197 deletions(-) create mode 100644 src/cz/crcs/ectester/standalone/test/StandaloneDefaultSuite.java create mode 100644 src/cz/crcs/ectester/standalone/test/StandaloneTestSuite.java (limited to 'src/cz/crcs/ectester/standalone/test/SignatureTestable.java') diff --git a/src/cz/crcs/ectester/reader/test/CardTestSuite.java b/src/cz/crcs/ectester/reader/test/CardTestSuite.java index e3cc155..3da5158 100644 --- a/src/cz/crcs/ectester/reader/test/CardTestSuite.java +++ b/src/cz/crcs/ectester/reader/test/CardTestSuite.java @@ -13,7 +13,6 @@ import cz.crcs.ectester.reader.ECTesterReader; import cz.crcs.ectester.reader.command.Command; import java.io.IOException; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java index 5ecff9b..433e6a8 100644 --- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java +++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java @@ -2,6 +2,9 @@ package cz.crcs.ectester.standalone; import cz.crcs.ectester.common.cli.*; import cz.crcs.ectester.common.ec.EC_Curve; +import cz.crcs.ectester.common.output.TextTestWriter; +import cz.crcs.ectester.common.test.TestException; +import cz.crcs.ectester.common.test.TestRunner; import cz.crcs.ectester.common.util.ByteUtil; import cz.crcs.ectester.common.util.ECUtil; import cz.crcs.ectester.data.EC_Store; @@ -9,6 +12,8 @@ import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; import cz.crcs.ectester.standalone.consts.SignatureIdent; import cz.crcs.ectester.standalone.libs.*; +import cz.crcs.ectester.standalone.test.StandaloneDefaultSuite; +import cz.crcs.ectester.standalone.test.StandaloneTestSuite; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; @@ -34,7 +39,7 @@ import java.util.stream.Collectors; * @version v0.1.0 */ public class ECTesterStandalone { - private ECLibrary[] libs = new ECLibrary[]{new SunECLib(), new BouncyCastleLib(), new TomcryptLib()}; + private ProviderECLibrary[] libs = new ProviderECLibrary[]{new SunECLib(), new BouncyCastleLib(), new TomcryptLib()}; private EC_Store dataStore; private Config cfg; @@ -91,9 +96,7 @@ public class ECTesterStandalone { System.err.println("Invalid algorithm parameter: " + e.getMessage()); } catch (NoSuchAlgorithmException nsaex) { System.err.println("Algorithm not supported by the selected library: " + nsaex.getMessage()); - } catch (InvalidKeyException e) { - e.printStackTrace(); - } catch (SignatureException e) { + } catch (InvalidKeyException | SignatureException | TestException e) { e.printStackTrace(); } } @@ -103,6 +106,11 @@ public class ECTesterStandalone { Options testOpts = new Options(); ParserOptions test = new ParserOptions(new DefaultParser(), testOpts); + testOpts.addOption(Option.builder("gt").longOpt("kpg-type").desc("Set the KeyPairGenerator object [type].").hasArg().argName("type").optionalArg(false).build()); + testOpts.addOption(Option.builder("kt").longOpt("ka-type").desc("Set the KeyAgreement object [type].").hasArg().argName("type").optionalArg(false).build()); + testOpts.addOption(Option.builder("st").longOpt("sig-type").desc("Set the Signature object [type].").hasArg().argName("type").optionalArg(false).build()); + testOpts.addOption(Option.builder("b").longOpt("bits").hasArg().argName("n").optionalArg(false).desc("What size of curve to use.").build()); + testOpts.addOption(Option.builder("nc").longOpt("named-curve").desc("Use a named curve, from CurveDB: ").hasArg().argName("cat/id").build()); actions.put("test", test); Options ecdhOpts = new Options(); @@ -188,70 +196,68 @@ public class ECTesterStandalone { * */ private void ecdh() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { - if (cfg.selected instanceof ProviderECLibrary) { - ProviderECLibrary lib = (ProviderECLibrary) cfg.selected; - - String algo = cli.getOptionValue("ecdh.type", "ECDH"); - KeyAgreementIdent kaIdent = lib.getKAs().stream() - .filter((ident) -> ident.contains(algo)) - .findFirst() - .orElse(null); - - KeyPairGeneratorIdent kpIdent = lib.getKPGs().stream() - .filter((ident) -> ident.contains(algo)) - .findFirst() - .orElse(lib.getKPGs().stream() - .filter((ident) -> ident.contains("EC")) - .findFirst() - .orElse(null)); - - - if (kaIdent == null || kpIdent == null) { - throw new NoSuchAlgorithmException(algo); - } else { - KeyAgreement ka = kaIdent.getInstance(lib.getProvider()); - KeyPairGenerator kpg = kpIdent.getInstance(lib.getProvider()); - AlgorithmParameterSpec spec = null; - if (cli.hasOption("ecdh.bits")) { - int bits = Integer.parseInt(cli.getOptionValue("ecdh.bits")); - kpg.initialize(bits); - } else if (cli.hasOption("ecdh.named-curve")) { - String curveName = cli.getOptionValue("ecdh.named-curve"); - EC_Curve curve = dataStore.getObject(EC_Curve.class, curveName); - if (curve == null) { - System.err.println("Curve not found: " + curveName); - return; - } - spec = curve.toSpec(); - kpg.initialize(spec); + ProviderECLibrary lib = cfg.selected; + + String algo = cli.getOptionValue("ecdh.type", "ECDH"); + KeyAgreementIdent kaIdent = lib.getKAs().stream() + .filter((ident) -> ident.contains(algo)) + .findFirst() + .orElse(null); + + KeyPairGeneratorIdent kpIdent = lib.getKPGs().stream() + .filter((ident) -> ident.contains(algo)) + .findFirst() + .orElse(lib.getKPGs().stream() + .filter((ident) -> ident.contains("EC")) + .findFirst() + .orElse(null)); + + + if (kaIdent == null || kpIdent == null) { + throw new NoSuchAlgorithmException(algo); + } else { + KeyAgreement ka = kaIdent.getInstance(lib.getProvider()); + KeyPairGenerator kpg = kpIdent.getInstance(lib.getProvider()); + AlgorithmParameterSpec spec = null; + if (cli.hasOption("ecdh.bits")) { + int bits = Integer.parseInt(cli.getOptionValue("ecdh.bits")); + kpg.initialize(bits); + } else if (cli.hasOption("ecdh.named-curve")) { + String curveName = cli.getOptionValue("ecdh.named-curve"); + EC_Curve curve = dataStore.getObject(EC_Curve.class, curveName); + if (curve == null) { + System.err.println("Curve not found: " + curveName); + return; } + spec = curve.toSpec(); + kpg.initialize(spec); + } - System.out.println("index;nanotime;pubW;privS;secret"); + System.out.println("index;nanotime;pubW;privS;secret"); - int amount = Integer.parseInt(cli.getOptionValue("ecdh.amount", "1")); - for (int i = 0; i < amount; ++i) { - KeyPair one = kpg.genKeyPair(); - KeyPair other = kpg.genKeyPair(); + int amount = Integer.parseInt(cli.getOptionValue("ecdh.amount", "1")); + for (int i = 0; i < amount; ++i) { + KeyPair one = kpg.genKeyPair(); + KeyPair other = kpg.genKeyPair(); - ECPrivateKey privkey = (ECPrivateKey) one.getPrivate(); - ECPublicKey pubkey = (ECPublicKey) other.getPublic(); + ECPrivateKey privkey = (ECPrivateKey) one.getPrivate(); + ECPublicKey pubkey = (ECPublicKey) other.getPublic(); - long elapsed = -System.nanoTime(); - if (spec != null) { - ka.init(privkey, spec); - } else { - ka.init(privkey); - } - ka.doPhase(pubkey, true); - elapsed += System.nanoTime(); - byte[] result = ka.generateSecret(); - ka = kaIdent.getInstance(lib.getProvider()); - - String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(pubkey.getW(), pubkey.getParams()), false); - String priv = ByteUtil.bytesToHex(privkey.getS().toByteArray(), false); - String dh = ByteUtil.bytesToHex(result, false); - System.out.println(String.format("%d;%d;%s;%s;%s", i, elapsed, pub, priv, dh)); + long elapsed = -System.nanoTime(); + if (spec != null) { + ka.init(privkey, spec); + } else { + ka.init(privkey); } + ka.doPhase(pubkey, true); + elapsed += System.nanoTime(); + byte[] result = ka.generateSecret(); + ka = kaIdent.getInstance(lib.getProvider()); + + String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(pubkey.getW(), pubkey.getParams()), false); + String priv = ByteUtil.bytesToHex(privkey.getS().toByteArray(), false); + String dh = ByteUtil.bytesToHex(result, false); + System.out.println(String.format("%d;%d;%s;%s;%s", i, elapsed, pub, priv, dh)); } } } @@ -278,70 +284,68 @@ public class ECTesterStandalone { dataString = ByteUtil.bytesToHex(data, false); } - if (cfg.selected instanceof ProviderECLibrary) { - ProviderECLibrary lib = (ProviderECLibrary) cfg.selected; - - String algo = cli.getOptionValue("ecdsa.type", "ECDSA"); - SignatureIdent sigIdent = lib.getSigs().stream() - .filter((ident) -> ident.contains(algo)) - .findFirst() - .orElse(null); - - KeyPairGeneratorIdent kpIdent = lib.getKPGs().stream() - .filter((ident) -> ident.contains(algo)) - .findFirst() - .orElse(lib.getKPGs().stream() - .filter((ident) -> ident.contains("EC")) - .findFirst() - .orElse(null)); - - if (sigIdent == null || kpIdent == null) { - throw new NoSuchAlgorithmException(algo); - } else { - Signature sig = sigIdent.getInstance(lib.getProvider()); - KeyPairGenerator kpg = kpIdent.getInstance(lib.getProvider()); - if (cli.hasOption("ecdsa.bits")) { - int bits = Integer.parseInt(cli.getOptionValue("ecdsa.bits")); - kpg.initialize(bits); - } else if (cli.hasOption("ecdsa.named-curve")) { - String curveName = cli.getOptionValue("ecdsa.named-curve"); - EC_Curve curve = dataStore.getObject(EC_Curve.class, curveName); - if (curve == null) { - System.err.println("Curve not found: " + curveName); - return; - } - kpg.initialize(curve.toSpec()); + ProviderECLibrary lib = cfg.selected; + + String algo = cli.getOptionValue("ecdsa.type", "ECDSA"); + SignatureIdent sigIdent = lib.getSigs().stream() + .filter((ident) -> ident.contains(algo)) + .findFirst() + .orElse(null); + + KeyPairGeneratorIdent kpIdent = lib.getKPGs().stream() + .filter((ident) -> ident.contains(algo)) + .findFirst() + .orElse(lib.getKPGs().stream() + .filter((ident) -> ident.contains("EC")) + .findFirst() + .orElse(null)); + + if (sigIdent == null || kpIdent == null) { + throw new NoSuchAlgorithmException(algo); + } else { + Signature sig = sigIdent.getInstance(lib.getProvider()); + KeyPairGenerator kpg = kpIdent.getInstance(lib.getProvider()); + if (cli.hasOption("ecdsa.bits")) { + int bits = Integer.parseInt(cli.getOptionValue("ecdsa.bits")); + kpg.initialize(bits); + } else if (cli.hasOption("ecdsa.named-curve")) { + String curveName = cli.getOptionValue("ecdsa.named-curve"); + EC_Curve curve = dataStore.getObject(EC_Curve.class, curveName); + if (curve == null) { + System.err.println("Curve not found: " + curveName); + return; } + kpg.initialize(curve.toSpec()); + } - System.out.println("index;data;signtime;verifytime;pubW;privS;signature;verified"); + System.out.println("index;data;signtime;verifytime;pubW;privS;signature;verified"); - int amount = Integer.parseInt(cli.getOptionValue("ecdsa.amount", "1")); - for (int i = 0; i < amount; ++i) { - KeyPair one = kpg.genKeyPair(); + int amount = Integer.parseInt(cli.getOptionValue("ecdsa.amount", "1")); + for (int i = 0; i < amount; ++i) { + KeyPair one = kpg.genKeyPair(); - ECPrivateKey privkey = (ECPrivateKey) one.getPrivate(); - ECPublicKey pubkey = (ECPublicKey) one.getPublic(); + ECPrivateKey privkey = (ECPrivateKey) one.getPrivate(); + ECPublicKey pubkey = (ECPublicKey) one.getPublic(); - sig.initSign(privkey); - sig.update(data); + sig.initSign(privkey); + sig.update(data); - long signTime = -System.nanoTime(); - byte[] signature = sig.sign(); - signTime += System.nanoTime(); + long signTime = -System.nanoTime(); + byte[] signature = sig.sign(); + signTime += System.nanoTime(); - sig.initVerify(pubkey); - sig.update(data); + sig.initVerify(pubkey); + sig.update(data); - long verifyTime = -System.nanoTime(); - boolean verified = sig.verify(signature); - verifyTime += System.nanoTime(); + long verifyTime = -System.nanoTime(); + boolean verified = sig.verify(signature); + verifyTime += System.nanoTime(); - String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(pubkey.getW(), pubkey.getParams()), false); - String priv = ByteUtil.bytesToHex(privkey.getS().toByteArray(), false); - String sign = ByteUtil.bytesToHex(signature, false); - System.out.println(String.format("%d;%s;%d;%d;%s;%s;%s;%d", i, dataString, signTime, verifyTime, pub, priv, sign, verified ? 1 : 0)); - } + String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(pubkey.getW(), pubkey.getParams()), false); + String priv = ByteUtil.bytesToHex(privkey.getS().toByteArray(), false); + String sign = ByteUtil.bytesToHex(signature, false); + System.out.println(String.format("%d;%s;%d;%d;%s;%s;%s;%d", i, dataString, signTime, verifyTime, pub, priv, sign, verified ? 1 : 0)); } } } @@ -350,46 +354,44 @@ public class ECTesterStandalone { * */ private void generate() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { - if (cfg.selected instanceof ProviderECLibrary) { - ProviderECLibrary lib = (ProviderECLibrary) cfg.selected; - KeyPairGeneratorIdent ident = null; - String algo = cli.getOptionValue("generate.type", "EC"); - for (KeyPairGeneratorIdent kpIdent : lib.getKPGs()) { - if (kpIdent.contains(algo)) { - ident = kpIdent; - break; - } + ProviderECLibrary lib = cfg.selected; + KeyPairGeneratorIdent ident = null; + String algo = cli.getOptionValue("generate.type", "EC"); + for (KeyPairGeneratorIdent kpIdent : lib.getKPGs()) { + if (kpIdent.contains(algo)) { + ident = kpIdent; + break; } - if (ident == null) { - throw new NoSuchAlgorithmException(algo); - } else { - KeyPairGenerator kpg = ident.getInstance(lib.getProvider()); - if (cli.hasOption("generate.bits")) { - int bits = Integer.parseInt(cli.getOptionValue("generate.bits")); - kpg.initialize(bits); - } else if (cli.hasOption("generate.named-curve")) { - String curveName = cli.getOptionValue("generate.named-curve"); - EC_Curve curve = dataStore.getObject(EC_Curve.class, curveName); - if (curve == null) { - System.err.println("Curve not found: " + curveName); - return; - } - kpg.initialize(curve.toSpec()); - } - System.out.println("index;nanotime;pubW;privS"); - - int amount = Integer.parseInt(cli.getOptionValue("generate.amount", "1")); - for (int i = 0; i < amount; ++i) { - long elapsed = -System.nanoTime(); - KeyPair kp = kpg.genKeyPair(); - elapsed += System.nanoTime(); - ECPublicKey publicKey = (ECPublicKey) kp.getPublic(); - ECPrivateKey privateKey = (ECPrivateKey) kp.getPrivate(); - - String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(publicKey.getW(), publicKey.getParams()), false); - String priv = ByteUtil.bytesToHex(privateKey.getS().toByteArray(), false); - System.out.println(String.format("%d;%d;%s;%s", i, elapsed, pub, priv)); + } + if (ident == null) { + throw new NoSuchAlgorithmException(algo); + } else { + KeyPairGenerator kpg = ident.getInstance(lib.getProvider()); + if (cli.hasOption("generate.bits")) { + int bits = Integer.parseInt(cli.getOptionValue("generate.bits")); + kpg.initialize(bits); + } else if (cli.hasOption("generate.named-curve")) { + String curveName = cli.getOptionValue("generate.named-curve"); + EC_Curve curve = dataStore.getObject(EC_Curve.class, curveName); + if (curve == null) { + System.err.println("Curve not found: " + curveName); + return; } + kpg.initialize(curve.toSpec()); + } + System.out.println("index;nanotime;pubW;privS"); + + int amount = Integer.parseInt(cli.getOptionValue("generate.amount", "1")); + for (int i = 0; i < amount; ++i) { + long elapsed = -System.nanoTime(); + KeyPair kp = kpg.genKeyPair(); + elapsed += System.nanoTime(); + ECPublicKey publicKey = (ECPublicKey) kp.getPublic(); + ECPrivateKey privateKey = (ECPrivateKey) kp.getPrivate(); + + String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(publicKey.getW(), publicKey.getParams()), false); + String priv = ByteUtil.bytesToHex(privateKey.getS().toByteArray(), false); + System.out.println(String.format("%d;%d;%s;%s", i, elapsed, pub, priv)); } } } @@ -397,39 +399,40 @@ public class ECTesterStandalone { /** * */ - private void test() { - + private void test() throws NoSuchAlgorithmException, TestException { + StandaloneTestSuite suite = new StandaloneDefaultSuite(dataStore, cfg, cli); + TestRunner runner = new TestRunner(suite, new TextTestWriter(System.out)); + suite.setup(); + runner.run(); } /** * */ private void export() throws NoSuchAlgorithmException, IOException { - if (cfg.selected instanceof ProviderECLibrary) { - ProviderECLibrary lib = (ProviderECLibrary) cfg.selected; - KeyPairGeneratorIdent ident = null; - String algo = cli.getOptionValue("export.type", "EC"); - for (KeyPairGeneratorIdent kpIdent : lib.getKPGs()) { - if (kpIdent.contains(algo)) { - ident = kpIdent; - break; - } + ProviderECLibrary lib = (ProviderECLibrary) cfg.selected; + KeyPairGeneratorIdent ident = null; + String algo = cli.getOptionValue("export.type", "EC"); + for (KeyPairGeneratorIdent kpIdent : lib.getKPGs()) { + if (kpIdent.contains(algo)) { + ident = kpIdent; + break; } - if (ident == null) { - throw new NoSuchAlgorithmException(algo); - } else { - KeyPairGenerator kpg = ident.getInstance(lib.getProvider()); - if (cli.hasOption("export.bits")) { - int bits = Integer.parseInt(cli.getOptionValue("export.bits")); - kpg.initialize(bits); - } - KeyPair kp = kpg.genKeyPair(); - ECPrivateKey privateKey = (ECPrivateKey) kp.getPrivate(); - ECParameterSpec params = privateKey.getParams(); - System.out.println(params); - EC_Curve curve = EC_Curve.fromSpec(params); - curve.writeCSV(System.out); + } + if (ident == null) { + throw new NoSuchAlgorithmException(algo); + } else { + KeyPairGenerator kpg = ident.getInstance(lib.getProvider()); + if (cli.hasOption("export.bits")) { + int bits = Integer.parseInt(cli.getOptionValue("export.bits")); + kpg.initialize(bits); } + KeyPair kp = kpg.genKeyPair(); + ECPrivateKey privateKey = (ECPrivateKey) kp.getPrivate(); + ECParameterSpec params = privateKey.getParams(); + System.out.println(params); + EC_Curve curve = EC_Curve.fromSpec(params); + curve.writeCSV(System.out); } } @@ -443,15 +446,15 @@ public class ECTesterStandalone { * */ public static class Config { - private ECLibrary[] libs; - public ECLibrary selected = null; + private ProviderECLibrary[] libs; + public ProviderECLibrary selected = null; - public Config(ECLibrary[] libs) { + public Config(ProviderECLibrary[] libs) { this.libs = libs; } boolean readOptions(TreeCommandLine cli) { - if (cli.isNext("generate") || cli.isNext("export") || cli.isNext("ecdh") || cli.isNext("ecdsa")) { + if (cli.isNext("generate") || cli.isNext("export") || cli.isNext("ecdh") || cli.isNext("ecdsa") || cli.isNext("test")) { if (!cli.hasArg(-1)) { System.err.println("Missing library name argument."); return false; @@ -466,8 +469,8 @@ public class ECTesterStandalone { String libraryName = cli.getArg(-1); if (libraryName != null) { - List matchedLibs = new LinkedList<>(); - for (ECLibrary lib : libs) { + List matchedLibs = new LinkedList<>(); + for (ProviderECLibrary lib : libs) { if (lib.name().toLowerCase().contains(libraryName.toLowerCase())) { matchedLibs.add(lib); } diff --git a/src/cz/crcs/ectester/standalone/consts/Ident.java b/src/cz/crcs/ectester/standalone/consts/Ident.java index e2556c5..40a44ac 100644 --- a/src/cz/crcs/ectester/standalone/consts/Ident.java +++ b/src/cz/crcs/ectester/standalone/consts/Ident.java @@ -42,6 +42,9 @@ public abstract class Ident { for (String alias : idents) { try { instance = getter.apply(alias, provider); + if (instance != null) { + break; + } } catch (Exception ignored) { } } diff --git a/src/cz/crcs/ectester/standalone/test/KeyAgreementTest.java b/src/cz/crcs/ectester/standalone/test/KeyAgreementTest.java index 9604e75..e273a44 100644 --- a/src/cz/crcs/ectester/standalone/test/KeyAgreementTest.java +++ b/src/cz/crcs/ectester/standalone/test/KeyAgreementTest.java @@ -43,7 +43,7 @@ public class KeyAgreementTest extends SimpleTest { @Override public String getDescription() { - return null; + return "KeyAgreement test"; } @Override diff --git a/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java b/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java index fedf519..8e9b0dd 100644 --- a/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java +++ b/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java @@ -2,12 +2,14 @@ package cz.crcs.ectester.standalone.test; import cz.crcs.ectester.common.test.BaseTestable; import cz.crcs.ectester.common.test.TestException; -import cz.crcs.ectester.common.test.Testable; import javax.crypto.KeyAgreement; +import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.interfaces.ECPrivateKey; import java.security.interfaces.ECPublicKey; +import java.security.spec.AlgorithmParameterSpec; +import java.security.spec.ECParameterSpec; /** * @author Jan Jancar johny@neuromancer.sk @@ -16,6 +18,9 @@ public class KeyAgreementTestable extends BaseTestable { private KeyAgreement ka; private ECPrivateKey privateKey; private ECPublicKey publicKey; + private KeyGeneratorTestable kgtPrivate; + private KeyGeneratorTestable kgtPublic; + private AlgorithmParameterSpec spec; private byte[] secret; public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey) { @@ -24,6 +29,27 @@ public class KeyAgreementTestable extends BaseTestable { this.publicKey = publicKey; } + public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey, ECParameterSpec spec) { + this(ka, privateKey, publicKey); + this.spec = spec; + } + + public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable kgt, ECPrivateKey privateKey, ECParameterSpec spec) { + this(ka, privateKey, null, spec); + this.kgtPublic = kgt; + } + + public KeyAgreementTestable(KeyAgreement ka, ECPublicKey publicKey, KeyGeneratorTestable kgt, ECParameterSpec spec) { + this(ka, null, publicKey, spec); + this.kgtPrivate = kgt; + } + + public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable privKgt, KeyGeneratorTestable pubKgt, ECParameterSpec spec) { + this(ka, (ECPrivateKey) null, null, spec); + this.kgtPrivate = privKgt; + this.kgtPublic = pubKgt; + } + public byte[] getSecret() { if (!hasRun) { return null; @@ -33,20 +59,38 @@ public class KeyAgreementTestable extends BaseTestable { @Override public void run() throws TestException { + if (kgtPrivate != null) { + privateKey = (ECPrivateKey) kgtPrivate.getKeyPair().getPrivate(); + } + + if (kgtPublic != null) { + publicKey = (ECPublicKey) kgtPublic.getKeyPair().getPublic(); + } + try { - ka.init(privateKey); - } catch (InvalidKeyException ikex) { - throw new TestException(ikex); + if (spec != null) { + ka.init(privateKey, spec); + } else { + ka.init(privateKey); + } + } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { + ok = false; + error = true; + hasRun = true; + return; } try { ka.doPhase(publicKey, true); - } catch (InvalidKeyException ikex) { - throw new TestException(ikex); - } catch (IllegalStateException isex) { + } catch (IllegalStateException e) { ok = false; hasRun = true; return; + } catch (InvalidKeyException e) { + ok = false; + error = true; + hasRun = true; + return; } try { @@ -55,7 +99,13 @@ public class KeyAgreementTestable extends BaseTestable { ok = false; hasRun = true; return; + } catch (UnsupportedOperationException uoe) { + ok = false; + error = true; + hasRun = false; + return; } + ok = true; hasRun = true; } diff --git a/src/cz/crcs/ectester/standalone/test/KeyGeneratorTest.java b/src/cz/crcs/ectester/standalone/test/KeyGeneratorTest.java index 9032415..a57e28c 100644 --- a/src/cz/crcs/ectester/standalone/test/KeyGeneratorTest.java +++ b/src/cz/crcs/ectester/standalone/test/KeyGeneratorTest.java @@ -28,7 +28,7 @@ public class KeyGeneratorTest extends SimpleTest { @Override public String getDescription() { - return null; + return "KeyPairGenerator test"; } @Override diff --git a/src/cz/crcs/ectester/standalone/test/SignatureTest.java b/src/cz/crcs/ectester/standalone/test/SignatureTest.java index 272a3f7..97e387c 100644 --- a/src/cz/crcs/ectester/standalone/test/SignatureTest.java +++ b/src/cz/crcs/ectester/standalone/test/SignatureTest.java @@ -1,5 +1,6 @@ package cz.crcs.ectester.standalone.test; +import cz.crcs.ectester.common.test.Result; import cz.crcs.ectester.common.test.SimpleTest; import cz.crcs.ectester.common.test.TestCallback; import cz.crcs.ectester.common.test.TestException; @@ -12,9 +13,22 @@ public class SignatureTest extends SimpleTest { super(sig, callback); } + public static SignatureTest expect(SignatureTestable kg, Result.ExpectedValue expected) { + return new SignatureTest(kg, new TestCallback() { + @Override + public Result apply(SignatureTestable signatureTestable) { + return new Result(Result.Value.fromExpected(expected, signatureTestable.ok(), signatureTestable.error())); + } + }); + } + + public static SignatureTest function(SignatureTestable ka, TestCallback callback) { + return new SignatureTest(ka, callback); + } + @Override public String getDescription() { - return null; + return "Signature test"; } @Override diff --git a/src/cz/crcs/ectester/standalone/test/SignatureTestable.java b/src/cz/crcs/ectester/standalone/test/SignatureTestable.java index 8b6815b..564a6bf 100644 --- a/src/cz/crcs/ectester/standalone/test/SignatureTestable.java +++ b/src/cz/crcs/ectester/standalone/test/SignatureTestable.java @@ -2,9 +2,9 @@ package cz.crcs.ectester.standalone.test; import cz.crcs.ectester.common.test.BaseTestable; import cz.crcs.ectester.common.test.TestException; -import cz.crcs.ectester.common.test.Testable; import java.security.InvalidKeyException; +import java.security.SecureRandom; import java.security.Signature; import java.security.SignatureException; import java.security.interfaces.ECPrivateKey; @@ -14,6 +14,7 @@ public class SignatureTestable extends BaseTestable { private Signature sig; private ECPrivateKey signKey; private ECPublicKey verifyKey; + private KeyGeneratorTestable kgt; private byte[] data; private byte[] signature; private boolean verified; @@ -23,6 +24,20 @@ public class SignatureTestable extends BaseTestable { this.signKey = signKey; this.verifyKey = verifyKey; this.data = data; + if (data == null) { + SecureRandom random = new SecureRandom(); + this.data = new byte[32]; + random.nextBytes(this.data); + } + } + + public SignatureTestable(Signature sig, KeyGeneratorTestable kgt, byte[] data) { + this(sig, null, null, data); + this.kgt = kgt; + } + + public byte[] getData() { + return data; } public byte[] getSignature() { @@ -35,6 +50,11 @@ public class SignatureTestable extends BaseTestable { @Override public void run() throws TestException { + if (kgt != null) { + signKey = (ECPrivateKey) kgt.getKeyPair().getPrivate(); + verifyKey = (ECPublicKey) kgt.getKeyPair().getPublic(); + } + try { sig.initSign(signKey); } catch (InvalidKeyException e) { diff --git a/src/cz/crcs/ectester/standalone/test/StandaloneDefaultSuite.java b/src/cz/crcs/ectester/standalone/test/StandaloneDefaultSuite.java new file mode 100644 index 0000000..7056d69 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/test/StandaloneDefaultSuite.java @@ -0,0 +1,76 @@ +package cz.crcs.ectester.standalone.test; + +import cz.crcs.ectester.common.cli.TreeCommandLine; +import cz.crcs.ectester.common.ec.EC_Curve; +import cz.crcs.ectester.common.test.Result; +import cz.crcs.ectester.data.EC_Store; +import cz.crcs.ectester.standalone.ECTesterStandalone; +import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; +import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; +import cz.crcs.ectester.standalone.consts.SignatureIdent; + +import javax.crypto.KeyAgreement; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.Signature; +import java.security.spec.ECParameterSpec; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class StandaloneDefaultSuite extends StandaloneTestSuite { + + public StandaloneDefaultSuite(EC_Store dataStore, ECTesterStandalone.Config cfg, TreeCommandLine cli) { + super(dataStore, cfg, cli, "default", "The default test suite tests basic support of ECDH and ECDSA."); + } + + @Override + public void setup() throws NoSuchAlgorithmException { + String kpgAlgo = cli.getOptionValue("test.kpg-type", "EC"); + String kaAlgo = cli.getOptionValue("test.ka-type"); + String sigAlgo = cli.getOptionValue("test.sig-type"); + + KeyPairGeneratorIdent kpgIdent = cfg.selected.getKPGs().stream() + .filter((ident) -> ident.contains(kpgAlgo)) + .findFirst().get(); + KeyPairGenerator kpg = kpgIdent.getInstance(cfg.selected.getProvider()); + + KeyGeneratorTestable kgtOne; + KeyGeneratorTestable kgtOther; + ECParameterSpec spec = null; + if (cli.hasOption("test.bits")) { + int bits = Integer.parseInt(cli.getOptionValue("test.bits")); + kgtOne = new KeyGeneratorTestable(kpg, bits); + kgtOther = new KeyGeneratorTestable(kpg, bits); + } else if (cli.hasOption("test.named-curve")) { + String curveName = cli.getOptionValue("test.named-curve"); + EC_Curve curve = dataStore.getObject(EC_Curve.class, curveName); + if (curve == null) { + System.err.println("Curve not found: " + curveName); + return; + } + spec = curve.toSpec(); + kgtOne = new KeyGeneratorTestable(kpg, spec); + kgtOther = new KeyGeneratorTestable(kpg, spec); + } else { + kgtOne = new KeyGeneratorTestable(kpg); + kgtOther = new KeyGeneratorTestable(kpg); + } + + tests.add(KeyGeneratorTest.expect(kgtOne, Result.ExpectedValue.SUCCESS)); + tests.add(KeyGeneratorTest.expect(kgtOther, Result.ExpectedValue.SUCCESS)); + + for (KeyAgreementIdent kaIdent : cfg.selected.getKAs()) { + if (kaAlgo == null || kaIdent.contains(kaAlgo)) { + KeyAgreement ka = kaIdent.getInstance(cfg.selected.getProvider()); + tests.add(KeyAgreementTest.expect(new KeyAgreementTestable(ka, kgtOne, kgtOther, spec), Result.ExpectedValue.SUCCESS)); + } + } + for (SignatureIdent sigIdent : cfg.selected.getSigs()) { + if (sigAlgo == null || sigIdent.contains(sigAlgo)) { + Signature sig = sigIdent.getInstance(cfg.selected.getProvider()); + tests.add(SignatureTest.expect(new SignatureTestable(sig, kgtOne, null), Result.ExpectedValue.SUCCESS)); + } + } + } +} diff --git a/src/cz/crcs/ectester/standalone/test/StandaloneTestSuite.java b/src/cz/crcs/ectester/standalone/test/StandaloneTestSuite.java new file mode 100644 index 0000000..5682cd5 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/test/StandaloneTestSuite.java @@ -0,0 +1,24 @@ +package cz.crcs.ectester.standalone.test; + +import cz.crcs.ectester.common.cli.TreeCommandLine; +import cz.crcs.ectester.common.test.TestSuite; +import cz.crcs.ectester.data.EC_Store; +import cz.crcs.ectester.standalone.ECTesterStandalone; + +import java.security.NoSuchAlgorithmException; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public abstract class StandaloneTestSuite extends TestSuite { + TreeCommandLine cli; + ECTesterStandalone.Config cfg; + + public StandaloneTestSuite(EC_Store dataStore, ECTesterStandalone.Config cfg, TreeCommandLine cli, String name, String description) { + super(dataStore, name, description); + this.cfg = cfg; + this.cli = cli; + } + + public abstract void setup() throws NoSuchAlgorithmException; +} -- cgit v1.2.3-70-g09d2