aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/test/CardTestSuite.java
blob: fc60ae55a2d8a8319914216d88f0a2e1d48ae8f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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_Params;
import cz.crcs.ectester.common.output.TestWriter;
import cz.crcs.ectester.common.test.CompoundTest;
import cz.crcs.ectester.common.test.Result;
import cz.crcs.ectester.common.test.Test;
import cz.crcs.ectester.common.test.TestSuite;
import cz.crcs.ectester.common.util.ECUtil;
import cz.crcs.ectester.reader.CardMngr;
import cz.crcs.ectester.reader.ECTesterReader;
import cz.crcs.ectester.reader.command.Command;

import java.util.Arrays;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public abstract class CardTestSuite extends TestSuite {
    ECTesterReader.Config cfg;
    CardMngr card;
    String[] options;

    CardTestSuite(TestWriter writer, ECTesterReader.Config cfg, CardMngr cardManager, String name, String[] options, String... description) {
        super(writer, name, description);
        this.card = cardManager;
        this.cfg = cfg;
        this.options = options;
    }

    public CardMngr getCard() {
        return card;
    }

    public ECTesterReader.Config getCfg() {
        return cfg;
    }

    public String[] getOptions() {
        if (options != null) {
            return options.clone();
        } else {
            return options;
        }
    }

    public Test setupKeypairs(EC_Curve curve, Result.ExpectedValue expected, byte keyPair) {
        if ((Arrays.asList(options).contains("preset") && cfg.testOptions.contains("preset")) || (Arrays.asList(options).contains("random") && cfg.testOptions.contains("random"))) {
            Test setLocal = null;
            if ((keyPair & ECTesterApplet.KEYPAIR_LOCAL) != 0) {
                EC_Params priv;
                if (cfg.testOptions.contains("preset")) {
                    priv = ECUtil.fixedRandomKey(curve);
                } else {
                    priv = ECUtil.fullRandomKey(curve);
                }
                setLocal = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_LOCAL, EC_Consts.CURVE_external, priv.getParams(), priv.flatten()), expected);
            }
            Test setRemote = null;
            if ((keyPair & ECTesterApplet.KEYPAIR_REMOTE) != 0) {
                EC_Params pub;
                if (cfg.testOptions.contains("preset")) {
                    pub = ECUtil.fixedRandomPoint(curve);
                } else {
                    pub = ECUtil.fullRandomPoint(curve);
                }
                if (pub == null) {
                    setRemote = CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_REMOTE), expected);
                } else {
                    setRemote = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_REMOTE, EC_Consts.CURVE_external, pub.getParams(), pub.flatten()), expected);
                }
            }

            if (keyPair == ECTesterApplet.KEYPAIR_LOCAL) {
                return setLocal;
            } else if (keyPair == ECTesterApplet.KEYPAIR_REMOTE) {
                return setRemote;
            } else {
                String desc;
                if (cfg.testOptions.contains("preset")) {
                    desc = "Set semi-random parameters.";
                } else {
                    desc = "Set fully-random parameters.";
                }
                return CompoundTest.all(expected, desc, setLocal, setRemote);
            }
        } else {
            return CommandTest.expect(new Command.Generate(this.card, keyPair), expected);
        }
    }
}