jdk/test/javax/crypto/KeyGenerator/TestKGParity.java
author tyan
Wed, 23 Sep 2015 21:28:14 +0000
changeset 32766 2322e6c2a032
parent 30046 cf2c86e1819e
child 32767 7c9edd1bbf35
permissions -rw-r--r--
8048604: Tests for strong crypto ciphers Summary: SQE test co-location effort Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28299
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     1
/*
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     4
 *
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     8
 *
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    13
 * accompanied this code).
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    14
 *
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    18
 *
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    21
 * questions.
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    22
 */
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    23
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    24
import java.io.PrintStream;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    25
import java.lang.String;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    26
import java.lang.System;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    27
import java.security.Provider;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    28
import java.security.SecureRandom;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    29
import java.security.Security;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    30
import javax.crypto.KeyGenerator;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    31
import static java.lang.System.out;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    32
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    33
/*
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    34
 * @test
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    35
 * @bug 8048607
32766
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents: 30046
diff changeset
    36
 * @library ../../../com/sun/crypto/provider/Cipher/DES/TestUtility.java
28299
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    37
 * @summary Test key generation of DES and DESEDE
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 28299
diff changeset
    38
 * @key randomness
28299
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    39
 */
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    40
public class TestKGParity {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    41
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    42
    private static final String[] ALGORITHM_ARR = {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    43
        "deS", "DesEDE"
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    44
    };
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    45
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    46
    public static void main(String argv[]) throws Exception {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    47
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    48
        TestKGParity test = new TestKGParity();
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    49
        test.run();
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    50
    }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    51
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    52
    private void run() throws Exception {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    53
        Provider[] providers = Security.getProviders();
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    54
        for (Provider p : providers) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    55
            String prvName = p.getName();
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    56
            if (prvName.startsWith("SunJCE")
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    57
                    || prvName.startsWith("SunPKCS11-")) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    58
                for (String algorithm : ALGORITHM_ARR) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    59
                    if (!runTest(p, algorithm)) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    60
                        throw new RuntimeException(
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    61
                                "Test failed with provider/algorithm:"
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    62
                                        + p.getName() + "/" + algorithm);
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    63
                    } else {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    64
                        out.println("Test passed with provider/algorithm:"
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    65
                                + p.getName() + "/" + algorithm);
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    66
                    }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    67
                }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    68
            }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    69
        }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    70
    }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    71
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    72
    public boolean runTest(Provider p, String algo) throws Exception {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    73
        byte[] keyValue = null;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    74
        try {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    75
            // Initialization
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    76
            SecureRandom sRdm = new SecureRandom();
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    77
            KeyGenerator kg = KeyGenerator.getInstance(algo, p);
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    78
            kg.init(sRdm);
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    79
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    80
            // Generate a SecretKey and retrieve its value
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    81
            keyValue = kg.generateKey().getEncoded();
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    82
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    83
            // Verify its parity in the unit of byte
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    84
            for (int i = 0; i < keyValue.length; i++) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    85
                if (!checkParity(keyValue[i])) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    86
                    out.println("Testing: "
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    87
                        + p.getName()
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    88
                        + "/"
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    89
                        + algo
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    90
                        + " failed when verify its parity in the unit of byte:"
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    91
                        + TestUtility.hexDump(keyValue, i));
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    92
                    return false;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    93
                }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    94
            }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    95
            return true;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    96
        } catch (Exception ex) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    97
            out.println("Testing: " + p.getName() + "/" + algo
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    98
                    + " failed with unexpected exception");
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
    99
            ex.printStackTrace();
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   100
            throw ex;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   101
        }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   102
    }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   103
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   104
    private boolean checkParity(byte keyByte) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   105
        boolean even = false;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   106
        byte[] PARITY_BIT_MASK = {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   107
                (byte) 0x40, (byte) 0x20, (byte) 0x10, (byte) 0x08,
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   108
                (byte) 0x04, (byte) 0x02, (byte) 0x01
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   109
        };
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   110
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   111
        for (int i = 0; i < 7; i++) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   112
            if ((keyByte & PARITY_BIT_MASK[i]) > 0) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   113
                even = !even;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   114
            }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   115
        }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   116
        if (keyByte < 0) {
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   117
            even = !even;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   118
        }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   119
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   120
        return even;
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   121
    }
7a7e7c08a9b5 8048607: Test key generation of DES and DESEDE
xuelei
parents:
diff changeset
   122
}