Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/AES/TestGHASH.java
38889 views
/*1* Copyright (c) 2015, Red Hat, Inc.2* Copyright (c) 2015, Oracle, Inc.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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @bug 806907227* @summary Test vectors for com.sun.crypto.provider.GHASH.28*29* Single iteration to verify software-only GHASH algorithm.30* @run main TestGHASH31*32* Multi-iteration to verify test intrinsics GHASH, if available.33* Many iterations are needed so we are sure hotspot will use intrinsic34* @run main TestGHASH -n 1000035*/36import java.lang.reflect.Constructor;37import java.lang.reflect.Method;38import java.nio.ByteBuffer;3940public class TestGHASH {4142private final Constructor<?> GHASH;43private final Method UPDATE;44private final Method DIGEST;4546TestGHASH(String className) throws Exception {47Class<?> cls = Class.forName(className);48GHASH = cls.getDeclaredConstructor(byte[].class);49GHASH.setAccessible(true);50UPDATE = cls.getDeclaredMethod("update", byte[].class);51UPDATE.setAccessible(true);52DIGEST = cls.getDeclaredMethod("digest");53DIGEST.setAccessible(true);54}555657private Object newGHASH(byte[] H) throws Exception {58return GHASH.newInstance(H);59}6061private void updateGHASH(Object hash, byte[] data)62throws Exception {63UPDATE.invoke(hash, data);64}6566private byte[] digestGHASH(Object hash) throws Exception {67return (byte[]) DIGEST.invoke(hash);68}6970private static final String HEX_DIGITS = "0123456789abcdef";7172private static String hex(byte[] bs) {73StringBuilder sb = new StringBuilder(2 * bs.length);74for (byte b : bs) {75sb.append(HEX_DIGITS.charAt((b >> 4) & 0xF));76sb.append(HEX_DIGITS.charAt(b & 0xF));77}78return sb.toString();79}8081private static byte[] bytes(String hex) {82if ((hex.length() & 1) != 0) {83throw new AssertionError();84}85byte[] result = new byte[hex.length() / 2];86for (int i = 0; i < result.length; ++i) {87int a = HEX_DIGITS.indexOf(hex.charAt(2 * i));88int b = HEX_DIGITS.indexOf(hex.charAt(2 * i + 1));89if ((a | b) < 0) {90if (a < 0) {91throw new AssertionError(92"bad character " + (int) hex.charAt(2 * i));93}94throw new AssertionError(95"bad character " + (int) hex.charAt(2 * i + 1));96}97result[i] = (byte) ((a << 4) | b);98}99return result;100}101102private static byte[] bytes(long L0, long L1) {103return ByteBuffer.allocate(16)104.putLong(L0)105.putLong(L1)106.array();107}108109private void check(int testCase, String H, String A,110String C, String expected) throws Exception {111int lenA = A.length() * 4;112while ((A.length() % 32) != 0) {113A += '0';114}115int lenC = C.length() * 4;116while ((C.length() % 32) != 0) {117C += '0';118}119120Object hash = newGHASH(bytes(H));121updateGHASH(hash, bytes(A));122updateGHASH(hash, bytes(C));123updateGHASH(hash, bytes(lenA, lenC));124byte[] digest = digestGHASH(hash);125String actual = hex(digest);126if (!expected.equals(actual)) {127throw new AssertionError(String.format("%d: expected %s, got %s",128testCase, expected, actual));129}130}131132public static void main(String[] args) throws Exception {133TestGHASH test;134String test_class = "com.sun.crypto.provider.GHASH";135int i = 0;136int num_of_loops = 1;137while (args.length > i) {138if (args[i].compareTo("-c") == 0) {139test_class = args[++i];140} else if (args[i].compareTo("-n") == 0) {141num_of_loops = Integer.parseInt(args[++i]);142}143i++;144}145146System.out.println("Running " + num_of_loops + " iterations.");147test = new TestGHASH(test_class);148i = 0;149150while (num_of_loops > i) {151// Test vectors from David A. McGrew, John Viega,152// "The Galois/Counter Mode of Operation (GCM)", 2005.153// <http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf>154test.check(1, "66e94bd4ef8a2c3b884cfa59ca342b2e", "", "",155"00000000000000000000000000000000");156test.check(2,157"66e94bd4ef8a2c3b884cfa59ca342b2e", "",158"0388dace60b6a392f328c2b971b2fe78",159"f38cbb1ad69223dcc3457ae5b6b0f885");160test.check(3,161"b83b533708bf535d0aa6e52980d53b78", "",162"42831ec2217774244b7221b784d0d49c" +163"e3aa212f2c02a4e035c17e2329aca12e" +164"21d514b25466931c7d8f6a5aac84aa05" +165"1ba30b396a0aac973d58e091473f5985",166"7f1b32b81b820d02614f8895ac1d4eac");167test.check(4,168"b83b533708bf535d0aa6e52980d53b78",169"feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",170"42831ec2217774244b7221b784d0d49c" +171"e3aa212f2c02a4e035c17e2329aca12e" +172"21d514b25466931c7d8f6a5aac84aa05" +173"1ba30b396a0aac973d58e091",174"698e57f70e6ecc7fd9463b7260a9ae5f");175test.check(5, "b83b533708bf535d0aa6e52980d53b78",176"feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",177"61353b4c2806934a777ff51fa22a4755" +178"699b2a714fcdc6f83766e5f97b6c7423" +179"73806900e49f24b22b097544d4896b42" +180"4989b5e1ebac0f07c23f4598",181"df586bb4c249b92cb6922877e444d37b");182i++;183}184}185}186187188