ccs.crypt
Class BCCHBlockCipher

java.lang.Object
  extended by ccs.crypt.Cipher
      extended by ccs.crypt.AbstractBlockCipher
          extended by ccs.crypt.BCCHBlockCipher
Direct Known Subclasses:
BCCHBlockCipher.AES, BCCHBlockCipher.Blowfish

public class BCCHBlockCipher
extends AbstractBlockCipher

A block cipher which uses BouncyCastle for the cryptographic primitives, in CBC mode with HMAC authentication.


Nested Class Summary
static class BCCHBlockCipher.AES
           
static class BCCHBlockCipher.Blowfish
           
 
Field Summary
 
Fields inherited from class ccs.crypt.AbstractBlockCipher
buffer
 
Fields inherited from class ccs.crypt.Cipher
passwd
 
Constructor Summary
BCCHBlockCipher(java.lang.Class enginec, java.lang.Class digestc)
          A block cipher using the specified cryptographic primitives.
 
Method Summary
protected  void bufferDecrypt(int blkleft)
          Decrypt the number of blocks specified from the internal buffer, updating the MAC with the result.
protected  void bufferEncrypt(int blkleft)
          Encrypt the number of blocks specified from the internal buffer, updating the MAC with the result.
 int getBlockSizeExp()
          The logarithm to base 2 of the block size of the cipher in bytes.
protected  int getHeaderLength()
          The size of the header, without any self-delimitation info.
 CipherInputStream getInputStream(java.io.InputStream is, boolean isSelfDelimit)
          Returns a CipherInputStream that reads ciphertext from the underlying stream and provides plaintext.
protected  byte[] getMAC()
          Obtain the MAC for the stream.
protected  int getMACLength()
          The size of the MAC.
 CipherOutputStream getOutputStream(java.io.OutputStream os, long plainlen)
          Returns a CipherOutputStream that accepts plaintext and writes ciphertext to the underlying stream.
protected  void getWhitening(byte[] buf)
          Produce and return 8 bytes (exactly) of "whitening".
 void setPasswd(byte[] pw)
          Sets the password, and should be called at the start of the encryption procedure.
 
Methods inherited from class ccs.crypt.AbstractBlockCipher
checkRegistered, getCiphertextLength, getPlaintextLength, getTrailerLength, register, unregister
 
Methods inherited from class ccs.crypt.Cipher
getPasswd, out
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BCCHBlockCipher

public BCCHBlockCipher(java.lang.Class enginec,
                       java.lang.Class digestc)
A block cipher using the specified cryptographic primitives.

Parameters:
enginec - The class of the encryption primitive engine to use. Must implement BlockCipher.
digestc - The class of the crytographic hash function to use. Must implement Digest.
Method Detail

setPasswd

public void setPasswd(byte[] pw)
               throws CipherException
Sets the password, and should be called at the start of the encryption procedure.

Overrides:
setPasswd in class Cipher
Parameters:
pw - the new password. Must be at least 8 bytes. If > 56 bytes, it is hashed down to 40 bytes; otherwise it is not pre-processed and can be used for verification using test vectors. (Password length > 56 bytes suggests you're trying to distil a silk purse out of several sows' ears. :-)
Throws:
CipherException - if the pass-string is too short.

getOutputStream

public CipherOutputStream getOutputStream(java.io.OutputStream os,
                                          long plainlen)
                                   throws java.io.IOException
Returns a CipherOutputStream that accepts plaintext and writes ciphertext to the underlying stream.

Specified by:
getOutputStream in class Cipher
Parameters:
os - The stream to write ciphertext to.
plainlen - The length of plaintext to be written. If known, this allows the length to be encrypted as part of the stream itself, and makes the ciphertext self-delimiting; where many ciphertexts are concatenated onto a stream, no boundary sequences are required in this mode, which is preferable. (Such sequences leak information.) Supply -1 if you really don't know; in this case, you must delimit the ciphertext yourself. Note that the limit is applied to the ciphertext, not the plaintext; the cipher may allow you to append a few bytes of garbage without complaining (For a block cipher with an incomplete last block, this does not change the ciphertext length).
Throws:
CipherException - if a cryptographic problem occurs.
java.io.IOException - if the CipherOutputStream could not be created for some reason.

getInputStream

public CipherInputStream getInputStream(java.io.InputStream is,
                                        boolean isSelfDelimit)
                                 throws java.io.IOException
Returns a CipherInputStream that reads ciphertext from the underlying stream and provides plaintext.

Specified by:
getInputStream in class Cipher
Parameters:
is - The stream to read ciphertext from.
isSelfDelimit - Whether the ciphertext is self-delimiting, ie. it has its own length stored inside itself. If not, the ciphertext is assumed to extend to EOF.
Throws:
CipherException - if a cryptographic problem occurs.
java.io.IOException - if the CipherInputStream could not be created for some reason.

bufferEncrypt

protected void bufferEncrypt(int blkleft)
Description copied from class: AbstractBlockCipher
Encrypt the number of blocks specified from the internal buffer, updating the MAC with the result.

Specified by:
bufferEncrypt in class AbstractBlockCipher

bufferDecrypt

protected void bufferDecrypt(int blkleft)
Description copied from class: AbstractBlockCipher
Decrypt the number of blocks specified from the internal buffer, updating the MAC with the result.

Specified by:
bufferDecrypt in class AbstractBlockCipher

getMAC

protected byte[] getMAC()
Description copied from class: AbstractBlockCipher
Obtain the MAC for the stream. This will only be called once for a given message. MAC the ciphtertext, not the plaintext, and do include the header as well as all ciphertext blocks. For self-delimited streams, you don't need to MAC the stream length (which is good, 'cos there's no easy way to find out what it is).

Specified by:
getMAC in class AbstractBlockCipher

getWhitening

protected void getWhitening(byte[] buf)
Description copied from class: AbstractBlockCipher
Produce and return 8 bytes (exactly) of "whitening". This should be random-looking but reproducible material which does not depend directly on the key, and can be calculated from the ciphertext header alone. (For CBC-mode ciphers, this header is typically just the IV, and the whitening is typically based on a hash of that IV). This whitening is used in self-delimiting cpihertext streams to obscure the stream length data; actually encrypting it causes too many problems in the event that a user innocently gets the wrong password. Whitening is requested immediately after the header is written (encrypt) or read(decrypt).

Specified by:
getWhitening in class AbstractBlockCipher
Parameters:
buf - The buffer into the first 8 bytes of which the whitening should be placed.

getBlockSizeExp

public int getBlockSizeExp()
Description copied from class: AbstractBlockCipher
The logarithm to base 2 of the block size of the cipher in bytes. Eg. a 64-bit block cipher should return 3.

Specified by:
getBlockSizeExp in class AbstractBlockCipher

getHeaderLength

protected int getHeaderLength()
Description copied from class: AbstractBlockCipher
The size of the header, without any self-delimitation info. Typically this header is just the IV (in CBC mode) and is easy to calculate the length of.

Specified by:
getHeaderLength in class AbstractBlockCipher

getMACLength

protected int getMACLength()
Description copied from class: AbstractBlockCipher
The size of the MAC. Typically this is a constant.

Specified by:
getMACLength in class AbstractBlockCipher