Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/util/ECUtil.java
41159 views
1
/*
2
* Copyright (c) 2006, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.util;
27
28
import jdk.internal.access.SharedSecrets;
29
30
import java.io.IOException;
31
import java.math.BigInteger;
32
import java.security.*;
33
import java.security.interfaces.*;
34
import java.security.spec.*;
35
import java.util.Arrays;
36
37
public final class ECUtil {
38
39
// Used by SunEC
40
public static byte[] sArray(BigInteger s, ECParameterSpec params) {
41
byte[] arr = s.toByteArray();
42
ArrayUtil.reverse(arr);
43
int byteLength = (params.getOrder().bitLength() + 7) / 8;
44
byte[] arrayS = new byte[byteLength];
45
int length = Math.min(byteLength, arr.length);
46
System.arraycopy(arr, 0, arrayS, 0, length);
47
return arrayS;
48
}
49
50
// Used by SunPKCS11 and SunJSSE.
51
public static ECPoint decodePoint(byte[] data, EllipticCurve curve)
52
throws IOException {
53
if ((data.length == 0) || (data[0] != 4)) {
54
throw new IOException("Only uncompressed point format supported");
55
}
56
// Per ANSI X9.62, an encoded point is a 1 byte type followed by
57
// ceiling(log base 2 field-size / 8) bytes of x and the same of y.
58
int n = (data.length - 1) / 2;
59
if (n != ((curve.getField().getFieldSize() + 7 ) >> 3)) {
60
throw new IOException("Point does not match field size");
61
}
62
63
byte[] xb = Arrays.copyOfRange(data, 1, 1 + n);
64
byte[] yb = Arrays.copyOfRange(data, n + 1, n + 1 + n);
65
66
return new ECPoint(new BigInteger(1, xb), new BigInteger(1, yb));
67
}
68
69
// Used by SunPKCS11 and SunJSSE.
70
public static byte[] encodePoint(ECPoint point, EllipticCurve curve) {
71
// get field size in bytes (rounding up)
72
int n = (curve.getField().getFieldSize() + 7) >> 3;
73
byte[] xb = trimZeroes(point.getAffineX().toByteArray());
74
byte[] yb = trimZeroes(point.getAffineY().toByteArray());
75
if ((xb.length > n) || (yb.length > n)) {
76
throw new RuntimeException
77
("Point coordinates do not match field size");
78
}
79
byte[] b = new byte[1 + (n << 1)];
80
b[0] = 4; // uncompressed
81
System.arraycopy(xb, 0, b, n - xb.length + 1, xb.length);
82
System.arraycopy(yb, 0, b, b.length - yb.length, yb.length);
83
return b;
84
}
85
86
public static byte[] trimZeroes(byte[] b) {
87
int i = 0;
88
while ((i < b.length - 1) && (b[i] == 0)) {
89
i++;
90
}
91
if (i == 0) {
92
return b;
93
}
94
95
return Arrays.copyOfRange(b, i, b.length);
96
}
97
98
private static KeyFactory getKeyFactory() {
99
try {
100
return KeyFactory.getInstance("EC", "SunEC");
101
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
102
throw new RuntimeException(e);
103
}
104
}
105
106
public static ECPublicKey decodeX509ECPublicKey(byte[] encoded)
107
throws InvalidKeySpecException {
108
KeyFactory keyFactory = getKeyFactory();
109
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);
110
111
return (ECPublicKey)keyFactory.generatePublic(keySpec);
112
}
113
114
public static byte[] x509EncodeECPublicKey(ECPoint w,
115
ECParameterSpec params) throws InvalidKeySpecException {
116
KeyFactory keyFactory = getKeyFactory();
117
ECPublicKeySpec keySpec = new ECPublicKeySpec(w, params);
118
Key key = keyFactory.generatePublic(keySpec);
119
120
return key.getEncoded();
121
}
122
123
public static ECPrivateKey decodePKCS8ECPrivateKey(byte[] encoded)
124
throws InvalidKeySpecException {
125
KeyFactory keyFactory = getKeyFactory();
126
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
127
try {
128
return (ECPrivateKey) keyFactory.generatePrivate(keySpec);
129
} finally {
130
SharedSecrets.getJavaSecuritySpecAccess().clearEncodedKeySpec(keySpec);
131
}
132
}
133
134
public static ECPrivateKey generateECPrivateKey(BigInteger s,
135
ECParameterSpec params) throws InvalidKeySpecException {
136
KeyFactory keyFactory = getKeyFactory();
137
ECPrivateKeySpec keySpec = new ECPrivateKeySpec(s, params);
138
139
return (ECPrivateKey)keyFactory.generatePrivate(keySpec);
140
}
141
142
public static AlgorithmParameters getECParameters(Provider p) {
143
try {
144
if (p != null) {
145
return AlgorithmParameters.getInstance("EC", p);
146
}
147
148
return AlgorithmParameters.getInstance("EC");
149
} catch (NoSuchAlgorithmException nsae) {
150
throw new RuntimeException(nsae);
151
}
152
}
153
154
public static byte[] encodeECParameterSpec(Provider p,
155
ECParameterSpec spec) {
156
AlgorithmParameters parameters = getECParameters(p);
157
158
try {
159
parameters.init(spec);
160
} catch (InvalidParameterSpecException ipse) {
161
throw new RuntimeException("Not a known named curve: " + spec);
162
}
163
164
try {
165
return parameters.getEncoded();
166
} catch (IOException ioe) {
167
// it is a bug if this should happen
168
throw new RuntimeException(ioe);
169
}
170
}
171
172
public static ECParameterSpec getECParameterSpec(Provider p,
173
ECParameterSpec spec) {
174
AlgorithmParameters parameters = getECParameters(p);
175
176
try {
177
parameters.init(spec);
178
return parameters.getParameterSpec(ECParameterSpec.class);
179
} catch (InvalidParameterSpecException ipse) {
180
return null;
181
}
182
}
183
184
public static ECParameterSpec getECParameterSpec(Provider p,
185
byte[] params)
186
throws IOException {
187
AlgorithmParameters parameters = getECParameters(p);
188
189
parameters.init(params);
190
191
try {
192
return parameters.getParameterSpec(ECParameterSpec.class);
193
} catch (InvalidParameterSpecException ipse) {
194
return null;
195
}
196
}
197
198
public static ECParameterSpec getECParameterSpec(Provider p, String name) {
199
AlgorithmParameters parameters = getECParameters(p);
200
201
try {
202
parameters.init(new ECGenParameterSpec(name));
203
return parameters.getParameterSpec(ECParameterSpec.class);
204
} catch (InvalidParameterSpecException ipse) {
205
return null;
206
}
207
}
208
209
public static ECParameterSpec getECParameterSpec(Provider p, int keySize) {
210
AlgorithmParameters parameters = getECParameters(p);
211
212
try {
213
parameters.init(new ECKeySizeParameterSpec(keySize));
214
return parameters.getParameterSpec(ECParameterSpec.class);
215
} catch (InvalidParameterSpecException ipse) {
216
return null;
217
}
218
219
}
220
221
public static String getCurveName(Provider p, ECParameterSpec spec) {
222
ECGenParameterSpec nameSpec;
223
AlgorithmParameters parameters = getECParameters(p);
224
225
try {
226
parameters.init(spec);
227
nameSpec = parameters.getParameterSpec(ECGenParameterSpec.class);
228
} catch (InvalidParameterSpecException ipse) {
229
return null;
230
}
231
232
if (nameSpec == null) {
233
return null;
234
}
235
236
return nameSpec.getName();
237
}
238
239
public static boolean equals(ECParameterSpec spec1, ECParameterSpec spec2) {
240
if (spec1 == spec2) {
241
return true;
242
}
243
244
if (spec1 == null || spec2 == null) {
245
return false;
246
}
247
return (spec1.getCofactor() == spec2.getCofactor() &&
248
spec1.getOrder().equals(spec2.getOrder()) &&
249
spec1.getCurve().equals(spec2.getCurve()) &&
250
spec1.getGenerator().equals(spec2.getGenerator()));
251
}
252
253
254
// Convert the concatenation R and S in into their DER encoding
255
public static byte[] encodeSignature(byte[] signature) throws SignatureException {
256
257
try {
258
259
int n = signature.length >> 1;
260
byte[] bytes = new byte[n];
261
System.arraycopy(signature, 0, bytes, 0, n);
262
BigInteger r = new BigInteger(1, bytes);
263
System.arraycopy(signature, n, bytes, 0, n);
264
BigInteger s = new BigInteger(1, bytes);
265
266
DerOutputStream out = new DerOutputStream(signature.length + 10);
267
out.putInteger(r);
268
out.putInteger(s);
269
DerValue result =
270
new DerValue(DerValue.tag_Sequence, out.toByteArray());
271
272
return result.toByteArray();
273
274
} catch (Exception e) {
275
throw new SignatureException("Could not encode signature", e);
276
}
277
}
278
279
// Convert the DER encoding of R and S into a concatenation of R and S
280
public static byte[] decodeSignature(byte[] sig) throws SignatureException {
281
282
try {
283
// Enforce strict DER checking for signatures
284
DerInputStream in = new DerInputStream(sig, 0, sig.length, false);
285
DerValue[] values = in.getSequence(2);
286
287
// check number of components in the read sequence
288
// and trailing data
289
if ((values.length != 2) || (in.available() != 0)) {
290
throw new IOException("Invalid encoding for signature");
291
}
292
293
BigInteger r = values[0].getPositiveBigInteger();
294
BigInteger s = values[1].getPositiveBigInteger();
295
296
// trim leading zeroes
297
byte[] rBytes = trimZeroes(r.toByteArray());
298
byte[] sBytes = trimZeroes(s.toByteArray());
299
int k = Math.max(rBytes.length, sBytes.length);
300
// r and s each occupy half the array
301
byte[] result = new byte[k << 1];
302
System.arraycopy(rBytes, 0, result, k - rBytes.length,
303
rBytes.length);
304
System.arraycopy(sBytes, 0, result, result.length - sBytes.length,
305
sBytes.length);
306
return result;
307
308
} catch (Exception e) {
309
throw new SignatureException("Invalid encoding for signature", e);
310
}
311
}
312
313
private ECUtil() {}
314
}
315
316