Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestGHASH.java
41161 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* @modules java.base/com.sun.crypto.provider:open28* @summary Test vectors for com.sun.crypto.provider.GHASH.29*30* Single iteration to verify software-only GHASH algorithm.31* @run main TestGHASH32*33* Multi-iteration to verify test intrinsics GHASH, if available.34* Many iterations are needed so we are sure hotspot will use intrinsic35* @run main TestGHASH -n 1000036*/37import java.lang.reflect.Constructor;38import java.lang.reflect.Method;39import java.nio.ByteBuffer;4041public class TestGHASH {4243private final Constructor<?> GHASH;44private final Method UPDATE;45private final Method DIGEST;4647TestGHASH(String className) throws Exception {48Class<?> cls = Class.forName(className);49GHASH = cls.getDeclaredConstructor(byte[].class);50GHASH.setAccessible(true);51UPDATE = cls.getDeclaredMethod("update", byte[].class);52UPDATE.setAccessible(true);53DIGEST = cls.getDeclaredMethod("digest");54DIGEST.setAccessible(true);55}565758private Object newGHASH(byte[] H) throws Exception {59return GHASH.newInstance(H);60}6162private void updateGHASH(Object hash, byte[] data)63throws Exception {64UPDATE.invoke(hash, data);65}6667private byte[] digestGHASH(Object hash) throws Exception {68return (byte[]) DIGEST.invoke(hash);69}7071private static final String HEX_DIGITS = "0123456789abcdef";7273private static String hex(byte[] bs) {74StringBuilder sb = new StringBuilder(2 * bs.length);75for (byte b : bs) {76sb.append(HEX_DIGITS.charAt((b >> 4) & 0xF));77sb.append(HEX_DIGITS.charAt(b & 0xF));78}79return sb.toString();80}8182private static byte[] bytes(String hex) {83if ((hex.length() & 1) != 0) {84throw new AssertionError();85}86byte[] result = new byte[hex.length() / 2];87for (int i = 0; i < result.length; ++i) {88int a = HEX_DIGITS.indexOf(hex.charAt(2 * i));89int b = HEX_DIGITS.indexOf(hex.charAt(2 * i + 1));90if ((a | b) < 0) {91if (a < 0) {92throw new AssertionError(93"bad character " + (int) hex.charAt(2 * i));94}95throw new AssertionError(96"bad character " + (int) hex.charAt(2 * i + 1));97}98result[i] = (byte) ((a << 4) | b);99}100return result;101}102103private static byte[] bytes(long L0, long L1) {104return ByteBuffer.allocate(16)105.putLong(L0)106.putLong(L1)107.array();108}109110private void check(int testCase, String H, String A,111String C, String expected) throws Exception {112int lenA = A.length() * 4;113while ((A.length() % 32) != 0) {114A += '0';115}116int lenC = C.length() * 4;117while ((C.length() % 32) != 0) {118C += '0';119}120121Object hash = newGHASH(bytes(H));122updateGHASH(hash, bytes(A));123updateGHASH(hash, bytes(C));124updateGHASH(hash, bytes(lenA, lenC));125byte[] digest = digestGHASH(hash);126String actual = hex(digest);127if (!expected.equals(actual)) {128throw new AssertionError(String.format("%d: expected %s, got %s",129testCase, expected, actual));130}131}132133public static void main(String[] args) throws Exception {134TestGHASH test;135String test_class = "com.sun.crypto.provider.GHASH";136int i = 0;137int num_of_loops = 1;138while (args.length > i) {139if (args[i].compareTo("-c") == 0) {140test_class = args[++i];141} else if (args[i].compareTo("-n") == 0) {142num_of_loops = Integer.parseInt(args[++i]);143}144i++;145}146147System.out.println("Running " + num_of_loops + " iterations.");148test = new TestGHASH(test_class);149i = 0;150151while (num_of_loops > i) {152// Test vectors from David A. McGrew, John Viega,153// "The Galois/Counter Mode of Operation (GCM)", 2005.154// <http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf>155test.check(1, "66e94bd4ef8a2c3b884cfa59ca342b2e", "", "",156"00000000000000000000000000000000");157test.check(2,158"66e94bd4ef8a2c3b884cfa59ca342b2e", "",159"0388dace60b6a392f328c2b971b2fe78",160"f38cbb1ad69223dcc3457ae5b6b0f885");161test.check(3,162"b83b533708bf535d0aa6e52980d53b78", "",163"42831ec2217774244b7221b784d0d49c" +164"e3aa212f2c02a4e035c17e2329aca12e" +165"21d514b25466931c7d8f6a5aac84aa05" +166"1ba30b396a0aac973d58e091473f5985",167"7f1b32b81b820d02614f8895ac1d4eac");168test.check(4,169"b83b533708bf535d0aa6e52980d53b78",170"feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",171"42831ec2217774244b7221b784d0d49c" +172"e3aa212f2c02a4e035c17e2329aca12e" +173"21d514b25466931c7d8f6a5aac84aa05" +174"1ba30b396a0aac973d58e091",175"698e57f70e6ecc7fd9463b7260a9ae5f");176test.check(5, "b83b533708bf535d0aa6e52980d53b78",177"feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",178"61353b4c2806934a777ff51fa22a4755" +179"699b2a714fcdc6f83766e5f97b6c7423" +180"73806900e49f24b22b097544d4896b42" +181"4989b5e1ebac0f07c23f4598",182"df586bb4c249b92cb6922877e444d37b");183i++;184}185}186}187188189