Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/sha/TestDigest.java
41153 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 803596826* @summary C2 support for MD5/SHA-1/SHA-224/SHA-256/SHA-384/SHA-512/SHA327*28* @run main/othervm/timeout=600 -Xbatch29* -Dalgorithm=MD530* compiler.intrinsics.sha.TestDigest31* @run main/othervm/timeout=600 -Xbatch32* -Dalgorithm=SHA-133* compiler.intrinsics.sha.TestDigest34* @run main/othervm/timeout=600 -Xbatch35* -Dalgorithm=SHA-22436* compiler.intrinsics.sha.TestDigest37* @run main/othervm/timeout=600 -Xbatch38* -Dalgorithm=SHA-25639* compiler.intrinsics.sha.TestDigest40* @run main/othervm/timeout=600 -Xbatch41* -Dalgorithm=SHA-38442* compiler.intrinsics.sha.TestDigest43* @run main/othervm/timeout=600 -Xbatch44* -Dalgorithm=SHA-51245* compiler.intrinsics.sha.TestDigest46* @run main/othervm/timeout=600 -Xbatch47* -Dalgorithm=SHA3-22448* compiler.intrinsics.sha.TestDigest49* @run main/othervm/timeout=600 -Xbatch50* -Dalgorithm=SHA3-25651* compiler.intrinsics.sha.TestDigest52* @run main/othervm/timeout=600 -Xbatch53* -Dalgorithm=SHA3-38454* compiler.intrinsics.sha.TestDigest55* @run main/othervm/timeout=600 -Xbatch56* -Dalgorithm=SHA3-51257* compiler.intrinsics.sha.TestDigest58*59* @run main/othervm/timeout=600 -Xbatch60* -Dalgorithm=MD5 -Doffset=161* compiler.intrinsics.sha.TestDigest62* @run main/othervm/timeout=600 -Xbatch63* -Dalgorithm=SHA-1 -Doffset=164* compiler.intrinsics.sha.TestDigest65* @run main/othervm/timeout=600 -Xbatch66* -Dalgorithm=SHA-224 -Doffset=167* compiler.intrinsics.sha.TestDigest68* @run main/othervm/timeout=600 -Xbatch69* -Dalgorithm=SHA-256 -Doffset=170* compiler.intrinsics.sha.TestDigest71* @run main/othervm/timeout=600 -Xbatch72* -Dalgorithm=SHA-384 -Doffset=173* compiler.intrinsics.sha.TestDigest74* @run main/othervm/timeout=600 -Xbatch75* -Dalgorithm=SHA-512 -Doffset=176* compiler.intrinsics.sha.TestDigest77* @run main/othervm/timeout=600 -Xbatch78* -Dalgorithm=SHA3-224 -Doffset=179* compiler.intrinsics.sha.TestDigest80* @run main/othervm/timeout=600 -Xbatch81* -Dalgorithm=SHA3-256 -Doffset=182* compiler.intrinsics.sha.TestDigest83* @run main/othervm/timeout=600 -Xbatch84* -Dalgorithm=SHA3-384 -Doffset=185* compiler.intrinsics.sha.TestDigest86* @run main/othervm/timeout=600 -Xbatch87* -Dalgorithm=SHA3-512 -Doffset=188* compiler.intrinsics.sha.TestDigest89*90* @run main/othervm/timeout=600 -Xbatch91* -Dalgorithm=SHA-1 -Dalgorithm2=SHA-25692* compiler.intrinsics.sha.TestDigest93* @run main/othervm/timeout=600 -Xbatch94* -Dalgorithm=SHA-1 -Dalgorithm2=SHA-51295* compiler.intrinsics.sha.TestDigest96* @run main/othervm/timeout=600 -Xbatch97* -Dalgorithm=SHA-256 -Dalgorithm2=SHA-51298* compiler.intrinsics.sha.TestDigest99*100* @run main/othervm/timeout=600 -Xbatch101* -Dalgorithm=SHA-1 -Dalgorithm2=MD5102* compiler.intrinsics.sha.TestDigest103* @run main/othervm/timeout=600 -Xbatch104* -Dalgorithm=MD5 -Dalgorithm2=SHA-1105* compiler.intrinsics.sha.TestDigest106*107* @run main/othervm/timeout=600 -Xbatch108* -Dalgorithm=SHA-1 -Dalgorithm2=SHA3-224109* compiler.intrinsics.sha.TestDigest110* @run main/othervm/timeout=600 -Xbatch111* -Dalgorithm=SHA-1 -Dalgorithm2=SHA3-256112* compiler.intrinsics.sha.TestDigest113* @run main/othervm/timeout=600 -Xbatch114* -Dalgorithm=SHA-1 -Dalgorithm2=SHA3-384115* compiler.intrinsics.sha.TestDigest116* @run main/othervm/timeout=600 -Xbatch117* -Dalgorithm=SHA-1 -Dalgorithm2=SHA3-512118* compiler.intrinsics.sha.TestDigest119* @run main/othervm/timeout=600 -Xbatch120* -Dalgorithm=SHA3-224 -Dalgorithm2=SHA-1121* compiler.intrinsics.sha.TestDigest122* @run main/othervm/timeout=600 -Xbatch123* -Dalgorithm=SHA3-256 -Dalgorithm2=SHA-1124* compiler.intrinsics.sha.TestDigest125* @run main/othervm/timeout=600 -Xbatch126* -Dalgorithm=SHA3-384 -Dalgorithm2=SHA-1127* compiler.intrinsics.sha.TestDigest128* @run main/othervm/timeout=600 -Xbatch129* -Dalgorithm=SHA3-512 -Dalgorithm2=SHA-1130* compiler.intrinsics.sha.TestDigest131*/132133package compiler.intrinsics.sha;134135import java.security.MessageDigest;136import java.util.Arrays;137138public class TestDigest {139private static final int HASH_LEN = 64; /* up to 512-bit */140private static final int ALIGN = 8; /* for different data alignments */141142public static void main(String[] args) throws Exception {143String provider = System.getProperty("provider", "SUN");144String algorithm = System.getProperty("algorithm", "SHA-1");145String algorithm2 = System.getProperty("algorithm2", "");146int msgSize = Integer.getInteger("msgSize", 1024);147int offset = Integer.getInteger("offset", 0) % ALIGN;148int iters = (args.length > 0 ? Integer.valueOf(args[0]) : 100000);149int warmupIters = (args.length > 1 ? Integer.valueOf(args[1]) : 20000);150151testDigest(provider, algorithm, msgSize, offset, iters, warmupIters);152153if (algorithm2.equals("") == false) {154testDigest(provider, algorithm2, msgSize, offset, iters, warmupIters);155}156}157158public static void testDigest(String provider, String algorithm, int msgSize,159int offset, int iters, int warmupIters) throws Exception {160System.out.println("provider = " + provider);161System.out.println("algorithm = " + algorithm);162System.out.println("msgSize = " + msgSize + " bytes");163System.out.println("offset = " + offset);164System.out.println("iters = " + iters);165166byte[] expectedHash = new byte[HASH_LEN];167byte[] hash = new byte[HASH_LEN];168byte[] data = new byte[msgSize + offset];169for (int i = 0; i < (msgSize + offset); i++) {170data[i] = (byte)(i & 0xff);171}172173try {174MessageDigest digest = MessageDigest.getInstance(algorithm, provider);175176/* do once, which doesn't use intrinsics */177digest.reset();178digest.update(data, offset, msgSize);179expectedHash = digest.digest();180181/* warm up */182for (int i = 0; i < warmupIters; i++) {183digest.reset();184digest.update(data, offset, msgSize);185hash = digest.digest();186}187188/* check result */189if (Arrays.equals(hash, expectedHash) == false) {190System.out.println("TestDigest Error: ");191showArray(expectedHash, "expectedHash");192showArray(hash, "computedHash");193//System.exit(1);194throw new Exception("TestDigest Error");195} else {196showArray(hash, "hash");197}198199/* measure performance */200long start = System.nanoTime();201for (int i = 0; i < iters; i++) {202digest.reset();203digest.update(data, offset, msgSize);204hash = digest.digest();205}206long end = System.nanoTime();207double total = (double)(end - start)/1e9; /* in seconds */208double thruput = (double)msgSize*iters/1e6/total; /* in MB/s */209System.out.println("TestDigest runtime = " + total + " seconds");210System.out.println("TestDigest throughput = " + thruput + " MB/s");211System.out.println();212} catch (Exception e) {213System.out.println("Exception: " + e);214//System.exit(1);215throw new Exception(e);216}217}218219static void showArray(byte b[], String name) {220System.out.format("%s [%d]: ", name, b.length);221for (int i = 0; i < Math.min(b.length, HASH_LEN); i++) {222System.out.format("%02x ", b[i] & 0xff);223}224System.out.println();225}226}227228229