aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2017-11-23 01:32:41 +0100
committerJ08nY2017-11-23 01:32:41 +0100
commit0fdfe31112924f51ca503c0ec0fff62ec20403c1 (patch)
tree35431fa7a6e96ce9ad8132eca3340048632699bf
parentc668d220aa8a2505de701a57803040a7def291b0 (diff)
downloadECTester-0fdfe31112924f51ca503c0ec0fff62ec20403c1.tar.gz
ECTester-0fdfe31112924f51ca503c0ec0fff62ec20403c1.tar.zst
ECTester-0fdfe31112924f51ca503c0ec0fff62ec20403c1.zip
-rw-r--r--src/cz/crcs/ectester/common/cli/ParserOptions.java13
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeCommandLine.java27
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeParser.java32
-rw-r--r--src/cz/crcs/ectester/standalone/ECTesterStandalone.java68
-rw-r--r--src/cz/crcs/ectester/standalone/consts/Ident.java4
5 files changed, 124 insertions, 20 deletions
diff --git a/src/cz/crcs/ectester/common/cli/ParserOptions.java b/src/cz/crcs/ectester/common/cli/ParserOptions.java
index 4216ce3..ee2097e 100644
--- a/src/cz/crcs/ectester/common/cli/ParserOptions.java
+++ b/src/cz/crcs/ectester/common/cli/ParserOptions.java
@@ -3,18 +3,27 @@ package cz.crcs.ectester.common.cli;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
+import java.util.Collections;
+import java.util.List;
+
/**
* @author Jan Jancar johny@neuromancer.sk
*/
public class ParserOptions {
private CommandLineParser parser;
private Options options;
+ private List<Argument> arguments;
public ParserOptions(CommandLineParser parser, Options options) {
this.parser = parser;
this.options = options;
}
+ public ParserOptions(CommandLineParser parser, Options options, List<Argument> arguments) {
+ this(parser, options);
+ this.arguments = arguments;
+ }
+
public CommandLineParser getParser() {
return parser;
}
@@ -22,4 +31,8 @@ public class ParserOptions {
public Options getOptions() {
return options;
}
+
+ public List<Argument> getArguments() {
+ return Collections.unmodifiableList(arguments);
+ }
}
diff --git a/src/cz/crcs/ectester/common/cli/TreeCommandLine.java b/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
index 82d1e15..39607dc 100644
--- a/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
+++ b/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
@@ -3,6 +3,7 @@ package cz.crcs.ectester.common.cli;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
+import sun.reflect.generics.tree.Tree;
import java.util.Iterator;
import java.util.List;
@@ -55,11 +56,21 @@ public class TreeCommandLine extends CommandLine {
return cli;
}
+ public int getDepth() {
+ if (next == null) {
+ return 0;
+ }
+ return next.getDepth() + 1;
+ }
+
private <T> T getOption(String opt, BiFunction<CommandLine, String, T> getter, T defaultValue) {
if (opt.contains(".")) {
String[] parts = opt.split("\\.", 2);
if (next != null && parts[0].equals(next.getName())) {
- return getter.apply(next, parts[1]);
+ T result = getter.apply(next, parts[1]);
+ if (result != null)
+ return result;
+ return defaultValue;
}
return defaultValue;
}
@@ -138,11 +149,21 @@ public class TreeCommandLine extends CommandLine {
return cli.getOptions();
}
+ public boolean hasArg(int index) {
+ if (next != null) {
+ return next.hasArg(index);
+ }
+ return Math.abs(index) < cli.getArgs().length;
+ }
+
public String getArg(int index) {
- if (index < 0 || index >= cli.getArgs().length) {
+ if (next != null) {
+ return next.getArg(index);
+ }
+ if (index >= cli.getArgs().length) {
return null;
}
- return cli.getArgs()[index];
+ return index < 0 ? cli.getArgs()[cli.getArgs().length + index] : cli.getArgs()[index];
}
@Override
diff --git a/src/cz/crcs/ectester/common/cli/TreeParser.java b/src/cz/crcs/ectester/common/cli/TreeParser.java
index 9b197a5..77cce30 100644
--- a/src/cz/crcs/ectester/common/cli/TreeParser.java
+++ b/src/cz/crcs/ectester/common/cli/TreeParser.java
@@ -3,6 +3,7 @@ package cz.crcs.ectester.common.cli;
import org.apache.commons.cli.*;
import java.util.*;
+import java.util.stream.Collectors;
/**
* @author Jan Jancar johny@neuromancer.sk
@@ -53,10 +54,10 @@ public class TreeParser implements CommandLineParser {
CommandLine cli = thisParser.parse(options, arguments, properties, true);
CommandLine subCli = null;
- String[] args = cli.getArgs();
+ String[] cliArgs = cli.getArgs();
String sub = null;
- if (args.length != 0) {
- sub = args[0];
+ if (cliArgs.length != 0) {
+ sub = cliArgs[0];
List<String> matches = new LinkedList<>();
String finalSub = sub;
@@ -73,8 +74,8 @@ public class TreeParser implements CommandLineParser {
if (matches.size() == 1) {
sub = matches.get(0);
ParserOptions subparser = parsers.get(sub);
- String[] remainingArgs = new String[args.length - 1];
- System.arraycopy(args, 1, remainingArgs, 0, args.length - 1);
+ String[] remainingArgs = new String[cliArgs.length - 1];
+ System.arraycopy(cliArgs, 1, remainingArgs, 0, cliArgs.length - 1);
subCli = subparser.getParser().parse(subparser.getOptions(), remainingArgs, true);
} else if (matches.size() > 1) {
throw new AmbiguousOptionException(sub, matches);
@@ -84,14 +85,35 @@ public class TreeParser implements CommandLineParser {
throw new MissingOptionException(new ArrayList(parsers.keySet()));
}
}
+
+ long requiredArgs = args.stream().filter(Argument::isRequired).count();
+ String reqArgs = String.join(" ", args.stream().filter(Argument::isRequired).map(Argument::getName).collect(Collectors.toList()));
+
if (subCli instanceof TreeCommandLine) {
TreeCommandLine subTreeCli = (TreeCommandLine) subCli;
+
+ TreeCommandLine lastCli = subTreeCli;
+ while (lastCli.getNext() != null) {
+ lastCli = lastCli.getNext();
+ }
+
+ if (lastCli.getArgs().length < requiredArgs) {
+ throw new MissingArgumentException("Not enough arguments: " + reqArgs);
+ }
+
subTreeCli.setName(sub);
return new TreeCommandLine(cli, subTreeCli);
} else if (subCli != null) {
+ if (subCli.getArgs().length < requiredArgs) {
+ throw new MissingArgumentException("Not enough arguments: " + reqArgs);
+ }
+
TreeCommandLine subTreeCli = new TreeCommandLine(sub, subCli, null);
return new TreeCommandLine(cli, subTreeCli);
} else {
+ if (cliArgs.length < requiredArgs) {
+ throw new MissingArgumentException("Not enough arguments: " + reqArgs);
+ }
return new TreeCommandLine(cli, null);
}
}
diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
index 3736e12..dc65856 100644
--- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
+++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
@@ -1,24 +1,26 @@
package cz.crcs.ectester.standalone;
import cz.crcs.ectester.common.cli.*;
-import cz.crcs.ectester.common.test.Result;
import cz.crcs.ectester.data.EC_Store;
+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.BouncyCastleLib;
import cz.crcs.ectester.standalone.libs.ECLibrary;
import cz.crcs.ectester.standalone.libs.JavaECLibrary;
import cz.crcs.ectester.standalone.libs.SunECLib;
-import cz.crcs.ectester.standalone.test.KeyGenerationTest;
-import cz.crcs.ectester.standalone.test.KeyGenerationTestable;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
+import sun.reflect.generics.tree.Tree;
import java.io.IOException;
+import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.*;
+import java.util.stream.Collectors;
/**
* Standalone part of ECTester, a tool for testing Elliptic curve implementations in software libraries.
@@ -30,10 +32,11 @@ public class ECTesterStandalone {
private ECLibrary[] libs = new ECLibrary[]{new SunECLib(), new BouncyCastleLib()};
private EC_Store dataStore;
- private Config cfg;
+ private Config cfg = new Config();
private Options opts = new Options();
private TreeParser optParser;
+ private TreeCommandLine cli;
private static final String VERSION = "v0.1.0";
private static final String DESCRIPTION = "ECTesterStandalone " + VERSION + ", an Elliptic Curve Cryptography support tester/utility.";
private static final String LICENSE = "MIT Licensed\nCopyright (c) 2016-2017 Petr Svenda <petr@svenda.com>";
@@ -42,9 +45,9 @@ public class ECTesterStandalone {
private void run(String[] args) {
try {
- TreeCommandLine cli = parseArgs(args);
+ cli = parseArgs(args);
- if (cli.hasOption("help")) {
+ if (cli.hasOption("help") || cli.getNext() == null) {
CLITools.help("ECTesterStandalone.jar", CLI_HEADER, opts, optParser, CLI_FOOTER, true);
return;
} else if (cli.hasOption("version")) {
@@ -52,7 +55,7 @@ public class ECTesterStandalone {
return;
}
- cfg = new Config();
+ cfg.readOptions(cli);
dataStore = new EC_Store();
if (cli.hasOption("list-named")) {
@@ -104,6 +107,8 @@ public class ECTesterStandalone {
Options generateOpts = new Options();
generateOpts.addOption(Option.builder("n").longOpt("amount").hasArg().argName("amount").optionalArg(false).desc("Generate [amount] of EC keys.").build());
+ generateOpts.addOption(Option.builder("t").longOpt("type").hasArg().argName("type").optionalArg(false).desc("Set KeyPairGenerator object [type].").build());
+ generateOpts.addOption(Option.builder("b").longOpt("bits").hasArg().argName("n").optionalArg(false).desc("What size of curve to use.").build());
ParserOptions generate = new ParserOptions(new DefaultParser(), generateOpts);
actions.put("generate", generate);
@@ -121,7 +126,9 @@ public class ECTesterStandalone {
ParserOptions listLibs = new ParserOptions(new DefaultParser(), listLibsOpts);
actions.put("list-libs", listLibs);
- optParser = new TreeParser(actions, false);
+ List<Argument> baseArgs = new LinkedList<>();
+ baseArgs.add(new Argument("lib", "What library to use.", false));
+ optParser = new TreeParser(actions, false, baseArgs);
opts.addOption(Option.builder("V").longOpt("version").desc("Print version info.").build());
opts.addOption(Option.builder("h").longOpt("help").desc("Print help.").build());
@@ -133,15 +140,35 @@ public class ECTesterStandalone {
*
*/
private void generate() {
+ if (!cli.hasArg(0)) {
+ System.err.println("Missing library name argument.");
+ return;
+ }
+ String libraryName = cli.getArg(0);
+
+ List<ECLibrary> matchedLibs = new LinkedList<>();
for (ECLibrary lib : libs) {
+ if (lib.name().toLowerCase().contains(libraryName.toLowerCase())) {
+ matchedLibs.add(lib);
+ }
+ }
+ if (matchedLibs.size() == 0) {
+ System.err.println("No library found.");
+ } else if (matchedLibs.size() > 1) {
+ System.err.println("Multiple matching libraries found: " + String.join(",", matchedLibs.stream().map(ECLibrary::name).collect(Collectors.toList())));
+ } else {
+ ECLibrary lib = matchedLibs.get(0);
if (lib instanceof JavaECLibrary) {
JavaECLibrary jlib = (JavaECLibrary) lib;
for (KeyPairGeneratorIdent ident : lib.getKPGs()) {
+ if (!ident.contains(cli.getOptionValue("generate.type", "EC"))) {
+ continue;
+ }
try {
KeyPairGenerator kpg = ident.getInstance(jlib.getProvider());
- KeyGenerationTestable kgt = new KeyGenerationTestable(kpg, 192);
- KeyGenerationTest kt = KeyGenerationTest.expect(kgt, Result.ExpectedValue.SUCCESS);
- System.out.println(kt);
+ kpg.initialize(Integer.parseInt(cli.getOptionValue("generate.bits", "256")));
+ KeyPair kp = kpg.genKeyPair();
+ System.out.println(kp.getPrivate());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
@@ -156,7 +183,19 @@ public class ECTesterStandalone {
private void listLibraries() {
for (ECLibrary lib : libs) {
if (lib.isInitialized()) {
- System.out.println(lib.name());
+ System.out.println("\t- " + lib.name());
+ Set<KeyPairGeneratorIdent> kpgs = lib.getKPGs();
+ if (!kpgs.isEmpty()) {
+ System.out.println("\t\t- KeyPairGenerators: " + String.join(",", kpgs.stream().map(KeyPairGeneratorIdent::getName).collect(Collectors.toList())));
+ }
+ Set<KeyAgreementIdent> eckas = lib.getECKAs();
+ if (!eckas.isEmpty()) {
+ System.out.println("\t\t- KeyAgreements: " + String.join(",", eckas.stream().map(KeyAgreementIdent::getName).collect(Collectors.toList())));
+ }
+ Set<SignatureIdent> sigs = lib.getECSigs();
+ if (!eckas.isEmpty()) {
+ System.out.println("\t\t- Signatures: " + String.join(",", sigs.stream().map(SignatureIdent::getName).collect(Collectors.toList())));
+ }
}
}
}
@@ -167,6 +206,11 @@ public class ECTesterStandalone {
}
public static class Config {
+ public ECLibrary selected;
+ boolean readOptions(TreeCommandLine cli) {
+
+ return true;
+ }
}
}
diff --git a/src/cz/crcs/ectester/standalone/consts/Ident.java b/src/cz/crcs/ectester/standalone/consts/Ident.java
index eaea0e3..84cce2d 100644
--- a/src/cz/crcs/ectester/standalone/consts/Ident.java
+++ b/src/cz/crcs/ectester/standalone/consts/Ident.java
@@ -24,6 +24,10 @@ public abstract class Ident {
return Collections.unmodifiableSet(idents);
}
+ public boolean contains(String other) {
+ return name.equals(other) || idents.contains(other);
+ }
+
@Override
public boolean equals(Object obj) {
if (this == obj) {