Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ec/SignatureKAT.java
41152 views
1
/*
2
* Copyright (c) 2020, 2021, 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
import jdk.test.lib.Convert;
25
26
import java.security.*;
27
import java.security.spec.*;
28
import java.math.*;
29
import java.util.*;
30
31
/*
32
* @test
33
* @bug 8172366
34
* @summary Known Answer Test for ECDSA signature
35
* @library /test/lib
36
* @build jdk.test.lib.Convert
37
* @run main/othervm SignatureKAT
38
*/
39
public class SignatureKAT {
40
41
private static String checkHex(String hex) {
42
// if hex length is odd, need to prepend 0
43
if (hex.length() % 2 != 0) {
44
hex = "0" + hex;
45
}
46
return hex;
47
}
48
49
private static class CurveData {
50
private String name;
51
private byte[] msgBytes;
52
private BigInteger priv;
53
private BigInteger pubX;
54
private BigInteger pubY;
55
56
private static BigInteger toBigInteger(String hex) {
57
byte[] bytes = HexFormat.of().parseHex(checkHex(hex));
58
return new BigInteger(1, bytes);
59
}
60
CurveData(String name, String msg, String priv, String pubX,
61
String pubY) {
62
this.name = name;
63
this.msgBytes = msg.getBytes();
64
this.priv = toBigInteger(priv);
65
this.pubX = toBigInteger(pubX);
66
this.pubY = toBigInteger(pubY);
67
}
68
}
69
70
private static class TestData {
71
private String sigName;
72
private CurveData cd;
73
private byte[] expSig;
74
75
TestData(String sigName, CurveData cd, String r, String s) {
76
this.sigName = sigName;
77
this.cd = cd;
78
if (r.length() != s.length() || r != checkHex(r) ||
79
s != checkHex(s)) {
80
throw new RuntimeException("Error: invalid r, s");
81
}
82
this.expSig = HexFormat.of().parseHex(r + s);
83
}
84
}
85
86
// These test values are from the examples shown in the page below:
87
// https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
88
private static final CurveData P_256 = new CurveData(
89
"secp256r1", "Example of ECDSA with P-256",
90
"C477F9F65C22CCE20657FAA5B2D1D8122336F851A508A1ED04E479C34985BF96",
91
"B7E08AFDFE94BAD3F1DC8C734798BA1C62B3A0AD1E9EA2A38201CD0889BC7A19",
92
"3603F747959DBF7A4BB226E41928729063ADC7AE43529E61B563BBC606CC5E09"
93
);
94
95
private static final CurveData P_384 = new CurveData(
96
"secp384r1", "Example of ECDSA with P-384",
97
"F92C02ED629E4B48C0584B1C6CE3A3E3B4FAAE4AFC6ACB0455E73DFC392E6A0AE393A8565E6B9714D1224B57D83F8A08",
98
"3BF701BC9E9D36B4D5F1455343F09126F2564390F2B487365071243C61E6471FB9D2AB74657B82F9086489D9EF0F5CB5",
99
"D1A358EAFBF952E68D533855CCBDAA6FF75B137A5101443199325583552A6295FFE5382D00CFCDA30344A9B5B68DB855"
100
);
101
102
private static final CurveData P_521 = new CurveData(
103
"secp521r1", "Example of ECDSA with P-521",
104
"100085F47B8E1B8B11B7EB33028C0B2888E304BFC98501955B45BBA1478DC184EEEDF09B86A5F7C21994406072787205E69A63709FE35AA93BA333514B24F961722",
105
"98E91EEF9A68452822309C52FAB453F5F117C1DA8ED796B255E9AB8F6410CCA16E59DF403A6BDC6CA467A37056B1E54B3005D8AC030DECFEB68DF18B171885D5C4",
106
"164350C321AECFC1CCA1BA4364C9B15656150B4B78D6A48D7D28E7F31985EF17BE8554376B72900712C4B83AD668327231526E313F5F092999A4632FD50D946BC2E"
107
);
108
109
private static TestData[] TEST_DATUM = {
110
// secp256r1, secp384r1, and secp521r1 remain enabled
111
new TestData("SHA256withECDSAinP1363Format", P_256,
112
"2B42F576D07F4165FF65D1F3B1500F81E44C316F1F0B3EF57325B69ACA46104F",
113
"DC42C2122D6392CD3E3A993A89502A8198C1886FE69D262C4B329BDB6B63FAF1"),
114
new TestData("SHA3-256withECDSAinP1363Format", P_256,
115
"2B42F576D07F4165FF65D1F3B1500F81E44C316F1F0B3EF57325B69ACA46104F",
116
"0A861C2526900245C73BACB9ADAEC1A5ACB3BA1F7114A3C334FDCD5B7690DADD"),
117
new TestData("SHA384withECDSAinP1363Format", P_384,
118
"30EA514FC0D38D8208756F068113C7CADA9F66A3B40EA3B313D040D9B57DD41A332795D02CC7D507FCEF9FAF01A27088",
119
"CC808E504BE414F46C9027BCBF78ADF067A43922D6FCAA66C4476875FBB7B94EFD1F7D5DBE620BFB821C46D549683AD8"),
120
new TestData("SHA3-384withECDSAinP1363Format", P_384,
121
"30EA514FC0D38D8208756F068113C7CADA9F66A3B40EA3B313D040D9B57DD41A332795D02CC7D507FCEF9FAF01A27088",
122
"691B9D4969451A98036D53AA725458602125DE74881BBC333012CA4FA55BDE39D1BF16A6AAE3FE4992C567C6E7892337"),
123
new TestData("SHA512withECDSAinP1363Format", P_521,
124
"0140C8EDCA57108CE3F7E7A240DDD3AD74D81E2DE62451FC1D558FDC79269ADACD1C2526EEEEF32F8C0432A9D56E2B4A8A732891C37C9B96641A9254CCFE5DC3E2BA",
125
"00D72F15229D0096376DA6651D9985BFD7C07F8D49583B545DB3EAB20E0A2C1E8615BD9E298455BDEB6B61378E77AF1C54EEE2CE37B2C61F5C9A8232951CB988B5B1"),
126
new TestData("SHA3-512withECDSAinP1363Format", P_521,
127
"0140C8EDCA57108CE3F7E7A240DDD3AD74D81E2DE62451FC1D558FDC79269ADACD1C2526EEEEF32F8C0432A9D56E2B4A8A732891C37C9B96641A9254CCFE5DC3E2BA",
128
"00B25188492D58E808EDEBD7BF440ED20DB771CA7C618595D5398E1B1C0098E300D8C803EC69EC5F46C84FC61967A302D366C627FCFA56F87F241EF921B6E627ADBF"),
129
};
130
131
private static void runTest(TestData td) throws Exception {
132
System.out.println("Testing " + td.sigName + " with " + td.cd.name);
133
134
AlgorithmParameters params =
135
AlgorithmParameters.getInstance("EC", "SunEC");
136
params.init(new ECGenParameterSpec(td.cd.name));
137
ECParameterSpec ecParams =
138
params.getParameterSpec(ECParameterSpec.class);
139
140
KeyFactory kf = KeyFactory.getInstance("EC", "SunEC");
141
PrivateKey privKey = kf.generatePrivate
142
(new ECPrivateKeySpec(td.cd.priv, ecParams));
143
144
Signature sig = Signature.getInstance(td.sigName, "SunEC");
145
sig.initSign(privKey);
146
sig.update(td.cd.msgBytes);
147
// NOTE: there is no way to set the nonce value into current SunEC
148
// ECDSA signature, thus the output signature bytes likely won't
149
// match the expected signature bytes
150
byte[] ov = sig.sign();
151
152
ECPublicKeySpec pubKeySpec = new ECPublicKeySpec
153
(new ECPoint(td.cd.pubX, td.cd.pubY), ecParams);
154
PublicKey pubKey = kf.generatePublic(pubKeySpec);
155
156
sig.initVerify(pubKey);
157
sig.update(td.cd.msgBytes);
158
if (!sig.verify(ov)) {
159
throw new RuntimeException("Error verifying actual sig bytes");
160
}
161
162
sig.update(td.cd.msgBytes);
163
if (!sig.verify(td.expSig)) {
164
throw new RuntimeException("Error verifying expected sig bytes");
165
}
166
}
167
168
public static void main(String[] args) throws Exception {
169
for (TestData td : TEST_DATUM) {
170
runTest(td);
171
}
172
}
173
}
174
175