Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Mac/MacKAT.java
38855 views
1
/*
2
* Copyright (c) 2003, 2016, 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 java.io.UnsupportedEncodingException;
25
import java.security.Provider;
26
import java.util.Arrays;
27
import java.util.List;
28
import java.util.Random;
29
import javax.crypto.Mac;
30
import javax.crypto.SecretKey;
31
import javax.crypto.spec.SecretKeySpec;
32
33
/**
34
* @test
35
* @bug 4846410 6313661 4963723
36
* @summary Basic known-answer-test for Hmac algorithms
37
* @author Andreas Sterbenz
38
* @library ..
39
* @run main/othervm MacKAT
40
* @run main/othervm MacKAT sm
41
*/
42
public class MacKAT extends PKCS11Test {
43
44
private final static byte[] ALONG, BLONG, BKEY, BKEY_20, DDDATA_50,
45
AAKEY_20, CDDATA_50, AAKEY_131;
46
47
static {
48
ALONG = new byte[1024 * 128];
49
Arrays.fill(ALONG, (byte)'a');
50
BLONG = new byte[1024 * 128];
51
Random random = new Random(12345678);
52
random.nextBytes(BLONG);
53
BKEY = new byte[128];
54
random.nextBytes(BKEY);
55
BKEY_20 = new byte[20];
56
Arrays.fill(BKEY_20, (byte) 0x0b);
57
DDDATA_50 = new byte[50];
58
Arrays.fill(DDDATA_50, (byte) 0xdd);
59
AAKEY_20 = new byte[20];
60
Arrays.fill(AAKEY_20, (byte) 0xaa);
61
CDDATA_50 = new byte[50];
62
Arrays.fill(CDDATA_50, (byte) 0xcd);
63
AAKEY_131 = new byte[131];
64
Arrays.fill(AAKEY_131, (byte) 0xaa);
65
}
66
67
private final static Test[] tests = {
68
newMacTest("SslMacMD5",
69
ALONG,
70
"f4:ad:01:71:51:f6:89:56:72:a3:32:bf:d9:2a:f2:a5",
71
"1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
72
newMacTest("SslMacMD5",
73
BLONG,
74
"34:1c:ad:a0:95:57:32:f8:8e:80:8f:ee:b2:d8:23:e5",
75
"76:00:4a:72:98:9b:65:ec:2e:f1:43:c4:65:4a:13:71"),
76
newMacTest("SslMacSHA1",
77
ALONG,
78
"11:c1:71:2e:61:be:4b:cf:bc:6d:e2:4c:58:ae:27:30:0b:24:a4:87",
79
"23:ae:dd:61:87:6c:7a:45:47:2f:2c:8f:ea:64:99:3e:27:5f:97:a5"),
80
newMacTest("SslMacSHA1",
81
BLONG,
82
"84:af:57:0a:af:ef:16:93:90:50:da:88:f8:ad:1a:c5:66:6c:94:d0",
83
"9b:bb:e2:aa:9b:28:1c:95:0e:ea:30:21:98:a5:7e:31:9e:bf:5f:51"),
84
newMacTest("HmacMD5",
85
ALONG,
86
"76:00:4a:72:98:9b:65:ec:2e:f1:43:c4:65:4a:13:71",
87
"1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
88
newMacTest("HmacMD5",
89
BLONG,
90
"6c:22:79:bb:34:9e:da:f4:f5:cf:df:0c:62:3d:59:e0",
91
"76:00:4a:72:98:9b:65:ec:2e:f1:43:c4:65:4a:13:71"),
92
newMacTest("HmacMD5",
93
BLONG,
94
"e6:ad:00:c9:49:6b:98:fe:53:a2:b9:2d:7d:41:a2:03",
95
BKEY),
96
newMacTest("HmacSHA1",
97
ALONG,
98
"9e:b3:6e:35:fa:fb:17:2e:2b:f3:b0:4a:9d:38:83:c4:5f:6d:d9:00",
99
"1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
100
newMacTest("HmacSHA1",
101
BLONG,
102
"80:2d:5b:ea:08:df:a4:1f:e5:3e:1c:fa:fc:ad:dd:31:da:15:60:2c",
103
"76:00:4a:72:98:9b:65:ec:2e:f1:43:c4:65:4a:13:71"),
104
newMacTest("HmacSHA1",
105
BLONG,
106
"a2:fa:2a:85:18:0e:94:b2:a5:e2:17:8b:2a:29:7a:95:cd:e8:aa:82",
107
BKEY),
108
newMacTest("HmacSHA256",
109
ALONG,
110
"3f:6d:08:df:0c:90:b0:e9:ed:13:4a:2e:c3:48:1d:3d:3e:61:2e:f1:"
111
+ "30:c2:63:c4:58:57:03:c2:cb:87:15:07",
112
"1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
113
newMacTest("HmacSHA256",
114
BLONG,
115
"e2:4e:a3:b9:0b:b8:99:e4:71:cf:ca:9f:f8:4e:f0:34:8b:19:9f:33:"
116
+ "4b:1a:b7:13:f7:c8:57:92:e3:03:74:78",
117
BKEY),
118
newMacTest("HmacSHA384",
119
ALONG,
120
"d0:f0:d4:54:1c:0a:6d:81:ed:15:20:d7:0c:96:06:61:a0:ff:c9:ff:"
121
+ "91:e9:a0:cd:e2:45:64:9d:93:4c:a9:fa:89:ae:c0:90:e6:"
122
+ "0b:a1:a0:56:80:57:3b:ed:4b:b0:71",
123
"1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
124
newMacTest("HmacSHA384",
125
BLONG,
126
"75:c4:ca:c7:f7:58:9d:d3:23:b1:1b:5c:93:2d:ec:7a:03:dc:8c:eb:"
127
+ "8d:fe:79:46:4f:30:e7:99:62:de:44:e2:38:95:0e:79:91:"
128
+ "78:2f:a4:05:0a:f0:17:10:38:a1:8e",
129
BKEY),
130
newMacTest("HmacSHA512",
131
ALONG,
132
"41:ea:4c:e5:31:3f:7c:18:0e:5e:95:a9:25:0a:10:58:e6:40:53:88:"
133
+ "82:4f:5a:da:6f:29:de:04:7b:8e:d7:ed:7c:4d:b8:2a:48:"
134
+ "2d:17:2a:2d:59:bb:81:9c:bf:33:40:04:77:44:fb:45:25:"
135
+ "1f:fd:b9:29:f4:a6:69:a3:43:6f",
136
"1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
137
newMacTest("HmacSHA512",
138
BLONG,
139
"fb:cf:4b:c6:d5:49:5a:5b:0b:d9:2a:32:f5:fa:68:d2:68:a4:0f:ae:"
140
+ "53:fc:49:12:e6:1d:53:cf:b2:cb:c5:c5:f2:2d:86:bd:14:"
141
+ "61:30:c3:a6:6f:44:1f:77:9b:aa:a1:22:48:a9:dd:d0:45:"
142
+ "86:d1:a1:82:53:13:c4:03:06:a3",
143
BKEY),
144
145
// Test vectors From RFC 4231
146
newMacTest("HmacSHA224",
147
bytes("Hi There"),
148
"89:6f:b1:12:8a:bb:df:19:68:32:10:7c:d4:9d:f3:3f:47:b4:b1:16:"
149
+ "99:12:ba:4f:53:68:4b:22",
150
BKEY_20),
151
newMacTest("HmacSHA224",
152
bytes("what do ya want for nothing?"),
153
"a3:0e:01:09:8b:c6:db:bf:45:69:0f:3a:7e:9e:6d:0f:8b:be:a2:a3:"
154
+ "9e:61:48:00:8f:d0:5e:44",
155
bytes("Jefe")),
156
newMacTest("HmacSHA224",
157
DDDATA_50,
158
"7f:b3:cb:35:88:c6:c1:f6:ff:a9:69:4d:7d:6a:d2:64:93:65:b0:c1:"
159
+ "f6:5d:69:d1:ec:83:33:ea",
160
AAKEY_20),
161
newMacTest("HmacSHA224",
162
CDDATA_50,
163
"6c:11:50:68:74:01:3c:ac:6a:2a:bc:1b:b3:82:62:7c:ec:6a:90:d8:"
164
+ "6e:fc:01:2d:e7:af:ec:5a",
165
"01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f:10:11:12:13:14:"
166
+ "15:16:17:18:19"),
167
newMacTest("HmacSHA224",
168
bytes("Test Using Larger Than Block-Size Key - Hash Key First"),
169
"95:e9:a0:db:96:20:95:ad:ae:be:9b:2d:6f:0d:bc:e2:d4:99:f1:12:"
170
+ "f2:d2:b7:27:3f:a6:87:0e",
171
AAKEY_131),
172
newMacTest("HmacSHA224",
173
bytes("This is a test using a larger than block-size key and "
174
+ "a larger than block-size data. The key needs to be "
175
+ "hashed before being used by the HMAC algorithm."),
176
"3a:85:41:66:ac:5d:9f:02:3f:54:d5:17:d0:b3:9d:bd:94:67:70:db:"
177
+ "9c:2b:95:c9:f6:f5:65:d1",
178
AAKEY_131),
179
};
180
181
public static void main(String[] args) throws Exception {
182
main(new MacKAT(), args);
183
}
184
185
@Override
186
public void main(Provider p) throws Exception {
187
long start = System.currentTimeMillis();
188
189
List<String> algorithms = getSupportedAlgorithms("Mac", "", p);
190
for (Test test : tests) {
191
if(!algorithms.contains(test.getAlg())) {
192
continue;
193
}
194
test.run(p);
195
}
196
197
System.out.println("All tests passed");
198
long stop = System.currentTimeMillis();
199
System.out.println("Done (" + (stop - start) + " ms).");
200
}
201
202
private static byte[] bytes(String s) {
203
try {
204
return s.getBytes("UTF8");
205
} catch (UnsupportedEncodingException e) {
206
throw new RuntimeException(e);
207
}
208
}
209
210
private static Test newMacTest(String alg, byte[] input, String macvalue,
211
String key) {
212
return new MacTest(alg, input, parse(macvalue), parse(key));
213
}
214
215
private static Test newMacTest(String alg, byte[] input, String macvalue,
216
byte[] key) {
217
return new MacTest(alg, input, parse(macvalue), key);
218
}
219
220
interface Test {
221
void run(Provider p) throws Exception;
222
String getAlg();
223
}
224
225
static class MacTest implements Test {
226
private final String alg;
227
private final byte[] input;
228
private final byte[] macvalue;
229
private final byte[] key;
230
231
MacTest(String alg, byte[] input, byte[] macvalue, byte[] key) {
232
this.alg = alg;
233
this.input = input;
234
this.macvalue = macvalue;
235
this.key = key;
236
}
237
238
@Override
239
public String getAlg() {
240
return alg;
241
}
242
243
@Override
244
public void run(Provider p) throws Exception {
245
Mac mac = Mac.getInstance(alg, p);
246
SecretKey keySpec = new SecretKeySpec(key, alg);
247
mac.init(keySpec);
248
mac.update(input);
249
byte[] macv = mac.doFinal();
250
if (Arrays.equals(macvalue, macv) == false) {
251
System.out.println("Mac test for " + alg + " failed:");
252
if (input.length < 256) {
253
System.out.println("input: "
254
+ PKCS11Test.toString(input));
255
}
256
System.out.println("key: " + PKCS11Test.toString(key));
257
System.out.println("macvalue: "
258
+ PKCS11Test.toString(macvalue));
259
System.out.println("calculated: " + PKCS11Test.toString(macv));
260
throw new Exception("Mac test for " + alg + " failed");
261
}
262
System.out.println("passed: " + alg);
263
}
264
}
265
266
}
267
268