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