kvn@4205: /* kvn@6653: * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. kvn@4205: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. kvn@4205: * kvn@4205: * This code is free software; you can redistribute it and/or modify it kvn@4205: * under the terms of the GNU General Public License version 2 only, as kvn@4205: * published by the Free Software Foundation. kvn@4205: * kvn@4205: * This code is distributed in the hope that it will be useful, but WITHOUT kvn@4205: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or kvn@4205: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License kvn@4205: * version 2 for more details (a copy is included in the LICENSE file that kvn@4205: * accompanied this code). kvn@4205: * kvn@4205: * You should have received a copy of the GNU General Public License version kvn@4205: * 2 along with this work; if not, write to the Free Software Foundation, kvn@4205: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. kvn@4205: * kvn@4205: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA kvn@4205: * or visit www.oracle.com if you need additional information or have any kvn@4205: * questions. kvn@4205: * kvn@4205: */ kvn@4205: kvn@4205: /** kvn@4205: * @author Tom Deneau kvn@4205: */ kvn@4205: kvn@4205: import javax.crypto.Cipher; kvn@4205: import javax.crypto.KeyGenerator; kvn@4205: import javax.crypto.SecretKey; kvn@4205: import javax.crypto.spec.IvParameterSpec; kvn@4205: import javax.crypto.spec.SecretKeySpec; kvn@4205: import java.security.AlgorithmParameters; kvn@4205: kvn@4205: import java.util.Random; kvn@4205: import java.util.Arrays; kvn@4205: kvn@4205: abstract public class TestAESBase { kvn@4205: int msgSize = Integer.getInteger("msgSize", 646); kvn@4205: boolean checkOutput = Boolean.getBoolean("checkOutput"); kvn@4205: boolean noReinit = Boolean.getBoolean("noReinit"); kvn@6653: boolean testingMisalignment; kvn@6653: private static final int ALIGN = 8; kvn@6653: int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN; kvn@6653: int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN; kvn@6653: int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN; kvn@6653: int lastChunkSize = Integer.getInteger("lastChunkSize", 32); kvn@4205: int keySize = Integer.getInteger("keySize", 128); kvn@6653: int inputLength; kvn@6653: int encodeLength; kvn@6653: int decodeLength; kvn@6653: int decodeMsgSize; kvn@4205: String algorithm = System.getProperty("algorithm", "AES"); kvn@4205: String mode = System.getProperty("mode", "CBC"); kvn@6653: String paddingStr = System.getProperty("paddingStr", "PKCS5Padding"); kvn@4205: byte[] input; kvn@4205: byte[] encode; kvn@4205: byte[] expectedEncode; kvn@4205: byte[] decode; kvn@4205: byte[] expectedDecode; kvn@4205: Random random = new Random(0); kvn@4205: Cipher cipher; kvn@4205: Cipher dCipher; kvn@4205: AlgorithmParameters algParams; kvn@4205: SecretKey key; kvn@4205: kvn@4205: static int numThreads = 0; kvn@4205: int threadId; kvn@4205: static synchronized int getThreadId() { kvn@4205: int id = numThreads; kvn@4205: numThreads++; kvn@4205: return id; kvn@4205: } kvn@4205: kvn@4205: abstract public void run(); kvn@4205: kvn@4205: public void prepare() { kvn@4205: try { kvn@6653: 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 ); kvn@6653: kvn@6653: if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 ) kvn@6653: testingMisalignment = true; kvn@4205: kvn@4205: int keyLenBytes = (keySize == 0 ? 16 : keySize/8); kvn@4205: byte keyBytes[] = new byte[keyLenBytes]; kvn@4205: if (keySize == 128) kvn@4205: keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; kvn@4205: else kvn@4205: random.nextBytes(keyBytes); kvn@4205: kvn@4205: key = new SecretKeySpec(keyBytes, algorithm); kvn@4205: if (threadId == 0) { kvn@4205: System.out.println("Algorithm: " + key.getAlgorithm() + "(" kvn@4205: + key.getEncoded().length * 8 + "bit)"); kvn@4205: } kvn@4205: kvn@4205: cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); kvn@4205: dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); kvn@4205: kvn@4363: if (mode.equals("CBC")) { kvn@4363: int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0); kvn@4363: IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]); kvn@4363: cipher.init(Cipher.ENCRYPT_MODE, key, initVector); kvn@4363: } else { kvn@4363: algParams = cipher.getParameters(); kvn@4363: cipher.init(Cipher.ENCRYPT_MODE, key, algParams); kvn@4363: } kvn@4205: algParams = cipher.getParameters(); kvn@4205: dCipher.init(Cipher.DECRYPT_MODE, key, algParams); kvn@4205: if (threadId == 0) { kvn@4205: childShowCipher(); kvn@4205: } kvn@4205: kvn@6653: inputLength = msgSize + encInputOffset; kvn@6653: if (testingMisalignment) { kvn@6653: encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset; kvn@6653: encodeLength += cipher.getOutputSize(lastChunkSize); kvn@6653: decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset; kvn@6653: decodeLength += dCipher.getOutputSize(lastChunkSize); kvn@6653: } else { kvn@6653: encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset; kvn@6653: decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset; kvn@6653: } kvn@6653: kvn@6653: input = new byte[inputLength]; kvn@6653: for (int i=encInputOffset, j=0; i