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