jdk/test/com/sun/crypto/provider/CICO/CICODESFuncTest.java
author tyan
Wed, 23 Sep 2015 21:28:14 +0000
changeset 32766 2322e6c2a032
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:
32766
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     1
/*
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     2
 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     4
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     8
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    13
 * accompanied this code).
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    14
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    18
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    21
 * questions.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    22
 */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    23
import static java.lang.System.out;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    24
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    25
import java.io.ByteArrayInputStream;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    26
import java.io.ByteArrayOutputStream;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    27
import java.io.IOException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    28
import java.security.GeneralSecurityException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    29
import java.security.NoSuchAlgorithmException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    30
import java.security.Provider;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    31
import java.security.Security;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    32
import java.security.spec.AlgorithmParameterSpec;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    33
import java.util.Arrays;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    34
import javax.crypto.CipherInputStream;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    35
import javax.crypto.CipherOutputStream;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    36
import javax.crypto.SecretKey;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    37
import javax.crypto.Cipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    38
import javax.crypto.KeyGenerator;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    39
import javax.crypto.spec.IvParameterSpec;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    40
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    41
/*
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    42
 * @test
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    43
 * @bug 8048604
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    44
 * @summary to verify cipherInputStream and cipherInputStream cipher function
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    45
 * @run main CICODESFuncTest
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    46
 */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    47
public class CICODESFuncTest {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    48
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    49
     * Algorithms name.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    50
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    51
    private static final String[] ALGORITHMS = { "DES", "DESede", "Blowfish" };
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    52
    private static final String[] MODES = { "ECB", "CBC", "CFB", "CFB24",
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    53
            "CFB32", "CFB40", "CFB72", "OFB", "OFB20", "OFB48", "OFB56",
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    54
            "OFB64", "PCBC" };
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    55
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    56
     * Padding mode.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    57
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    58
    private static final String[] PADDINGS = { "noPadding", "pkcs5padding" };
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    59
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    60
     * Plain text length.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    61
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    62
    private static final int TEXT_LENGTH = 80;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    63
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    64
     * Initialization vector length.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    65
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    66
    private static final int IV_LENGTH = 8;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    67
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    68
    public static void main(String[] args) throws Exception {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    69
        Provider provider = Security.getProvider("SunJCE");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    70
        if (provider == null) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    71
            throw new RuntimeException("SunJCE provider does not exist.");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    72
        }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    73
        for (String algorithm : ALGORITHMS) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    74
            for (String mode : MODES) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    75
                // We only test noPadding and pkcs5padding for CFB72, OFB20, ECB
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    76
                // PCBC and CBC. Otherwise test noPadding only.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    77
                int padKinds = 1;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    78
                if (mode.equalsIgnoreCase("CFB72")
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    79
                        || mode.equalsIgnoreCase("OFB20")
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    80
                        || mode.equalsIgnoreCase("ECB")
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    81
                        || mode.equalsIgnoreCase("PCBC")
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    82
                        || mode.equalsIgnoreCase("CBC")) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    83
                    padKinds = PADDINGS.length;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    84
                }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    85
                // PKCS5padding is meaningful only for ECB, CBC, PCBC
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    86
                for (int k = 0; k < padKinds; k++) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    87
                    for (ReadModel readMode : ReadModel.values()) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    88
                        runTest(provider, algorithm, mode, PADDINGS[k], readMode);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    89
                    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    90
                }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    91
            }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    92
        }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    93
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    94
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    95
    private static void runTest(Provider p, String algo, String mo, String pad,
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    96
            ReadModel whichRead) throws GeneralSecurityException, IOException {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    97
        // Do initialization
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    98
        byte[] plainText = TestUtilities.generateBytes(TEXT_LENGTH);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    99
        byte[] iv = TestUtilities.generateBytes(IV_LENGTH);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   100
        AlgorithmParameterSpec aps = new IvParameterSpec(iv);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   101
        try {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   102
            KeyGenerator kg = KeyGenerator.getInstance(algo, p);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   103
            out.println(algo + "/" + mo + "/" + pad + "/" + whichRead);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   104
            SecretKey key = kg.generateKey();
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   105
            Cipher ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   106
            if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   107
                throw new RuntimeException(
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   108
                        "NoSuchAlgorithmException not throw when mode"
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   109
                                + " is CFB72 or OFB20");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   110
            }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   111
            Cipher ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   112
            if ("ECB".equalsIgnoreCase(mo)) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   113
                ci1.init(Cipher.ENCRYPT_MODE, key);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   114
                ci2.init(Cipher.DECRYPT_MODE, key);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   115
            } else {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   116
                ci1.init(Cipher.ENCRYPT_MODE, key, aps);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   117
                ci2.init(Cipher.DECRYPT_MODE, key, aps);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   118
            }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   119
            ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   120
            try (CipherInputStream cInput
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   121
                    = new CipherInputStream(
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   122
                            new ByteArrayInputStream(plainText), ci1);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   123
                    CipherOutputStream ciOutput
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   124
                        = new CipherOutputStream(baOutput, ci2);) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   125
                // Read from the input and write to the output using 2 types
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   126
                // of buffering : byte[] and int
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   127
                whichRead.read(cInput, ciOutput, ci1, plainText.length);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   128
            }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   129
            // Verify input and output are same.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   130
            if (!Arrays.equals(plainText, baOutput.toByteArray())) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   131
                throw new RuntimeException("Test failed due to compare fail ");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   132
            }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   133
        } catch (NoSuchAlgorithmException nsaEx) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   134
            if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   135
                out.println("NoSuchAlgorithmException is expected for CFB72 and OFB20");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   136
            } else {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   137
                throw new RuntimeException("Unexpected exception testing: "
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   138
                        + algo + "/" + mo + "/" + pad + "/" + whichRead, nsaEx);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   139
            }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   140
        }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   141
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
   142
}