jdk/test/sun/security/pkcs11/KeyAgreement/TestDH.java
author mchung
Wed, 14 Dec 2016 10:51:13 -0800
changeset 42693 6645de32a866
parent 40975 680639c9b307
child 43248 5e15de85a1a0
permissions -rw-r--r--
8171201: Drop java.compact$N aggregator modules 8171202: Rename jdk.crypto.pkcs11 and jdk.pack200 to end with Java letters Reviewed-by: alanb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    24
/*
2
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 ..
42693
6645de32a866 8171201: Drop java.compact$N aggregator modules
mchung
parents: 40975
diff changeset
    30
 * @modules jdk.crypto.token
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    31
 * @run main/othervm TestDH
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    32
 * @run main/othervm TestDH sm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    35
import java.security.KeyPair;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    36
import java.security.KeyPairGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    37
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    38
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    39
import javax.crypto.KeyAgreement;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    40
import javax.crypto.SecretKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class TestDH extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    44
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (p.getService("KeyAgreement", "DH") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            System.out.println("DH not supported, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        KeyPair kp1 = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        KeyPair kp2 = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        KeyAgreement ka1, ka2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ka1 = KeyAgreement.getInstance("DH", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        ka1.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        ka1.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.out.println("Derive 1...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        byte[] secret1 = ka1.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ka1.init(kp2.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        ka1.doPhase(kp1.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.out.println("Derive 2...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        byte[] secret2 = ka1.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (Arrays.equals(secret1, secret2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new Exception("Secrets (1,2) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ka2 = KeyAgreement.getInstance("DH", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ka2.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        ka2.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        System.out.println("Derive 3...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        byte[] secret3 = ka2.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (Arrays.equals(secret1, secret3) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new Exception("Secrets (1,3) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ka2.init(kp2.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ka2.doPhase(kp1.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.out.println("Derive 4...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        byte[] secret4 = ka2.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (Arrays.equals(secret1, secret4) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new Exception("Secrets (1,4) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testAlgorithm(ka2, kp2, ka1, kp1, "DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        testAlgorithm(ka2, kp2, ka1, kp1, "DESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
//      testAlgorithm(ka2, kp2, ka1, kp1, "AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
//      testAlgorithm(ka2, kp2, ka1, kp1, "RC4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        testAlgorithm(ka2, kp2, ka1, kp1, "Blowfish");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        testAlgorithm(ka2, kp2, ka1, kp1, "TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    98
    private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1,
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    99
            KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
   100
        SecretKey key1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ka1.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        ka1.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        System.out.println("Derive " + algorithm + " using SunJCE...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        key1 = ka1.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ka2.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ka2.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        System.out.println("Derive " + algorithm + " using PKCS#11...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        SecretKey key2 = ka2.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        byte[] b1 = key1.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        byte[] b2 = key2.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (Arrays.equals(b1, b2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println(b1.length + " bytes: " + toString(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println(b2.length + " bytes: " + toString(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new Exception(algorithm + " secret mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
   123
        main(new TestDH(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}