001: package org.bouncycastle.jce.provider.test;
002:
003: import java.math.BigInteger;
004: import java.security.KeyFactory;
005: import java.security.KeyPair;
006: import java.security.KeyPairGenerator;
007: import java.security.PrivateKey;
008: import java.security.PublicKey;
009: import java.security.SecureRandom;
010: import java.security.Security;
011: import java.security.Signature;
012: import java.security.spec.PKCS8EncodedKeySpec;
013: import java.security.spec.X509EncodedKeySpec;
014:
015: import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
016: import org.bouncycastle.jce.interfaces.GOST3410PrivateKey;
017: import org.bouncycastle.jce.interfaces.GOST3410PublicKey;
018: import org.bouncycastle.jce.provider.BouncyCastleProvider;
019: import org.bouncycastle.jce.spec.ECParameterSpec;
020: import org.bouncycastle.jce.spec.ECPrivateKeySpec;
021: import org.bouncycastle.jce.spec.ECPublicKeySpec;
022: import org.bouncycastle.jce.spec.GOST3410ParameterSpec;
023: import org.bouncycastle.math.ec.ECCurve;
024: import org.bouncycastle.math.ec.ECFieldElement;
025: import org.bouncycastle.math.ec.ECPoint;
026: import org.bouncycastle.util.BigIntegers;
027: import org.bouncycastle.util.test.FixedSecureRandom;
028: import org.bouncycastle.util.test.SimpleTest;
029:
030: public class GOST3410Test extends SimpleTest {
031: private void ecGOST3410Test() throws Exception {
032:
033: BigInteger r = new BigInteger(
034: "29700980915817952874371204983938256990422752107994319651632687982059210933395");
035: BigInteger s = new BigInteger(
036: "46959264877825372965922731380059061821746083849389763294914877353246631700866");
037:
038: byte[] kData = BigIntegers
039: .asUnsignedByteArray(new BigInteger(
040: "53854137677348463731403841147996619241504003434302020712960838528893196233395"));
041:
042: SecureRandom k = new FixedSecureRandom(kData);
043:
044: BigInteger mod_p = new BigInteger(
045: "57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p
046:
047: ECCurve curve = new ECCurve.Fp(
048: mod_p, // p
049: new BigInteger("7"), // a
050: new BigInteger(
051: "43308876546767276905765904595650931995942111794451039583252968842033849580414")); // b
052:
053: ECParameterSpec spec = new ECParameterSpec(
054: curve,
055: new ECPoint.Fp(curve,
056: new ECFieldElement.Fp(mod_p,
057: new BigInteger("2")), // x
058: new ECFieldElement.Fp(
059: mod_p,
060: new BigInteger(
061: "4018974056539037503335449422937059775635739389905545080690979365213431566280"))), // y
062: new BigInteger(
063: "57896044618658097711785492504343953927082934583725450622380973592137631069619")); // q
064:
065: ECPrivateKeySpec priKey = new ECPrivateKeySpec(
066: new BigInteger(
067: "55441196065363246126355624130324183196576709222340016572108097750006097525544"), // d
068: spec);
069:
070: ECPublicKeySpec pubKey = new ECPublicKeySpec(
071: new ECPoint.Fp(
072: curve,
073: new ECFieldElement.Fp(
074: mod_p,
075: new BigInteger(
076: "57520216126176808443631405023338071176630104906313632182896741342206604859403")), // x
077: new ECFieldElement.Fp(
078: mod_p,
079: new BigInteger(
080: "17614944419213781543809391949654080031942662045363639260709847859438286763994"))), // y
081: spec);
082:
083: Signature sgr = Signature.getInstance("ECGOST3410", "BC");
084: KeyFactory f = KeyFactory.getInstance("ECGOST3410", "BC");
085: PrivateKey sKey = f.generatePrivate(priKey);
086: PublicKey vKey = f.generatePublic(pubKey);
087:
088: sgr.initSign(sKey, k);
089:
090: byte[] message = new byte[] { (byte) 'a', (byte) 'b',
091: (byte) 'c' };
092:
093: sgr.update(message);
094:
095: byte[] sigBytes = sgr.sign();
096:
097: sgr.initVerify(vKey);
098:
099: sgr.update(message);
100:
101: if (!sgr.verify(sigBytes)) {
102: fail("ECGOST3410 verification failed");
103: }
104:
105: BigInteger[] sig = decode(sigBytes);
106:
107: if (!r.equals(sig[0])) {
108: fail(": r component wrong."
109: + System.getProperty("line.separator")
110: + " expecting: " + r
111: + System.getProperty("line.separator")
112: + " got : " + sig[0]);
113: }
114:
115: if (!s.equals(sig[1])) {
116: fail(": s component wrong."
117: + System.getProperty("line.separator")
118: + " expecting: " + s
119: + System.getProperty("line.separator")
120: + " got : " + sig[1]);
121: }
122: }
123:
124: private void generationTest() throws Exception {
125: Signature s = Signature.getInstance("GOST3410", "BC");
126: KeyPairGenerator g = KeyPairGenerator.getInstance("GOST3410",
127: "BC");
128: byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
129: GOST3410ParameterSpec gost3410P = new GOST3410ParameterSpec(
130: CryptoProObjectIdentifiers.gostR3410_94_CryptoPro_A
131: .getId());
132:
133: g.initialize(gost3410P, new SecureRandom());
134:
135: KeyPair p = g.generateKeyPair();
136:
137: PrivateKey sKey = p.getPrivate();
138: PublicKey vKey = p.getPublic();
139:
140: s.initSign(sKey);
141:
142: s.update(data);
143:
144: byte[] sigBytes = s.sign();
145:
146: s = Signature.getInstance("GOST3410", "BC");
147:
148: s.initVerify(vKey);
149:
150: s.update(data);
151:
152: if (!s.verify(sigBytes)) {
153: fail("GOST3410 verification failed");
154: }
155:
156: //
157: // default iniialisation test
158: //
159: s = Signature.getInstance("GOST3410", "BC");
160: g = KeyPairGenerator.getInstance("GOST3410", "BC");
161:
162: p = g.generateKeyPair();
163:
164: sKey = p.getPrivate();
165: vKey = p.getPublic();
166:
167: s.initSign(sKey);
168:
169: s.update(data);
170:
171: sigBytes = s.sign();
172:
173: s = Signature.getInstance("GOST3410", "BC");
174:
175: s.initVerify(vKey);
176:
177: s.update(data);
178:
179: if (!s.verify(sigBytes)) {
180: fail("GOST3410 verification failed");
181: }
182:
183: //
184: // encoded test
185: //
186: KeyFactory f = KeyFactory.getInstance("GOST3410", "BC");
187:
188: X509EncodedKeySpec x509s = new X509EncodedKeySpec(vKey
189: .getEncoded());
190: GOST3410PublicKey k1 = (GOST3410PublicKey) f
191: .generatePublic(x509s);
192:
193: if (!k1.getY().equals(((GOST3410PublicKey) vKey).getY())) {
194: fail("public number not decoded properly");
195: }
196:
197: PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec(sKey
198: .getEncoded());
199: GOST3410PrivateKey k2 = (GOST3410PrivateKey) f
200: .generatePrivate(pkcs8);
201:
202: if (!k2.getX().equals(((GOST3410PrivateKey) sKey).getX())) {
203: fail("private number not decoded properly");
204: }
205:
206: //
207: // ECGOST3410 generation test
208: //
209: s = Signature.getInstance("ECGOST3410", "BC");
210: g = KeyPairGenerator.getInstance("ECGOST3410", "BC");
211:
212: BigInteger mod_p = new BigInteger(
213: "57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p
214:
215: ECCurve curve = new ECCurve.Fp(
216: mod_p, // p
217: new BigInteger("7"), // a
218: new BigInteger(
219: "43308876546767276905765904595650931995942111794451039583252968842033849580414")); // b
220:
221: ECParameterSpec ecSpec = new ECParameterSpec(
222: curve,
223: new ECPoint.Fp(curve,
224: new ECFieldElement.Fp(mod_p,
225: new BigInteger("2")), // x
226: new ECFieldElement.Fp(
227: mod_p,
228: new BigInteger(
229: "4018974056539037503335449422937059775635739389905545080690979365213431566280"))), // y
230: new BigInteger(
231: "57896044618658097711785492504343953927082934583725450622380973592137631069619")); // q
232:
233: g.initialize(ecSpec, new SecureRandom());
234:
235: p = g.generateKeyPair();
236:
237: sKey = p.getPrivate();
238: vKey = p.getPublic();
239:
240: s.initSign(sKey);
241:
242: s.update(data);
243:
244: sigBytes = s.sign();
245:
246: s = Signature.getInstance("ECGOST3410", "BC");
247:
248: s.initVerify(vKey);
249:
250: s.update(data);
251:
252: if (!s.verify(sigBytes)) {
253: fail("ECGOST3410 verification failed");
254: }
255: }
256:
257: private void parametersTest() throws Exception {
258: // AlgorithmParameterGenerator a = AlgorithmParameterGenerator.getInstance("GOST3410", "BC");
259: // a.init(512, random);
260: // AlgorithmParameters params = a.generateParameters();
261: //
262: // byte[] encodeParams = params.getEncoded();
263: //
264: // AlgorithmParameters a2 = AlgorithmParameters.getInstance("GOST3410", "BC");
265: // a2.init(encodeParams);
266: //
267: // // a and a2 should be equivalent!
268: // byte[] encodeParams_2 = a2.getEncoded();
269: //
270: // if (!arrayEquals(encodeParams, encodeParams_2))
271: // {
272: // fail("encode/decode parameters failed");
273: // }
274:
275: GOST3410ParameterSpec gost3410P = new GOST3410ParameterSpec(
276: CryptoProObjectIdentifiers.gostR3410_94_CryptoPro_B
277: .getId());
278:
279: KeyPairGenerator g = KeyPairGenerator.getInstance("GOST3410",
280: "BC");
281: g.initialize(gost3410P, new SecureRandom());
282: KeyPair p = g.generateKeyPair();
283:
284: PrivateKey sKey = p.getPrivate();
285: PublicKey vKey = p.getPublic();
286:
287: Signature s = Signature.getInstance("GOST3410", "BC");
288: byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
289:
290: s.initSign(sKey);
291:
292: s.update(data);
293:
294: byte[] sigBytes = s.sign();
295:
296: s = Signature.getInstance("GOST3410", "BC");
297:
298: s.initVerify(vKey);
299:
300: s.update(data);
301:
302: if (!s.verify(sigBytes)) {
303: fail("GOST3410 verification failed");
304: }
305: }
306:
307: private BigInteger[] decode(byte[] encoding) {
308: byte[] r = new byte[32];
309: byte[] s = new byte[32];
310:
311: System.arraycopy(encoding, 0, s, 0, 32);
312:
313: System.arraycopy(encoding, 32, r, 0, 32);
314:
315: BigInteger[] sig = new BigInteger[2];
316:
317: sig[0] = new BigInteger(1, r);
318: sig[1] = new BigInteger(1, s);
319:
320: return sig;
321: }
322:
323: public String getName() {
324: return "GOST3410/ECGOST3410";
325: }
326:
327: public void performTest() throws Exception {
328: ecGOST3410Test();
329: generationTest();
330: parametersTest();
331: }
332:
333: public static void main(String[] args) {
334: Security.addProvider(new BouncyCastleProvider());
335:
336: runTest(new GOST3410Test());
337: }
338: }
|