iaik.security.cipher
Class Twofish

java.lang.Object
  |
  +--javax.crypto.CipherSpi
        |
        +--iaik.security.cipher.BufferedCipher
              |
              +--iaik.security.cipher.Twofish

public class Twofish
extends iaik.security.cipher.BufferedCipher

Extends the BufferedCipher class for adding a buffering mechanism to the underlying Twofish cipher.

Twofish is a 128-bit Feistel-type block cipher that accepts a variable-length key up to 256 bits. It has been developed by B. Schneier, J. Kelsey, D. Whiting, D. Wagner, C. Hall and N. Ferguson. It belongs to the final AES candidates. For more information consult the AES Web site at http://www.nist.gov/aes/.

You should contact the authors to determine the legal status of this algorithm!

This class only creates a BufferedCipher object for the Twofish cipher.

Applications shall use

Cipher.getInstance("Twofish", "IAIK");
 
for creating a Twofish object. They optionally may specifiy operation mode (ECB (default), CBC, PCBC, OFB, CFB) and padding scheme (NoPadding (default), or PKCS5Padding as described in the PKCS #5: Password-Based Encryption Standard).

When requesting this Twofish implementation without any mode specification (Cipher.getInstance("Twofish")), the Twofish algorithm is used in pure ECB (Electronic Code Book) mode encrypting plaintext blocks into ciphertext blocks independently from each other. The ECB mode is prone to codebook attacks and block replay. A codebook attack may be successfully when being able to read plain- and corresponding ciphertext blocks for a certain quantity of messages making it possible to generate a codebook for decrypting blocks of further messages without knowing the key. For being effective against codebook analyses and block replay (often messages contain common sub-parts making it possible to unnoticed replace these blocks) one can use the CBC mode makes the encryption of one block of plain data conditional on all previously encrypted data blocks.

Since ECB encrypts each single block independently, it enables random access to encrypted data blocks which may be preferable for database encrypting. Often ECB is used for key-encrypting.

Version:
File Revision 13
See Also:
TwofishKeyGenerator, BufferedCipher, Cipher

Constructor Summary
Twofish()
          Creates a Twofish object by calling the BufferedCipher constructor for the Twofish cipher.
 
Method Summary
 byte[] engineDoFinal(byte[] in, int inOff, int inLen)
          Returns the result of the last step of a multi-step en/decryption operation or the result of a single-step en/decryption operation by processing the given input data and any remaining buffered data.
 int engineDoFinal(byte[] in, int inOff, int inLen, byte[] out, int outOff)
          Performs the last step of a multi-step en/decryption operation or a single-step en/decryption operation by processing the given input data and any remaining buffered data.
 int engineGetBlockSize()
          Returns the block size corresponding to this cipher.
 byte[] engineGetIV()
          Returns a byte array containing the initialization vector (IV).
protected  int engineGetKeySize(Key key)
          New method in JCE 1.2.1
 int engineGetOutputSize(int inLen)
          Returns the output buffer size necessary for capturing the data resulting from the next update or doFinal operation including any data currently being buffered.
 AlgorithmParameters engineGetParameters()
          Returns the parameters used with this cipher.
 void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)
          Initializes this cipher object with proper key and algorithm parameter values, and some random seed.
 void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)
          Initializes this cipher object with proper key and algorithm parameter values, and some random seed.
 void engineInit(int opmode, Key key, SecureRandom random)
          Initializes this cipher object with a proper key and some random seed.
 void engineSetMode(String mode)
          Sets the mode of this cipher.
 void engineSetPadding(String paddingName)
          Sets the padding scheme of this cipher.
protected  Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)
          Engine method for key unwrapping.
 byte[] engineUpdate(byte[] in, int inOff, int inLen)
          This method is used to encrypt or decrypt chunks of data of any length.
 int engineUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff)
          This method is used to encrypt or decrypt chunks of data of any length.
protected  byte[] engineWrap(Key key)
          Engine method for key wrapping.
 int getModeBlockSize()
          Returns the block size corresponding to the actual cipher mode.
 String toString()
          Returns a string representation of this Cipher.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Twofish

public Twofish()
Creates a Twofish object by calling the BufferedCipher constructor for the Twofish cipher. This constructor is only internally used for initializing a Twofish Cipher. Applications should not call this constructor to get a Twofish Cipher; they should call one of the Cipher.getInstance factory methods instead.

See Also:
Cipher.getInstance(java.lang.String)
Method Detail

engineInit

public void engineInit(int opmode,
                       Key key,
                       AlgorithmParameterSpec params,
                       SecureRandom random)
                throws InvalidKeyException,
                       InvalidAlgorithmParameterException
Initializes this cipher object with proper key and algorithm parameter values, and some random seed.

Before a cipher object is ready for data processing, it has to be initialized according to the desired cryptographic operation, which is specified by the opmode parameter (either ENCRYPT_MODE or DECCRYPT_MODE), e.g.:

 cipher_obj.init(Cipher.ENCRYPT_MODE, key, alg_params, random_seed);
 

The Cipher init will call the proper CipherSpi engineInit method.

Overrides:
engineInit in class CipherSpi
Parameters:
opmode - the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)
key - the key
params - the algorithm parameters
random - the random seed
Throws:
InvalidKeyException - if the given key cannot be used for initializing this cipher
InvalidAlgorithmParameterException - if the given algorithm parameters donīt match to this cipher
See Also:
Cipher.init(int, java.security.Key), CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)

engineInit

public void engineInit(int opmode,
                       Key key,
                       SecureRandom random)
                throws InvalidKeyException
Initializes this cipher object with a proper key and some random seed.

Before a cipher object is ready for data processing, it has to be initialized according to the desired cryptographic operation, which is specified by the opmode parameter (either ENCRYPT_MODE or DECCRYPT_MODE), e.g.:

 cipher_obj.init(Cipher.ENCRYPT_MODE, key, random_seed);
 

The Cipher init will call the proper CipherSpi engineInit method.

If this cipher (including its underlying feedback or padding scheme) requires any random bytes, it will get them from random.

Overrides:
engineInit in class CipherSpi
Parameters:
opmode - the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)
key - the key
random - the random seed
Throws:
InvalidKeyException - if the given key cannot be used for initializing this cipher
See Also:
Cipher.init(int, java.security.Key), CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)

engineInit

public void engineInit(int opmode,
                       Key key,
                       AlgorithmParameters params,
                       SecureRandom random)
                throws InvalidKeyException,
                       InvalidAlgorithmParameterException
Initializes this cipher object with proper key and algorithm parameter values, and some random seed.

Before a cipher object is ready for data processing, it has to be initialized according to the desired cryptographic operation, which is specified by the opmode parameter (either ENCRYPT_MODE or DECCRYPT_MODE), e.g.:

 cipher_obj.init(Cipher.ENCRYPT_MODE, key, alg_params, random_seed);
 

The Cipher init will call the proper CipherSpi engineInit method.

Overrides:
engineInit in class CipherSpi
Parameters:
opmode - the operation mode for which this cipher is used (ENCRYPT_MODE or DECRYPT_MODE)
key - the key
params - the algorithm parameters
random - the random seed
Throws:
InvalidKeyException - if the given key cannot be used for initializing this cipher
InvalidAlgorithmParameterException - if the given algorithm parameters donīt match to this cipher
See Also:
Cipher.init(int, java.security.Key), CipherSpi.engineInit(int, java.security.Key, java.security.SecureRandom)

engineGetParameters

public AlgorithmParameters engineGetParameters()
Description copied from class: CipherSpi
Returns the parameters used with this cipher. The returned parameters may be the same that were used to initialize this cipher, or may contain the default set of parameters or a set of randomly generated parameters used by the underlying cipher implementation (provided that the underlying cipher implementation uses a default set of parameters or creates new parameters if it needs parameters but was not initialized with any).
Overrides:
engineGetParameters in class CipherSpi
Following copied from class: javax.crypto.CipherSpi
Returns:
the parameters used with this cipher, or null if this cipher does not use any parameters

engineSetPadding

public void engineSetPadding(String paddingName)
                      throws NoSuchPaddingException
Sets the padding scheme of this cipher.

If an application creates a cipher object by calling Cipher.getInstance(...) without specifying a particular padding scheme, engineSetMode is supplied with the default "NoPadding" scheme.

The following padding schemes are supported:

Overrides:
engineSetPadding in class CipherSpi
Parameters:
paddingName - the name of the padding scheme
Throws:
NoSuchPaddingException - if this padding scheme is not supported
See Also:
CipherSpi.engineSetPadding(java.lang.String)

engineSetMode

public void engineSetMode(String mode)
                   throws NoSuchAlgorithmException
Sets the mode of this cipher.

If an application creates a cipher object by calling Cipher.getInstance(...) without specifying a particular cipher mode, engineSetMode is supplied with the default "ECB" mode. The following modes are supported:

Overrides:
engineSetMode in class CipherSpi
Parameters:
mode - the cipher mode
Throws:
NoSuchAlgorithmException - if this cipher mode is not supported
See Also:
CipherSpi.engineSetMode(java.lang.String)

engineUpdate

public byte[] engineUpdate(byte[] in,
                           int inOff,
                           int inLen)
This method is used to encrypt or decrypt chunks of data of any length. To get get the last data in the buffer use a doFinal() method. This method will automatically allocate an output buffer of the right size.
Overrides:
engineUpdate in class CipherSpi
Parameters:
in - the input data.
inOff - the offset indicating where the subarray starts in the in array.
inLen - the length of the subarray.
isFinal - true, if this is the last call to update
Returns:
array containing the en/decrypted data
See Also:
BufferedCipher.engineDoFinal(byte[], int, int, byte[], int), Cipher.doFinal(), Cipher.update(byte[]), CipherSpi.engineUpdate(byte[], int, int)

engineUpdate

public int engineUpdate(byte[] in,
                        int inOff,
                        int inLen,
                        byte[] out,
                        int outOff)
                 throws ShortBufferException
This method is used to encrypt or decrypt chunks of data of any length. To get get the last data in the buffer use a doFinal() method.
Overrides:
engineUpdate in class CipherSpi
Parameters:
in - the input data.
inOff - the offset indicating where the subarray starts in the in array.
inLen - the length of the subarray.
out - the output buffer.
outOff - the offset indicating where to start writing the result into the output buffer.
Returns:
number of bytes written to out array.
Throws:
ShoetBufferException - if the buffer size is to short
See Also:
BufferedCipher.engineDoFinal(byte[], int, int, byte[], int), Cipher.doFinal(), Cipher.update(byte[]), CipherSpi.engineUpdate(byte[], int, int)

engineGetOutputSize

public int engineGetOutputSize(int inLen)
Returns the output buffer size necessary for capturing the data resulting from the next update or doFinal operation including any data currently being buffered.
Overrides:
engineGetOutputSize in class CipherSpi
Parameters:
inLen - the number of bytes to process
Returns:
the size of the output buffer (in bytes)
See Also:
Cipher.getOutputSize(int), CipherSpi.engineGetOutputSize(int)

engineGetIV

public byte[] engineGetIV()
Returns a byte array containing the initialization vector (IV). If the algorithm corresponding to this cipher object does not use an initialization vector, null is returned.
Overrides:
engineGetIV in class CipherSpi
Returns:
the initialization vector in a byte array if this cipher object operates with an IV and the IV already has been set, null otherwise.
See Also:
Cipher.getIV(), CipherSpi.engineGetIV()

getModeBlockSize

public int getModeBlockSize()
Returns the block size corresponding to the actual cipher mode. This may differ from the actual block size of the cipher in the OFB, CFB and CTR mode. This is the input size that an application must supply to an update call to get an output.
Returns:
The block size (in bytes) of the current mode. This is always at least one.

engineGetBlockSize

public int engineGetBlockSize()
Returns the block size corresponding to this cipher. The block size is returned in bytes. If the implemented algorithm is not a block cipher, 0 is returned.
Overrides:
engineGetBlockSize in class CipherSpi
Returns:
the block size (in bytes) if this cipher is a block cipher, 0 otherwise.
See Also:
Cipher.getBlockSize(), CipherSpi.engineGetBlockSize()

engineDoFinal

public int engineDoFinal(byte[] in,
                         int inOff,
                         int inLen,
                         byte[] out,
                         int outOff)
                  throws ShortBufferException,
                         IllegalBlockSizeException,
                         BadPaddingException
Performs the last step of a multi-step en/decryption operation or a single-step en/decryption operation by processing the given input data and any remaining buffered data.

The data to be processed is given in an input byte array. Beginning at inputOffset, only the first inputLen bytes are en/decrypted, including any buffered bytes of a previous update operation. If necessary, padding is performed. The result is stored in the given output byte array, beginning at outputOffset. The number of bytes stored in this byte array are returned.

Overrides:
engineDoFinal in class CipherSpi
Parameters:
in - the byte array holding the data to be processed
inOff - the offset indicating the start position within the input byte array
inLen - the number of bytes to be processed
out - the byte array for holding the result
outOff - the offset indicating the start position within the output byte array to which the en/decrypted data is written
Returns:
the number of bytes stored in the output byte array
Throws:
ShortBufferException - if the given output buffer is too small for holding the result
IllegalBlockSizeException - if the total length of the processed data is not a multiple of the block size for a (no padding performing) block cipher
BadPaddingException - if the decrypted data is not bounded by the proper padding bytes after data decryption including (un)padding
See Also:
Cipher.doFinal(), CipherSpi.engineDoFinal(byte[], int, int)

engineDoFinal

public byte[] engineDoFinal(byte[] in,
                            int inOff,
                            int inLen)
                     throws IllegalBlockSizeException,
                            BadPaddingException
Returns the result of the last step of a multi-step en/decryption operation or the result of a single-step en/decryption operation by processing the given input data and any remaining buffered data.

The data to be processed is given in an input byte array. Beginning at inputOffset, only the first inputLen bytes are en/decrypted, including any buffered bytes of a previous update operation. If necessary, padding is performed. The result is returned as a output byte array.

Overrides:
engineDoFinal in class CipherSpi
Parameters:
in - the byte array holding the data to be processed
inOff - the offset indicating the start position within the input byte array
inLen - the number of bytes to be processed
Returns:
the byte array containing the en/decrypted data
Throws:
IllegalBlockSizeException - if the total length of the processed data is not a multiple of the block size for a (no padding performing) block cipher
BadPaddingException - if the decrypted data is not bounded by the proper padding bytes after data decryption including (un)padding
See Also:
Cipher.doFinal(), CipherSpi.engineDoFinal(byte[], int, int)

toString

public String toString()
Returns a string representation of this Cipher.
Overrides:
toString in class Object
Returns:
a string representation

engineGetKeySize

protected int engineGetKeySize(Key key)
                        throws InvalidKeyException
Description copied from class: CipherSpi
New method in JCE 1.2.1
Overrides:
engineGetKeySize in class CipherSpi

engineWrap

protected byte[] engineWrap(Key key)
                     throws IllegalBlockSizeException,
                            InvalidKeyException
Description copied from class: CipherSpi
Engine method for key wrapping. Since JCE 1.2.1
Overrides:
engineWrap in class CipherSpi

engineUnwrap

protected Key engineUnwrap(byte[] wrappedKey,
                           String wrappedKeyAlgorithm,
                           int wrappedKeyType)
                    throws InvalidKeyException,
                           NoSuchAlgorithmException
Description copied from class: CipherSpi
Engine method for key unwrapping. Since JCE 1.2.1
Overrides:
engineUnwrap in class CipherSpi

This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK