cryptix.provider.mode

Class CBC

Implemented Interfaces:
FeedbackCipher, Parameterized, SymmetricCipher

public class CBC
extends FeedbackMode

Implements a block cipher in CBC mode. The block size is the same as that of the underlying cipher.

References:

  1. Bruce Schneier, "Section 9.3 Cipher Block Chaining 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 © 1997, 1998 Systemics Ltd on behalf of the Cryptix Development Team.
All rights reserved.

$Revision: 1.7 $

Authors:
David Hopwood
Raif S. Naffah
Since:
Cryptix 2.2.2

Field Summary

protected byte[]
xorBlock
Temporary buffer to hold input bytes when there's not enough of them to cipher.

Fields inherited from class cryptix.provider.mode.FeedbackMode

currentByte, ivBlock, ivStart, length

Fields inherited from class xjava.security.Mode

cipher

Fields inherited from class xjava.security.Cipher

DECRYPT, ENCRYPT, UNINITIALIZED

Fields inherited from class xjava.security.IJCE_Traceable

tracing

Constructor Summary

CBC()
Constructs a CBC cipher, assuming that the IV will be provided via setInitializationVector.
CBC(Cipher cipher)
Constructs a CBC cipher, assuming that the IV will be provided via setInitializationVector.
CBC(Cipher cipher, byte[] iv)
Constructs a CBC 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(Key newkey)
SPI: Initializes this cipher for decryption, using the specified key.
protected void
engineInitEncrypt(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.

Methods inherited from class cryptix.provider.mode.FeedbackMode

engineSetCipher, getInitializationVector, getInitializationVectorLength, setInitializationVector

Methods inherited from class xjava.security.Mode

engineGetParameter, engineSetCipher, engineSetParameter, getAlgorithms, getAlgorithms, getInstance, getInstance, toString

Methods inherited from class xjava.security.Cipher

blockSize, clone, crypt, crypt, crypt, doFinal, doFinal, doFinal, doFinal, engineBlockSize, engineCiphertextBlockSize, engineCrypt, engineGetParameter, engineInBufferSize, engineInitDecrypt, engineInitEncrypt, engineOutBufferSize, enginePlaintextBlockSize, engineSetPaddingScheme, engineSetParameter, engineUpdate, getAlgorithm, getAlgorithms, getAlgorithms, getCiphertextBlockSize, getInputBlockSize, getInstance, getInstance, getInstance, getMode, getOutputBlockSize, getPadding, getPaddingScheme, getParameter, getPlaintextBlockSize, getProvider, getState, inBufferSize, inBufferSizeFinal, initDecrypt, initEncrypt, isPaddingBlockCipher, outBufferSize, outBufferSizeFinal, setParameter, toString, update, update, update, update

Methods inherited from class xjava.security.IJCE_Traceable

disableTracing, enableTracing, traceMethod, traceResult, traceResult, traceVoidMethod

Field Details

xorBlock

protected byte[] xorBlock
Temporary buffer to hold input bytes when there's not enough of them to cipher.

Constructor Details

CBC

public CBC()

CBC

public CBC(Cipher cipher)
Constructs a CBC 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 CBC mode.

CBC

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

For CBC mode the IV value should be random, but unlike CFB mode, it is not absolutely required to be unique.

Parameters:
cipher - the block cipher to use
iv - the initial value for the shift register (IV)

Method Details

engineBlockSize

protected int engineBlockSize()
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 interface Cipher
Returns:
the length in bytes of a block for this cipher.

engineInitDecrypt

protected void engineInitDecrypt(Key newkey)
            throws KeyException
SPI: Initializes this cipher for decryption, using the specified key.

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

Overrides:
engineInitDecrypt in interface Cipher
Parameters:

engineInitEncrypt

protected void engineInitEncrypt(Key newkey)
            throws KeyException
SPI: Initializes this cipher for encryption, using the specified key.

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

Overrides:
engineInitEncrypt in interface Cipher
Parameters:

engineSetCipher

protected void engineSetCipher(Cipher cipher)
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 interface FeedbackMode
Parameters:
cipher - the underlying cipher object

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.

Overrides:
engineUpdate in interface 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.