jdk/test/sun/security/pkcs11/KeyAgreement/TestDH.java
author tbell
Tue, 11 Nov 2008 22:01:08 -0800
changeset 1546 a02228c60567
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6764892: VS2008 changes required to compile hotspot sources Summary: Minor changes required to build using the Visual Studio 2008 compiler Reviewed-by: kvn, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4921804 6324825
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify that DH works properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class TestDH extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        if (p.getService("KeyAgreement", "DH") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            System.out.println("DH not supported, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        KeyPair kp1 = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        KeyPair kp2 = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        KeyAgreement ka1, ka2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ka1 = KeyAgreement.getInstance("DH", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        ka1.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        ka1.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.out.println("Derive 1...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        byte[] secret1 = ka1.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        ka1.init(kp2.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        ka1.doPhase(kp1.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        System.out.println("Derive 2...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        byte[] secret2 = ka1.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (Arrays.equals(secret1, secret2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new Exception("Secrets (1,2) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ka2 = KeyAgreement.getInstance("DH", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        ka2.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ka2.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.out.println("Derive 3...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        byte[] secret3 = ka2.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (Arrays.equals(secret1, secret3) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new Exception("Secrets (1,3) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ka2.init(kp2.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        ka2.doPhase(kp1.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.out.println("Derive 4...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        byte[] secret4 = ka2.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (Arrays.equals(secret1, secret4) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new Exception("Secrets (1,4) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        testAlgorithm(ka2, kp2, ka1, kp1, "DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        testAlgorithm(ka2, kp2, ka1, kp1, "DESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
//      testAlgorithm(ka2, kp2, ka1, kp1, "AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
//      testAlgorithm(ka2, kp2, ka1, kp1, "RC4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testAlgorithm(ka2, kp2, ka1, kp1, "Blowfish");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        testAlgorithm(ka2, kp2, ka1, kp1, "TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1, KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        SecretKey key1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ka1.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        ka1.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.out.println("Derive " + algorithm + " using SunJCE...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        key1 = ka1.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ka2.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        ka2.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        System.out.println("Derive " + algorithm + " using PKCS#11...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        SecretKey key2 = ka2.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        byte[] b1 = key1.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        byte[] b2 = key2.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (Arrays.equals(b1, b2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.out.println(b1.length + " bytes: " + toString(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println(b2.length + " bytes: " + toString(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new Exception(algorithm + " secret mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        main(new TestDH());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}