package cz.crcs.ectester.reader.test;

import cz.crcs.ectester.applet.ECTesterApplet;
import cz.crcs.ectester.applet.EC_Consts;
import cz.crcs.ectester.common.ec.EC_Curve;
import cz.crcs.ectester.common.ec.EC_Key;
import cz.crcs.ectester.common.output.TestWriter;
import cz.crcs.ectester.common.test.CompoundTest;
import cz.crcs.ectester.common.test.Test;
import cz.crcs.ectester.data.EC_Store;
import cz.crcs.ectester.reader.CardMngr;
import cz.crcs.ectester.reader.ECTesterReader;
import cz.crcs.ectester.reader.command.Command;

import java.util.Map;

import static cz.crcs.ectester.common.test.Result.ExpectedValue;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class CardCompositeCurvesSuite extends CardTestSuite {

    public CardCompositeCurvesSuite(TestWriter writer, ECTesterReader.Config cfg, CardMngr cardManager) {
        super(writer, cfg, cardManager, "composite", "The composite suite runs ECDH over curves with composite order. This should generally fail, as using such a curve is unsafe.");
    }

    @Override
    protected void runTests() throws Exception {
        /* Do the default run with the public keys set to provided smallorder keys
         * over composite order curves. Essentially small subgroup attacks.
         * These should fail, the curves aren't safe so that if the computation with
         * a small order public key succeeds the private key modulo the public key order
         * is revealed.
         */
        Map<String, EC_Key> keys = EC_Store.getInstance().getObjects(EC_Key.class, "composite");
        for (EC_Key key : keys.values()) {
            EC_Curve curve = EC_Store.getInstance().getObject(EC_Curve.class, key.getCurve());
            Test allocate = CommandTest.expect(new Command.Allocate(this.card, ECTesterApplet.KEYPAIR_BOTH, curve.getBits(), curve.getField()), ExpectedValue.SUCCESS);
            Test set = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_BOTH, EC_Consts.CURVE_external, curve.getParams(), curve.flatten()), ExpectedValue.ANY);
            Test generate = CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_LOCAL), ExpectedValue.ANY);
            Command ecdhCommand = new Command.ECDH_direct(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_NONE, EC_Consts.KeyAgreement_ALG_EC_SVDP_DH, key.flatten());
            Test ecdh = CommandTest.expect(ecdhCommand, ExpectedValue.FAILURE, "Card correctly rejected to do ECDH over a composite order curve.", "Card incorrectly does ECDH over a composite order curve, leaks bits of private key.");

            doTest(CompoundTest.greedyAllTry(ExpectedValue.SUCCESS, "Composite test of " + curve.getId() + ", " + key.getDesc(), allocate, set, generate, ecdh));
            new Command.Cleanup(this.card).send();
        }
    }
}

@@ -165,16 +165,63 @@
 _Curve;%0A
+import cz.crcs.ectester.common.ec.EC_KAResult;%0A
 import c
@@ -254,16 +254,16 @@
 EC_Key;%0A
-
 import c
@@ -572,16 +572,39 @@
 mmand;%0A%0A
+import java.util.List;%0A
 import j
@@ -1862,20 +1862,21 @@
 KEYPAIR_
-BOTH
+LOCAL
 , curve.
@@ -2025,12 +2025,13 @@
 AIR_
-BOTH
+LOCAL
 , EC
@@ -2691,16 +2691,16 @@
 ey.%22);%0A%0A
-
         
@@ -2913,16 +2913,1549 @@
        %7D
+%0A%0A        /* Also test having a G of small order, so small R.%0A         */%0A        Map%3CString, EC_Curve%3E results = EC_Store.getInstance().getObjects(EC_Curve.class, %22composite%22);%0A        List%3CMap.Entry%3CString, List%3CEC_Curve%3E%3E%3E groupList = EC_Store.mapToPrefix(results.values());%0A        List%3CEC_Curve%3E smallRCurves = groupList.stream().filter((e) -%3E e.getKey().equals(%22small%22)).findFirst().get().getValue();%0A        for (EC_Curve curve : smallRCurves) %7B%0A            Test allocate = CommandTest.expect(new Command.Allocate(this.card, ECTesterApplet.KEYPAIR_BOTH, curve.getBits(), curve.getField()), ExpectedValue.SUCCESS);%0A            Test set = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_BOTH, EC_Consts.CURVE_external, curve.getParams(), curve.flatten()), ExpectedValue.ANY);%0A            Test generate = CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_BOTH), ExpectedValue.ANY);%0A            Test ecdh = CommandTest.expect(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_NONE, EC_Consts.KeyAgreement_ALG_EC_SVDP_DH), ExpectedValue.FAILURE, %22Card correctly rejected to do ECDH over a small order generator.%22, %22Card incorrectly does ECDH over a small order generator.%22);%0A%0A            doTest(CompoundTest.greedyAllTry(ExpectedValue.SUCCESS, %22Small generator order test of %22 + curve.getId() + %22.%22, allocate, set, generate, ecdh));%0A            new Command.Cleanup(this.card).send();%0A        %7D
 %0A    %7D%0A%7D

Add tests for small R to composite test suite.