blob: ecd22a8178d9b82ddaac6cb2fa7020a1cbb7fbf4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-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 DHKeyFactory
28 * @author Jan Luehe
29 */
30
31import java.io.*;
32import java.math.BigInteger;
33import java.security.*;
34import java.security.spec.*;
35import java.security.interfaces.*;
36import javax.crypto.*;
37import javax.crypto.spec.*;
38import javax.crypto.interfaces.*;
39import com.sun.crypto.provider.SunJCE;
40
41/**
42 * This test creates a DH keypair, retrieves the encodings of the DH public and
43 * private key, and feeds those encodings to a DH key factory, which parses
44 * the encodings and creates a DH public and private key from them.
45 */
46
47public class DHKeyFactory {
48
49 private DHKeyFactory() {}
50
51 public static void main(String argv[]) throws Exception {
52 // Add JCE to the list of providers
53 SunJCE jce = new SunJCE();
54 Security.addProvider(jce);
55
56 DHKeyFactory test = new DHKeyFactory();
57 test.run();
58 System.out.println("Test Passed");
59 }
60
61 private void run() throws Exception {
62
63 DHParameterSpec dhSkipParamSpec;
64
65 // use some pre-generated, default DH parameters
66 System.err.println("Using SKIP Diffie-Hellman parameters");
67 dhSkipParamSpec = new DHParameterSpec(skip1024Modulus,
68 skip1024Base);
69
70 KeyPairGenerator kpgen = KeyPairGenerator.getInstance("DH");
71 kpgen.initialize(dhSkipParamSpec);
72 KeyPair kp = kpgen.generateKeyPair();
73
74 // get the public key encoding
75 byte[] pubKeyEnc = kp.getPublic().getEncoded();
76
77 // get the private key encoding
78 byte[] privKeyEnc = kp.getPrivate().getEncoded();
79
80 KeyFactory kfac = KeyFactory.getInstance("DH");
81
82 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKeyEnc);
83 PublicKey pubKey = kfac.generatePublic(x509KeySpec);
84
85 PKCS8EncodedKeySpec pkcsKeySpec = new PKCS8EncodedKeySpec(privKeyEnc);
86 PrivateKey privKey = kfac.generatePrivate(pkcsKeySpec);
87 }
88
89 // The 1024 bit Diffie-Hellman modulus values used by SKIP
90 private static final byte skip1024ModulusBytes[] = {
91 (byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,
92 (byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,
93 (byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,
94 (byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,
95 (byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,
96 (byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,
97 (byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,
98 (byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,
99 (byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,
100 (byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,
101 (byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,
102 (byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,
103 (byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,
104 (byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,
105 (byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,
106 (byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,
107 (byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,
108 (byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,
109 (byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,
110 (byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,
111 (byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,
112 (byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,
113 (byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,
114 (byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,
115 (byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,
116 (byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,
117 (byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,
118 (byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,
119 (byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,
120 (byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,
121 (byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,
122 (byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7
123 };
124
125 // The SKIP 1024 bit modulus
126 private static final BigInteger skip1024Modulus
127 = new BigInteger(1, skip1024ModulusBytes);
128
129 // The base used with the SKIP 1024 bit modulus
130 private static final BigInteger skip1024Base = BigInteger.valueOf(2);
131}