< prev index next >

test/com/sun/crypto/provider/Cipher/AES/TestGHASH.java

Print this page


   1 /*
   2  * Copyright (c) 2015, Red Hat, 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 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 8069072
  27  * @summary Test vectors for com.sun.crypto.provider.GHASH







  28  */
  29 import java.lang.reflect.Constructor;
  30 import java.lang.reflect.Method;
  31 import java.nio.ByteBuffer;
  32 
  33 public class TestGHASH {
  34 
  35     private final Constructor<?> GHASH;
  36     private final Method UPDATE;
  37     private final Method DIGEST;
  38 
  39     TestGHASH(String className) throws Exception {
  40         Class<?> cls = Class.forName(className);
  41         GHASH = cls.getDeclaredConstructor(byte[].class);
  42         GHASH.setAccessible(true);
  43         UPDATE = cls.getDeclaredMethod("update", byte[].class);
  44         UPDATE.setAccessible(true);
  45         DIGEST = cls.getDeclaredMethod("digest");
  46         DIGEST.setAccessible(true);
  47     }


 107         }
 108         int lenC = C.length() * 4;
 109         while ((C.length() % 32) != 0) {
 110             C += '0';
 111         }
 112 
 113         Object hash = newGHASH(bytes(H));
 114         updateGHASH(hash, bytes(A));
 115         updateGHASH(hash, bytes(C));
 116         updateGHASH(hash, bytes(lenA, lenC));
 117         byte[] digest = digestGHASH(hash);
 118         String actual = hex(digest);
 119         if (!expected.equals(actual)) {
 120             throw new AssertionError(String.format("%d: expected %s, got %s",
 121                     testCase, expected, actual));
 122         }
 123     }
 124 
 125     public static void main(String[] args) throws Exception {
 126         TestGHASH test;
 127         if (args.length == 0) {
 128             test = new TestGHASH("com.sun.crypto.provider.GHASH");
 129         } else {
 130             test = new TestGHASH(args[0]);
 131         }










 132 

 133         // Test vectors from David A. McGrew, John Viega,
 134         // "The Galois/Counter Mode of Operation (GCM)", 2005.
 135         // <http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf>
 136 
 137         test.check(1, "66e94bd4ef8a2c3b884cfa59ca342b2e", "", "",
 138                 "00000000000000000000000000000000");
 139         test.check(2,
 140                 "66e94bd4ef8a2c3b884cfa59ca342b2e", "",
 141                 "0388dace60b6a392f328c2b971b2fe78",
 142                 "f38cbb1ad69223dcc3457ae5b6b0f885");
 143         test.check(3,
 144                 "b83b533708bf535d0aa6e52980d53b78", "",
 145                 "42831ec2217774244b7221b784d0d49c" +
 146                 "e3aa212f2c02a4e035c17e2329aca12e" +
 147                 "21d514b25466931c7d8f6a5aac84aa05" +
 148                 "1ba30b396a0aac973d58e091473f5985",
 149                 "7f1b32b81b820d02614f8895ac1d4eac");
 150         test.check(4,
 151                 "b83b533708bf535d0aa6e52980d53b78",
 152                 "feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",
 153                 "42831ec2217774244b7221b784d0d49c" +
 154                 "e3aa212f2c02a4e035c17e2329aca12e" +
 155                 "21d514b25466931c7d8f6a5aac84aa05" +
 156                 "1ba30b396a0aac973d58e091",
 157                 "698e57f70e6ecc7fd9463b7260a9ae5f");
 158         test.check(5, "b83b533708bf535d0aa6e52980d53b78",
 159                 "feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",
 160                 "61353b4c2806934a777ff51fa22a4755" +
 161                 "699b2a714fcdc6f83766e5f97b6c7423" +
 162                 "73806900e49f24b22b097544d4896b42" +
 163                 "4989b5e1ebac0f07c23f4598",
 164                 "df586bb4c249b92cb6922877e444d37b");


 165     }
 166 }
   1 /*
   2  * Copyright (c) 2015, Red Hat, Inc.
   3  * Copyright (c) 2015, Oracle, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * @test
  27  * @bug 8069072
  28  * @summary Test vectors for com.sun.crypto.provider.GHASH.
  29  *
  30  * Single iteration to verify software-only GHASH algorithm.
  31  * @run main TestGHASH
  32  *
  33  * Multi-iteration to verify test intrinsics GHASH, if available.
  34  * Many iterations are needed so we are sure hotspot will use intrinsic
  35  * @run main TestGHASH -n 10000
  36  */
  37 import java.lang.reflect.Constructor;
  38 import java.lang.reflect.Method;
  39 import java.nio.ByteBuffer;
  40 
  41 public class TestGHASH {
  42 
  43     private final Constructor<?> GHASH;
  44     private final Method UPDATE;
  45     private final Method DIGEST;
  46 
  47     TestGHASH(String className) throws Exception {
  48         Class<?> cls = Class.forName(className);
  49         GHASH = cls.getDeclaredConstructor(byte[].class);
  50         GHASH.setAccessible(true);
  51         UPDATE = cls.getDeclaredMethod("update", byte[].class);
  52         UPDATE.setAccessible(true);
  53         DIGEST = cls.getDeclaredMethod("digest");
  54         DIGEST.setAccessible(true);
  55     }


 115         }
 116         int lenC = C.length() * 4;
 117         while ((C.length() % 32) != 0) {
 118             C += '0';
 119         }
 120 
 121         Object hash = newGHASH(bytes(H));
 122         updateGHASH(hash, bytes(A));
 123         updateGHASH(hash, bytes(C));
 124         updateGHASH(hash, bytes(lenA, lenC));
 125         byte[] digest = digestGHASH(hash);
 126         String actual = hex(digest);
 127         if (!expected.equals(actual)) {
 128             throw new AssertionError(String.format("%d: expected %s, got %s",
 129                     testCase, expected, actual));
 130         }
 131     }
 132 
 133     public static void main(String[] args) throws Exception {
 134         TestGHASH test;
 135         String test_class = "com.sun.crypto.provider.GHASH";
 136         int i = 0;
 137         int num_of_loops = 1;
 138         while (args.length > i) {
 139             if (args[i].compareTo("-c") == 0) {
 140                 test_class = args[++i];
 141             } else if (args[i].compareTo("-n") == 0) {
 142                 num_of_loops = Integer.parseInt(args[++i]);
 143             }
 144             i++;
 145         }
 146 
 147         System.out.println("Running " + num_of_loops + " iterations.");
 148         test = new TestGHASH(test_class);
 149         i = 0;
 150 
 151         while (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>

 155             test.check(1, "66e94bd4ef8a2c3b884cfa59ca342b2e", "", "",
 156                        "00000000000000000000000000000000");
 157             test.check(2,
 158                        "66e94bd4ef8a2c3b884cfa59ca342b2e", "",
 159                        "0388dace60b6a392f328c2b971b2fe78",
 160                        "f38cbb1ad69223dcc3457ae5b6b0f885");
 161             test.check(3,
 162                        "b83b533708bf535d0aa6e52980d53b78", "",
 163                        "42831ec2217774244b7221b784d0d49c" +
 164                        "e3aa212f2c02a4e035c17e2329aca12e" +
 165                        "21d514b25466931c7d8f6a5aac84aa05" +
 166                        "1ba30b396a0aac973d58e091473f5985",
 167                        "7f1b32b81b820d02614f8895ac1d4eac");
 168             test.check(4,
 169                        "b83b533708bf535d0aa6e52980d53b78",
 170                        "feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",
 171                        "42831ec2217774244b7221b784d0d49c" +
 172                        "e3aa212f2c02a4e035c17e2329aca12e" +
 173                        "21d514b25466931c7d8f6a5aac84aa05" +
 174                        "1ba30b396a0aac973d58e091",
 175                        "698e57f70e6ecc7fd9463b7260a9ae5f");
 176             test.check(5, "b83b533708bf535d0aa6e52980d53b78",
 177                        "feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",
 178                        "61353b4c2806934a777ff51fa22a4755" +
 179                        "699b2a714fcdc6f83766e5f97b6c7423" +
 180                        "73806900e49f24b22b097544d4896b42" +
 181                        "4989b5e1ebac0f07c23f4598",
 182                        "df586bb4c249b92cb6922877e444d37b");
 183             i++;
 184         }
 185     }
 186 }
< prev index next >