test/compiler/7184394/TestAESBase.java

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/7184394/TestAESBase.java	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,191 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +/**
    1.29 + * @author Tom Deneau
    1.30 + */
    1.31 +
    1.32 +import javax.crypto.Cipher;
    1.33 +import javax.crypto.KeyGenerator;
    1.34 +import javax.crypto.SecretKey;
    1.35 +import javax.crypto.spec.IvParameterSpec;
    1.36 +import javax.crypto.spec.SecretKeySpec;
    1.37 +import java.security.AlgorithmParameters;
    1.38 +
    1.39 +import java.util.Random;
    1.40 +import java.util.Arrays;
    1.41 +
    1.42 +abstract public class TestAESBase {
    1.43 +  int msgSize = Integer.getInteger("msgSize", 646);
    1.44 +  boolean checkOutput = Boolean.getBoolean("checkOutput");
    1.45 +  boolean noReinit = Boolean.getBoolean("noReinit");
    1.46 +  boolean testingMisalignment;
    1.47 +  private static final int ALIGN = 8;
    1.48 +  int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN;
    1.49 +  int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN;
    1.50 +  int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN;
    1.51 +  int lastChunkSize = Integer.getInteger("lastChunkSize", 32);
    1.52 +  int keySize = Integer.getInteger("keySize", 128);
    1.53 +  int inputLength;
    1.54 +  int encodeLength;
    1.55 +  int decodeLength;
    1.56 +  int decodeMsgSize;
    1.57 +  String algorithm = System.getProperty("algorithm", "AES");
    1.58 +  String mode = System.getProperty("mode", "CBC");
    1.59 +  String paddingStr = System.getProperty("paddingStr", "PKCS5Padding");
    1.60 +  byte[] input;
    1.61 +  byte[] encode;
    1.62 +  byte[] expectedEncode;
    1.63 +  byte[] decode;
    1.64 +  byte[] expectedDecode;
    1.65 +  Random random = new Random(0);
    1.66 +  Cipher cipher;
    1.67 +  Cipher dCipher;
    1.68 +  AlgorithmParameters algParams;
    1.69 +  SecretKey key;
    1.70 +
    1.71 +  static int numThreads = 0;
    1.72 +  int  threadId;
    1.73 +  static synchronized int getThreadId() {
    1.74 +    int id = numThreads;
    1.75 +    numThreads++;
    1.76 +    return id;
    1.77 +  }
    1.78 +
    1.79 +  abstract public void run();
    1.80 +
    1.81 +  public void prepare() {
    1.82 +    try {
    1.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 );
    1.84 +
    1.85 +      if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 )
    1.86 +        testingMisalignment = true;
    1.87 +
    1.88 +      int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
    1.89 +      byte keyBytes[] = new byte[keyLenBytes];
    1.90 +      if (keySize == 128)
    1.91 +        keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
    1.92 +      else
    1.93 +        random.nextBytes(keyBytes);
    1.94 +
    1.95 +      key = new SecretKeySpec(keyBytes, algorithm);
    1.96 +      if (threadId == 0) {
    1.97 +        System.out.println("Algorithm: " + key.getAlgorithm() + "("
    1.98 +                           + key.getEncoded().length * 8 + "bit)");
    1.99 +      }
   1.100 +
   1.101 +      cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
   1.102 +      dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
   1.103 +
   1.104 +      if (mode.equals("CBC")) {
   1.105 +        int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
   1.106 +        IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
   1.107 +        cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
   1.108 +      } else {
   1.109 +        algParams = cipher.getParameters();
   1.110 +        cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
   1.111 +      }
   1.112 +      algParams = cipher.getParameters();
   1.113 +      dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
   1.114 +      if (threadId == 0) {
   1.115 +        childShowCipher();
   1.116 +      }
   1.117 +
   1.118 +      inputLength = msgSize + encInputOffset;
   1.119 +      if (testingMisalignment) {
   1.120 +        encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset;
   1.121 +        encodeLength += cipher.getOutputSize(lastChunkSize);
   1.122 +        decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset;
   1.123 +        decodeLength += dCipher.getOutputSize(lastChunkSize);
   1.124 +      } else {
   1.125 +        encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset;
   1.126 +        decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset;
   1.127 +      }
   1.128 +
   1.129 +      input = new byte[inputLength];
   1.130 +      for (int i=encInputOffset, j=0; i<inputLength; i++, j++) {
   1.131 +        input[i] = (byte) (j & 0xff);
   1.132 +      }
   1.133 +
   1.134 +      // do one encode and decode in preparation
   1.135 +      encode = new byte[encodeLength];
   1.136 +      decode = new byte[decodeLength];
   1.137 +      if (testingMisalignment) {
   1.138 +        decodeMsgSize = cipher.update(input, encInputOffset, (msgSize - lastChunkSize), encode, encOutputOffset);
   1.139 +        decodeMsgSize += cipher.doFinal(input, (encInputOffset + msgSize - lastChunkSize), lastChunkSize, encode, (encOutputOffset + decodeMsgSize));
   1.140 +
   1.141 +        int tempSize = dCipher.update(encode, encOutputOffset, (decodeMsgSize - lastChunkSize), decode, decOutputOffset);
   1.142 +        dCipher.doFinal(encode, (encOutputOffset + decodeMsgSize - lastChunkSize), lastChunkSize, decode, (decOutputOffset + tempSize));
   1.143 +      } else {
   1.144 +        decodeMsgSize = cipher.doFinal(input, encInputOffset, msgSize, encode, encOutputOffset);
   1.145 +        dCipher.doFinal(encode, encOutputOffset, decodeMsgSize, decode, decOutputOffset);
   1.146 +      }
   1.147 +      if (checkOutput) {
   1.148 +        expectedEncode = (byte[]) encode.clone();
   1.149 +        expectedDecode = (byte[]) decode.clone();
   1.150 +        showArray(key.getEncoded()  ,  "key:    ");
   1.151 +        showArray(input,  "input:  ");
   1.152 +        showArray(encode, "encode: ");
   1.153 +        showArray(decode, "decode: ");
   1.154 +      }
   1.155 +    }
   1.156 +    catch (Exception e) {
   1.157 +      e.printStackTrace();
   1.158 +      System.exit(1);
   1.159 +    }
   1.160 +  }
   1.161 +
   1.162 +  void showArray(byte b[], String name) {
   1.163 +    System.out.format("%s [%d]: ", name, b.length);
   1.164 +    for (int i=0; i<Math.min(b.length, 32); i++) {
   1.165 +      System.out.format("%02x ", b[i] & 0xff);
   1.166 +    }
   1.167 +    System.out.println();
   1.168 +  }
   1.169 +
   1.170 +  void compareArrays(byte b[], byte exp[]) {
   1.171 +    if (b.length != exp.length) {
   1.172 +      System.out.format("different lengths for actual and expected output arrays\n");
   1.173 +      showArray(b, "test: ");
   1.174 +      showArray(exp, "exp : ");
   1.175 +      System.exit(1);
   1.176 +    }
   1.177 +    for (int i=0; i< exp.length; i++) {
   1.178 +      if (b[i] != exp[i]) {
   1.179 +        System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff);
   1.180 +        showArray(b, "test: ");
   1.181 +        showArray(exp, "exp : ");
   1.182 +        System.exit(1);
   1.183 +      }
   1.184 +    }
   1.185 +  }
   1.186 +
   1.187 +
   1.188 +  void showCipher(Cipher c, String kind) {
   1.189 +    System.out.println(kind + " cipher provider: " + cipher.getProvider());
   1.190 +    System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
   1.191 +  }
   1.192 +
   1.193 +  abstract void childShowCipher();
   1.194 +}

mercurial