TestCurves.java revision 16823:0c12834e44c2
1/*
2 * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 6405536 6414980
27 * @summary Basic consistency test for all curves using ECDSA and ECDH
28 * @author Andreas Sterbenz
29 * @library ..
30 * @modules jdk.crypto.cryptoki/sun.security.pkcs11.wrapper
31 * @run main/othervm TestCurves
32 * @run main/othervm TestCurves sm
33 * @key randomness
34 */
35
36import java.security.KeyPair;
37import java.security.KeyPairGenerator;
38import java.security.Provider;
39import java.security.ProviderException;
40import java.security.Signature;
41import java.security.spec.ECParameterSpec;
42import java.util.Arrays;
43import java.util.List;
44import java.util.Random;
45import javax.crypto.KeyAgreement;
46
47public class TestCurves extends PKCS11Test {
48
49    public static void main(String[] args) throws Exception {
50        main(new TestCurves(), args);
51    }
52
53    @Override
54    public void main(Provider p) throws Exception {
55        if (p.getService("KeyAgreement", "ECDH") == null) {
56            System.out.println("Not supported by provider, skipping");
57            return;
58        }
59
60        if (isBadNSSVersion(p)) {
61            return;
62        }
63
64        if (isBadSolarisSparc(p)) {
65            return;
66        }
67
68        // Check if this is sparc for later failure avoidance.
69        boolean sparc = false;
70        if (props.getProperty("os.arch").equals("sparcv9")) {
71            sparc = true;
72            System.out.println("This is a sparcv9");
73        }
74
75        Random random = new Random();
76        byte[] data = new byte[2048];
77        random.nextBytes(data);
78
79        List<ECParameterSpec> curves = getKnownCurves(p);
80        for (ECParameterSpec params : curves) {
81            System.out.println("Testing " + params + "...");
82            KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
83            kpg.initialize(params);
84            KeyPair kp1, kp2;
85
86            kp1 = kpg.generateKeyPair();
87            kp2 = kpg.generateKeyPair();
88
89            testSigning(p, "SHA1withECDSA", data, kp1, kp2);
90            // Check because Solaris ncp driver does not support these but
91            // Solaris metaslot causes them to be run.
92            try {
93                testSigning(p, "SHA224withECDSA", data, kp1, kp2);
94                testSigning(p, "SHA256withECDSA", data, kp1, kp2);
95                testSigning(p, "SHA384withECDSA", data, kp1, kp2);
96                testSigning(p, "SHA512withECDSA", data, kp1, kp2);
97            } catch (ProviderException e) {
98                if (sparc) {
99                    Throwable t = e.getCause();
100                    if (t instanceof sun.security.pkcs11.wrapper.PKCS11Exception &&
101                        t.getMessage().equals("CKR_ATTRIBUTE_VALUE_INVALID")) {
102                        System.out.print("-Failure not uncommon.  Probably pre-T4.");
103                    } else {
104                        throw e;
105                    }
106                } else {
107                    throw e;
108                }
109            }
110            System.out.println();
111
112            KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);
113            ka1.init(kp1.getPrivate());
114            ka1.doPhase(kp2.getPublic(), true);
115            byte[] secret1 = ka1.generateSecret();
116
117            KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);
118            ka2.init(kp2.getPrivate());
119            ka2.doPhase(kp1.getPublic(), true);
120            byte[] secret2 = ka2.generateSecret();
121
122            if (Arrays.equals(secret1, secret2) == false) {
123                throw new Exception("Secrets do not match");
124            }
125        }
126
127        System.out.println("OK");
128    }
129
130    private static void testSigning(Provider p, String algorithm,
131            byte[] data, KeyPair kp1, KeyPair kp2) throws Exception {
132        System.out.print("  " + algorithm);
133        Signature s = Signature.getInstance(algorithm, p);
134        s.initSign(kp1.getPrivate());
135        s.update(data);
136        byte[] sig = s.sign();
137
138        s = Signature.getInstance(algorithm, p);
139        s.initVerify(kp1.getPublic());
140        s.update(data);
141        boolean r = s.verify(sig);
142        if (r == false) {
143            throw new Exception("Signature did not verify");
144        }
145
146        s.initVerify(kp2.getPublic());
147        s.update(data);
148        r = s.verify(sig);
149        if (r) {
150            throw new Exception("Signature should not verify");
151        }
152    }
153}
154