blob: d4e6a3933ffc5ef4a833b3191498a19facfb3809 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 0000000
27 * @summary DHGenSharedSecret
28 * @author Jan Luehe
29 */
30import java.security.*;
31import java.security.spec.*;
32import java.security.interfaces.*;
33import javax.crypto.*;
34import javax.crypto.spec.*;
35import com.sun.crypto.provider.*;
36import java.math.BigInteger;
37
38public class DHGenSharedSecret {
39
40 static byte[] DHPrime = {
41(byte)0x00, (byte)0x8D, (byte)0x8A, (byte)0x6C, (byte)0x7F, (byte)0xCC,
42(byte)0xA5, (byte)0xBF, (byte)0x9C, (byte)0xE1, (byte)0xFA, (byte)0x3C,
43(byte)0xCA, (byte)0x98, (byte)0xB7, (byte)0x99, (byte)0xD1, (byte)0xE5,
44(byte)0x2C, (byte)0xC0, (byte)0x26, (byte)0x97, (byte)0x12, (byte)0x80,
45(byte)0x12, (byte)0xEF, (byte)0x0B, (byte)0xDE, (byte)0x71, (byte)0x76,
46(byte)0xAA, (byte)0x2D, (byte)0x86, (byte)0x41, (byte)0x0E, (byte)0x6A,
47(byte)0xC2, (byte)0x12, (byte)0xAA, (byte)0xAA, (byte)0xE4, (byte)0x84,
48(byte)0x80, (byte)0x13, (byte)0x95, (byte)0x06, (byte)0xC4, (byte)0x83,
49(byte)0xB9, (byte)0xD3, (byte)0x72, (byte)0xC5, (byte)0xC8, (byte)0x85,
50(byte)0x96, (byte)0x59, (byte)0x08, (byte)0xFA, (byte)0x9E, (byte)0x3C,
51(byte)0xDC, (byte)0x92, (byte)0x28, (byte)0xC3, (byte)0x1D, (byte)0x6F,
52(byte)0x44, (byte)0x36, (byte)0x70, (byte)0x40, (byte)0x80, (byte)0xF1,
53(byte)0x35
54 };
55
56 static byte[] DHBase = {
57(byte)0x72, (byte)0x21, (byte)0xB3, (byte)0xA8, (byte)0x83, (byte)0xDD,
58(byte)0x76, (byte)0xF5, (byte)0x0D, (byte)0x9B, (byte)0x81, (byte)0x11,
59(byte)0x15, (byte)0x03, (byte)0x6D, (byte)0x4D, (byte)0x46, (byte)0x65,
60(byte)0x30, (byte)0xB0, (byte)0xFA, (byte)0xFE, (byte)0xBE, (byte)0xA8,
61(byte)0xD9, (byte)0x83, (byte)0x33, (byte)0x54, (byte)0xC7, (byte)0xF6,
62(byte)0x81, (byte)0xAC, (byte)0xCC, (byte)0xA3, (byte)0xAE, (byte)0xAA,
63(byte)0xC8, (byte)0x11, (byte)0x38, (byte)0xD4, (byte)0x4F, (byte)0xC4,
64(byte)0x89, (byte)0xD3, (byte)0x72, (byte)0xEE, (byte)0x22, (byte)0x5A,
65(byte)0x68, (byte)0xF7, (byte)0xAC, (byte)0x24, (byte)0x01, (byte)0x9B,
66(byte)0xE9, (byte)0x08, (byte)0xFE, (byte)0x58, (byte)0x0A, (byte)0xCF,
67(byte)0xB9, (byte)0x52, (byte)0xB4, (byte)0x02, (byte)0x73, (byte)0xA4,
68(byte)0xA6, (byte)0xB9, (byte)0x0C, (byte)0x8D, (byte)0xA7, (byte)0xFB,
69 };
70
71 public static void main(String[] args) throws Exception {
72 SunJCE jce = new SunJCE();
73 Security.addProvider(jce);
74 DHGenSharedSecret test = new DHGenSharedSecret();
75 test.run();
76 }
77
78 public void run() throws Exception {
79 long start, end;
80
81 BigInteger p = new BigInteger(1, DHPrime);
82 BigInteger g = new BigInteger(1, DHBase);
83 int l = 512;
84
85 DHParameterSpec spec =
86 new DHParameterSpec(p, g, l);
87
88 // generate keyPairs using parameters
89 KeyPairGenerator keyGen =
90 KeyPairGenerator.getInstance("DH", "SunJCE");
91 keyGen.initialize(spec);
92
93 // Alice generates her key pairs
94 KeyPair keyA = keyGen.generateKeyPair();
95
96 // Bob generates his key pairs
97 KeyPair keyB = keyGen.generateKeyPair();
98
99 // Alice encodes her public key in x509 format
100 // , and sends it over to Bob.
101 byte[] alicePubKeyEnc = keyA.getPublic().getEncoded();
102
103 // bob encodes his publicKey in x509 format and
104 // sends it over to Alice
105 byte[] bobPubKeyEnc = keyB.getPublic().getEncoded();
106
107 // bob uses it to generate Secret
108 X509EncodedKeySpec x509Spec =
109 new X509EncodedKeySpec(alicePubKeyEnc);
110 KeyFactory bobKeyFac = KeyFactory.getInstance("DH", "SunJCE");
111 PublicKey alicePubKey = bobKeyFac.generatePublic(x509Spec);
112
113
114 KeyAgreement bobAlice = KeyAgreement.getInstance("DH", "SunJCE");
115 start = System.currentTimeMillis();
116 bobAlice.init(keyB.getPrivate());
117 bobAlice.doPhase(alicePubKey, true);
118 byte[] bobSecret = bobAlice.generateSecret();
119 end = System.currentTimeMillis();
120
121 System.out.println("Time elapsed: " + (end - start));
122 System.out.println("Test Passed!");
123 }
124}