test/jdk/sun/security/pkcs11/Mac/MacKAT.java
author jjiang
Wed, 15 Aug 2018 18:41:18 +0800
changeset 51460 97e361fe3433
parent 47216 71c04702a3d5
permissions -rw-r--r--
8164639: Configure PKCS11 tests to use user-supplied NSS libraries Summary: Provide system property "test.nss.lib.paths" for specifying a set of absolute paths to the custom NSS lib directories Reviewed-by: weijun, rhalade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28403
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
     1
/*
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
28403
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
     4
 *
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
     7
 * published by the Free Software Foundation.
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
     8
 *
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    13
 * accompanied this code).
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    14
 *
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    18
 *
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    21
 * questions.
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    22
 */
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    23
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    24
/*
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    25
 * @test
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    26
 * @bug 4846410 6313661 4963723
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    27
 * @summary Basic known-answer-test for Hmac algorithms
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    28
 * @author Andreas Sterbenz
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    29
 * @library /test/lib ..
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
    30
 * @modules jdk.crypto.cryptoki
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    31
 * @run main/othervm MacKAT
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    32
 * @run main/othervm MacKAT sm
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    33
 */
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    34
28403
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    35
import java.io.UnsupportedEncodingException;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    36
import java.security.Provider;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    37
import java.util.Arrays;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    38
import java.util.List;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    39
import java.util.Random;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    40
import javax.crypto.Mac;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    41
import javax.crypto.SecretKey;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    42
import javax.crypto.spec.SecretKeySpec;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    43
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    44
public class MacKAT extends PKCS11Test {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    45
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    46
    private final static byte[] ALONG, BLONG, BKEY, BKEY_20, DDDATA_50,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    47
            AAKEY_20, CDDATA_50, AAKEY_131;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    48
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    49
    static {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    50
        ALONG = new byte[1024 * 128];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    51
        Arrays.fill(ALONG, (byte)'a');
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    52
        BLONG = new byte[1024 * 128];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    53
        Random random = new Random(12345678);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    54
        random.nextBytes(BLONG);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    55
        BKEY = new byte[128];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    56
        random.nextBytes(BKEY);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    57
        BKEY_20 = new byte[20];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    58
        Arrays.fill(BKEY_20, (byte) 0x0b);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    59
        DDDATA_50 = new byte[50];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    60
        Arrays.fill(DDDATA_50, (byte) 0xdd);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    61
        AAKEY_20 = new byte[20];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    62
        Arrays.fill(AAKEY_20, (byte) 0xaa);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    63
        CDDATA_50 = new byte[50];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    64
        Arrays.fill(CDDATA_50, (byte) 0xcd);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    65
        AAKEY_131 = new byte[131];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    66
        Arrays.fill(AAKEY_131, (byte) 0xaa);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    67
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    68
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    69
    private final static Test[] tests = {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    70
        newMacTest("SslMacMD5",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    71
                ALONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    72
                "f4:ad:01:71:51:f6:89:56:72:a3:32:bf:d9:2a:f2:a5",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    73
                "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    74
        newMacTest("SslMacMD5",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    75
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    76
                "34:1c:ad:a0:95:57:32:f8:8e:80:8f:ee:b2:d8:23:e5",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    77
                "76:00:4a:72:98:9b:65:ec:2e:f1:43:c4:65:4a:13:71"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    78
        newMacTest("SslMacSHA1",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    79
                ALONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    80
                "11:c1:71:2e:61:be:4b:cf:bc:6d:e2:4c:58:ae:27:30:0b:24:a4:87",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    81
                "23:ae:dd:61:87:6c:7a:45:47:2f:2c:8f:ea:64:99:3e:27:5f:97:a5"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    82
        newMacTest("SslMacSHA1",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    83
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    84
                "84:af:57:0a:af:ef:16:93:90:50:da:88:f8:ad:1a:c5:66:6c:94:d0",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    85
                "9b:bb:e2:aa:9b:28:1c:95:0e:ea:30:21:98:a5:7e:31:9e:bf:5f:51"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    86
        newMacTest("HmacMD5",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    87
                ALONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    88
                "76:00:4a:72:98:9b:65:ec:2e:f1:43:c4:65:4a:13:71",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    89
                "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    90
        newMacTest("HmacMD5",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    91
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    92
                "6c:22:79:bb:34:9e:da:f4:f5:cf:df:0c:62:3d:59:e0",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    93
                "76:00:4a:72:98:9b:65:ec:2e:f1:43:c4:65:4a:13:71"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    94
        newMacTest("HmacMD5",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    95
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    96
                "e6:ad:00:c9:49:6b:98:fe:53:a2:b9:2d:7d:41:a2:03",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    97
                BKEY),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    98
        newMacTest("HmacSHA1",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    99
                ALONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   100
                "9e:b3:6e:35:fa:fb:17:2e:2b:f3:b0:4a:9d:38:83:c4:5f:6d:d9:00",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   101
                "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   102
        newMacTest("HmacSHA1",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   103
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   104
                "80:2d:5b:ea:08:df:a4:1f:e5:3e:1c:fa:fc:ad:dd:31:da:15:60:2c",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   105
                "76:00:4a:72:98:9b:65:ec:2e:f1:43:c4:65:4a:13:71"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   106
        newMacTest("HmacSHA1",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   107
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   108
                "a2:fa:2a:85:18:0e:94:b2:a5:e2:17:8b:2a:29:7a:95:cd:e8:aa:82",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   109
                BKEY),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   110
        newMacTest("HmacSHA256",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   111
                ALONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   112
                "3f:6d:08:df:0c:90:b0:e9:ed:13:4a:2e:c3:48:1d:3d:3e:61:2e:f1:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   113
                        + "30:c2:63:c4:58:57:03:c2:cb:87:15:07",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   114
                "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   115
        newMacTest("HmacSHA256",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   116
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   117
                "e2:4e:a3:b9:0b:b8:99:e4:71:cf:ca:9f:f8:4e:f0:34:8b:19:9f:33:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   118
                        + "4b:1a:b7:13:f7:c8:57:92:e3:03:74:78",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   119
                BKEY),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   120
        newMacTest("HmacSHA384",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   121
                ALONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   122
                "d0:f0:d4:54:1c:0a:6d:81:ed:15:20:d7:0c:96:06:61:a0:ff:c9:ff:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   123
                        + "91:e9:a0:cd:e2:45:64:9d:93:4c:a9:fa:89:ae:c0:90:e6:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   124
                        + "0b:a1:a0:56:80:57:3b:ed:4b:b0:71",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   125
                "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   126
        newMacTest("HmacSHA384",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   127
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   128
                "75:c4:ca:c7:f7:58:9d:d3:23:b1:1b:5c:93:2d:ec:7a:03:dc:8c:eb:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   129
                        + "8d:fe:79:46:4f:30:e7:99:62:de:44:e2:38:95:0e:79:91:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   130
                        + "78:2f:a4:05:0a:f0:17:10:38:a1:8e",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   131
                BKEY),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   132
        newMacTest("HmacSHA512",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   133
                ALONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   134
                "41:ea:4c:e5:31:3f:7c:18:0e:5e:95:a9:25:0a:10:58:e6:40:53:88:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   135
                        + "82:4f:5a:da:6f:29:de:04:7b:8e:d7:ed:7c:4d:b8:2a:48:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   136
                        + "2d:17:2a:2d:59:bb:81:9c:bf:33:40:04:77:44:fb:45:25:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   137
                        + "1f:fd:b9:29:f4:a6:69:a3:43:6f",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   138
                "1b:34:61:29:05:0d:73:db:25:d0:dd:64:06:29:f6:8a"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   139
        newMacTest("HmacSHA512",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   140
                BLONG,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   141
                "fb:cf:4b:c6:d5:49:5a:5b:0b:d9:2a:32:f5:fa:68:d2:68:a4:0f:ae:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   142
                        + "53:fc:49:12:e6:1d:53:cf:b2:cb:c5:c5:f2:2d:86:bd:14:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   143
                        + "61:30:c3:a6:6f:44:1f:77:9b:aa:a1:22:48:a9:dd:d0:45:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   144
                        + "86:d1:a1:82:53:13:c4:03:06:a3",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   145
                BKEY),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   146
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   147
        // Test vectors From RFC 4231
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   148
        newMacTest("HmacSHA224",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   149
                bytes("Hi There"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   150
                "89:6f:b1:12:8a:bb:df:19:68:32:10:7c:d4:9d:f3:3f:47:b4:b1:16:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   151
                        + "99:12:ba:4f:53:68:4b:22",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   152
                BKEY_20),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   153
        newMacTest("HmacSHA224",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   154
                bytes("what do ya want for nothing?"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   155
                "a3:0e:01:09:8b:c6:db:bf:45:69:0f:3a:7e:9e:6d:0f:8b:be:a2:a3:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   156
                        + "9e:61:48:00:8f:d0:5e:44",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   157
                bytes("Jefe")),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   158
        newMacTest("HmacSHA224",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   159
                DDDATA_50,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   160
                "7f:b3:cb:35:88:c6:c1:f6:ff:a9:69:4d:7d:6a:d2:64:93:65:b0:c1:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   161
                        + "f6:5d:69:d1:ec:83:33:ea",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   162
                AAKEY_20),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   163
        newMacTest("HmacSHA224",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   164
                CDDATA_50,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   165
                "6c:11:50:68:74:01:3c:ac:6a:2a:bc:1b:b3:82:62:7c:ec:6a:90:d8:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   166
                        + "6e:fc:01:2d:e7:af:ec:5a",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   167
                "01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f:10:11:12:13:14:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   168
                        + "15:16:17:18:19"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   169
        newMacTest("HmacSHA224",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   170
                bytes("Test Using Larger Than Block-Size Key - Hash Key First"),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   171
                "95:e9:a0:db:96:20:95:ad:ae:be:9b:2d:6f:0d:bc:e2:d4:99:f1:12:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   172
                        + "f2:d2:b7:27:3f:a6:87:0e",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   173
                AAKEY_131),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   174
        newMacTest("HmacSHA224",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   175
                bytes("This is a test using a larger than block-size key and "
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   176
                        + "a larger than block-size data. The key needs to be "
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   177
                        + "hashed before being used by the HMAC algorithm."),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   178
                "3a:85:41:66:ac:5d:9f:02:3f:54:d5:17:d0:b3:9d:bd:94:67:70:db:"
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   179
                        + "9c:2b:95:c9:f6:f5:65:d1",
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   180
                AAKEY_131),
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   181
    };
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   182
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   183
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 28403
diff changeset
   184
        main(new MacKAT(), args);
28403
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   185
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   186
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   187
    @Override
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   188
    public void main(Provider p) throws Exception {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   189
        long start = System.currentTimeMillis();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   190
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   191
        List<String> algorithms = getSupportedAlgorithms("Mac", "", p);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   192
        for (Test test : tests) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   193
            if(!algorithms.contains(test.getAlg())) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   194
                continue;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   195
            }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   196
            test.run(p);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   197
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   198
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   199
        System.out.println("All tests passed");
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   200
        long stop = System.currentTimeMillis();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   201
        System.out.println("Done (" + (stop - start) + " ms).");
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   202
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   203
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   204
    private static byte[] bytes(String s) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   205
        try {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   206
            return s.getBytes("UTF8");
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   207
        } catch (UnsupportedEncodingException e) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   208
            throw new RuntimeException(e);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   209
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   210
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   211
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   212
    private static Test newMacTest(String alg, byte[] input, String macvalue,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   213
            String key) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   214
        return new MacTest(alg, input, parse(macvalue), parse(key));
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   215
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   216
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   217
    private static Test newMacTest(String alg, byte[] input, String macvalue,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   218
            byte[] key) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   219
        return new MacTest(alg, input, parse(macvalue), key);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   220
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   221
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   222
    interface Test {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   223
        void run(Provider p) throws Exception;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   224
        String getAlg();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   225
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   226
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   227
    static class MacTest implements Test {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   228
        private final String alg;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   229
        private final byte[] input;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   230
        private final byte[] macvalue;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   231
        private final byte[] key;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   232
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   233
        MacTest(String alg, byte[] input, byte[] macvalue, byte[] key) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   234
            this.alg = alg;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   235
            this.input = input;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   236
            this.macvalue = macvalue;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   237
            this.key = key;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   238
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   239
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   240
        @Override
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   241
        public String getAlg() {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   242
            return alg;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   243
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   244
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   245
        @Override
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   246
        public void run(Provider p) throws Exception {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   247
            Mac mac = Mac.getInstance(alg, p);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   248
            SecretKey keySpec = new SecretKeySpec(key, alg);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   249
            mac.init(keySpec);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   250
            mac.update(input);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   251
            byte[] macv = mac.doFinal();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   252
            if (Arrays.equals(macvalue, macv) == false) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   253
                System.out.println("Mac test for " + alg + " failed:");
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   254
                if (input.length < 256) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   255
                    System.out.println("input:       "
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   256
                            + PKCS11Test.toString(input));
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   257
                }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   258
                System.out.println("key:        " + PKCS11Test.toString(key));
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   259
                System.out.println("macvalue:   "
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   260
                        + PKCS11Test.toString(macvalue));
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   261
                System.out.println("calculated: " + PKCS11Test.toString(macv));
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   262
                throw new Exception("Mac test for " + alg + " failed");
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   263
            }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   264
            System.out.println("passed: " + alg);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   265
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   266
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   267
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   268
}