jdk/src/java.base/share/classes/com/sun/crypto/provider/CipherBlockChaining.java
author rraghavan
Thu, 17 Nov 2016 01:17:26 -0800
changeset 42679 25fec8839946
parent 31671 362e0c0acece
permissions -rw-r--r--
8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0 Summary: Handled 0 length input case in Java wrapper method Reviewed-by: alanb, ascarpino, kvn, sherman, thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.InvalidKeyException;
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
    29
import java.security.ProviderException;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
    30
import java.util.Objects;
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
    31
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
    32
import jdk.internal.HotSpotIntrinsicCandidate;
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
    33
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class represents ciphers in cipher block chaining (CBC) mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>This mode is implemented independently of a particular cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Ciphers to which this mode should apply (e.g., DES) must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <i>plugged-in</i> using the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>NOTE: This class does not deal with buffering or padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Gigi Ankeny
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class CipherBlockChaining extends FeedbackCipher  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * random bytes that are initialized with iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected byte[] r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private byte[] k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // variables for save/restore calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private byte[] rSave = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    CipherBlockChaining(SymmetricCipher embeddedCipher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super(embeddedCipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        k = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        r = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Gets the name of this feedback mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return the string <code>CBC</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    String getFeedback() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return "CBC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Initializes the cipher in the specified mode with the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * and iv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param decrypting flag indicating encryption or decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param algorithm the algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param iv the iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if ((key == null) || (iv == null) || (iv.length != blockSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new InvalidKeyException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.iv = iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        embeddedCipher.init(decrypting, algorithm, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Resets the iv to its original value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * This is used when doFinal is called in the Cipher class, so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * cipher can be reused (with its original iv).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.arraycopy(iv, 0, r, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Save the current content of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    void save() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (rSave == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            rSave = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.arraycopy(r, 0, rSave, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Restores the content of this cipher to the previous saved one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    void restore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.arraycopy(rSave, 0, r, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Performs encryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p>The input plain text <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>plainOffset</code> and ending at
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   130
     * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The result is stored in <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>cipherOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param plain the buffer with the input data to be encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param plainLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param cipher the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param cipherOffset the offset in <code>cipher</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   139
     * @exception ProviderException if <code>len</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   140
     * a multiple of the block size
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 10336
diff changeset
   141
     * @return the length of the encrypted data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 10336
diff changeset
   143
    int encrypt(byte[] plain, int plainOffset, int plainLen,
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   144
                byte[] cipher, int cipherOffset) {
42679
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 31671
diff changeset
   145
        if (plainLen <= 0) {
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 31671
diff changeset
   146
            return plainLen;
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 31671
diff changeset
   147
        }
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   148
        cryptBlockSizeCheck(plainLen);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   149
        cryptNullAndBoundsCheck(plain, plainOffset, plainLen);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   150
        cryptNullAndBoundsCheck(cipher, cipherOffset, plainLen);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   151
        return implEncrypt(plain, plainOffset, plainLen,
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   152
                           cipher, cipherOffset);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   153
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   154
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   155
    @HotSpotIntrinsicCandidate
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   156
    private int implEncrypt(byte[] plain, int plainOffset, int plainLen,
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   157
                            byte[] cipher, int cipherOffset)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int endIndex = plainOffset + plainLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (; plainOffset < endIndex;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   162
             plainOffset += blockSize, cipherOffset += blockSize) {
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   163
            for (int i = 0; i < blockSize; i++) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   164
                k[i] = (byte)(plain[i + plainOffset] ^ r[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 10336
diff changeset
   169
        return plainLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Performs decryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p>The input cipher text <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>cipherOffset</code> and ending at
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   177
     * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * The result is stored in <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <code>plainOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p>It is also the application's responsibility to make sure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <code>init</code> has been called before this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * (This check is omitted here, to avoid double checking.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param cipher the buffer with the input data to be decrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param cipherOffset the offset in <code>cipherOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param cipherLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param plain the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param plainOffset the offset in <code>plain</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   190
     * @exception ProviderException if <code>len</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   191
     * a multiple of the block size
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 10336
diff changeset
   192
     * @return the length of the decrypted data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 10336
diff changeset
   194
    int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   195
                byte[] plain, int plainOffset) {
42679
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 31671
diff changeset
   196
        if (cipherLen <= 0) {
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 31671
diff changeset
   197
            return cipherLen;
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 31671
diff changeset
   198
        }
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   199
        cryptBlockSizeCheck(cipherLen);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   200
        cryptNullAndBoundsCheck(cipher, cipherOffset, cipherLen);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   201
        cryptNullAndBoundsCheck(plain, plainOffset, cipherLen);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   202
        return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   203
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   204
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   205
    @HotSpotIntrinsicCandidate
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   206
    private int implDecrypt(byte[] cipher, int cipherOffset, int cipherLen,
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   207
                            byte[] plain, int plainOffset)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int endIndex = cipherOffset + cipherLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        for (; cipherOffset < endIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             cipherOffset += blockSize, plainOffset += blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0);
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   214
            for (int i = 0; i < blockSize; i++) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   215
                plain[i + plainOffset] = (byte)(k[i] ^ r[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
21837
0c41fa97176a 8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents: 20752
diff changeset
   217
            System.arraycopy(cipher, cipherOffset, r, 0, blockSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 10336
diff changeset
   219
        return cipherLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   221
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   222
    private void cryptBlockSizeCheck(int len) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   223
        if ((len % blockSize) != 0) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   224
            throw new ProviderException("Internal error in input buffering");
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   225
        }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   226
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   227
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   228
    private static void cryptNullAndBoundsCheck(byte[] array, int offset, int len) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   229
        Objects.requireNonNull(array);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   230
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   231
        if (offset < 0 || offset >= array.length) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   232
            throw new ArrayIndexOutOfBoundsException(offset);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   233
        }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   234
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   235
        int endIndex = offset + len - 1;
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   236
        if (endIndex < 0 || endIndex >= array.length) {
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   237
            throw new ArrayIndexOutOfBoundsException(endIndex);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   238
        }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28241
diff changeset
   239
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
}