author | jjiang |
Wed, 15 Aug 2018 18:41:18 +0800 | |
changeset 51460 | 97e361fe3433 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
28403 | 1 |
/* |
51460
97e361fe3433
8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. |
28403 | 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 |
||
40975
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
24 |
/* |
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
25 |
* @test |
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
26 |
* @bug 8048603 |
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
27 |
* @summary Check if doFinal and update operation result in same Mac |
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
28 |
* @author Yu-Ching Valerie Peng, Bill Situ, Alexander Fomin |
51460
97e361fe3433
8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents:
47216
diff
changeset
|
29 |
* @library /test/lib .. |
43248
5e15de85a1a0
8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents:
42693
diff
changeset
|
30 |
* @modules jdk.crypto.cryptoki |
40975
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
31 |
* @run main/othervm MacSameTest |
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
32 |
* @run main/othervm MacSameTest sm |
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
33 |
* @key randomness |
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
34 |
*/ |
680639c9b307
8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents:
35379
diff
changeset
|
35 |
|
28403 | 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.spec.SecretKeySpec; |
|
44 |
||
45 |
public class MacSameTest extends PKCS11Test { |
|
46 |
||
47 |
private static final int MESSAGE_SIZE = 25; |
|
48 |
private static final int OFFSET = 5; |
|
49 |
private static final int KEY_SIZE = 70; |
|
50 |
||
51 |
/** |
|
52 |
* Initialize a message, instantiate a Mac object, |
|
53 |
* initialize the object with a SecretKey, |
|
54 |
* feed the message into the Mac object |
|
55 |
* all at once and get the output MAC as result1. |
|
56 |
* Reset the Mac object, chop the message into three pieces, |
|
57 |
* feed into the Mac object sequentially, and get the output MAC as result2. |
|
58 |
* Finally, compare result1 and result2 and see if they are the same. |
|
59 |
* |
|
60 |
* @param args the command line arguments |
|
61 |
*/ |
|
62 |
public static void main(String[] args) throws Exception { |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
30046
diff
changeset
|
63 |
main(new MacSameTest(), args); |
28403 | 64 |
} |
65 |
||
66 |
@Override |
|
67 |
public void main(Provider p) { |
|
68 |
List<String> algorithms = getSupportedAlgorithms("Mac", "Hmac", p); |
|
69 |
boolean success = true; |
|
70 |
for (String alg : algorithms) { |
|
71 |
try { |
|
72 |
doTest(alg, p); |
|
73 |
} catch (Exception e) { |
|
74 |
System.out.println("Unexpected exception: " + e); |
|
75 |
e.printStackTrace(); |
|
76 |
success = false; |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
if (!success) { |
|
81 |
throw new RuntimeException("Test failed"); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
private void doTest(String algo, Provider provider) |
|
86 |
throws NoSuchAlgorithmException, NoSuchProviderException, |
|
87 |
InvalidKeyException { |
|
88 |
System.out.println("Test " + algo); |
|
89 |
Mac mac; |
|
90 |
try { |
|
91 |
mac = Mac.getInstance(algo, provider); |
|
92 |
} catch (NoSuchAlgorithmException nsae) { |
|
93 |
if ("SunPKCS11-Solaris".equals(provider.getName())) { |
|
94 |
// depending on Solaris configuration, |
|
95 |
// it can support HMAC or not with Mac |
|
96 |
System.out.println("Expected NoSuchAlgorithmException thrown: " |
|
97 |
+ nsae); |
|
98 |
return; |
|
99 |
} |
|
100 |
throw nsae; |
|
101 |
} |
|
102 |
||
103 |
byte[] plain = new byte[MESSAGE_SIZE]; |
|
104 |
for (int i = 0; i < MESSAGE_SIZE; i++) { |
|
105 |
plain[i] = (byte) (i % 256); |
|
106 |
} |
|
107 |
||
108 |
byte[] tail = new byte[plain.length - OFFSET]; |
|
109 |
System.arraycopy(plain, OFFSET, tail, 0, tail.length); |
|
110 |
||
111 |
SecureRandom srdm = new SecureRandom(); |
|
112 |
byte[] keyVal = new byte[KEY_SIZE]; |
|
113 |
srdm.nextBytes(keyVal); |
|
114 |
SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC"); |
|
115 |
||
116 |
mac.init(keySpec); |
|
117 |
byte[] result1 = mac.doFinal(plain); |
|
118 |
||
119 |
mac.reset(); |
|
120 |
mac.update(plain[0]); |
|
121 |
mac.update(plain, 1, OFFSET - 1); |
|
122 |
byte[] result2 = mac.doFinal(tail); |
|
123 |
||
124 |
if (!java.util.Arrays.equals(result1, result2)) { |
|
125 |
throw new RuntimeException("result1 and result2 are not the same"); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
} |