From 9e615b101398bd4c8e2678bf86337e2756a8ee7a Mon Sep 17 00:00:00 2001 From: J08nY Date: Sun, 12 Nov 2017 23:39:35 +0100 Subject: Implement collecting of supported KeyAgreement and Signature objects. --- src/cz/crcs/ectester/standalone/libs/ECLibrary.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/cz/crcs/ectester/standalone/libs/ECLibrary.java (limited to 'src/cz/crcs/ectester/standalone/libs/ECLibrary.java') diff --git a/src/cz/crcs/ectester/standalone/libs/ECLibrary.java b/src/cz/crcs/ectester/standalone/libs/ECLibrary.java new file mode 100644 index 0000000..b2792bd --- /dev/null +++ b/src/cz/crcs/ectester/standalone/libs/ECLibrary.java @@ -0,0 +1,21 @@ +package cz.crcs.ectester.standalone.libs; + +import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; +import cz.crcs.ectester.standalone.consts.SignatureIdent; + +import java.util.Set; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public interface ECLibrary { + boolean initialize(); + + boolean isInitialized(); + + Set getECKAs(); + + Set getECSigs(); + + String name(); +} -- cgit v1.2.3-70-g09d2 From 80e14c7d3f9eeec34f0236bfb8c595033142756a Mon Sep 17 00:00:00 2001 From: J08nY Date: Mon, 13 Nov 2017 23:18:35 +0100 Subject: Add KeyPairGenerator idents. --- .../ectester/standalone/ECTesterStandalone.java | 25 +++++++++++++-- .../standalone/consts/KeyPairGeneratorIdent.java | 36 ++++++++++++++++++++++ .../crcs/ectester/standalone/libs/ECLibrary.java | 3 ++ .../ectester/standalone/libs/JavaECLibrary.java | 35 +++++++++++---------- 4 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 src/cz/crcs/ectester/standalone/consts/KeyPairGeneratorIdent.java (limited to 'src/cz/crcs/ectester/standalone/libs/ECLibrary.java') diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java index 47b7121..016d095 100644 --- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java +++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java @@ -3,12 +3,17 @@ package cz.crcs.ectester.standalone; import cz.crcs.ectester.applet.EC_Consts; import cz.crcs.ectester.common.ec.EC_Curve; import cz.crcs.ectester.data.EC_Store; +import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; 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 org.apache.commons.cli.*; import java.io.IOException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; import java.util.Arrays; /** @@ -45,9 +50,23 @@ public class ECTesterStandalone { cfg = new Config(); dataStore = new EC_Store(); for (ECLibrary lib : libs) { - lib.initialize(); - lib.getECKAs(); - lib.getECSigs(); + if (lib instanceof JavaECLibrary) { + JavaECLibrary jlib = (JavaECLibrary) lib; + lib.initialize(); + lib.getECKAs(); + lib.getECSigs(); + for (KeyPairGeneratorIdent ident : lib.getKPGs()) { + try { + KeyPairGenerator kpg = ident.getInstance(jlib.getProvider()); + kpg.initialize(192); + KeyPair kp = kpg.genKeyPair(); + System.out.println(kp); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + } + } + } System.out.println(Arrays.toString(libs)); diff --git a/src/cz/crcs/ectester/standalone/consts/KeyPairGeneratorIdent.java b/src/cz/crcs/ectester/standalone/consts/KeyPairGeneratorIdent.java new file mode 100644 index 0000000..f806282 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/consts/KeyPairGeneratorIdent.java @@ -0,0 +1,36 @@ +package cz.crcs.ectester.standalone.consts; + +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.Provider; +import java.util.LinkedList; +import java.util.List; + +public class KeyPairGeneratorIdent extends Ident { + private static final List ALL = new LinkedList<>(); + + static { + ALL.add(new KeyPairGeneratorIdent("EC")); + ALL.add(new KeyPairGeneratorIdent("ECDH")); + ALL.add(new KeyPairGeneratorIdent("ECDSA")); + ALL.add(new KeyPairGeneratorIdent("ECDHC")); + ALL.add(new KeyPairGeneratorIdent("ECMQV")); + } + + public static KeyPairGeneratorIdent get(String ident) { + for (KeyPairGeneratorIdent kg : ALL) { + if (kg.getIdents().contains(ident)) { + return kg; + } + } + return null; + } + + public KeyPairGeneratorIdent(String name, String... aliases) { + super(name, aliases); + } + + public KeyPairGenerator getInstance(Provider provider) throws NoSuchAlgorithmException { + return KeyPairGenerator.getInstance(name, provider); + } +} diff --git a/src/cz/crcs/ectester/standalone/libs/ECLibrary.java b/src/cz/crcs/ectester/standalone/libs/ECLibrary.java index b2792bd..7e26dbe 100644 --- a/src/cz/crcs/ectester/standalone/libs/ECLibrary.java +++ b/src/cz/crcs/ectester/standalone/libs/ECLibrary.java @@ -1,6 +1,7 @@ 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.util.Set; @@ -17,5 +18,7 @@ public interface ECLibrary { Set getECSigs(); + Set getKPGs(); + String name(); } diff --git a/src/cz/crcs/ectester/standalone/libs/JavaECLibrary.java b/src/cz/crcs/ectester/standalone/libs/JavaECLibrary.java index f8848da..5689b2b 100644 --- a/src/cz/crcs/ectester/standalone/libs/JavaECLibrary.java +++ b/src/cz/crcs/ectester/standalone/libs/JavaECLibrary.java @@ -1,12 +1,15 @@ package cz.crcs.ectester.standalone.libs; +import cz.crcs.ectester.standalone.consts.Ident; import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; +import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; import cz.crcs.ectester.standalone.consts.SignatureIdent; import java.security.Provider; import java.security.Security; import java.util.HashSet; import java.util.Set; +import java.util.function.Function; /** * @author Jan Jancar johny@neuromancer.sk @@ -39,34 +42,32 @@ public abstract class JavaECLibrary implements ECLibrary { return initialized; } - @Override - public Set getECKAs() { - Set results = new HashSet<>(); + private Set getIdents(String type, Function getter) { + Set results = new HashSet<>(); for (Provider.Service service : provider.getServices()) { - if (service.getType().equals("KeyAgreement")) { - KeyAgreementIdent id = KeyAgreementIdent.get(service.getAlgorithm()); + if (service.getType().equals(type)) { + T id = getter.apply(service.getAlgorithm()); if (id != null) { results.add(id); } } } - System.out.println(results); return results; } + @Override + public Set getECKAs() { + return getIdents("KeyAgreement", KeyAgreementIdent::get); + } + @Override public Set getECSigs() { - Set results = new HashSet<>(); - for (Provider.Service service : provider.getServices()) { - if (service.getType().equals("Signature")) { - SignatureIdent id = SignatureIdent.get(service.getAlgorithm()); - if (id != null) { - results.add(id); - } - } - } - System.out.println(results); - return results; + return getIdents("Signature", SignatureIdent::get); + } + + @Override + public Set getKPGs() { + return getIdents("KeyPairGenerator", KeyPairGeneratorIdent::get); } @Override -- cgit v1.2.3-70-g09d2 From e22139e18d28906f9533a1dc31e0622080b5f35c Mon Sep 17 00:00:00 2001 From: J08nY Date: Tue, 28 Nov 2017 00:25:54 +0100 Subject: Add ECDH testing to standalone part. --- .../ectester/standalone/ECTesterStandalone.java | 112 +++++++++++++++------ .../crcs/ectester/standalone/libs/ECLibrary.java | 4 +- .../ectester/standalone/libs/NativeECLibrary.java | 54 +--------- .../standalone/libs/ProviderECLibrary.java | 15 +-- .../crcs/ectester/standalone/libs/TomcryptLib.java | 2 +- 5 files changed, 100 insertions(+), 87 deletions(-) (limited to 'src/cz/crcs/ectester/standalone/libs/ECLibrary.java') 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: ").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 eckas = lib.getECKAs(); + Set eckas = lib.getKAs(); if (!eckas.isEmpty()) { System.out.println("\t\t- KeyAgreements: " + String.join(",", eckas.stream().map(KeyAgreementIdent::getName).collect(Collectors.toList()))); } - Set sigs = lib.getECSigs(); + Set 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 getECKAs(); + Set getKAs(); - Set getECSigs(); + Set getSigs(); Set 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 getECKAs() { - if (!isInitialized()) { - return Collections.emptySet(); - } - return loaded.getECKAs(); - } - - @Override - public Set getECSigs() { - if (!isInitialized()) { - return Collections.emptySet(); - } - return loaded.getECSigs(); - } - - @Override - public Set 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 getECKAs() { + public Set getKAs() { return getIdents("KeyAgreement", KeyAgreementIdent::get); } @Override - public Set getECSigs() { + public Set 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(); } -- cgit v1.2.3-70-g09d2 From 03265f4cf09b7bb59c09af722b8f44445a635d83 Mon Sep 17 00:00:00 2001 From: J08nY Date: Wed, 29 Nov 2017 02:22:30 +0100 Subject: Add listing of supported curves to standalone libs. --- build-standalone.xml | 4 +-- nbproject/reader/project.properties | 2 +- nbproject/standalone/project.properties | 2 +- .../ectester/standalone/ECTesterStandalone.java | 5 +++ .../ectester/standalone/libs/BouncyCastleLib.java | 14 ++++++++ .../crcs/ectester/standalone/libs/ECLibrary.java | 2 ++ src/cz/crcs/ectester/standalone/libs/SunECLib.java | 14 ++++++++ .../crcs/ectester/standalone/libs/TomcryptLib.java | 4 +++ .../standalone/libs/jni/NativeProvider.java | 8 ++--- src/cz/crcs/ectester/standalone/libs/jni/native.h | 8 +++++ .../crcs/ectester/standalone/libs/jni/tomcrypt.c | 41 +++++++++++++--------- 11 files changed, 78 insertions(+), 26 deletions(-) (limited to 'src/cz/crcs/ectester/standalone/libs/ECLibrary.java') diff --git a/build-standalone.xml b/build-standalone.xml index 300eec8..6e65c67 100644 --- a/build-standalone.xml +++ b/build-standalone.xml @@ -76,10 +76,10 @@ - + - + diff --git a/nbproject/reader/project.properties b/nbproject/reader/project.properties index d372fd5..21441b3 100644 --- a/nbproject/reader/project.properties +++ b/nbproject/reader/project.properties @@ -6,7 +6,7 @@ annotation.processing.source.output=${build.generated.sources.dir}/ap-source-out application.title=ECTesterReader application.vendor=xsvenda build.classes.dir=${build.dir}/classes -build.classes.excludes=**/*.java,**/*.form,**/*.c,**/*.h,**/*.a +build.classes.excludes=**/*.java,**/*.form,**/*.c,**/*.h,**/*.a,**/*.o,**/Makefile # This directory is removed when the project is cleaned: build.dir=build build.generated.dir=${build.dir}/generated diff --git a/nbproject/standalone/project.properties b/nbproject/standalone/project.properties index 7714914..9fed4c2 100644 --- a/nbproject/standalone/project.properties +++ b/nbproject/standalone/project.properties @@ -6,7 +6,7 @@ annotation.processing.source.output=${build.generated.sources.dir}/ap-source-out application.title=ECTesterStandalone application.vendor=xsvenda build.classes.dir=${build.dir}/classes -build.classes.excludes=**/*.java,**/*.form,**/*.c,**/*.h,**/*.a +build.classes.excludes=**/*.java,**/*.form,**/*.c,**/*.h,**/*.a,**/*.o,**/Makefile # This directory is removed when the project is cleaned: build.dir=build build.generated.dir=${build.dir}/generated diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java index de9953a..9fca2b6 100644 --- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java +++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java @@ -174,6 +174,11 @@ public class ECTesterStandalone { if (!eckas.isEmpty()) { System.out.println("\t\t- Signatures: " + String.join(",", sigs.stream().map(SignatureIdent::getName).collect(Collectors.toList()))); } + Set curves = lib.getCurves(); + if (!curves.isEmpty()) { + System.out.println("\t\t- Curves: " + String.join(",", curves)); + } + System.out.println(); } } } diff --git a/src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java b/src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java index aaf76be..c6600f9 100644 --- a/src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java +++ b/src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java @@ -1,7 +1,12 @@ package cz.crcs.ectester.standalone.libs; +import org.bouncycastle.jce.ECNamedCurveTable; import org.bouncycastle.jce.provider.BouncyCastleProvider; +import java.util.Enumeration; +import java.util.Set; +import java.util.TreeSet; + /** * @author Jan Jancar johny@neuromancer.sk */ @@ -11,4 +16,13 @@ public class BouncyCastleLib extends ProviderECLibrary { super(new BouncyCastleProvider()); } + @Override + public Set getCurves() { + Set result = new TreeSet<>(); + Enumeration names = ECNamedCurveTable.getNames(); + while (names.hasMoreElements()) { + result.add((String) names.nextElement()); + } + return result; + } } diff --git a/src/cz/crcs/ectester/standalone/libs/ECLibrary.java b/src/cz/crcs/ectester/standalone/libs/ECLibrary.java index 1e73e61..0f81978 100644 --- a/src/cz/crcs/ectester/standalone/libs/ECLibrary.java +++ b/src/cz/crcs/ectester/standalone/libs/ECLibrary.java @@ -14,6 +14,8 @@ public interface ECLibrary { boolean isInitialized(); + Set getCurves(); + Set getKAs(); Set getSigs(); diff --git a/src/cz/crcs/ectester/standalone/libs/SunECLib.java b/src/cz/crcs/ectester/standalone/libs/SunECLib.java index b64b740..3aec842 100644 --- a/src/cz/crcs/ectester/standalone/libs/SunECLib.java +++ b/src/cz/crcs/ectester/standalone/libs/SunECLib.java @@ -2,6 +2,9 @@ package cz.crcs.ectester.standalone.libs; import sun.security.ec.SunEC; +import java.util.Set; +import java.util.TreeSet; + /** * @author Jan Jancar johny@neuromancer.sk */ @@ -11,4 +14,15 @@ public class SunECLib extends ProviderECLibrary { super(new SunEC()); } + @Override + public Set getCurves() { + String curves = provider.get("AlgorithmParameters.EC SupportedCurves").toString(); + String[] split = curves.split("\\|"); + Set result = new TreeSet<>(); + for (String curve : split) { + String body = curve.split(",")[0].substring(1); + result.add(body); + } + return result; + } } diff --git a/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java b/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java index 49e810c..78db00e 100644 --- a/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java +++ b/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java @@ -1,6 +1,7 @@ package cz.crcs.ectester.standalone.libs; import java.security.Provider; +import java.util.Set; /** * @author Jan Jancar johny@neuromancer.sk @@ -13,4 +14,7 @@ public class TomcryptLib extends NativeECLibrary { @Override native Provider createProvider(); + + @Override + public native Set getCurves(); } diff --git a/src/cz/crcs/ectester/standalone/libs/jni/NativeProvider.java b/src/cz/crcs/ectester/standalone/libs/jni/NativeProvider.java index 53f8b3c..a2233e2 100644 --- a/src/cz/crcs/ectester/standalone/libs/jni/NativeProvider.java +++ b/src/cz/crcs/ectester/standalone/libs/jni/NativeProvider.java @@ -12,11 +12,9 @@ public abstract class NativeProvider extends Provider { public NativeProvider(String name, double version, String info) { super(name, version, info); - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - setup(); - return null; - } + AccessController.doPrivileged((PrivilegedAction) () -> { + setup(); + return null; }); } diff --git a/src/cz/crcs/ectester/standalone/libs/jni/native.h b/src/cz/crcs/ectester/standalone/libs/jni/native.h index e4e5009..e607738 100644 --- a/src/cz/crcs/ectester/standalone/libs/jni/native.h +++ b/src/cz/crcs/ectester/standalone/libs/jni/native.h @@ -15,6 +15,14 @@ extern "C" { JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_createProvider (JNIEnv *, jobject); +/* + * Class: cz_crcs_ectester_standalone_libs_TomcryptLib + * Method: getCurves + * Signature: ()Ljava/util/Set; + */ +JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_getCurves + (JNIEnv *, jobject); + #ifdef __cplusplus } #endif diff --git a/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c b/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c index db1de88..7f8f303 100644 --- a/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c +++ b/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c @@ -7,49 +7,56 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_crea jclass provider_class = (*env)->FindClass(env, "cz/crcs/ectester/standalone/libs/jni/NativeProvider$TomCrypt"); jmethodID init = (*env)->GetMethodID(env, provider_class, "", "(Ljava/lang/String;DLjava/lang/String;)V"); - if (init == NULL) { - return NULL; - } + jstring name = (*env)->NewStringUTF(env, "libtomcrypt " SCRYPT); double version = strtod(SCRYPT, NULL); + return (*env)->NewObject(env, provider_class, init, name, version, name); } JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeProvider_00024TomCrypt_setup(JNIEnv *env, jobject this) { - /* Initialize libtommath as the math lib. */ ltc_mp = ltm_desc; jclass provider_class = (*env)->FindClass(env, "cz/crcs/ectester/standalone/libs/jni/NativeProvider$TomCrypt"); jmethodID put = (*env)->GetMethodID(env, provider_class, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); - if (put == NULL) { - return; - } - const ltc_ecc_set_type * curve = ltc_ecc_sets; - while (curve->name != NULL) { - printf("%s\n", curve->name); - curve++; - } - /* Just test ecc key generation at this time. */ +/* *//* Just test ecc key generation at this time. *//* ecc_key mykey; prng_state prng; int err; - /* register yarrow */ + *//* register yarrow *//* if (register_prng(&yarrow_desc) == -1) { printf("Error registering Yarrow\n"); return; } - /* setup the PRNG */ + *//* setup the PRNG *//* if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) { printf("Error setting up PRNG, %s\n", error_to_string(err)); return; } - /* make a 192-bit ECC key */ + *//* make a 192-bit ECC key *//* if ((err = ecc_make_key(&prng, find_prng("yarrow"), 24, &mykey)) != CRYPT_OK) { printf("Error making key: %s\n", error_to_string(err)); return; } - return; + return;*/ +} + +JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_getCurves(JNIEnv *env, jobject this) { + jclass hash_set_class = (*env)->FindClass(env, "java/util/TreeSet"); + + jmethodID hash_set_ctr = (*env)->GetMethodID(env, hash_set_class, "", "()V"); + jmethodID hash_set_add = (*env)->GetMethodID(env, hash_set_class, "add", "(Ljava/lang/Object;)Z"); + + jobject result = (*env)->NewObject(env, hash_set_class, hash_set_ctr); + const ltc_ecc_set_type * curve = ltc_ecc_sets; + while (curve->size != 0) { + jstring curve_name = (*env)->NewStringUTF(env, curve->name); + (*env)->CallBooleanMethod(env, result, hash_set_add, curve_name); + curve++; + } + + return result; } \ No newline at end of file -- cgit v1.2.3-70-g09d2