Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/spec/ECCBasic.java
41149 views
1
/*
2
* Copyright (c) 2003, 2007, 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 4794996
27
* @summary Ensure basic functionality of these new ECC classes.
28
* @author Valerie Peng
29
*/
30
import java.math.BigInteger;
31
import java.util.Arrays;
32
import java.security.*;
33
import java.security.spec.*;
34
35
public class ECCBasic {
36
private static final BigInteger ZERO = BigInteger.ZERO;
37
private static final BigInteger ONE = BigInteger.ONE;
38
private static final BigInteger TEN = BigInteger.TEN;
39
private static final ECFieldFp FP = new ECFieldFp(TEN);
40
private static final int F2M_M = 4;
41
private static final int[] F2M_KS;
42
static {
43
F2M_KS = new int[1];
44
F2M_KS[0] = 1;
45
}
46
private static final BigInteger F2M_RP = BigInteger.valueOf(19);
47
private static final ECFieldF2m F2M = new ECFieldF2m(F2M_M, F2M_KS);
48
private static final EllipticCurve CURVE = new EllipticCurve
49
(new ECFieldFp(TEN), ONE, ONE);
50
private static final ECPoint POINT = new ECPoint(ONE, TEN);
51
private static final BigInteger ORDER = ONE;
52
private static final int COFACTOR = 3;
53
private static final ECParameterSpec PARAMS = new ECParameterSpec
54
(CURVE, POINT, ORDER, COFACTOR);
55
private static final ECGenParameterSpec GENPARAMS =
56
new ECGenParameterSpec("prime192v1");
57
private static final ECPrivateKeySpec PRIV_KEY = new ECPrivateKeySpec
58
(TEN, PARAMS);
59
private static final ECPublicKeySpec PUB_KEY = new ECPublicKeySpec
60
(POINT, PARAMS);
61
62
private static void testECFieldFp() throws Exception {
63
System.out.println("Testing ECFieldFp(BigInteger)");
64
try {
65
new ECFieldFp(ZERO);
66
throw new Exception("...should throw IAE");
67
} catch (IllegalArgumentException iae) {
68
System.out.println("...expected IAE thrown");
69
}
70
try {
71
new ECFieldFp(null);
72
throw new Exception("...should throw NPE");
73
} catch (NullPointerException npe) {
74
System.out.println("...expected NPE thrown");
75
}
76
if (TEN.equals(FP.getP()) == false) {
77
throw new Exception("...error in getP()");
78
}
79
if (FP.getFieldSize() != TEN.bitLength()) {
80
throw new Exception("...error in getFieldSize()");
81
}
82
}
83
private static void testECFieldF2m() throws Exception {
84
// invalid parameters
85
int mBad = 0;
86
int[] ksBad;
87
for (int i=0; i<7; i++) {
88
System.out.print("Testing ECFieldF2m");
89
try {
90
switch (i) {
91
case 0:
92
System.out.println("(int)");
93
new ECFieldF2m(mBad);
94
break;
95
case 1:
96
System.out.println("(int, BigInteger)#1");
97
new ECFieldF2m(mBad, F2M_RP);
98
break;
99
case 2:
100
System.out.println("(int, BigInteger)#2");
101
new ECFieldF2m(mBad, F2M_RP);
102
break;
103
case 3:
104
System.out.println("(int, int[])#1");
105
new ECFieldF2m(mBad, F2M_KS);
106
break;
107
case 4:
108
ksBad = new int[2];
109
System.out.println("(int, int[])#2");
110
new ECFieldF2m(F2M_M, ksBad);
111
break;
112
case 5:
113
ksBad = new int[1];
114
ksBad[0] = 5;
115
System.out.println("(int, int[])#3");
116
new ECFieldF2m(F2M_M, ksBad);
117
break;
118
case 6:
119
ksBad = new int[3];
120
ksBad[0] = 1;
121
ksBad[1] = 2;
122
ksBad[2] = 3;
123
System.out.println("(int, int[])#4");
124
new ECFieldF2m(F2M_M, ksBad);
125
break;
126
}
127
throw new Exception("...should throw IAE");
128
} catch (IllegalArgumentException iae) {
129
System.out.println("...expected IAE thrown");
130
}
131
}
132
for (int i=0; i<2; i++) {
133
System.out.print("Testing ECFieldF2m");
134
try {
135
switch (i) {
136
case 0:
137
System.out.println("(int, BigInteger)");
138
new ECFieldF2m(F2M_M, (BigInteger) null);
139
break;
140
case 1:
141
System.out.println("(int, int[])#1");
142
new ECFieldF2m(F2M_M, (int[]) null);
143
break;
144
}
145
throw new Exception("...should throw NPE");
146
} catch (NullPointerException npe) {
147
System.out.println("...expected NPE thrown");
148
}
149
}
150
if (F2M_RP.compareTo(F2M.getReductionPolynomial()) != 0) {
151
throw new Exception("...error in getReductionPolynomial()");
152
}
153
ECFieldF2m field = new ECFieldF2m(F2M_M, F2M_RP);
154
if (!(Arrays.equals(F2M_KS,
155
field.getMidTermsOfReductionPolynomial()))) {
156
throw new Exception("...error in getMidTermsOfReductionPolynomial()");
157
}
158
if (field.getFieldSize() != F2M_M) {
159
throw new Exception("...error in getFieldSize()");
160
}
161
}
162
private static void testECParameterSpec() throws Exception {
163
System.out.println("Testing ECParameterSpec(...)");
164
for (int i = 0; i < 2; i++) {
165
try {
166
switch (i) {
167
case 0:
168
System.out.println("with zero order");
169
new ECParameterSpec(CURVE, POINT, ZERO, COFACTOR);
170
break;
171
case 1:
172
System.out.println("with negative cofactor");
173
new ECParameterSpec(CURVE, POINT, ORDER, -1);
174
break;
175
}
176
throw new Exception("...should throw IAE");
177
} catch (IllegalArgumentException iae) {
178
System.out.println("...expected IAE thrown");
179
}
180
}
181
for (int i = 0; i < 3; i++) {
182
try {
183
switch (i) {
184
case 0:
185
System.out.println("with null curve");
186
new ECParameterSpec(null, POINT, ORDER, COFACTOR);
187
break;
188
case 1:
189
System.out.println("with null generator");
190
new ECParameterSpec(CURVE, null, ORDER, COFACTOR);
191
break;
192
case 2:
193
System.out.println("with null order");
194
new ECParameterSpec(CURVE, POINT, null, COFACTOR);
195
break;
196
}
197
throw new Exception("...should throw NPE");
198
} catch (NullPointerException npe) {
199
System.out.println("...expected NPE thrown");
200
}
201
}
202
if (!(CURVE.equals(PARAMS.getCurve()))) {
203
throw new Exception("...error in getCurve()");
204
}
205
if (!(POINT.equals(PARAMS.getGenerator()))) {
206
throw new Exception("...error in getGenerator()");
207
}
208
if (!(ORDER.equals(PARAMS.getOrder()))) {
209
throw new Exception("...error in getOrder()");
210
}
211
if (COFACTOR != PARAMS.getCofactor()) {
212
throw new Exception("...error in getCofactor()");
213
}
214
}
215
private static void testECGenParameterSpec() throws Exception {
216
System.out.println("Testing ECGenParameterSpec(String)");
217
try {
218
new ECGenParameterSpec(null);
219
throw new Exception("...should throw NPE");
220
} catch (NullPointerException npe) {
221
System.out.println("...expected NPE thrown");
222
}
223
if (!("prime192v1".equals(GENPARAMS.getName()))) {
224
throw new Exception("...error in getName()");
225
}
226
}
227
private static void testECPoint() throws Exception {
228
System.out.println("Testing ECParameterSpec(...)");
229
for (int i = 0; i < 2; i++) {
230
try {
231
switch (i) {
232
case 0:
233
System.out.println("with null x-coordinate");
234
new ECPoint(null, TEN);
235
break;
236
case 1:
237
System.out.println("with null y-coordinate");
238
new ECPoint(ONE, null);
239
break;
240
}
241
throw new Exception("...should throw NPE");
242
} catch (NullPointerException npe) {
243
System.out.println("...expected NPE thrown");
244
}
245
}
246
if (!(ONE.equals(POINT.getAffineX()))) {
247
throw new Exception("...error in getAffineX()");
248
}
249
if (!(TEN.equals(POINT.getAffineY()))) {
250
throw new Exception("...error in getAffineY()");
251
}
252
}
253
private static void testECPublicKeySpec() throws Exception {
254
System.out.println("Testing ECPublicKeySpec(...)");
255
for (int i = 0; i < 2; i++) {
256
try {
257
switch (i) {
258
case 0:
259
System.out.println("with null public value");
260
new ECPublicKeySpec(null, PARAMS);
261
break;
262
case 1:
263
System.out.println("with null params");
264
new ECPublicKeySpec(POINT, null);
265
break;
266
}
267
throw new Exception("...should throw NPE");
268
} catch (NullPointerException npe) {
269
System.out.println("...expected NPE thrown");
270
}
271
}
272
if (!(POINT.equals(PUB_KEY.getW()))) {
273
throw new Exception("...error in getW()");
274
}
275
if (!(PARAMS.equals(PUB_KEY.getParams()))) {
276
throw new Exception("...error in getParams()");
277
}
278
}
279
private static void testECPrivateKeySpec() throws Exception {
280
System.out.println("Testing ECPrivateKeySpec(...)");
281
for (int i = 0; i < 2; i++) {
282
try {
283
switch (i) {
284
case 0:
285
System.out.println("with null private value");
286
new ECPrivateKeySpec(null, PARAMS);
287
break;
288
case 1:
289
System.out.println("with null param");
290
new ECPrivateKeySpec(ONE, null);
291
break;
292
}
293
throw new Exception("...should throw NPE");
294
} catch (NullPointerException npe) {
295
System.out.println("...expected NPE thrown");
296
}
297
}
298
if (!(TEN.equals(PRIV_KEY.getS()))) {
299
throw new Exception("...error in getS()");
300
}
301
if (!(PARAMS.equals(PRIV_KEY.getParams()))) {
302
throw new Exception("...error in getParams()");
303
}
304
}
305
private static void testEllipticCurve() throws Exception {
306
System.out.println("Testing EllipticCurve(...)");
307
for (int j = 0; j < 2; j++) {
308
BigInteger a = ONE;
309
BigInteger b = ONE;
310
if (j == 0) { // test a out of range
311
System.out.println("with a's value out of range");
312
a = BigInteger.valueOf(20);
313
} else { // test b out of range
314
System.out.println("with b's value out of range");
315
b = BigInteger.valueOf(20);
316
}
317
System.out.println(">> over ECFieldFp");
318
for (int i = 0; i < 3; i++) {
319
try {
320
switch (i) {
321
case 0:
322
new EllipticCurve(FP, a, b);
323
break;
324
case 1:
325
new EllipticCurve(FP, a, b, null);
326
break;
327
case 2:
328
new EllipticCurve(FP, a, b, new byte[8]);
329
break;
330
}
331
throw new Exception("...should throw IAE");
332
} catch (IllegalArgumentException iae) {
333
System.out.println("...expected IAE thrown for #" + (i+1));
334
}
335
}
336
System.out.println(">> over ECFieldF2m");
337
for (int i = 0; i < 3; i++) {
338
try {
339
switch (i) {
340
case 0:
341
new EllipticCurve(F2M, a, b);
342
break;
343
case 1:
344
new EllipticCurve(F2M, a, b, null);
345
break;
346
case 2:
347
new EllipticCurve(F2M, a, b, new byte[8]);
348
break;
349
}
350
throw new Exception("...should throw IAE");
351
} catch (IllegalArgumentException iae) {
352
System.out.println("...expected IAE thrown for #" + (i+1));
353
}
354
}
355
}
356
for (int i = 0; i < 6; i++) {
357
try {
358
switch (i) {
359
case 0:
360
new EllipticCurve(null, ONE, TEN);
361
break;
362
case 1:
363
new EllipticCurve(FP, null, TEN);
364
break;
365
case 2:
366
new EllipticCurve(FP, ONE, null);
367
break;
368
case 3:
369
new EllipticCurve(null, ONE, TEN, null);
370
break;
371
case 4:
372
new EllipticCurve(FP, null, TEN, null);
373
break;
374
case 5:
375
new EllipticCurve(FP, ONE, null, null);
376
break;
377
}
378
throw new Exception("...should throw NPE");
379
} catch (NullPointerException npe) {
380
System.out.println("...expected NPE thrown");
381
}
382
}
383
if (!(FP.equals(CURVE.getField()))) {
384
throw new Exception("...error in getField()");
385
}
386
if (!(ONE.equals(CURVE.getA()))) {
387
throw new Exception("...error in getA()");
388
}
389
if (!(ONE.equals(CURVE.getB()))) {
390
throw new Exception("...error in getB()");
391
}
392
if (!(CURVE.equals(new EllipticCurve(FP, ONE, ONE, null)))) {
393
throw new Exception("...error in equals()");
394
}
395
}
396
private static void testAllHashCode() throws Exception {
397
// make sure no unexpected exception when hashCode() is called.
398
FP.hashCode();
399
F2M.hashCode();
400
GENPARAMS.hashCode();
401
PARAMS.hashCode();
402
POINT.hashCode();
403
PRIV_KEY.hashCode();
404
PUB_KEY.hashCode();
405
CURVE.hashCode();
406
}
407
public static void main(String[] argv) throws Exception {
408
testECFieldFp();
409
testECFieldF2m();
410
testECParameterSpec();
411
testECGenParameterSpec();
412
testECPoint();
413
testECPrivateKeySpec();
414
testECPublicKeySpec();
415
testEllipticCurve();
416
testAllHashCode();
417
System.out.println("All Test Passed");
418
}
419
}
420
421