jdk/test/sun/security/pkcs11/ec/TestECDH.java
author asmotrak
Tue, 26 Jan 2016 13:32:07 -0800
changeset 35379 1e8e336ef66b
parent 19067 5271291b7121
child 40975 680639c9b307
permissions -rw-r--r--
8144539: Update PKCS11 tests to run with security manager Reviewed-by: valeriep, ascarpino
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: 19067
diff changeset
     2
 * Copyright (c) 2006, 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
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 6405536
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic known answer test for ECDH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    30
 * @library ../../../../java/security/testlibrary
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    31
 * @run main/othervm TestECDH
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    32
 * @run main/othervm TestECDH sm policy
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: 19067
diff changeset
    35
import java.security.KeyFactory;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    36
import java.security.KeyPair;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    37
import java.security.KeyPairGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    38
import java.security.PrivateKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    39
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    40
import java.security.PublicKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.interfaces.ECPublicKey;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    42
import java.security.spec.PKCS8EncodedKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    43
import java.security.spec.X509EncodedKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    44
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    45
import javax.crypto.KeyAgreement;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class TestECDH extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final static String pub192a  = "30:49:30:13:06:07:2a:86:48:ce:3d:02:01:06:08:2a:86:48:ce:3d:03:01:01:03:32:00:04:bc:49:85:81:4d:d0:a4:ef:67:09:f1:9f:f5:ee:ff:4c:2f:0e:74:2c:a0:98:a8:69:79:9c:0c:3c:e8:99:f2:f2:3c:6f:48:bf:2a:ea:45:e9:76:be:1b:4a:45:0c:a2:99";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final static String priv192a = "30:39:02:01:00:30:13:06:07:2a:86:48:ce:3d:02:01:06:08:2a:86:48:ce:3d:03:01:01:04:1f:30:1d:02:01:01:04:18:50:9a:f1:fb:14:91:08:91:18:b9:46:7f:c3:ff:84:db:be:4c:70:89:41:5e:5a:f5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final static String pub192b  = "30:49:30:13:06:07:2a:86:48:ce:3d:02:01:06:08:2a:86:48:ce:3d:03:01:01:03:32:00:04:41:f3:1d:09:19:6e:dc:bf:6e:14:3a:b8:1a:40:44:ef:7b:51:fc:e1:9a:64:ac:46:47:ab:31:e2:1b:d3:76:d9:85:7a:b8:e6:95:f5:75:3f:13:7a:3a:88:02:57:de:8f";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final static String priv192b = "30:39:02:01:00:30:13:06:07:2a:86:48:ce:3d:02:01:06:08:2a:86:48:ce:3d:03:01:01:04:1f:30:1d:02:01:01:04:18:1d:8c:7d:64:1a:c1:ca:7d:59:d6:e7:11:61:e3:4d:d4:64:31:d9:76:17:a4:dd:6b";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final static String secret192 = "1f:48:aa:23:8e:6f:8a:70:87:af:3f:cd:53:f9:ae:85:41:1f:25:7e:b9:88:1f:6b";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final static String pub163a  = "30:40:30:10:06:07:2a:86:48:ce:3d:02:01:06:05:2b:81:04:00:0f:03:2c:00:04:04:81:99:2a:6d:53:e1:9a:31:4b:42:5b:01:41:bd:69:3f:73:63:f2:c5:02:70:25:7c:81:ce:6a:00:a0:fa:43:33:25:5b:ac:1f:66:82:1f:fa:63";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final static String priv163a = "30:33:02:01:00:30:10:06:07:2a:86:48:ce:3d:02:01:06:05:2b:81:04:00:0f:04:1c:30:1a:02:01:01:04:15:01:a0:2c:f6:24:bb:c8:2f:6e:f3:86:e2:24:bc:f1:01:ce:49:15:09:b9";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final static String pub163b  = "30:40:30:10:06:07:2a:86:48:ce:3d:02:01:06:05:2b:81:04:00:0f:03:2c:00:04:03:59:e7:69:a5:89:2f:28:ba:75:ac:bf:01:d5:ad:14:d8:f8:19:25:81:01:31:b3:e2:2d:f3:db:f1:d2:cd:fc:94:af:d2:1d:16:58:94:fe:d5:65";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final static String priv163b = "30:33:02:01:00:30:10:06:07:2a:86:48:ce:3d:02:01:06:05:2b:81:04:00:0f:04:1c:30:1a:02:01:01:04:15:02:4e:49:b1:8b:36:d8:71:22:81:06:8d:14:a9:4c:5c:7c:61:8b:e2:95";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static String secret163 = "04:ae:71:c1:c6:4d:f4:34:4d:72:70:a4:64:65:7f:2d:88:2d:3f:50:be";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    63
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (p.getService("KeyAgreement", "ECDH") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            System.out.println("Provider does not support ECDH, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    69
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 14342
diff changeset
    70
        if (isNSS(p) && getNSSECC() == ECCState.Basic) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 14342
diff changeset
    71
            System.out.println("NSS only supports Basic ECC.  Skipping..");
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 14342
diff changeset
    72
            return;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 14342
diff changeset
    73
        }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 14342
diff changeset
    74
13661
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    75
        /*
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    76
         * PKCS11Test.main will remove this provider if needed
7c894680910a 6995421: Eliminate the static dependency to sun.security.ec.ECKeyFactory
mullan
parents: 10328
diff changeset
    77
         */
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    78
        Providers.setAt(p, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            kpg.initialize(163);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.out.println(toString(kp.getPublic().getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.out.println(toString(kp.getPrivate().getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.out.println(toString(kp.getPublic().getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.out.println(toString(kp.getPrivate().getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        test(p, pub192a, priv192a, pub192b, priv192b, secret192);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        test(p, pub163a, priv163a, pub163b, priv163b, secret163);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("OK");
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: 19067
diff changeset
    98
    private final static void test(Provider p, String pub1s, String priv1s,
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
    99
            String pub2s, String priv2s, String secrets) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        KeyFactory kf = KeyFactory.getInstance("EC", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        PublicKey pub1 = kf.generatePublic(new X509EncodedKeySpec(parse(pub1s)));
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
   102
        System.out.println("Testing using parameters "
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
   103
                + ((ECPublicKey)pub1).getParams() + "...");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        PrivateKey priv1 = kf.generatePrivate(new PKCS8EncodedKeySpec(parse(priv1s)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        PublicKey pub2 = kf.generatePublic(new X509EncodedKeySpec(parse(pub2s)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        PrivateKey priv2 = kf.generatePrivate(new PKCS8EncodedKeySpec(parse(priv2s)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        byte[] secret = parse(secrets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ka1.init(priv1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        ka1.doPhase(pub2, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        byte[] s1 = ka1.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (Arrays.equals(secret, s1) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println("expected: " + toString(secret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println("actual:   " + toString(s1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new Exception("Secret 1 does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        ka2.init(priv2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        ka2.doPhase(pub1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        byte[] s2 = ka2.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (Arrays.equals(secret, s2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.out.println("expected: " + toString(secret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.out.println("actual:   " + toString(s2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new Exception("Secret 2 does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 19067
diff changeset
   132
        main(new TestECDH(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}