From 6f397b26eef8906c46878cd207492da59adff29b Mon Sep 17 00:00:00 2001 From: davidhofman Date: Mon, 16 Aug 2021 18:30:44 +0200 Subject: implement StandaloneTestVectorSuite (#6) * Add StandaloneTestVectorSuite class (not implemented) * Add option to choose test suites in standalone app, StandaloneTestVectorSuite now shows up in list-libs * partially implement StandaloneTestVectorSuite * partially implement StandaloneTestVectorSuite 2 * remove unused variable * modify xml data to support standalone test vectors * StandaloneTestVectorSuite is now working * CardTestVectorSuite now skips results with DH_RAW keyAlgo * some additional debug information are now printed * change DH_RAW to DH_PLAIN * remove debug printing, clean up * remove unnecessary if check in CardTestVectorSuite * fix test suite command line option Co-authored-by: davidhofman --- src/cz/crcs/ectester/data/test/results.xml | 89 ++++++++++++++++++++++ .../ectester/standalone/ECTesterStandalone.java | 17 ++++- .../test/suites/StandaloneTestVectorSuite.java | 59 ++++++++++++++ 3 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 src/cz/crcs/ectester/standalone/test/suites/StandaloneTestVectorSuite.java (limited to 'src') diff --git a/src/cz/crcs/ectester/data/test/results.xml b/src/cz/crcs/ectester/data/test/results.xml index 64fa86a..fa43e4b 100644 --- a/src/cz/crcs/ectester/data/test/results.xml +++ b/src/cz/crcs/ectester/data/test/results.xml @@ -171,4 +171,93 @@ test/p521-A test/p521-B + + secp160r1-dh-plain + DH_PLAIN + secg/secp160r1-dh-raw.csv + secg/secp160r1 + test/secp160r1-U + test/secp160r1-V + + + sect163k1-dh-plain + DH_PLAIN + secg/sect163k1-dh-raw.csv + secg/sect163k1 + test/sect163k1-U + test/sect163k1-V + + + + brainpoolP224r1-dh-plain + DH_PLAIN + brainpool/brainpoolP224r1-dh-raw.csv + brainpool/brainpoolP224r1 + test/brainpoolP224r1-A + test/brainpoolP224r1-B + + + brainpoolP256r1-dh-plain + DH_PLAIN + brainpool/brainpoolP256r1-dh-raw.csv + brainpool/brainpoolP256r1 + test/brainpoolP256r1-A + test/brainpoolP256r1-B + + + brainpoolP384r1-dh-plain + DH_PLAIN + brainpool/brainpoolP384r1-dh-raw.csv + brainpool/brainpoolP384r1 + test/brainpoolP384r1-A + test/brainpoolP384r1-B + + + brainpoolP512r1-dh-plain + DH_PLAIN + brainpool/brainpoolP512r1-dh-raw.csv + brainpool/brainpoolP512r1 + test/brainpoolP512r1-A + test/brainpoolP512r1-B + + + p192-dhc-plain + DH_PLAIN + nist/p192-dhc-raw.csv + nist/P-192 + test/p192-A + test/p192-B + + + p224-dhc-plain + DH_PLAIN + nist/p224-dhc-raw.csv + nist/P-224 + test/p224-A + test/p224-B + + + p256-dhc-plain + DH_PLAIN + nist/p256-dhc-raw.csv + nist/P-256 + test/p256-A + test/p256-B + + + p384-dhc-plain + DH_PLAIN + nist/p384-dhc-raw.csv + nist/P-384 + test/p384-A + test/p384-B + + + p521-dhc-plain + DH_PLAIN + nist/p521-dhc-raw.csv + nist/P-521 + test/p521-A + test/p521-B + \ No newline at end of file diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java index 65997c1..f5686b2 100644 --- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java +++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java @@ -41,6 +41,7 @@ import cz.crcs.ectester.standalone.output.XMLTestWriter; import cz.crcs.ectester.standalone.output.YAMLTestWriter; import cz.crcs.ectester.standalone.test.suites.StandaloneDefaultSuite; import cz.crcs.ectester.standalone.test.suites.StandaloneTestSuite; +import cz.crcs.ectester.standalone.test.suites.StandaloneTestVectorSuite; import org.apache.commons.cli.*; import javax.crypto.KeyAgreement; @@ -311,7 +312,9 @@ public class ECTesterStandalone { * */ private void listSuites() { - StandaloneTestSuite[] suites = new StandaloneTestSuite[]{new StandaloneDefaultSuite(null, null, null)}; + StandaloneTestSuite[] suites = new StandaloneTestSuite[]{ + new StandaloneDefaultSuite(null, null, null), + new StandaloneTestVectorSuite(null, null, null)}; for (StandaloneTestSuite suite : suites) { System.out.println(" - " + suite.getName()); for (String line : suite.getDescription()) { @@ -739,7 +742,17 @@ public class ECTesterStandalone { break; } - StandaloneTestSuite suite = new StandaloneDefaultSuite(writer, cfg, cli); + StandaloneTestSuite suite; + + switch(cli.getArg(0).toLowerCase()) { + case "test-vectors": + suite = new StandaloneTestVectorSuite(writer, cfg, cli); + break; + case "default": + default: + suite = new StandaloneDefaultSuite(writer, cfg, cli); + } + suite.run(); } diff --git a/src/cz/crcs/ectester/standalone/test/suites/StandaloneTestVectorSuite.java b/src/cz/crcs/ectester/standalone/test/suites/StandaloneTestVectorSuite.java new file mode 100644 index 0000000..c4a866b --- /dev/null +++ b/src/cz/crcs/ectester/standalone/test/suites/StandaloneTestVectorSuite.java @@ -0,0 +1,59 @@ +package cz.crcs.ectester.standalone.test.suites; + +import cz.crcs.ectester.common.cli.TreeCommandLine; +import cz.crcs.ectester.common.ec.*; +import cz.crcs.ectester.common.output.TestWriter; +import cz.crcs.ectester.common.util.ECUtil; +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.test.base.KeyAgreementTest; +import cz.crcs.ectester.standalone.test.base.KeyAgreementTestable; + +import javax.crypto.KeyAgreement; +import java.io.IOException; +import java.security.interfaces.ECPrivateKey; +import java.security.interfaces.ECPublicKey; +import java.util.Map; + +public class StandaloneTestVectorSuite extends StandaloneTestSuite { + + public StandaloneTestVectorSuite(TestWriter writer, ECTesterStandalone.Config cfg, TreeCommandLine cli) { + super(writer, cfg, cli, "test-vectors", "The test-vectors suite contains a collection of test vectors which test basic ECDH correctness."); + } + + @Override + protected void runTests() throws Exception { + Map results = EC_Store.getInstance().getObjects(EC_KAResult.class, "test"); + for (EC_KAResult result : results.values()) { + + if(!"DH_PLAIN".equals(result.getKA())) { + continue; + } + + EC_Params onekey = EC_Store.getInstance().getObject(EC_Keypair.class, result.getOneKey()); + if (onekey == null) { + onekey = EC_Store.getInstance().getObject(EC_Key.Private.class, result.getOneKey()); + } + EC_Params otherkey = EC_Store.getInstance().getObject(EC_Keypair.class, result.getOtherKey()); + if (otherkey == null) { + otherkey = EC_Store.getInstance().getObject(EC_Key.Public.class, result.getOtherKey()); + } + if (onekey == null || otherkey == null) { + throw new IOException("Test vector keys couldn't be located."); + } + + ECPrivateKey privkey = onekey instanceof EC_Keypair ? + (ECPrivateKey) ECUtil.toKeyPair((EC_Keypair) onekey).getPrivate() : + ECUtil.toPrivateKey((EC_Key.Private) onekey); + ECPublicKey pubkey = otherkey instanceof EC_Keypair ? + (ECPublicKey) ECUtil.toKeyPair((EC_Keypair) otherkey).getPublic() : + ECUtil.toPublicKey((EC_Key.Public) otherkey); + + KeyAgreementIdent kaIdent = KeyAgreementIdent.get("ECDH"); + KeyAgreement ka = kaIdent.getInstance(cfg.selected.getProvider()); + KeyAgreementTestable testable = new KeyAgreementTestable(ka, privkey, pubkey); + doTest(KeyAgreementTest.match(testable, result.getData(0))); + } + } +} -- cgit v1.2.3-70-g09d2