1 /*
   2  * Copyright (c) 2012, 2018, 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  * @test
  26  * @bug 7184394
  27  * @key stress
  28  * @summary add intrinsics to use AES instructions
  29  * @library /test/lib /
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @build sun.hotspot.WhiteBox
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  34  *
  35  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CBC
  36  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  37  *      compiler.codegen.aes.TestAESMain
  38  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CBC -DencInputOffset=1
  39  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  40  *      compiler.codegen.aes.TestAESMain
  41  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CBC -DencOutputOffset=1
  42  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  43  *      compiler.codegen.aes.TestAESMain
  44  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CBC -DdecOutputOffset=1
  45  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  46  *      compiler.codegen.aes.TestAESMain
  47  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CBC -DencInputOffset=1 -DencOutputOffset=1
  48  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  49  *      compiler.codegen.aes.TestAESMain
  50  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CBC -DencInputOffset=1 -DencOutputOffset=1 -DdecOutputOffset=1
  51  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  52  *      compiler.codegen.aes.TestAESMain
  53  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CBC -DencInputOffset=1 -DencOutputOffset=1 -DdecOutputOffset=1 -DpaddingStr=NoPadding -DmsgSize=640
  54  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  55  *      compiler.codegen.aes.TestAESMain
  56  *
  57  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=ECB
  58  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  59  *      compiler.codegen.aes.TestAESMain
  60  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=ECB -DencInputOffset=1
  61  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  62  *      compiler.codegen.aes.TestAESMain
  63  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=ECB -DencOutputOffset=
  64  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  65  *      compiler.codegen.aes.TestAESMain
  66  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=ECB -DdecOutputOffset=1
  67  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  68  *      compiler.codegen.aes.TestAESMain
  69  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=ECB -DencInputOffset=1 -DencOutputOffset=1
  70  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  71  *      compiler.codegen.aes.TestAESMain
  72  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=ECB -DencInputOffset=1 -DencOutputOffset=1 -DdecOutputOffset=1
  73  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  74  *      compiler.codegen.aes.TestAESMain
  75  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=ECB -DencInputOffset=1 -DencOutputOffset=1 -DdecOutputOffset=1 -DpaddingStr=NoPadding -DmsgSize=640
  76  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  77  *      compiler.codegen.aes.TestAESMain
  78  *
  79  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=GCM
  80  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  81  *      compiler.codegen.aes.TestAESMain
  82  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=GCM -DencInputOffset=1
  83  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  84  *      compiler.codegen.aes.TestAESMain
  85  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=GCM -DencOutputOffset=1
  86  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  87  *      compiler.codegen.aes.TestAESMain
  88  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=GCM -DdecOutputOffset=1
  89  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  90  *      compiler.codegen.aes.TestAESMain
  91  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=GCM -DencInputOffset=1 -DencOutputOffset=1
  92  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  93  *      compiler.codegen.aes.TestAESMain
  94  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=GCM -DencInputOffset=1 -DencOutputOffset=1 -DdecOutputOffset=1
  95  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  96  *      compiler.codegen.aes.TestAESMain
  97  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=GCM -DencInputOffset=1 -DencOutputOffset=1 -DdecOutputOffset=1 -DpaddingStr=NoPadding -DmsgSize=640
  98  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  99  *      compiler.codegen.aes.TestAESMain
 100  *
 101  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CTR
 102  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 103  *      compiler.codegen.aes.TestAESMain
 104  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CTR -DencInputOffset=1
 105  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 106  *      compiler.codegen.aes.TestAESMain
 107  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CTR -DencOutputOffset=1
 108  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 109  *      compiler.codegen.aes.TestAESMain
 110  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CTR -DdecOutputOffset=1
 111  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 112  *      compiler.codegen.aes.TestAESMain
 113  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CTR -DencInputOffset=1 -DencOutputOffset=1
 114  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 115  *      compiler.codegen.aes.TestAESMain
 116  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CTR -DencInputOffset=1 -DencOutputOffset=1 -DdecOutputOffset=1
 117  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 118  *      compiler.codegen.aes.TestAESMain
 119  * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true -Dmode=CTR -DencInputOffset=1 -DencOutputOffset=1 -DdecOutputOffset=1 -DpaddingStr=NoPadding -DmsgSize=640
 120  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 121  *      compiler.codegen.aes.TestAESMain
 122  *
 123  * @author Tom Deneau
 124  */
 125 
 126 package compiler.codegen.aes;
 127 
 128 import compiler.whitebox.CompilerWhiteBoxTest;
 129 import sun.hotspot.code.Compiler;
 130 import jtreg.SkippedException;
 131 
 132 public class TestAESMain {
 133     public static void main(String[] args) {
 134         String mode = System.getProperty("mode", "CBC");
 135         if ((mode.equals("CBC") || mode.equals("ECB")) &&
 136             !Compiler.isIntrinsicAvailable(CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION, "com.sun.crypto.provider.AESCrypt", "implEncryptBlock", byte[].class, int.class, byte[].class, int.class)) {
 137             throw new SkippedException("AES intrinsic is not available");
 138         }
 139         if (mode.equals("GCM") &&
 140             !Compiler.isIntrinsicAvailable(CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION, "com.sun.crypto.provider.GHASH", "processBlocks", byte[].class, int.class, int.class, long[].class, long[].class)) {
 141             throw new SkippedException("GHASH intrinsic is not available");
 142         }
 143         if (mode.equals("CTR") &&
 144             !Compiler.isIntrinsicAvailable(CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION, "com.sun.crypto.provider.CounterMode", "implCrypt", byte[].class, int.class, int.class, byte[].class, int.class)) {
 145             throw new SkippedException("AES-CTR intrinsic is not available");
 146         }
 147         int iters = (args.length > 0 ? Integer.valueOf(args[0]) : 100000);
 148         int warmupIters = (args.length > 1 ? Integer.valueOf(args[1]) : 20000);
 149         System.out.println(iters + " iterations");
 150         TestAESEncode etest = new TestAESEncode();
 151         etest.prepare();
 152         // warm-up
 153         System.out.println("Starting encryption warm-up");
 154         for (int i = 0; i < warmupIters; i++) {
 155             etest.run();
 156         }
 157         System.out.println("Finished encryption warm-up");
 158         long start = System.nanoTime();
 159         for (int i = 0; i < iters; i++) {
 160             etest.run();
 161         }
 162         long end = System.nanoTime();
 163         System.out.println("TestAESEncode runtime was " + (double) ((end - start) / 1000000.0) + " ms");
 164 
 165         TestAESDecode dtest = new TestAESDecode();
 166         dtest.prepare();
 167         // warm-up
 168         System.out.println("Starting decryption warm-up");
 169         for (int i = 0; i < warmupIters; i++) {
 170             dtest.run();
 171         }
 172         System.out.println("Finished decryption warm-up");
 173         start = System.nanoTime();
 174         for (int i = 0; i < iters; i++) {
 175             dtest.run();
 176         }
 177         end = System.nanoTime();
 178         System.out.println("TestAESDecode runtime was " + (double) ((end - start) / 1000000.0) + " ms");
 179     }
 180 }