Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/DHKeyAgreement3.java
41155 views
/*1* Copyright (c) 1998, 2021, 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 java.util.HexFormat;36import javax.crypto.*;37import javax.crypto.spec.*;38import javax.crypto.interfaces.*;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 {4950// Hex formatter to upper case with ":" delimiter51private static final HexFormat HEX_FORMATTER = HexFormat.ofDelimiter(":").withUpperCase();5253private DHKeyAgreement3() {}5455public static void main(String argv[]) throws Exception {56DHKeyAgreement3 keyAgree = new DHKeyAgreement3();57keyAgree.run();58System.out.println("Test Passed");59}6061private void run() throws Exception {6263DHParameterSpec dhSkipParamSpec;6465System.err.println("Using SKIP Diffie-Hellman parameters");66dhSkipParamSpec = new DHParameterSpec(skip1024Modulus, skip1024Base);6768// Alice creates her own DH key pair69System.err.println("ALICE: Generate DH keypair ...");70KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH", "SunJCE");71aliceKpairGen.initialize(dhSkipParamSpec);72KeyPair aliceKpair = aliceKpairGen.generateKeyPair();7374// Bob creates his own DH key pair75System.err.println("BOB: Generate DH keypair ...");76KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH", "SunJCE");77bobKpairGen.initialize(dhSkipParamSpec);78KeyPair bobKpair = bobKpairGen.generateKeyPair();7980// Carol creates her own DH key pair81System.err.println("CAROL: Generate DH keypair ...");82KeyPairGenerator carolKpairGen = KeyPairGenerator.getInstance("DH", "SunJCE");83carolKpairGen.initialize(dhSkipParamSpec);84KeyPair carolKpair = carolKpairGen.generateKeyPair();858687// Alice initialize88System.err.println("ALICE: Initialize ...");89KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH", "SunJCE");90aliceKeyAgree.init(aliceKpair.getPrivate());9192// Bob initialize93System.err.println("BOB: Initialize ...");94KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH", "SunJCE");95bobKeyAgree.init(bobKpair.getPrivate());9697// Carol initialize98System.err.println("CAROL: Initialize ...");99KeyAgreement carolKeyAgree = KeyAgreement.getInstance("DH", "SunJCE");100carolKeyAgree.init(carolKpair.getPrivate());101102103// Alice uses Carol's public key104Key ac = aliceKeyAgree.doPhase(carolKpair.getPublic(), false);105106// Bob uses Alice's public key107Key ba = bobKeyAgree.doPhase(aliceKpair.getPublic(), false);108109// Carol uses Bob's public key110Key cb = carolKeyAgree.doPhase(bobKpair.getPublic(), false);111112113// Alice uses Carol's result from above114aliceKeyAgree.doPhase(cb, true);115116// Bob uses Alice's result from above117bobKeyAgree.doPhase(ac, true);118119// Carol uses Bob's result from above120carolKeyAgree.doPhase(ba, true);121122123// Alice, Bob and Carol compute their secrets124byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();125int aliceLen = aliceSharedSecret.length;126System.out.println("Alice secret: " + HEX_FORMATTER.formatHex(aliceSharedSecret));127128byte[] bobSharedSecret = bobKeyAgree.generateSecret();129int bobLen = bobSharedSecret.length;130System.out.println("Bob secret: " + HEX_FORMATTER.formatHex(bobSharedSecret));131132byte[] carolSharedSecret = carolKeyAgree.generateSecret();133int carolLen = carolSharedSecret.length;134System.out.println("Carol secret: " + HEX_FORMATTER.formatHex(carolSharedSecret));135136137// Compare Alice and Bob138if (aliceLen != bobLen) {139throw new Exception("Alice and Bob have different lengths");140}141for (int i=0; i<aliceLen; i++) {142if (aliceSharedSecret[i] != bobSharedSecret[i]) {143throw new Exception("Alice and Bob differ");144}145}146System.err.println("Alice and Bob are the same");147148// Compare Bob and Carol149if (bobLen != carolLen) {150throw new Exception("Bob and Carol have different lengths");151}152for (int i=0; i<bobLen; i++) {153if (bobSharedSecret[i] != carolSharedSecret[i]) {154throw new Exception("Bob and Carol differ");155}156}157System.err.println("Bob and Carol are the same");158}159160161/*162* Converts a byte to hex digit and writes to the supplied buffer163*/164private void byte2hex(byte b, StringBuffer buf) {165char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',166'9', 'A', 'B', 'C', 'D', 'E', 'F' };167int high = ((b & 0xf0) >> 4);168int low = (b & 0x0f);169buf.append(hexChars[high]);170buf.append(hexChars[low]);171}172173/*174* Prints the usage of this test.175*/176private void usage() {177System.err.print("DHKeyAgreement usage: ");178System.err.println("[-gen]");179}180181// The 1024 bit Diffie-Hellman modulus values used by SKIP182private static final byte skip1024ModulusBytes[] = {183(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,184(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,185(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,186(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,187(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,188(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,189(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,190(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,191(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,192(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,193(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,194(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,195(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,196(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,197(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,198(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,199(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,200(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,201(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,202(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,203(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,204(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,205(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,206(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,207(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,208(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,209(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,210(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,211(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,212(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,213(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,214(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7215};216217// The SKIP 1024 bit modulus218private static final BigInteger skip1024Modulus219= new BigInteger(1, skip1024ModulusBytes);220221// The base used with the SKIP 1024 bit modulus222private static final BigInteger skip1024Base = BigInteger.valueOf(2);223}224225226