For the latest news and information visit
The GNU Crypto project

gnu.crypto.cipher
Class BaseCipher

java.lang.Object
  extended bygnu.crypto.cipher.BaseCipher
All Implemented Interfaces:
java.lang.Cloneable, IBlockCipher, IBlockCipherSpi
Direct Known Subclasses:
Anubis, Blowfish, Cast5, DES, Khazad, NullCipher, Rijndael, Serpent, Square, TripleDES, Twofish

public abstract class BaseCipher
extends java.lang.Object
implements IBlockCipher, IBlockCipherSpi

A basic abstract class to facilitate implementing symmetric key block ciphers.

Version:
$Revision: 1.2 $

Field Summary
protected  int currentBlockSize
          The current block size, in bytes.
protected  java.lang.Object currentKey
          The session key for this instance.
protected  int defaultBlockSize
          The default block size, in bytes.
protected  int defaultKeySize
          The default key size, in bytes.
protected  java.lang.Object lock
          The instance lock.
protected  java.lang.String name
          The canonical name prefix of the cipher.
 
Fields inherited from interface gnu.crypto.cipher.IBlockCipher
CIPHER_BLOCK_SIZE, KEY_MATERIAL
 
Constructor Summary
protected BaseCipher(java.lang.String name, int defaultBlockSize, int defaultKeySize)
          Trivial constructor for use by concrete subclasses.
 
Method Summary
 java.util.Iterator blockSizes()
          Returns an Iterator over the supported block sizes.
abstract  java.lang.Object clone()
          Returns a clone of this instance.
 int currentBlockSize()
          Returns the currently set block size for this instance.
 void decrypt(byte[] in, int inOffset, byte[] out, int outOffset, java.lang.Object k, int bs)
          Decrypts exactly one block of ciphertext.
 void decryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
          Decrypts exactly one block of ciphertext.
 int defaultBlockSize()
          Returns the default value, in bytes, of the algorithm's block size.
 int defaultKeySize()
          Returns the default value, in bytes, of the algorithm's key size.
 void encrypt(byte[] in, int inOffset, byte[] out, int outOffset, java.lang.Object k, int bs)
          Encrypts exactly one block of plaintext.
 void encryptBlock(byte[] in, int inOffset, byte[] out, int outOffset)
          Encrypts exactly one block of plaintext.
 void init(java.util.Map attributes)
          Initialises the algorithm with designated attributes.
 java.util.Iterator keySizes()
          Returns an Iterator over the supported key sizes.
 java.lang.Object makeKey(byte[] k, int bs)
          Expands a user-supplied key material into a session key for a designated block size.
 java.lang.String name()
          Returns the canonical name of this instance.
 void reset()
          Resets the algorithm instance for re-initialisation and use with other characteristics.
 boolean selfTest()
          A correctness test that consists of basic symmetric encryption / decryption test(s) for all supported block and key sizes, as well as one (1) variable key Known Answer Test (KAT).
protected  boolean testKat(byte[] kb, byte[] ct)
           
protected  boolean testKat(byte[] kb, byte[] ct, byte[] pt)
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface gnu.crypto.cipher.IBlockCipher
blockSizes, keySizes
 

Field Detail

name

protected java.lang.String name
The canonical name prefix of the cipher.


defaultBlockSize

protected int defaultBlockSize
The default block size, in bytes.


defaultKeySize

protected int defaultKeySize
The default key size, in bytes.


currentBlockSize

protected int currentBlockSize
The current block size, in bytes.


currentKey

protected transient java.lang.Object currentKey
The session key for this instance.


lock

protected java.lang.Object lock
The instance lock.

Constructor Detail

BaseCipher

protected BaseCipher(java.lang.String name,
                     int defaultBlockSize,
                     int defaultKeySize)

Trivial constructor for use by concrete subclasses.

Parameters:
name - the canonical name prefix of this instance.
defaultBlockSize - the default block size in bytes.
defaultKeySize - the default key size in bytes.
Method Detail

clone

public abstract java.lang.Object clone()
Description copied from interface: IBlockCipher

Returns a clone of this instance.

Specified by:
clone in interface IBlockCipher

name

public java.lang.String name()
Description copied from interface: IBlockCipher

Returns the canonical name of this instance.

Specified by:
name in interface IBlockCipher
Returns:
the canonical name of this instance.

defaultBlockSize

public int defaultBlockSize()
Description copied from interface: IBlockCipher

Returns the default value, in bytes, of the algorithm's block size.

Specified by:
defaultBlockSize in interface IBlockCipher
Returns:
the default value, in bytes, of the algorithm's block size.

defaultKeySize

public int defaultKeySize()
Description copied from interface: IBlockCipher

Returns the default value, in bytes, of the algorithm's key size.

Specified by:
defaultKeySize in interface IBlockCipher
Returns:
the default value, in bytes, of the algorithm's key size.

init

public void init(java.util.Map attributes)
          throws java.security.InvalidKeyException
Description copied from interface: IBlockCipher

Initialises the algorithm with designated attributes. Permissible names and values are described in the class documentation above.

Specified by:
init in interface IBlockCipher
Parameters:
attributes - a set of name-value pairs that describes the desired future behaviour of this instance.
Throws:
java.security.InvalidKeyException - if the key data is invalid.
See Also:
IBlockCipher.KEY_MATERIAL, IBlockCipher.CIPHER_BLOCK_SIZE

currentBlockSize

public int currentBlockSize()
Description copied from interface: IBlockCipher

Returns the currently set block size for this instance.

Specified by:
currentBlockSize in interface IBlockCipher
Returns:
the current block size for this instance.

reset

public void reset()
Description copied from interface: IBlockCipher

Resets the algorithm instance for re-initialisation and use with other characteristics. This method always succeeds.

Specified by:
reset in interface IBlockCipher

encryptBlock

public void encryptBlock(byte[] in,
                         int inOffset,
                         byte[] out,
                         int outOffset)
                  throws java.lang.IllegalStateException
Description copied from interface: IBlockCipher

Encrypts exactly one block of plaintext.

Specified by:
encryptBlock in interface IBlockCipher
Parameters:
in - the plaintext.
inOffset - index of in from which to start considering data.
out - the ciphertext.
outOffset - index of out from which to store result.
Throws:
java.lang.IllegalStateException - if the instance is not initialised.

decryptBlock

public void decryptBlock(byte[] in,
                         int inOffset,
                         byte[] out,
                         int outOffset)
                  throws java.lang.IllegalStateException
Description copied from interface: IBlockCipher

Decrypts exactly one block of ciphertext.

Specified by:
decryptBlock in interface IBlockCipher
Parameters:
in - the plaintext.
inOffset - index of in from which to start considering data.
out - the ciphertext.
outOffset - index of out from which to store result.
Throws:
java.lang.IllegalStateException - if the instance is not initialised.

selfTest

public boolean selfTest()
Description copied from interface: IBlockCipher

A correctness test that consists of basic symmetric encryption / decryption test(s) for all supported block and key sizes, as well as one (1) variable key Known Answer Test (KAT).

Specified by:
selfTest in interface IBlockCipher
Returns:
true if the implementation passes simple correctness tests. Returns false otherwise.

testKat

protected boolean testKat(byte[] kb,
                          byte[] ct)

testKat

protected boolean testKat(byte[] kb,
                          byte[] ct,
                          byte[] pt)

blockSizes

public java.util.Iterator blockSizes()

Returns an Iterator over the supported block sizes. Each element returned by this object is a Integer.

Returns:
an Iterator over the supported block sizes.

keySizes

public java.util.Iterator keySizes()

Returns an Iterator over the supported key sizes. Each element returned by this object is a Integer.

Returns:
an Iterator over the supported key sizes.

makeKey

public java.lang.Object makeKey(byte[] k,
                                int bs)
                         throws java.security.InvalidKeyException

Expands a user-supplied key material into a session key for a designated block size.

Parameters:
k - the user-supplied key material.
bs - the desired block size in bytes.
Returns:
an Object encapsulating the session key.
Throws:
java.lang.IllegalArgumentException - if the block size is invalid.
java.security.InvalidKeyException - if the key data is invalid.

encrypt

public void encrypt(byte[] in,
                    int inOffset,
                    byte[] out,
                    int outOffset,
                    java.lang.Object k,
                    int bs)

Encrypts exactly one block of plaintext.

Parameters:
in - the plaintext.
inOffset - index of in from which to start considering data.
out - the ciphertext.
outOffset - index of out from which to store the result.
k - the session key to use.
bs - the block size to use.
Throws:
java.lang.IllegalArgumentException - if the block size is invalid.
java.lang.ArrayIndexOutOfBoundsException - if there is not enough room in either the plaintext or ciphertext buffers.

decrypt

public void decrypt(byte[] in,
                    int inOffset,
                    byte[] out,
                    int outOffset,
                    java.lang.Object k,
                    int bs)

Decrypts exactly one block of ciphertext.

Parameters:
in - the ciphertext.
inOffset - index of in from which to start considering data.
out - the plaintext.
outOffset - index of out from which to store the result.
k - the session key to use.
bs - the block size to use.
Throws:
java.lang.IllegalArgumentException - if the block size is invalid.
java.lang.ArrayIndexOutOfBoundsException - if there is not enough room in either the plaintext or ciphertext buffers.

For the latest news and information visit
The GNU Crypto project

Copyright © 2001, 2002, 2003 Free Software Foundation, Inc. All Rights Reserved.