test/jdk/com/sun/crypto/provider/Cipher/AES/TestNoPaddingModes.java
author valeriep
Thu, 25 Jul 2019 02:16:49 +0000
changeset 57538 445c32471dc6
child 57586 f459f98aa30d
permissions -rw-r--r--
8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations Summary: Change JCA Cipher class to create obj to ensure the mode and padding combination is supported Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     1
/*
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     4
 *
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     8
 *
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    13
 * accompanied this code).
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    14
 *
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    18
 *
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    21
 * questions.
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    22
 */
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    23
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    24
/*
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    25
 * @test
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    26
 * @bug 8180392
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    27
 * @summary Ensure SunJCE provider throws exception for unsupported modes
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    28
 *          and padding combinations
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    29
 */
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    30
import java.security.*;
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    31
import java.security.spec.AlgorithmParameterSpec;
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    32
import javax.crypto.*;
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    33
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    34
public class TestNoPaddingModes {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    35
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    36
    // SunJCE only supports NoPadding with following modes
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    37
    private static final String[] MODES = {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    38
        "CTR", "CTS", "GCM"
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    39
    };
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    40
    private static final String[] PADDINGS = {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    41
        "PKCS5Padding", "ISO10126Padding"
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    42
    };
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    43
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    45
        Provider p = Security.getProvider("SunJCE");
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    46
        String transformation;
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    47
        for (String mode : MODES) {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    48
            for (String padding : PADDINGS) {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    49
                transformation = "AES/" + mode + "/" + padding;
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    50
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    51
                System.out.println("Test using " + transformation);
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    52
                try {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    53
                    Cipher c = Cipher.getInstance(transformation, "SunJCE");
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    54
                    throw new RuntimeException("=> Fail, no exception thrown");
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    55
                } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    56
                    System.out.println("=> Expected ex: " + ex);
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    57
                }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    58
                try {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    59
                    Cipher c = Cipher.getInstance(transformation, p);
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    60
                    throw new RuntimeException("=> Fail, no exception thrown");
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    61
                } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    62
                    System.out.println("=> Expected ex: " + ex);
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    63
                }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    64
            }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    65
        }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    66
        System.out.println("Test Passed");
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    67
    }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    68
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    69
    public static class MyCipImpl extends CipherSpi {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    70
        public MyCipImpl() {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    71
            super();
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    72
            System.out.println("MyCipImpl is created");
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    73
        }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    74
        protected void engineSetMode(String mode)
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    75
            throws NoSuchAlgorithmException {};
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    76
        protected void engineSetPadding(String padding)
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    77
            throws NoSuchPaddingException {};
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    78
        protected int engineGetBlockSize() { return 16; }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    79
        protected int engineGetOutputSize(int inputLen) { return 0; }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    80
        protected byte[] engineGetIV() { return null; }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    81
        protected AlgorithmParameters engineGetParameters() { return null; }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    82
        protected void engineInit(int opmode, Key key, SecureRandom random)
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    83
            throws InvalidKeyException {};
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    84
        protected void engineInit(int opmode, Key key,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    85
                                  AlgorithmParameterSpec params,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    86
                                  SecureRandom random)
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    87
            throws InvalidKeyException, InvalidAlgorithmParameterException {};
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    88
        protected void engineInit(int opmode, Key key,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    89
                                  AlgorithmParameters params,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    90
                                  SecureRandom random)
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    91
            throws InvalidKeyException, InvalidAlgorithmParameterException {};
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    92
        protected byte[] engineUpdate(byte[] input, int inputOffset,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    93
                                      int inputLen) { return null; }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    94
        protected int engineUpdate(byte[] input, int inputOffset,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    95
                                   int inputLen, byte[] output,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    96
                                   int outputOffset)
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    97
            throws ShortBufferException { return 0; };
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    98
        protected byte[] engineDoFinal(byte[] input, int inputOffset,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
    99
                                       int inputLen)
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   100
            throws IllegalBlockSizeException, BadPaddingException {
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   101
            return null;
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   102
        }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   103
        protected int engineDoFinal(byte[] input, int inputOffset,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   104
                                    int inputLen, byte[] output,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   105
                                    int outputOffset)
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   106
            throws ShortBufferException, IllegalBlockSizeException,
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   107
                   BadPaddingException  { return 0; }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   108
    }
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents:
diff changeset
   109
}