Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyAgreement/TestDH.java
38855 views
/*1* Copyright (c) 2003, 2017, 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 4921804 632482526* @summary Verify that DH works properly27* @author Andreas Sterbenz28* @library ..29* @run main/othervm -Djdk.crypto.KeyAgreement.legacyKDF=true TestDH30* @run main/othervm -Djdk.crypto.KeyAgreement.legacyKDF=true TestDH sm31*/3233import java.security.KeyPair;34import java.security.KeyPairGenerator;35import java.security.Provider;36import java.util.Arrays;37import javax.crypto.KeyAgreement;38import javax.crypto.SecretKey;3940public class TestDH extends PKCS11Test {4142@Override43public void main(Provider p) throws Exception {44if (p.getService("KeyAgreement", "DH") == null) {45System.out.println("DH not supported, skipping");46return;47}48KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);49kpg.initialize(512);50KeyPair kp1 = kpg.generateKeyPair();51KeyPair kp2 = kpg.generateKeyPair();5253KeyAgreement ka1, ka2;54ka1 = KeyAgreement.getInstance("DH", p);55ka1.init(kp1.getPrivate());56ka1.doPhase(kp2.getPublic(), true);57System.out.println("Derive 1...");58byte[] secret1 = ka1.generateSecret();5960ka1.init(kp2.getPrivate());61ka1.doPhase(kp1.getPublic(), true);62System.out.println("Derive 2...");63byte[] secret2 = ka1.generateSecret();6465if (Arrays.equals(secret1, secret2) == false) {66throw new Exception("Secrets (1,2) do not match");67}6869ka2 = KeyAgreement.getInstance("DH", "SunJCE");70ka2.init(kp1.getPrivate());71ka2.doPhase(kp2.getPublic(), true);72System.out.println("Derive 3...");73byte[] secret3 = ka2.generateSecret();7475if (Arrays.equals(secret1, secret3) == false) {76throw new Exception("Secrets (1,3) do not match");77}7879ka2.init(kp2.getPrivate());80ka2.doPhase(kp1.getPublic(), true);81System.out.println("Derive 4...");82byte[] secret4 = ka2.generateSecret();8384if (Arrays.equals(secret1, secret4) == false) {85throw new Exception("Secrets (1,4) do not match");86}8788testAlgorithm(ka2, kp2, ka1, kp1, "DES");89testAlgorithm(ka2, kp2, ka1, kp1, "DESede");90// testAlgorithm(ka2, kp2, ka1, kp1, "AES");91// testAlgorithm(ka2, kp2, ka1, kp1, "RC4");92testAlgorithm(ka2, kp2, ka1, kp1, "Blowfish");93testAlgorithm(ka2, kp2, ka1, kp1, "TlsPremasterSecret");94}9596private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1,97KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {98SecretKey key1;99100ka1.init(kp1.getPrivate());101ka1.doPhase(kp2.getPublic(), true);102System.out.println("Derive " + algorithm + " using SunJCE...");103key1 = ka1.generateSecret(algorithm);104105ka2.init(kp1.getPrivate());106ka2.doPhase(kp2.getPublic(), true);107System.out.println("Derive " + algorithm + " using PKCS#11...");108SecretKey key2 = ka2.generateSecret(algorithm);109110byte[] b1 = key1.getEncoded();111byte[] b2 = key2.getEncoded();112113if (Arrays.equals(b1, b2) == false) {114System.out.println(b1.length + " bytes: " + toString(b1));115System.out.println(b2.length + " bytes: " + toString(b2));116throw new Exception(algorithm + " secret mismatch");117}118}119120public static void main(String[] args) throws Exception {121main(new TestDH(), args);122}123124}125126127