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