author | ykantser |
Mon, 04 May 2015 16:30:07 +0200 | |
changeset 30604 | b8d532cb6420 |
parent 27699 | 9913b19c0948 |
child 31404 | 63e8fcd70bfc |
permissions | -rw-r--r-- |
14132 | 1 |
/* |
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
27699
diff
changeset
|
2 |
* Copyright (c) 2012, 2015, 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 |
*/ |
|
24 |
||
25 |
/** |
|
26 |
* @author Tom Deneau |
|
27 |
*/ |
|
28 |
||
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
27699
diff
changeset
|
29 |
import jdk.test.lib.Utils; |
27453
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
24328
diff
changeset
|
30 |
import java.security.AlgorithmParameters; |
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
24328
diff
changeset
|
31 |
import java.util.Random; |
14132 | 32 |
import javax.crypto.Cipher; |
33 |
import javax.crypto.SecretKey; |
|
34 |
import javax.crypto.spec.IvParameterSpec; |
|
35 |
import javax.crypto.spec.SecretKeySpec; |
|
36 |
||
37 |
abstract public class TestAESBase { |
|
38 |
int msgSize = Integer.getInteger("msgSize", 646); |
|
39 |
boolean checkOutput = Boolean.getBoolean("checkOutput"); |
|
40 |
boolean noReinit = Boolean.getBoolean("noReinit"); |
|
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
41 |
boolean testingMisalignment; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
42 |
private static final int ALIGN = 8; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
43 |
int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
44 |
int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
45 |
int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
46 |
int lastChunkSize = Integer.getInteger("lastChunkSize", 32); |
14132 | 47 |
int keySize = Integer.getInteger("keySize", 128); |
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
48 |
int inputLength; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
49 |
int encodeLength; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
50 |
int decodeLength; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
51 |
int decodeMsgSize; |
14132 | 52 |
String algorithm = System.getProperty("algorithm", "AES"); |
53 |
String mode = System.getProperty("mode", "CBC"); |
|
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
54 |
String paddingStr = System.getProperty("paddingStr", "PKCS5Padding"); |
14132 | 55 |
byte[] input; |
56 |
byte[] encode; |
|
57 |
byte[] expectedEncode; |
|
58 |
byte[] decode; |
|
59 |
byte[] expectedDecode; |
|
27453
9aeb9b97bef6
8044186: Introduce a reproducible random generator
iignatyev
parents:
24328
diff
changeset
|
60 |
final Random random = Utils.getRandomInstance(); |
14132 | 61 |
Cipher cipher; |
62 |
Cipher dCipher; |
|
63 |
AlgorithmParameters algParams; |
|
64 |
SecretKey key; |
|
65 |
||
66 |
static int numThreads = 0; |
|
67 |
int threadId; |
|
68 |
static synchronized int getThreadId() { |
|
69 |
int id = numThreads; |
|
70 |
numThreads++; |
|
71 |
return id; |
|
72 |
} |
|
73 |
||
74 |
abstract public void run(); |
|
75 |
||
76 |
public void prepare() { |
|
77 |
try { |
|
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
78 |
System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", paddingStr=" + paddingStr + ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + ", checkOutput=" + checkOutput + ", encInputOffset=" + encInputOffset + ", encOutputOffset=" + encOutputOffset + ", decOutputOffset=" + decOutputOffset + ", lastChunkSize=" +lastChunkSize ); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
79 |
|
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
80 |
if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 ) |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
81 |
testingMisalignment = true; |
14132 | 82 |
|
83 |
int keyLenBytes = (keySize == 0 ? 16 : keySize/8); |
|
84 |
byte keyBytes[] = new byte[keyLenBytes]; |
|
85 |
if (keySize == 128) |
|
86 |
keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; |
|
87 |
else |
|
88 |
random.nextBytes(keyBytes); |
|
89 |
||
90 |
key = new SecretKeySpec(keyBytes, algorithm); |
|
91 |
if (threadId == 0) { |
|
92 |
System.out.println("Algorithm: " + key.getAlgorithm() + "(" |
|
93 |
+ key.getEncoded().length * 8 + "bit)"); |
|
94 |
} |
|
95 |
||
96 |
cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
|
97 |
dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
|
98 |
||
14834 | 99 |
if (mode.equals("CBC")) { |
100 |
int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0); |
|
101 |
IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]); |
|
102 |
cipher.init(Cipher.ENCRYPT_MODE, key, initVector); |
|
103 |
} else { |
|
104 |
algParams = cipher.getParameters(); |
|
105 |
cipher.init(Cipher.ENCRYPT_MODE, key, algParams); |
|
106 |
} |
|
14132 | 107 |
algParams = cipher.getParameters(); |
108 |
dCipher.init(Cipher.DECRYPT_MODE, key, algParams); |
|
109 |
if (threadId == 0) { |
|
110 |
childShowCipher(); |
|
111 |
} |
|
112 |
||
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
113 |
inputLength = msgSize + encInputOffset; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
114 |
if (testingMisalignment) { |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
115 |
encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
116 |
encodeLength += cipher.getOutputSize(lastChunkSize); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
117 |
decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
118 |
decodeLength += dCipher.getOutputSize(lastChunkSize); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
119 |
} else { |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
120 |
encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
121 |
decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
122 |
} |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
123 |
|
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
124 |
input = new byte[inputLength]; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
125 |
for (int i=encInputOffset, j=0; i<inputLength; i++, j++) { |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
126 |
input[i] = (byte) (j & 0xff); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
127 |
} |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
128 |
|
14132 | 129 |
// do one encode and decode in preparation |
24328
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
130 |
encode = new byte[encodeLength]; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
131 |
decode = new byte[decodeLength]; |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
132 |
if (testingMisalignment) { |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
133 |
decodeMsgSize = cipher.update(input, encInputOffset, (msgSize - lastChunkSize), encode, encOutputOffset); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
134 |
decodeMsgSize += cipher.doFinal(input, (encInputOffset + msgSize - lastChunkSize), lastChunkSize, encode, (encOutputOffset + decodeMsgSize)); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
135 |
|
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
136 |
int tempSize = dCipher.update(encode, encOutputOffset, (decodeMsgSize - lastChunkSize), decode, decOutputOffset); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
137 |
dCipher.doFinal(encode, (encOutputOffset + decodeMsgSize - lastChunkSize), lastChunkSize, decode, (decOutputOffset + tempSize)); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
138 |
} else { |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
139 |
decodeMsgSize = cipher.doFinal(input, encInputOffset, msgSize, encode, encOutputOffset); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
140 |
dCipher.doFinal(encode, encOutputOffset, decodeMsgSize, decode, decOutputOffset); |
bddefb356fba
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
14834
diff
changeset
|
141 |
} |
14132 | 142 |
if (checkOutput) { |
143 |
expectedEncode = (byte[]) encode.clone(); |
|
144 |
expectedDecode = (byte[]) decode.clone(); |
|
145 |
showArray(key.getEncoded() , "key: "); |
|
146 |
showArray(input, "input: "); |
|
147 |
showArray(encode, "encode: "); |
|
148 |
showArray(decode, "decode: "); |
|
149 |
} |
|
150 |
} |
|
151 |
catch (Exception e) { |
|
152 |
e.printStackTrace(); |
|
153 |
System.exit(1); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
void showArray(byte b[], String name) { |
|
158 |
System.out.format("%s [%d]: ", name, b.length); |
|
159 |
for (int i=0; i<Math.min(b.length, 32); i++) { |
|
160 |
System.out.format("%02x ", b[i] & 0xff); |
|
161 |
} |
|
162 |
System.out.println(); |
|
163 |
} |
|
164 |
||
165 |
void compareArrays(byte b[], byte exp[]) { |
|
166 |
if (b.length != exp.length) { |
|
167 |
System.out.format("different lengths for actual and expected output arrays\n"); |
|
168 |
showArray(b, "test: "); |
|
169 |
showArray(exp, "exp : "); |
|
170 |
System.exit(1); |
|
171 |
} |
|
172 |
for (int i=0; i< exp.length; i++) { |
|
173 |
if (b[i] != exp[i]) { |
|
174 |
System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff); |
|
175 |
showArray(b, "test: "); |
|
176 |
showArray(exp, "exp : "); |
|
177 |
System.exit(1); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
||
183 |
void showCipher(Cipher c, String kind) { |
|
184 |
System.out.println(kind + " cipher provider: " + cipher.getProvider()); |
|
185 |
System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm()); |
|
186 |
} |
|
187 |
||
188 |
abstract void childShowCipher(); |
|
189 |
} |