Class Twofish

java.lang.Object
  extended byTwofish

public final class Twofish
extends Object

Twofish is an AES candidate algorithm. It is a balanced 128-bit Feistel cipher, consisting of 16 rounds. In each round, a 64-bit S-box value is computed from 64 bits of the block, and this value is xored into the other half of the block. The two half-blocks are then exchanged, and the next round begins. Before the first round, all input bits are xored with key- dependent "whitening" subkeys, and after the final round the output bits are xored with other key-dependent whitening subkeys; these subkeys are not used anywhere else in the algorithm.

Twofish was submitted by Bruce Schneier, Doug Whiting, John Kelsey, Chris Hall and David Wagner.

Reference:

  1. TWOFISH2.C -- Optimized C API calls for TWOFISH AES submission, Version 1.00, April 1998, by Doug Whiting.

Copyright © 1998 Systemics Ltd on behalf of the Cryptix Development Team.
All rights reserved.

$Revision: #3 $

Author:
Raif S. Naffah

Field Summary
(package private) static int BLOCK_SIZE
           
private static int GF256_FDBK
          Primitive polynomial for GF(256)
private static int GF256_FDBK_2
           
private static int GF256_FDBK_4
           
private static char[] HEX_DIGITS
          data for hexadecimal visualisation.
private static int INPUT_WHITEN
           
private static int MAX_ROUNDS
           
private static int[][] MDS
          MDS matrix
private static int OUTPUT_WHITEN
           
private static byte[][] P
          Fixed 8x8 permutation S-boxes
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
           
 
Constructor Summary
Twofish()
           
 
Method Summary
private static int _b(int x, int N)
           
private static boolean areEqual(byte[] a, byte[] b)
           
private static int b0(int x)
           
private static int b1(int x)
           
private static int b2(int x)
           
private static int b3(int x)
           
static byte[] blockDecrypt(byte[] in, int inOffset, Object sessionKey)
          Decrypt exactly one block of ciphertext.
static byte[] blockEncrypt(byte[] in, int inOffset, Object sessionKey)
          Encrypt exactly one block of plaintext.
static int blockSize()
           
private static int F32(int k64Cnt, int x, int[] k32)
           
private static int Fe32(int[] sBox, int x, int R)
           
private static String intToString(int n)
          Returns a string of 8 hexadecimal digits (most significant digit first) corresponding to the integer n, which is treated as unsigned.
private static int LFSR1(int x)
           
private static int LFSR2(int x)
           
static Object makeKey(byte[] k)
          Expand a user-supplied key material into a session key.
private static int Mx_1(int x)
           
private static int Mx_X(int x)
           
private static int Mx_Y(int x)
           
private static 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 two key material 32-bit entities.
private static int RS_rem(int x)
           
static boolean self_test()
          A basic symmetric encryption/decryption test.
private static boolean self_test(int keysize)
          A basic symmetric encryption/decryption test for a given key size.
private static String toString(byte[] ba)
          Returns a string of hexadecimal digits from a byte array.
private static String toString(byte[] ba, int offset, int length)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK_SIZE

static final int BLOCK_SIZE
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

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

P

private static final byte[][] P
Fixed 8x8 permutation S-boxes


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
Primitive polynomial for GF(256)

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

MDS

private static final int[][] MDS
MDS matrix


RS_GF_FDBK

private static final int RS_GF_FDBK
See Also:
Constant Field Values

HEX_DIGITS

private static final char[] HEX_DIGITS
data for hexadecimal visualisation.

Constructor Detail

Twofish

public Twofish()
Method Detail

LFSR1

private static final int LFSR1(int x)

LFSR2

private static final int LFSR2(int x)

Mx_1

private static final int Mx_1(int x)

Mx_X

private static final int Mx_X(int x)

Mx_Y

private static final int Mx_Y(int x)

makeKey

public static Object makeKey(byte[] k)
                      throws InvalidKeyException
Expand a user-supplied key material into a session key.

Parameters:
k - The 64/128/192/256-bit user-key to use.
Returns:
This cipher's round keys.
Throws:
InvalidKeyException - If the key is invalid.

blockEncrypt

public static byte[] blockEncrypt(byte[] in,
                                  int inOffset,
                                  Object sessionKey)
Encrypt exactly one block of plaintext.

Parameters:
in - The plaintext.
inOffset - Index of in from which to start considering data.
sessionKey - The session key to use for encryption.
Returns:
The ciphertext generated from a plaintext using the session key.

blockDecrypt

public static byte[] blockDecrypt(byte[] in,
                                  int inOffset,
                                  Object sessionKey)
Decrypt exactly one block of ciphertext.

Parameters:
in - The ciphertext.
inOffset - Index of in from which to start considering data.
sessionKey - The session key to use for decryption.
Returns:
The plaintext generated from a ciphertext using the session key.

self_test

public static boolean self_test()
A basic symmetric encryption/decryption test.


b0

private static final int b0(int x)

b1

private static final int b1(int x)

b2

private static final int b2(int x)

b3

private static final int b3(int x)

RS_MDS_Encode

private static 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 two key material 32-bit entities.

Parameters:
k0 - 1st 32-bit entity.
k1 - 2nd 32-bit entity.
Returns:
Remainder polynomial generated using RS code

RS_rem

private static final int RS_rem(int x)

F32

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

Fe32

private static final int Fe32(int[] sBox,
                              int x,
                              int R)

_b

private static final int _b(int x,
                            int N)

blockSize

public static int blockSize()
Returns:
The length in bytes of the Algorithm input block.

self_test

private static boolean self_test(int keysize)
A basic symmetric encryption/decryption test for a given key size.


areEqual

private static boolean areEqual(byte[] a,
                                byte[] b)
Returns:
True iff the arrays have identical contents.

intToString

private static String intToString(int n)
Returns a string of 8 hexadecimal digits (most significant digit first) corresponding to the integer n, which is treated as unsigned.


toString

private static String toString(byte[] ba)
Returns a string of hexadecimal digits from a byte array. Each byte is converted to 2 hex symbols.


toString

private static String toString(byte[] ba,
                               int offset,
                               int length)