src/java.base/share/classes/com/sun/crypto/provider/AESCrypt.java
author valeriep
Fri, 13 Jul 2018 02:36:42 +0000
changeset 51052 080776992b29
parent 47216 71c04702a3d5
permissions -rw-r--r--
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73) Summary: Do bounds check per encryption/decryption call instead of per block Reviewed-by: ascarpino, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/* $Id: Rijndael.java,v 1.6 2000/02/10 01:31:41 gelderen Exp $
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Copyright (C) 1995-2000 The Cryptix Foundation Limited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Use, modification, copying and distribution of this softwareas is subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * the terms and conditions of the Cryptix General Licence. You should have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * received a copy of the Cryptix General Licence along with this library;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * if not, you can download a copy from http://www.cryptix.org/ .
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.InvalidKeyException;
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 31671
diff changeset
    40
import java.security.MessageDigest;
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    41
import java.util.Arrays;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    42
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    43
import jdk.internal.HotSpotIntrinsicCandidate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Rijndael --pronounced Reindaal-- is a symmetric cipher with a 128-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * block size and variable key-size (128-, 192- and 256-bit).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Rijndael was designed by <a href="mailto:rijmen@esat.kuleuven.ac.be">Vincent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Rijmen</a> and <a href="mailto:Joan.Daemen@village.uunet.be">Joan Daemen</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
final class AESCrypt extends SymmetricCipher implements AESConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean ROUNDS_12 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean ROUNDS_14 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** Session and Sub keys */
37333
774ed0e5e01b 8152172: PPC64: Support AES intrinsics
simonis
parents: 35626
diff changeset
    58
    private int[][] sessionK = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int[] K = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    61
    /** Cipher encryption/decryption key */
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    62
    // skip re-generating Session and Sub keys if the cipher key is
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    63
    // the same
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    64
    private byte[] lastKey = null;
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    65
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    66
    /** ROUNDS * 4 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int limit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    AESCrypt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Returns this cipher's block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @return this cipher's block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    int getBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return AES_BLOCK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    void init(boolean decrypting, String algorithm, byte[] key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (!algorithm.equalsIgnoreCase("AES")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    && !algorithm.equalsIgnoreCase("Rijndael")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                ("Wrong algorithm: AES or Rijndael required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (!isKeySizeValid(key.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new InvalidKeyException("Invalid AES key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                key.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 31671
diff changeset
    94
        if (!MessageDigest.isEqual(key, lastKey)) {
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    95
            // re-generate session key 'sessionK' when cipher key changes
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    96
            makeSessionKey(key);
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
    97
            lastKey = key.clone();  // save cipher key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   100
        // set sub key to the corresponding session Key
37333
774ed0e5e01b 8152172: PPC64: Support AES intrinsics
simonis
parents: 35626
diff changeset
   101
        this.K = sessionK[(decrypting? 1:0)];
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   102
    }
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   103
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   104
    /**
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   105
     * Expand an int[(ROUNDS+1)][4] into int[(ROUNDS+1)*4].
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   106
     * For decryption round keys, need to rotate right by 4 ints.
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   107
     * @param kr The round keys for encryption or decryption.
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   108
     * @param decrypting True if 'kr' is for decryption and false otherwise.
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   109
     */
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   110
    private static final int[] expandToSubKey(int[][] kr, boolean decrypting) {
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   111
        int total = kr.length;
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   112
        int[] expK = new int[total*4];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (decrypting) {
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   114
            // decrypting, rotate right by 4 ints
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   115
            // i.e. i==0
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   116
            for(int j=0; j<4; j++) {
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   117
                expK[j] = kr[total-1][j];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   119
            for(int i=1; i<total; i++) {
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   120
                for(int j=0; j<4; j++) {
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   121
                    expK[i*4 + j] = kr[i-1][j];
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   122
                }
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   123
            }
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   124
        } else {
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   125
            // encrypting, straight expansion
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   126
            for(int i=0; i<total; i++) {
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   127
                for(int j=0; j<4; j++) {
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   128
                    expK[i*4 + j] = kr[i][j];
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   129
                }
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   130
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   132
        return expK;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        alog = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        log  = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static final byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        S  = new byte[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Si = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static final int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        T1 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        T2 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        T3 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        T4 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        T5 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        T6 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        T7 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        T8 = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static final int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        U1 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        U2 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        U3 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        U4 = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final byte[] rcon = new byte[30];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // Static code - to intialise S-boxes and T-boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int ROOT = 0x11B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int i, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // produce log and alog tables, needed for multiplying in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // field GF(2^m) (generator = 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        alog[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        for (i = 1; i < 256; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            j = (alog[i-1] << 1) ^ alog[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if ((j & 0x100) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                j ^= ROOT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            alog[i] = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        for (i = 1; i < 255; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            log[alog[i]] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        byte[][] A = new byte[][]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            {1, 1, 1, 1, 1, 0, 0, 0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            {0, 1, 1, 1, 1, 1, 0, 0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            {0, 0, 1, 1, 1, 1, 1, 0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            {0, 0, 0, 1, 1, 1, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            {1, 0, 0, 0, 1, 1, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            {1, 1, 0, 0, 0, 1, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            {1, 1, 1, 0, 0, 0, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            {1, 1, 1, 1, 0, 0, 0, 1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        byte[] B = new byte[] { 0, 1, 1, 0, 0, 0, 1, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // substitution box based on F^{-1}(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        byte[][] box = new byte[256][8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        box[1][7] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        for (i = 2; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            j = alog[255 - log[i]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            for (t = 0; t < 8; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                box[i][t] = (byte)((j >>> (7 - t)) & 0x01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // affine transform:  box[i] <- B + A*box[i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        byte[][] cox = new byte[256][8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            for (t = 0; t < 8; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                cox[i][t] = B[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                for (j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    cox[i][t] ^= A[t][j] * box[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // S-boxes and inverse S-boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            S[i] = (byte)(cox[i][0] << 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            for (t = 1; t < 8; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    S[i] ^= cox[i][t] << (7-t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            Si[S[i] & 0xFF] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // T-boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        byte[][] G = new byte[][] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            {2, 1, 1, 3},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            {3, 2, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            {1, 3, 2, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            {1, 1, 3, 2}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        byte[][] AA = new byte[4][8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            for (j = 0; j < 4; j++) AA[i][j] = G[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            AA[i][i+4] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        byte pivot, tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        byte[][] iG = new byte[4][4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            pivot = AA[i][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (pivot == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                t = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                while ((AA[t][i] == 0) && (t < 4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    t++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (t == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    throw new RuntimeException("G matrix is not invertible");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    for (j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        tmp = AA[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        AA[i][j] = AA[t][j];
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   261
                        AA[t][j] = tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    pivot = AA[i][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            for (j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                if (AA[i][j] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    AA[i][j] = (byte)
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   269
                        alog[(255 + log[AA[i][j] & 0xFF] - log[pivot & 0xFF])
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   270
                        % 255];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            for (t = 0; t < 4; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if (i != t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    for (j = i+1; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        AA[t][j] ^= mul(AA[i][j], AA[t][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    AA[t][i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            for (j = 0; j < 4; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                iG[i][j] = AA[i][j + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        for (t = 0; t < 256; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            s = S[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            T1[t] = mul4(s, G[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            T2[t] = mul4(s, G[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            T3[t] = mul4(s, G[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            T4[t] = mul4(s, G[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            s = Si[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            T5[t] = mul4(s, iG[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            T6[t] = mul4(s, iG[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            T7[t] = mul4(s, iG[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            T8[t] = mul4(s, iG[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            U1[t] = mul4(t, iG[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            U2[t] = mul4(t, iG[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            U3[t] = mul4(t, iG[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            U4[t] = mul4(t, iG[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // round constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        rcon[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        int r = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        for (t = 1; t < 30; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            r = mul(2, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            rcon[t] = (byte) r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        log = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        alog = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    // multiply two elements of GF(2^m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private static final int mul (int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return (a != 0 && b != 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            alog[(log[a & 0xFF] + log[b & 0xFF]) % 255] :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    // convenience method used in generating Transposition boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private static final int mul4 (int a, byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (a == 0) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        a = log[a & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int a0 = (b[0] != 0) ? alog[(a + log[b[0] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        int a1 = (b[1] != 0) ? alog[(a + log[b[1] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        int a2 = (b[2] != 0) ? alog[(a + log[b[2] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int a3 = (b[3] != 0) ? alog[(a + log[b[3] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return a0 << 24 | a1 << 16 | a2 << 8 | a3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // check if the specified length (in bytes) is a valid keysize for AES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    static final boolean isKeySizeValid(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        for (int i = 0; i < AES_KEYSIZES.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (len == AES_KEYSIZES[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Encrypt exactly one block of plaintext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    void encryptBlock(byte[] in, int inOffset,
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   352
                      byte[] out, int outOffset) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   353
        // Array bound checks are done in caller code, i.e.
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   354
        // FeedbackCipher.encrypt/decrypt(...) to improve performance.
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   355
        implEncryptBlock(in, inOffset, out, outOffset);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   356
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   357
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   358
    // Encryption operation. Possibly replaced with a compiler intrinsic.
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   359
    @HotSpotIntrinsicCandidate
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   360
    private void implEncryptBlock(byte[] in, int inOffset,
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   361
                                  byte[] out, int outOffset)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        int keyOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int t0   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int t1   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int t2   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int t3   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // apply round transforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        while( keyOffset < limit )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            int a0, a1, a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            a0 = T1[(t0 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                 T2[(t1 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                 T3[(t2 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                 T4[(t3       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            a1 = T1[(t1 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                 T2[(t2 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                 T3[(t3 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                 T4[(t0       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            a2 = T1[(t2 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                 T2[(t3 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                 T3[(t0 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                 T4[(t1       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            t3 = T1[(t3 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                 T2[(t0 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                 T3[(t1 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                 T4[(t2       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            t0 = a0; t1 = a1; t2 = a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // last round is special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        int tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        out[outOffset++] = (byte)(S[(t0 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        out[outOffset++] = (byte)(S[(t1 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        out[outOffset++] = (byte)(S[(t2 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        out[outOffset++] = (byte)(S[(t3       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        out[outOffset++] = (byte)(S[(t1 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        out[outOffset++] = (byte)(S[(t2 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        out[outOffset++] = (byte)(S[(t3 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        out[outOffset++] = (byte)(S[(t0       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        out[outOffset++] = (byte)(S[(t2 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        out[outOffset++] = (byte)(S[(t3 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        out[outOffset++] = (byte)(S[(t0 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        out[outOffset++] = (byte)(S[(t1       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        out[outOffset++] = (byte)(S[(t3 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        out[outOffset++] = (byte)(S[(t0 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        out[outOffset++] = (byte)(S[(t1 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        out[outOffset  ] = (byte)(S[(t2       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Decrypt exactly one block of plaintext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    void decryptBlock(byte[] in, int inOffset,
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   431
                      byte[] out, int outOffset) {
51052
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   432
        // Array bound checks are done in caller code, i.e.
080776992b29 8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents: 47216
diff changeset
   433
        // FeedbackCipher.encrypt/decrypt(...) to improve performance.
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   434
        implDecryptBlock(in, inOffset, out, outOffset);
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   435
    }
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   436
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   437
    // Decrypt operation. Possibly replaced with a compiler intrinsic.
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   438
    @HotSpotIntrinsicCandidate
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   439
    private void implDecryptBlock(byte[] in, int inOffset,
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   440
                                  byte[] out, int outOffset)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int keyOffset = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        int t0 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        int t1 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        int t2 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int t3 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                  (in[inOffset  ] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        int a0, a1, a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if(ROUNDS_12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                 T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                 T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                 T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                 T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                 T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                 T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                 T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                 T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if(ROUNDS_14)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                     T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                     T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                     T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                     T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                     T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                     T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                     T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                     T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        t1 = K[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        out[outOffset++] = (byte)(Si[(a0 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        out[outOffset++] = (byte)(Si[(t3 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        out[outOffset++] = (byte)(Si[(a2 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        out[outOffset++] = (byte)(Si[(a1       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        t1 = K[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        out[outOffset++] = (byte)(Si[(a1 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        out[outOffset++] = (byte)(Si[(a0 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        out[outOffset++] = (byte)(Si[(t3 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        out[outOffset++] = (byte)(Si[(a2       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        t1 = K[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        out[outOffset++] = (byte)(Si[(a2 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        out[outOffset++] = (byte)(Si[(a1 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        out[outOffset++] = (byte)(Si[(a0 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        out[outOffset++] = (byte)(Si[(t3       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        t1 = K[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        out[outOffset++] = (byte)(Si[(t3 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        out[outOffset++] = (byte)(Si[(a2 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        out[outOffset++] = (byte)(Si[(a1 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        out[outOffset  ] = (byte)(Si[(a0       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Expand a user-supplied key material into a session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   598
     * @param k The 128/192/256-bit cipher key to use.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @exception InvalidKeyException  If the key is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   601
    private void makeSessionKey(byte[] k) throws InvalidKeyException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            throw new InvalidKeyException("Empty key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (!isKeySizeValid(k.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
             throw new InvalidKeyException("Invalid AES key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                           k.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        int ROUNDS          = getRounds(k.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int ROUND_KEY_COUNT = (ROUNDS + 1) * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        int BC = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        int[][] Ke = new int[ROUNDS + 1][4]; // encryption round keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        int[][] Kd = new int[ROUNDS + 1][4]; // decryption round keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        int KC = k.length/4; // keylen in 32-bit elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        int[] tk = new int[KC];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        // copy user material bytes into temporary ints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        for (i = 0, j = 0; i < KC; i++, j+=4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            tk[i] = (k[j]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    (k[j+1] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    (k[j+2] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    (k[j+3] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // copy values into round key arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        int t = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            Ke[t / 4][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            Kd[ROUNDS - (t / 4)][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        int tt, rconpointer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        while (t < ROUND_KEY_COUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // extrapolate using phi (the round key evolution function)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            tt = tk[KC - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            tk[0] ^= (S[(tt >>> 16) & 0xFF]       ) << 24 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                     (S[(tt >>>  8) & 0xFF] & 0xFF) << 16 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                     (S[(tt       ) & 0xFF] & 0xFF) <<  8 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                     (S[(tt >>> 24)       ] & 0xFF)       ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                     (rcon[rconpointer++]         ) << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (KC != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                for (i = 1, j = 0; i < KC; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                for (i = 1, j = 0; i < KC / 2; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                tt = tk[KC / 2 - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                tk[KC / 2] ^= (S[(tt       ) & 0xFF] & 0xFF)       ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                              (S[(tt >>>  8) & 0xFF] & 0xFF) <<  8 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                              (S[(tt >>> 16) & 0xFF] & 0xFF) << 16 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                              (S[(tt >>> 24)       ]       ) << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                for (j = KC / 2, i = j + 1; i < KC; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            // copy values into round key arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                Ke[t / 4][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                Kd[ROUNDS - (t / 4)][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        for (int r = 1; r < ROUNDS; r++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            // inverse MixColumn where needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            for (j = 0; j < BC; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                tt = Kd[r][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                Kd[r][j] = U1[(tt >>> 24) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                           U2[(tt >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                           U3[(tt >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                           U4[ tt         & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
13559
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   671
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   672
        // assemble the encryption (Ke) and decryption (Kd) round keys
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   673
        // and expand them into arrays of ints.
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   674
        int[] expandedKe = expandToSubKey(Ke, false); // decrypting==false
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   675
        int[] expandedKd = expandToSubKey(Kd, true);  // decrypting==true
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   676
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   677
        ROUNDS_12 = (ROUNDS>=12);
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   678
        ROUNDS_14 = (ROUNDS==14);
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   679
        limit = ROUNDS*4;
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   680
17445744f2af 7185471: Avoid key expansion when AES cipher is re-init w/ the same key
valeriep
parents: 10336
diff changeset
   681
        // store the expanded sub keys into 'sessionK'
37333
774ed0e5e01b 8152172: PPC64: Support AES intrinsics
simonis
parents: 35626
diff changeset
   682
        sessionK = new int[][] { expandedKe, expandedKd };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Return The number of rounds for a given Rijndael keysize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @param keySize  The size of the user key material in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *                 MUST be one of (16, 24, 32).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @return         The number of rounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    private static int getRounds(int keySize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return (keySize >> 2) + 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
}