hotspot/test/compiler/7184394/TestAESBase.java
author jlaskey
Tue, 23 Jul 2013 12:00:29 -0300
changeset 19089 51cfdcf21d35
parent 14834 f29c91f2f22b
child 24328 bddefb356fba
permissions -rw-r--r--
8021130: Comments need to be tokens Reviewed-by: lagergren, attila Contributed-by: james.laskey@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     1
/*
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     4
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     8
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    13
 * accompanied this code).
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    14
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    18
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    21
 * questions.
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    22
 *
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    23
 */
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    24
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    25
/**
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    26
 * @author Tom Deneau
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    27
 */
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    28
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    29
import javax.crypto.Cipher;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    30
import javax.crypto.KeyGenerator;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    31
import javax.crypto.SecretKey;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    32
import javax.crypto.spec.IvParameterSpec;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    33
import javax.crypto.spec.SecretKeySpec;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    34
import java.security.AlgorithmParameters;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    35
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    36
import java.util.Random;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    37
import java.util.Arrays;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    38
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    39
abstract public class TestAESBase {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    40
  int msgSize = Integer.getInteger("msgSize", 646);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    41
  boolean checkOutput = Boolean.getBoolean("checkOutput");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    42
  boolean noReinit = Boolean.getBoolean("noReinit");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    43
  int keySize = Integer.getInteger("keySize", 128);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    44
  String algorithm = System.getProperty("algorithm", "AES");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    45
  String mode = System.getProperty("mode", "CBC");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    46
  byte[] input;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    47
  byte[] encode;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    48
  byte[] expectedEncode;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    49
  byte[] decode;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    50
  byte[] expectedDecode;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    51
  Random random = new Random(0);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    52
  Cipher cipher;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    53
  Cipher dCipher;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    54
  String paddingStr = "PKCS5Padding";
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    55
  AlgorithmParameters algParams;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    56
  SecretKey key;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    57
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    58
  static int numThreads = 0;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    59
  int  threadId;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    60
  static synchronized int getThreadId() {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    61
    int id = numThreads;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    62
    numThreads++;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    63
    return id;
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    64
  }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    65
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    66
  abstract public void run();
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    67
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    68
  public void prepare() {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    69
    try {
14834
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    70
    System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + ", checkOutput=" + checkOutput);
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    71
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    72
      int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    73
      byte keyBytes[] = new byte[keyLenBytes];
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    74
      if (keySize == 128)
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    75
        keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    76
      else
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    77
        random.nextBytes(keyBytes);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    78
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    79
      key = new SecretKeySpec(keyBytes, algorithm);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    80
      if (threadId == 0) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    81
        System.out.println("Algorithm: " + key.getAlgorithm() + "("
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    82
                           + key.getEncoded().length * 8 + "bit)");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    83
      }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    84
      input = new byte[msgSize];
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    85
      for (int i=0; i<input.length; i++) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    86
        input[i] = (byte) (i & 0xff);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    87
      }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    88
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    89
      cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    90
      dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
    91
14834
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    92
      if (mode.equals("CBC")) {
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    93
        int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    94
        IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    95
        cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    96
      } else {
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    97
        algParams = cipher.getParameters();
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    98
        cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
f29c91f2f22b 8004835: Improve AES intrinsics on x86
kvn
parents: 14132
diff changeset
    99
      }
14132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   100
      algParams = cipher.getParameters();
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   101
      dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   102
      if (threadId == 0) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   103
        childShowCipher();
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   104
      }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   105
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   106
      // do one encode and decode in preparation
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   107
      // this will also create the encode buffer and decode buffer
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   108
      encode = cipher.doFinal(input);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   109
      decode = dCipher.doFinal(encode);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   110
      if (checkOutput) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   111
        expectedEncode = (byte[]) encode.clone();
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   112
        expectedDecode = (byte[]) decode.clone();
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   113
        showArray(key.getEncoded()  ,  "key:    ");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   114
        showArray(input,  "input:  ");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   115
        showArray(encode, "encode: ");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   116
        showArray(decode, "decode: ");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   117
      }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   118
    }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   119
    catch (Exception e) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   120
      e.printStackTrace();
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   121
      System.exit(1);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   122
    }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   123
  }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   124
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   125
  void showArray(byte b[], String name) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   126
    System.out.format("%s [%d]: ", name, b.length);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   127
    for (int i=0; i<Math.min(b.length, 32); i++) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   128
      System.out.format("%02x ", b[i] & 0xff);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   129
    }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   130
    System.out.println();
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   131
  }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   132
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   133
  void compareArrays(byte b[], byte exp[]) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   134
    if (b.length != exp.length) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   135
      System.out.format("different lengths for actual and expected output arrays\n");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   136
      showArray(b, "test: ");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   137
      showArray(exp, "exp : ");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   138
      System.exit(1);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   139
    }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   140
    for (int i=0; i< exp.length; i++) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   141
      if (b[i] != exp[i]) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   142
        System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   143
        showArray(b, "test: ");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   144
        showArray(exp, "exp : ");
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   145
        System.exit(1);
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   146
      }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   147
    }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   148
  }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   149
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   150
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   151
  void showCipher(Cipher c, String kind) {
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   152
    System.out.println(kind + " cipher provider: " + cipher.getProvider());
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   153
    System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   154
  }
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   155
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   156
  abstract void childShowCipher();
3c1437abcefd 7184394: add intrinsics to use AES instructions
kvn
parents:
diff changeset
   157
}