1 /*
   2  * Copyright (c) 2012, 2015, 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 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       }
 123 
 124       input = new byte[inputLength];
 125       for (int i=encInputOffset, j=0; i<inputLength; i++, j++) {
 126         input[i] = (byte) (j & 0xff);
 127       }
 128 
 129       // do one encode and decode in preparation
 130       encode = new byte[encodeLength];
 131       decode = new byte[decodeLength];
 132       if (testingMisalignment) {
 133         decodeMsgSize = cipher.update(input, encInputOffset, (msgSize - lastChunkSize), encode, encOutputOffset);
 134         decodeMsgSize += cipher.doFinal(input, (encInputOffset + msgSize - lastChunkSize), lastChunkSize, encode, (encOutputOffset + decodeMsgSize));
 135 
 136         int tempSize = dCipher.update(encode, encOutputOffset, (decodeMsgSize - lastChunkSize), decode, decOutputOffset);
 137         dCipher.doFinal(encode, (encOutputOffset + decodeMsgSize - lastChunkSize), lastChunkSize, decode, (decOutputOffset + tempSize));
 138       } else {
 139         decodeMsgSize = cipher.doFinal(input, encInputOffset, msgSize, encode, encOutputOffset);
 140         dCipher.doFinal(encode, encOutputOffset, decodeMsgSize, decode, decOutputOffset);
 141       }
 142       if (checkOutput) {
 143         expectedEncode = (byte[]) encode.clone();
 144         expectedDecode = (byte[]) decode.clone();
 145         showArray(key.getEncoded()  ,  "key:    ");
 146         showArray(input,  "input:  ");
 147         showArray(encode, "encode: ");
 148         showArray(decode, "decode: ");
 149       }
 150     }
 151     catch (Exception e) {
 152       e.printStackTrace();
 153       System.exit(1);
 154     }
 155   }
 156 
 157   void showArray(byte b[], String name) {
 158     System.out.format("%s [%d]: ", name, b.length);
 159     for (int i=0; i<Math.min(b.length, 32); i++) {
 160       System.out.format("%02x ", b[i] & 0xff);
 161     }
 162     System.out.println();
 163   }
 164 
 165   void compareArrays(byte b[], byte exp[]) {
 166     if (b.length != exp.length) {
 167       System.out.format("different lengths for actual and expected output arrays\n");
 168       showArray(b, "test: ");
 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 }
--- EOF ---