Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyAgreement/TestShort.java
38855 views
/*1* Copyright (c) 2003, 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 4942494 714672826* @summary KAT test for DH (normal and with secret that has leading a 0x00 byte)27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestShort30* @run main/othervm TestShort sm31*/3233import java.math.BigInteger;34import java.security.KeyFactory;35import java.security.PrivateKey;36import java.security.Provider;37import java.security.PublicKey;38import java.util.Arrays;39import javax.crypto.KeyAgreement;40import javax.crypto.spec.DHPrivateKeySpec;41import javax.crypto.spec.DHPublicKeySpec;4243public class TestShort extends PKCS11Test {4445private final static BigInteger p = new BigInteger46("132323768951986124075479307182674357577285270296234088722451560397577130"47+ "29036368719146452186041204237350521785240337048752071462798273003935646"48+ "236777459223");4950private final static BigInteger g = new BigInteger51("542164405743647514160964848832570512804742839438047437683466730076610826"52+ "26139005426812890807137245973106730741193551360857959820973906708903671"53+ "85141189796");5455private final static BigInteger y1 = new BigInteger56("917822587297202019713917824657175324360828836418754472207798053179332700"57+ "39938196470323405362414543604756313574842317687108720161868374135893507"58+ "32549013008");5960private final static BigInteger x1 = new BigInteger61("44680539865608058021525420137770558786664900449");6263private final static BigInteger y2 = new BigInteger64("971516093764754129400636279042779828227876735997548759620533874940954728"65+ "96003923584532197641582422156725687657451980378160229472095259392582713"66+ "54693857368");6768private final static BigInteger x2 = new BigInteger69("433011588852527167500079509018272713204454720683");7071private final static byte[] s2 = parse72("00:19:c7:f1:bb:2e:3d:93:fa:02:d2:e9:9f:75:32:b9:e6:7a:a0:4a:10:45:81:d4:2b:"73+ "e2:77:4c:70:41:39:7c:19:fa:65:64:47:49:8a:ad:0a:fa:9d:e9:62:68:97:c5:52"74+ ":b1:37:03:d9:cd:aa:e1:bd:7e:71:0c:fc:15:a1:95");7576private final static BigInteger y3 = new BigInteger77("487191942830952492045314176949691887949505843590154039270855000076570641"78+ "84133173374554778014985281423493547105556633876312739488944445812738030"79+ "00691614787");8081private final static BigInteger x3 = new BigInteger82("1105612503769813327556221318510360767544481637404");8384private final static byte[] s3 = parse85("98:62:f3:e4:ff:2b:8d:8a:5a:20:fe:52:35:56:73:09:8e:b3:e2:cb:e2:45:e5:b7:"86+ "1a:6a:15:d8:a4:8c:0a:ce:f0:15:03:0c:c2:56:82:a2:75:9b:49:fe:ed:60:c5:6e"87+ ":de:47:55:62:4f:16:20:6d:74:cc:7b:95:93:25:2c:ea");8889@Override90public void main(Provider provider) throws Exception {91if (provider.getService("KeyAgreement", "DH") == null) {92System.out.println("DH not supported, skipping");93return;94}95try {96DHPublicKeySpec publicSpec;97DHPrivateKeySpec privateSpec;98KeyFactory kf = KeyFactory.getInstance("DH", provider);99KeyAgreement ka = KeyAgreement.getInstance("DH", provider);100101PrivateKey pr1 = kf.generatePrivate(new DHPrivateKeySpec(x1, p, g));102PublicKey pu2 = kf.generatePublic(new DHPublicKeySpec(y2, p, g));103PublicKey pu3 = kf.generatePublic(new DHPublicKeySpec(y3, p, g));104105ka.init(pr1);106ka.doPhase(pu2, true);107byte[] n2 = ka.generateSecret();108if (Arrays.equals(s2, n2) == false) {109throw new Exception("mismatch 2");110}111System.out.println("short ok");112113ka.init(pr1);114ka.doPhase(pu3, true);115byte[] n3 = ka.generateSecret();116if (Arrays.equals(s3, n3) == false) {117throw new Exception("mismatch 3");118}119System.out.println("normal ok");120} catch (Exception ex) {121System.out.println("Unexpected Exception: " + ex);122ex.printStackTrace();123throw ex;124}125126/*127KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", provider);128kpg.initialize(512);129// KeyPair kp1 = kpg.generateKeyPair();130// System.out.println(kp1.getPublic());131// System.out.println(kp1.getPrivate());132while (true) {133KeyAgreement ka = KeyAgreement.getInstance("DH", provider);134ka.init(pr1);135KeyPair kp2 = kpg.generateKeyPair();136ka.doPhase(kp2.getPublic(), true);137byte[] sec = ka.generateSecret();138if (sec.length == 64) {139System.out.println(kp2.getPrivate());140System.out.println(kp2.getPublic());141System.out.println(toString(sec));142break;143}144}145/**/146}147148public static void main(String[] args) throws Exception {149main(new TestShort(), args);150}151152}153154155