Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/KeyAgreement/DHGenSharedSecret.java
38867 views
/*1* Copyright (c) 1997, 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 DHGenSharedSecret27* @author Jan Luehe28*/29import java.security.*;30import java.security.spec.*;31import java.security.interfaces.*;32import javax.crypto.*;33import javax.crypto.spec.*;34import com.sun.crypto.provider.*;35import java.math.BigInteger;3637public class DHGenSharedSecret {3839static byte[] DHPrime = {40(byte)0x00, (byte)0x8D, (byte)0x8A, (byte)0x6C, (byte)0x7F, (byte)0xCC,41(byte)0xA5, (byte)0xBF, (byte)0x9C, (byte)0xE1, (byte)0xFA, (byte)0x3C,42(byte)0xCA, (byte)0x98, (byte)0xB7, (byte)0x99, (byte)0xD1, (byte)0xE5,43(byte)0x2C, (byte)0xC0, (byte)0x26, (byte)0x97, (byte)0x12, (byte)0x80,44(byte)0x12, (byte)0xEF, (byte)0x0B, (byte)0xDE, (byte)0x71, (byte)0x76,45(byte)0xAA, (byte)0x2D, (byte)0x86, (byte)0x41, (byte)0x0E, (byte)0x6A,46(byte)0xC2, (byte)0x12, (byte)0xAA, (byte)0xAA, (byte)0xE4, (byte)0x84,47(byte)0x80, (byte)0x13, (byte)0x95, (byte)0x06, (byte)0xC4, (byte)0x83,48(byte)0xB9, (byte)0xD3, (byte)0x72, (byte)0xC5, (byte)0xC8, (byte)0x85,49(byte)0x96, (byte)0x59, (byte)0x08, (byte)0xFA, (byte)0x9E, (byte)0x3C,50(byte)0xDC, (byte)0x92, (byte)0x28, (byte)0xC3, (byte)0x1D, (byte)0x6F,51(byte)0x44, (byte)0x36, (byte)0x70, (byte)0x40, (byte)0x80, (byte)0xF1,52(byte)0x3553};5455static byte[] DHBase = {56(byte)0x72, (byte)0x21, (byte)0xB3, (byte)0xA8, (byte)0x83, (byte)0xDD,57(byte)0x76, (byte)0xF5, (byte)0x0D, (byte)0x9B, (byte)0x81, (byte)0x11,58(byte)0x15, (byte)0x03, (byte)0x6D, (byte)0x4D, (byte)0x46, (byte)0x65,59(byte)0x30, (byte)0xB0, (byte)0xFA, (byte)0xFE, (byte)0xBE, (byte)0xA8,60(byte)0xD9, (byte)0x83, (byte)0x33, (byte)0x54, (byte)0xC7, (byte)0xF6,61(byte)0x81, (byte)0xAC, (byte)0xCC, (byte)0xA3, (byte)0xAE, (byte)0xAA,62(byte)0xC8, (byte)0x11, (byte)0x38, (byte)0xD4, (byte)0x4F, (byte)0xC4,63(byte)0x89, (byte)0xD3, (byte)0x72, (byte)0xEE, (byte)0x22, (byte)0x5A,64(byte)0x68, (byte)0xF7, (byte)0xAC, (byte)0x24, (byte)0x01, (byte)0x9B,65(byte)0xE9, (byte)0x08, (byte)0xFE, (byte)0x58, (byte)0x0A, (byte)0xCF,66(byte)0xB9, (byte)0x52, (byte)0xB4, (byte)0x02, (byte)0x73, (byte)0xA4,67(byte)0xA6, (byte)0xB9, (byte)0x0C, (byte)0x8D, (byte)0xA7, (byte)0xFB,68};6970public static void main(String[] args) throws Exception {71SunJCE jce = new SunJCE();72Security.addProvider(jce);73DHGenSharedSecret test = new DHGenSharedSecret();74test.run();75}7677public void run() throws Exception {78long start, end;7980BigInteger p = new BigInteger(1, DHPrime);81BigInteger g = new BigInteger(1, DHBase);82int l = 512;8384DHParameterSpec spec =85new DHParameterSpec(p, g, l);8687// generate keyPairs using parameters88KeyPairGenerator keyGen =89KeyPairGenerator.getInstance("DH", "SunJCE");90keyGen.initialize(spec);9192// Alice generates her key pairs93KeyPair keyA = keyGen.generateKeyPair();9495// Bob generates his key pairs96KeyPair keyB = keyGen.generateKeyPair();9798// Alice encodes her public key in x509 format99// , and sends it over to Bob.100byte[] alicePubKeyEnc = keyA.getPublic().getEncoded();101102// bob encodes his publicKey in x509 format and103// sends it over to Alice104byte[] bobPubKeyEnc = keyB.getPublic().getEncoded();105106// bob uses it to generate Secret107X509EncodedKeySpec x509Spec =108new X509EncodedKeySpec(alicePubKeyEnc);109KeyFactory bobKeyFac = KeyFactory.getInstance("DH", "SunJCE");110PublicKey alicePubKey = bobKeyFac.generatePublic(x509Spec);111112113KeyAgreement bobAlice = KeyAgreement.getInstance("DH", "SunJCE");114start = System.currentTimeMillis();115bobAlice.init(keyB.getPrivate());116bobAlice.doPhase(alicePubKey, true);117byte[] bobSecret = bobAlice.generateSecret();118end = System.currentTimeMillis();119120System.out.println("Time elapsed: " + (end - start));121System.out.println("Test Passed!");122}123}124125126