~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/asn1/x9/X9ECParameters.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.asn1.x9;
 
2
 
 
3
import org.bouncycastle.asn1.ASN1Encodable;
 
4
import org.bouncycastle.asn1.ASN1EncodableVector;
 
5
import org.bouncycastle.asn1.ASN1OctetString;
 
6
import org.bouncycastle.asn1.ASN1Sequence;
 
7
import org.bouncycastle.asn1.DERInteger;
 
8
import org.bouncycastle.asn1.DERObject;
 
9
import org.bouncycastle.asn1.DERSequence;
 
10
import org.bouncycastle.math.ec.ECCurve;
 
11
import org.bouncycastle.math.ec.ECPoint;
 
12
 
 
13
import java.math.BigInteger;
 
14
 
 
15
/**
 
16
 * ASN.1 def for Elliptic-Curve ECParameters structure. See
 
17
 * X9.62, for further details.
 
18
 */
 
19
public class X9ECParameters
 
20
    extends ASN1Encodable
 
21
    implements X9ObjectIdentifiers
 
22
{
 
23
    private static final BigInteger   ONE = BigInteger.valueOf(1);
 
24
 
 
25
    private X9FieldID           fieldID;
 
26
    private ECCurve             curve;
 
27
    private ECPoint             g;
 
28
    private BigInteger          n;
 
29
    private BigInteger          h;
 
30
    private byte[]              seed;
 
31
 
 
32
    public X9ECParameters(
 
33
        ASN1Sequence  seq)
 
34
    {
 
35
        if (!(seq.getObjectAt(0) instanceof DERInteger)
 
36
           || !((DERInteger)seq.getObjectAt(0)).getValue().equals(ONE))
 
37
        {
 
38
            throw new IllegalArgumentException("bad version in X9ECParameters");
 
39
        }
 
40
 
 
41
        X9Curve     x9c = new X9Curve(
 
42
                        new X9FieldID((ASN1Sequence)seq.getObjectAt(1)),
 
43
                        (ASN1Sequence)seq.getObjectAt(2));
 
44
 
 
45
        this.curve = x9c.getCurve();
 
46
        this.g = new X9ECPoint(curve, (ASN1OctetString)seq.getObjectAt(3)).getPoint();
 
47
        this.n = ((DERInteger)seq.getObjectAt(4)).getValue();
 
48
        this.seed = x9c.getSeed();
 
49
 
 
50
        if (seq.size() == 6)
 
51
        {
 
52
            this.h = ((DERInteger)seq.getObjectAt(5)).getValue();
 
53
        }
 
54
    }
 
55
 
 
56
    public X9ECParameters(
 
57
        ECCurve     curve,
 
58
        ECPoint     g,
 
59
        BigInteger  n)
 
60
    {
 
61
        this(curve, g, n, ONE, null);
 
62
    }
 
63
 
 
64
    public X9ECParameters(
 
65
        ECCurve     curve,
 
66
        ECPoint     g,
 
67
        BigInteger  n,
 
68
        BigInteger  h)
 
69
    {
 
70
        this(curve, g, n, h, null);
 
71
    }
 
72
 
 
73
    public X9ECParameters(
 
74
        ECCurve     curve,
 
75
        ECPoint     g,
 
76
        BigInteger  n,
 
77
        BigInteger  h,
 
78
        byte[]      seed)
 
79
    {
 
80
        this.curve = curve;
 
81
        this.g = g;
 
82
        this.n = n;
 
83
        this.h = h;
 
84
        this.seed = seed;
 
85
 
 
86
        if (curve instanceof ECCurve.Fp)
 
87
        {
 
88
            this.fieldID = new X9FieldID(((ECCurve.Fp)curve).getQ());
 
89
        }
 
90
        else
 
91
        {
 
92
            if (curve instanceof ECCurve.F2m)
 
93
            {
 
94
                ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
 
95
                this.fieldID = new X9FieldID(curveF2m.getM(), curveF2m.getK1(),
 
96
                    curveF2m.getK2(), curveF2m.getK3());
 
97
            }
 
98
        }
 
99
    }
 
100
 
 
101
    public ECCurve getCurve()
 
102
    {
 
103
        return curve;
 
104
    }
 
105
 
 
106
    public ECPoint getG()
 
107
    {
 
108
        return g;
 
109
    }
 
110
 
 
111
    public BigInteger getN()
 
112
    {
 
113
        return n;
 
114
    }
 
115
 
 
116
    public BigInteger getH()
 
117
    {
 
118
        if (h == null)
 
119
        {
 
120
            return ONE;        // TODO - this should be calculated, it will cause issues with custom curves.
 
121
        }
 
122
 
 
123
        return h;
 
124
    }
 
125
 
 
126
    public byte[] getSeed()
 
127
    {
 
128
        return seed;
 
129
    }
 
130
 
 
131
    /**
 
132
     * Produce an object suitable for an ASN1OutputStream.
 
133
     * <pre>
 
134
     *  ECParameters ::= SEQUENCE {
 
135
     *      version         INTEGER { ecpVer1(1) } (ecpVer1),
 
136
     *      fieldID         FieldID {{FieldTypes}},
 
137
     *      curve           X9Curve,
 
138
     *      base            X9ECPoint,
 
139
     *      order           INTEGER,
 
140
     *      cofactor        INTEGER OPTIONAL
 
141
     *  }
 
142
     * </pre>
 
143
     */
 
144
    public DERObject toASN1Object()
 
145
    {
 
146
        ASN1EncodableVector v = new ASN1EncodableVector();
 
147
 
 
148
        v.add(new DERInteger(1));
 
149
        v.add(fieldID);
 
150
        v.add(new X9Curve(curve, seed));
 
151
        v.add(new X9ECPoint(g));
 
152
        v.add(new DERInteger(n));
 
153
 
 
154
        if (h != null)
 
155
        {
 
156
            v.add(new DERInteger(h));
 
157
        }
 
158
 
 
159
        return new DERSequence(v);
 
160
    }
 
161
}