2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
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 |
* @compile -XDignore.symbol.file TestCurves.java
|
|
31 |
* @run main TestCurves
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.util.*;
|
|
35 |
|
|
36 |
import java.security.*;
|
|
37 |
import java.security.spec.*;
|
|
38 |
|
|
39 |
import javax.crypto.*;
|
|
40 |
|
|
41 |
// XXX no public API to enumerate supported named curves
|
|
42 |
import sun.security.ec.NamedCurve;
|
|
43 |
|
|
44 |
public class TestCurves extends PKCS11Test {
|
|
45 |
|
|
46 |
public static void main(String[] args) throws Exception {
|
|
47 |
main(new TestCurves());
|
|
48 |
}
|
|
49 |
|
|
50 |
public void main(Provider p) throws Exception {
|
|
51 |
if (p.getService("KeyAgreement", "ECDH") == null) {
|
|
52 |
System.out.println("Not supported by provider, skipping");
|
|
53 |
return;
|
|
54 |
}
|
|
55 |
|
|
56 |
Random random = new Random();
|
|
57 |
byte[] data = new byte[2048];
|
|
58 |
random.nextBytes(data);
|
|
59 |
|
|
60 |
Collection<? extends ECParameterSpec> curves =
|
|
61 |
NamedCurve.knownECParameterSpecs();
|
|
62 |
for (ECParameterSpec params : curves) {
|
|
63 |
System.out.println("Testing " + params + "...");
|
|
64 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
|
|
65 |
kpg.initialize(params);
|
|
66 |
KeyPair kp1, kp2;
|
|
67 |
kp1 = kpg.generateKeyPair();
|
|
68 |
kp2 = kpg.generateKeyPair();
|
|
69 |
|
|
70 |
testSigning(p, "SHA1withECDSA", data, kp1, kp2);
|
|
71 |
testSigning(p, "SHA256withECDSA", data, kp1, kp2);
|
|
72 |
testSigning(p, "SHA384withECDSA", data, kp1, kp2);
|
|
73 |
testSigning(p, "SHA512withECDSA", data, kp1, kp2);
|
|
74 |
// System.out.println();
|
|
75 |
|
|
76 |
KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);
|
|
77 |
ka1.init(kp1.getPrivate());
|
|
78 |
ka1.doPhase(kp2.getPublic(), true);
|
|
79 |
byte[] secret1 = ka1.generateSecret();
|
|
80 |
|
|
81 |
KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);
|
|
82 |
ka2.init(kp2.getPrivate());
|
|
83 |
ka2.doPhase(kp1.getPublic(), true);
|
|
84 |
byte[] secret2 = ka2.generateSecret();
|
|
85 |
|
|
86 |
if (Arrays.equals(secret1, secret2) == false) {
|
|
87 |
throw new Exception("Secrets do not match");
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
System.out.println("OK");
|
|
92 |
}
|
|
93 |
|
|
94 |
private static void testSigning(Provider p, String algorithm,
|
|
95 |
byte[] data, KeyPair kp1, KeyPair kp2) throws Exception {
|
|
96 |
// System.out.print(" " + algorithm);
|
|
97 |
Signature s = Signature.getInstance(algorithm, p);
|
|
98 |
s.initSign(kp1.getPrivate());
|
|
99 |
s.update(data);
|
|
100 |
byte[] sig = s.sign();
|
|
101 |
|
|
102 |
s = Signature.getInstance(algorithm, p);
|
|
103 |
s.initVerify(kp1.getPublic());
|
|
104 |
s.update(data);
|
|
105 |
boolean r = s.verify(sig);
|
|
106 |
if (r == false) {
|
|
107 |
throw new Exception("Signature did not verify");
|
|
108 |
}
|
|
109 |
|
|
110 |
s.initVerify(kp2.getPublic());
|
|
111 |
s.update(data);
|
|
112 |
r = s.verify(sig);
|
|
113 |
if (r) {
|
|
114 |
throw new Exception("Signature should not verify");
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
|
|
119 |
}
|