001: package org.bouncycastle.asn1.test;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.math.BigInteger;
006:
007: import org.bouncycastle.math.ec.ECFieldElement;
008: import org.bouncycastle.math.ec.ECPoint;
009: import org.bouncycastle.util.encoders.Base64;
010: import org.bouncycastle.util.test.SimpleTest;
011: import org.bouncycastle.asn1.ASN1InputStream;
012: import org.bouncycastle.asn1.ASN1OctetString;
013: import org.bouncycastle.asn1.DERObject;
014: import org.bouncycastle.asn1.DEROutputStream;
015: import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
016: import org.bouncycastle.asn1.sec.ECPrivateKeyStructure;
017: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
018: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
019: import org.bouncycastle.asn1.x9.X962NamedCurves;
020: import org.bouncycastle.asn1.x9.X962Parameters;
021: import org.bouncycastle.asn1.x9.X9ECParameters;
022: import org.bouncycastle.asn1.x9.X9ECPoint;
023: import org.bouncycastle.asn1.x9.X9IntegerConverter;
024: import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
025:
026: public class X9Test extends SimpleTest {
027: private byte[] namedPub = Base64
028: .decode("MBowEwYHKoZIzj0CAQYIKoZIzj0DAQEDAwADAQ==");
029: private byte[] expPub = Base64
030: .decode("MIHcMIHUBgcqhkjOPQIBMIHIAgEBMCkGByqGSM49AQECHn///////////////3///////4AAAA"
031: + "AAAH///////zBXBB5///////////////9///////+AAAAAAAB///////wEHiVXBfoqMGZUsfTL"
032: + "A9anUKMMJQEC1JiHF9m6FattPgMVAH1zdBaP/jRxtgqFdoahlHXTv6L/BB8DZ2iujhi7ks/PAF"
033: + "yUmqLG2UhT0OZgu/hUsclQX+laAh5///////////////9///+XXetBs6YFfDxDIUZSZVEDAwAD"
034: + "AQ==");
035:
036: private byte[] namedPriv = Base64
037: .decode("MCICAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQEECDAGAgEBBAEK");
038: private byte[] expPriv = Base64
039: .decode("MIHkAgEAMIHUBgcqhkjOPQIBMIHIAgEBMCkGByqGSM49AQECHn///////////////3///////4"
040: + "AAAAAAAH///////zBXBB5///////////////9///////+AAAAAAAB///////wEHiVXBfoqMGZU"
041: + "sfTLA9anUKMMJQEC1JiHF9m6FattPgMVAH1zdBaP/jRxtgqFdoahlHXTv6L/BB8DZ2iujhi7ks"
042: + "/PAFyUmqLG2UhT0OZgu/hUsclQX+laAh5///////////////9///+XXetBs6YFfDxDIUZSZVEE"
043: + "CDAGAgEBBAEU");
044:
045: private void encodePublicKey() throws Exception {
046: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
047: DEROutputStream dOut = new DEROutputStream(bOut);
048: X9ECParameters ecP = X962NamedCurves
049: .getByOID(X9ObjectIdentifiers.prime239v3);
050:
051: X9IntegerConverter conv = new X9IntegerConverter();
052:
053: if (conv.getByteLength(ecP.getCurve()) != 30) {
054: fail("wrong byte length reported for curve");
055: }
056:
057: if (ecP.getCurve().getFieldSize() != 239) {
058: fail("wrong field size reported for curve");
059: }
060:
061: //
062: // named curve
063: //
064: X962Parameters params = new X962Parameters(
065: X9ObjectIdentifiers.prime192v1);
066:
067: ASN1OctetString p = (ASN1OctetString) (new X9ECPoint(
068: new ECPoint.Fp(ecP.getCurve(), new ECFieldElement.Fp(
069: BigInteger.valueOf(2), BigInteger.valueOf(1)),
070: new ECFieldElement.Fp(BigInteger.valueOf(4),
071: BigInteger.valueOf(3)), true))
072: .getDERObject());
073:
074: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
075: new AlgorithmIdentifier(
076: X9ObjectIdentifiers.id_ecPublicKey, params), p
077: .getOctets());
078:
079: if (!areEqual(info.getEncoded(), namedPub)) {
080: fail("failed public named generation");
081: }
082:
083: ASN1InputStream aIn = new ASN1InputStream(
084: new ByteArrayInputStream(namedPub));
085: DERObject o = aIn.readObject();
086:
087: if (!info.equals(o)) {
088: fail("failed public named equality");
089: }
090:
091: //
092: // explicit curve parameters
093: //
094: params = new X962Parameters(ecP);
095:
096: info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(
097: X9ObjectIdentifiers.id_ecPublicKey, params), p
098: .getOctets());
099:
100: if (!areEqual(info.getEncoded(), expPub)) {
101: fail("failed public explicit generation");
102: }
103:
104: aIn = new ASN1InputStream(new ByteArrayInputStream(expPub));
105: o = aIn.readObject();
106:
107: if (!info.equals(o)) {
108: fail("failed public explicit equality");
109: }
110: }
111:
112: private void encodePrivateKey() throws Exception {
113: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
114: DEROutputStream dOut = new DEROutputStream(bOut);
115: X9ECParameters ecP = X962NamedCurves
116: .getByOID(X9ObjectIdentifiers.prime239v3);
117:
118: //
119: // named curve
120: //
121: X962Parameters params = new X962Parameters(
122: X9ObjectIdentifiers.prime192v1);
123:
124: ASN1OctetString p = (ASN1OctetString) (new X9ECPoint(
125: new ECPoint.Fp(ecP.getCurve(), new ECFieldElement.Fp(
126: BigInteger.valueOf(2), BigInteger.valueOf(1)),
127: new ECFieldElement.Fp(BigInteger.valueOf(4),
128: BigInteger.valueOf(3)), true))
129: .getDERObject());
130:
131: PrivateKeyInfo info = new PrivateKeyInfo(
132: new AlgorithmIdentifier(
133: X9ObjectIdentifiers.id_ecPublicKey, params),
134: new ECPrivateKeyStructure(BigInteger.valueOf(10))
135: .getDERObject());
136:
137: if (!areEqual(info.getEncoded(), namedPriv)) {
138: fail("failed private named generation");
139: }
140:
141: ASN1InputStream aIn = new ASN1InputStream(
142: new ByteArrayInputStream(namedPriv));
143: DERObject o = aIn.readObject();
144:
145: if (!info.equals(o)) {
146: fail("failed private named equality");
147: }
148:
149: //
150: // explicit curve parameters
151: //
152: params = new X962Parameters(ecP);
153:
154: info = new PrivateKeyInfo(new AlgorithmIdentifier(
155: X9ObjectIdentifiers.id_ecPublicKey, params),
156: new ECPrivateKeyStructure(BigInteger.valueOf(20))
157: .toASN1Object());
158:
159: if (!areEqual(info.getEncoded(), expPriv)) {
160: fail("failed private explicit generation");
161: }
162:
163: aIn = new ASN1InputStream(new ByteArrayInputStream(expPriv));
164: o = aIn.readObject();
165:
166: if (!info.equals(o)) {
167: fail("failed private explicit equality");
168: }
169: }
170:
171: public void performTest() throws Exception {
172: encodePublicKey();
173: encodePrivateKey();
174: }
175:
176: public String getName() {
177: return "X9";
178: }
179:
180: public static void main(String[] args) {
181: runTest(new X9Test());
182: }
183: }
|