author | valeriep |
Mon, 29 Jul 2019 20:18:43 +0000 | |
changeset 57586 | f459f98aa30d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
14132 | 1 |
/* |
57586
f459f98aa30d
8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding
valeriep
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
14132 | 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 |
||
40059 | 24 |
package compiler.codegen.aes; |
14132 | 25 |
|
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
27699
diff
changeset
|
26 |
import jdk.test.lib.Utils; |
40059 | 27 |
|
14132 | 28 |
import javax.crypto.Cipher; |
29 |
import javax.crypto.SecretKey; |
|
31404
63e8fcd70bfc
8073108: Use x86 and SPARC CPU instructions for GHASH acceleration
ascarpino
parents:
30604
diff
changeset
|
30 |
import javax.crypto.spec.GCMParameterSpec; |
14132 | 31 |
import javax.crypto.spec.IvParameterSpec; |
32 |
import javax.crypto.spec.SecretKeySpec; |
|
40059 | 33 |
import java.security.AlgorithmParameters; |
34 |
import java.util.Random; |
|
14132 | 35 |
|
40059 | 36 |
/** |
37 |
* @author Tom Deneau |
|
38 |
*/ |
|
39 |
public abstract class TestAESBase { |
|
40 |
int msgSize = Integer.getInteger("msgSize", 646); |
|
41 |
boolean checkOutput = Boolean.getBoolean("checkOutput"); |
|
42 |
boolean noReinit = Boolean.getBoolean("noReinit"); |
|
43 |
boolean testingMisalignment; |
|
44 |
private static final int ALIGN = 8; |
|
45 |
int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN; |
|
46 |
int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN; |
|
47 |
int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN; |
|
48 |
int lastChunkSize = Integer.getInteger("lastChunkSize", 32); |
|
49 |
int keySize = Integer.getInteger("keySize", 128); |
|
50 |
int inputLength; |
|
51 |
int encodeLength; |
|
52 |
int decodeLength; |
|
53 |
int decodeMsgSize; |
|
54 |
String algorithm = System.getProperty("algorithm", "AES"); |
|
55 |
String mode = System.getProperty("mode", "CBC"); |
|
57586
f459f98aa30d
8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding
valeriep
parents:
47216
diff
changeset
|
56 |
String paddingStr = System.getProperty("paddingStr", |
f459f98aa30d
8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding
valeriep
parents:
47216
diff
changeset
|
57 |
(mode.equals("GCM") || mode.equals("CTR") || mode.equals("CTS")) ? |
f459f98aa30d
8228668: compiler/codegen/aes/TestAESMain.java failed with GCM mode must be used with NoPadding
valeriep
parents:
47216
diff
changeset
|
58 |
"NoPadding" : "PKCS5Padding"); |
40059 | 59 |
byte[] input; |
60 |
byte[] encode; |
|
61 |
byte[] expectedEncode; |
|
62 |
byte[] decode; |
|
63 |
byte[] expectedDecode; |
|
64 |
final Random random = Utils.getRandomInstance(); |
|
65 |
Cipher cipher; |
|
66 |
Cipher dCipher; |
|
67 |
AlgorithmParameters algParams = null; |
|
68 |
SecretKey key; |
|
69 |
GCMParameterSpec gcm_spec; |
|
70 |
byte[] aad = {0x11, 0x22, 0x33, 0x44, 0x55}; |
|
71 |
int tlen = 12; |
|
72 |
byte[] iv = new byte[16]; |
|
73 |
||
74 |
static int numThreads = 0; |
|
75 |
int threadId; |
|
14132 | 76 |
|
40059 | 77 |
static synchronized int getThreadId() { |
78 |
int id = numThreads; |
|
79 |
numThreads++; |
|
80 |
return id; |
|
81 |
} |
|
14132 | 82 |
|
40059 | 83 |
abstract public void run(); |
14132 | 84 |
|
40059 | 85 |
public void prepare() { |
86 |
try { |
|
87 |
System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", paddingStr=" + paddingStr + |
|
88 |
", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + |
|
89 |
", checkOutput=" + checkOutput + ", encInputOffset=" + encInputOffset + ", encOutputOffset=" + |
|
90 |
encOutputOffset + ", decOutputOffset=" + decOutputOffset + ", lastChunkSize=" + lastChunkSize); |
|
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
91 |
|
40059 | 92 |
if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN != 0) |
93 |
testingMisalignment = true; |
|
14132 | 94 |
|
40059 | 95 |
int keyLenBytes = (keySize == 0 ? 16 : keySize / 8); |
96 |
byte keyBytes[] = new byte[keyLenBytes]; |
|
97 |
if (keySize == 128) |
|
98 |
keyBytes = new byte[]{-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; |
|
99 |
else |
|
100 |
random.nextBytes(keyBytes); |
|
101 |
||
102 |
key = new SecretKeySpec(keyBytes, algorithm); |
|
103 |
if (threadId == 0) { |
|
104 |
System.out.println("Algorithm: " + key.getAlgorithm() + "(" |
|
105 |
+ key.getEncoded().length * 8 + "bit)"); |
|
106 |
} |
|
14132 | 107 |
|
40059 | 108 |
cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
109 |
dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
|
14132 | 110 |
|
40059 | 111 |
// CBC or CTR init |
112 |
if (mode.equals("CBC") || mode.equals("CTR")) { |
|
113 |
IvParameterSpec initVector = new IvParameterSpec(iv); |
|
114 |
cipher.init(Cipher.ENCRYPT_MODE, key, initVector); |
|
115 |
algParams = cipher.getParameters(); |
|
116 |
dCipher.init(Cipher.DECRYPT_MODE, key, initVector); |
|
14132 | 117 |
|
40059 | 118 |
// GCM init |
119 |
} else if (mode.equals("GCM")) { |
|
120 |
gcm_init(true); |
|
121 |
gcm_init(false); |
|
31771
c9f593020799
8130341: GHASH 32bit intrinsics has AEADBadTagException
ascarpino
parents:
31404
diff
changeset
|
122 |
|
40059 | 123 |
// ECB init |
124 |
} else { |
|
125 |
cipher.init(Cipher.ENCRYPT_MODE, key, algParams); |
|
126 |
dCipher.init(Cipher.DECRYPT_MODE, key, algParams); |
|
127 |
} |
|
31771
c9f593020799
8130341: GHASH 32bit intrinsics has AEADBadTagException
ascarpino
parents:
31404
diff
changeset
|
128 |
|
40059 | 129 |
if (threadId == 0) { |
130 |
childShowCipher(); |
|
131 |
} |
|
14132 | 132 |
|
40059 | 133 |
inputLength = msgSize + encInputOffset; |
134 |
if (testingMisalignment) { |
|
135 |
encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset; |
|
136 |
encodeLength += cipher.getOutputSize(lastChunkSize); |
|
137 |
decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset; |
|
138 |
decodeLength += dCipher.getOutputSize(lastChunkSize); |
|
139 |
} else { |
|
140 |
encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset; |
|
141 |
decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset; |
|
142 |
} |
|
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
143 |
|
40059 | 144 |
input = new byte[inputLength]; |
145 |
for (int i = encInputOffset, j = 0; i < inputLength; i++, j++) { |
|
146 |
input[i] = (byte) (j & 0xff); |
|
147 |
} |
|
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
148 |
|
40059 | 149 |
// do one encode and decode in preparation |
150 |
encode = new byte[encodeLength]; |
|
151 |
decode = new byte[decodeLength]; |
|
152 |
if (testingMisalignment) { |
|
153 |
decodeMsgSize = cipher.update(input, encInputOffset, (msgSize - lastChunkSize), encode, encOutputOffset); |
|
154 |
decodeMsgSize += cipher.doFinal(input, (encInputOffset + msgSize - lastChunkSize), lastChunkSize, encode, (encOutputOffset + decodeMsgSize)); |
|
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
155 |
|
40059 | 156 |
int tempSize = dCipher.update(encode, encOutputOffset, (decodeMsgSize - lastChunkSize), decode, decOutputOffset); |
157 |
dCipher.doFinal(encode, (encOutputOffset + decodeMsgSize - lastChunkSize), lastChunkSize, decode, (decOutputOffset + tempSize)); |
|
158 |
} else { |
|
159 |
decodeMsgSize = cipher.doFinal(input, encInputOffset, msgSize, encode, encOutputOffset); |
|
160 |
dCipher.doFinal(encode, encOutputOffset, decodeMsgSize, decode, decOutputOffset); |
|
161 |
} |
|
162 |
if (checkOutput) { |
|
163 |
expectedEncode = (byte[]) encode.clone(); |
|
164 |
expectedDecode = (byte[]) decode.clone(); |
|
165 |
showArray(key.getEncoded(), "key: "); |
|
166 |
showArray(input, "input: "); |
|
167 |
showArray(encode, "encode: "); |
|
168 |
showArray(decode, "decode: "); |
|
169 |
} |
|
170 |
} catch (Exception e) { |
|
171 |
e.printStackTrace(); |
|
172 |
System.exit(1); |
|
173 |
} |
|
14132 | 174 |
} |
40059 | 175 |
|
176 |
void showArray(byte b[], String name) { |
|
177 |
System.out.format("%s [%d]: ", name, b.length); |
|
178 |
for (int i = 0; i < Math.min(b.length, 32); i++) { |
|
179 |
System.out.format("%02x ", b[i] & 0xff); |
|
180 |
} |
|
181 |
System.out.println(); |
|
14132 | 182 |
} |
183 |
||
40059 | 184 |
void compareArrays(byte b[], byte exp[]) { |
185 |
if (b.length != exp.length) { |
|
186 |
System.out.format("different lengths for actual and expected output arrays\n"); |
|
187 |
showArray(b, "test: "); |
|
188 |
showArray(exp, "exp : "); |
|
189 |
System.exit(1); |
|
190 |
} |
|
191 |
for (int i = 0; i < exp.length; i++) { |
|
192 |
if (b[i] != exp[i]) { |
|
193 |
System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff); |
|
194 |
showArray(b, "test: "); |
|
195 |
showArray(exp, "exp : "); |
|
196 |
System.exit(1); |
|
197 |
} |
|
198 |
} |
|
14132 | 199 |
} |
200 |
||
40059 | 201 |
void showCipher(Cipher c, String kind) { |
202 |
System.out.println(kind + " cipher provider: " + cipher.getProvider()); |
|
203 |
System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm()); |
|
14132 | 204 |
} |
40059 | 205 |
|
206 |
abstract void childShowCipher(); |
|
207 |
||
208 |
void gcm_init(boolean encrypt) throws Exception { |
|
209 |
gcm_spec = new GCMParameterSpec(tlen * 8, iv); |
|
210 |
if (encrypt) { |
|
211 |
// Get a new instance everytime because of reuse IV restrictions |
|
212 |
cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
|
213 |
cipher.init(Cipher.ENCRYPT_MODE, key, gcm_spec); |
|
214 |
cipher.updateAAD(aad); |
|
215 |
} else { |
|
216 |
dCipher.init(Cipher.DECRYPT_MODE, key, gcm_spec); |
|
217 |
dCipher.updateAAD(aad); |
|
14132 | 218 |
|
219 |
||
40059 | 220 |
} |
31771
c9f593020799
8130341: GHASH 32bit intrinsics has AEADBadTagException
ascarpino
parents:
31404
diff
changeset
|
221 |
} |
14132 | 222 |
} |