org.bouncycastle.crypto.engines
Class RijndaelEngine

java.lang.Object
  extended by org.bouncycastle.crypto.engines.RijndaelEngine
All Implemented Interfaces:
BlockCipher

public class RijndaelEngine
extends java.lang.Object
implements BlockCipher

an implementation of Rijndael, based on the documentation and reference implementation by Paulo Barreto, Vincent Rijmen, for v2.0 August '99.

Note: this implementation is based on information prior to final NIST publication.


Field Summary
private  long A0
           
private  long A1
           
private  long A2
           
private  long A3
           
private static byte[] aLogtable
           
private  int BC
           
private  long BC_MASK
           
private  int blockBits
           
private  boolean forEncryption
           
private static byte[] logtable
           
private static int MAXKC
           
private static int MAXROUNDS
           
private static int[] rcon
           
private  int ROUNDS
           
private static byte[] S
           
(package private) static byte[][] shifts0
           
private  byte[] shifts0SC
           
(package private) static byte[][] shifts1
           
private  byte[] shifts1SC
           
private static byte[] Si
           
private  long[][] workingKey
           
 
Constructor Summary
RijndaelEngine()
          default constructor - 128 bit block size.
RijndaelEngine(int blockBits)
          basic constructor - set the cipher up for a given blocksize
 
Method Summary
private  long applyS(long r, byte[] box)
           
private  void decryptBlock(long[][] rk)
           
private  void encryptBlock(long[][] rk)
           
private  long[][] generateWorkingKey(byte[] key)
          Calculate the necessary round keys The number of calculations depends on keyBits and blockBits
 java.lang.String getAlgorithmName()
          Return the name of the algorithm the cipher implements.
 int getBlockSize()
          Return the block size for this cipher (in bytes).
 void init(boolean forEncryption, CipherParameters params)
          initialise a Rijndael cipher.
private  void InvMixColumn()
          Mix the bytes of every column in a linear way This is the opposite operation of Mixcolumn
private  void KeyAddition(long[] rk)
          xor corresponding text input and round key input bytes
private  void MixColumn()
          Mix the bytes of every column in a linear way
private  byte mul0x2(int b)
          multiply two elements of GF(2^m) needed for MixColumn and InvMixColumn
private  byte mul0x3(int b)
           
private  byte mul0x9(int b)
           
private  byte mul0xb(int b)
           
private  byte mul0xd(int b)
           
private  byte mul0xe(int b)
           
private  void packBlock(byte[] bytes, int off)
           
 int processBlock(byte[] in, int inOff, byte[] out, int outOff)
          Process one block of input from the array in and write it to the out array.
 void reset()
          Reset the cipher.
private  long shift(long r, int shift)
           
private  void ShiftRow(byte[] shiftsSC)
          Row 0 remains unchanged The other three rows are shifted a variable amount
private  void Substitution(byte[] box)
          Replace every byte of the input by the byte at that place in the nonlinear S-box
private  void unpackBlock(byte[] bytes, int off)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXROUNDS

private static final int MAXROUNDS
See Also:
Constant Field Values

MAXKC

private static final int MAXKC
See Also:
Constant Field Values

logtable

private static final byte[] logtable

aLogtable

private static final byte[] aLogtable

S

private static final byte[] S

Si

private static final byte[] Si

rcon

private static final int[] rcon

shifts0

static byte[][] shifts0

shifts1

static byte[][] shifts1

BC

private int BC

BC_MASK

private long BC_MASK

ROUNDS

private int ROUNDS

blockBits

private int blockBits

workingKey

private long[][] workingKey

A0

private long A0

A1

private long A1

A2

private long A2

A3

private long A3

forEncryption

private boolean forEncryption

shifts0SC

private byte[] shifts0SC

shifts1SC

private byte[] shifts1SC
Constructor Detail

RijndaelEngine

public RijndaelEngine()
default constructor - 128 bit block size.


RijndaelEngine

public RijndaelEngine(int blockBits)
basic constructor - set the cipher up for a given blocksize

Parameters:
blockBits - the blocksize in bits, must be 128, 192, or 256.
Method Detail

mul0x2

private byte mul0x2(int b)
multiply two elements of GF(2^m) needed for MixColumn and InvMixColumn


mul0x3

private byte mul0x3(int b)

mul0x9

private byte mul0x9(int b)

mul0xb

private byte mul0xb(int b)

mul0xd

private byte mul0xd(int b)

mul0xe

private byte mul0xe(int b)

KeyAddition

private void KeyAddition(long[] rk)
xor corresponding text input and round key input bytes


shift

private long shift(long r,
                   int shift)

ShiftRow

private void ShiftRow(byte[] shiftsSC)
Row 0 remains unchanged The other three rows are shifted a variable amount


applyS

private long applyS(long r,
                    byte[] box)

Substitution

private void Substitution(byte[] box)
Replace every byte of the input by the byte at that place in the nonlinear S-box


MixColumn

private void MixColumn()
Mix the bytes of every column in a linear way


InvMixColumn

private void InvMixColumn()
Mix the bytes of every column in a linear way This is the opposite operation of Mixcolumn


generateWorkingKey

private long[][] generateWorkingKey(byte[] key)
Calculate the necessary round keys The number of calculations depends on keyBits and blockBits


init

public void init(boolean forEncryption,
                 CipherParameters params)
initialise a Rijndael cipher.

Specified by:
init in interface BlockCipher
Parameters:
forEncryption - whether or not we are for encryption.
params - the parameters required to set up the cipher.
Throws:
java.lang.IllegalArgumentException - if the params argument is inappropriate.

getAlgorithmName

public java.lang.String getAlgorithmName()
Description copied from interface: BlockCipher
Return the name of the algorithm the cipher implements.

Specified by:
getAlgorithmName in interface BlockCipher
Returns:
the name of the algorithm the cipher implements.

getBlockSize

public int getBlockSize()
Description copied from interface: BlockCipher
Return the block size for this cipher (in bytes).

Specified by:
getBlockSize in interface BlockCipher
Returns:
the block size for this cipher in bytes.

processBlock

public int processBlock(byte[] in,
                        int inOff,
                        byte[] out,
                        int outOff)
Description copied from interface: BlockCipher
Process one block of input from the array in and write it to the out array.

Specified by:
processBlock in interface BlockCipher
Parameters:
in - the array containing the input data.
inOff - offset into the in array the data starts at.
out - the array the output data will be copied into.
outOff - the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.

reset

public void reset()
Description copied from interface: BlockCipher
Reset the cipher. After resetting the cipher is in the same state as it was after the last init (if there was one).

Specified by:
reset in interface BlockCipher

unpackBlock

private final void unpackBlock(byte[] bytes,
                               int off)

packBlock

private final void packBlock(byte[] bytes,
                             int off)

encryptBlock

private final void encryptBlock(long[][] rk)

decryptBlock

private final void decryptBlock(long[][] rk)