Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/KeyWrap/NISTWrapKAT.java
38889 views
1
/*
2
* Copyright (c) 2004, 2007, 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 5008156
27
* @run main NISTWrapKAT
28
* @summary Verify that the "AESWrap" key wrap cipher work as
29
* expected using NIST test vectors.
30
* @author Valerie Peng
31
*/
32
import java.security.Key;
33
import java.security.AlgorithmParameters;
34
import javax.crypto.*;
35
import javax.crypto.spec.*;
36
import java.util.Arrays;
37
import java.math.BigInteger;
38
39
public class NISTWrapKAT {
40
41
private static final String KEK =
42
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
43
private static final String DATA =
44
"00112233445566778899aabbccddeeff000102030405060708090a0b0c0d0e0f";
45
46
private static String AES128_128 =
47
"1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5";
48
private static String AES192_128 =
49
"96778b25ae6ca435f92b5b97c050aed2468ab8a17ad84e5d";
50
private static String AES192_192 =
51
"031d33264e15d33268f24ec260743edce1c6c7ddee725a936ba814915c6762d2";
52
private static String AES256_128 =
53
"64e8c3f9ce0f5ba263e9777905818a2a93c8191e7d6e8ae7";
54
private static String AES256_192 =
55
"a8f9bc1612c68b3ff6e6f4fbe30e71e4769c8b80a32cb8958cd5d17d6b254da1";
56
private static String AES256_256 =
57
"28c9f404c4b810f4cbccb35cfb87f8263f5786e2d80ed326cbc7f0e71a99f43bfb988b9b7a02dd21";
58
59
public static void testKeyWrap(int keyLen, int dataLen,
60
String expected) throws Exception {
61
System.out.println("Testing AESWrap Cipher with " +
62
dataLen + "-byte data with " + 8*keyLen + "-bit key");
63
Cipher c = Cipher.getInstance("AESWrap", "SunJCE");
64
byte[] keyVal = new byte[keyLen];
65
byte[] dataVal = new byte[dataLen];
66
67
// setup the key encryption key and the to-be-wrapped key
68
BigInteger temp = new BigInteger(KEK.substring(0, keyLen*2), 16);
69
byte[] val = temp.toByteArray();
70
System.arraycopy(val, 0, keyVal, keyVal.length-val.length,
71
val.length);
72
temp = new BigInteger(DATA.substring(0, dataLen*2), 16);
73
val = temp.toByteArray();
74
System.arraycopy(val, 0, dataVal, dataVal.length-val.length,
75
val.length);
76
77
SecretKey cipherKey = new SecretKeySpec(keyVal, "AES");
78
c.init(Cipher.WRAP_MODE, cipherKey);
79
SecretKey toBeWrappedKey = new SecretKeySpec(dataVal, "AES");
80
81
// first test WRAP with known values
82
byte[] wrapped = c.wrap(toBeWrappedKey);
83
byte[] expectedVal = new BigInteger(expected, 16).toByteArray();
84
// need to add offset since BigInteger may pad "0x00" in the beginning
85
int offset = expectedVal.length - wrapped.length;
86
for (int i=0; i<wrapped.length; i++) {
87
if (wrapped[i] != expectedVal[offset + i]) {
88
throw new Exception("Wrap failed; got different result");
89
}
90
}
91
92
// then test UNWRAP and compare with the initial values
93
c.init(Cipher.UNWRAP_MODE, cipherKey);
94
Key unwrapped = c.unwrap(wrapped, "AES", Cipher.SECRET_KEY);
95
if (!Arrays.equals(unwrapped.getEncoded(), dataVal)) {
96
throw new Exception("Unwrap failed; got different result");
97
}
98
}
99
100
public static void main(String[] argv) throws Exception {
101
testKeyWrap(16, 16, AES128_128);
102
// only run the tests on longer key lengths if unlimited version
103
// of JCE jurisdiction policy files are installed
104
int allowed = Cipher.getMaxAllowedKeyLength("AES");
105
if (allowed >= 24*8) {
106
testKeyWrap(24, 16, AES192_128);
107
testKeyWrap(24, 24, AES192_192);
108
}
109
if (allowed >= 32*8) {
110
testKeyWrap(32, 16, AES256_128);
111
testKeyWrap(32, 24, AES256_192);
112
testKeyWrap(32, 32, AES256_256);
113
}
114
System.out.println("All Tests Passed");
115
}
116
}
117
118