src/java.base/share/classes/com/sun/crypto/provider/CipherFeedback.java
author mbalao
Tue, 12 Nov 2019 00:30:55 -0300
changeset 59158 438337c846fb
parent 51052 080776992b29
permissions -rw-r--r--
8233404: System property to set the number of PBE iterations in JCEKS keystores Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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;
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
    30
import sun.security.util.ArrayUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class represents ciphers in cipher-feedback (CFB) mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>This mode is implemented independently of a particular cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Ciphers to which this mode should apply (e.g., DES) must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <i>plugged-in</i> using the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>NOTE: This class does not deal with buffering or padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Gigi Ankeny
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
final class CipherFeedback extends FeedbackCipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * encrypt/decrypt output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final byte[] k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * register value, initialized with iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final byte[] register;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * number of bytes for each stream unit, defaults to the blocksize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * of the embedded cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private int numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // variables for save/restore calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private byte[] registerSave = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    CipherFeedback(SymmetricCipher embeddedCipher, int numBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        super(embeddedCipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (numBytes > blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            numBytes = blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.numBytes = numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        k = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        register = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Gets the name of this feedback mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @return the string <code>CFB</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    String getFeedback() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return "CFB";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Initializes the cipher in the specified mode with the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * and iv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param decrypting flag indicating encryption or decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param algorithm the algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param iv the iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if ((key == null) || (iv == null) || (iv.length != blockSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new InvalidKeyException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.iv = iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // always encrypt mode for embedded cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        embeddedCipher.init(false, algorithm, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Resets the iv to its original value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * This is used when doFinal is called in the Cipher class, so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * cipher can be reused (with its original iv).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.arraycopy(iv, 0, register, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Save the current content of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void save() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (registerSave == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            registerSave = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.arraycopy(register, 0, registerSave, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Restores the content of this cipher to the previous saved one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    void restore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.arraycopy(registerSave, 0, register, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Performs encryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>The input plain text <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <code>plainOffset</code> and ending at
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   138
     * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * The result is stored in <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <code>cipherOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param plain the buffer with the input data to be encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param plainOffset the offset in <code>plain</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param plainLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param cipher the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param cipherOffset the offset in <code>cipher</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   147
     * @exception ProviderException if <code>plainLen</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   148
     * a multiple of the <code>numBytes</code>
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   149
     * @return the length of the encrypted data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   151
    int encrypt(byte[] plain, int plainOffset, int plainLen,
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   152
                byte[] cipher, int cipherOffset) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   153
        ArrayUtil.blockSizeCheck(plainLen, numBytes);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   154
        ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   155
        ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   156
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   157
        int nShift = blockSize - numBytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int loopCount = plainLen / numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   160
        for (; loopCount > 0 ;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   161
             plainOffset += numBytes, cipherOffset += numBytes,
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   162
             loopCount--) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   163
            embeddedCipher.encryptBlock(register, 0, k, 0);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   164
            if (nShift != 0) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   165
                System.arraycopy(register, numBytes, register, 0, nShift);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   166
            }
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   167
            for (int i = 0; i < numBytes; i++) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   168
                register[nShift + i] = cipher[i + cipherOffset] =
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   169
                        (byte)(k[i] ^ plain[i + plainOffset]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   171
        }
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   172
        return plainLen;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   173
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   175
    /**
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   176
     * Performs the last encryption operation.
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   177
     *
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   178
     * <p>The input plain text <code>plain</code>, starting at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   179
     * <code>plainOffset</code> and ending at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   180
     * <code>(plainOffset + plainLen - 1)</code>, is encrypted.
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   181
     * The result is stored in <code>cipher</code>, starting at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   182
     * <code>cipherOffset</code>.
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   183
     *
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   184
     * @param plain the buffer with the input data to be encrypted
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   185
     * @param plainOffset the offset in <code>plain</code>
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   186
     * @param plainLen the length of the input data
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   187
     * @param cipher the buffer for the result
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   188
     * @param cipherOffset the offset in <code>cipher</code>
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   189
     * @return the number of bytes placed into <code>cipher</code>
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   190
     */
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   191
    int encryptFinal(byte[] plain, int plainOffset, int plainLen,
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   192
                     byte[] cipher, int cipherOffset) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   193
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   194
        int oddBytes = plainLen % numBytes;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   195
        int len = encrypt(plain, plainOffset, (plainLen - oddBytes),
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   196
                          cipher, cipherOffset);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   197
        plainOffset += len;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   198
        cipherOffset += len;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   199
        if (oddBytes != 0) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   200
            embeddedCipher.encryptBlock(register, 0, k, 0);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   201
            for (int i = 0; i < oddBytes; i++) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   202
                 cipher[i + cipherOffset] =
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   203
                    (byte)(k[i] ^ plain[i + plainOffset]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   206
        return plainLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Performs decryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>The input cipher text <code>cipher</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <code>cipherOffset</code> and ending at
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   214
     * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * The result is stored in <code>plain</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <code>plainOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   218
     * @param cipher the buffer with the input data to be decrypted
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   219
     * @param cipherOffset the offset in <code>cipherOffset</code>
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   220
     * @param cipherLen the length of the input data
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   221
     * @param plain the buffer for the result
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   222
     * @param plainOffset the offset in <code>plain</code>
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   223
     * @exception ProviderException if <code>cipherLen</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   224
     * a multiple of the <code>numBytes</code>
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   225
     * @return the length of the decrypted data
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   226
     */
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   227
    int decrypt(byte[] cipher, int cipherOffset, int cipherLen,
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   228
                byte[] plain, int plainOffset) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   229
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   230
        ArrayUtil.blockSizeCheck(cipherLen, numBytes);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   231
        ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   232
        ArrayUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen);
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   233
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   234
        int nShift = blockSize - numBytes;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   235
        int loopCount = cipherLen / numBytes;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   236
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   237
        for (; loopCount > 0;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   238
             plainOffset += numBytes, cipherOffset += numBytes,
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   239
             loopCount--) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   240
            embeddedCipher.encryptBlock(register, 0, k, 0);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   241
            if (nShift != 0) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   242
                System.arraycopy(register, numBytes, register, 0, nShift);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   243
            }
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   244
            for (int i = 0; i < numBytes; i++) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   245
                register[i + nShift] = cipher[i + cipherOffset];
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   246
                plain[i + plainOffset]
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   247
                    = (byte)(cipher[i + cipherOffset] ^ k[i]);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   248
            }
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   249
        }
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   250
        return cipherLen;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   251
    }
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   252
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   253
    /**
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   254
     * Performs the last decryption operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   256
     * <p>The input cipher text <code>cipher</code>, starting at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   257
     * <code>cipherOffset</code> and ending at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   258
     * <code>(cipherOffset + cipherLen - 1)</code>, is decrypted.
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   259
     * The result is stored in <code>plain</code>, starting at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   260
     * <code>plainOffset</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param cipher the buffer with the input data to be decrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param cipherOffset the offset in <code>cipherOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param cipherLen the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param plain the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param plainOffset the offset in <code>plain</code>
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   267
     * @return the length of the decrypted data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   269
    int decryptFinal(byte[] cipher, int cipherOffset, int cipherLen,
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   270
                byte[] plain, int plainOffset) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   272
        int oddBytes = cipherLen % numBytes;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   273
        int len = decrypt(cipher, cipherOffset, (cipherLen - oddBytes),
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   274
                          plain, plainOffset);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   275
        cipherOffset += len;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   276
        plainOffset += len;
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   277
        if (oddBytes != 0) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   278
            embeddedCipher.encryptBlock(register, 0, k, 0);
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   279
            for (int i = 0; i < oddBytes; i++) {
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   280
                plain[i + plainOffset]
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   281
                    = (byte)(cipher[i + cipherOffset] ^ k[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   284
        return cipherLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}