jdk/test/com/sun/crypto/provider/Cipher/DES/TextPKCS5PaddingTest.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) 2001, 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
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    24
/*
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    25
 * @test
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    26
 * @bug 8048604
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    27
 * @summary This test checks boundary conditions for testing
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    28
 *          ShortBufferException.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    29
 */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    30
import static java.lang.System.out;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    31
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    32
import java.security.AlgorithmParameters;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    33
import java.security.Provider;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    34
import java.security.Security;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    35
import javax.crypto.BadPaddingException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    36
import javax.crypto.Cipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    37
import javax.crypto.KeyGenerator;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    38
import javax.crypto.SecretKey;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    39
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    40
public class TextPKCS5PaddingTest {
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 plain text.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    43
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    44
    private static final byte[] PLAIN_TEXT = {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    45
        0b10001, 0b10001, 0b10001, 0b10001,
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    46
        0b10001, 0b10001, 0b11,    0b11
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    47
    };
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    48
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    50
        Provider provider = Security.getProvider("SunJCE");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    51
        if (provider == null) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    52
            throw new RuntimeException("SunJCE provider not exist");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    53
        }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    54
        // generate no-padding cipher with secret key
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    55
        Cipher c = Cipher.getInstance("DES/CBC/NoPadding", provider);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    56
        KeyGenerator kgen = KeyGenerator.getInstance("DES", provider);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    57
        SecretKey skey = kgen.generateKey();
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    58
        // this is the improperly padded plaintext
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    59
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    60
        c.init(Cipher.ENCRYPT_MODE, skey);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    61
        // encrypt plaintext
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    62
        byte[] cipher = c.doFinal(PLAIN_TEXT);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    63
        AlgorithmParameters params = c.getParameters();
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    64
        // generate cipher that enforces PKCS5 padding
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    65
        c = Cipher.getInstance("DES/CBC/PKCS5Padding", provider);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    66
        c.init(Cipher.DECRYPT_MODE, skey, params);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    67
        try {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    68
            c.doFinal(cipher);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    69
            throw new RuntimeException(
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    70
                    "ERROR: Expected BadPaddingException not thrown");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    71
        } catch (BadPaddingException expected) {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    72
            out.println("Expected BadPaddingException thrown");
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    73
        }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    74
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    75
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    76
}