~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/src/org/bouncycastle/asn1/test/X9Test.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.test;
 
2
 
 
3
import java.io.ByteArrayInputStream;
 
4
import java.io.ByteArrayOutputStream;
 
5
import java.math.BigInteger;
 
6
 
 
7
import org.bouncycastle.math.ec.ECFieldElement;
 
8
import org.bouncycastle.math.ec.ECPoint;
 
9
import org.bouncycastle.util.encoders.Base64;
 
10
import org.bouncycastle.util.test.SimpleTest;
 
11
import org.bouncycastle.asn1.ASN1InputStream;
 
12
import org.bouncycastle.asn1.ASN1OctetString;
 
13
import org.bouncycastle.asn1.DERObject;
 
14
import org.bouncycastle.asn1.DEROutputStream;
 
15
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
 
16
import org.bouncycastle.asn1.sec.ECPrivateKeyStructure;
 
17
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
 
18
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
 
19
import org.bouncycastle.asn1.x9.X962NamedCurves;
 
20
import org.bouncycastle.asn1.x9.X962Parameters;
 
21
import org.bouncycastle.asn1.x9.X9ECParameters;
 
22
import org.bouncycastle.asn1.x9.X9ECPoint;
 
23
import org.bouncycastle.asn1.x9.X9IntegerConverter;
 
24
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
 
25
 
 
26
public class X9Test
 
27
    extends SimpleTest
 
28
{
 
29
    private byte[] namedPub = Base64.decode("MBowEwYHKoZIzj0CAQYIKoZIzj0DAQEDAwADAQ==");
 
30
    private byte[] expPub = Base64.decode(
 
31
                "MIHfMIHXBgcqhkjOPQIBMIHLAgEBMCkGByqGSM49AQECHn///////////////3///////4AAAA"
 
32
              + "AAAH///////zBXBB5///////////////9///////+AAAAAAAB///////wEHiVXBfoqMGZUsfTL"
 
33
              + "A9anUKMMJQEC1JiHF9m6FattPgMVAH1zdBaP/jRxtgqFdoahlHXTv6L/BB8DZ2iujhi7ks/PAF"
 
34
              + "yUmqLG2UhT0OZgu/hUsclQX+laAh5///////////////9///+XXetBs6YFfDxDIUZSZVECAQED"
 
35
              + "AwADAQ==");
 
36
 
 
37
    private byte[] namedPriv = Base64.decode("MCICAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQEECDAGAgEBBAEK");
 
38
    private byte[] expPriv = Base64.decode(
 
39
                "MIHnAgEAMIHXBgcqhkjOPQIBMIHLAgEBMCkGByqGSM49AQECHn///////////////3///////4"
 
40
              + "AAAAAAAH///////zBXBB5///////////////9///////+AAAAAAAB///////wEHiVXBfoqMGZU"
 
41
              + "sfTLA9anUKMMJQEC1JiHF9m6FattPgMVAH1zdBaP/jRxtgqFdoahlHXTv6L/BB8DZ2iujhi7ks"
 
42
              + "/PAFyUmqLG2UhT0OZgu/hUsclQX+laAh5///////////////9///+XXetBs6YFfDxDIUZSZVEC"
 
43
              + "AQEECDAGAgEBBAEU");
 
44
 
 
45
    private void encodePublicKey()
 
46
        throws Exception
 
47
    {
 
48
        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
 
49
        DEROutputStream         dOut = new DEROutputStream(bOut);
 
50
        X9ECParameters          ecP = X962NamedCurves.getByOID(X9ObjectIdentifiers.prime239v3);
 
51
 
 
52
        X9IntegerConverter conv = new X9IntegerConverter();
 
53
 
 
54
        if (conv.getByteLength(ecP.getCurve()) != 30)
 
55
        {
 
56
            fail("wrong byte length reported for curve");
 
57
        }
 
58
 
 
59
        if (ecP.getCurve().getFieldSize() != 239)
 
60
        {
 
61
            fail("wrong field size reported for curve");
 
62
        }
 
63
 
 
64
        //
 
65
        // named curve
 
66
        //
 
67
        X962Parameters          params = new X962Parameters(X9ObjectIdentifiers.prime192v1);
 
68
 
 
69
        ASN1OctetString         p = (ASN1OctetString)(new X9ECPoint(new ECPoint.Fp(ecP.getCurve(), new ECFieldElement.Fp(BigInteger.valueOf(2), BigInteger.valueOf(1)), new ECFieldElement.Fp(BigInteger.valueOf(4), BigInteger.valueOf(3)), true)).getDERObject());
 
70
 
 
71
        SubjectPublicKeyInfo    info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());
 
72
 
 
73
        if (!areEqual(info.getEncoded(), namedPub))
 
74
        {
 
75
            fail("failed public named generation");
 
76
        }
 
77
        
 
78
        ASN1InputStream         aIn = new ASN1InputStream(new ByteArrayInputStream(namedPub));
 
79
        DERObject               o = aIn.readObject();
 
80
        
 
81
        if (!info.equals(o))
 
82
        {
 
83
            fail("failed public named equality");
 
84
        }
 
85
        
 
86
        //
 
87
        // explicit curve parameters
 
88
        //
 
89
        params = new X962Parameters(ecP);
 
90
        
 
91
        info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());
 
92
 
 
93
        if (!areEqual(info.getEncoded(), expPub))
 
94
        {
 
95
            fail("failed public explicit generation");
 
96
        }
 
97
        
 
98
        aIn = new ASN1InputStream(new ByteArrayInputStream(expPub));
 
99
        o = aIn.readObject();
 
100
        
 
101
        if (!info.equals(o))
 
102
        {
 
103
            fail("failed public explicit equality");
 
104
        }
 
105
    }
 
106
    
 
107
    private void encodePrivateKey()
 
108
        throws Exception
 
109
    {
 
110
        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
 
111
        DEROutputStream         dOut = new DEROutputStream(bOut);
 
112
        X9ECParameters          ecP = X962NamedCurves.getByOID(X9ObjectIdentifiers.prime239v3);
 
113
 
 
114
        //
 
115
        // named curve
 
116
        //
 
117
        X962Parameters          params = new X962Parameters(X9ObjectIdentifiers.prime192v1);
 
118
 
 
119
        ASN1OctetString         p = (ASN1OctetString)(new X9ECPoint(new ECPoint.Fp(ecP.getCurve(), new ECFieldElement.Fp(BigInteger.valueOf(2), BigInteger.valueOf(1)), new ECFieldElement.Fp(BigInteger.valueOf(4), BigInteger.valueOf(3)), true)).getDERObject());
 
120
 
 
121
        PrivateKeyInfo          info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), new ECPrivateKeyStructure(BigInteger.valueOf(10)).getDERObject());
 
122
 
 
123
        if (!areEqual(info.getEncoded(), namedPriv))
 
124
        {
 
125
            fail("failed private named generation");
 
126
        }
 
127
        
 
128
        ASN1InputStream         aIn = new ASN1InputStream(new ByteArrayInputStream(namedPriv));
 
129
        DERObject               o = aIn.readObject();
 
130
        
 
131
        if (!info.equals(o))
 
132
        {
 
133
            fail("failed private named equality");
 
134
        }
 
135
        
 
136
        //
 
137
        // explicit curve parameters
 
138
        //
 
139
        params = new X962Parameters(ecP);
 
140
        
 
141
        info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), new ECPrivateKeyStructure(BigInteger.valueOf(20)).toASN1Object());
 
142
 
 
143
        if (!areEqual(info.getEncoded(), expPriv))
 
144
        {
 
145
            fail("failed private explicit generation");
 
146
        }
 
147
        
 
148
        aIn = new ASN1InputStream(new ByteArrayInputStream(expPriv));
 
149
        o = aIn.readObject();
 
150
        
 
151
        if (!info.equals(o))
 
152
        {
 
153
            fail("failed private explicit equality");
 
154
        }
 
155
    }
 
156
    
 
157
    public void performTest()
 
158
        throws Exception
 
159
    {
 
160
        encodePublicKey();
 
161
        encodePrivateKey();
 
162
    }
 
163
 
 
164
    public String getName()
 
165
    {
 
166
        return "X9";
 
167
    }
 
168
 
 
169
    public static void main(
 
170
        String[]    args)
 
171
    {
 
172
        runTest(new X9Test());
 
173
    }
 
174
}