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 jdk.test.lib.Utils; 30 import java.security.AlgorithmParameters; 31 import java.util.Random; 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"); 41 boolean testingMisalignment; 42 private static final int ALIGN = 8; 43 int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN; 44 int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN; 45 int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN; 46 int lastChunkSize = Integer.getInteger("lastChunkSize", 32); 47 int keySize = Integer.getInteger("keySize", 128); 48 int inputLength; 49 int encodeLength; 50 int decodeLength; 51 int decodeMsgSize; 52 String algorithm = System.getProperty("algorithm", "AES"); 53 String mode = System.getProperty("mode", "CBC"); 54 String paddingStr = System.getProperty("paddingStr", "PKCS5Padding"); 55 byte[] input; 56 byte[] encode; 57 byte[] expectedEncode; 58 byte[] decode; 59 byte[] expectedDecode; 60 final Random random = Utils.getRandomInstance(); 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 { 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 ); 79 80 if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 ) 81 testingMisalignment = true; 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 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 } 107 algParams = cipher.getParameters(); 108 dCipher.init(Cipher.DECRYPT_MODE, key, algParams); 109 if (threadId == 0) { 110 childShowCipher(); 111 } 112 113 inputLength = msgSize + encInputOffset; 114 if (testingMisalignment) { 115 encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset; 116 encodeLength += cipher.getOutputSize(lastChunkSize); 117 decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset; 118 decodeLength += dCipher.getOutputSize(lastChunkSize); 119 } else { 120 encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset; 121 decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset; 122 } 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 } | 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 jdk.test.lib.Utils; 30 import java.security.AlgorithmParameters; 31 import java.util.Random; 32 import javax.crypto.Cipher; 33 import javax.crypto.SecretKey; 34 import javax.crypto.spec.GCMParameterSpec; 35 import javax.crypto.spec.IvParameterSpec; 36 import javax.crypto.spec.SecretKeySpec; 37 38 abstract public class TestAESBase { 39 int msgSize = Integer.getInteger("msgSize", 646); 40 boolean checkOutput = Boolean.getBoolean("checkOutput"); 41 boolean noReinit = Boolean.getBoolean("noReinit"); 42 boolean testingMisalignment; 43 private static final int ALIGN = 8; 44 int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN; 45 int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN; 46 int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN; 47 int lastChunkSize = Integer.getInteger("lastChunkSize", 32); 48 int keySize = Integer.getInteger("keySize", 128); 49 int inputLength; 50 int encodeLength; 51 int decodeLength; 52 int decodeMsgSize; 53 String algorithm = System.getProperty("algorithm", "AES"); 54 String mode = System.getProperty("mode", "CBC"); 55 String paddingStr = System.getProperty("paddingStr", "PKCS5Padding"); 56 byte[] input; 57 byte[] encode; 58 byte[] expectedEncode; 59 byte[] decode; 60 byte[] expectedDecode; 61 final Random random = Utils.getRandomInstance(); 62 Cipher cipher; 63 Cipher dCipher; 64 AlgorithmParameters algParams; 65 SecretKey key; 66 GCMParameterSpec gcm_spec; 67 byte[] aad; 68 int tlen = 12; 69 byte[] iv; 70 71 static int numThreads = 0; 72 int threadId; 73 static synchronized int getThreadId() { 74 int id = numThreads; 75 numThreads++; 76 return id; 77 } 78 79 abstract public void run(); 80 81 public void prepare() { 82 try { 83 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 ); 84 85 if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 ) 86 testingMisalignment = true; 87 88 int keyLenBytes = (keySize == 0 ? 16 : keySize/8); 89 byte keyBytes[] = new byte[keyLenBytes]; 90 if (keySize == 128) 91 keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; 92 else 93 random.nextBytes(keyBytes); 94 95 key = new SecretKeySpec(keyBytes, algorithm); 96 if (threadId == 0) { 97 System.out.println("Algorithm: " + key.getAlgorithm() + "(" 98 + key.getEncoded().length * 8 + "bit)"); 99 } 100 101 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); 102 dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); 103 104 if (mode.equals("CBC")) { 105 int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0); 106 IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]); 107 cipher.init(Cipher.ENCRYPT_MODE, key, initVector); 108 } else if (mode.equals("GCM")) { 109 iv = new byte[64]; 110 random.nextBytes(iv); 111 aad = new byte[5]; 112 random.nextBytes(aad); 113 gcm_init(); 114 } else { 115 algParams = cipher.getParameters(); 116 cipher.init(Cipher.ENCRYPT_MODE, key, algParams); 117 } 118 algParams = cipher.getParameters(); 119 dCipher.init(Cipher.DECRYPT_MODE, key, algParams); 120 if (threadId == 0) { 121 childShowCipher(); 122 } 123 124 inputLength = msgSize + encInputOffset; 125 if (testingMisalignment) { 126 encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset; 127 encodeLength += cipher.getOutputSize(lastChunkSize); 128 decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset; 129 decodeLength += dCipher.getOutputSize(lastChunkSize); 130 } else { 131 encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset; 132 decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset; 133 } 180 showArray(exp, "exp : "); 181 System.exit(1); 182 } 183 for (int i=0; i< exp.length; i++) { 184 if (b[i] != exp[i]) { 185 System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff); 186 showArray(b, "test: "); 187 showArray(exp, "exp : "); 188 System.exit(1); 189 } 190 } 191 } 192 193 194 void showCipher(Cipher c, String kind) { 195 System.out.println(kind + " cipher provider: " + cipher.getProvider()); 196 System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm()); 197 } 198 199 abstract void childShowCipher(); 200 201 void gcm_init() throws Exception { 202 tlen = 12; 203 gcm_spec = new GCMParameterSpec(tlen * 8, iv); 204 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); 205 cipher.init(Cipher.ENCRYPT_MODE, key, gcm_spec); 206 cipher.update(aad); 207 } 208 } |