1 /*
   2  * Copyright (c) 2014, 2020, 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 8035968
  27  * @summary C2 support for MD5/SHA-1/SHA-224/SHA-256/SHA-384/SHA-512/SHA3
  28  *
  29  * @run main/othervm/timeout=600 -Xbatch
  30  *      -Dalgorithm=MD5
  31  *      compiler.intrinsics.sha.TestDigest
  32  * @run main/othervm/timeout=600 -Xbatch
  33  *      -Dalgorithm=SHA-1
  34  *      compiler.intrinsics.sha.TestDigest
  35  * @run main/othervm/timeout=600 -Xbatch
  36  *      -Dalgorithm=SHA-224
  37  *      compiler.intrinsics.sha.TestDigest
  38  * @run main/othervm/timeout=600 -Xbatch
  39  *      -Dalgorithm=SHA-256
  40  *      compiler.intrinsics.sha.TestDigest
  41  * @run main/othervm/timeout=600 -Xbatch
  42  *      -Dalgorithm=SHA-384
  43  *      compiler.intrinsics.sha.TestDigest
  44  * @run main/othervm/timeout=600 -Xbatch
  45  *      -Dalgorithm=SHA-512
  46  *      compiler.intrinsics.sha.TestDigest
  47  * @run main/othervm/timeout=600 -Xbatch
  48  *      -Dalgorithm=SHA3-224
  49  *      compiler.intrinsics.sha.TestDigest
  50  * @run main/othervm/timeout=600 -Xbatch
  51  *      -Dalgorithm=SHA3-256
  52  *      compiler.intrinsics.sha.TestDigest
  53  * @run main/othervm/timeout=600 -Xbatch
  54  *      -Dalgorithm=SHA3-384
  55  *      compiler.intrinsics.sha.TestDigest
  56  * @run main/othervm/timeout=600 -Xbatch
  57  *      -Dalgorithm=SHA3-512
  58  *      compiler.intrinsics.sha.TestDigest
  59  *
  60  * @run main/othervm/timeout=600 -Xbatch
  61  *      -Dalgorithm=MD5   -Doffset=1
  62  *      compiler.intrinsics.sha.TestDigest
  63  * @run main/othervm/timeout=600 -Xbatch
  64  *      -Dalgorithm=SHA-1   -Doffset=1
  65  *      compiler.intrinsics.sha.TestDigest
  66  * @run main/othervm/timeout=600 -Xbatch
  67  *      -Dalgorithm=SHA-224 -Doffset=1
  68  *      compiler.intrinsics.sha.TestDigest
  69  * @run main/othervm/timeout=600 -Xbatch
  70  *      -Dalgorithm=SHA-256 -Doffset=1
  71  *      compiler.intrinsics.sha.TestDigest
  72  * @run main/othervm/timeout=600 -Xbatch
  73  *      -Dalgorithm=SHA-384 -Doffset=1
  74  *      compiler.intrinsics.sha.TestDigest
  75  * @run main/othervm/timeout=600 -Xbatch
  76  *      -Dalgorithm=SHA-512 -Doffset=1
  77  *      compiler.intrinsics.sha.TestDigest
  78  * @run main/othervm/timeout=600 -Xbatch
  79  *      -Dalgorithm=SHA3-224 -Doffset=1
  80  *      compiler.intrinsics.sha.TestDigest
  81  * @run main/othervm/timeout=600 -Xbatch
  82  *      -Dalgorithm=SHA3-256 -Doffset=1
  83  *      compiler.intrinsics.sha.TestDigest
  84  * @run main/othervm/timeout=600 -Xbatch
  85  *      -Dalgorithm=SHA3-384 -Doffset=1
  86  *      compiler.intrinsics.sha.TestDigest
  87  * @run main/othervm/timeout=600 -Xbatch
  88  *      -Dalgorithm=SHA3-512 -Doffset=1
  89  *      compiler.intrinsics.sha.TestDigest
  90  *
  91  * @run main/othervm/timeout=600 -Xbatch
  92  *      -Dalgorithm=SHA-1   -Dalgorithm2=SHA-256
  93  *      compiler.intrinsics.sha.TestDigest
  94  * @run main/othervm/timeout=600 -Xbatch
  95  *      -Dalgorithm=SHA-1   -Dalgorithm2=SHA-512
  96  *      compiler.intrinsics.sha.TestDigest
  97  * @run main/othervm/timeout=600 -Xbatch
  98  *      -Dalgorithm=SHA-256 -Dalgorithm2=SHA-512
  99  *      compiler.intrinsics.sha.TestDigest
 100  *
 101  * @run main/othervm/timeout=600 -Xbatch
 102  *      -Dalgorithm=SHA-1   -Dalgorithm2=MD5
 103  *      compiler.intrinsics.sha.TestDigest
 104  * @run main/othervm/timeout=600 -Xbatch
 105  *      -Dalgorithm=MD5     -Dalgorithm2=SHA-1
 106  *      compiler.intrinsics.sha.TestDigest
 107  *
 108  * @run main/othervm/timeout=600 -Xbatch
 109  *      -Dalgorithm=SHA-1   -Dalgorithm2=SHA3-224
 110  *      compiler.intrinsics.sha.TestDigest
 111  * @run main/othervm/timeout=600 -Xbatch
 112  *      -Dalgorithm=SHA-1   -Dalgorithm2=SHA3-256
 113  *      compiler.intrinsics.sha.TestDigest
 114  * @run main/othervm/timeout=600 -Xbatch
 115  *      -Dalgorithm=SHA-1   -Dalgorithm2=SHA3-384
 116  *      compiler.intrinsics.sha.TestDigest
 117  * @run main/othervm/timeout=600 -Xbatch
 118  *      -Dalgorithm=SHA-1   -Dalgorithm2=SHA3-512
 119  *      compiler.intrinsics.sha.TestDigest
 120  * @run main/othervm/timeout=600 -Xbatch
 121  *      -Dalgorithm=SHA3-224 -Dalgorithm2=SHA-1
 122  *      compiler.intrinsics.sha.TestDigest
 123  * @run main/othervm/timeout=600 -Xbatch
 124  *      -Dalgorithm=SHA3-256 -Dalgorithm2=SHA-1
 125  *      compiler.intrinsics.sha.TestDigest
 126  * @run main/othervm/timeout=600 -Xbatch
 127  *      -Dalgorithm=SHA3-384 -Dalgorithm2=SHA-1
 128  *      compiler.intrinsics.sha.TestDigest
 129  * @run main/othervm/timeout=600 -Xbatch
 130  *      -Dalgorithm=SHA3-512 -Dalgorithm2=SHA-1
 131  *      compiler.intrinsics.sha.TestDigest
 132  */
 133 
 134 package compiler.intrinsics.sha;
 135 
 136 import java.security.MessageDigest;
 137 import java.util.Arrays;
 138 
 139 public class TestDigest {
 140     private static final int HASH_LEN = 64; /* up to 512-bit */
 141     private static final int ALIGN = 8;     /* for different data alignments */
 142 
 143     public static void main(String[] args) throws Exception {
 144         String provider = System.getProperty("provider", "SUN");
 145         String algorithm = System.getProperty("algorithm", "SHA-1");
 146         String algorithm2 = System.getProperty("algorithm2", "");
 147         int msgSize = Integer.getInteger("msgSize", 1024);
 148         int offset = Integer.getInteger("offset", 0)  % ALIGN;
 149         int iters = (args.length > 0 ? Integer.valueOf(args[0]) : 100000);
 150         int warmupIters = (args.length > 1 ? Integer.valueOf(args[1]) : 20000);
 151 
 152         testDigest(provider, algorithm, msgSize, offset, iters, warmupIters);
 153 
 154         if (algorithm2.equals("") == false) {
 155             testDigest(provider, algorithm2, msgSize, offset, iters, warmupIters);
 156         }
 157     }
 158 
 159     public static void testDigest(String provider, String algorithm, int msgSize,
 160                         int offset, int iters, int warmupIters) throws Exception {
 161         System.out.println("provider = " + provider);
 162         System.out.println("algorithm = " + algorithm);
 163         System.out.println("msgSize = " + msgSize + " bytes");
 164         System.out.println("offset = " + offset);
 165         System.out.println("iters = " + iters);
 166 
 167         byte[] expectedHash = new byte[HASH_LEN];
 168         byte[] hash = new byte[HASH_LEN];
 169         byte[] data = new byte[msgSize + offset];
 170         for (int i = 0; i < (msgSize + offset); i++) {
 171             data[i] = (byte)(i & 0xff);
 172         }
 173 
 174         try {
 175             MessageDigest digest = MessageDigest.getInstance(algorithm, provider);
 176 
 177             /* do once, which doesn't use intrinsics */
 178             digest.reset();
 179             digest.update(data, offset, msgSize);
 180             expectedHash = digest.digest();
 181 
 182             /* warm up */
 183             for (int i = 0; i < warmupIters; i++) {
 184                 digest.reset();
 185                 digest.update(data, offset, msgSize);
 186                 hash = digest.digest();
 187             }
 188 
 189             /* check result */
 190             if (Arrays.equals(hash, expectedHash) == false) {
 191                 System.out.println("TestDigest Error: ");
 192                 showArray(expectedHash, "expectedHash");
 193                 showArray(hash,         "computedHash");
 194                 //System.exit(1);
 195                 throw new Exception("TestDigest Error");
 196             } else {
 197                 showArray(hash, "hash");
 198             }
 199 
 200             /* measure performance */
 201             long start = System.nanoTime();
 202             for (int i = 0; i < iters; i++) {
 203                 digest.reset();
 204                 digest.update(data, offset, msgSize);
 205                 hash = digest.digest();
 206             }
 207             long end = System.nanoTime();
 208             double total = (double)(end - start)/1e9;         /* in seconds */
 209             double thruput = (double)msgSize*iters/1e6/total; /* in MB/s */
 210             System.out.println("TestDigest runtime = " + total + " seconds");
 211             System.out.println("TestDigest throughput = " + thruput + " MB/s");
 212             System.out.println();
 213         } catch (Exception e) {
 214             System.out.println("Exception: " + e);
 215             //System.exit(1);
 216             throw new Exception(e);
 217         }
 218     }
 219 
 220     static void showArray(byte b[], String name) {
 221         System.out.format("%s [%d]: ", name, b.length);
 222         for (int i = 0; i < Math.min(b.length, HASH_LEN); i++) {
 223             System.out.format("%02x ", b[i] & 0xff);
 224         }
 225         System.out.println();
 226     }
 227 }