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