Path: blob/master/test/jdk/sun/security/pkcs11/KeyAgreement/TestDH.java
41155 views
/*1* Copyright (c) 2003, 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 4921804 632482526* @summary Verify that DH works properly27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm -Djdk.crypto.KeyAgreement.legacyKDF=true TestDH31* @run main/othervm -Djava.security.manager=allow -Djdk.crypto.KeyAgreement.legacyKDF=true TestDH sm32*/3334import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.Provider;37import java.util.Arrays;38import javax.crypto.KeyAgreement;39import javax.crypto.SecretKey;4041public class TestDH extends PKCS11Test {4243@Override44public void main(Provider p) throws Exception {45if (p.getService("KeyAgreement", "DH") == null) {46System.out.println("DH not supported, skipping");47return;48}49KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);50kpg.initialize(512);51KeyPair kp1 = kpg.generateKeyPair();52KeyPair kp2 = kpg.generateKeyPair();5354KeyAgreement ka1, ka2;55ka1 = KeyAgreement.getInstance("DH", p);56ka1.init(kp1.getPrivate());57ka1.doPhase(kp2.getPublic(), true);58System.out.println("Derive 1...");59byte[] secret1 = ka1.generateSecret();6061ka1.init(kp2.getPrivate());62ka1.doPhase(kp1.getPublic(), true);63System.out.println("Derive 2...");64byte[] secret2 = ka1.generateSecret();6566if (Arrays.equals(secret1, secret2) == false) {67throw new Exception("Secrets (1,2) do not match");68}6970ka2 = KeyAgreement.getInstance("DH", "SunJCE");71ka2.init(kp1.getPrivate());72ka2.doPhase(kp2.getPublic(), true);73System.out.println("Derive 3...");74byte[] secret3 = ka2.generateSecret();7576if (Arrays.equals(secret1, secret3) == false) {77throw new Exception("Secrets (1,3) do not match");78}7980ka2.init(kp2.getPrivate());81ka2.doPhase(kp1.getPublic(), true);82System.out.println("Derive 4...");83byte[] secret4 = ka2.generateSecret();8485if (Arrays.equals(secret1, secret4) == false) {86throw new Exception("Secrets (1,4) do not match");87}8889testAlgorithm(ka2, kp2, ka1, kp1, "DES");90testAlgorithm(ka2, kp2, ka1, kp1, "DESede");91// testAlgorithm(ka2, kp2, ka1, kp1, "AES");92// testAlgorithm(ka2, kp2, ka1, kp1, "RC4");93testAlgorithm(ka2, kp2, ka1, kp1, "Blowfish");94testAlgorithm(ka2, kp2, ka1, kp1, "TlsPremasterSecret");95}9697private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1,98KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {99SecretKey key1;100101ka1.init(kp1.getPrivate());102ka1.doPhase(kp2.getPublic(), true);103System.out.println("Derive " + algorithm + " using SunJCE...");104key1 = ka1.generateSecret(algorithm);105106ka2.init(kp1.getPrivate());107ka2.doPhase(kp2.getPublic(), true);108System.out.println("Derive " + algorithm + " using PKCS#11...");109SecretKey key2 = ka2.generateSecret(algorithm);110111byte[] b1 = key1.getEncoded();112byte[] b2 = key2.getEncoded();113114if (Arrays.equals(b1, b2) == false) {115System.out.println(b1.length + " bytes: " + toString(b1));116System.out.println(b2.length + " bytes: " + toString(b2));117throw new Exception(algorithm + " secret mismatch");118}119}120121public static void main(String[] args) throws Exception {122main(new TestDH(), args);123}124125}126127128