au.net.aba.crypto.provider
Class BlockCipher

java.lang.Object
  |
  +--javax.crypto.CipherSpi
        |
        +--au.net.aba.crypto.provider.BlockCipher
Direct Known Subclasses:
Blowfish, DES, IDEA, RSA, Twofish

public abstract class BlockCipher
extends CipherSpi

This abstract class is the base class for all Block Ciphers. It will perform the necessary block buffering and then pass the data on through the protected interface.

This class supports PKCS#5 and NoPadding, as well as supporting ECB and CBC modes. The standard block size is 8.

To implement a new block cipher it is necessary to implement the three abstract methods that re-key the cipher, do encryption and decryption using the current. It may also be necessary to override the engineSetMode method if CBC or ECB are not supported. If different padding mechanisms are used engineSetPadding may be overridden (in which case it may also be necessary to override engineGetOutputSize). For block sizes other than 8 engineGetBlockSize should be overridden. Finally if the algorithm supports AlgorithmParameterSpec just override the engineInit method that accepts those parameters.


Field Summary
protected static int BLOCK_SIZE
           
protected  byte[] buffer
           
protected  int bufferPos
           
protected static int CBC
           
protected  byte[] cbcV
           
protected static int ECB
           
protected  boolean firstBlock
           
static java.lang.String ident
           
protected  byte[] ivec
           
protected  boolean ivEncrypted
           
protected  boolean ivInline
           
protected  java.security.Key key
           
protected  int mode
           
protected  boolean paddedStream
           
protected  java.security.SecureRandom random
           
protected  int streamMode
           
 
Fields inherited from class javax.crypto.CipherSpi
ident
 
Constructor Summary
BlockCipher()
           
 
Method Summary
protected abstract  int decryptBlock(byte[] in, int inoff, int len, byte[] out, int outOff)
          Decrypt the given block starting at the given offset and place the result in the provided buffer starting at the given offset.
protected abstract  int encryptBlock(byte[] in, int inoff, int len, byte[] out, int outOff)
          Encrypt the given block starting at the given offset and place the result in the provided buffer starting at the given offset.
protected  byte[] engineDoFinal(byte[] input, int inputOff, int inputLen)
          Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
protected  int engineDoFinal(byte[] input, int inputOff, int inputLen, byte[] output, int outputOff)
          Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
protected  int engineGetBlockSize()
          Returns the block size (in bytes).
protected  byte[] engineGetIV()
          Returns the initialisation vector (IV) in a new buffer.
protected  int engineGetOutputSize(int inputLen)
          Returns the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inputLen (in bytes).
protected  java.security.AlgorithmParameters engineGetParameters()
          Returns the parameters used with this cipher.
protected  void engineInit(int opmode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random)
          Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness.
protected  void engineInit(int opmode, java.security.Key key, java.security.AlgorithmParameters params, java.security.SecureRandom random)
          Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness.
protected  void engineInit(int opmode, java.security.Key key, java.security.SecureRandom random)
          Initialises this cipher with a key and a source of randomness.
protected  void engineSetMode(java.lang.String mode)
          Sets the mode of this cipher.
protected  void engineSetPadding(java.lang.String padding)
          Sets the padding mechanism of this cipher.
protected  byte[] engineUpdate(byte[] input, int inputOff, int inputLen)
          Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part.
protected  int engineUpdate(byte[] input, int inputOff, int inputLen, byte[] output, int outputOff)
          Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part.
protected  void reset()
          Reset the Cipher to uninitialised.
protected abstract  void setKey(java.security.Key key)
          Re-key the cipher.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ident

public static final java.lang.String ident

BLOCK_SIZE

protected static final int BLOCK_SIZE

ECB

protected static final int ECB

CBC

protected static final int CBC

buffer

protected byte[] buffer

bufferPos

protected int bufferPos

mode

protected int mode

paddedStream

protected boolean paddedStream

streamMode

protected int streamMode

ivec

protected byte[] ivec

cbcV

protected byte[] cbcV

ivInline

protected boolean ivInline

ivEncrypted

protected boolean ivEncrypted

firstBlock

protected boolean firstBlock

key

protected java.security.Key key

random

protected java.security.SecureRandom random
Constructor Detail

BlockCipher

public BlockCipher()
Method Detail

engineSetMode

protected void engineSetMode(java.lang.String mode)
                      throws java.security.NoSuchAlgorithmException
Sets the mode of this cipher. Valid modes for are; ECB or CBC.
Parameters:
mode - the cipher mode
Throws:
java.security.NoSuchAlgorithmException - if the requested cipher mode does not exist
Overrides:
engineSetMode in class CipherSpi

engineSetPadding

protected void engineSetPadding(java.lang.String padding)
                         throws NoSuchPaddingException
Sets the padding mechanism of this cipher. Valid padding mechanisms are; "PKCS5Padding" and "NoPadding".
Parameters:
padding - the padding mechanism
Throws:
NoSuchPaddingException - if the requested padding mechanism does not exist.
Overrides:
engineSetPadding in class CipherSpi

engineGetBlockSize

protected int engineGetBlockSize()
Returns the block size (in bytes). In this case 8. This method should be overridden if the given algorithm has a different block size.
Returns:
the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
Overrides:
engineGetBlockSize in class CipherSpi

engineGetOutputSize

protected int engineGetOutputSize(int inputLen)
Returns the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inputLen (in bytes).

This call takes into account any unprocessed (buffered) data from a previous update call, and padding.

The actual output length of the next update or doFinal call may be smaller than the length returned by this method.

Parameters:
inputLen - the input length (in bytes)
Returns:
the required output buffer size (in bytes)
Overrides:
engineGetOutputSize in class CipherSpi

engineGetIV

protected byte[] engineGetIV()
Returns the initialisation vector (IV) in a new buffer.

This is useful in the context of password-based encryption or decryption, where the IV is derived from a user-provided passphrase.

Returns:
the initialisation vector in a new buffer, or null if the underlying algorithm does not use an IV, or if the IV has not yet been set.
Overrides:
engineGetIV in class CipherSpi

engineGetParameters

protected java.security.AlgorithmParameters engineGetParameters()
Returns the parameters used with this cipher.

The returned parameters may be the same that were used to initialise 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 initialised with any).

Returns:
the parameters used with this cipher, or null if this cipher does not use any parameters.
Overrides:
engineGetParameters in class CipherSpi

engineInit

protected void engineInit(int opmode,
                          java.security.Key key,
                          java.security.SecureRandom random)
                   throws java.security.InvalidKeyException
Initialises this cipher with a key and a source of randomness.

The cipher is initialised for encryption or decryption, depending on the value of opmode.

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialised for encryption, and raise an InvalidKeyException if it is being initialised for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

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

Note that when a Cipher object is initialised, it loses all previously-acquired state. In other words, initialising a Cipher is equivalent to creating a new instance of that Cipher and initialising it

Parameters:
opmode - the operation mode of this cipher (this is either ENCRYPT_MODE or DECRYPT_MODE)
key - the encryption key
random - the source of randomness
Throws:
java.security.InvalidKeyException - if the given key is inappropriate for initialising this cipher
Overrides:
engineInit in class CipherSpi

engineInit

protected void engineInit(int opmode,
                          java.security.Key key,
                          java.security.AlgorithmParameters params,
                          java.security.SecureRandom random)
                   throws java.security.InvalidKeyException,
                          java.security.InvalidAlgorithmParameterException
Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness.

The cipher is initialised for encryption or decryption, depending on the value of opmode.

If this cipher requires any algorithm parameters and params is null, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialised for encryption, and raise an InvalidAlgorithmParameterException if it is being initialised for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

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

Note that when a Cipher object is initialised, it loses all previously-acquired state. In other words, initialising a Cipher is equivalent to creating a new instance of that Cipher and initialising it.

Parameters:
opmode - the operation mode of this cipher (this is either ENCRYPT_MODE or DECRYPT_MODE)
key - the encryption key
params - the algorithm parameters
random - the source of randomness
Throws:
java.security.InvalidKeyException - if the given key is inappropriate for initialising this cipher
java.security.InvalidAlgorithmParameterException - if the given algorithm parameters are inappropriate for this cipher, or if this cipher is being initialised fro decryption and requires algorithm parameters and params is null
Overrides:
engineInit in class CipherSpi

engineInit

protected void engineInit(int opmode,
                          java.security.Key key,
                          java.security.spec.AlgorithmParameterSpec params,
                          java.security.SecureRandom random)
                   throws java.security.InvalidKeyException,
                          java.security.InvalidAlgorithmParameterException
Initialises this cipher with a key, a set of algorithm parameters, and a source of randomness.

The cipher is initialised for encryption or decryption, depending on the value of opmode.

If this cipher requires any algorithm parameters and params is null, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialised for encryption, and raise an InvalidAlgorithmParameterException if it is being initialised for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

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

Note that when a Cipher object is initialised, it loses all previously-acquired state. In other words, initialising a Cipher is equivalent to creating a new instance of that Cipher and initialising it.

Parameters:
opmode - the operation mode of this cipher (this is either ENCRYPT_MODE or DECRYPT_MODE)
key - the encryption key
params - the algorithm parameters
random - the source of randomness
Throws:
java.security.InvalidKeyException - if the given key is inappropriate for initialising this cipher
java.security.InvalidAlgorithmParameterException - if the given algorithm parameters are inappropriate for this cipher, or if this cipher is being initialised fro decryption and requires algorithm parameters and params is null
Overrides:
engineInit in class CipherSpi

engineUpdate

protected byte[] engineUpdate(byte[] input,
                              int inputOff,
                              int inputLen)
Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part.

The first inputLen bytes in the input buffer, starting at inputOffset, are processed, and the result is stored in a new buffer.

Parameters:
input - the input buffer
inputOffset - the offset in input where the input starts
inputLen - the input length
Returns:
the new buffer with the result, or null if the underlying cipher is a block cipher and the input data is too short to result in a new block.
Overrides:
engineUpdate in class CipherSpi

engineUpdate

protected int engineUpdate(byte[] input,
                           int inputOff,
                           int inputLen,
                           byte[] output,
                           int outputOff)
                    throws ShortBufferException
Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialised), processing another data part.

The first inputLen bytes in the input buffer, starting at inputOffset, are processed, and the result is stored in the output buffer, starting at outputOffset.

If the output buffer is too small to hold the result, a ShortBufferException is thrown. In this case, repeat this call with a larger output buffer. Use getOutputSize to determine how big the output buffer should be.

Parameters:
input - the input buffer
inputOffset - the offset in input where the input starts
inputLen - the input length
output - the buffer for the result
outputOffset - the offset in output where the result is stored
Returns:
the number of bytes stored in output
Throws:
ShortBufferException - if the given output buffer is too small to hold the result
Overrides:
engineUpdate in class CipherSpi

engineDoFinal

protected byte[] engineDoFinal(byte[] input,
                               int inputOff,
                               int inputLen)
                        throws IllegalBlockSizeException,
                               BadPaddingException
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialised.

The first inputLen bytes in the input buffer, starting at inputOffset, and any input bytes that may have been buffered during a previous update operation, are processed, with padding (if requested) being applied. The result is stored in a new buffer.

The cipher is reset to its initial state (uninitialised) after this call.

Parameters:
input - the input buffer
inputOff - the offset in input where the input starts
inputLen - the input length
Returns:
the new buffer with the result
Throws:
IllegalBlockSizeException - if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size
BadPaddingException - if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes.
Overrides:
engineDoFinal in class CipherSpi

engineDoFinal

protected int engineDoFinal(byte[] input,
                            int inputOff,
                            int inputLen,
                            byte[] output,
                            int outputOff)
                     throws ShortBufferException,
                            IllegalBlockSizeException,
                            BadPaddingException
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialised.

The first inputLen bytes in the input buffer, starting at inputOffset, and any input bytes that may have been buffered during a previous update operation, are processed, with padding (if requested) being applied. The result is stored in the output buffer, starting at outputOffset.

If the output buffer is too small to hold the result, a ShortBufferException is thrown. In this case, repeat this call with a larger output buffer. Use getOutputSize to determine how big the output buffer should be.

Parameters:
input - the input buffer
inputOffset - - the offset in input where the input starts
inputLen - - the input length
output - - the buffer for the result
outputOffset - - the offset in output where the result is stored
Throws:
IllegalBlockSizeException - if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size
ShortBufferException - if the given output buffer is too small to hold the result
BadPaddingException - if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes
Overrides:
engineDoFinal in class CipherSpi

reset

protected void reset()
Reset the Cipher to uninitialised.

setKey

protected abstract void setKey(java.security.Key key)
                        throws java.security.InvalidKeyException
Re-key the cipher. If the provided Key is not compatible with this cipher the exception should throw an InvalidKeyException.

encryptBlock

protected abstract int encryptBlock(byte[] in,
                                    int inoff,
                                    int len,
                                    byte[] out,
                                    int outOff)
                             throws IllegalBlockSizeException
Encrypt the given block 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.

decryptBlock

protected abstract int decryptBlock(byte[] in,
                                    int inoff,
                                    int len,
                                    byte[] out,
                                    int outOff)
                             throws BadPaddingException
Decrypt the given block 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.