src/java.base/share/classes/com/sun/crypto/provider/CounterMode.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44641 jdk/src/java.base/share/classes/com/sun/crypto/provider/CounterMode.java@64c16b6d5165
child 51052 080776992b29
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44641
64c16b6d5165 8177784: Use CounterMode intrinsic for AES/GCM
ascarpino
parents: 42679
diff changeset
     2
 * Copyright (c) 2002, 2017, 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;
35258
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
    29
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
35258
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
    31
import jdk.internal.HotSpotIntrinsicCandidate;
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class represents ciphers in counter (CTR) mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>This mode is implemented independently of a particular cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Ciphers to which this mode should apply (e.g., DES) must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <i>plugged-in</i> using the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>NOTE: This class does not deal with buffering or padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
44641
64c16b6d5165 8177784: Use CounterMode intrinsic for AES/GCM
ascarpino
parents: 42679
diff changeset
    45
class CounterMode extends FeedbackCipher {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // current counter value
44641
64c16b6d5165 8177784: Use CounterMode intrinsic for AES/GCM
ascarpino
parents: 42679
diff changeset
    48
    final byte[] counter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // encrypted bytes of the previous counter value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final byte[] encryptedCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // number of bytes in encryptedCounter already used up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // variables for save/restore calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private byte[] counterSave = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private byte[] encryptedCounterSave = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int usedSave = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    CounterMode(SymmetricCipher embeddedCipher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        super(embeddedCipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        counter = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        encryptedCounter = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Gets the name of the feedback mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @return the name of the feedback mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    String getFeedback() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return "CTR";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Resets the iv to its original value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * This is used when doFinal is called in the Cipher class, so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * cipher can be reused (with its original iv).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.arraycopy(iv, 0, counter, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        used = blockSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Save the current content of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    void save() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (counterSave == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            counterSave = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            encryptedCounterSave = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.arraycopy(counter, 0, counterSave, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.arraycopy(encryptedCounter, 0, encryptedCounterSave, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        usedSave = used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Restores the content of this cipher to the previous saved one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    void restore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        System.arraycopy(counterSave, 0, counter, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.arraycopy(encryptedCounterSave, 0, encryptedCounter, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        used = usedSave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Initializes the cipher in the specified mode with the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * and iv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param decrypting flag indicating encryption or decryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param algorithm the algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param iv the iv
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if ((key == null) || (iv == null) || (iv.length != blockSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new InvalidKeyException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.iv = iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // always encrypt mode for embedded cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        embeddedCipher.init(false, algorithm, key);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <code>(plainOffset + len - 1)</code>, is encrypted.
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 in the buffer with the input data to be encrypted
35258
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
   143
     * @param inOff the offset in <code>plain</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param len the length of the input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param out the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param outOff the offset in <code>cipher</code>
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   147
     * @return the length of the encrypted data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   149
    int encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   150
        return crypt(in, inOff, len, out, outOff);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   153
    // CTR encrypt and decrypt are identical
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   154
    int decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   155
        return crypt(in, inOff, len, out, outOff);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Increment the counter value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static void increment(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int n = b.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        while ((n >= 0) && (++b[n] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Do the actual encryption/decryption operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Essentially we XOR the input plaintext/ciphertext stream with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * keystream generated by encrypting the counter values. Counter values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * are encrypted on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   174
    private int crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
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: 35626
diff changeset
   175
        if (len == 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: 35626
diff changeset
   176
            return 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: 35626
diff changeset
   177
        }
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 35626
diff changeset
   178
        Objects.checkFromIndexSize(inOff, len, in.length);
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 35626
diff changeset
   179
        Objects.checkFromIndexSize(outOff, len, out.length);
25fec8839946 8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents: 35626
diff changeset
   180
        return implCrypt(in, inOff, len, out, outOff);
35258
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
   181
    }
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
   182
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
   183
    // Implementation of crpyt() method. Possibly replaced with a compiler intrinsic.
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
   184
    @HotSpotIntrinsicCandidate
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
   185
    private int implCrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   186
        int result = len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        while (len-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (used >= blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                embeddedCipher.encryptBlock(counter, 0, encryptedCounter, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                increment(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                used = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            out[outOff++] = (byte)(in[inOff++] ^ encryptedCounter[used++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 5506
diff changeset
   195
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
35258
5b3c803c2d1e 8143925: Enhancing CounterMode.crypt() for AES
kvn
parents: 28241
diff changeset
   197
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}