Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ec/xec/XECIterative.java
41155 views
1
/*
2
* Copyright (c) 2018, 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
/*
25
* @test
26
* @bug 8171277
27
* @summary XEC curve operations iterative test vectors
28
* @library /test/lib
29
* @build jdk.test.lib.Convert
30
* @modules jdk.crypto.ec/sun.security.ec
31
* @run main XECIterative 0 10000
32
* @run main XECIterative 10000 20000
33
* @run main XECIterative 20000 30000
34
* @run main XECIterative 30000 40000
35
* @run main XECIterative 40000 50000
36
* @run main XECIterative 50000 60000
37
* @run main XECIterative 60000 70000
38
* @run main XECIterative 70000 80000
39
* @run main XECIterative 80000 90000
40
* @run main XECIterative 90000 100000
41
*/
42
43
import sun.security.ec.*;
44
45
import java.io.*;
46
import java.security.spec.NamedParameterSpec;
47
import java.util.*;
48
49
/*
50
* This test is derived from the iterative test in RFC 7748. To produce the
51
* test vectors, the implementation ran for 1,000,000 iterations and saved
52
* values of k and u at periodic checkpoints. The RFC includes the correct
53
* value of k after 1,000,000 iterations, and this value was used to ensure
54
* that the test vectors are correct. This test has multiple @run tags so that
55
* no single run takes too long.
56
*/
57
58
public class XECIterative {
59
60
private static class KU {
61
62
public byte[] k;
63
public byte[] u;
64
65
}
66
67
public static void main(String[] args) throws IOException {
68
69
long start = Long.parseLong(args[0]);
70
long end = Long.parseLong(args[1]);
71
72
XECIterative m = new XECIterative();
73
74
m.runIterativeTest("X25519", start, end);
75
m.runIterativeTest("X448", start, end);
76
77
}
78
79
private void runIterativeTest(String opName, long start, long end)
80
throws IOException {
81
82
NamedParameterSpec paramSpec = new NamedParameterSpec(opName);
83
XECParameters settings =
84
XECParameters.get(RuntimeException::new, paramSpec);
85
XECOperations ops = new XECOperations(settings);
86
87
File vectorFile = new File(System.getProperty("test.src", "."),
88
opName + ".iter");
89
90
Map<Long, KU> testIters = new HashMap<Long, KU>();
91
BufferedReader in = new BufferedReader(new FileReader(vectorFile));
92
String line;
93
while ((line = in.readLine()) != null) {
94
StringTokenizer tok = new StringTokenizer(line, ",");
95
long iter = Long.parseLong(tok.nextToken());
96
String kOrU = tok.nextToken();
97
byte[] value = HexFormat.of().parseHex(tok.nextToken());
98
KU entry = testIters.get(iter);
99
if (entry == null) {
100
entry = new KU();
101
testIters.put(iter, entry);
102
}
103
if (kOrU.equals("k")) {
104
entry.k = value;
105
} else {
106
entry.u = value;
107
}
108
}
109
110
KU startEntry = testIters.get(start);
111
byte[] k = startEntry.k.clone();
112
byte[] u = startEntry.u.clone();
113
114
for (long i = start; i <= end; i++) {
115
KU curEntry;
116
if (i % 1000 == 0 && (curEntry = testIters.get(i)) != null) {
117
if (!Arrays.equals(k, curEntry.k)) {
118
throw new RuntimeException("At iter " + i + ": expected k: "
119
+ HexFormat.of().withUpperCase().formatHex(curEntry.k)
120
+ ", computed k: " + HexFormat.of().withUpperCase().formatHex(k));
121
}
122
if (!Arrays.equals(u, curEntry.u)) {
123
throw new RuntimeException("At iter " + i + ": expected u: "
124
+ HexFormat.of().withUpperCase().formatHex(curEntry.u)
125
+ ", computed u: " + HexFormat.of().withUpperCase().formatHex(u));
126
}
127
System.out.println(opName + " checkpoint passed at " + i);
128
}
129
130
byte[] k_copy = Arrays.copyOf(k, k.length);
131
byte[] u_out = ops.encodedPointMultiply(k, u);
132
u = k_copy;
133
k = u_out;
134
}
135
}
136
137
}
138
139
140
141