diff options
10 files changed, 377 insertions, 188 deletions
diff --git a/src/cz/crcs/ectester/reader/test/CardTestSuite.java b/src/cz/crcs/ectester/reader/test/CardTestSuite.java index e3cc155..3da5158 100644 --- a/src/cz/crcs/ectester/reader/test/CardTestSuite.java +++ b/src/cz/crcs/ectester/reader/test/CardTestSuite.java @@ -13,7 +13,6 @@ import cz.crcs.ectester.reader.ECTesterReader; import cz.crcs.ectester.reader.command.Command; import java.io.IOException; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; 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); } diff --git a/src/cz/crcs/ectester/standalone/consts/Ident.java b/src/cz/crcs/ectester/standalone/consts/Ident.java index e2556c5..40a44ac 100644 --- a/src/cz/crcs/ectester/standalone/consts/Ident.java +++ b/src/cz/crcs/ectester/standalone/consts/Ident.java @@ -42,6 +42,9 @@ public abstract class Ident { for (String alias : idents) { try { instance = getter.apply(alias, provider); + if (instance != null) { + break; + } } catch (Exception ignored) { } } diff --git a/src/cz/crcs/ectester/standalone/test/KeyAgreementTest.java b/src/cz/crcs/ectester/standalone/test/KeyAgreementTest.java index 9604e75..e273a44 100644 --- a/src/cz/crcs/ectester/standalone/test/KeyAgreementTest.java +++ b/src/cz/crcs/ectester/standalone/test/KeyAgreementTest.java @@ -43,7 +43,7 @@ public class KeyAgreementTest extends SimpleTest<KeyAgreementTestable> { @Override public String getDescription() { - return null; + return "KeyAgreement test"; } @Override diff --git a/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java b/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java index fedf519..8e9b0dd 100644 --- a/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java +++ b/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java @@ -2,12 +2,14 @@ package cz.crcs.ectester.standalone.test; import cz.crcs.ectester.common.test.BaseTestable; import cz.crcs.ectester.common.test.TestException; -import cz.crcs.ectester.common.test.Testable; import javax.crypto.KeyAgreement; +import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.interfaces.ECPrivateKey; import java.security.interfaces.ECPublicKey; +import java.security.spec.AlgorithmParameterSpec; +import java.security.spec.ECParameterSpec; /** * @author Jan Jancar johny@neuromancer.sk @@ -16,6 +18,9 @@ public class KeyAgreementTestable extends BaseTestable { private KeyAgreement ka; private ECPrivateKey privateKey; private ECPublicKey publicKey; + private KeyGeneratorTestable kgtPrivate; + private KeyGeneratorTestable kgtPublic; + private AlgorithmParameterSpec spec; private byte[] secret; public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey) { @@ -24,6 +29,27 @@ public class KeyAgreementTestable extends BaseTestable { this.publicKey = publicKey; } + public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey, ECParameterSpec spec) { + this(ka, privateKey, publicKey); + this.spec = spec; + } + + public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable kgt, ECPrivateKey privateKey, ECParameterSpec spec) { + this(ka, privateKey, null, spec); + this.kgtPublic = kgt; + } + + public KeyAgreementTestable(KeyAgreement ka, ECPublicKey publicKey, KeyGeneratorTestable kgt, ECParameterSpec spec) { + this(ka, null, publicKey, spec); + this.kgtPrivate = kgt; + } + + public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable privKgt, KeyGeneratorTestable pubKgt, ECParameterSpec spec) { + this(ka, (ECPrivateKey) null, null, spec); + this.kgtPrivate = privKgt; + this.kgtPublic = pubKgt; + } + public byte[] getSecret() { if (!hasRun) { return null; @@ -33,20 +59,38 @@ public class KeyAgreementTestable extends BaseTestable { @Override public void run() throws TestException { + if (kgtPrivate != null) { + privateKey = (ECPrivateKey) kgtPrivate.getKeyPair().getPrivate(); + } + + if (kgtPublic != null) { + publicKey = (ECPublicKey) kgtPublic.getKeyPair().getPublic(); + } + try { - ka.init(privateKey); - } catch (InvalidKeyException ikex) { - throw new TestException(ikex); + if (spec != null) { + ka.init(privateKey, spec); + } else { + ka.init(privateKey); + } + } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { + ok = false; + error = true; + hasRun = true; + return; } try { ka.doPhase(publicKey, true); - } catch (InvalidKeyException ikex) { - throw new TestException(ikex); - } catch (IllegalStateException isex) { + } catch (IllegalStateException e) { ok = false; hasRun = true; return; + } catch (InvalidKeyException e) { + ok = false; + error = true; + hasRun = true; + return; } try { @@ -55,7 +99,13 @@ public class KeyAgreementTestable extends BaseTestable { ok = false; hasRun = true; return; + } catch (UnsupportedOperationException uoe) { + ok = false; + error = true; + hasRun = false; + return; } + ok = true; hasRun = true; } diff --git a/src/cz/crcs/ectester/standalone/test/KeyGeneratorTest.java b/src/cz/crcs/ectester/standalone/test/KeyGeneratorTest.java index 9032415..a57e28c 100644 --- a/src/cz/crcs/ectester/standalone/test/KeyGeneratorTest.java +++ b/src/cz/crcs/ectester/standalone/test/KeyGeneratorTest.java @@ -28,7 +28,7 @@ public class KeyGeneratorTest extends SimpleTest<KeyGeneratorTestable> { @Override public String getDescription() { - return null; + return "KeyPairGenerator test"; } @Override diff --git a/src/cz/crcs/ectester/standalone/test/SignatureTest.java b/src/cz/crcs/ectester/standalone/test/SignatureTest.java index 272a3f7..97e387c 100644 --- a/src/cz/crcs/ectester/standalone/test/SignatureTest.java +++ b/src/cz/crcs/ectester/standalone/test/SignatureTest.java @@ -1,5 +1,6 @@ package cz.crcs.ectester.standalone.test; +import cz.crcs.ectester.common.test.Result; import cz.crcs.ectester.common.test.SimpleTest; import cz.crcs.ectester.common.test.TestCallback; import cz.crcs.ectester.common.test.TestException; @@ -12,9 +13,22 @@ public class SignatureTest extends SimpleTest<SignatureTestable> { super(sig, callback); } + public static SignatureTest expect(SignatureTestable kg, Result.ExpectedValue expected) { + return new SignatureTest(kg, new TestCallback<SignatureTestable>() { + @Override + public Result apply(SignatureTestable signatureTestable) { + return new Result(Result.Value.fromExpected(expected, signatureTestable.ok(), signatureTestable.error())); + } + }); + } + + public static SignatureTest function(SignatureTestable ka, TestCallback<SignatureTestable> callback) { + return new SignatureTest(ka, callback); + } + @Override public String getDescription() { - return null; + return "Signature test"; } @Override diff --git a/src/cz/crcs/ectester/standalone/test/SignatureTestable.java b/src/cz/crcs/ectester/standalone/test/SignatureTestable.java index 8b6815b..564a6bf 100644 --- a/src/cz/crcs/ectester/standalone/test/SignatureTestable.java +++ b/src/cz/crcs/ectester/standalone/test/SignatureTestable.java @@ -2,9 +2,9 @@ package cz.crcs.ectester.standalone.test; import cz.crcs.ectester.common.test.BaseTestable; import cz.crcs.ectester.common.test.TestException; -import cz.crcs.ectester.common.test.Testable; import java.security.InvalidKeyException; +import java.security.SecureRandom; import java.security.Signature; import java.security.SignatureException; import java.security.interfaces.ECPrivateKey; @@ -14,6 +14,7 @@ public class SignatureTestable extends BaseTestable { private Signature sig; private ECPrivateKey signKey; private ECPublicKey verifyKey; + private KeyGeneratorTestable kgt; private byte[] data; private byte[] signature; private boolean verified; @@ -23,6 +24,20 @@ public class SignatureTestable extends BaseTestable { this.signKey = signKey; this.verifyKey = verifyKey; this.data = data; + if (data == null) { + SecureRandom random = new SecureRandom(); + this.data = new byte[32]; + random.nextBytes(this.data); + } + } + + public SignatureTestable(Signature sig, KeyGeneratorTestable kgt, byte[] data) { + this(sig, null, null, data); + this.kgt = kgt; + } + + public byte[] getData() { + return data; } public byte[] getSignature() { @@ -35,6 +50,11 @@ public class SignatureTestable extends BaseTestable { @Override public void run() throws TestException { + if (kgt != null) { + signKey = (ECPrivateKey) kgt.getKeyPair().getPrivate(); + verifyKey = (ECPublicKey) kgt.getKeyPair().getPublic(); + } + try { sig.initSign(signKey); } catch (InvalidKeyException e) { diff --git a/src/cz/crcs/ectester/standalone/test/StandaloneDefaultSuite.java b/src/cz/crcs/ectester/standalone/test/StandaloneDefaultSuite.java new file mode 100644 index 0000000..7056d69 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/test/StandaloneDefaultSuite.java @@ -0,0 +1,76 @@ +package cz.crcs.ectester.standalone.test; + +import cz.crcs.ectester.common.cli.TreeCommandLine; +import cz.crcs.ectester.common.ec.EC_Curve; +import cz.crcs.ectester.common.test.Result; +import cz.crcs.ectester.data.EC_Store; +import cz.crcs.ectester.standalone.ECTesterStandalone; +import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; +import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; +import cz.crcs.ectester.standalone.consts.SignatureIdent; + +import javax.crypto.KeyAgreement; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.Signature; +import java.security.spec.ECParameterSpec; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class StandaloneDefaultSuite extends StandaloneTestSuite { + + public StandaloneDefaultSuite(EC_Store dataStore, ECTesterStandalone.Config cfg, TreeCommandLine cli) { + super(dataStore, cfg, cli, "default", "The default test suite tests basic support of ECDH and ECDSA."); + } + + @Override + public void setup() throws NoSuchAlgorithmException { + String kpgAlgo = cli.getOptionValue("test.kpg-type", "EC"); + String kaAlgo = cli.getOptionValue("test.ka-type"); + String sigAlgo = cli.getOptionValue("test.sig-type"); + + KeyPairGeneratorIdent kpgIdent = cfg.selected.getKPGs().stream() + .filter((ident) -> ident.contains(kpgAlgo)) + .findFirst().get(); + KeyPairGenerator kpg = kpgIdent.getInstance(cfg.selected.getProvider()); + + KeyGeneratorTestable kgtOne; + KeyGeneratorTestable kgtOther; + ECParameterSpec spec = null; + if (cli.hasOption("test.bits")) { + int bits = Integer.parseInt(cli.getOptionValue("test.bits")); + kgtOne = new KeyGeneratorTestable(kpg, bits); + kgtOther = new KeyGeneratorTestable(kpg, bits); + } else if (cli.hasOption("test.named-curve")) { + String curveName = cli.getOptionValue("test.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(); + kgtOne = new KeyGeneratorTestable(kpg, spec); + kgtOther = new KeyGeneratorTestable(kpg, spec); + } else { + kgtOne = new KeyGeneratorTestable(kpg); + kgtOther = new KeyGeneratorTestable(kpg); + } + + tests.add(KeyGeneratorTest.expect(kgtOne, Result.ExpectedValue.SUCCESS)); + tests.add(KeyGeneratorTest.expect(kgtOther, Result.ExpectedValue.SUCCESS)); + + for (KeyAgreementIdent kaIdent : cfg.selected.getKAs()) { + if (kaAlgo == null || kaIdent.contains(kaAlgo)) { + KeyAgreement ka = kaIdent.getInstance(cfg.selected.getProvider()); + tests.add(KeyAgreementTest.expect(new KeyAgreementTestable(ka, kgtOne, kgtOther, spec), Result.ExpectedValue.SUCCESS)); + } + } + for (SignatureIdent sigIdent : cfg.selected.getSigs()) { + if (sigAlgo == null || sigIdent.contains(sigAlgo)) { + Signature sig = sigIdent.getInstance(cfg.selected.getProvider()); + tests.add(SignatureTest.expect(new SignatureTestable(sig, kgtOne, null), Result.ExpectedValue.SUCCESS)); + } + } + } +} diff --git a/src/cz/crcs/ectester/standalone/test/StandaloneTestSuite.java b/src/cz/crcs/ectester/standalone/test/StandaloneTestSuite.java new file mode 100644 index 0000000..5682cd5 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/test/StandaloneTestSuite.java @@ -0,0 +1,24 @@ +package cz.crcs.ectester.standalone.test; + +import cz.crcs.ectester.common.cli.TreeCommandLine; +import cz.crcs.ectester.common.test.TestSuite; +import cz.crcs.ectester.data.EC_Store; +import cz.crcs.ectester.standalone.ECTesterStandalone; + +import java.security.NoSuchAlgorithmException; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public abstract class StandaloneTestSuite extends TestSuite { + TreeCommandLine cli; + ECTesterStandalone.Config cfg; + + public StandaloneTestSuite(EC_Store dataStore, ECTesterStandalone.Config cfg, TreeCommandLine cli, String name, String description) { + super(dataStore, name, description); + this.cfg = cfg; + this.cli = cli; + } + + public abstract void setup() throws NoSuchAlgorithmException; +} |
