jdk/src/share/classes/com/sun/crypto/provider/AESCrypt.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 10336 0bb1999251f8
child 13559 17445744f2af
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
     2
 * Copyright (c) 2002, 2011, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Rijndael --pronounced Reindaal-- is a symmetric cipher with a 128-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * block size and variable key-size (128-, 192- and 256-bit).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Rijndael was designed by <a href="mailto:rijmen@esat.kuleuven.ac.be">Vincent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Rijmen</a> and <a href="mailto:Joan.Daemen@village.uunet.be">Joan Daemen</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class AESCrypt extends SymmetricCipher implements AESConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private boolean ROUNDS_12 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean ROUNDS_14 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** Session and Sub keys */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Object[] sessionK = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int[] K = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** (ROUNDS-1) * 4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int limit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    AESCrypt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Returns this cipher's block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return this cipher's block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    int getBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return AES_BLOCK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    void init(boolean decrypting, String algorithm, byte[] key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (!algorithm.equalsIgnoreCase("AES")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    && !algorithm.equalsIgnoreCase("Rijndael")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                ("Wrong algorithm: AES or Rijndael required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (!isKeySizeValid(key.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new InvalidKeyException("Invalid AES key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                key.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // generate session key and reset sub key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        sessionK = makeKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        setSubKey(decrypting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private void setSubKey(boolean decrypting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int[][] Kd = (int[][]) sessionK[decrypting ? 1 : 0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int rounds = Kd.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.K = new int[rounds*4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        for(int i=0; i<rounds; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            for(int j=0; j<4; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                K[i*4 + j] = Kd[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (decrypting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            int j0 = K[K.length-4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            int j1 = K[K.length-3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            int j2 = K[K.length-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            int j3 = K[K.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            for (int i=this.K.length-1; i>3; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                this.K[i] = this.K[i-4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            K[0] = j0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            K[1] = j1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            K[2] = j2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            K[3] = j3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        ROUNDS_12 = (rounds>=13);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        ROUNDS_14 = (rounds==15);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        rounds--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        limit=rounds*4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        alog = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        log  = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        S  = new byte[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Si = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        T1 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        T2 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        T3 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        T4 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        T5 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        T6 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        T7 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        T8 = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static final int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        U1 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        U2 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        U3 = new int[256],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        U4 = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static final byte[] rcon = new byte[30];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // Static code - to intialise S-boxes and T-boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int ROOT = 0x11B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int i, j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // produce log and alog tables, needed for multiplying in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // field GF(2^m) (generator = 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        alog[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (i = 1; i < 256; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            j = (alog[i-1] << 1) ^ alog[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if ((j & 0x100) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                j ^= ROOT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            alog[i] = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (i = 1; i < 255; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            log[alog[i]] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        byte[][] A = new byte[][]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            {1, 1, 1, 1, 1, 0, 0, 0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            {0, 1, 1, 1, 1, 1, 0, 0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            {0, 0, 1, 1, 1, 1, 1, 0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            {0, 0, 0, 1, 1, 1, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            {1, 0, 0, 0, 1, 1, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            {1, 1, 0, 0, 0, 1, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            {1, 1, 1, 0, 0, 0, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            {1, 1, 1, 1, 0, 0, 0, 1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        byte[] B = new byte[] { 0, 1, 1, 0, 0, 0, 1, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // substitution box based on F^{-1}(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        byte[][] box = new byte[256][8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        box[1][7] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        for (i = 2; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            j = alog[255 - log[i]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            for (t = 0; t < 8; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                box[i][t] = (byte)((j >>> (7 - t)) & 0x01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // affine transform:  box[i] <- B + A*box[i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        byte[][] cox = new byte[256][8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            for (t = 0; t < 8; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                cox[i][t] = B[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                for (j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    cox[i][t] ^= A[t][j] * box[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // S-boxes and inverse S-boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            S[i] = (byte)(cox[i][0] << 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            for (t = 1; t < 8; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    S[i] ^= cox[i][t] << (7-t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            Si[S[i] & 0xFF] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // T-boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        byte[][] G = new byte[][] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            {2, 1, 1, 3},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            {3, 2, 1, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            {1, 3, 2, 1},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            {1, 1, 3, 2}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        byte[][] AA = new byte[4][8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            for (j = 0; j < 4; j++) AA[i][j] = G[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            AA[i][i+4] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        byte pivot, tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        byte[][] iG = new byte[4][4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            pivot = AA[i][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (pivot == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                t = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                while ((AA[t][i] == 0) && (t < 4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    t++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (t == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    throw new RuntimeException("G matrix is not invertible");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    for (j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        tmp = AA[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        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
   248
                        AA[t][j] = tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    pivot = AA[i][i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            for (j = 0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (AA[i][j] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    AA[i][j] = (byte)
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   256
                        alog[(255 + log[AA[i][j] & 0xFF] - log[pivot & 0xFF])
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   257
                        % 255];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            for (t = 0; t < 4; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (i != t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    for (j = i+1; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        AA[t][j] ^= mul(AA[i][j], AA[t][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    AA[t][i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            for (j = 0; j < 4; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                iG[i][j] = AA[i][j + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        for (t = 0; t < 256; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            s = S[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            T1[t] = mul4(s, G[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            T2[t] = mul4(s, G[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            T3[t] = mul4(s, G[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            T4[t] = mul4(s, G[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            s = Si[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            T5[t] = mul4(s, iG[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            T6[t] = mul4(s, iG[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            T7[t] = mul4(s, iG[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            T8[t] = mul4(s, iG[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            U1[t] = mul4(t, iG[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            U2[t] = mul4(t, iG[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            U3[t] = mul4(t, iG[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            U4[t] = mul4(t, iG[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // round constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        rcon[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int r = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        for (t = 1; t < 30; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            r = mul(2, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            rcon[t] = (byte) r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        log = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        alog = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    // multiply two elements of GF(2^m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private static final int mul (int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return (a != 0 && b != 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            alog[(log[a & 0xFF] + log[b & 0xFF]) % 255] :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    // convenience method used in generating Transposition boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static final int mul4 (int a, byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (a == 0) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        a = log[a & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        int a0 = (b[0] != 0) ? alog[(a + log[b[0] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int a1 = (b[1] != 0) ? alog[(a + log[b[1] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int a2 = (b[2] != 0) ? alog[(a + log[b[2] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int a3 = (b[3] != 0) ? alog[(a + log[b[3] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return a0 << 24 | a1 << 16 | a2 << 8 | a3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    // check if the specified length (in bytes) is a valid keysize for AES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static final boolean isKeySizeValid(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        for (int i = 0; i < AES_KEYSIZES.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (len == AES_KEYSIZES[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Encrypt exactly one block of plaintext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    void encryptBlock(byte[] in, int inOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                              byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int keyOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int t0   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int t1   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int t2   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int t3   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // apply round transforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        while( keyOffset < limit )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            int a0, a1, a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            a0 = T1[(t0 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                 T2[(t1 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                 T3[(t2 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                 T4[(t3       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            a1 = T1[(t1 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                 T2[(t2 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                 T3[(t3 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                 T4[(t0       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            a2 = T1[(t2 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                 T2[(t3 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                 T3[(t0 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                 T4[(t1       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            t3 = T1[(t3 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                 T2[(t0 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                 T3[(t1 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                 T4[(t2       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            t0 = a0; t1 = a1; t2 = a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // last round is special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        int tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        out[outOffset++] = (byte)(S[(t0 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        out[outOffset++] = (byte)(S[(t1 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        out[outOffset++] = (byte)(S[(t2 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        out[outOffset++] = (byte)(S[(t3       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        out[outOffset++] = (byte)(S[(t1 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        out[outOffset++] = (byte)(S[(t2 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        out[outOffset++] = (byte)(S[(t3 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        out[outOffset++] = (byte)(S[(t0       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        out[outOffset++] = (byte)(S[(t2 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        out[outOffset++] = (byte)(S[(t3 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        out[outOffset++] = (byte)(S[(t0 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        out[outOffset++] = (byte)(S[(t1       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        out[outOffset++] = (byte)(S[(t3 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        out[outOffset++] = (byte)(S[(t0 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        out[outOffset++] = (byte)(S[(t1 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        out[outOffset  ] = (byte)(S[(t2       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Decrypt exactly one block of plaintext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    void decryptBlock(byte[] in, int inOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                              byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int keyOffset = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int t0 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int t1 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int t2 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int t3 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                  (in[inOffset  ] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        int a0, a1, a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if(ROUNDS_12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                 T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                 T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                 T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                 T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                 T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                 T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                 T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                 T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if(ROUNDS_14)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                     T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                     T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                     T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                     T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                     T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                     T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                     T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                     T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        t1 = K[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        out[outOffset++] = (byte)(Si[(a0 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        out[outOffset++] = (byte)(Si[(t3 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        out[outOffset++] = (byte)(Si[(a2 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        out[outOffset++] = (byte)(Si[(a1       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        t1 = K[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        out[outOffset++] = (byte)(Si[(a1 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        out[outOffset++] = (byte)(Si[(a0 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        out[outOffset++] = (byte)(Si[(t3 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        out[outOffset++] = (byte)(Si[(a2       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        t1 = K[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        out[outOffset++] = (byte)(Si[(a2 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        out[outOffset++] = (byte)(Si[(a1 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        out[outOffset++] = (byte)(Si[(a0 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        out[outOffset++] = (byte)(Si[(t3       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        t1 = K[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        out[outOffset++] = (byte)(Si[(t3 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        out[outOffset++] = (byte)(Si[(a2 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        out[outOffset++] = (byte)(Si[(a1 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        out[outOffset  ] = (byte)(Si[(a0       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Expand a user-supplied key material into a session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @param key The 128/192/256-bit user-key to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @exception InvalidKeyException  If the key is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    private static Object[] makeKey(byte[] k) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            throw new InvalidKeyException("Empty key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (!isKeySizeValid(k.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
             throw new InvalidKeyException("Invalid AES key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                           k.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        int ROUNDS          = getRounds(k.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        int ROUND_KEY_COUNT = (ROUNDS + 1) * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        int BC = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        int[][] Ke = new int[ROUNDS + 1][4]; // encryption round keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        int[][] Kd = new int[ROUNDS + 1][4]; // decryption round keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        int KC = k.length/4; // keylen in 32-bit elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        int[] tk = new int[KC];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        // copy user material bytes into temporary ints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        for (i = 0, j = 0; i < KC; i++, j+=4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            tk[i] = (k[j]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    (k[j+1] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    (k[j+2] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    (k[j+3] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        // copy values into round key arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        int t = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            Ke[t / 4][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            Kd[ROUNDS - (t / 4)][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        int tt, rconpointer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        while (t < ROUND_KEY_COUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            // extrapolate using phi (the round key evolution function)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            tt = tk[KC - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            tk[0] ^= (S[(tt >>> 16) & 0xFF]       ) << 24 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                     (S[(tt >>>  8) & 0xFF] & 0xFF) << 16 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                     (S[(tt       ) & 0xFF] & 0xFF) <<  8 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                     (S[(tt >>> 24)       ] & 0xFF)       ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                     (rcon[rconpointer++]         ) << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            if (KC != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                for (i = 1, j = 0; i < KC; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                for (i = 1, j = 0; i < KC / 2; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                tt = tk[KC / 2 - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                tk[KC / 2] ^= (S[(tt       ) & 0xFF] & 0xFF)       ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                              (S[(tt >>>  8) & 0xFF] & 0xFF) <<  8 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                              (S[(tt >>> 16) & 0xFF] & 0xFF) << 16 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                              (S[(tt >>> 24)       ]       ) << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                for (j = KC / 2, i = j + 1; i < KC; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            // copy values into round key arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                Ke[t / 4][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                Kd[ROUNDS - (t / 4)][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        for (int r = 1; r < ROUNDS; r++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            // inverse MixColumn where needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            for (j = 0; j < BC; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                tt = Kd[r][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                Kd[r][j] = U1[(tt >>> 24) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                           U2[(tt >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                           U3[(tt >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                           U4[ tt         & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        // assemble the encryption (Ke) and decryption (Kd) round keys into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // one sessionKey object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        Object[] result = new Object[] {Ke, Kd};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * Return The number of rounds for a given Rijndael keysize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param keySize  The size of the user key material in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *                 MUST be one of (16, 24, 32).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @return         The number of rounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    private static int getRounds(int keySize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return (keySize >> 2) + 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
}