Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestGHASH.java
41161 views
1
/*
2
* Copyright (c) 2015, Red Hat, Inc.
3
* Copyright (c) 2015, Oracle, Inc.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
/*
26
* @test
27
* @bug 8069072
28
* @modules java.base/com.sun.crypto.provider:open
29
* @summary Test vectors for com.sun.crypto.provider.GHASH.
30
*
31
* Single iteration to verify software-only GHASH algorithm.
32
* @run main TestGHASH
33
*
34
* Multi-iteration to verify test intrinsics GHASH, if available.
35
* Many iterations are needed so we are sure hotspot will use intrinsic
36
* @run main TestGHASH -n 10000
37
*/
38
import java.lang.reflect.Constructor;
39
import java.lang.reflect.Method;
40
import java.nio.ByteBuffer;
41
42
public class TestGHASH {
43
44
private final Constructor<?> GHASH;
45
private final Method UPDATE;
46
private final Method DIGEST;
47
48
TestGHASH(String className) throws Exception {
49
Class<?> cls = Class.forName(className);
50
GHASH = cls.getDeclaredConstructor(byte[].class);
51
GHASH.setAccessible(true);
52
UPDATE = cls.getDeclaredMethod("update", byte[].class);
53
UPDATE.setAccessible(true);
54
DIGEST = cls.getDeclaredMethod("digest");
55
DIGEST.setAccessible(true);
56
}
57
58
59
private Object newGHASH(byte[] H) throws Exception {
60
return GHASH.newInstance(H);
61
}
62
63
private void updateGHASH(Object hash, byte[] data)
64
throws Exception {
65
UPDATE.invoke(hash, data);
66
}
67
68
private byte[] digestGHASH(Object hash) throws Exception {
69
return (byte[]) DIGEST.invoke(hash);
70
}
71
72
private static final String HEX_DIGITS = "0123456789abcdef";
73
74
private static String hex(byte[] bs) {
75
StringBuilder sb = new StringBuilder(2 * bs.length);
76
for (byte b : bs) {
77
sb.append(HEX_DIGITS.charAt((b >> 4) & 0xF));
78
sb.append(HEX_DIGITS.charAt(b & 0xF));
79
}
80
return sb.toString();
81
}
82
83
private static byte[] bytes(String hex) {
84
if ((hex.length() & 1) != 0) {
85
throw new AssertionError();
86
}
87
byte[] result = new byte[hex.length() / 2];
88
for (int i = 0; i < result.length; ++i) {
89
int a = HEX_DIGITS.indexOf(hex.charAt(2 * i));
90
int b = HEX_DIGITS.indexOf(hex.charAt(2 * i + 1));
91
if ((a | b) < 0) {
92
if (a < 0) {
93
throw new AssertionError(
94
"bad character " + (int) hex.charAt(2 * i));
95
}
96
throw new AssertionError(
97
"bad character " + (int) hex.charAt(2 * i + 1));
98
}
99
result[i] = (byte) ((a << 4) | b);
100
}
101
return result;
102
}
103
104
private static byte[] bytes(long L0, long L1) {
105
return ByteBuffer.allocate(16)
106
.putLong(L0)
107
.putLong(L1)
108
.array();
109
}
110
111
private void check(int testCase, String H, String A,
112
String C, String expected) throws Exception {
113
int lenA = A.length() * 4;
114
while ((A.length() % 32) != 0) {
115
A += '0';
116
}
117
int lenC = C.length() * 4;
118
while ((C.length() % 32) != 0) {
119
C += '0';
120
}
121
122
Object hash = newGHASH(bytes(H));
123
updateGHASH(hash, bytes(A));
124
updateGHASH(hash, bytes(C));
125
updateGHASH(hash, bytes(lenA, lenC));
126
byte[] digest = digestGHASH(hash);
127
String actual = hex(digest);
128
if (!expected.equals(actual)) {
129
throw new AssertionError(String.format("%d: expected %s, got %s",
130
testCase, expected, actual));
131
}
132
}
133
134
public static void main(String[] args) throws Exception {
135
TestGHASH test;
136
String test_class = "com.sun.crypto.provider.GHASH";
137
int i = 0;
138
int num_of_loops = 1;
139
while (args.length > i) {
140
if (args[i].compareTo("-c") == 0) {
141
test_class = args[++i];
142
} else if (args[i].compareTo("-n") == 0) {
143
num_of_loops = Integer.parseInt(args[++i]);
144
}
145
i++;
146
}
147
148
System.out.println("Running " + num_of_loops + " iterations.");
149
test = new TestGHASH(test_class);
150
i = 0;
151
152
while (num_of_loops > i) {
153
// Test vectors from David A. McGrew, John Viega,
154
// "The Galois/Counter Mode of Operation (GCM)", 2005.
155
// <http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf>
156
test.check(1, "66e94bd4ef8a2c3b884cfa59ca342b2e", "", "",
157
"00000000000000000000000000000000");
158
test.check(2,
159
"66e94bd4ef8a2c3b884cfa59ca342b2e", "",
160
"0388dace60b6a392f328c2b971b2fe78",
161
"f38cbb1ad69223dcc3457ae5b6b0f885");
162
test.check(3,
163
"b83b533708bf535d0aa6e52980d53b78", "",
164
"42831ec2217774244b7221b784d0d49c" +
165
"e3aa212f2c02a4e035c17e2329aca12e" +
166
"21d514b25466931c7d8f6a5aac84aa05" +
167
"1ba30b396a0aac973d58e091473f5985",
168
"7f1b32b81b820d02614f8895ac1d4eac");
169
test.check(4,
170
"b83b533708bf535d0aa6e52980d53b78",
171
"feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",
172
"42831ec2217774244b7221b784d0d49c" +
173
"e3aa212f2c02a4e035c17e2329aca12e" +
174
"21d514b25466931c7d8f6a5aac84aa05" +
175
"1ba30b396a0aac973d58e091",
176
"698e57f70e6ecc7fd9463b7260a9ae5f");
177
test.check(5, "b83b533708bf535d0aa6e52980d53b78",
178
"feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",
179
"61353b4c2806934a777ff51fa22a4755" +
180
"699b2a714fcdc6f83766e5f97b6c7423" +
181
"73806900e49f24b22b097544d4896b42" +
182
"4989b5e1ebac0f07c23f4598",
183
"df586bb4c249b92cb6922877e444d37b");
184
i++;
185
}
186
}
187
}
188
189