• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.bouncycastle.jcajce.provider.asymmetric.util;
2 
3 import java.math.BigInteger;
4 import java.security.spec.ECField;
5 import java.security.spec.ECFieldF2m;
6 import java.security.spec.ECFieldFp;
7 import java.security.spec.ECParameterSpec;
8 import java.security.spec.ECPoint;
9 import java.security.spec.EllipticCurve;
10 
11 import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
12 import org.bouncycastle.jce.spec.ECNamedCurveSpec;
13 import org.bouncycastle.math.ec.ECCurve;
14 
15 public class EC5Util
16 {
convertCurve( ECCurve curve, byte[] seed)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 
convertCurve( EllipticCurve ec)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 
convertSpec( EllipticCurve ellipticCurve, org.bouncycastle.jce.spec.ECParameterSpec spec)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 
convertSpec( ECParameterSpec ecSpec, boolean withCompression)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 
convertPoint( ECParameterSpec ecSpec, ECPoint point, boolean withCompression)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 
convertPoint( ECCurve curve, ECPoint point, boolean withCompression)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