org.bouncycastle.crypto.engines
Class TwofishEngine

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

public final class TwofishEngine
extends java.lang.Object
implements BlockCipher

A class that provides Twofish encryption operations. This Java implementation is based on the Java reference implementation provided by Bruce Schneier and developed by Raif S. Naffah.


Field Summary
private static int BLOCK_SIZE
           
private  boolean encrypting
           
private static int GF256_FDBK
           
private static int GF256_FDBK_2
           
private static int GF256_FDBK_4
           
private  int[] gMDS0
           
private  int[] gMDS1
           
private  int[] gMDS2
           
private  int[] gMDS3
           
private  int[] gSBox
           
private  int[] gSubKeys
          gSubKeys[] and gSBox[] are eventually used in the encryption and decryption methods.
private static int INPUT_WHITEN
           
private  int k64Cnt
           
private static int MAX_KEY_BITS
           
private static int MAX_ROUNDS
           
private static int OUTPUT_WHITEN
           
private static byte[][] P
           
private static int P_00
          Define the fixed p0/p1 permutations used in keyed S-box lookup.
private static int P_01
           
private static int P_02
           
private static int P_03
           
private static int P_04
           
private static int P_10
           
private static int P_11
           
private static int P_12
           
private static int P_13
           
private static int P_14
           
private static int P_20
           
private static int P_21
           
private static int P_22
           
private static int P_23
           
private static int P_24
           
private static int P_30
           
private static int P_31
           
private static int P_32
           
private static int P_33
           
private static int P_34
           
private static int ROUND_SUBKEYS
           
private static int ROUNDS
           
private static int RS_GF_FDBK
           
private static int SK_BUMP
           
private static int SK_ROTL
           
private static int SK_STEP
           
private static int TOTAL_SUBKEYS
           
private  byte[] workingKey
           
 
Constructor Summary
TwofishEngine()
           
 
Method Summary
private  int b0(int x)
           
private  int b1(int x)
           
private  int b2(int x)
           
private  int b3(int x)
           
private  void Bits32ToBytes(int in, byte[] b, int offset)
           
private  int BytesTo32Bits(byte[] b, int p)
           
private  void decryptBlock(byte[] src, int srcIndex, byte[] dst, int dstIndex)
          Decrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset.
private  void encryptBlock(byte[] src, int srcIndex, byte[] dst, int dstIndex)
          Encrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset.
private  int F32(int x, int[] k32)
           
private  int Fe32_0(int x)
           
private  int Fe32_3(int x)
           
 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 encrypting, CipherParameters params)
          initialise a Twofish cipher.
private  int LFSR1(int x)
           
private  int LFSR2(int x)
           
private  int Mx_X(int x)
           
private  int Mx_Y(int x)
           
 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  int RS_MDS_Encode(int k0, int k1)
          Use (12, 8) Reed-Solomon code over GF(256) to produce a key S-box 32-bit entity from 2 key material 32-bit entities.
private  int RS_rem(int x)
          Reed-Solomon code parameters: (12,8) reversible code:
private  void setKey(byte[] key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

P

private static final byte[][] P

P_00

private static final int P_00
Define the fixed p0/p1 permutations used in keyed S-box lookup. By changing the following constant definitions, the S-boxes will automatically get changed in the Twofish engine.

See Also:
Constant Field Values

P_01

private static final int P_01
See Also:
Constant Field Values

P_02

private static final int P_02
See Also:
Constant Field Values

P_03

private static final int P_03
See Also:
Constant Field Values

P_04

private static final int P_04
See Also:
Constant Field Values

P_10

private static final int P_10
See Also:
Constant Field Values

P_11

private static final int P_11
See Also:
Constant Field Values

P_12

private static final int P_12
See Also:
Constant Field Values

P_13

private static final int P_13
See Also:
Constant Field Values

P_14

private static final int P_14
See Also:
Constant Field Values

P_20

private static final int P_20
See Also:
Constant Field Values

P_21

private static final int P_21
See Also:
Constant Field Values

P_22

private static final int P_22
See Also:
Constant Field Values

P_23

private static final int P_23
See Also:
Constant Field Values

P_24

private static final int P_24
See Also:
Constant Field Values

P_30

private static final int P_30
See Also:
Constant Field Values

P_31

private static final int P_31
See Also:
Constant Field Values

P_32

private static final int P_32
See Also:
Constant Field Values

P_33

private static final int P_33
See Also:
Constant Field Values

P_34

private static final int P_34
See Also:
Constant Field Values

GF256_FDBK

private static final int GF256_FDBK
See Also:
Constant Field Values

GF256_FDBK_2

private static final int GF256_FDBK_2
See Also:
Constant Field Values

GF256_FDBK_4

private static final int GF256_FDBK_4
See Also:
Constant Field Values

RS_GF_FDBK

private static final int RS_GF_FDBK
See Also:
Constant Field Values

ROUNDS

private static final int ROUNDS
See Also:
Constant Field Values

MAX_ROUNDS

private static final int MAX_ROUNDS
See Also:
Constant Field Values

BLOCK_SIZE

private static final int BLOCK_SIZE
See Also:
Constant Field Values

MAX_KEY_BITS

private static final int MAX_KEY_BITS
See Also:
Constant Field Values

INPUT_WHITEN

private static final int INPUT_WHITEN
See Also:
Constant Field Values

OUTPUT_WHITEN

private static final int OUTPUT_WHITEN
See Also:
Constant Field Values

ROUND_SUBKEYS

private static final int ROUND_SUBKEYS
See Also:
Constant Field Values

TOTAL_SUBKEYS

private static final int TOTAL_SUBKEYS
See Also:
Constant Field Values

SK_STEP

private static final int SK_STEP
See Also:
Constant Field Values

SK_BUMP

private static final int SK_BUMP
See Also:
Constant Field Values

SK_ROTL

private static final int SK_ROTL
See Also:
Constant Field Values

encrypting

private boolean encrypting

gMDS0

private int[] gMDS0

gMDS1

private int[] gMDS1

gMDS2

private int[] gMDS2

gMDS3

private int[] gMDS3

gSubKeys

private int[] gSubKeys
gSubKeys[] and gSBox[] are eventually used in the encryption and decryption methods.


gSBox

private int[] gSBox

k64Cnt

private int k64Cnt

workingKey

private byte[] workingKey
Constructor Detail

TwofishEngine

public TwofishEngine()
Method Detail

init

public void init(boolean encrypting,
                 CipherParameters params)
initialise a Twofish cipher.

Specified by:
init in interface BlockCipher
Parameters:
encrypting - 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.

processBlock

public final 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

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.

setKey

private void setKey(byte[] key)

encryptBlock

private void encryptBlock(byte[] src,
                          int srcIndex,
                          byte[] dst,
                          int dstIndex)
Encrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset. The input will be an exact multiple of our blocksize. encryptBlock uses the pre-calculated gSBox[] and subKey[] arrays.


decryptBlock

private void decryptBlock(byte[] src,
                          int srcIndex,
                          byte[] dst,
                          int dstIndex)
Decrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset. The input will be an exact multiple of our blocksize.


F32

private final int F32(int x,
                      int[] k32)

RS_MDS_Encode

private final int RS_MDS_Encode(int k0,
                                int k1)
Use (12, 8) Reed-Solomon code over GF(256) to produce a key S-box 32-bit entity from 2 key material 32-bit entities.

Parameters:
k0 - first 32-bit entity
k1 - second 32-bit entity
Returns:
Remainder polynomial generated using RS code

RS_rem

private final int RS_rem(int x)
Reed-Solomon code parameters: (12,8) reversible code:

 g(x) = x^4 + (a+1/a)x^3 + ax^2 + (a+1/a)x + 1
 
where a = primitive root of field generator 0x14D


LFSR1

private final int LFSR1(int x)

LFSR2

private final int LFSR2(int x)

Mx_X

private final int Mx_X(int x)

Mx_Y

private final int Mx_Y(int x)

b0

private final int b0(int x)

b1

private final int b1(int x)

b2

private final int b2(int x)

b3

private final int b3(int x)

Fe32_0

private final int Fe32_0(int x)

Fe32_3

private final int Fe32_3(int x)

BytesTo32Bits

private final int BytesTo32Bits(byte[] b,
                                int p)

Bits32ToBytes

private final void Bits32ToBytes(int in,
                                 byte[] b,
                                 int offset)