1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   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");
  43   int keySize = Integer.getInteger("keySize", 128);
  44   String algorithm = System.getProperty("algorithm", "AES");
  45   String mode = System.getProperty("mode", "CBC");
  46   byte[] input;
  47   byte[] encode;
  48   byte[] expectedEncode;
  49   byte[] decode;
  50   byte[] expectedDecode;
  51   Random random = new Random(0);
  52   Cipher cipher;
  53   Cipher dCipher;
  54   String paddingStr = "PKCS5Padding";
  55   AlgorithmParameters algParams;
  56   SecretKey key;
  57 
  58   static int numThreads = 0;
  59   int  threadId;
  60   static synchronized int getThreadId() {
  61     int id = numThreads;
  62     numThreads++;
  63     return id;
  64   }
  65 
  66   abstract public void run();
  67 
  68   public void prepare() {
  69     try {
  70     System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + ", checkOutput=" + checkOutput);
  71 
  72       int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
  73       byte keyBytes[] = new byte[keyLenBytes];
  74       if (keySize == 128)
  75         keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
  76       else
  77         random.nextBytes(keyBytes);
  78 
  79       key = new SecretKeySpec(keyBytes, algorithm);
  80       if (threadId == 0) {
  81         System.out.println("Algorithm: " + key.getAlgorithm() + "("
  82                            + key.getEncoded().length * 8 + "bit)");
  83       }
  84       input = new byte[msgSize];
  85       for (int i=0; i<input.length; i++) {
  86         input[i] = (byte) (i & 0xff);
  87       }
  88 
  89       cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
  90       dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
  91 
  92       if (mode.equals("CBC")) {
  93         int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
  94         IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
  95         cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
  96       } else {
  97         algParams = cipher.getParameters();
  98         cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
  99       }
 100       algParams = cipher.getParameters();
 101       dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
 102       if (threadId == 0) {
 103         childShowCipher();
 104       }
 105 
 106       // do one encode and decode in preparation
 107       // this will also create the encode buffer and decode buffer
 108       encode = cipher.doFinal(input);
 109       decode = dCipher.doFinal(encode);
 110       if (checkOutput) {
 111         expectedEncode = (byte[]) encode.clone();
 112         expectedDecode = (byte[]) decode.clone();
 113         showArray(key.getEncoded()  ,  "key:    ");
 114         showArray(input,  "input:  ");
 115         showArray(encode, "encode: ");
 116         showArray(decode, "decode: ");
 117       }
 118     }
 119     catch (Exception e) {
 120       e.printStackTrace();
 121       System.exit(1);
 122     }
 123   }
 124 
 125   void showArray(byte b[], String name) {
 126     System.out.format("%s [%d]: ", name, b.length);
 127     for (int i=0; i<Math.min(b.length, 32); i++) {
 128       System.out.format("%02x ", b[i] & 0xff);
 129     }
 130     System.out.println();
 131   }
 132 
 133   void compareArrays(byte b[], byte exp[]) {
 134     if (b.length != exp.length) {
 135       System.out.format("different lengths for actual and expected output arrays\n");
 136       showArray(b, "test: ");
 137       showArray(exp, "exp : ");
 138       System.exit(1);
 139     }
 140     for (int i=0; i< exp.length; i++) {
 141       if (b[i] != exp[i]) {
 142         System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff);
 143         showArray(b, "test: ");
 144         showArray(exp, "exp : ");
 145         System.exit(1);
 146       }
 147     }
 148   }
 149 
 150 
 151   void showCipher(Cipher c, String kind) {
 152     System.out.println(kind + " cipher provider: " + cipher.getProvider());
 153     System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
 154   }
 155 
 156   abstract void childShowCipher();
 157 }