src/java.base/share/classes/com/sun/crypto/provider/ElectronicCodeBook.java
author valeriep
Fri, 13 Jul 2018 02:36:42 +0000
changeset 51052 080776992b29
parent 47216 71c04702a3d5
child 57786 948ac3112da8
permissions -rw-r--r--
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73) Summary: Do bounds check per encryption/decryption call instead of per block Reviewed-by: ascarpino, redestad
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 electronic codebook (ECB) 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 ElectronicCodeBook extends FeedbackCipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    ElectronicCodeBook(SymmetricCipher embeddedCipher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super(embeddedCipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Gets the name of the feedback mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @return the name of the feedback mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    String getFeedback() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return "ECB";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Resets the iv to its original value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * This is used when doFinal is called in the Cipher class, so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * cipher can be reused (with its original iv).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // empty
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
     * Save the current content of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    void save() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Restores the content of this cipher to the previous saved one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    void restore() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Initializes the cipher in the specified mode with the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * and iv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param decrypting flag indicating encryption or decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param algorithm the algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param iv the iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if ((key == null) || (iv != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new InvalidKeyException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        embeddedCipher.init(decrypting, algorithm, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Performs encryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   101
     * <p>The input plain text <code>in</code>, starting at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   102
     * <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   103
     * is encrypted. The result is stored in <code>out</code>, starting at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   104
     * <code>outOff</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param in the buffer with the input data to be encrypted
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   107
     * @param inOff the offset in <code>plain</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param len the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param out the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param outOff the offset in <code>cipher</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   111
     * @exception ProviderException if <code>len</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   112
     * a multiple of the block size
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   113
     * @return the length of the encrypted data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   115
    int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   116
        ArrayUtil.blockSizeCheck(len, blockSize);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   117
        ArrayUtil.nullAndBoundsCheck(in, inOff, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   118
        ArrayUtil.nullAndBoundsCheck(out, outOff, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   119
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   120
        for (int i = len; i >= blockSize; i -= blockSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            embeddedCipher.encryptBlock(in, inOff, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            inOff += blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            outOff += blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   125
        return len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Performs decryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   131
     * <p>The input cipher text <code>in</code>, starting at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   132
     * <code>inOff</code> and ending at * <code>(inOff + len - 1)</code>,
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   133
     * is decrypted.The result is stored in <code>out</code>, starting at
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   134
     * <code>outOff</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param in the buffer with the input data to be decrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param inOff the offset in <code>cipherOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param len the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param out the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param outOff the offset in <code>plain</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   141
     * @exception ProviderException if <code>len</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   142
     * a multiple of the block size
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   143
     * @return the length of the decrypted data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   145
    int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   146
        ArrayUtil.blockSizeCheck(len, blockSize);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   147
        ArrayUtil.nullAndBoundsCheck(in, inOff, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   148
        ArrayUtil.nullAndBoundsCheck(out, outOff, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   149
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   150
        for (int i = len; i >= blockSize; i -= blockSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            embeddedCipher.decryptBlock(in, inOff, out, outOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            inOff += blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            outOff += blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   155
        return len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}