J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 DHKeyAgreement3 |
| 28 | * @author Jan Luehe |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | import java.math.BigInteger; |
| 33 | import java.security.*; |
| 34 | import java.security.spec.*; |
| 35 | import java.security.interfaces.*; |
| 36 | import javax.crypto.*; |
| 37 | import javax.crypto.spec.*; |
| 38 | import javax.crypto.interfaces.*; |
| 39 | import com.sun.crypto.provider.SunJCE; |
| 40 | |
| 41 | /** |
| 42 | * This test utility executes the Diffie-Hellman key agreement protocol |
| 43 | * between 3 parties: Alice, Bob, and Carol. |
| 44 | * |
| 45 | * We use the same 1024 bit prime modulus and base generator that are used by |
| 46 | * SKIP. |
| 47 | */ |
| 48 | |
| 49 | public class DHKeyAgreement3 { |
| 50 | |
| 51 | private DHKeyAgreement3() {} |
| 52 | |
| 53 | public static void main(String argv[]) throws Exception { |
| 54 | // Add JCE to the list of providers |
| 55 | SunJCE jce = new SunJCE(); |
| 56 | Security.addProvider(jce); |
| 57 | |
| 58 | DHKeyAgreement3 keyAgree = new DHKeyAgreement3(); |
| 59 | keyAgree.run(); |
| 60 | System.out.println("Test Passed"); |
| 61 | } |
| 62 | |
| 63 | private void run() throws Exception { |
| 64 | |
| 65 | DHParameterSpec dhSkipParamSpec; |
| 66 | |
| 67 | System.err.println("Using SKIP Diffie-Hellman parameters"); |
| 68 | dhSkipParamSpec = new DHParameterSpec(skip1024Modulus, skip1024Base); |
| 69 | |
| 70 | // Alice creates her own DH key pair |
| 71 | System.err.println("ALICE: Generate DH keypair ..."); |
| 72 | KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH"); |
| 73 | aliceKpairGen.initialize(dhSkipParamSpec); |
| 74 | KeyPair aliceKpair = aliceKpairGen.generateKeyPair(); |
| 75 | |
| 76 | // Bob creates his own DH key pair |
| 77 | System.err.println("BOB: Generate DH keypair ..."); |
| 78 | KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH"); |
| 79 | bobKpairGen.initialize(dhSkipParamSpec); |
| 80 | KeyPair bobKpair = bobKpairGen.generateKeyPair(); |
| 81 | |
| 82 | // Carol creates her own DH key pair |
| 83 | System.err.println("CAROL: Generate DH keypair ..."); |
| 84 | KeyPairGenerator carolKpairGen = KeyPairGenerator.getInstance("DH"); |
| 85 | carolKpairGen.initialize(dhSkipParamSpec); |
| 86 | KeyPair carolKpair = carolKpairGen.generateKeyPair(); |
| 87 | |
| 88 | |
| 89 | // Alice initialize |
| 90 | System.err.println("ALICE: Initialize ..."); |
| 91 | KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH"); |
| 92 | aliceKeyAgree.init(aliceKpair.getPrivate()); |
| 93 | |
| 94 | // Bob initialize |
| 95 | System.err.println("BOB: Initialize ..."); |
| 96 | KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH"); |
| 97 | bobKeyAgree.init(bobKpair.getPrivate()); |
| 98 | |
| 99 | // Carol initialize |
| 100 | System.err.println("CAROL: Initialize ..."); |
| 101 | KeyAgreement carolKeyAgree = KeyAgreement.getInstance("DH"); |
| 102 | carolKeyAgree.init(carolKpair.getPrivate()); |
| 103 | |
| 104 | |
| 105 | // Alice uses Carol's public key |
| 106 | Key ac = aliceKeyAgree.doPhase(carolKpair.getPublic(), false); |
| 107 | |
| 108 | // Bob uses Alice's public key |
| 109 | Key ba = bobKeyAgree.doPhase(aliceKpair.getPublic(), false); |
| 110 | |
| 111 | // Carol uses Bob's public key |
| 112 | Key cb = carolKeyAgree.doPhase(bobKpair.getPublic(), false); |
| 113 | |
| 114 | |
| 115 | // Alice uses Carol's result from above |
| 116 | aliceKeyAgree.doPhase(cb, true); |
| 117 | |
| 118 | // Bob uses Alice's result from above |
| 119 | bobKeyAgree.doPhase(ac, true); |
| 120 | |
| 121 | // Carol uses Bob's result from above |
| 122 | carolKeyAgree.doPhase(ba, true); |
| 123 | |
| 124 | |
| 125 | // Alice, Bob and Carol compute their secrets |
| 126 | byte[] aliceSharedSecret = aliceKeyAgree.generateSecret(); |
| 127 | int aliceLen = aliceSharedSecret.length; |
| 128 | System.out.println("Alice secret: " + toHexString(aliceSharedSecret)); |
| 129 | |
| 130 | byte[] bobSharedSecret = bobKeyAgree.generateSecret(); |
| 131 | int bobLen = bobSharedSecret.length; |
| 132 | System.out.println("Bob secret: " + toHexString(bobSharedSecret)); |
| 133 | |
| 134 | byte[] carolSharedSecret = carolKeyAgree.generateSecret(); |
| 135 | int carolLen = carolSharedSecret.length; |
| 136 | System.out.println("Carol secret: " + toHexString(carolSharedSecret)); |
| 137 | |
| 138 | |
| 139 | // Compare Alice and Bob |
| 140 | if (aliceLen != bobLen) { |
| 141 | throw new Exception("Alice and Bob have different lengths"); |
| 142 | } |
| 143 | for (int i=0; i<aliceLen; i++) { |
| 144 | if (aliceSharedSecret[i] != bobSharedSecret[i]) { |
| 145 | throw new Exception("Alice and Bob differ"); |
| 146 | } |
| 147 | } |
| 148 | System.err.println("Alice and Bob are the same"); |
| 149 | |
| 150 | // Compare Bob and Carol |
| 151 | if (bobLen != carolLen) { |
| 152 | throw new Exception("Bob and Carol have different lengths"); |
| 153 | } |
| 154 | for (int i=0; i<bobLen; i++) { |
| 155 | if (bobSharedSecret[i] != carolSharedSecret[i]) { |
| 156 | throw new Exception("Bob and Carol differ"); |
| 157 | } |
| 158 | } |
| 159 | System.err.println("Bob and Carol are the same"); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /* |
| 164 | * Converts a byte to hex digit and writes to the supplied buffer |
| 165 | */ |
| 166 | private void byte2hex(byte b, StringBuffer buf) { |
| 167 | char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', |
| 168 | '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 169 | int high = ((b & 0xf0) >> 4); |
| 170 | int low = (b & 0x0f); |
| 171 | buf.append(hexChars[high]); |
| 172 | buf.append(hexChars[low]); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Converts a byte array to hex string |
| 177 | */ |
| 178 | private String toHexString(byte[] block) { |
| 179 | StringBuffer buf = new StringBuffer(); |
| 180 | |
| 181 | int len = block.length; |
| 182 | |
| 183 | for (int i = 0; i < len; i++) { |
| 184 | byte2hex(block[i], buf); |
| 185 | if (i < len-1) { |
| 186 | buf.append(":"); |
| 187 | } |
| 188 | } |
| 189 | return buf.toString(); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Prints the usage of this test. |
| 194 | */ |
| 195 | private void usage() { |
| 196 | System.err.print("DHKeyAgreement usage: "); |
| 197 | System.err.println("[-gen]"); |
| 198 | } |
| 199 | |
| 200 | // The 1024 bit Diffie-Hellman modulus values used by SKIP |
| 201 | private static final byte skip1024ModulusBytes[] = { |
| 202 | (byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58, |
| 203 | (byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD, |
| 204 | (byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4, |
| 205 | (byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B, |
| 206 | (byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D, |
| 207 | (byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C, |
| 208 | (byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C, |
| 209 | (byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6, |
| 210 | (byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0, |
| 211 | (byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B, |
| 212 | (byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB, |
| 213 | (byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D, |
| 214 | (byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD, |
| 215 | (byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43, |
| 216 | (byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C, |
| 217 | (byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C, |
| 218 | (byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C, |
| 219 | (byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40, |
| 220 | (byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C, |
| 221 | (byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72, |
| 222 | (byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03, |
| 223 | (byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29, |
| 224 | (byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C, |
| 225 | (byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB, |
| 226 | (byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B, |
| 227 | (byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08, |
| 228 | (byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D, |
| 229 | (byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C, |
| 230 | (byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22, |
| 231 | (byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB, |
| 232 | (byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55, |
| 233 | (byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7 |
| 234 | }; |
| 235 | |
| 236 | // The SKIP 1024 bit modulus |
| 237 | private static final BigInteger skip1024Modulus |
| 238 | = new BigInteger(1, skip1024ModulusBytes); |
| 239 | |
| 240 | // The base used with the SKIP 1024 bit modulus |
| 241 | private static final BigInteger skip1024Base = BigInteger.valueOf(2); |
| 242 | } |