01: package org.bouncycastle.jce.provider;
02:
03: import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
04: import org.bouncycastle.jce.spec.ECNamedCurveSpec;
05: import org.bouncycastle.math.ec.ECCurve;
06:
07: import java.math.BigInteger;
08: import java.security.spec.ECField;
09: import java.security.spec.ECFieldF2m;
10: import java.security.spec.ECFieldFp;
11: import java.security.spec.ECParameterSpec;
12: import java.security.spec.ECPoint;
13: import java.security.spec.EllipticCurve;
14:
15: public class EC5Util {
16: static EllipticCurve convertCurve(ECCurve curve, byte[] seed) {
17: if (curve instanceof ECCurve.Fp) {
18: return new EllipticCurve(new ECFieldFp(((ECCurve.Fp) curve)
19: .getQ()), curve.getA().toBigInteger(), curve.getB()
20: .toBigInteger(), seed);
21: } else {
22: ECCurve.F2m curveF2m = (ECCurve.F2m) curve;
23: int ks[];
24:
25: if (curveF2m.isTrinomial()) {
26: ks = new int[] { curveF2m.getK1() };
27:
28: return new EllipticCurve(new ECFieldF2m(
29: curveF2m.getM(), ks), curve.getA()
30: .toBigInteger(), curve.getB().toBigInteger(),
31: seed);
32: } else {
33: ks = new int[] { curveF2m.getK3(), curveF2m.getK2(),
34: curveF2m.getK1() };
35:
36: return new EllipticCurve(new ECFieldF2m(
37: curveF2m.getM(), ks), curve.getA()
38: .toBigInteger(), curve.getB().toBigInteger(),
39: seed);
40: }
41: }
42: }
43:
44: static ECCurve convertCurve(EllipticCurve ec) {
45: ECField field = ec.getField();
46: BigInteger a = ec.getA();
47: BigInteger b = ec.getB();
48:
49: if (field instanceof ECFieldFp) {
50: return new ECCurve.Fp(((ECFieldFp) field).getP(), a, b);
51: } else {
52: ECFieldF2m fieldF2m = (ECFieldF2m) field;
53: int m = fieldF2m.getM();
54: int ks[] = ECUtil.convertMidTerms(fieldF2m
55: .getMidTermsOfReductionPolynomial());
56: return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b);
57: }
58: }
59:
60: static ECParameterSpec convertSpec(EllipticCurve ellipticCurve,
61: org.bouncycastle.jce.spec.ECParameterSpec spec) {
62: if (spec instanceof ECNamedCurveParameterSpec) {
63: return new ECNamedCurveSpec(
64: ((ECNamedCurveParameterSpec) spec).getName(),
65: ellipticCurve, new ECPoint(spec.getG().getX()
66: .toBigInteger(), spec.getG().getY()
67: .toBigInteger()), spec.getN(), spec.getH());
68: } else {
69: return new ECParameterSpec(ellipticCurve, new ECPoint(spec
70: .getG().getX().toBigInteger(), spec.getG().getY()
71: .toBigInteger()), spec.getN(), spec.getH()
72: .intValue());
73: }
74: }
75:
76: static org.bouncycastle.jce.spec.ECParameterSpec convertSpec(
77: ECParameterSpec ecSpec, boolean withCompression) {
78: ECCurve curve = convertCurve(ecSpec.getCurve());
79:
80: return new org.bouncycastle.jce.spec.ECParameterSpec(curve,
81: convertPoint(curve, ecSpec.getGenerator(),
82: withCompression), ecSpec.getOrder(), BigInteger
83: .valueOf(ecSpec.getCofactor()), ecSpec
84: .getCurve().getSeed());
85: }
86:
87: static org.bouncycastle.math.ec.ECPoint convertPoint(
88: ECParameterSpec ecSpec, ECPoint point,
89: boolean withCompression) {
90: return convertPoint(convertCurve(ecSpec.getCurve()), point,
91: withCompression);
92: }
93:
94: static org.bouncycastle.math.ec.ECPoint convertPoint(ECCurve curve,
95: ECPoint point, boolean withCompression) {
96: return curve.createPoint(point.getAffineX(),
97: point.getAffineY(), withCompression);
98: }
99: }
|