Mercurial > hg > truffle
annotate test/compiler/7184394/TestAESBase.java @ 18267:fa6c97ede679
added Remote interface and applied it to API types that will be proxied for the purpose of replay/remote compilation
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Wed, 05 Nov 2014 22:18:55 +0100 |
parents | 03214612e77e |
children |
rev | line source |
---|---|
6894 | 1 /* |
17910
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
6894 | 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 | |
29 import javax.crypto.Cipher; | |
30 import javax.crypto.KeyGenerator; | |
31 import javax.crypto.SecretKey; | |
32 import javax.crypto.spec.IvParameterSpec; | |
33 import javax.crypto.spec.SecretKeySpec; | |
34 import java.security.AlgorithmParameters; | |
35 | |
36 import java.util.Random; | |
37 import java.util.Arrays; | |
38 | |
39 abstract public class TestAESBase { | |
40 int msgSize = Integer.getInteger("msgSize", 646); | |
41 boolean checkOutput = Boolean.getBoolean("checkOutput"); | |
42 boolean noReinit = Boolean.getBoolean("noReinit"); | |
17910
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
43 boolean testingMisalignment; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
44 private static final int ALIGN = 8; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
45 int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
46 int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
47 int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
48 int lastChunkSize = Integer.getInteger("lastChunkSize", 32); |
6894 | 49 int keySize = Integer.getInteger("keySize", 128); |
17910
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
50 int inputLength; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
51 int encodeLength; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
52 int decodeLength; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
53 int decodeMsgSize; |
6894 | 54 String algorithm = System.getProperty("algorithm", "AES"); |
55 String mode = System.getProperty("mode", "CBC"); | |
17910
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
56 String paddingStr = System.getProperty("paddingStr", "PKCS5Padding"); |
6894 | 57 byte[] input; |
58 byte[] encode; | |
59 byte[] expectedEncode; | |
60 byte[] decode; | |
61 byte[] expectedDecode; | |
62 Random random = new Random(0); | |
63 Cipher cipher; | |
64 Cipher dCipher; | |
65 AlgorithmParameters algParams; | |
66 SecretKey key; | |
67 | |
68 static int numThreads = 0; | |
69 int threadId; | |
70 static synchronized int getThreadId() { | |
71 int id = numThreads; | |
72 numThreads++; | |
73 return id; | |
74 } | |
75 | |
76 abstract public void run(); | |
77 | |
78 public void prepare() { | |
79 try { | |
17910
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
80 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 ); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
81 |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
82 if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 ) |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
83 testingMisalignment = true; |
6894 | 84 |
85 int keyLenBytes = (keySize == 0 ? 16 : keySize/8); | |
86 byte keyBytes[] = new byte[keyLenBytes]; | |
87 if (keySize == 128) | |
88 keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; | |
89 else | |
90 random.nextBytes(keyBytes); | |
91 | |
92 key = new SecretKeySpec(keyBytes, algorithm); | |
93 if (threadId == 0) { | |
94 System.out.println("Algorithm: " + key.getAlgorithm() + "(" | |
95 + key.getEncoded().length * 8 + "bit)"); | |
96 } | |
97 | |
98 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); | |
99 dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); | |
100 | |
7427 | 101 if (mode.equals("CBC")) { |
102 int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0); | |
103 IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]); | |
104 cipher.init(Cipher.ENCRYPT_MODE, key, initVector); | |
105 } else { | |
106 algParams = cipher.getParameters(); | |
107 cipher.init(Cipher.ENCRYPT_MODE, key, algParams); | |
108 } | |
6894 | 109 algParams = cipher.getParameters(); |
110 dCipher.init(Cipher.DECRYPT_MODE, key, algParams); | |
111 if (threadId == 0) { | |
112 childShowCipher(); | |
113 } | |
114 | |
17910
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
115 inputLength = msgSize + encInputOffset; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
116 if (testingMisalignment) { |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
117 encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
118 encodeLength += cipher.getOutputSize(lastChunkSize); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
119 decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
120 decodeLength += dCipher.getOutputSize(lastChunkSize); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
121 } else { |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
122 encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
123 decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
124 } |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
125 |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
126 input = new byte[inputLength]; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
127 for (int i=encInputOffset, j=0; i<inputLength; i++, j++) { |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
128 input[i] = (byte) (j & 0xff); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
129 } |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
130 |
6894 | 131 // do one encode and decode in preparation |
17910
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
132 encode = new byte[encodeLength]; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
133 decode = new byte[decodeLength]; |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
134 if (testingMisalignment) { |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
135 decodeMsgSize = cipher.update(input, encInputOffset, (msgSize - lastChunkSize), encode, encOutputOffset); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
136 decodeMsgSize += cipher.doFinal(input, (encInputOffset + msgSize - lastChunkSize), lastChunkSize, encode, (encOutputOffset + decodeMsgSize)); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
137 |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
138 int tempSize = dCipher.update(encode, encOutputOffset, (decodeMsgSize - lastChunkSize), decode, decOutputOffset); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
139 dCipher.doFinal(encode, (encOutputOffset + decodeMsgSize - lastChunkSize), lastChunkSize, decode, (decOutputOffset + tempSize)); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
140 } else { |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
141 decodeMsgSize = cipher.doFinal(input, encInputOffset, msgSize, encode, encOutputOffset); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
142 dCipher.doFinal(encode, encOutputOffset, decodeMsgSize, decode, decOutputOffset); |
03214612e77e
8035936: SIGBUS in StubRoutines::aesencryptBlock, solaris-sparc
kvn
parents:
7427
diff
changeset
|
143 } |
6894 | 144 if (checkOutput) { |
145 expectedEncode = (byte[]) encode.clone(); | |
146 expectedDecode = (byte[]) decode.clone(); | |
147 showArray(key.getEncoded() , "key: "); | |
148 showArray(input, "input: "); | |
149 showArray(encode, "encode: "); | |
150 showArray(decode, "decode: "); | |
151 } | |
152 } | |
153 catch (Exception e) { | |
154 e.printStackTrace(); | |
155 System.exit(1); | |
156 } | |
157 } | |
158 | |
159 void showArray(byte b[], String name) { | |
160 System.out.format("%s [%d]: ", name, b.length); | |
161 for (int i=0; i<Math.min(b.length, 32); i++) { | |
162 System.out.format("%02x ", b[i] & 0xff); | |
163 } | |
164 System.out.println(); | |
165 } | |
166 | |
167 void compareArrays(byte b[], byte exp[]) { | |
168 if (b.length != exp.length) { | |
169 System.out.format("different lengths for actual and expected output arrays\n"); | |
170 showArray(b, "test: "); | |
171 showArray(exp, "exp : "); | |
172 System.exit(1); | |
173 } | |
174 for (int i=0; i< exp.length; i++) { | |
175 if (b[i] != exp[i]) { | |
176 System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff); | |
177 showArray(b, "test: "); | |
178 showArray(exp, "exp : "); | |
179 System.exit(1); | |
180 } | |
181 } | |
182 } | |
183 | |
184 | |
185 void showCipher(Cipher c, String kind) { | |
186 System.out.println(kind + " cipher provider: " + cipher.getProvider()); | |
187 System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm()); | |
188 } | |
189 | |
190 abstract void childShowCipher(); | |
191 } |