aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone
diff options
context:
space:
mode:
authorJ08nY2017-11-23 01:32:41 +0100
committerJ08nY2017-11-23 01:32:41 +0100
commit0fdfe31112924f51ca503c0ec0fff62ec20403c1 (patch)
tree35431fa7a6e96ce9ad8132eca3340048632699bf /src/cz/crcs/ectester/standalone
parentc668d220aa8a2505de701a57803040a7def291b0 (diff)
downloadECTester-0fdfe31112924f51ca503c0ec0fff62ec20403c1.tar.gz
ECTester-0fdfe31112924f51ca503c0ec0fff62ec20403c1.tar.zst
ECTester-0fdfe31112924f51ca503c0ec0fff62ec20403c1.zip
Sketch out KeyPairGeneration in standalone.
Diffstat (limited to '')
-rw-r--r--src/cz/crcs/ectester/standalone/ECTesterStandalone.java68
-rw-r--r--src/cz/crcs/ectester/standalone/consts/Ident.java4
2 files changed, 60 insertions, 12 deletions
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) {