01: package org.bouncycastle.jce;
02:
03: import java.security.spec.ECFieldF2m;
04: import java.security.spec.ECFieldFp;
05: import java.security.spec.ECPoint;
06: import java.security.spec.EllipticCurve;
07:
08: import org.bouncycastle.math.ec.ECCurve;
09:
10: /**
11: * Utility class for handling EC point decoding.
12: */
13: public class ECPointUtil {
14: /**
15: * Decode a point on this curve which has been encoded using point
16: * compression (X9.62 s 4.2.1 and 4.2.2) or regular encoding.
17: *
18: * @param curve
19: * The elliptic curve.
20: * @param encoded
21: * The encoded point.
22: * @return the decoded point.
23: */
24: public static ECPoint decodePoint(EllipticCurve curve,
25: byte[] encoded) {
26: ECCurve c = null;
27:
28: if (curve.getField() instanceof ECFieldFp) {
29: c = new ECCurve.Fp(((ECFieldFp) curve.getField()).getP(),
30: curve.getA(), curve.getB());
31: } else {
32: int k[] = ((ECFieldF2m) curve.getField())
33: .getMidTermsOfReductionPolynomial();
34:
35: if (k.length == 3) {
36: c = new ECCurve.F2m(((ECFieldF2m) curve.getField())
37: .getM(), k[2], k[1], k[0], curve.getA(), curve
38: .getB());
39: } else {
40: c = new ECCurve.F2m(((ECFieldF2m) curve.getField())
41: .getM(), k[0], curve.getA(), curve.getB());
42: }
43: }
44:
45: org.bouncycastle.math.ec.ECPoint p = c.decodePoint(encoded);
46:
47: return new ECPoint(p.getX().toBigInteger(), p.getY()
48: .toBigInteger());
49: }
50: }
|