~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to jdk1.0/org/bouncycastle/asn1/x9/X9Curve.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 java.math.BigInteger;
 
4
 
 
5
import org.bouncycastle.asn1.ASN1EncodableVector;
 
6
import org.bouncycastle.asn1.ASN1OctetString;
 
7
import org.bouncycastle.asn1.ASN1Sequence;
 
8
import org.bouncycastle.asn1.DERBitString;
 
9
import org.bouncycastle.asn1.DEREncodable;
 
10
import org.bouncycastle.asn1.DERInteger;
 
11
import org.bouncycastle.asn1.DERObject;
 
12
import org.bouncycastle.asn1.DERSequence;
 
13
import org.bouncycastle.math.ec.ECCurve;
 
14
import org.bouncycastle.math.ec.ECCurveFp;
 
15
 
 
16
/**
 
17
 * ASN.1 def for Elliptic-Curve Curve structure. See
 
18
 * X9.62, for further details.
 
19
 */
 
20
public class X9Curve
 
21
    implements DEREncodable, X9ObjectIdentifiers
 
22
{
 
23
    private ECCurve     curve;
 
24
    private byte[]      seed;
 
25
 
 
26
        public X9Curve(
 
27
                ECCurve     curve)
 
28
        {
 
29
        this.curve = curve;
 
30
        this.seed = null;
 
31
        }
 
32
 
 
33
        public X9Curve(
 
34
                ECCurve     curve,
 
35
        byte[]      seed)
 
36
        {
 
37
        this.curve = curve;
 
38
        this.seed = seed;
 
39
        }
 
40
 
 
41
    public X9Curve(
 
42
        X9FieldID               fieldID,
 
43
        ASN1Sequence  seq)
 
44
    {
 
45
        if (fieldID.getIdentifier().equals(prime_field))
 
46
        {
 
47
            BigInteger      q = ((DERInteger)fieldID.getParameters()).getValue();
 
48
            X9FieldElement  x9A = new X9FieldElement(true, q, (ASN1OctetString)seq.getObjectAt(0));
 
49
            X9FieldElement  x9B = new X9FieldElement(true, q, (ASN1OctetString)seq.getObjectAt(1));
 
50
            curve = new ECCurveFp(q, x9A.getValue().toBigInteger(), x9B.getValue().toBigInteger());
 
51
        }
 
52
        else
 
53
        {
 
54
            throw new RuntimeException("not implemented");
 
55
        }
 
56
 
 
57
        if (seq.size() == 3)
 
58
        {
 
59
            seed = ((DERBitString)seq.getObjectAt(2)).getBytes();
 
60
        }
 
61
    }
 
62
 
 
63
    public ECCurve  getCurve()
 
64
    {
 
65
        return curve;
 
66
    }
 
67
 
 
68
    public byte[]   getSeed()
 
69
    {
 
70
        return seed;
 
71
    }
 
72
 
 
73
    /**
 
74
     * <pre>
 
75
     *  Curve ::= SEQUENCE {
 
76
     *      a               FieldElement,
 
77
     *      b               FieldElement,
 
78
     *      seed            BIT STRING      OPTIONAL
 
79
     *  }
 
80
     * </pre>
 
81
     */
 
82
    public DERObject getDERObject()
 
83
    {
 
84
        ASN1EncodableVector seq = new ASN1EncodableVector();
 
85
 
 
86
        seq.add(new X9FieldElement(curve.getA()).getDERObject());
 
87
        seq.add(new X9FieldElement(curve.getB()).getDERObject());
 
88
 
 
89
        if (seed != null)
 
90
        {
 
91
            seq.add(new DERBitString(seed));
 
92
        }
 
93
 
 
94
        return new DERSequence(seq);
 
95
    }
 
96
}