src/java.base/share/classes/com/sun/crypto/provider/GaloisCounterMode.java
author valeriep
Fri, 13 Jul 2018 02:36:42 +0000
changeset 51052 080776992b29
parent 49789 27b359322b1e
child 53721 f35a8aaabcb9
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:
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
     1
/*
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
     4
 *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
16748
ed60b6527f64 8012048: JDK8 b85 source with GPL header errors
katleman
parents: 15008
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    10
 *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    15
 * accompanied this code).
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    16
 *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    20
 *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    23
 * questions.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    24
 */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    25
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    26
package com.sun.crypto.provider;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    27
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    28
import java.util.Arrays;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    29
import java.io.*;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    30
import java.security.*;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    31
import javax.crypto.*;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    32
import static com.sun.crypto.provider.AESConstants.AES_BLOCK_SIZE;
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
    33
import sun.security.util.ArrayUtil;
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
    34
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    35
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    36
/**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    37
 * This class represents ciphers in GaloisCounter (GCM) mode.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    38
 *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    39
 * <p>This mode currently should only be used w/ AES cipher.
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    40
 * Although no checking is done, caller should only pass AES
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    41
 * Cipher to the constructor.
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    42
 *
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    43
 * <p>NOTE: Unlike other modes, when used for decryption, this class
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    44
 * will buffer all processed outputs internally and won't return them
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    45
 * until the tag has been successfully verified.
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    46
 *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    47
 * @since 1.8
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    48
 */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    49
final class GaloisCounterMode extends FeedbackCipher {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    50
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    51
    static int DEFAULT_TAG_LEN = AES_BLOCK_SIZE;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    52
    static int DEFAULT_IV_LEN = 12; // in bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    53
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    54
    // In NIST SP 800-38D, GCM input size is limited to be no longer
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    55
    // than (2^36 - 32) bytes. Otherwise, the counter will wrap
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    56
    // around and lead to a leak of plaintext.
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    57
    // However, given the current GCM spec requirement that recovered
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    58
    // text can only be returned after successful tag verification,
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    59
    // we are bound by limiting the data size to the size limit of
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    60
    // java byte array, e.g. Integer.MAX_VALUE, since all data
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    61
    // can only be returned by the doFinal(...) call.
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    62
    private static final int MAX_BUF_SIZE = Integer.MAX_VALUE;
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
    63
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    64
    // buffer for AAD data; if null, meaning update has been called
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    65
    private ByteArrayOutputStream aadBuffer = new ByteArrayOutputStream();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    66
    private int sizeOfAAD = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    67
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    68
    // buffer for storing input in decryption, not used for encryption
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    69
    private ByteArrayOutputStream ibuffer = null;
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    70
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    71
    // in bytes; need to convert to bits (default value 128) when needed
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    72
    private int tagLenBytes = DEFAULT_TAG_LEN;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    73
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    74
    // these following 2 fields can only be initialized after init() is
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    75
    // called, e.g. after cipher key k is set, and STAY UNCHANGED
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    76
    private byte[] subkeyH = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    77
    private byte[] preCounterBlock = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    78
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    79
    private GCTR gctrPAndC = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    80
    private GHASH ghashAllToS = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    81
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    82
    // length of total data, i.e. len(C)
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    83
    private int processed = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    84
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    85
    // additional variables for save/restore calls
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    86
    private byte[] aadBufferSave = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    87
    private int sizeOfAADSave = 0;
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    88
    private byte[] ibufferSave = null;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    89
    private int processedSave = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    90
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    91
    // value must be 16-byte long; used by GCTR and GHASH as well
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    92
    static void increment32(byte[] value) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    93
        if (value.length != AES_BLOCK_SIZE) {
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    94
            // should never happen
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    95
            throw new ProviderException("Illegal counter block length");
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    96
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    97
        // start from last byte and only go over 4 bytes, i.e. total 32 bits
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    98
        int n = value.length - 1;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    99
        while ((n >= value.length - 4) && (++value[n] == 0)) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   100
            n--;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   101
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   102
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   103
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   104
    private static byte[] getLengthBlock(int ivLenInBytes) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   105
        long ivLen = ((long)ivLenInBytes) << 3;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   106
        byte[] out = new byte[AES_BLOCK_SIZE];
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   107
        out[8] = (byte)(ivLen >>> 56);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   108
        out[9] = (byte)(ivLen >>> 48);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   109
        out[10] = (byte)(ivLen >>> 40);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   110
        out[11] = (byte)(ivLen >>> 32);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   111
        out[12] = (byte)(ivLen >>> 24);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   112
        out[13] = (byte)(ivLen >>> 16);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   113
        out[14] = (byte)(ivLen >>> 8);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   114
        out[15] = (byte)ivLen;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   115
        return out;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   116
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   117
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   118
    private static byte[] getLengthBlock(int aLenInBytes, int cLenInBytes) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   119
        long aLen = ((long)aLenInBytes) << 3;
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   120
        long cLen = ((long)cLenInBytes) << 3;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   121
        byte[] out = new byte[AES_BLOCK_SIZE];
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   122
        out[0] = (byte)(aLen >>> 56);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   123
        out[1] = (byte)(aLen >>> 48);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   124
        out[2] = (byte)(aLen >>> 40);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   125
        out[3] = (byte)(aLen >>> 32);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   126
        out[4] = (byte)(aLen >>> 24);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   127
        out[5] = (byte)(aLen >>> 16);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   128
        out[6] = (byte)(aLen >>> 8);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   129
        out[7] = (byte)aLen;
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   130
        out[8] = (byte)(cLen >>> 56);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   131
        out[9] = (byte)(cLen >>> 48);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   132
        out[10] = (byte)(cLen >>> 40);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   133
        out[11] = (byte)(cLen >>> 32);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   134
        out[12] = (byte)(cLen >>> 24);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   135
        out[13] = (byte)(cLen >>> 16);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   136
        out[14] = (byte)(cLen >>> 8);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   137
        out[15] = (byte)cLen;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   138
        return out;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   139
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   140
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   141
    private static byte[] expandToOneBlock(byte[] in, int inOfs, int len) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   142
        if (len > AES_BLOCK_SIZE) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   143
            throw new ProviderException("input " + len + " too long");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   144
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   145
        if (len == AES_BLOCK_SIZE && inOfs == 0) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   146
            return in;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   147
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   148
            byte[] paddedIn = new byte[AES_BLOCK_SIZE];
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   149
            System.arraycopy(in, inOfs, paddedIn, 0, len);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   150
            return paddedIn;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   151
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   152
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   153
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   154
    private static byte[] getJ0(byte[] iv, byte[] subkeyH) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   155
        byte[] j0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   156
        if (iv.length == 12) { // 96 bits
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   157
            j0 = expandToOneBlock(iv, 0, iv.length);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   158
            j0[AES_BLOCK_SIZE - 1] = 1;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   159
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   160
            GHASH g = new GHASH(subkeyH);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   161
            int lastLen = iv.length % AES_BLOCK_SIZE;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   162
            if (lastLen != 0) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   163
                g.update(iv, 0, iv.length - lastLen);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   164
                byte[] padded =
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   165
                    expandToOneBlock(iv, iv.length - lastLen, lastLen);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   166
                g.update(padded);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   167
            } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   168
                g.update(iv);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   169
            }
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   170
            byte[] lengthBlock = getLengthBlock(iv.length);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   171
            g.update(lengthBlock);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   172
            j0 = g.digest();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   173
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   174
        return j0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   175
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   176
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   177
    private static void checkDataLength(int processed, int len) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   178
        if (processed > MAX_BUF_SIZE - len) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   179
            throw new ProviderException("SunJCE provider only supports " +
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   180
                "input size up to " + MAX_BUF_SIZE + " bytes");
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   181
        }
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   182
    }
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   183
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   184
    GaloisCounterMode(SymmetricCipher embeddedCipher) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   185
        super(embeddedCipher);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   186
        aadBuffer = new ByteArrayOutputStream();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   187
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   188
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   189
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   190
     * Gets the name of the feedback mechanism
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   191
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   192
     * @return the name of the feedback mechanism
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   193
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   194
    String getFeedback() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   195
        return "GCM";
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   196
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   197
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   198
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   199
     * Resets the cipher object to its original state.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   200
     * This is used when doFinal is called in the Cipher class, so that the
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   201
     * cipher can be reused (with its original key and iv).
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   202
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   203
    void reset() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   204
        if (aadBuffer == null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   205
            aadBuffer = new ByteArrayOutputStream();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   206
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   207
            aadBuffer.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   208
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   209
        if (gctrPAndC != null) gctrPAndC.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   210
        if (ghashAllToS != null) ghashAllToS.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   211
        processed = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   212
        sizeOfAAD = 0;
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   213
        if (ibuffer != null) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   214
            ibuffer.reset();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   215
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   216
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   217
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   218
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   219
     * Save the current content of this cipher.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   220
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   221
    void save() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   222
        processedSave = processed;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   223
        sizeOfAADSave = sizeOfAAD;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   224
        aadBufferSave =
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   225
            ((aadBuffer == null || aadBuffer.size() == 0)?
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   226
             null : aadBuffer.toByteArray());
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   227
        if (gctrPAndC != null) gctrPAndC.save();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   228
        if (ghashAllToS != null) ghashAllToS.save();
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   229
        if (ibuffer != null) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   230
            ibufferSave = ibuffer.toByteArray();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   231
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   232
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   233
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   234
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   235
     * Restores the content of this cipher to the previous saved one.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   236
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   237
    void restore() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   238
        processed = processedSave;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   239
        sizeOfAAD = sizeOfAADSave;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   240
        if (aadBuffer != null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   241
            aadBuffer.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   242
            if (aadBufferSave != null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   243
                aadBuffer.write(aadBufferSave, 0, aadBufferSave.length);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   244
            }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   245
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   246
        if (gctrPAndC != null) gctrPAndC.restore();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   247
        if (ghashAllToS != null) ghashAllToS.restore();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   248
        if (ibuffer != null) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   249
            ibuffer.reset();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   250
            ibuffer.write(ibufferSave, 0, ibufferSave.length);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   251
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   252
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   253
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   254
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   255
     * Initializes the cipher in the specified mode with the given key
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   256
     * and iv.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   257
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   258
     * @param decrypting flag indicating encryption or decryption
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   259
     * @param algorithm the algorithm name
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   260
     * @param key the key
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   261
     * @param iv the iv
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   262
     * @param tagLenBytes the length of tag in bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   263
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   264
     * @exception InvalidKeyException if the given key is inappropriate for
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   265
     * initializing this cipher
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   266
     */
49789
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   267
    @Override
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   268
    void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
49789
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   269
            throws InvalidKeyException, InvalidAlgorithmParameterException {
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   270
        init(decrypting, algorithm, key, iv, DEFAULT_TAG_LEN);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   271
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   272
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   273
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   274
     * Initializes the cipher in the specified mode with the given key
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   275
     * and iv.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   276
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   277
     * @param decrypting flag indicating encryption or decryption
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   278
     * @param algorithm the algorithm name
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   279
     * @param key the key
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   280
     * @param iv the iv
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   281
     * @param tagLenBytes the length of tag in bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   282
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   283
     * @exception InvalidKeyException if the given key is inappropriate for
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   284
     * initializing this cipher
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   285
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   286
    void init(boolean decrypting, String algorithm, byte[] keyValue,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   287
              byte[] ivValue, int tagLenBytes)
49789
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   288
              throws InvalidKeyException, InvalidAlgorithmParameterException {
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   289
        if (keyValue == null) {
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   290
            throw new InvalidKeyException("Internal error");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   291
        }
49789
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   292
        if (ivValue == null) {
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   293
            throw new InvalidAlgorithmParameterException("Internal error");
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   294
        }
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   295
        if (ivValue.length == 0) {
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   296
            throw new InvalidAlgorithmParameterException("IV is empty");
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   297
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   298
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   299
        // always encrypt mode for embedded cipher
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   300
        this.embeddedCipher.init(false, algorithm, keyValue);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   301
        this.subkeyH = new byte[AES_BLOCK_SIZE];
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   302
        this.embeddedCipher.encryptBlock(new byte[AES_BLOCK_SIZE], 0,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   303
                this.subkeyH, 0);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   304
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   305
        this.iv = ivValue.clone();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   306
        preCounterBlock = getJ0(iv, subkeyH);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   307
        byte[] j0Plus1 = preCounterBlock.clone();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   308
        increment32(j0Plus1);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   309
        gctrPAndC = new GCTR(embeddedCipher, j0Plus1);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   310
        ghashAllToS = new GHASH(subkeyH);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   311
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   312
        this.tagLenBytes = tagLenBytes;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   313
        if (aadBuffer == null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   314
            aadBuffer = new ByteArrayOutputStream();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   315
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   316
            aadBuffer.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   317
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   318
        processed = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   319
        sizeOfAAD = 0;
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   320
        if (decrypting) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   321
            ibuffer = new ByteArrayOutputStream();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   322
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   323
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   324
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   325
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   326
     * Continues a multi-part update of the Additional Authentication
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   327
     * Data (AAD), using a subset of the provided buffer. If this
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   328
     * cipher is operating in either GCM or CCM mode, all AAD must be
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   329
     * supplied before beginning operations on the ciphertext (via the
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   330
     * {@code update} and {@code doFinal} methods).
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   331
     * <p>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   332
     * NOTE: Given most modes do not accept AAD, default impl for this
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   333
     * method throws IllegalStateException.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   334
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   335
     * @param src the buffer containing the AAD
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   336
     * @param offset the offset in {@code src} where the AAD input starts
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   337
     * @param len the number of AAD bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   338
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   339
     * @throws IllegalStateException if this cipher is in a wrong state
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   340
     * (e.g., has not been initialized), does not accept AAD, or if
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   341
     * operating in either GCM or CCM mode and one of the {@code update}
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   342
     * methods has already been called for the active
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   343
     * encryption/decryption operation
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   344
     * @throws UnsupportedOperationException if this method
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   345
     * has not been overridden by an implementation
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   346
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   347
     * @since 1.8
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   348
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   349
    void updateAAD(byte[] src, int offset, int len) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   350
        if (aadBuffer != null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   351
            aadBuffer.write(src, offset, len);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   352
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   353
            // update has already been called
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   354
            throw new IllegalStateException
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   355
                ("Update has been called; no more AAD data");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   356
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   357
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   358
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   359
    // Feed the AAD data to GHASH, pad if necessary
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   360
    void processAAD() {
39750
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   361
        if (aadBuffer != null) {
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   362
            if (aadBuffer.size() > 0) {
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   363
                byte[] aad = aadBuffer.toByteArray();
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   364
                sizeOfAAD = aad.length;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   365
39750
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   366
                int lastLen = aad.length % AES_BLOCK_SIZE;
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   367
                if (lastLen != 0) {
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   368
                    ghashAllToS.update(aad, 0, aad.length - lastLen);
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   369
                    byte[] padded = expandToOneBlock(aad, aad.length - lastLen,
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   370
                                                     lastLen);
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   371
                    ghashAllToS.update(padded);
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   372
                } else {
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   373
                    ghashAllToS.update(aad);
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   374
                }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   375
            }
39750
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   376
            aadBuffer = null;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   377
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   378
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   379
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   380
    // Utility to process the last block; used by encryptFinal and decryptFinal
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   381
    void doLastBlock(byte[] in, int inOfs, int len, byte[] out, int outOfs,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   382
                     boolean isEncrypt) throws IllegalBlockSizeException {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   383
        // process data in 'in'
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   384
        gctrPAndC.doFinal(in, inOfs, len, out, outOfs);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   385
        processed += len;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   386
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   387
        byte[] ct;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   388
        int ctOfs;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   389
        if (isEncrypt) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   390
            ct = out;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   391
            ctOfs = outOfs;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   392
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   393
            ct = in;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   394
            ctOfs = inOfs;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   395
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   396
        int lastLen = len  % AES_BLOCK_SIZE;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   397
        if (lastLen != 0) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   398
            ghashAllToS.update(ct, ctOfs, len - lastLen);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   399
            byte[] padded =
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   400
                expandToOneBlock(ct, (ctOfs + len - lastLen), lastLen);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   401
            ghashAllToS.update(padded);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   402
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   403
            ghashAllToS.update(ct, ctOfs, len);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   404
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   405
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   406
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   407
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   408
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   409
     * Performs encryption operation.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   410
     *
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   411
     * <p>The input plain text <code>in</code>, starting at <code>inOfs</code>
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   412
     * and ending at <code>(inOfs + len - 1)</code>, is encrypted. The result
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   413
     * is stored in <code>out</code>, starting at <code>outOfs</code>.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   414
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   415
     * @param in the buffer with the input data to be encrypted
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   416
     * @param inOfs the offset in <code>in</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   417
     * @param len the length of the input data
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   418
     * @param out the buffer for the result
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   419
     * @param outOfs the offset in <code>out</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   420
     * @exception ProviderException if <code>len</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   421
     * a multiple of the block size
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   422
     * @return the number of bytes placed into the <code>out</code> buffer
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   423
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   424
    int encrypt(byte[] in, int inOfs, int len, byte[] out, int outOfs) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   425
        ArrayUtil.blockSizeCheck(len, blockSize);
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   426
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   427
        checkDataLength(processed, len);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   428
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   429
        processAAD();
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   430
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   431
        if (len > 0) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   432
            ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   433
            ArrayUtil.nullAndBoundsCheck(out, outOfs, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   434
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   435
            gctrPAndC.update(in, inOfs, len, out, outOfs);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   436
            processed += len;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   437
            ghashAllToS.update(out, outOfs, len);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   438
        }
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   439
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   440
        return len;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   441
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   442
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   443
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   444
     * Performs encryption operation for the last time.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   445
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   446
     * @param in the input buffer with the data to be encrypted
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   447
     * @param inOfs the offset in <code>in</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   448
     * @param len the length of the input data
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   449
     * @param out the buffer for the encryption result
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   450
     * @param outOfs the offset in <code>out</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   451
     * @return the number of bytes placed into the <code>out</code> buffer
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   452
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   453
    int encryptFinal(byte[] in, int inOfs, int len, byte[] out, int outOfs)
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   454
        throws IllegalBlockSizeException, ShortBufferException {
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   455
        if (len > MAX_BUF_SIZE - tagLenBytes) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   456
            throw new ShortBufferException
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   457
                ("Can't fit both data and tag into one buffer");
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   458
        }
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   459
        try {
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   460
            ArrayUtil.nullAndBoundsCheck(out, outOfs,
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   461
                (len + tagLenBytes));
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   462
        } catch (ArrayIndexOutOfBoundsException aiobe) {
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   463
            throw new ShortBufferException("Output buffer too small");
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   464
        }
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   465
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   466
        checkDataLength(processed, len);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   467
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   468
        processAAD();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   469
        if (len > 0) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   470
            ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   471
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   472
            doLastBlock(in, inOfs, len, out, outOfs, true);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   473
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   474
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   475
        byte[] lengthBlock =
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   476
            getLengthBlock(sizeOfAAD, processed);
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   477
        ghashAllToS.update(lengthBlock);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   478
        byte[] s = ghashAllToS.digest();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   479
        byte[] sOut = new byte[s.length];
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   480
        GCTR gctrForSToTag = new GCTR(embeddedCipher, this.preCounterBlock);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   481
        gctrForSToTag.doFinal(s, 0, s.length, sOut, 0);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   482
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   483
        System.arraycopy(sOut, 0, out, (outOfs + len), tagLenBytes);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   484
        return (len + tagLenBytes);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   485
    }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   486
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   487
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   488
     * Performs decryption operation.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   489
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   490
     * <p>The input cipher text <code>in</code>, starting at
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   491
     * <code>inOfs</code> and ending at <code>(inOfs + len - 1)</code>,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   492
     * is decrypted. The result is stored in <code>out</code>, starting at
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   493
     * <code>outOfs</code>.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   494
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   495
     * @param in the buffer with the input data to be decrypted
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   496
     * @param inOfs the offset in <code>in</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   497
     * @param len the length of the input data
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   498
     * @param out the buffer for the result
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   499
     * @param outOfs the offset in <code>out</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   500
     * @exception ProviderException if <code>len</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   501
     * a multiple of the block size
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   502
     * @return the number of bytes placed into the <code>out</code> buffer
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   503
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   504
    int decrypt(byte[] in, int inOfs, int len, byte[] out, int outOfs) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   505
        ArrayUtil.blockSizeCheck(len, blockSize);
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   506
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   507
        checkDataLength(ibuffer.size(), len);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   508
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   509
        processAAD();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   510
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   511
        if (len > 0) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   512
            // store internally until decryptFinal is called because
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   513
            // spec mentioned that only return recovered data after tag
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   514
            // is successfully verified
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   515
            ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   516
            ibuffer.write(in, inOfs, len);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   517
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   518
        return 0;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   519
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   520
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   521
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   522
     * Performs decryption operation for the last time.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   523
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   524
     * <p>NOTE: For cipher feedback modes which does not perform
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   525
     * special handling for the last few blocks, this is essentially
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   526
     * the same as <code>encrypt(...)</code>. Given most modes do
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   527
     * not do special handling, the default impl for this method is
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   528
     * to simply call <code>decrypt(...)</code>.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   529
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   530
     * @param in the input buffer with the data to be decrypted
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   531
     * @param inOfs the offset in <code>cipher</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   532
     * @param len the length of the input data
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   533
     * @param out the buffer for the decryption result
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   534
     * @param outOfs the offset in <code>plain</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   535
     * @return the number of bytes placed into the <code>out</code> buffer
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   536
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   537
    int decryptFinal(byte[] in, int inOfs, int len,
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   538
                     byte[] out, int outOfs)
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   539
        throws IllegalBlockSizeException, AEADBadTagException,
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   540
        ShortBufferException {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   541
        if (len < tagLenBytes) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   542
            throw new AEADBadTagException("Input too short - need tag");
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   543
        }
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   544
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   545
        // do this check here can also catch the potential integer overflow
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   546
        // scenario for the subsequent output buffer capacity check.
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   547
        checkDataLength(ibuffer.size(), (len - tagLenBytes));
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   548
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   549
        try {
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   550
            ArrayUtil.nullAndBoundsCheck(out, outOfs,
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   551
                (ibuffer.size() + len) - tagLenBytes);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   552
        } catch (ArrayIndexOutOfBoundsException aiobe) {
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   553
            throw new ShortBufferException("Output buffer too small");
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   554
        }
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   555
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   556
        processAAD();
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   557
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   558
        ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   559
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   560
        // get the trailing tag bytes from 'in'
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   561
        byte[] tag = new byte[tagLenBytes];
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   562
        System.arraycopy(in, inOfs + len - tagLenBytes, tag, 0, tagLenBytes);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   563
        len -= tagLenBytes;
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   564
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   565
        if (len > 0) {
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   566
            ibuffer.write(in, inOfs, len);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   567
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   568
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   569
        // refresh 'in' to all buffered-up bytes
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   570
        in = ibuffer.toByteArray();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   571
        inOfs = 0;
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   572
        len = in.length;
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   573
        ibuffer.reset();
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   574
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   575
        if (len > 0) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   576
            doLastBlock(in, inOfs, len, out, outOfs, false);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   577
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   578
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   579
        byte[] lengthBlock =
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   580
            getLengthBlock(sizeOfAAD, processed);
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   581
        ghashAllToS.update(lengthBlock);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   582
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   583
        byte[] s = ghashAllToS.digest();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   584
        byte[] sOut = new byte[s.length];
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   585
        GCTR gctrForSToTag = new GCTR(embeddedCipher, this.preCounterBlock);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   586
        gctrForSToTag.doFinal(s, 0, s.length, sOut, 0);
37579
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   587
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   588
        // check entire authentication tag for time-consistency
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   589
        int mismatch = 0;
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   590
        for (int i = 0; i < tagLenBytes; i++) {
37579
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   591
            mismatch |= tag[i] ^ sOut[i];
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   592
        }
37579
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   593
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   594
        if (mismatch != 0) {
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   595
            throw new AEADBadTagException("Tag mismatch!");
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   596
        }
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   597
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   598
        return len;
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   599
    }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   600
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   601
    // return tag length in bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   602
    int getTagLen() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   603
        return this.tagLenBytes;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   604
    }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   605
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   606
    int getBufferedLength() {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   607
        if (ibuffer == null) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   608
            return 0;
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   609
        } else {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   610
            return ibuffer.size();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   611
        }
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   612
    }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   613
}