Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/KeyAgreement/DHKeyAgreement3.java
38867 views
/*1* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 000000026* @summary DHKeyAgreement327* @author Jan Luehe28*/2930import java.io.*;31import java.math.BigInteger;32import java.security.*;33import java.security.spec.*;34import java.security.interfaces.*;35import javax.crypto.*;36import javax.crypto.spec.*;37import javax.crypto.interfaces.*;38import com.sun.crypto.provider.SunJCE;3940/**41* This test utility executes the Diffie-Hellman key agreement protocol42* between 3 parties: Alice, Bob, and Carol.43*44* We use the same 1024 bit prime modulus and base generator that are used by45* SKIP.46*/4748public class DHKeyAgreement3 {4950private DHKeyAgreement3() {}5152public static void main(String argv[]) throws Exception {53// Add JCE to the list of providers54SunJCE jce = new SunJCE();55Security.addProvider(jce);5657DHKeyAgreement3 keyAgree = new DHKeyAgreement3();58keyAgree.run();59System.out.println("Test Passed");60}6162private void run() throws Exception {6364DHParameterSpec dhSkipParamSpec;6566System.err.println("Using SKIP Diffie-Hellman parameters");67dhSkipParamSpec = new DHParameterSpec(skip1024Modulus, skip1024Base);6869// Alice creates her own DH key pair70System.err.println("ALICE: Generate DH keypair ...");71KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");72aliceKpairGen.initialize(dhSkipParamSpec);73KeyPair aliceKpair = aliceKpairGen.generateKeyPair();7475// Bob creates his own DH key pair76System.err.println("BOB: Generate DH keypair ...");77KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH");78bobKpairGen.initialize(dhSkipParamSpec);79KeyPair bobKpair = bobKpairGen.generateKeyPair();8081// Carol creates her own DH key pair82System.err.println("CAROL: Generate DH keypair ...");83KeyPairGenerator carolKpairGen = KeyPairGenerator.getInstance("DH");84carolKpairGen.initialize(dhSkipParamSpec);85KeyPair carolKpair = carolKpairGen.generateKeyPair();868788// Alice initialize89System.err.println("ALICE: Initialize ...");90KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");91aliceKeyAgree.init(aliceKpair.getPrivate());9293// Bob initialize94System.err.println("BOB: Initialize ...");95KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");96bobKeyAgree.init(bobKpair.getPrivate());9798// Carol initialize99System.err.println("CAROL: Initialize ...");100KeyAgreement carolKeyAgree = KeyAgreement.getInstance("DH");101carolKeyAgree.init(carolKpair.getPrivate());102103104// Alice uses Carol's public key105Key ac = aliceKeyAgree.doPhase(carolKpair.getPublic(), false);106107// Bob uses Alice's public key108Key ba = bobKeyAgree.doPhase(aliceKpair.getPublic(), false);109110// Carol uses Bob's public key111Key cb = carolKeyAgree.doPhase(bobKpair.getPublic(), false);112113114// Alice uses Carol's result from above115aliceKeyAgree.doPhase(cb, true);116117// Bob uses Alice's result from above118bobKeyAgree.doPhase(ac, true);119120// Carol uses Bob's result from above121carolKeyAgree.doPhase(ba, true);122123124// Alice, Bob and Carol compute their secrets125byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();126int aliceLen = aliceSharedSecret.length;127System.out.println("Alice secret: " + toHexString(aliceSharedSecret));128129byte[] bobSharedSecret = bobKeyAgree.generateSecret();130int bobLen = bobSharedSecret.length;131System.out.println("Bob secret: " + toHexString(bobSharedSecret));132133byte[] carolSharedSecret = carolKeyAgree.generateSecret();134int carolLen = carolSharedSecret.length;135System.out.println("Carol secret: " + toHexString(carolSharedSecret));136137138// Compare Alice and Bob139if (aliceLen != bobLen) {140throw new Exception("Alice and Bob have different lengths");141}142for (int i=0; i<aliceLen; i++) {143if (aliceSharedSecret[i] != bobSharedSecret[i]) {144throw new Exception("Alice and Bob differ");145}146}147System.err.println("Alice and Bob are the same");148149// Compare Bob and Carol150if (bobLen != carolLen) {151throw new Exception("Bob and Carol have different lengths");152}153for (int i=0; i<bobLen; i++) {154if (bobSharedSecret[i] != carolSharedSecret[i]) {155throw new Exception("Bob and Carol differ");156}157}158System.err.println("Bob and Carol are the same");159}160161162/*163* Converts a byte to hex digit and writes to the supplied buffer164*/165private void byte2hex(byte b, StringBuffer buf) {166char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',167'9', 'A', 'B', 'C', 'D', 'E', 'F' };168int high = ((b & 0xf0) >> 4);169int low = (b & 0x0f);170buf.append(hexChars[high]);171buf.append(hexChars[low]);172}173174/*175* Converts a byte array to hex string176*/177private String toHexString(byte[] block) {178StringBuffer buf = new StringBuffer();179180int len = block.length;181182for (int i = 0; i < len; i++) {183byte2hex(block[i], buf);184if (i < len-1) {185buf.append(":");186}187}188return buf.toString();189}190191/*192* Prints the usage of this test.193*/194private void usage() {195System.err.print("DHKeyAgreement usage: ");196System.err.println("[-gen]");197}198199// The 1024 bit Diffie-Hellman modulus values used by SKIP200private static final byte skip1024ModulusBytes[] = {201(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,202(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,203(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,204(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,205(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,206(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,207(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,208(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,209(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,210(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,211(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,212(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,213(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,214(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,215(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,216(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,217(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,218(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,219(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,220(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,221(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,222(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,223(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,224(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,225(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,226(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,227(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,228(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,229(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,230(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,231(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,232(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7233};234235// The SKIP 1024 bit modulus236private static final BigInteger skip1024Modulus237= new BigInteger(1, skip1024ModulusBytes);238239// The base used with the SKIP 1024 bit modulus240private static final BigInteger skip1024Base = BigInteger.valueOf(2);241}242243244