cryptix.provider.mode
Class CFB

java.lang.Object
  extended byxjava.security.IJCE_Traceable
      extended byxjava.security.Cipher
          extended byxjava.security.Mode
              extended bycryptix.provider.mode.FeedbackMode
                  extended bycryptix.provider.mode.CFB
All Implemented Interfaces:
FeedbackCipher, Parameterized, SymmetricCipher
Direct Known Subclasses:
CFB_PGP

public class CFB
extends FeedbackMode

Implements a byte-oriented stream cipher using n-bit CFB with an n-bit-sized block cipher.

The full block size of the supplied cipher is used for the Cipher Feedback Mode. The bytes supplied are processed and returned immediately.

References:

  1. Bruce Schneier, "Section 9.6 Cipher Feedback Mode," and "Section 9.11 Choosing a Cipher Mode," Applied Cryptography, 2nd edition, John Wiley & Sons, 1996

  2. sci.crypt FAQ, "Part 5: Product Ciphers," ftp://ftp.rtfm.mit.edu/pub/usenet/news.answers/cryptography-faq/part05

  3. National Bureau of Standards (now NIST), "DES Modes of Operation," NBS FIPS PUB 81, U.S. Department of Commerce, December 1980

Copyright © 1995-1997 Systemics Ltd on behalf of the Cryptix Development Team.
All rights reserved.

$Revision: 1.5 $

Since:
Cryptix 2.2.2
Author:
David Hopwood, Raif S. Naffah

Field Summary
protected  int currentByte
          Index into the ivBlock.
protected  byte[] ivBlock
          Value of the current feedback register/queue/buffer.
protected  byte[] ivStart
          Value of the IV at initialisation phase as supplied by user.
protected  int length
          Size of the feedback register/queue/buffer.
 
Fields inherited from class xjava.security.Mode
cipher
 
Fields inherited from class xjava.security.Cipher
DECRYPT, ENCRYPT, UNINITIALIZED
 
Constructor Summary
CFB()
          Constructs a CFB mode object.
CFB(Cipher cipher)
          Constructs a CFB cipher, assuming that the IV will be provided via setInitializationVector.
CFB(Cipher cipher, byte[] iv)
          Constructs a CFB cipher, using an initialization vector provided in the constructor.
 
Method Summary
protected  int engineBlockSize()
          SPI: Returns the length of a block, in bytes.
protected  void engineInitDecrypt(java.security.Key newkey)
          SPI: Initializes this cipher for decryption, using the specified key.
protected  void engineInitEncrypt(java.security.Key newkey)
          SPI: Initializes this cipher for encryption, using the specified key.
protected  void engineSetCipher(Cipher cipher)
          SPI: Sets the underlying cipher.
protected  int engineUpdate(byte[] in, int inOffset, int inLen, byte[] out, int outOffset)
          SPI: This is the main engine method for updating data.
 byte[] getInitializationVector()
          Gets a copy of the starting initialization vector.
 int getInitializationVectorLength()
          Returns the size of the initialization vector expected by setInitializationVector.
protected  void next_block()
          Rotates the IV left by currentByte bytes, to mimic the V2.2 behaviour.
 void setInitializationVector(byte[] iv)
          Sets the initialization vector.
 
Methods inherited from class xjava.security.Mode
engineGetParameter, engineSetParameter, getAlgorithms, getAlgorithms, getInstance, getInstance, toString
 
Methods inherited from class xjava.security.Cipher
blockSize, clone, crypt, crypt, crypt, doFinal, doFinal, doFinal, doFinal, engineCiphertextBlockSize, engineCrypt, engineInBufferSize, engineOutBufferSize, enginePlaintextBlockSize, engineSetPaddingScheme, getAlgorithm, getCiphertextBlockSize, getInputBlockSize, getInstance, getMode, getOutputBlockSize, getPadding, getPaddingScheme, getParameter, getPlaintextBlockSize, getProvider, getState, inBufferSize, inBufferSizeFinal, initDecrypt, initEncrypt, isPaddingBlockCipher, outBufferSize, outBufferSizeFinal, setParameter, update, update, update, update
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ivBlock

protected byte[] ivBlock
Value of the current feedback register/queue/buffer.


ivStart

protected byte[] ivStart
Value of the IV at initialisation phase as supplied by user.


currentByte

protected int currentByte
Index into the ivBlock.


length

protected int length
Size of the feedback register/queue/buffer.

Constructor Detail

CFB

public CFB()
Constructs a CFB mode object.

The IV is provided via setInitializationVector. This IV must be unique during the lifetime of the key. If it is not unique, at least the first block of the plaintext can be recovered.


CFB

public CFB(Cipher cipher)
Constructs a CFB cipher, assuming that the IV will be provided via setInitializationVector. See the previous constructor for more details.

Parameters:
cipher - the cipher object to use in CFB mode.
Throws:
java.lang.NullPointerException - if cipher == null

CFB

public CFB(Cipher cipher,
           byte[] iv)
Constructs a CFB cipher, using an initialization vector provided in the constructor.

Parameters:
cipher - the block cipher to use
iv - the initial value for the shift register (IV)
Throws:
java.lang.NullPointerException - if cipher == null
Method Detail

engineSetCipher

protected void engineSetCipher(Cipher cipher)
Description copied from class: FeedbackMode
SPI: Sets the underlying cipher.

For example, to create an IDEA cipher in CBC mode, the cipher for "IDEA" would be passed to the mode for "CBC" using this method. It is called once, immediately after the mode object is constructed.

Subclasses that override this method (to do initialization that depends on the cipher being set) should call super.engineSetCipher(cipher) first.

Overrides:
engineSetCipher in class FeedbackMode
Parameters:
cipher - the underlying cipher object

engineBlockSize

protected int engineBlockSize()
Description copied from class: Cipher
SPI: Returns the length of a block, in bytes. Ciphers for which plaintext and ciphertext blocks are the same size may override this method. Otherwise, both enginePlaintextBlockSize and engineCiphertextBlockSize should be overridden.

The value may change when initEncrypt or initDecrypt is called, but it should not change at other times.

Overrides:
engineBlockSize in class Cipher
Returns:
the length in bytes of a block for this cipher.

engineInitEncrypt

protected void engineInitEncrypt(java.security.Key newkey)
                          throws java.security.KeyException
Description copied from class: Cipher
SPI: Initializes this cipher for encryption, using the specified key.

After a call to this method, the cipher's state is set to ENCRYPT.

Specified by:
engineInitEncrypt in class Cipher
Parameters:
newkey - the key to use for encryption.
Throws:
java.security.KeyException - if the key is invalid.

engineInitDecrypt

protected void engineInitDecrypt(java.security.Key newkey)
                          throws java.security.KeyException
Description copied from class: Cipher
SPI: Initializes this cipher for decryption, using the specified key.

After a call to this method, the cipher's state is set to DECRYPT.

Specified by:
engineInitDecrypt in class Cipher
Parameters:
newkey - the key to use for decryption.
Throws:
java.security.KeyException - if the key is invalid.

engineUpdate

protected int engineUpdate(byte[] in,
                           int inOffset,
                           int inLen,
                           byte[] out,
                           int outOffset)
SPI: This is the main engine method for updating data. It may be called with any size of input.

in and out may be the same array, and the input and output regions may overlap.

Specified by:
engineUpdate in class Cipher
Parameters:
in - the input data.
inOffset - the offset into in specifying where the data starts.
inLen - the length of the subarray.
out - the output array.
outOffset - the offset indicating where to start writing into the out array.
Returns:
the number of bytes written.

next_block

protected void next_block()
Rotates the IV left by currentByte bytes, to mimic the V2.2 behaviour. This is needed in order to implement the variant of CFB used by PGP.

See Also:
CFB_PGP

setInitializationVector

public void setInitializationVector(byte[] iv)
                             throws java.security.InvalidParameterException
Sets the initialization vector.

Note: in JavaSoft's version of JCE, this method may only be called when the cipher is in the UNINITIALIZED state. In IJCE that is relaxed to also allow it to be called after initEncrypt/initDecrypt, but before the first call to update or crypt, provided that the IV is not set twice.

Specified by:
setInitializationVector in interface FeedbackCipher
Parameters:
iv - the initialization vector.
Throws:
java.security.InvalidParameterException - if the initialization vector is of the wrong length or has already been set.

getInitializationVector

public byte[] getInitializationVector()
Gets a copy of the starting initialization vector. It will return null if the initialization vector has not been set.

Specified by:
getInitializationVector in interface FeedbackCipher
Returns:
a copy of the initialization vector for this cipher object.

getInitializationVectorLength

public int getInitializationVectorLength()
Returns the size of the initialization vector expected by setInitializationVector. For this class, that is the block size of the underlying cipher.

Specified by:
getInitializationVectorLength in interface FeedbackCipher
Returns:
the required size of the argument to setInitializationVector.