Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/TestECDSA2.java
38855 views
1
/*
2
* Copyright (c) 2012, 2016, 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
27
* @summary basic test of ECDSA signatures for P-256 and P-384 from the
28
* example data in "Suite B Implementer's Guide to FIPS 186-3".
29
* @library ..
30
* @library ../../../../java/security/testlibrary
31
* @compile -XDignore.symbol.file TestECDSA2.java
32
* @run main/othervm TestECDSA2
33
* @run main/othervm TestECDSA2 sm
34
*/
35
36
import java.math.BigInteger;
37
import java.security.AlgorithmParameters;
38
import java.security.KeyFactory;
39
import java.security.KeyPair;
40
import java.security.PrivateKey;
41
import java.security.Provider;
42
import java.security.PublicKey;
43
import java.security.Signature;
44
import java.security.spec.ECGenParameterSpec;
45
import java.security.spec.ECParameterSpec;
46
import java.security.spec.ECPoint;
47
import java.security.spec.ECPrivateKeySpec;
48
import java.security.spec.ECPublicKeySpec;
49
50
public class TestECDSA2 extends PKCS11Test {
51
52
// values of the keys we use for the tests
53
54
// keypair using NIST P-256
55
private final static String privD256 = "70a12c2db16845ed56ff68cfc21a472b3f04d7d6851bf6349f2d7d5b3452b38a";
56
private final static String pubX256 = "8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8";
57
private final static String pubY256 = "d8a12ba61d599235f67d9cb4d58f1783d3ca43e78f0a5abaa624079936c0c3a9";
58
59
// keypair using NIST P-384
60
private final static String privD384 = "c838b85253ef8dc7394fa5808a5183981c7deef5a69ba8f4f2117ffea39cfcd90e95f6cbc854abacab701d50c1f3cf24";
61
private final static String pubX384 = "1fbac8eebd0cbf35640b39efe0808dd774debff20a2a329e91713baf7d7f3c3e81546d883730bee7e48678f857b02ca0";
62
private final static String pubY384 = "eb213103bd68ce343365a8a4c3d4555fa385f5330203bdd76ffad1f3affb95751c132007e1b240353cb0a4cf1693bdf9";
63
64
// data to be signed
65
private final static byte[] data = "This is only a test message. It is 48 bytes long".getBytes();
66
67
private KeyFactory kf = null;
68
69
private static void testSignAndVerify(String alg, KeyPair kp, Provider p) throws Exception {
70
Signature s = Signature.getInstance(alg, p);
71
s.initSign(kp.getPrivate());
72
s.update(data);
73
byte[] result = s.sign();
74
75
s.initVerify(kp.getPublic());
76
s.update(data);
77
if (!s.verify(result)) {
78
throw new Exception("Error: Signature verification failed");
79
}
80
System.out.println(p.getName() + ": " + alg + " Passed");
81
}
82
83
private KeyPair genECKeyPair(String curvName, String privD, String pubX,
84
String pubY, Provider p) throws Exception {
85
AlgorithmParameters params = AlgorithmParameters.getInstance("EC", p);
86
params.init(new ECGenParameterSpec(curvName));
87
ECParameterSpec ecParams = params.getParameterSpec(ECParameterSpec.class);
88
ECPrivateKeySpec privKeySpec =
89
new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
90
ECPublicKeySpec pubKeySpec =
91
new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)),
92
ecParams);
93
PrivateKey privKey = kf.generatePrivate(privKeySpec);
94
PublicKey pubKey = kf.generatePublic(pubKeySpec);
95
return new KeyPair(pubKey, privKey);
96
}
97
98
public static void main(String[] args) throws Exception {
99
main(new TestECDSA2(), args);
100
}
101
102
@Override
103
public void main(Provider provider) throws Exception {
104
boolean testP256 =
105
(provider.getService("Signature", "SHA256withECDSA") != null);
106
107
boolean testP384 =
108
(provider.getService("Signature", "SHA384withECDSA") != null);
109
110
if (!testP256 && !testP384) {
111
System.out.println("ECDSA not supported, skipping");
112
return;
113
}
114
115
if (isBadNSSVersion(provider)) {
116
return;
117
}
118
119
kf = KeyFactory.getInstance("EC", provider);
120
121
long start = System.currentTimeMillis();
122
if (testP256) {
123
// can use secp256r1, NIST P-256, X9.62 prime256v1, or 1.2.840.10045.3.1.7
124
KeyPair kp =
125
genECKeyPair("secp256r1", privD256, pubX256, pubY256, provider);
126
testSignAndVerify("SHA256withECDSA", kp, provider);
127
}
128
if (testP384) {
129
// can use secp384r1, NIST P-384, 1.3.132.0.34
130
KeyPair kp =
131
genECKeyPair("secp384r1", privD384, pubX384, pubY384, provider);
132
testSignAndVerify("SHA384withECDSA", kp, provider);
133
}
134
long stop = System.currentTimeMillis();
135
System.out.println("All tests passed (" + (stop - start) + " ms).");
136
}
137
}
138
139