org.bouncycastle.crypto.modes
Class PGPCFBBlockCipher

java.lang.Object
  extended by org.bouncycastle.crypto.modes.PGPCFBBlockCipher
All Implemented Interfaces:
BlockCipher

public class PGPCFBBlockCipher
extends java.lang.Object
implements BlockCipher

Implements OpenPGP's rather strange version of Cipher-FeedBack (CFB) mode on top of a simple cipher. For further info see RFC 2440.


Field Summary
private  int blockSize
           
private  BlockCipher cipher
           
private  int count
           
private  boolean forEncryption
           
private  byte[] FR
           
private  byte[] FRE
           
private  boolean inlineIv
           
private  byte[] IV
           
private  byte[] tmp
           
 
Constructor Summary
PGPCFBBlockCipher(BlockCipher cipher, boolean inlineIv)
          Basic constructor.
 
Method Summary
private  int decryptBlock(byte[] in, int inOff, byte[] out, int outOff)
          Do the appropriate processing for CFB mode decryption.
private  int decryptBlockWithIV(byte[] in, int inOff, byte[] out, int outOff)
          Do the appropriate processing for CFB IV mode decryption.
private  int encryptBlock(byte[] in, int inOff, byte[] out, int outOff)
          Do the appropriate processing for CFB mode encryption.
private  int encryptBlockWithIV(byte[] in, int inOff, byte[] out, int outOff)
          Do the appropriate processing for CFB IV mode encryption.
private  byte encryptByte(byte data, int blockOff)
          Encrypt one byte of data according to CFB mode.
 java.lang.String getAlgorithmName()
          return the algorithm name and mode.
 int getBlockSize()
          return the block size we are operating at.
 BlockCipher getUnderlyingCipher()
          return the underlying block cipher that we are wrapping.
 void init(boolean forEncryption, CipherParameters params)
          Initialise the cipher and, possibly, the initialisation vector (IV).
 int processBlock(byte[] in, int inOff, byte[] out, int outOff)
          Process one block of input from the array in and write it to the out array.
 void reset()
          reset the chaining vector back to the IV and reset the underlying cipher.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IV

private byte[] IV

FR

private byte[] FR

FRE

private byte[] FRE

tmp

private byte[] tmp

cipher

private BlockCipher cipher

count

private int count

blockSize

private int blockSize

forEncryption

private boolean forEncryption

inlineIv

private boolean inlineIv
Constructor Detail

PGPCFBBlockCipher

public PGPCFBBlockCipher(BlockCipher cipher,
                         boolean inlineIv)
Basic constructor.

Parameters:
cipher - the block cipher to be used as the basis of the feedback mode.
inlineIv - if true this is for PGP CFB with a prepended iv.
Method Detail

getUnderlyingCipher

public BlockCipher getUnderlyingCipher()
return the underlying block cipher that we are wrapping.

Returns:
the underlying block cipher that we are wrapping.

getAlgorithmName

public java.lang.String getAlgorithmName()
return the algorithm name and mode.

Specified by:
getAlgorithmName in interface BlockCipher
Returns:
the name of the underlying algorithm followed by "/PGPCFB" and the block size in bits.

getBlockSize

public int getBlockSize()
return the block size we are operating at.

Specified by:
getBlockSize in interface BlockCipher
Returns:
the block size we are operating at (in bytes).

processBlock

public int processBlock(byte[] in,
                        int inOff,
                        byte[] out,
                        int outOff)
                 throws DataLengthException,
                        java.lang.IllegalStateException
Process one block of input from the array in and write it to the out array.

Specified by:
processBlock in interface BlockCipher
Parameters:
in - the array containing the input data.
inOff - offset into the in array the data starts at.
out - the array the output data will be copied into.
outOff - the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
DataLengthException - if there isn't enough data in in, or space in out.
java.lang.IllegalStateException - if the cipher isn't initialised.

reset

public void reset()
reset the chaining vector back to the IV and reset the underlying cipher.

Specified by:
reset in interface BlockCipher

init

public void init(boolean forEncryption,
                 CipherParameters params)
          throws java.lang.IllegalArgumentException
Initialise the cipher and, possibly, the initialisation vector (IV). If an IV isn't passed as part of the parameter, the IV will be all zeros. An IV which is too short is handled in FIPS compliant fashion.

Specified by:
init in interface BlockCipher
Parameters:
forEncryption - if true the cipher is initialised for encryption, if false for decryption.
params - the key and other data required by the cipher.
Throws:
java.lang.IllegalArgumentException - if the params argument is inappropriate.

encryptByte

private byte encryptByte(byte data,
                         int blockOff)
Encrypt one byte of data according to CFB mode.

Parameters:
data - the byte to encrypt
where - am i in the current block, determines when to resync the block

encryptBlockWithIV

private int encryptBlockWithIV(byte[] in,
                               int inOff,
                               byte[] out,
                               int outOff)
                        throws DataLengthException,
                               java.lang.IllegalStateException
Do the appropriate processing for CFB IV mode encryption.

Parameters:
in - the array containing the data to be encrypted.
inOff - offset into the in array the data starts at.
out - the array the encrypted data will be copied into.
outOff - the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
DataLengthException - if there isn't enough data in in, or space in out.
java.lang.IllegalStateException - if the cipher isn't initialised.

decryptBlockWithIV

private int decryptBlockWithIV(byte[] in,
                               int inOff,
                               byte[] out,
                               int outOff)
                        throws DataLengthException,
                               java.lang.IllegalStateException
Do the appropriate processing for CFB IV mode decryption.

Parameters:
in - the array containing the data to be decrypted.
inOff - offset into the in array the data starts at.
out - the array the encrypted data will be copied into.
outOff - the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
DataLengthException - if there isn't enough data in in, or space in out.
java.lang.IllegalStateException - if the cipher isn't initialised.

encryptBlock

private int encryptBlock(byte[] in,
                         int inOff,
                         byte[] out,
                         int outOff)
                  throws DataLengthException,
                         java.lang.IllegalStateException
Do the appropriate processing for CFB mode encryption.

Parameters:
in - the array containing the data to be encrypted.
inOff - offset into the in array the data starts at.
out - the array the encrypted data will be copied into.
outOff - the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
DataLengthException - if there isn't enough data in in, or space in out.
java.lang.IllegalStateException - if the cipher isn't initialised.

decryptBlock

private int decryptBlock(byte[] in,
                         int inOff,
                         byte[] out,
                         int outOff)
                  throws DataLengthException,
                         java.lang.IllegalStateException
Do the appropriate processing for CFB mode decryption.

Parameters:
in - the array containing the data to be decrypted.
inOff - offset into the in array the data starts at.
out - the array the encrypted data will be copied into.
outOff - the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
DataLengthException - if there isn't enough data in in, or space in out.
java.lang.IllegalStateException - if the cipher isn't initialised.