de.flexiprovider.api
Class Mode

java.lang.Object
  |
  +--de.flexiprovider.api.Mode
Direct Known Subclasses:
CBC, CFB, CTR, ECB, OFB

public abstract class Mode
extends java.lang.Object

The abstract class Mode is the base for all modes implemented in the package cdc. It defines the set of methods which have to be implemented to cooperate with the BasicCipher class. Each class that implements such a mode has to exist in the package "de.flexiprovider.common.mode" and the classname must be the same as the name used in the call of Cipher.getInstance(). e.g. if you call Cipher.getInstance("SAFER+/CBC/NoPadding"), the code will look for the class de.flexiprovider.common.mode.CBC

Author:
Marcus Lippert, Ralf-Philipp Weinmann

Field Summary
protected  int blockSize
          The block size of the mode
protected  byte[] iv
          The initialization vector
 
Constructor Summary
Mode()
           
 
Method Summary
protected  int getCipherBlockSize()
          Return the block size of the underlying cipher.
protected  void initCipherDecrypt(SecretKey key, AlgorithmParameterSpec cipherParams)
          Initialize the block cipher for decryption.
protected  void initCipherEncrypt(SecretKey key, AlgorithmParameterSpec cipherParams)
          Initialize the block cipher for encryption.
protected abstract  void initDecrypt(SecretKey key, ModeParameterSpec modeParams, AlgorithmParameterSpec cipherParams)
          Initialize the Mode object for decryption and compute the mode block size.
protected abstract  void initEncrypt(SecretKey key, ModeParameterSpec modeParams, AlgorithmParameterSpec cipherParams)
          Initialize the Mode object for encryption and compute the mode block size.
protected abstract  void nextChunkDecrypt(byte[] input, int inOff, byte[] output, int outOff)
          Decrypt the next data block.
protected abstract  void nextChunkEncrypt(byte[] input, int inOff, byte[] output, int outOff)
          Encrypt the next data block.
protected abstract  void reset()
          reset() is called after doFinal() in order to prepare the mode for the next operation.
(package private)  void setBlockCipher(BlockCipher blockCipher)
          Set the BlockCipher to use with this mode.
protected  void singleBlockDecrypt(byte[] input, int inOff, byte[] output, int outOff)
          Decrypt a single block with the cipher engine.
protected  void singleBlockEncrypt(byte[] input, int inOff, byte[] output, int outOff)
          Encrypt a single block with the cipher engine.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

iv

protected byte[] iv
The initialization vector

blockSize

protected int blockSize
The block size of the mode
Constructor Detail

Mode

public Mode()
Method Detail

setBlockCipher

final void setBlockCipher(BlockCipher blockCipher)
Set the BlockCipher to use with this mode.
Parameters:
blockCipher - the block cipher

initEncrypt

protected abstract void initEncrypt(SecretKey key,
                                    ModeParameterSpec modeParams,
                                    AlgorithmParameterSpec cipherParams)
                             throws InvalidKeyException,
                                    InvalidAlgorithmParameterException
Initialize the Mode object for encryption and compute the mode block size. This value usually depends on the block size of the used block cipher. It is supposed that all block ciphers return a sane value via BlockCipher.getCipherBlockSize() after initialization.
Parameters:
key - the key used for encryption
modeParams - additional mode parameters
cipherParams - additional algorithm parameters
Throws:
InvalidKeyException - if the key is inappropriate for initializing the underlying block cipher.
InvalidAlgorithmParameterException - if the parameters are inappropriate for initializing the underlying block cipher.

initDecrypt

protected abstract void initDecrypt(SecretKey key,
                                    ModeParameterSpec modeParams,
                                    AlgorithmParameterSpec cipherParams)
                             throws InvalidKeyException,
                                    InvalidAlgorithmParameterException
Initialize the Mode object for decryption and compute the mode block size. This value usually depends on the block size of the used block cipher. It is supposed that all block ciphers return a sane value via BlockCipher.getCipherBlockSize() after initialization.
Parameters:
key - the key used for decryption
modeParams - additional mode parameters
cipherParams - additional algorithm parameters
Throws:
InvalidKeyException - if the key is inappropriate for initializing the underlying block cipher.
InvalidAlgorithmParameterException - if the parameters are inappropriate for initializing the underlying block cipher.

nextChunkEncrypt

protected abstract void nextChunkEncrypt(byte[] input,
                                         int inOff,
                                         byte[] output,
                                         int outOff)
Encrypt the next data block. Any special features of the Mode should be implemented here.
Parameters:
input - input data buffer
inOff - input data offset
output - output data buffer
outOff - output data offset

nextChunkDecrypt

protected abstract void nextChunkDecrypt(byte[] input,
                                         int inOff,
                                         byte[] output,
                                         int outOff)
Decrypt the next data block. Any special features of the Mode should be implemented here.
Parameters:
input - input data buffer
inOff - input data offset
output - output data buffer
outOff - output data offset

reset

protected abstract void reset()
reset() is called after doFinal() in order to prepare the mode for the next operation.

initCipherEncrypt

protected final void initCipherEncrypt(SecretKey key,
                                       AlgorithmParameterSpec cipherParams)
                                throws InvalidKeyException,
                                       InvalidAlgorithmParameterException
Initialize the block cipher for encryption.
Parameters:
key - the secret key to use for encryption
cipherParams - the parameters
Throws:
InvalidKeyException - if the given key is inappropriate for this cipher.
InvalidAlgorithmParameterException - if the given parameters are inappropriate for this cipher.

initCipherDecrypt

protected final void initCipherDecrypt(SecretKey key,
                                       AlgorithmParameterSpec cipherParams)
                                throws InvalidKeyException,
                                       InvalidAlgorithmParameterException
Initialize the block cipher for decryption.
Parameters:
key - the secret key to use for decryption
cipherParams - the parameters
Throws:
InvalidKeyException - if the given key is inappropriate for this cipher.
InvalidAlgorithmParameterException - if the given parameters are inappropriate for this cipher.

getCipherBlockSize

protected final int getCipherBlockSize()
Return the block size of the underlying cipher. It is supposed that all block ciphers return a sane value via BlockCipher.getCipherBlockSize() after initialization.
Returns:
the block size of the underlying cipher

singleBlockEncrypt

protected final void singleBlockEncrypt(byte[] input,
                                        int inOff,
                                        byte[] output,
                                        int outOff)
Encrypt a single block with the cipher engine.
Parameters:
input - array of bytes which contains the plaintext to be encrypted
inOff - index in array in, where the plaintext block starts
output - array of bytes which will contain the ciphertext startig at outOffset
outOff - index in array out, where the ciphertext block will start

singleBlockDecrypt

protected final void singleBlockDecrypt(byte[] input,
                                        int inOff,
                                        byte[] output,
                                        int outOff)
Decrypt a single block with the cipher engine.
Parameters:
input - array of bytes which contains the ciphertext to be decrypted
inOff - index in array in, where the ciphertext block starts
output - array of bytes which will contain the plaintext starting at outOffset
outOff - index in array out, where the plaintext block will start