test/jdk/javax/crypto/Cipher/CipherInputStreamExceptions.java
author valeriep
Thu, 25 Jul 2019 02:16:49 +0000
changeset 57538 445c32471dc6
parent 47216 71c04702a3d5
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:
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
     1
/*
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
     4
 *
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
     8
 *
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    13
 * accompanied this code).
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    14
 *
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    18
 *
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    21
 * questions.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    22
 */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    23
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    24
/*
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    25
 * @test
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    26
 * @bug 8064546
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    27
 * @summary Throw exceptions during reading but not closing of a
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    28
 * CipherInputStream:
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    29
 * - Make sure authenticated algorithms continue to throwing exceptions
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    30
 *   when the authentication tag fails verification.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    31
 * - Make sure other algorithms do not throw exceptions when the stream
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    32
 *   calls close() and only throw when read() errors.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    33
 */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    34
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    35
import java.io.ByteArrayInputStream;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    36
import java.io.IOException;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    37
import java.lang.Exception;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    38
import java.lang.RuntimeException;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    39
import java.lang.Throwable;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    40
import java.security.AlgorithmParameters;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    41
import javax.crypto.AEADBadTagException;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    42
import javax.crypto.Cipher;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    43
import javax.crypto.CipherInputStream;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    44
import javax.crypto.IllegalBlockSizeException;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    45
import javax.crypto.spec.IvParameterSpec;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    46
import javax.crypto.spec.SecretKeySpec;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    47
import javax.crypto.spec.GCMParameterSpec;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    48
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    49
public class CipherInputStreamExceptions {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    50
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    51
    static SecretKeySpec key = new SecretKeySpec(new byte[16], "AES");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    52
    static GCMParameterSpec gcmspec = new GCMParameterSpec(128, new byte[16]);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    53
    static IvParameterSpec iv = new IvParameterSpec(new byte[16]);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    54
    static boolean failure = false;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    55
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    56
    /* Full read stream, check that getMoreData() is throwing an exception
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    57
     * This test
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
    58
     *   1) Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    59
     *   2) Changes the last byte to invalidate the authetication tag.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    60
     *   3) Fully reads CipherInputStream to decrypt the message and closes
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    61
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    62
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    63
    static void gcm_AEADBadTag() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    64
        Cipher c;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    65
        byte[] read = new byte[200];
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    66
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    67
        System.out.println("Running gcm_AEADBadTag");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    68
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
    69
        // Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    70
        byte[] ct = encryptedText("GCM", 100);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    71
        // Corrupt the encrypted message
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    72
        ct = corruptGCM(ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    73
        // Create stream for decryption
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    74
        CipherInputStream in = getStream("GCM", ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    75
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    76
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    77
            int size = in.read(read);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    78
            throw new RuntimeException("Fail: CipherInputStream.read() " +
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    79
                    "returned " + size + " and didn't throw an exception.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    80
        } catch (IOException e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    81
            Throwable ec = e.getCause();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    82
            if (ec instanceof AEADBadTagException) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    83
                System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    84
            } else {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    85
                System.out.println("  Fail: " + ec.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    86
                throw new RuntimeException(ec);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    87
            }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    88
        } finally {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    89
            in.close();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    90
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    91
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    92
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    93
    /* Short read stream,
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    94
     * This test
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
    95
     *   1) Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    96
     *   2) Reads 100 bytes from stream to decrypt the message and closes
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    97
     *   3) Make sure no value is returned by read()
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    98
     *   4) Make sure no exception is thrown
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
    99
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   100
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   101
    static void gcm_shortReadAEAD() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   102
        Cipher c;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   103
        byte[] read = new byte[100];
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   104
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   105
        System.out.println("Running gcm_shortReadAEAD");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   106
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   107
        byte[] pt = new byte[600];
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   108
        pt[0] = 1;
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   109
        // Encrypt provided 600 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   110
        byte[] ct = encryptedText("GCM", pt);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   111
        // Create stream for decryption
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   112
        CipherInputStream in = getStream("GCM", ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   113
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   114
        int size = 0;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   115
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   116
            size = in.read(read);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   117
            in.close();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   118
            if (read.length != 100) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   119
                throw new RuntimeException("Fail: read size = " + read.length +
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   120
                        "should be 100.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   121
            }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   122
            if (read[0] != 1) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   123
                throw new RuntimeException("Fail: The decrypted text does " +
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   124
                        "not match the plaintext: '" + read[0] +"'");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   125
            }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   126
        } catch (IOException e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   127
            System.out.println("  Fail: " + e.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   128
            throw new RuntimeException(e.getCause());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   129
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   130
        System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   131
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   132
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   133
    /*
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   134
     * Verify doFinal() exception is suppressed when input stream is not
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   135
     * read before it is closed.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   136
     * This test:
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   137
     *   1) Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   138
     *   2) Changes the last byte to invalidate the authetication tag.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   139
     *   3) Opens a CipherInputStream and the closes it. Never reads from it.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   140
     *
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   141
     * There should be no exception thrown.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   142
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   143
    static void gcm_suppressUnreadCorrupt() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   144
        Cipher c;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   145
        byte[] read = new byte[200];
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   146
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   147
        System.out.println("Running supressUnreadCorrupt test");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   148
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   149
        // Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   150
        byte[] ct = encryptedText("GCM", 100);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   151
        // Corrupt the encrypted message
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   152
        ct = corruptGCM(ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   153
        // Create stream for decryption
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   154
        CipherInputStream in = getStream("GCM", ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   155
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   156
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   157
            in.close();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   158
            System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   159
        } catch (IOException e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   160
            System.out.println("  Fail: " + e.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   161
            throw new RuntimeException(e.getCause());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   162
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   163
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   164
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   165
    /*
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   166
     * Verify noexception thrown when 1 byte is read from a GCM stream
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   167
     * and then closed
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   168
     * This test:
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   169
     *   1) Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   170
     *   2) Read one byte from the stream, expect no exception thrown.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   171
     *   4) Close stream,expect no exception thrown.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   172
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   173
    static void gcm_oneReadByte() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   174
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   175
        System.out.println("Running gcm_oneReadByte test");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   176
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   177
        // Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   178
        byte[] ct = encryptedText("GCM", 100);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   179
        // Create stream for decryption
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   180
        CipherInputStream in = getStream("GCM", ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   181
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   182
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   183
            in.read();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   184
            System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   185
        } catch (Exception e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   186
            System.out.println("  Fail: " + e.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   187
            throw new RuntimeException(e.getCause());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   188
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   189
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   190
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   191
    /*
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   192
     * Verify exception thrown when 1 byte is read from a corrupted GCM stream
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   193
     * and then closed
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   194
     * This test:
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   195
     *   1) Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   196
     *   2) Changes the last byte to invalidate the authetication tag.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   197
     *   3) Read one byte from the stream, expect exception thrown.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   198
     *   4) Close stream,expect no exception thrown.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   199
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   200
    static void gcm_oneReadByteCorrupt() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   201
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   202
        System.out.println("Running gcm_oneReadByteCorrupt test");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   203
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   204
        // Encrypt 100 bytes with AES/GCM/NoPadding
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   205
        byte[] ct = encryptedText("GCM", 100);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   206
        // Corrupt the encrypted message
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   207
        ct = corruptGCM(ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   208
        // Create stream for decryption
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   209
        CipherInputStream in = getStream("GCM", ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   210
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   211
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   212
            in.read();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   213
            System.out.println("  Fail. No exception thrown.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   214
        } catch (IOException e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   215
            Throwable ec = e.getCause();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   216
            if (ec instanceof AEADBadTagException) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   217
                System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   218
            } else {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   219
                System.out.println("  Fail: " + ec.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   220
                throw new RuntimeException(ec);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   221
            }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   222
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   223
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   224
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   225
    /* Check that close() does not throw an exception with full message in
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   226
     * CipherInputStream's ibuffer.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   227
     * This test:
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   228
     *   1) Encrypts a 97 byte message with AES/CBC/PKCS5Padding
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   229
     *   2) Create a stream that sends 96 bytes.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   230
     *   3) Read stream once,
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   231
     *   4) Close and expect no exception
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   232
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   233
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   234
    static void cbc_shortStream() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   235
        Cipher c;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   236
        AlgorithmParameters params;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   237
        byte[] read = new byte[200];
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   238
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   239
        System.out.println("Running cbc_shortStream");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   240
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   241
        // Encrypt 97 byte with AES/CBC/PKCS5Padding
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   242
        byte[] ct = encryptedText("CBC", 97);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   243
        // Create stream with only 96 bytes of encrypted data
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   244
        CipherInputStream in = getStream("CBC", ct, 96);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   245
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   246
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   247
            int size = in.read(read);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   248
            in.close();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   249
            if (size != 80) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   250
                throw new RuntimeException("Fail: CipherInputStream.read() " +
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   251
                        "returned " + size + ". Should have been 80");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   252
            }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   253
            System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   254
        } catch (IOException e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   255
            System.out.println("  Fail:  " + e.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   256
            throw new RuntimeException(e.getCause());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   257
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   258
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   259
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   260
    /* Check that close() does not throw an exception when the whole message is
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   261
     * inside the internal buffer (ibuffer) in CipherInputStream and we read
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   262
     * one byte and close the stream.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   263
     * This test:
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   264
     *   1) Encrypts a 400 byte message with AES/CBC/PKCS5Padding
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   265
     *   2) Read one byte from the stream
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   266
     *   3) Close and expect no exception
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   267
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   268
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   269
    static void cbc_shortRead400() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   270
        System.out.println("Running cbc_shortRead400");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   271
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   272
        // Encrypt 400 byte with AES/CBC/PKCS5Padding
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   273
        byte[] ct = encryptedText("CBC", 400);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   274
        // Create stream with encrypted data
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   275
        CipherInputStream in = getStream("CBC", ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   276
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   277
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   278
            in.read();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   279
            in.close();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   280
            System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   281
        } catch (IOException e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   282
            System.out.println("  Fail:  " + e.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   283
            throw new RuntimeException(e.getCause());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   284
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   285
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   286
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   287
    /* Check that close() does not throw an exception when the  inside the
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   288
     * internal buffer (ibuffer) in CipherInputStream does not contain the
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   289
     * whole message.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   290
     * This test:
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   291
     *   1) Encrypts a 600 byte message with AES/CBC/PKCS5Padding
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   292
     *   2) Read one byte from the stream
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   293
     *   3) Close and expect no exception
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   294
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   295
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   296
    static void cbc_shortRead600() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   297
        System.out.println("Running cbc_shortRead600");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   298
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   299
        // Encrypt 600 byte with AES/CBC/PKCS5Padding
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   300
        byte[] ct = encryptedText("CBC", 600);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   301
        // Create stream with encrypted data
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   302
        CipherInputStream in = getStream("CBC", ct);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   303
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   304
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   305
            in.read();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   306
            in.close();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   307
            System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   308
        } catch (IOException e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   309
            System.out.println("  Fail:  " + e.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   310
            throw new RuntimeException(e.getCause());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   311
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   312
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   313
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   314
    /* Check that exception is thrown when message is fully read
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   315
     * This test:
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   316
     *   1) Encrypts a 96 byte message with AES/CBC/PKCS5Padding
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   317
     *   2) Create a stream that sends 95 bytes.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   318
     *   3) Read stream to the end
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   319
     *   4) Expect IllegalBlockSizeException thrown
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   320
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   321
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   322
    static void cbc_readAllIllegalBlockSize() throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   323
        byte[] read = new byte[200];
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   324
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   325
        System.out.println("Running cbc_readAllIllegalBlockSize test");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   326
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   327
        // Encrypt 96 byte with AES/CBC/PKCS5Padding
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   328
        byte[] ct = encryptedText("CBC", 96);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   329
        // Create a stream with only 95 bytes of encrypted data
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   330
        CipherInputStream in = getStream("CBC", ct, 95);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   331
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   332
        try {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   333
            int s, size = 0;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   334
            while ((s = in.read(read)) != -1) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   335
                size += s;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   336
            }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   337
            throw new RuntimeException("Fail: No IllegalBlockSizeException. " +
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   338
                    "CipherInputStream.read() returned " + size);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   339
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   340
        } catch (IOException e) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   341
            Throwable ec = e.getCause();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   342
            if (ec instanceof IllegalBlockSizeException) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   343
                System.out.println("  Pass.");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   344
            } else {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   345
                System.out.println("  Fail: " + ec.getMessage());
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   346
                throw new RuntimeException(ec);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   347
            }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   348
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   349
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   350
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   351
    /* Generic method to create encrypted text */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   352
    static byte[] encryptedText(String mode, int length) throws Exception{
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   353
        return encryptedText(mode, new byte[length]);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   354
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   355
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   356
    /* Generic method to create encrypted text */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   357
    static byte[] encryptedText(String mode, byte[] pt) throws Exception{
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   358
        Cipher c;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   359
        if (mode.compareTo("GCM") == 0) {
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   360
            c = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   361
            c.init(Cipher.ENCRYPT_MODE, key, gcmspec);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   362
        } else if (mode.compareTo("CBC") == 0) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   363
            c = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   364
            c.init(Cipher.ENCRYPT_MODE, key, iv);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   365
        } else {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   366
            return null;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   367
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   368
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   369
        return c.doFinal(pt);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   370
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   371
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   372
    /* Generic method to get a properly setup CipherInputStream */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   373
    static CipherInputStream getStream(String mode, byte[] ct) throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   374
        return getStream(mode, ct, ct.length);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   375
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   376
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   377
    /* Generic method to get a properly setup CipherInputStream */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   378
    static CipherInputStream getStream(String mode, byte[] ct, int length)
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   379
            throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   380
        Cipher c;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   381
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   382
        if (mode.compareTo("GCM") == 0) {
57538
445c32471dc6 8180392: SunJCE provider should throw exceptions for unsupported mode and padding combinations
valeriep
parents: 47216
diff changeset
   383
            c = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE");
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   384
            c.init(Cipher.DECRYPT_MODE, key, gcmspec);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   385
        } else if (mode.compareTo("CBC") == 0) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   386
            c = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   387
            c.init(Cipher.DECRYPT_MODE, key, iv);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   388
        } else {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   389
            return null;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   390
        }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   391
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   392
        return new CipherInputStream(new ByteArrayInputStream(ct, 0, length), c);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   393
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   394
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   395
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   396
    /* Generic method for corrupting a GCM message.  Change the last
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   397
     * byte on of the authentication tag
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   398
     */
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   399
    static byte[] corruptGCM(byte[] ct) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   400
        ct[ct.length - 1] = (byte) (ct[ct.length - 1] + 1);
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   401
        return ct;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   402
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   403
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   404
    public static void main(String[] args) throws Exception {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   405
        gcm_AEADBadTag();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   406
        gcm_shortReadAEAD();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   407
        gcm_suppressUnreadCorrupt();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   408
        gcm_oneReadByte();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   409
        gcm_oneReadByteCorrupt();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   410
        cbc_shortStream();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   411
        cbc_shortRead400();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   412
        cbc_shortRead600();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   413
        cbc_readAllIllegalBlockSize();
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   414
    }
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents:
diff changeset
   415
}