test/jdk/sun/security/pkcs11/Mac/MacSameTest.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) 1998, 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 8048603
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    27
 * @summary Check if doFinal and update operation result in same Mac
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    28
 * @author Yu-Ching Valerie Peng, Bill Situ, Alexander Fomin
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 MacSameTest
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    32
 * @run main/othervm MacSameTest sm
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    33
 * @key randomness
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    34
 */
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    35
28403
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    36
import java.security.InvalidKeyException;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    37
import java.security.NoSuchAlgorithmException;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    38
import java.security.NoSuchProviderException;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    39
import java.security.Provider;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    40
import java.security.SecureRandom;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    41
import java.util.List;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    42
import javax.crypto.Mac;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    43
import javax.crypto.spec.SecretKeySpec;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    44
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    45
public class MacSameTest extends PKCS11Test {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    46
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    47
    private static final int MESSAGE_SIZE = 25;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    48
    private static final int OFFSET = 5;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    49
    private static final int KEY_SIZE = 70;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    50
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    51
    /**
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    52
     * Initialize a message, instantiate a Mac object,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    53
     * initialize the object with a SecretKey,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    54
     * feed the message into the Mac object
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    55
     * all at once and get the output MAC as result1.
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    56
     * Reset the Mac object, chop the message into three pieces,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    57
     * feed into the Mac object sequentially, and get the output MAC as result2.
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    58
     * Finally, compare result1 and result2 and see if they are the same.
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    59
     *
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    60
     * @param args the command line arguments
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    61
     */
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    62
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30046
diff changeset
    63
        main(new MacSameTest(), args);
28403
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    64
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    65
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    66
    @Override
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    67
    public void main(Provider p) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    68
        List<String> algorithms = getSupportedAlgorithms("Mac", "Hmac", p);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    69
        boolean success = true;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    70
        for (String alg : algorithms) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    71
            try {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    72
                doTest(alg, p);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    73
            } catch (Exception e) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    74
                System.out.println("Unexpected exception: " + e);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    75
                e.printStackTrace();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    76
                success = false;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    77
            }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    78
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    79
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    80
        if (!success) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    81
            throw new RuntimeException("Test failed");
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    82
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    83
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    84
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    85
    private void doTest(String algo, Provider provider)
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    86
            throws NoSuchAlgorithmException, NoSuchProviderException,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    87
            InvalidKeyException {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    88
        System.out.println("Test " + algo);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    89
        Mac mac;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    90
        try {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    91
            mac = Mac.getInstance(algo, provider);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    92
        } catch (NoSuchAlgorithmException nsae) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    93
            if ("SunPKCS11-Solaris".equals(provider.getName())) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    94
                // depending on Solaris configuration,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    95
                // it can support HMAC or not with Mac
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    96
                System.out.println("Expected NoSuchAlgorithmException thrown: "
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    97
                        + nsae);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    98
                return;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
    99
            }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   100
            throw nsae;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   101
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   102
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   103
        byte[] plain = new byte[MESSAGE_SIZE];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   104
        for (int i = 0; i < MESSAGE_SIZE; i++) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   105
            plain[i] = (byte) (i % 256);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   106
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   107
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   108
        byte[] tail = new byte[plain.length - OFFSET];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   109
        System.arraycopy(plain, OFFSET, tail, 0, tail.length);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   110
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   111
        SecureRandom srdm = new SecureRandom();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   112
        byte[] keyVal = new byte[KEY_SIZE];
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   113
        srdm.nextBytes(keyVal);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   114
        SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   115
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   116
        mac.init(keySpec);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   117
        byte[] result1 = mac.doFinal(plain);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   118
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   119
        mac.reset();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   120
        mac.update(plain[0]);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   121
        mac.update(plain, 1, OFFSET - 1);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   122
        byte[] result2 = mac.doFinal(tail);
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   123
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   124
        if (!java.util.Arrays.equals(result1, result2)) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   125
            throw new RuntimeException("result1 and result2 are not the same");
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   126
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   127
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   128
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents:
diff changeset
   129
}