src/java.base/share/classes/sun/security/util/ArrayUtil.java
author valeriep
Fri, 13 Jul 2018 02:36:42 +0000
changeset 51052 080776992b29
child 52942 746602d9682f
child 56863 c9d3ea14d270
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:
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     1
/*
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     4
 *
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    10
 *
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    15
 * accompanied this code).
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    16
 *
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    20
 *
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    23
 * questions.
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    24
 */
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    25
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    26
package sun.security.util;
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    27
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    28
import java.util.List;
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    29
import java.util.function.BiFunction;
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    30
import java.security.*;
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    31
import jdk.internal.util.Preconditions;
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    32
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    33
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    34
/**
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    35
 * This class holds the various utility methods for array range checks.
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    36
 */
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    37
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    38
public final class ArrayUtil {
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    39
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    40
    private static final BiFunction<String, List<Integer>,
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    41
            ArrayIndexOutOfBoundsException> AIOOBE_SUPPLIER =
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    42
            Preconditions.outOfBoundsExceptionFormatter
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    43
            (ArrayIndexOutOfBoundsException::new);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    44
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    45
    public static void blockSizeCheck(int len, int blockSize) {
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    46
        if ((len % blockSize) != 0) {
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    47
            throw new ProviderException("Internal error in input buffering");
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    48
        }
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    49
    }
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    50
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    51
    public static void nullAndBoundsCheck(byte[] array, int offset, int len) {
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    52
        // NPE is thrown when array is null
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    53
        Preconditions.checkFromIndexSize(offset, len, array.length, AIOOBE_SUPPLIER);
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    54
    }
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
diff changeset
    55
}