Source Code Cross Referenced for GOST3410Test.java in  » Security » Bouncy-Castle » org » bouncycastle » jce » provider » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Security » Bouncy Castle » org.bouncycastle.jce.provider.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.