Top Description Fields Constructors Methods
org.bouncycastle.crypto.general

pack-priv final Class TwofishEngine

extends Object
implements BlockCipher
Class Inheritance
All Implemented Interfaces
org.bouncycastle.crypto.internal.BlockCipher
Imports
org.bouncycastle.crypto.internal.BlockCipher, .CipherParameters, .DataLengthException, .OutputLengthException, org.bouncycastle.crypto.internal.params.KeyParameter

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

Modifier and TypeField and Description
private static final int
private boolean
private static final int
private static final int
private static final int
private int[]
private int[]
private int[]
private int[]
private int[]
private int[]
gSubKeys

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

private static final int
private int
private static final int
private static final int
private static final int
private static final byte[][]
P

private static final int
P_00

Define the fixed p0/p1 permutations used in keyed S-box lookup.

private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private byte[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
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)

public String
getAlgorithmName()

Implements org.bouncycastle.crypto.internal.BlockCipher.getAlgorithmName.

Return the name of the algorithm the cipher implements.
public int
getBlockSize()

Implements org.bouncycastle.crypto.internal.BlockCipher.getBlockSize.

Return the block size for this cipher (in bytes).
public void
init(boolean
whether or not we are for encryption.
encrypting
,
CipherParameters
the parameters required to set up the cipher.
params
)

Implements org.bouncycastle.crypto.internal.BlockCipher.init.

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)

public int
processBlock(byte[]
the array containing the input data.
in
,
int
offset into the in array the data starts at.
inOff
,
byte[]
the array the output data will be copied into.
out
,
int
the offset into the out array the output will start at.
outOff
)

Implements org.bouncycastle.crypto.internal.BlockCipher.processBlock.

Process one block of input from the array in and write it to the out array.
public void
private int

Returns:

Remainder polynomial generated using RS code
RS_MDS_Encode
(int
first 32-bit entity
k0
,
int
second 32-bit entity
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:

g(x) = x^4 + (a+1/a)x^3 + ax^2 + (a+1/a)x + 1
private void
setKey(byte[] key)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BLOCK_SIZEback to summary
private static final int BLOCK_SIZE
encryptingback to summary
private boolean encrypting
GF256_FDBKback to summary
private static final int GF256_FDBK
GF256_FDBK_2back to summary
private static final int GF256_FDBK_2
GF256_FDBK_4back to summary
private static final int GF256_FDBK_4
gMDS0back to summary
private int[] gMDS0
gMDS1back to summary
private int[] gMDS1
gMDS2back to summary
private int[] gMDS2
gMDS3back to summary
private int[] gMDS3
gSBoxback to summary
private int[] gSBox
gSubKeysback to summary
private int[] gSubKeys

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

INPUT_WHITENback to summary
private static final int INPUT_WHITEN
k64Cntback to summary
private int k64Cnt
MAX_KEY_BITSback to summary
private static final int MAX_KEY_BITS
MAX_ROUNDSback to summary
private static final int MAX_ROUNDS
OUTPUT_WHITENback to summary
private static final int OUTPUT_WHITEN
Pback to summary
private static final byte[][] P
P_00back to summary
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.

P_01back to summary
private static final int P_01
P_02back to summary
private static final int P_02
P_03back to summary
private static final int P_03
P_04back to summary
private static final int P_04
P_10back to summary
private static final int P_10
P_11back to summary
private static final int P_11
P_12back to summary
private static final int P_12
P_13back to summary
private static final int P_13
P_14back to summary
private static final int P_14
P_20back to summary
private static final int P_20
P_21back to summary
private static final int P_21
P_22back to summary
private static final int P_22
P_23back to summary
private static final int P_23
P_24back to summary
private static final int P_24
P_30back to summary
private static final int P_30
P_31back to summary
private static final int P_31
P_32back to summary
private static final int P_32
P_33back to summary
private static final int P_33
P_34back to summary
private static final int P_34
ROUND_SUBKEYSback to summary
private static final int ROUND_SUBKEYS
ROUNDSback to summary
private static final int ROUNDS
RS_GF_FDBKback to summary
private static final int RS_GF_FDBK
SK_BUMPback to summary
private static final int SK_BUMP
SK_ROTLback to summary
private static final int SK_ROTL
SK_STEPback to summary
private static final int SK_STEP
TOTAL_SUBKEYSback to summary
private static final int TOTAL_SUBKEYS
workingKeyback to summary
private byte[] workingKey

Constructor Detail

TwofishEngineback to summary
public TwofishEngine()

Method Detail

b0back to summary
private int b0(int x)
b1back to summary
private int b1(int x)
b2back to summary
private int b2(int x)
b3back to summary
private int b3(int x)
Bits32ToBytesback to summary
private void Bits32ToBytes(int in, byte[] b, int offset)
BytesTo32Bitsback to summary
private int BytesTo32Bits(byte[] b, int p)
decryptBlockback to summary
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.

encryptBlockback to summary
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.

F32back to summary
private int F32(int x, int[] k32)
Fe32_0back to summary
private int Fe32_0(int x)
Fe32_3back to summary
private int Fe32_3(int x)
getAlgorithmNameback to summary
public String getAlgorithmName()

Implements org.bouncycastle.crypto.internal.BlockCipher.getAlgorithmName.

Doc from org.bouncycastle.crypto.internal.BlockCipher.getAlgorithmName.

Return the name of the algorithm the cipher implements.

Returns:String

the name of the algorithm the cipher implements.

getBlockSizeback to summary
public int getBlockSize()

Implements org.bouncycastle.crypto.internal.BlockCipher.getBlockSize.

Doc from org.bouncycastle.crypto.internal.BlockCipher.getBlockSize.

Return the block size for this cipher (in bytes).

Returns:int

the block size for this cipher in bytes.

initback to summary
public void init(boolean encrypting, CipherParameters params)

Implements org.bouncycastle.crypto.internal.BlockCipher.init.

initialise a Twofish cipher.

Parameters
encrypting:boolean

whether or not we are for encryption.

params:CipherParameters

the parameters required to set up the cipher.

Exceptions
IllegalArgumentException:
if the params argument is inappropriate.
LFSR1back to summary
private int LFSR1(int x)
LFSR2back to summary
private int LFSR2(int x)
Mx_Xback to summary
private int Mx_X(int x)
Mx_Yback to summary
private int Mx_Y(int x)
processBlockback to summary
public int processBlock(byte[] in, int inOff, byte[] out, int outOff)

Implements org.bouncycastle.crypto.internal.BlockCipher.processBlock.

Doc from org.bouncycastle.crypto.internal.BlockCipher.processBlock.

Process one block of input from the array in and write it to the out array.

Parameters
in:byte[]

the array containing the input data.

inOff:int

offset into the in array the data starts at.

out:byte[]

the array the output data will be copied into.

outOff:int

the offset into the out array the output will start at.

Returns:int

the number of bytes processed and produced.

resetback to summary
public void reset()

Implements org.bouncycastle.crypto.internal.BlockCipher.reset.

Doc from org.bouncycastle.crypto.internal.BlockCipher.reset.

Reset the cipher. After resetting the cipher is in the same state as it was after the last init (if there was one).

RS_MDS_Encodeback to summary
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.

Parameters
k0:int

first 32-bit entity

k1:int

second 32-bit entity

Returns:int

Remainder polynomial generated using RS code

RS_remback to summary
private 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
setKeyback to summary
private void setKey(byte[] key)