src/java.base/share/classes/com/sun/crypto/provider/GaloisCounterMode.java
author mbalao
Tue, 12 Nov 2019 00:30:55 -0300
changeset 59158 438337c846fb
parent 54029 4ff6c8365b69
permissions -rw-r--r--
8233404: System property to set the number of PBE iterations in JCEKS keystores Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
     1
/*
53721
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
     2
 * Copyright (c) 2013, 2019, 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
53721
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
    64
    // data size when buffer is divided up to aid in intrinsics
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
    65
    private static final int TRIGGERLEN = 65536;  // 64k
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
    66
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    67
    // buffer for AAD data; if null, meaning update has been called
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    68
    private ByteArrayOutputStream aadBuffer = new ByteArrayOutputStream();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    69
    private int sizeOfAAD = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    70
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    71
    // buffer for storing input in decryption, not used for encryption
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    72
    private ByteArrayOutputStream ibuffer = null;
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    73
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    74
    // in bytes; need to convert to bits (default value 128) when needed
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    75
    private int tagLenBytes = DEFAULT_TAG_LEN;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    76
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    77
    // these following 2 fields can only be initialized after init() is
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    78
    // called, e.g. after cipher key k is set, and STAY UNCHANGED
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    79
    private byte[] subkeyH = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    80
    private byte[] preCounterBlock = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    81
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    82
    private GCTR gctrPAndC = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    83
    private GHASH ghashAllToS = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    84
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    85
    // length of total data, i.e. len(C)
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    86
    private int processed = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    87
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    88
    // additional variables for save/restore calls
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    89
    private byte[] aadBufferSave = null;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    90
    private int sizeOfAADSave = 0;
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    91
    private byte[] ibufferSave = null;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    92
    private int processedSave = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    93
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    94
    // value must be 16-byte long; used by GCTR and GHASH as well
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    95
    static void increment32(byte[] value) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    96
        if (value.length != AES_BLOCK_SIZE) {
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    97
            // should never happen
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
    98
            throw new ProviderException("Illegal counter block length");
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
    99
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   100
        // start from last byte and only go over 4 bytes, i.e. total 32 bits
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   101
        int n = value.length - 1;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   102
        while ((n >= value.length - 4) && (++value[n] == 0)) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   103
            n--;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   104
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   105
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   106
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   107
    private static byte[] getLengthBlock(int ivLenInBytes) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   108
        long ivLen = ((long)ivLenInBytes) << 3;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   109
        byte[] out = new byte[AES_BLOCK_SIZE];
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   110
        out[8] = (byte)(ivLen >>> 56);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   111
        out[9] = (byte)(ivLen >>> 48);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   112
        out[10] = (byte)(ivLen >>> 40);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   113
        out[11] = (byte)(ivLen >>> 32);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   114
        out[12] = (byte)(ivLen >>> 24);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   115
        out[13] = (byte)(ivLen >>> 16);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   116
        out[14] = (byte)(ivLen >>> 8);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   117
        out[15] = (byte)ivLen;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   118
        return out;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   119
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   120
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   121
    private static byte[] getLengthBlock(int aLenInBytes, int cLenInBytes) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   122
        long aLen = ((long)aLenInBytes) << 3;
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   123
        long cLen = ((long)cLenInBytes) << 3;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   124
        byte[] out = new byte[AES_BLOCK_SIZE];
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   125
        out[0] = (byte)(aLen >>> 56);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   126
        out[1] = (byte)(aLen >>> 48);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   127
        out[2] = (byte)(aLen >>> 40);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   128
        out[3] = (byte)(aLen >>> 32);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   129
        out[4] = (byte)(aLen >>> 24);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   130
        out[5] = (byte)(aLen >>> 16);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   131
        out[6] = (byte)(aLen >>> 8);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   132
        out[7] = (byte)aLen;
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   133
        out[8] = (byte)(cLen >>> 56);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   134
        out[9] = (byte)(cLen >>> 48);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   135
        out[10] = (byte)(cLen >>> 40);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   136
        out[11] = (byte)(cLen >>> 32);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   137
        out[12] = (byte)(cLen >>> 24);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   138
        out[13] = (byte)(cLen >>> 16);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   139
        out[14] = (byte)(cLen >>> 8);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   140
        out[15] = (byte)cLen;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   141
        return out;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   142
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   143
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   144
    private static byte[] expandToOneBlock(byte[] in, int inOfs, int len) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   145
        if (len > AES_BLOCK_SIZE) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   146
            throw new ProviderException("input " + len + " too long");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   147
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   148
        if (len == AES_BLOCK_SIZE && inOfs == 0) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   149
            return in;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   150
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   151
            byte[] paddedIn = new byte[AES_BLOCK_SIZE];
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   152
            System.arraycopy(in, inOfs, paddedIn, 0, len);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   153
            return paddedIn;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   154
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   155
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   156
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   157
    private static byte[] getJ0(byte[] iv, byte[] subkeyH) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   158
        byte[] j0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   159
        if (iv.length == 12) { // 96 bits
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   160
            j0 = expandToOneBlock(iv, 0, iv.length);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   161
            j0[AES_BLOCK_SIZE - 1] = 1;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   162
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   163
            GHASH g = new GHASH(subkeyH);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   164
            int lastLen = iv.length % AES_BLOCK_SIZE;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   165
            if (lastLen != 0) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   166
                g.update(iv, 0, iv.length - lastLen);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   167
                byte[] padded =
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   168
                    expandToOneBlock(iv, iv.length - lastLen, lastLen);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   169
                g.update(padded);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   170
            } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   171
                g.update(iv);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   172
            }
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   173
            byte[] lengthBlock = getLengthBlock(iv.length);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   174
            g.update(lengthBlock);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   175
            j0 = g.digest();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   176
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   177
        return j0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   178
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   179
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   180
    private static void checkDataLength(int processed, int len) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   181
        if (processed > MAX_BUF_SIZE - len) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   182
            throw new ProviderException("SunJCE provider only supports " +
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   183
                "input size up to " + MAX_BUF_SIZE + " bytes");
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   184
        }
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   185
    }
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   186
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   187
    GaloisCounterMode(SymmetricCipher embeddedCipher) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   188
        super(embeddedCipher);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   189
        aadBuffer = new ByteArrayOutputStream();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   190
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   191
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   192
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   193
     * Gets the name of the feedback mechanism
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   194
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   195
     * @return the name of the feedback mechanism
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   196
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   197
    String getFeedback() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   198
        return "GCM";
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   199
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   200
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   201
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   202
     * Resets the cipher object to its original state.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   203
     * This is used when doFinal is called in the Cipher class, so that the
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   204
     * cipher can be reused (with its original key and iv).
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   205
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   206
    void reset() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   207
        if (aadBuffer == null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   208
            aadBuffer = new ByteArrayOutputStream();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   209
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   210
            aadBuffer.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   211
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   212
        if (gctrPAndC != null) gctrPAndC.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   213
        if (ghashAllToS != null) ghashAllToS.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   214
        processed = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   215
        sizeOfAAD = 0;
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   216
        if (ibuffer != null) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   217
            ibuffer.reset();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   218
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   219
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   220
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   221
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   222
     * Save the current content of this cipher.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   223
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   224
    void save() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   225
        processedSave = processed;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   226
        sizeOfAADSave = sizeOfAAD;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   227
        aadBufferSave =
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   228
            ((aadBuffer == null || aadBuffer.size() == 0)?
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   229
             null : aadBuffer.toByteArray());
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   230
        if (gctrPAndC != null) gctrPAndC.save();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   231
        if (ghashAllToS != null) ghashAllToS.save();
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   232
        if (ibuffer != null) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   233
            ibufferSave = ibuffer.toByteArray();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   234
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   235
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   236
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   237
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   238
     * Restores the content of this cipher to the previous saved one.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   239
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   240
    void restore() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   241
        processed = processedSave;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   242
        sizeOfAAD = sizeOfAADSave;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   243
        if (aadBuffer != null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   244
            aadBuffer.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   245
            if (aadBufferSave != null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   246
                aadBuffer.write(aadBufferSave, 0, aadBufferSave.length);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   247
            }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   248
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   249
        if (gctrPAndC != null) gctrPAndC.restore();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   250
        if (ghashAllToS != null) ghashAllToS.restore();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   251
        if (ibuffer != null) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   252
            ibuffer.reset();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   253
            ibuffer.write(ibufferSave, 0, ibufferSave.length);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   254
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   255
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   256
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   257
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   258
     * Initializes the cipher in the specified mode with the given key
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   259
     * and iv.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   260
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   261
     * @param decrypting flag indicating encryption or decryption
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   262
     * @param algorithm the algorithm name
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   263
     * @param key the key
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   264
     * @param iv the iv
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   265
     * @param tagLenBytes the length of tag in bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   266
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   267
     * @exception InvalidKeyException if the given key is inappropriate for
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   268
     * initializing this cipher
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   269
     */
49789
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   270
    @Override
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   271
    void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
49789
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   272
            throws InvalidKeyException, InvalidAlgorithmParameterException {
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   273
        init(decrypting, algorithm, key, iv, DEFAULT_TAG_LEN);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   274
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   275
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   276
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   277
     * Initializes the cipher in the specified mode with the given key
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   278
     * and iv.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   279
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   280
     * @param decrypting flag indicating encryption or decryption
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   281
     * @param algorithm the algorithm name
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   282
     * @param key the key
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   283
     * @param iv the iv
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   284
     * @param tagLenBytes the length of tag in bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   285
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   286
     * @exception InvalidKeyException if the given key is inappropriate for
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   287
     * initializing this cipher
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   288
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   289
    void init(boolean decrypting, String algorithm, byte[] keyValue,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   290
              byte[] ivValue, int tagLenBytes)
49789
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   291
              throws InvalidKeyException, InvalidAlgorithmParameterException {
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   292
        if (keyValue == null) {
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   293
            throw new InvalidKeyException("Internal error");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   294
        }
49789
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   295
        if (ivValue == null) {
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   296
            throw new InvalidAlgorithmParameterException("Internal error");
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   297
        }
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   298
        if (ivValue.length == 0) {
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   299
            throw new InvalidAlgorithmParameterException("IV is empty");
27b359322b1e 8193409: Improve AES supporting classes
apetcher
parents: 47216
diff changeset
   300
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   301
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   302
        // always encrypt mode for embedded cipher
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   303
        this.embeddedCipher.init(false, algorithm, keyValue);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   304
        this.subkeyH = new byte[AES_BLOCK_SIZE];
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   305
        this.embeddedCipher.encryptBlock(new byte[AES_BLOCK_SIZE], 0,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   306
                this.subkeyH, 0);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   307
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   308
        this.iv = ivValue.clone();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   309
        preCounterBlock = getJ0(iv, subkeyH);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   310
        byte[] j0Plus1 = preCounterBlock.clone();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   311
        increment32(j0Plus1);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   312
        gctrPAndC = new GCTR(embeddedCipher, j0Plus1);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   313
        ghashAllToS = new GHASH(subkeyH);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   314
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   315
        this.tagLenBytes = tagLenBytes;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   316
        if (aadBuffer == null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   317
            aadBuffer = new ByteArrayOutputStream();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   318
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   319
            aadBuffer.reset();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   320
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   321
        processed = 0;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   322
        sizeOfAAD = 0;
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   323
        if (decrypting) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   324
            ibuffer = new ByteArrayOutputStream();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   325
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   326
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   327
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   328
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   329
     * Continues a multi-part update of the Additional Authentication
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   330
     * Data (AAD), using a subset of the provided buffer. If this
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   331
     * cipher is operating in either GCM or CCM mode, all AAD must be
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   332
     * supplied before beginning operations on the ciphertext (via the
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   333
     * {@code update} and {@code doFinal} methods).
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   334
     * <p>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   335
     * NOTE: Given most modes do not accept AAD, default impl for this
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   336
     * method throws IllegalStateException.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   337
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   338
     * @param src the buffer containing the AAD
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   339
     * @param offset the offset in {@code src} where the AAD input starts
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   340
     * @param len the number of AAD bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   341
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   342
     * @throws IllegalStateException if this cipher is in a wrong state
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   343
     * (e.g., has not been initialized), does not accept AAD, or if
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   344
     * operating in either GCM or CCM mode and one of the {@code update}
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   345
     * methods has already been called for the active
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   346
     * encryption/decryption operation
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   347
     * @throws UnsupportedOperationException if this method
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   348
     * has not been overridden by an implementation
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   349
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   350
     * @since 1.8
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   351
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   352
    void updateAAD(byte[] src, int offset, int len) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   353
        if (aadBuffer != null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   354
            aadBuffer.write(src, offset, len);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   355
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   356
            // update has already been called
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   357
            throw new IllegalStateException
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   358
                ("Update has been called; no more AAD data");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   359
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   360
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   361
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   362
    // Feed the AAD data to GHASH, pad if necessary
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   363
    void processAAD() {
39750
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   364
        if (aadBuffer != null) {
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   365
            if (aadBuffer.size() > 0) {
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   366
                byte[] aad = aadBuffer.toByteArray();
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   367
                sizeOfAAD = aad.length;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   368
39750
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   369
                int lastLen = aad.length % AES_BLOCK_SIZE;
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   370
                if (lastLen != 0) {
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   371
                    ghashAllToS.update(aad, 0, aad.length - lastLen);
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   372
                    byte[] padded = expandToOneBlock(aad, aad.length - lastLen,
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   373
                                                     lastLen);
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   374
                    ghashAllToS.update(padded);
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   375
                } else {
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   376
                    ghashAllToS.update(aad);
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   377
                }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   378
            }
39750
982b75e31495 8149070: Enforce update ordering
valeriep
parents: 37579
diff changeset
   379
            aadBuffer = null;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   380
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   381
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   382
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   383
    // Utility to process the last block; used by encryptFinal and decryptFinal
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   384
    void doLastBlock(byte[] in, int inOfs, int len, byte[] out, int outOfs,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   385
                     boolean isEncrypt) throws IllegalBlockSizeException {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   386
        byte[] ct;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   387
        int ctOfs;
53721
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   388
        int ilen = len;  // internal length
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   389
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   390
        if (isEncrypt) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   391
            ct = out;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   392
            ctOfs = outOfs;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   393
        } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   394
            ct = in;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   395
            ctOfs = inOfs;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   396
        }
53721
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   397
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   398
        // Divide up larger data sizes to trigger CTR & GHASH intrinsic quicker
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   399
        if (len > TRIGGERLEN) {
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   400
            int i = 0;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   401
            int tlen;  // incremental lengths
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   402
            final int plen = AES_BLOCK_SIZE * 6;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   403
            // arbitrary formula to aid intrinsic without reaching buffer end
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   404
            final int count = len / 1024;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   405
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   406
            while (count > i) {
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   407
                tlen = gctrPAndC.update(in, inOfs, plen, out, outOfs);
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   408
                ghashAllToS.update(ct, ctOfs, tlen);
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   409
                inOfs += tlen;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   410
                outOfs += tlen;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   411
                ctOfs += tlen;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   412
                i++;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   413
            }
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   414
            ilen -= count * plen;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   415
            processed += count * plen;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   416
        }
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   417
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   418
        gctrPAndC.doFinal(in, inOfs, ilen, out, outOfs);
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   419
        processed += ilen;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   420
54029
4ff6c8365b69 8220165: Encryption using GCM results in RuntimeException- input length out of bound
ascarpino
parents: 53721
diff changeset
   421
        int lastLen = ilen % AES_BLOCK_SIZE;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   422
        if (lastLen != 0) {
54029
4ff6c8365b69 8220165: Encryption using GCM results in RuntimeException- input length out of bound
ascarpino
parents: 53721
diff changeset
   423
            ghashAllToS.update(ct, ctOfs, ilen - lastLen);
53721
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   424
            ghashAllToS.update(
54029
4ff6c8365b69 8220165: Encryption using GCM results in RuntimeException- input length out of bound
ascarpino
parents: 53721
diff changeset
   425
                    expandToOneBlock(ct, (ctOfs + ilen - lastLen), lastLen));
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   426
        } else {
53721
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   427
            ghashAllToS.update(ct, ctOfs, ilen);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   428
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   429
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   430
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   431
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   432
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   433
     * Performs encryption operation.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   434
     *
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   435
     * <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
   436
     * and ending at <code>(inOfs + len - 1)</code>, is encrypted. The result
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   437
     * is stored in <code>out</code>, starting at <code>outOfs</code>.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   438
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   439
     * @param in the buffer with the input data to be encrypted
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   440
     * @param inOfs the offset in <code>in</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   441
     * @param len the length of the input data
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   442
     * @param out the buffer for the result
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   443
     * @param outOfs the offset in <code>out</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   444
     * @exception ProviderException if <code>len</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   445
     * a multiple of the block size
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   446
     * @return the number of bytes placed into the <code>out</code> buffer
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   447
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   448
    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
   449
        ArrayUtil.blockSizeCheck(len, blockSize);
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   450
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   451
        checkDataLength(processed, len);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   452
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   453
        processAAD();
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   454
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   455
        if (len > 0) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   456
            ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   457
            ArrayUtil.nullAndBoundsCheck(out, outOfs, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   458
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   459
            gctrPAndC.update(in, inOfs, len, out, outOfs);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   460
            processed += len;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   461
            ghashAllToS.update(out, outOfs, len);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   462
        }
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   463
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   464
        return len;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   465
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   466
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   467
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   468
     * Performs encryption operation for the last time.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   469
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   470
     * @param in the input buffer with the data to be encrypted
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   471
     * @param inOfs the offset in <code>in</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   472
     * @param len the length of the input data
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   473
     * @param out the buffer for the encryption result
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   474
     * @param outOfs the offset in <code>out</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   475
     * @return the number of bytes placed into the <code>out</code> buffer
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   476
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   477
    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
   478
        throws IllegalBlockSizeException, ShortBufferException {
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   479
        if (len > MAX_BUF_SIZE - tagLenBytes) {
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   480
            throw new ShortBufferException
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   481
                ("Can't fit both data and tag into one buffer");
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   482
        }
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   483
        try {
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   484
            ArrayUtil.nullAndBoundsCheck(out, outOfs,
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   485
                (len + tagLenBytes));
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   486
        } catch (ArrayIndexOutOfBoundsException aiobe) {
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   487
            throw new ShortBufferException("Output buffer too small");
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   488
        }
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   489
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   490
        checkDataLength(processed, len);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   491
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   492
        processAAD();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   493
        if (len > 0) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   494
            ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   495
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   496
            doLastBlock(in, inOfs, len, out, outOfs, true);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   497
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   498
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   499
        byte[] lengthBlock =
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   500
            getLengthBlock(sizeOfAAD, processed);
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   501
        ghashAllToS.update(lengthBlock);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   502
        byte[] s = ghashAllToS.digest();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   503
        byte[] sOut = new byte[s.length];
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   504
        GCTR gctrForSToTag = new GCTR(embeddedCipher, this.preCounterBlock);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   505
        gctrForSToTag.doFinal(s, 0, s.length, sOut, 0);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   506
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   507
        System.arraycopy(sOut, 0, out, (outOfs + len), tagLenBytes);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   508
        return (len + tagLenBytes);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   509
    }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   510
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   511
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   512
     * Performs decryption operation.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   513
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   514
     * <p>The input cipher text <code>in</code>, starting at
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   515
     * <code>inOfs</code> and ending at <code>(inOfs + len - 1)</code>,
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   516
     * is decrypted. The result is stored in <code>out</code>, starting at
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   517
     * <code>outOfs</code>.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   518
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   519
     * @param in the buffer with the input data to be decrypted
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   520
     * @param inOfs the offset in <code>in</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   521
     * @param len the length of the input data
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   522
     * @param out the buffer for the result
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   523
     * @param outOfs the offset in <code>out</code>
28241
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   524
     * @exception ProviderException if <code>len</code> is not
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   525
     * a multiple of the block size
b7b95c178b76 8049312: AES/CICO test failed with on several modes
valeriep
parents: 25859
diff changeset
   526
     * @return the number of bytes placed into the <code>out</code> buffer
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   527
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   528
    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
   529
        ArrayUtil.blockSizeCheck(len, blockSize);
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   530
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   531
        checkDataLength(ibuffer.size(), len);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   532
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   533
        processAAD();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   534
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   535
        if (len > 0) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   536
            // store internally until decryptFinal is called because
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   537
            // spec mentioned that only return recovered data after tag
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   538
            // is successfully verified
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   539
            ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   540
            ibuffer.write(in, inOfs, len);
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   541
        }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   542
        return 0;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   543
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   544
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   545
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   546
     * Performs decryption operation for the last time.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   547
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   548
     * <p>NOTE: For cipher feedback modes which does not perform
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   549
     * special handling for the last few blocks, this is essentially
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   550
     * the same as <code>encrypt(...)</code>. Given most modes do
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   551
     * not do special handling, the default impl for this method is
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   552
     * to simply call <code>decrypt(...)</code>.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   553
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   554
     * @param in the input buffer with the data to be decrypted
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   555
     * @param inOfs the offset in <code>cipher</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   556
     * @param len the length of the input data
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   557
     * @param out the buffer for the decryption result
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   558
     * @param outOfs the offset in <code>plain</code>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   559
     * @return the number of bytes placed into the <code>out</code> buffer
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   560
     */
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   561
    int decryptFinal(byte[] in, int inOfs, int len,
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   562
                     byte[] out, int outOfs)
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   563
        throws IllegalBlockSizeException, AEADBadTagException,
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   564
        ShortBufferException {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   565
        if (len < tagLenBytes) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   566
            throw new AEADBadTagException("Input too short - need tag");
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   567
        }
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   568
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   569
        // do this check here can also catch the potential integer overflow
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   570
        // scenario for the subsequent output buffer capacity check.
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   571
        checkDataLength(ibuffer.size(), (len - tagLenBytes));
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   572
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   573
        try {
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   574
            ArrayUtil.nullAndBoundsCheck(out, outOfs,
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   575
                (ibuffer.size() + len) - tagLenBytes);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   576
        } catch (ArrayIndexOutOfBoundsException aiobe) {
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   577
            throw new ShortBufferException("Output buffer too small");
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   578
        }
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   579
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   580
        processAAD();
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   581
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   582
        ArrayUtil.nullAndBoundsCheck(in, inOfs, len);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 49789
diff changeset
   583
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   584
        // get the trailing tag bytes from 'in'
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   585
        byte[] tag = new byte[tagLenBytes];
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   586
        System.arraycopy(in, inOfs + len - tagLenBytes, tag, 0, tagLenBytes);
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   587
        len -= tagLenBytes;
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   588
53721
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   589
        // If decryption is in-place or there is buffered "ibuffer" data, copy
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   590
        // the "in" byte array into the ibuffer before proceeding.
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   591
        if (in == out || ibuffer.size() > 0) {
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   592
            if (len > 0) {
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   593
                ibuffer.write(in, inOfs, len);
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   594
            }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   595
53721
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   596
            // refresh 'in' to all buffered-up bytes
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   597
            in = ibuffer.toByteArray();
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   598
            inOfs = 0;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   599
            len = in.length;
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   600
            ibuffer.reset();
f35a8aaabcb9 8201633: Problems with AES-GCM native acceleration
ascarpino
parents: 51052
diff changeset
   601
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   602
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   603
        if (len > 0) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   604
            doLastBlock(in, inOfs, len, out, outOfs, false);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   605
        }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   606
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   607
        byte[] lengthBlock =
39752
365d487907ac 8146514: Enforce GCM limits
valeriep
parents: 39750
diff changeset
   608
            getLengthBlock(sizeOfAAD, processed);
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   609
        ghashAllToS.update(lengthBlock);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   610
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   611
        byte[] s = ghashAllToS.digest();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   612
        byte[] sOut = new byte[s.length];
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   613
        GCTR gctrForSToTag = new GCTR(embeddedCipher, this.preCounterBlock);
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   614
        gctrForSToTag.doFinal(s, 0, s.length, sOut, 0);
37579
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   615
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   616
        // check entire authentication tag for time-consistency
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   617
        int mismatch = 0;
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   618
        for (int i = 0; i < tagLenBytes; i++) {
37579
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   619
            mismatch |= tag[i] ^ sOut[i];
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   620
        }
37579
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   621
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   622
        if (mismatch != 0) {
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   623
            throw new AEADBadTagException("Tag mismatch!");
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   624
        }
839fba6035e9 8143945: Better GCM validation
ascarpino
parents: 28241
diff changeset
   625
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   626
        return len;
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   627
    }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   628
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   629
    // return tag length in bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   630
    int getTagLen() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   631
        return this.tagLenBytes;
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   632
    }
20752
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   633
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   634
    int getBufferedLength() {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   635
        if (ibuffer == null) {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   636
            return 0;
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   637
        } else {
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   638
            return ibuffer.size();
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   639
        }
f0f0acea9113 8012900: CICO ignores AAD in GCM mode
valeriep
parents: 16748
diff changeset
   640
    }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents:
diff changeset
   641
}