summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cz/crcs/ectester/standalone/ECTesterStandalone.java')
-rw-r--r--src/cz/crcs/ectester/standalone/ECTesterStandalone.java355
1 files changed, 179 insertions, 176 deletions
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: <cat/id>").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;
+ ProviderECLibrary lib = cfg.selected;
- String algo = cli.getOptionValue("ecdh.type", "ECDH");
- KeyAgreementIdent kaIdent = lib.getKAs().stream()
- .filter((ident) -> ident.contains(algo))
- .findFirst()
- .orElse(null);
+ 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));
+ 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);
+ 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");
-
- int amount = Integer.parseInt(cli.getOptionValue("ecdh.amount", "1"));
- for (int i = 0; i < amount; ++i) {
- KeyPair one = kpg.genKeyPair();
- KeyPair other = kpg.genKeyPair();
+ System.out.println("index;nanotime;pubW;privS;secret");
- ECPrivateKey privkey = (ECPrivateKey) one.getPrivate();
- ECPublicKey pubkey = (ECPublicKey) other.getPublic();
+ int amount = Integer.parseInt(cli.getOptionValue("ecdh.amount", "1"));
+ for (int i = 0; i < amount; ++i) {
+ KeyPair one = kpg.genKeyPair();
+ KeyPair other = kpg.genKeyPair();
- 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());
+ ECPrivateKey privkey = (ECPrivateKey) one.getPrivate();
+ ECPublicKey pubkey = (ECPublicKey) other.getPublic();
- 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;
+ ProviderECLibrary lib = cfg.selected;
- String algo = cli.getOptionValue("ecdsa.type", "ECDSA");
- SignatureIdent sigIdent = lib.getSigs().stream()
- .filter((ident) -> ident.contains(algo))
- .findFirst()
- .orElse(null);
+ 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));
+ 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());
+ 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());
+ }
+ 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;
}
- System.out.println("index;nanotime;pubW;privS");
+ 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();
+ 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));
- }
+ 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<ECLibrary> matchedLibs = new LinkedList<>();
- for (ECLibrary lib : libs) {
+ List<ProviderECLibrary> matchedLibs = new LinkedList<>();
+ for (ProviderECLibrary lib : libs) {
if (lib.name().toLowerCase().contains(libraryName.toLowerCase())) {
matchedLibs.add(lib);
}