Path: blob/master/test/jdk/sun/security/pkcs11/KeyAgreement/TestInterop.java
41155 views
/*1* Copyright (c) 2012, 2018, 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 /test/lib ..28* @modules jdk.crypto.cryptoki29* @run main/othervm TestInterop30* @run main/othervm -Djava.security.manager=allow TestInterop sm31*/32import java.math.BigInteger;33import java.security.KeyFactory;34import java.security.PrivateKey;35import java.security.Provider;36import java.security.PublicKey;37import java.util.Arrays;38import javax.crypto.KeyAgreement;39import javax.crypto.spec.DHPrivateKeySpec;40import javax.crypto.spec.DHPublicKeySpec;4142public class TestInterop extends PKCS11Test {4344private final static BigInteger p = new BigInteger45("171718397966129586011229151993178480901904202533705695869569760169920539"46+ "80807543778874708672297590042574075430109846864794139516459381007417046"47+ "27996080624930219892858374168155487210358743785481212360509485282294161"48+ "39585571568998066586304075565145536350296006867635076744949977849997684"49+ "222020336013226588207303");5051private final static BigInteger g = new BigInteger("2");5253private final static BigInteger ya = new BigInteger54("687709211571508809414670982463565909269384277848448625781941269577397703"55+ "73675199968849153119146758339814638228795348558483510369322822476757204"56+ "22158455966026517829008713407587339322132253724742557954802911059639161"57+ "24827916158465757962384625410294483756242900146397201260757102085985457"58+ "09397033481077351036224");5960private final static BigInteger xa = new BigInteger61("104917367119952955556289227181599819745346393858545449202252025137706135"62+ "98100778613457655440586438263591136003106529323555991109623536177695714"63+ "66884181531401472902830508361532232717792847436112280721439936797741371"64+ "245140912614191507");6566private final static BigInteger yb = new BigInteger67("163887874871842952463100699681506173424091615364591742415764095471629919"68+ "08421025296419917755446931473037086355546823601999684501737493240373415"69+ "65608293667837249198973539289354492348897732633852665609611113031379864"70+ "58514616034107537409230452318065341748503347627733368519091332060477528"71+ "173423377887175351037810");7273private final static BigInteger xb = new BigInteger74("127757517533485947079959908591028646859165238853082197617179368337276371"75+ "51601819447716934542027725311863797141734616730248519214531856941516613"76+ "30313414180008978013330410484011186019824874948204261839391153650949864"77+ "429505597086564709");7879@Override80public void main(Provider prov) throws Exception {81if (prov.getService("KeyAgreement", "DH") == null) {82System.out.println("DH not supported, skipping");83return;84}85try {86System.out.println("testing generateSecret()");8788DHPublicKeySpec publicSpec;89DHPrivateKeySpec privateSpec;90KeyFactory kf = KeyFactory.getInstance("DH");91KeyAgreement ka = KeyAgreement.getInstance("DH", prov);92KeyAgreement kbSunJCE = KeyAgreement.getInstance("DH", "SunJCE");93DHPrivateKeySpec privSpecA = new DHPrivateKeySpec(xa, p, g);94DHPublicKeySpec pubSpecA = new DHPublicKeySpec(ya, p, g);95PrivateKey privA = kf.generatePrivate(privSpecA);96PublicKey pubA = kf.generatePublic(pubSpecA);9798DHPrivateKeySpec privSpecB = new DHPrivateKeySpec(xb, p, g);99DHPublicKeySpec pubSpecB = new DHPublicKeySpec(yb, p, g);100PrivateKey privB = kf.generatePrivate(privSpecB);101PublicKey pubB = kf.generatePublic(pubSpecB);102103ka.init(privA);104ka.doPhase(pubB, true);105byte[] n1 = ka.generateSecret();106107kbSunJCE.init(privB);108kbSunJCE.doPhase(pubA, true);109byte[] n2 = kbSunJCE.generateSecret();110111if (Arrays.equals(n1, n2) == false) {112throw new Exception("values mismatch!");113} else {114System.out.println("values: same");115}116117System.out.println("testing generateSecret(byte[], int)");118byte[] n3 = new byte[n1.length];119ka.init(privB);120ka.doPhase(pubA, true);121int n3Len = ka.generateSecret(n3, 0);122if (n3Len != n3.length) {123throw new Exception("PKCS11 Length mismatch!");124} else System.out.println("PKCS11 Length: ok");125byte[] n4 = new byte[n2.length];126kbSunJCE.init(privA);127kbSunJCE.doPhase(pubB, true);128int n4Len = kbSunJCE.generateSecret(n4, 0);129if (n4Len != n4.length) {130throw new Exception("SunJCE Length mismatch!");131} else System.out.println("SunJCE Length: ok");132133if (Arrays.equals(n3, n4) == false) {134throw new Exception("values mismatch! ");135} else {136System.out.println("values: same");137}138} catch (Exception ex) {139System.out.println("Unexpected ex: " + ex);140ex.printStackTrace();141throw ex;142}143}144145public static void main(String[] args) throws Exception {146main(new TestInterop(), args);147}148}149150151