Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/KeyAgreement/DHKeyFactory.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 DHKeyFactory27* @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 creates a DH keypair, retrieves the encodings of the DH public and42* private key, and feeds those encodings to a DH key factory, which parses43* the encodings and creates a DH public and private key from them.44*/4546public class DHKeyFactory {4748private DHKeyFactory() {}4950public static void main(String argv[]) throws Exception {51// Add JCE to the list of providers52SunJCE jce = new SunJCE();53Security.addProvider(jce);5455DHKeyFactory test = new DHKeyFactory();56test.run();57System.out.println("Test Passed");58}5960private void run() throws Exception {6162DHParameterSpec dhSkipParamSpec;6364// use some pre-generated, default DH parameters65System.err.println("Using SKIP Diffie-Hellman parameters");66dhSkipParamSpec = new DHParameterSpec(skip1024Modulus,67skip1024Base);6869KeyPairGenerator kpgen = KeyPairGenerator.getInstance("DH");70kpgen.initialize(dhSkipParamSpec);71KeyPair kp = kpgen.generateKeyPair();7273// get the public key encoding74byte[] pubKeyEnc = kp.getPublic().getEncoded();7576// get the private key encoding77byte[] privKeyEnc = kp.getPrivate().getEncoded();7879KeyFactory kfac = KeyFactory.getInstance("DH");8081X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKeyEnc);82PublicKey pubKey = kfac.generatePublic(x509KeySpec);8384PKCS8EncodedKeySpec pkcsKeySpec = new PKCS8EncodedKeySpec(privKeyEnc);85PrivateKey privKey = kfac.generatePrivate(pkcsKeySpec);86}8788// The 1024 bit Diffie-Hellman modulus values used by SKIP89private static final byte skip1024ModulusBytes[] = {90(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,91(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,92(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,93(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,94(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,95(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,96(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,97(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,98(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,99(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,100(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,101(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,102(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,103(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,104(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,105(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,106(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,107(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,108(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,109(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,110(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,111(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,112(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,113(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,114(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,115(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,116(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,117(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,118(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,119(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,120(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,121(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7122};123124// The SKIP 1024 bit modulus125private static final BigInteger skip1024Modulus126= new BigInteger(1, skip1024ModulusBytes);127128// The base used with the SKIP 1024 bit modulus129private static final BigInteger skip1024Base = BigInteger.valueOf(2);130}131132133