jdk/src/share/classes/com/sun/crypto/provider/AESCrypt.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7043 5e2d1edeb2c7
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2007, 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];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        AA[t][j] = (byte) tmp;
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)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        alog[(255 + log[AA[i][j] & 0xFF] - log[pivot & 0xFF]) % 255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            for (t = 0; t < 4; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (i != t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    for (j = i+1; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        AA[t][j] ^= mul(AA[i][j], AA[t][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    AA[t][i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            for (j = 0; j < 4; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                iG[i][j] = AA[i][j + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        for (t = 0; t < 256; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            s = S[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            T1[t] = mul4(s, G[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            T2[t] = mul4(s, G[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            T3[t] = mul4(s, G[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            T4[t] = mul4(s, G[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            s = Si[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            T5[t] = mul4(s, iG[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            T6[t] = mul4(s, iG[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            T7[t] = mul4(s, iG[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            T8[t] = mul4(s, iG[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            U1[t] = mul4(t, iG[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            U2[t] = mul4(t, iG[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            U3[t] = mul4(t, iG[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            U4[t] = mul4(t, iG[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // round constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        rcon[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int r = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        for (t = 1; t < 30; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            r = mul(2, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            rcon[t] = (byte) r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        log = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        alog = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    // multiply two elements of GF(2^m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private static final int mul (int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return (a != 0 && b != 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            alog[(log[a & 0xFF] + log[b & 0xFF]) % 255] :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    // convenience method used in generating Transposition boxes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static final int mul4 (int a, byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (a == 0) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        a = log[a & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int a0 = (b[0] != 0) ? alog[(a + log[b[0] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        int a1 = (b[1] != 0) ? alog[(a + log[b[1] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int a2 = (b[2] != 0) ? alog[(a + log[b[2] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int a3 = (b[3] != 0) ? alog[(a + log[b[3] & 0xFF]) % 255] & 0xFF : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return a0 << 24 | a1 << 16 | a2 << 8 | a3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    // check if the specified length (in bytes) is a valid keysize for AES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static final boolean isKeySizeValid(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        for (int i = 0; i < AES_KEYSIZES.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (len == AES_KEYSIZES[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Encrypt exactly one block of plaintext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    void encryptBlock(byte[] in, int inOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                              byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        int keyOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int t0   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int t1   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int t2   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int t3   = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // apply round transforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        while( keyOffset < limit )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            int a0, a1, a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            a0 = T1[(t0 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                 T2[(t1 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                 T3[(t2 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                 T4[(t3       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            a1 = T1[(t1 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                 T2[(t2 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                 T3[(t3 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                 T4[(t0       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            a2 = T1[(t2 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                 T2[(t3 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                 T3[(t0 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                 T4[(t1       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            t3 = T1[(t3 >>> 24)       ] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                 T2[(t0 >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                 T3[(t1 >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                 T4[(t2       ) & 0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            t0 = a0; t1 = a1; t2 = a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // last round is special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        out[outOffset++] = (byte)(S[(t0 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        out[outOffset++] = (byte)(S[(t1 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        out[outOffset++] = (byte)(S[(t2 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        out[outOffset++] = (byte)(S[(t3       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        out[outOffset++] = (byte)(S[(t1 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        out[outOffset++] = (byte)(S[(t2 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        out[outOffset++] = (byte)(S[(t3 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        out[outOffset++] = (byte)(S[(t0       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        out[outOffset++] = (byte)(S[(t2 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        out[outOffset++] = (byte)(S[(t3 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        out[outOffset++] = (byte)(S[(t0 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        out[outOffset++] = (byte)(S[(t1       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        tt = K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        out[outOffset++] = (byte)(S[(t3 >>> 24)       ] ^ (tt >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        out[outOffset++] = (byte)(S[(t0 >>> 16) & 0xFF] ^ (tt >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        out[outOffset++] = (byte)(S[(t1 >>>  8) & 0xFF] ^ (tt >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        out[outOffset  ] = (byte)(S[(t2       ) & 0xFF] ^ (tt       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
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
     * Decrypt exactly one block of plaintext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    void decryptBlock(byte[] in, int inOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                              byte[] out, int outOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        int keyOffset = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int t0 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int t1 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int t2 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                  (in[inOffset++] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        int t3 = ((in[inOffset++]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                  (in[inOffset++] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                  (in[inOffset++] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                  (in[inOffset  ] & 0xFF)        ) ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        int a0, a1, a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if(ROUNDS_12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                 T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                 T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                 T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                 T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                 T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                 T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                 T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                 T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if(ROUNDS_14)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                     T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                     T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                     T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                     T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                     T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                     T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                     T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                     T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        t0 = T5[(a0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
             T7[(a2>>> 8)&0xFF] ^ T8[(a1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        t1 = T5[(a1>>>24)     ] ^ T6[(a0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
             T7[(t3>>> 8)&0xFF] ^ T8[(a2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        t2 = T5[(a2>>>24)     ] ^ T6[(a1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
             T7[(a0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        t3 = T5[(t3>>>24)     ] ^ T6[(a2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
             T7[(a1>>> 8)&0xFF] ^ T8[(a0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        a0 = T5[(t0>>>24)     ] ^ T6[(t3>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
             T7[(t2>>> 8)&0xFF] ^ T8[(t1     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        a1 = T5[(t1>>>24)     ] ^ T6[(t0>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
             T7[(t3>>> 8)&0xFF] ^ T8[(t2     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        a2 = T5[(t2>>>24)     ] ^ T6[(t1>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
             T7[(t0>>> 8)&0xFF] ^ T8[(t3     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        t3 = T5[(t3>>>24)     ] ^ T6[(t2>>>16)&0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
             T7[(t1>>> 8)&0xFF] ^ T8[(t0     )&0xFF] ^ K[keyOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        t1 = K[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        out[outOffset++] = (byte)(Si[(a0 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        out[outOffset++] = (byte)(Si[(t3 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        out[outOffset++] = (byte)(Si[(a2 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        out[outOffset++] = (byte)(Si[(a1       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        t1 = K[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        out[outOffset++] = (byte)(Si[(a1 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        out[outOffset++] = (byte)(Si[(a0 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        out[outOffset++] = (byte)(Si[(t3 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        out[outOffset++] = (byte)(Si[(a2       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        t1 = K[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        out[outOffset++] = (byte)(Si[(a2 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        out[outOffset++] = (byte)(Si[(a1 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        out[outOffset++] = (byte)(Si[(a0 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        out[outOffset++] = (byte)(Si[(t3       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        t1 = K[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        out[outOffset++] = (byte)(Si[(t3 >>> 24)       ] ^ (t1 >>> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        out[outOffset++] = (byte)(Si[(a2 >>> 16) & 0xFF] ^ (t1 >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        out[outOffset++] = (byte)(Si[(a1 >>>  8) & 0xFF] ^ (t1 >>>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        out[outOffset  ] = (byte)(Si[(a0       ) & 0xFF] ^ (t1       ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
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
     * Expand a user-supplied key material into a session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @param key The 128/192/256-bit user-key to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @exception InvalidKeyException  If the key is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    private static Object[] makeKey(byte[] k) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            throw new InvalidKeyException("Empty key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (!isKeySizeValid(k.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
             throw new InvalidKeyException("Invalid AES key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                                           k.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        int ROUNDS          = getRounds(k.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        int ROUND_KEY_COUNT = (ROUNDS + 1) * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        int BC = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        int[][] Ke = new int[ROUNDS + 1][4]; // encryption round keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        int[][] Kd = new int[ROUNDS + 1][4]; // decryption round keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        int KC = k.length/4; // keylen in 32-bit elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        int[] tk = new int[KC];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        // copy user material bytes into temporary ints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        for (i = 0, j = 0; i < KC; i++, j+=4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            tk[i] = (k[j]       ) << 24 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    (k[j+1] & 0xFF) << 16 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    (k[j+2] & 0xFF) <<  8 |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    (k[j+3] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        // copy values into round key arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        int t = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            Ke[t / 4][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            Kd[ROUNDS - (t / 4)][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        int tt, rconpointer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        while (t < ROUND_KEY_COUNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            // extrapolate using phi (the round key evolution function)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            tt = tk[KC - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            tk[0] ^= (S[(tt >>> 16) & 0xFF]       ) << 24 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                     (S[(tt >>>  8) & 0xFF] & 0xFF) << 16 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                     (S[(tt       ) & 0xFF] & 0xFF) <<  8 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                     (S[(tt >>> 24)       ] & 0xFF)       ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                     (rcon[rconpointer++]         ) << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            if (KC != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                for (i = 1, j = 0; i < KC; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                for (i = 1, j = 0; i < KC / 2; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                tt = tk[KC / 2 - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                tk[KC / 2] ^= (S[(tt       ) & 0xFF] & 0xFF)       ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                              (S[(tt >>>  8) & 0xFF] & 0xFF) <<  8 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                              (S[(tt >>> 16) & 0xFF] & 0xFF) << 16 ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                              (S[(tt >>> 24)       ]       ) << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                for (j = KC / 2, i = j + 1; i < KC; i++, j++) tk[i] ^= tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            // copy values into round key arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                Ke[t / 4][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                Kd[ROUNDS - (t / 4)][t % 4] = tk[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        for (int r = 1; r < ROUNDS; r++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            // inverse MixColumn where needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            for (j = 0; j < BC; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                tt = Kd[r][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                Kd[r][j] = U1[(tt >>> 24) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                           U2[(tt >>> 16) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                           U3[(tt >>>  8) & 0xFF] ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                           U4[ tt         & 0xFF];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        // assemble the encryption (Ke) and decryption (Kd) round keys into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        // one sessionKey object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        Object[] result = new Object[] {Ke, Kd};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
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
     * Return The number of rounds for a given Rijndael keysize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @param keySize  The size of the user key material in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *                 MUST be one of (16, 24, 32).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @return         The number of rounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    private static int getRounds(int keySize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        return (keySize >> 2) + 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
}