aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2017-11-28 00:25:54 +0100
committerJ08nY2017-11-28 00:25:54 +0100
commite22139e18d28906f9533a1dc31e0622080b5f35c (patch)
treed06fdb8d34d0187889140e12b01ff071e1b8ec8a
parent9bce1e13ec136c06650868acf3438e789e366d5f (diff)
downloadECTester-e22139e18d28906f9533a1dc31e0622080b5f35c.tar.gz
ECTester-e22139e18d28906f9533a1dc31e0622080b5f35c.tar.zst
ECTester-e22139e18d28906f9533a1dc31e0622080b5f35c.zip
-rw-r--r--src/cz/crcs/ectester/standalone/ECTesterStandalone.java112
-rw-r--r--src/cz/crcs/ectester/standalone/libs/ECLibrary.java4
-rw-r--r--src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java54
-rw-r--r--src/cz/crcs/ectester/standalone/libs/ProviderECLibrary.java15
-rw-r--r--src/cz/crcs/ectester/standalone/libs/TomcryptLib.java2
5 files changed, 100 insertions, 87 deletions
diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
index e36ffcd..ec681c7 100644
--- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
+++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
@@ -17,11 +17,9 @@ import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
+import javax.crypto.KeyAgreement;
import java.io.IOException;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.KeyPair;
-import java.security.KeyPairGenerator;
-import java.security.NoSuchAlgorithmException;
+import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECParameterSpec;
@@ -87,9 +85,13 @@ public class ECTesterStandalone {
export();
}
- } catch (ParseException | NoSuchAlgorithmException | IOException ex) {
+ } catch (ParseException | IOException ex) {
System.err.println(ex.getMessage());
} catch (InvalidAlgorithmParameterException e) {
+ 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();
}
}
@@ -103,6 +105,9 @@ public class ECTesterStandalone {
Options ecdhOpts = new Options();
ecdhOpts.addOption(Option.builder("t").longOpt("type").desc("Set KeyAgreement object [type].").hasArg().argName("type").optionalArg(false).build());
+ ecdhOpts.addOption(Option.builder("n").longOpt("amount").hasArg().argName("amount").optionalArg(false).desc("Do ECDH [amount] times.").build());
+ ecdhOpts.addOption(Option.builder("b").longOpt("bits").hasArg().argName("n").optionalArg(false).desc("What size of curve to use.").build());
+ ecdhOpts.addOption(Option.builder("nc").longOpt("named-curve").desc("Use a named curve, from CurveDB: <cat/id>").hasArg().argName("cat/id").build());
ParserOptions ecdh = new ParserOptions(new DefaultParser(), ecdhOpts);
actions.put("ecdh", ecdh);
@@ -156,11 +161,11 @@ public class ECTesterStandalone {
if (!kpgs.isEmpty()) {
System.out.println("\t\t- KeyPairGenerators: " + String.join(",", kpgs.stream().map(KeyPairGeneratorIdent::getName).collect(Collectors.toList())));
}
- Set<KeyAgreementIdent> eckas = lib.getECKAs();
+ Set<KeyAgreementIdent> eckas = lib.getKAs();
if (!eckas.isEmpty()) {
System.out.println("\t\t- KeyAgreements: " + String.join(",", eckas.stream().map(KeyAgreementIdent::getName).collect(Collectors.toList())));
}
- Set<SignatureIdent> sigs = lib.getECSigs();
+ Set<SignatureIdent> sigs = lib.getSigs();
if (!eckas.isEmpty()) {
System.out.println("\t\t- Signatures: " + String.join(",", sigs.stream().map(SignatureIdent::getName).collect(Collectors.toList())));
}
@@ -171,8 +176,68 @@ public class ECTesterStandalone {
/**
*
*/
- private void ecdh() {
+ 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 = null;
+ for (KeyAgreementIdent ident : lib.getKAs()) {
+ if (ident.contains(algo)) {
+ kaIdent = ident;
+ break;
+ }
+ }
+
+ KeyPairGeneratorIdent kpIdent = null;
+ for (KeyPairGeneratorIdent ident : lib.getKPGs()) {
+ if (ident.contains("EC")) {
+ kpIdent = ident;
+ break;
+ }
+ }
+
+ if (kaIdent == null || kpIdent == null) {
+ throw new NoSuchAlgorithmException(algo);
+ } else {
+ KeyAgreement ka = kaIdent.getInstance(lib.getProvider());
+ KeyPairGenerator kpg = kpIdent.getInstance(lib.getProvider());
+ 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;
+ }
+ kpg.initialize(curve.toSpec());
+ }
+ 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();
+
+ ECPrivateKey privkey = (ECPrivateKey) one.getPrivate();
+ ECPublicKey pubkey = (ECPublicKey) other.getPublic();
+
+ long elapsed = -System.nanoTime();
+ ka.init(privkey);
+ ka.doPhase(pubkey, true);
+ elapsed += System.nanoTime();
+ byte[] result = ka.generateSecret();
+
+ String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(pubkey.getW()), 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));
+ }
+ }
+ }
}
/**
@@ -187,10 +252,10 @@ public class ECTesterStandalone {
*/
private void generate() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
if (cfg.selected instanceof ProviderECLibrary) {
- ProviderECLibrary jlib = (ProviderECLibrary) cfg.selected;
+ ProviderECLibrary lib = (ProviderECLibrary) cfg.selected;
KeyPairGeneratorIdent ident = null;
String algo = cli.getOptionValue("generate.type", "EC");
- for (KeyPairGeneratorIdent kpIdent : jlib.getKPGs()) {
+ for (KeyPairGeneratorIdent kpIdent : lib.getKPGs()) {
if (kpIdent.contains(algo)) {
ident = kpIdent;
break;
@@ -199,7 +264,7 @@ public class ECTesterStandalone {
if (ident == null) {
throw new NoSuchAlgorithmException(algo);
} else {
- KeyPairGenerator kpg = ident.getInstance(jlib.getProvider());
+ KeyPairGenerator kpg = ident.getInstance(lib.getProvider());
if (cli.hasOption("generate.bits")) {
int bits = Integer.parseInt(cli.getOptionValue("generate.bits"));
kpg.initialize(bits);
@@ -212,7 +277,7 @@ public class ECTesterStandalone {
}
kpg.initialize(curve.toSpec());
}
- System.out.println("index;time;pubW;privS");
+ System.out.println("index;nanotime;pubW;privS");
int amount = Integer.parseInt(cli.getOptionValue("generate.amount", "1"));
for (int i = 0; i < amount; ++i) {
@@ -224,7 +289,7 @@ public class ECTesterStandalone {
String pub = ByteUtil.bytesToHex(ECUtil.toX962Uncompressed(publicKey.getW()), false);
String priv = ByteUtil.bytesToHex(privateKey.getS().toByteArray(), false);
- System.out.println(String.format("%d;%d;%s;%s", i, elapsed / 1000000, pub, priv));
+ System.out.println(String.format("%d;%d;%s;%s", i, elapsed, pub, priv));
}
}
}
@@ -242,10 +307,10 @@ public class ECTesterStandalone {
*/
private void export() throws NoSuchAlgorithmException, IOException {
if (cfg.selected instanceof ProviderECLibrary) {
- ProviderECLibrary jlib = (ProviderECLibrary) cfg.selected;
+ ProviderECLibrary lib = (ProviderECLibrary) cfg.selected;
KeyPairGeneratorIdent ident = null;
String algo = cli.getOptionValue("export.type", "EC");
- for (KeyPairGeneratorIdent kpIdent : jlib.getKPGs()) {
+ for (KeyPairGeneratorIdent kpIdent : lib.getKPGs()) {
if (kpIdent.contains(algo)) {
ident = kpIdent;
break;
@@ -254,7 +319,7 @@ public class ECTesterStandalone {
if (ident == null) {
throw new NoSuchAlgorithmException(algo);
} else {
- KeyPairGenerator kpg = ident.getInstance(jlib.getProvider());
+ KeyPairGenerator kpg = ident.getInstance(lib.getProvider());
if (cli.hasOption("export.bits")) {
int bits = Integer.parseInt(cli.getOptionValue("export.bits"));
kpg.initialize(bits);
@@ -287,23 +352,14 @@ public class ECTesterStandalone {
}
boolean readOptions(TreeCommandLine cli) {
- if (cli.isNext("generate")) {
- if (!cli.hasArg(-1)) {
- System.err.println("Missing library name argument.");
- return false;
- }
-
- if (cli.hasOption("generate.bits") && cli.hasOption("generate.named-curve")) {
- System.err.println("You can only specify bitsize or a named curve, nor both.");
- return false;
- }
- } else if (cli.isNext("export")) {
+ if (cli.isNext("generate") || cli.isNext("export") || cli.isNext("ecdh")) {
if (!cli.hasArg(-1)) {
System.err.println("Missing library name argument.");
return false;
}
- if (cli.hasOption("export.bits") && cli.hasOption("export.named-curve")) {
+ String next = cli.getNextName();
+ if (cli.hasOption(next + ".bits") && cli.hasOption(next + ".named-curve")) {
System.err.println("You can only specify bitsize or a named curve, nor both.");
return false;
}
diff --git a/src/cz/crcs/ectester/standalone/libs/ECLibrary.java b/src/cz/crcs/ectester/standalone/libs/ECLibrary.java
index 7e26dbe..1e73e61 100644
--- a/src/cz/crcs/ectester/standalone/libs/ECLibrary.java
+++ b/src/cz/crcs/ectester/standalone/libs/ECLibrary.java
@@ -14,9 +14,9 @@ public interface ECLibrary {
boolean isInitialized();
- Set<KeyAgreementIdent> getECKAs();
+ Set<KeyAgreementIdent> getKAs();
- Set<SignatureIdent> getECSigs();
+ Set<SignatureIdent> getSigs();
Set<KeyPairGeneratorIdent> getKPGs();
diff --git a/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java b/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java
index 40691e6..44fb47b 100644
--- a/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java
+++ b/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java
@@ -1,9 +1,5 @@
package cz.crcs.ectester.standalone.libs;
-import cz.crcs.ectester.standalone.consts.KeyAgreementIdent;
-import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent;
-import cz.crcs.ectester.standalone.consts.SignatureIdent;
-
import java.io.File;
import java.io.IOException;
import java.net.URL;
@@ -13,14 +9,11 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.Provider;
-import java.util.Collections;
-import java.util.Set;
/**
* @author Jan Jancar johny@neuromancer.sk
*/
-public abstract class NativeECLibrary implements ECLibrary {
- private ProviderECLibrary loaded;
+public abstract class NativeECLibrary extends ProviderECLibrary {
private String resource;
private String libname;
@@ -80,52 +73,13 @@ public abstract class NativeECLibrary implements ECLibrary {
System.load(libPath.toString());
- loaded = new ProviderECLibrary(getProvider());
- return true;
+ provider = createProvider();
+ return super.initialize();
} catch (IOException ignored) {
}
return false;
}
- public abstract Provider getProvider();
-
- @Override
- public boolean isInitialized() {
- return loaded != null && loaded.isInitialized();
- }
-
- @Override
- public Set<KeyAgreementIdent> getECKAs() {
- if (!isInitialized()) {
- return Collections.emptySet();
- }
- return loaded.getECKAs();
- }
-
- @Override
- public Set<SignatureIdent> getECSigs() {
- if (!isInitialized()) {
- return Collections.emptySet();
- }
- return loaded.getECSigs();
- }
-
- @Override
- public Set<KeyPairGeneratorIdent> getKPGs() {
- if (!isInitialized()) {
- return Collections.emptySet();
- }
- return loaded.getKPGs();
- }
-
- @Override
- public String name() {
- return loaded.name();
- }
-
- @Override
- public String toString() {
- return name();
- }
+ abstract Provider createProvider();
}
diff --git a/src/cz/crcs/ectester/standalone/libs/ProviderECLibrary.java b/src/cz/crcs/ectester/standalone/libs/ProviderECLibrary.java
index 879cc16..9108eaf 100644
--- a/src/cz/crcs/ectester/standalone/libs/ProviderECLibrary.java
+++ b/src/cz/crcs/ectester/standalone/libs/ProviderECLibrary.java
@@ -14,13 +14,16 @@ import java.util.function.Function;
/**
* @author Jan Jancar johny@neuromancer.sk
*/
-public class ProviderECLibrary implements ECLibrary {
- private Provider provider;
- private boolean initialized;
+public abstract class ProviderECLibrary implements ECLibrary {
+ Provider provider;
+ private boolean initialized = false;
+
+ public ProviderECLibrary() {
+
+ }
public ProviderECLibrary(Provider provider) {
this.provider = provider;
- this.initialized = false;
}
@Override
@@ -60,12 +63,12 @@ public class ProviderECLibrary implements ECLibrary {
}
@Override
- public Set<KeyAgreementIdent> getECKAs() {
+ public Set<KeyAgreementIdent> getKAs() {
return getIdents("KeyAgreement", KeyAgreementIdent::get);
}
@Override
- public Set<SignatureIdent> getECSigs() {
+ public Set<SignatureIdent> getSigs() {
return getIdents("Signature", SignatureIdent::get);
}
diff --git a/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java b/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java
index 31d6812..fe4a79d 100644
--- a/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java
+++ b/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java
@@ -12,5 +12,5 @@ public class TomcryptLib extends NativeECLibrary {
}
@Override
- public native Provider getProvider();
+ native Provider createProvider();
}