0N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4921804 6324825
0N/A * @summary Verify that DH works properly
0N/A * @author Andreas Sterbenz
0N/A * @library ..
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/A
0N/Aimport javax.crypto.*;
0N/A
0N/Apublic class TestDH extends PKCS11Test {
0N/A
0N/A public void main(Provider p) throws Exception {
0N/A if (p.getService("KeyAgreement", "DH") == null) {
0N/A System.out.println("DH not supported, skipping");
0N/A return;
0N/A }
0N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);
0N/A kpg.initialize(512);
0N/A KeyPair kp1 = kpg.generateKeyPair();
0N/A KeyPair kp2 = kpg.generateKeyPair();
0N/A
0N/A KeyAgreement ka1, ka2;
0N/A ka1 = KeyAgreement.getInstance("DH", p);
0N/A ka1.init(kp1.getPrivate());
0N/A ka1.doPhase(kp2.getPublic(), true);
0N/A System.out.println("Derive 1...");
0N/A byte[] secret1 = ka1.generateSecret();
0N/A
0N/A ka1.init(kp2.getPrivate());
0N/A ka1.doPhase(kp1.getPublic(), true);
0N/A System.out.println("Derive 2...");
0N/A byte[] secret2 = ka1.generateSecret();
0N/A
0N/A if (Arrays.equals(secret1, secret2) == false) {
0N/A throw new Exception("Secrets (1,2) do not match");
0N/A }
0N/A
0N/A ka2 = KeyAgreement.getInstance("DH", "SunJCE");
0N/A ka2.init(kp1.getPrivate());
0N/A ka2.doPhase(kp2.getPublic(), true);
0N/A System.out.println("Derive 3...");
0N/A byte[] secret3 = ka2.generateSecret();
0N/A
0N/A if (Arrays.equals(secret1, secret3) == false) {
0N/A throw new Exception("Secrets (1,3) do not match");
0N/A }
0N/A
0N/A ka2.init(kp2.getPrivate());
0N/A ka2.doPhase(kp1.getPublic(), true);
0N/A System.out.println("Derive 4...");
0N/A byte[] secret4 = ka2.generateSecret();
0N/A
0N/A if (Arrays.equals(secret1, secret4) == false) {
0N/A throw new Exception("Secrets (1,4) do not match");
0N/A }
0N/A
0N/A testAlgorithm(ka2, kp2, ka1, kp1, "DES");
0N/A testAlgorithm(ka2, kp2, ka1, kp1, "DESede");
0N/A// testAlgorithm(ka2, kp2, ka1, kp1, "AES");
0N/A// testAlgorithm(ka2, kp2, ka1, kp1, "RC4");
0N/A testAlgorithm(ka2, kp2, ka1, kp1, "Blowfish");
0N/A testAlgorithm(ka2, kp2, ka1, kp1, "TlsPremasterSecret");
0N/A }
0N/A
0N/A private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1, KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {
0N/A SecretKey key1 = null;
0N/A
0N/A ka1.init(kp1.getPrivate());
0N/A ka1.doPhase(kp2.getPublic(), true);
0N/A System.out.println("Derive " + algorithm + " using SunJCE...");
0N/A key1 = ka1.generateSecret(algorithm);
0N/A
0N/A ka2.init(kp1.getPrivate());
0N/A ka2.doPhase(kp2.getPublic(), true);
0N/A System.out.println("Derive " + algorithm + " using PKCS#11...");
0N/A SecretKey key2 = ka2.generateSecret(algorithm);
0N/A
0N/A byte[] b1 = key1.getEncoded();
0N/A byte[] b2 = key2.getEncoded();
0N/A
0N/A if (Arrays.equals(b1, b2) == false) {
0N/A System.out.println(b1.length + " bytes: " + toString(b1));
0N/A System.out.println(b2.length + " bytes: " + toString(b2));
0N/A throw new Exception(algorithm + " secret mismatch");
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A main(new TestDH());
0N/A }
0N/A
0N/A}