ECNamedCurveSpec.java revision 5db505e1f6a68c8d5dfdb0fed0b8607dea7bed96
1package org.bouncycastle.jce.spec;
2
3import java.math.BigInteger;
4import java.security.spec.ECFieldF2m;
5import java.security.spec.ECFieldFp;
6import java.security.spec.ECPoint;
7import java.security.spec.EllipticCurve;
8
9import org.bouncycastle.math.ec.ECCurve;
10
11/**
12 * specification signifying that the curve parameters can also be
13 * referred to by name.
14 */
15public class ECNamedCurveSpec
16    extends java.security.spec.ECParameterSpec
17{
18    private String  name;
19
20    private static EllipticCurve convertCurve(
21        ECCurve  curve,
22        byte[]   seed)
23    {
24        if (curve instanceof ECCurve.Fp)
25        {
26            return new EllipticCurve(new ECFieldFp(((ECCurve.Fp)curve).getQ()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
27        }
28        else
29        {
30            ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
31            int ks[];
32
33            if (curveF2m.isTrinomial())
34            {
35                ks = new int[] { curveF2m.getK1() };
36
37                return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
38            }
39            else
40            {
41                ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
42
43                return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
44            }
45        }
46
47    }
48
49    private static ECPoint convertPoint(
50        org.bouncycastle.math.ec.ECPoint  g)
51    {
52        g = g.normalize();
53        return new ECPoint(g.getAffineXCoord().toBigInteger(), g.getAffineYCoord().toBigInteger());
54    }
55
56    public ECNamedCurveSpec(
57        String                              name,
58        ECCurve                             curve,
59        org.bouncycastle.math.ec.ECPoint    g,
60        BigInteger                          n)
61    {
62        super(convertCurve(curve, null), convertPoint(g), n, 1);
63
64        this.name = name;
65    }
66
67    public ECNamedCurveSpec(
68        String          name,
69        EllipticCurve   curve,
70        ECPoint         g,
71        BigInteger      n)
72    {
73        super(curve, g, n, 1);
74
75        this.name = name;
76    }
77
78    public ECNamedCurveSpec(
79        String                              name,
80        ECCurve                             curve,
81        org.bouncycastle.math.ec.ECPoint    g,
82        BigInteger                          n,
83        BigInteger                          h)
84    {
85        super(convertCurve(curve, null), convertPoint(g), n, h.intValue());
86
87        this.name = name;
88    }
89
90    public ECNamedCurveSpec(
91        String          name,
92        EllipticCurve   curve,
93        ECPoint         g,
94        BigInteger      n,
95        BigInteger      h)
96    {
97        super(curve, g, n, h.intValue());
98
99        this.name = name;
100    }
101
102    public ECNamedCurveSpec(
103        String                              name,
104        ECCurve                             curve,
105        org.bouncycastle.math.ec.ECPoint    g,
106        BigInteger                          n,
107        BigInteger                          h,
108        byte[]                              seed)
109    {
110        super(convertCurve(curve, seed), convertPoint(g), n, h.intValue());
111
112        this.name = name;
113    }
114
115    /**
116     * return the name of the curve the EC domain parameters belong to.
117     */
118    public String getName()
119    {
120        return name;
121    }
122}
123