Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Mac/MacSameTest.java
41152 views
1
/*
2
* Copyright (c) 1998, 2020, 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 8048603 8242332
27
* @summary Check if doFinal and update operation result in same Mac
28
* @author Yu-Ching Valerie Peng, Bill Situ, Alexander Fomin
29
* @library /test/lib ..
30
* @modules jdk.crypto.cryptoki
31
* @run main/othervm MacSameTest
32
* @run main/othervm -Djava.security.manager=allow MacSameTest sm
33
* @key randomness
34
*/
35
36
import java.security.InvalidKeyException;
37
import java.security.NoSuchAlgorithmException;
38
import java.security.NoSuchProviderException;
39
import java.security.Provider;
40
import java.security.SecureRandom;
41
import java.util.List;
42
import javax.crypto.Mac;
43
import javax.crypto.KeyGenerator;
44
import javax.crypto.SecretKey;
45
import javax.crypto.spec.SecretKeySpec;
46
47
public class MacSameTest extends PKCS11Test {
48
49
private static final int MESSAGE_SIZE = 25;
50
private static final int OFFSET = 5;
51
private static final int KEY_SIZE = 128;
52
53
/**
54
* Initialize a message, instantiate a Mac object,
55
* initialize the object with a SecretKey,
56
* feed the message into the Mac object
57
* all at once and get the output MAC as result1.
58
* Reset the Mac object, chop the message into three pieces,
59
* feed into the Mac object sequentially, and get the output MAC as result2.
60
* Finally, compare result1 and result2 and see if they are the same.
61
*
62
* @param args the command line arguments
63
*/
64
public static void main(String[] args) throws Exception {
65
main(new MacSameTest(), args);
66
}
67
68
@Override
69
public void main(Provider p) {
70
List<String> algorithms = getSupportedAlgorithms("Mac", "Hmac", p);
71
boolean success = true;
72
SecureRandom srdm = new SecureRandom();
73
74
for (String alg : algorithms) {
75
// first try w/ java secret key object
76
byte[] keyVal = new byte[KEY_SIZE];
77
srdm.nextBytes(keyVal);
78
SecretKey skey = new SecretKeySpec(keyVal, alg);
79
80
try {
81
doTest(alg, skey, p);
82
} catch (Exception e) {
83
System.out.println("Unexpected exception: " + e);
84
e.printStackTrace();
85
success = false;
86
}
87
88
try {
89
KeyGenerator kg = KeyGenerator.getInstance(alg, p);
90
kg.init(KEY_SIZE);
91
skey = kg.generateKey();
92
doTest(alg, skey, p);
93
} catch (NoSuchAlgorithmException nsae) {
94
System.out.println("Skip test using native key for " + alg);
95
continue;
96
} catch (Exception e) {
97
System.out.println("Unexpected exception: " + e);
98
e.printStackTrace();
99
success = false;
100
}
101
}
102
103
if (!success) {
104
throw new RuntimeException("Test failed");
105
}
106
}
107
108
private void doTest(String algo, SecretKey key, Provider provider)
109
throws NoSuchAlgorithmException, NoSuchProviderException,
110
InvalidKeyException {
111
System.out.println("Test " + algo);
112
Mac mac = Mac.getInstance(algo, provider);
113
114
byte[] plain = new byte[MESSAGE_SIZE];
115
for (int i = 0; i < MESSAGE_SIZE; i++) {
116
plain[i] = (byte) (i % 256);
117
}
118
119
byte[] tail = new byte[plain.length - OFFSET];
120
System.arraycopy(plain, OFFSET, tail, 0, tail.length);
121
122
mac.init(key);
123
byte[] result1 = mac.doFinal(plain);
124
125
mac.reset();
126
mac.update(plain[0]);
127
mac.update(plain, 1, OFFSET - 1);
128
byte[] result2 = mac.doFinal(tail);
129
130
if (!java.util.Arrays.equals(result1, result2)) {
131
throw new RuntimeException("result1 and result2 are not the same");
132
}
133
}
134
135
}
136
137