Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyAgreement/TestInterop.java
38855 views
/*1* Copyright (c) 2012, 2016, 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 714672826* @summary Interop test for DH with secret that has a leading 0x00 byte27* @library ..28* @run main/othervm TestInterop29* @run main/othervm TestInterop sm30*/31import java.math.BigInteger;32import java.security.KeyFactory;33import java.security.PrivateKey;34import java.security.Provider;35import java.security.PublicKey;36import java.util.Arrays;37import javax.crypto.KeyAgreement;38import javax.crypto.spec.DHPrivateKeySpec;39import javax.crypto.spec.DHPublicKeySpec;4041public class TestInterop extends PKCS11Test {4243private final static BigInteger p = new BigInteger44("171718397966129586011229151993178480901904202533705695869569760169920539"45+ "80807543778874708672297590042574075430109846864794139516459381007417046"46+ "27996080624930219892858374168155487210358743785481212360509485282294161"47+ "39585571568998066586304075565145536350296006867635076744949977849997684"48+ "222020336013226588207303");4950private final static BigInteger g = new BigInteger("2");5152private final static BigInteger ya = new BigInteger53("687709211571508809414670982463565909269384277848448625781941269577397703"54+ "73675199968849153119146758339814638228795348558483510369322822476757204"55+ "22158455966026517829008713407587339322132253724742557954802911059639161"56+ "24827916158465757962384625410294483756242900146397201260757102085985457"57+ "09397033481077351036224");5859private final static BigInteger xa = new BigInteger60("104917367119952955556289227181599819745346393858545449202252025137706135"61+ "98100778613457655440586438263591136003106529323555991109623536177695714"62+ "66884181531401472902830508361532232717792847436112280721439936797741371"63+ "245140912614191507");6465private final static BigInteger yb = new BigInteger66("163887874871842952463100699681506173424091615364591742415764095471629919"67+ "08421025296419917755446931473037086355546823601999684501737493240373415"68+ "65608293667837249198973539289354492348897732633852665609611113031379864"69+ "58514616034107537409230452318065341748503347627733368519091332060477528"70+ "173423377887175351037810");7172private final static BigInteger xb = new BigInteger73("127757517533485947079959908591028646859165238853082197617179368337276371"74+ "51601819447716934542027725311863797141734616730248519214531856941516613"75+ "30313414180008978013330410484011186019824874948204261839391153650949864"76+ "429505597086564709");7778@Override79public void main(Provider prov) throws Exception {80if (prov.getService("KeyAgreement", "DH") == null) {81System.out.println("DH not supported, skipping");82return;83}84try {85System.out.println("testing generateSecret()");8687DHPublicKeySpec publicSpec;88DHPrivateKeySpec privateSpec;89KeyFactory kf = KeyFactory.getInstance("DH");90KeyAgreement ka = KeyAgreement.getInstance("DH", prov);91KeyAgreement kbSunJCE = KeyAgreement.getInstance("DH", "SunJCE");92DHPrivateKeySpec privSpecA = new DHPrivateKeySpec(xa, p, g);93DHPublicKeySpec pubSpecA = new DHPublicKeySpec(ya, p, g);94PrivateKey privA = kf.generatePrivate(privSpecA);95PublicKey pubA = kf.generatePublic(pubSpecA);9697DHPrivateKeySpec privSpecB = new DHPrivateKeySpec(xb, p, g);98DHPublicKeySpec pubSpecB = new DHPublicKeySpec(yb, p, g);99PrivateKey privB = kf.generatePrivate(privSpecB);100PublicKey pubB = kf.generatePublic(pubSpecB);101102ka.init(privA);103ka.doPhase(pubB, true);104byte[] n1 = ka.generateSecret();105106kbSunJCE.init(privB);107kbSunJCE.doPhase(pubA, true);108byte[] n2 = kbSunJCE.generateSecret();109110if (Arrays.equals(n1, n2) == false) {111throw new Exception("values mismatch!");112} else {113System.out.println("values: same");114}115116System.out.println("testing generateSecret(byte[], int)");117byte[] n3 = new byte[n1.length];118ka.init(privB);119ka.doPhase(pubA, true);120int n3Len = ka.generateSecret(n3, 0);121if (n3Len != n3.length) {122throw new Exception("PKCS11 Length mismatch!");123} else System.out.println("PKCS11 Length: ok");124byte[] n4 = new byte[n2.length];125kbSunJCE.init(privA);126kbSunJCE.doPhase(pubB, true);127int n4Len = kbSunJCE.generateSecret(n4, 0);128if (n4Len != n4.length) {129throw new Exception("SunJCE Length mismatch!");130} else System.out.println("SunJCE Length: ok");131132if (Arrays.equals(n3, n4) == false) {133throw new Exception("values mismatch! ");134} else {135System.out.println("values: same");136}137} catch (Exception ex) {138System.out.println("Unexpected ex: " + ex);139ex.printStackTrace();140throw ex;141}142}143144public static void main(String[] args) throws Exception {145main(new TestInterop(), args);146}147}148149150