EC5Util.java revision 5db505e1f6a68c8d5dfdb0fed0b8607dea7bed96
1package org.bouncycastle.jcajce.provider.asymmetric.util;
2
3import java.math.BigInteger;
4import java.security.spec.ECField;
5import java.security.spec.ECFieldF2m;
6import java.security.spec.ECFieldFp;
7import java.security.spec.ECParameterSpec;
8import java.security.spec.ECPoint;
9import java.security.spec.EllipticCurve;
10
11import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
12import org.bouncycastle.jce.spec.ECNamedCurveSpec;
13import org.bouncycastle.math.ec.ECCurve;
14
15public class EC5Util
16{
17    public static EllipticCurve convertCurve(
18        ECCurve curve,
19        byte[]  seed)
20    {
21        // TODO: the Sun EC implementation doesn't currently handle the seed properly
22        // so at the moment it's set to null. Should probably look at making this configurable
23        if (curve instanceof ECCurve.Fp)
24        {
25            return new EllipticCurve(new ECFieldFp(((ECCurve.Fp)curve).getQ()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
26        }
27        else
28        {
29            ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
30            int ks[];
31
32            if (curveF2m.isTrinomial())
33            {
34                ks = new int[] { curveF2m.getK1() };
35
36                return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
37            }
38            else
39            {
40                ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
41
42                return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
43            }
44        }
45    }
46
47    public static ECCurve convertCurve(
48        EllipticCurve ec)
49    {
50        ECField field = ec.getField();
51        BigInteger a = ec.getA();
52        BigInteger b = ec.getB();
53
54        if (field instanceof ECFieldFp)
55        {
56            return new ECCurve.Fp(((ECFieldFp)field).getP(), a, b);
57        }
58        else
59        {
60            ECFieldF2m fieldF2m = (ECFieldF2m)field;
61            int m = fieldF2m.getM();
62            int ks[] = ECUtil.convertMidTerms(fieldF2m.getMidTermsOfReductionPolynomial());
63            return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b);
64        }
65    }
66
67    public static ECParameterSpec convertSpec(
68        EllipticCurve ellipticCurve,
69        org.bouncycastle.jce.spec.ECParameterSpec spec)
70    {
71        if (spec instanceof ECNamedCurveParameterSpec)
72        {
73            return new ECNamedCurveSpec(
74                ((ECNamedCurveParameterSpec)spec).getName(),
75                ellipticCurve,
76                new ECPoint(
77                    spec.getG().getAffineXCoord().toBigInteger(),
78                    spec.getG().getAffineYCoord().toBigInteger()),
79                spec.getN(),
80                spec.getH());
81        }
82        else
83        {
84            return new ECParameterSpec(
85                ellipticCurve,
86                new ECPoint(
87                    spec.getG().getAffineXCoord().toBigInteger(),
88                    spec.getG().getAffineYCoord().toBigInteger()),
89                spec.getN(),
90                spec.getH().intValue());
91        }
92    }
93
94    public static org.bouncycastle.jce.spec.ECParameterSpec convertSpec(
95        ECParameterSpec ecSpec,
96        boolean withCompression)
97    {
98        ECCurve curve = convertCurve(ecSpec.getCurve());
99
100        return new org.bouncycastle.jce.spec.ECParameterSpec(
101            curve,
102            convertPoint(curve, ecSpec.getGenerator(), withCompression),
103            ecSpec.getOrder(),
104            BigInteger.valueOf(ecSpec.getCofactor()),
105            ecSpec.getCurve().getSeed());
106    }
107
108    public static org.bouncycastle.math.ec.ECPoint convertPoint(
109        ECParameterSpec ecSpec,
110        ECPoint point,
111        boolean withCompression)
112    {
113        return convertPoint(convertCurve(ecSpec.getCurve()), point, withCompression);
114    }
115
116    public static org.bouncycastle.math.ec.ECPoint convertPoint(
117        ECCurve curve,
118        ECPoint point,
119        boolean withCompression)
120    {
121        return curve.createPoint(point.getAffineX(), point.getAffineY(), withCompression);
122    }
123}
124