summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/ec/EC_Curve.java
blob: 173fd2938cf83799fad32bb681bb72a3809b0685 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cz.crcs.ectester.common.ec;

import cz.crcs.ectester.applet.EC_Consts;
import cz.crcs.ectester.common.util.ByteUtil;
import javacard.security.KeyPair;

import java.math.BigInteger;
import java.security.spec.*;

/**
 * An Elliptic curve, contains parameters Fp/F2M, A, B, G, R, (K)?.
 *
 * @author Jan Jancar johny@neuromancer.sk
 */
public class EC_Curve extends EC_Params {
    private short bits;
    private byte field;
    private String desc;

    /**
     * @param bits
     * @param field KeyPair.ALG_EC_FP or KeyPair.ALG_EC_F2M
     */
    public EC_Curve(short bits, byte field) {
        super(field == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M);
        this.bits = bits;
        this.field = field;
    }

    public EC_Curve(String id, short bits, byte field) {
        this(bits, field);
        this.id = id;
    }

    public EC_Curve(String id, short bits, byte field, String desc) {
        this(id, bits, field);
        this.desc = desc;
    }

    public short getBits() {
        return bits;
    }

    public byte getField() {
        return field;
    }

    public String getDesc() {
        return desc;
    }

    @Override
    public String toString() {
        return "<" + getId() + "> " + (field == KeyPair.ALG_EC_FP ? "Prime" : "Binary") + " field Elliptic curve (" + String.valueOf(bits) + "b)" + (desc == null ? "" : ": " + desc);
    }

    public ECParameterSpec toSpec() {
        ECField field;
        if (this.field == KeyPair.ALG_EC_FP) {
            field = new ECFieldFp(new BigInteger(1, getData(0)));
        } else {
            byte[][] fieldData = getParam(EC_Consts.PARAMETER_F2M);
            int m = ByteUtil.getShort(fieldData[0], 0);
            int e1 = ByteUtil.getShort(fieldData[1], 0);
            int e2 = ByteUtil.getShort(fieldData[2], 0);
            int e3 = ByteUtil.getShort(fieldData[3], 0);
            int[] powers = new int[]{e1, e2, e3};
            field = new ECFieldF2m(m, powers);
        }

        BigInteger a = new BigInteger(1, getParam(EC_Consts.PARAMETER_A)[0]);
        BigInteger b = new BigInteger(1, getParam(EC_Consts.PARAMETER_B)[0]);

        EllipticCurve curve = new EllipticCurve(field, a, b);

        byte[][] G = getParam(EC_Consts.PARAMETER_G);
        BigInteger gx = new BigInteger(1, G[0]);
        BigInteger gy = new BigInteger(1, G[1]);
        ECPoint generator = new ECPoint(gx, gy);

        BigInteger n = new BigInteger(1, getParam(EC_Consts.PARAMETER_R)[0]);

        int h = ByteUtil.getShort(getParam(EC_Consts.PARAMETER_K)[0], 0);

        return new ECParameterSpec(curve, generator, n, h);
    }

    public static EC_Curve fromSpec(ECParameterSpec spec) {
        EllipticCurve curve = spec.getCurve();
        ECField field = curve.getField();

        short bits = (short) field.getFieldSize();
        byte[][] params;
        int paramIndex = 0;
        byte fieldType;
        if (field instanceof ECFieldFp) {
            ECFieldFp primeField = (ECFieldFp) field;
            params = new byte[7][];
            params[paramIndex++] = primeField.getP().toByteArray();
            fieldType = KeyPair.ALG_EC_FP;
        } else if (field instanceof ECFieldF2m) {
            ECFieldF2m binaryField = (ECFieldF2m) field;
            params = new byte[10][];
            params[paramIndex] = new byte[2];
            ByteUtil.setShort(params[paramIndex++], 0, (short) binaryField.getM());
            int[] powers = binaryField.getMidTermsOfReductionPolynomial();
            for (int i = 0; i < 3; ++i) {
                params[paramIndex] = new byte[2];
                ByteUtil.setShort(params[paramIndex++], 0, (short) powers[i]);
            }
            fieldType = KeyPair.ALG_EC_F2M;
        } else {
            throw new IllegalArgumentException("ECParameterSpec with an unknown field.");
        }

        ECPoint generator = spec.getGenerator();

        params[paramIndex++] = curve.getA().toByteArray();
        params[paramIndex++] = curve.getB().toByteArray();

        params[paramIndex++] = generator.getAffineX().toByteArray();
        params[paramIndex++] = generator.getAffineY().toByteArray();

        params[paramIndex++] = spec.getOrder().toByteArray();
        params[paramIndex] = new byte[2];
        ByteUtil.setShort(params[paramIndex], 0, (short) spec.getCofactor());

        EC_Curve result = new EC_Curve(bits, fieldType);
        result.readByteArray(params);
        return result;
    }
}