aboutsummaryrefslogtreecommitdiff
path: root/test/com/sun/crypto/provider/Cipher/AES/TestGHASH.java
blob: 8d05806dc5f3fa7c62dbb15df903326662747d7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2015, Red Hat, Inc.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8069072
 * @summary Test vectors for com.sun.crypto.provider.GHASH
 */
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;

public class TestGHASH {

    private final Constructor<?> GHASH;
    private final Method UPDATE;
    private final Method DIGEST;

    TestGHASH(String className) throws Exception {
        Class<?> cls = Class.forName(className);
        GHASH = cls.getDeclaredConstructor(byte[].class);
        GHASH.setAccessible(true);
        UPDATE = cls.getDeclaredMethod("update", byte[].class);
        UPDATE.setAccessible(true);
        DIGEST = cls.getDeclaredMethod("digest");
        DIGEST.setAccessible(true);
    }


    private Object newGHASH(byte[] H) throws Exception {
        return GHASH.newInstance(H);
    }

    private void updateGHASH(Object hash, byte[] data)
            throws Exception {
        UPDATE.invoke(hash, data);
    }

    private byte[] digestGHASH(Object hash) throws Exception {
        return (byte[]) DIGEST.invoke(hash);
    }

    private static final String HEX_DIGITS = "0123456789abcdef";

    private static String hex(byte[] bs) {
        StringBuilder sb = new StringBuilder(2 * bs.length);
        for (byte b : bs) {
            sb.append(HEX_DIGITS.charAt((b >> 4) & 0xF));
            sb.append(HEX_DIGITS.charAt(b & 0xF));
        }
        return sb.toString();
    }

    private static byte[] bytes(String hex) {
        if ((hex.length() & 1) != 0) {
            throw new AssertionError();
        }
        byte[] result = new byte[hex.length() / 2];
        for (int i = 0; i < result.length; ++i) {
            int a = HEX_DIGITS.indexOf(hex.charAt(2 * i));
            int b = HEX_DIGITS.indexOf(hex.charAt(2 * i + 1));
            if ((a | b) < 0) {
                if (a < 0) {
                    throw new AssertionError(
                            "bad character " + (int) hex.charAt(2 * i));
                }
                throw new AssertionError(
                        "bad character " + (int) hex.charAt(2 * i + 1));
            }
            result[i] = (byte) ((a << 4) | b);
        }
        return result;
    }

    private static byte[] bytes(long L0, long L1) {
        return ByteBuffer.allocate(16)
                .putLong(L0)
                .putLong(L1)
                .array();
    }

    private void check(int testCase, String H, String A,
            String C, String expected) throws Exception {
        int lenA = A.length() * 4;
        while ((A.length() % 32) != 0) {
            A += '0';
        }
        int lenC = C.length() * 4;
        while ((C.length() % 32) != 0) {
            C += '0';
        }

        Object hash = newGHASH(bytes(H));
        updateGHASH(hash, bytes(A));
        updateGHASH(hash, bytes(C));
        updateGHASH(hash, bytes(lenA, lenC));
        byte[] digest = digestGHASH(hash);
        String actual = hex(digest);
        if (!expected.equals(actual)) {
            throw new AssertionError(String.format("%d: expected %s, got %s",
                    testCase, expected, actual));
        }
    }

    public static void main(String[] args) throws Exception {
        TestGHASH test;
        if (args.length == 0) {
            test = new TestGHASH("com.sun.crypto.provider.GHASH");
        } else {
            test = new TestGHASH(args[0]);
        }

        // Test vectors from David A. McGrew, John Viega,
        // "The Galois/Counter Mode of Operation (GCM)", 2005.
        // <http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf>

        test.check(1, "66e94bd4ef8a2c3b884cfa59ca342b2e", "", "",
                "00000000000000000000000000000000");
        test.check(2,
                "66e94bd4ef8a2c3b884cfa59ca342b2e", "",
                "0388dace60b6a392f328c2b971b2fe78",
                "f38cbb1ad69223dcc3457ae5b6b0f885");
        test.check(3,
                "b83b533708bf535d0aa6e52980d53b78", "",
                "42831ec2217774244b7221b784d0d49c" +
                "e3aa212f2c02a4e035c17e2329aca12e" +
                "21d514b25466931c7d8f6a5aac84aa05" +
                "1ba30b396a0aac973d58e091473f5985",
                "7f1b32b81b820d02614f8895ac1d4eac");
        test.check(4,
                "b83b533708bf535d0aa6e52980d53b78",
                "feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",
                "42831ec2217774244b7221b784d0d49c" +
                "e3aa212f2c02a4e035c17e2329aca12e" +
                "21d514b25466931c7d8f6a5aac84aa05" +
                "1ba30b396a0aac973d58e091",
                "698e57f70e6ecc7fd9463b7260a9ae5f");
        test.check(5, "b83b533708bf535d0aa6e52980d53b78",
                "feedfacedeadbeeffeedfacedeadbeef" + "abaddad2",
                "61353b4c2806934a777ff51fa22a4755" +
                "699b2a714fcdc6f83766e5f97b6c7423" +
                "73806900e49f24b22b097544d4896b42" +
                "4989b5e1ebac0f07c23f4598",
                "df586bb4c249b92cb6922877e444d37b");
    }
}