xjava.security
Class PaddingScheme

java.lang.Object
  |
  +--xjava.security.IJCE_Traceable
        |
        +--xjava.security.PaddingScheme
All Implemented Interfaces:
Padding, Parameterized
Direct Known Subclasses:
OneAndZeroes, PKCS5, PKCS7

public abstract class PaddingScheme
extends IJCE_Traceable
implements Parameterized, Padding

This class is extended by classes that provide a general-purpose padding scheme, such as the ones described in PKCS #5 or in RFC 1423 (PEM).

For simplicity, an assumption is made that padding schemes always extend the plaintext to the next block boundary. That is, the input to the padding algorithm always has a length between 0 and blockSize-1, and the output always has length blockSize.

This class is not supported in JavaSoft's version of JCE.

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

$Revision: 1.8 $

Since:
IJCE 1.0.1
Author:
David Hopwood, Raif S. Naffah

Field Summary
protected  int blockSize
           
 
Fields inherited from class xjava.security.IJCE_Traceable
tracing
 
Constructor Summary
protected PaddingScheme(java.lang.String algorithm)
          Constructor for a PaddingScheme.
 
Method Summary
 java.lang.Object clone()
          Returns a clone of this cipher.
protected  java.lang.Object engineGetParameter(java.lang.String param)
          SPI: Gets the value of the specified algorithm parameter.
protected  boolean engineIsValidBlockSize(int size)
          SPI: Returns true if size is a valid block size (in bytes) for this scheme.
protected abstract  int enginePad(byte[] in, int offset, int length)
          SPI: Pads a single incomplete block.
protected  void engineSetBlockSize(int size)
          Sets the blockSize variable for this instance.
protected  void engineSetParameter(java.lang.String param, java.lang.Object value)
          SPI: Sets the specified algorithm parameter to the specified value.
protected abstract  int engineUnpad(byte[] in, int offset, int length)
          SPI: Given the specified subarray of bytes that includes padding bytes, returns the index indicating where padding starts.
 java.lang.String getAlgorithm()
          Returns the standard name of the padding scheme implemented.
static java.lang.String[] getAlgorithms()
          Gets the standard names of all PaddingSchemes implemented by any installed provider.
static java.lang.String[] getAlgorithms(java.security.Provider provider)
          Gets the standard names of all PaddingSchemes implemented by a provider.
 int getBlockSize()
          Returns the block size for this padding scheme.
static PaddingScheme getInstance(java.lang.String algorithm)
          Generates a PaddingScheme object that implements the algorithm requested, as available in the environment.
static PaddingScheme getInstance(java.lang.String algorithm, java.lang.String provider)
          Generates a PaddingScheme object implementing the specified algorithm, as supplied from the specified provider, if such an algorithm is available from the provider.
 java.lang.Object getParameter(java.lang.String param)
          Gets the value of the specified algorithm parameter.
 int pad(byte[] in, int offset, int length)
          Pads a given array of bytes.
 java.lang.String paddingScheme()
          Deprecated. Use getAlgorithm() instead.
 int padLength(int length)
          Returns the increase in size that a padding operation would cause on input data of a given length.
 void setParameter(java.lang.String param, java.lang.Object value)
          Sets the specified algorithm parameter to the specified value.
 java.lang.String toString()
           
 int unpad(byte[] in, int offset, int length)
          Given the specified subarray of bytes that includes padding bytes, returns the index indicating where padding starts.
 
Methods inherited from class xjava.security.IJCE_Traceable
, disableTracing, enableTracing, traceMethod, traceResult, traceResult, traceVoidMethod
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

blockSize

protected int blockSize
Constructor Detail

PaddingScheme

protected PaddingScheme(java.lang.String algorithm)
Constructor for a PaddingScheme. This constructor is only for use by subclasses; applications cannot call it directly.

See International JCE Standard Algorithm Names for a list of PaddingScheme algorithm names.

Parameters:
algorithm - the standard string name of the algorithm.
Throws:
NullPointerException - if algorithm == null
Method Detail

getInstance

public static PaddingScheme getInstance(java.lang.String algorithm)
                                 throws java.security.NoSuchAlgorithmException
Generates a PaddingScheme object that implements the algorithm requested, as available in the environment.

See International JCE Standard Algorithm Names for a list of PaddingScheme algorithm names.

Parameters:
algorithm - the standard name or an alias for the algorithm.
Returns:
the new PaddingScheme object.
Throws:
java.security.NoSuchAlgorithmException - if the algorithm is not available in the environment.

getInstance

public static PaddingScheme getInstance(java.lang.String algorithm,
                                        java.lang.String provider)
                                 throws java.security.NoSuchAlgorithmException,
                                        java.security.NoSuchProviderException
Generates a PaddingScheme object implementing the specified algorithm, as supplied from the specified provider, if such an algorithm is available from the provider.

See International JCE Standard Algorithm Names for a list of PaddingScheme algorithm names.

Parameters:
algorithm - the standard name or an alias for the algorithm.
provider - the string name of the provider.
Returns:
the new KeyGenerator object.
Throws:
java.security.NoSuchAlgorithmException - if the algorithm is not available from the provider.
java.security.NoSuchProviderException - if the provider is not available in the environment.

getAlgorithms

public static java.lang.String[] getAlgorithms(java.security.Provider provider)
Gets the standard names of all PaddingSchemes implemented by a provider.

getAlgorithms

public static java.lang.String[] getAlgorithms()
Gets the standard names of all PaddingSchemes implemented by any installed provider. Algorithm names are not duplicated if they are supported by more than one provider. The built-in PaddingScheme "NONE" is included.

getAlgorithm

public final java.lang.String getAlgorithm()
Returns the standard name of the padding scheme implemented.

See International JCE Standard Algorithm Names for a list of PaddingScheme algorithm names.

Returns:
the standard name of the padding scheme, such as "PKCS#5".

getBlockSize

public final int getBlockSize()
Returns the block size for this padding scheme.

pad

public final int pad(byte[] in,
                     int offset,
                     int length)
Pads a given array of bytes. The padding is written to the same buffer that is used for input (in). When this method returns, the padded output will be stored at in[offset .. offset+length+padLength(length)-1].

The in array should be long enough to accomodate the padding.

The return value is the number of bytes written, not the total length of the padded block.

Specified by:
pad in interface Padding
Parameters:
in - the buffer containing the input.
offset - the offset of the bytes to be padded.
length - the number of bytes from the in buffer, starting at offset, that need to be padded.
Returns:
the number of padding bytes written to out.
Throws:
ArrayIndexOutOfBoundsException - if offset < 0 || length < 0 || (long)offset + length + padLength(length) > in.length

padLength

public final int padLength(int length)
Returns the increase in size that a padding operation would cause on input data of a given length. This is always blockSize - (length % blockSize).
Specified by:
padLength in interface Padding
Parameters:
length - the length of the data to be padded.
Returns:
the increase in size that a padding operation would cause on input of the specified length.

unpad

public final int unpad(byte[] in,
                       int offset,
                       int length)
Given the specified subarray of bytes that includes padding bytes, returns the index indicating where padding starts.

length must be at least blockSize.

Specified by:
unpad in interface Padding
Parameters:
in - the buffer containing the bytes.
offset - the offset into the in buffer of the first byte in the block.
length - the total length in bytes of the blocks to be unpadded.
Returns:
the index into the in buffer indicating where the padding starts.
Throws:
ArrayIndexOutOfBoundsException - if offset < 0 || length < 0 || (long)offset + length > in.length
IllegalBlockSizeException - if length < blockSize

paddingScheme

public final java.lang.String paddingScheme()
Deprecated. Use getAlgorithm() instead.

Returns the standard name of the padding scheme implemented.

See International JCE Standard Algorithm Names for a list of PaddingScheme algorithm names.

Specified by:
paddingScheme in interface Padding
Returns:
the standard name of the padding scheme.

setParameter

public void setParameter(java.lang.String param,
                         java.lang.Object value)
                  throws NoSuchParameterException,
                         java.security.InvalidParameterException,
                         InvalidParameterTypeException
Sets the specified algorithm parameter to the specified value.

This method supplies a general-purpose mechanism through which it is possible to set the various parameters of this object. A uniform algorithm-specific naming scheme for each parameter is desirable but left unspecified at this time.

This method is not supported in JavaSoft's version of JCE.

Specified by:
setParameter in interface Parameterized
Parameters:
param - the string identifier of the parameter.
value - the parameter value.
Throws:
NullPointerException - if param == null
NoSuchParameterException - if there is no parameter with name param for this padding scheme implementation.
java.security.InvalidParameterException - if the parameter exists but cannot be set.
InvalidParameterTypeException - if value is the wrong type for this parameter.

getParameter

public java.lang.Object getParameter(java.lang.String param)
                              throws NoSuchParameterException,
                                     java.security.InvalidParameterException
Gets the value of the specified algorithm parameter.

This method supplies a general-purpose mechanism through which it is possible to get the various parameters of this object. A uniform algorithm-specific naming scheme for each parameter is desirable but left unspecified at this time.

This method is not supported in JavaSoft's version of JCE.

Specified by:
getParameter in interface Parameterized
Parameters:
param - the string name of the parameter.
Returns:
the object that represents the parameter value.
Throws:
NullPointerException - if param == null
NoSuchParameterException - if there is no parameter with name param for this padding scheme implementation.
java.security.InvalidParameterException - if the parameter exists but cannot be read.

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Returns a clone of this cipher.
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException - if the cipher is not cloneable.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

engineSetBlockSize

protected void engineSetBlockSize(int size)
Sets the blockSize variable for this instance.

Subclasses that override this method (to do initialization that depends on blockSize being set) should call super.engineSetBlockSize(size) first.

Throws:
IllegalBlockSizeException - if size < 1 || !engineIsValidBlockSize(size)

enginePad

protected abstract int enginePad(byte[] in,
                                 int offset,
                                 int length)
SPI: Pads a single incomplete block.

The padding is written to the same buffer that is used for input (in). When this method returns, the padded block should be stored at in[offset .. offset+blockSize-1].

in will be long enough to accomodate the padding. length is guaranteed to be in the range 0 .. blockSize-1.

Parameters:
in - the buffer containing the incomplete block.
offset - the offset into the in buffer of the block.
length - the number of bytes from the in buffer, starting at offset, that need to be unpadded.
Returns:
the number of padding bytes written.

engineUnpad

protected abstract int engineUnpad(byte[] in,
                                   int offset,
                                   int length)
SPI: Given the specified subarray of bytes that includes padding bytes, returns the index indicating where padding starts.

length is guaranteed to be a non-negative multiple of blockSize.

Parameters:
in - the buffer containing the bytes.
offset - the offset into the in buffer of the first byte to be unpadded.
length - the total length in bytes of the blocks to be unpadded.
Returns:
the index into the in buffer indicating where the padding starts.

engineIsValidBlockSize

protected boolean engineIsValidBlockSize(int size)
SPI: Returns true if size is a valid block size (in bytes) for this scheme.

The default implementation always returns true.


engineSetParameter

protected void engineSetParameter(java.lang.String param,
                                  java.lang.Object value)
                           throws NoSuchParameterException,
                                  java.security.InvalidParameterException,
                                  InvalidParameterTypeException
SPI: Sets the specified algorithm parameter to the specified value.

This method supplies a general-purpose mechanism through which it is possible to set the various parameters of this object. A uniform algorithm-specific naming scheme for each parameter is desirable but left unspecified at this time.

The default implementation always throws a NoSuchParameterException.

This method is not supported in JavaSoft's version of JCE.

Parameters:
param - the string name of the parameter.
value - the parameter value.
Throws:
NoSuchParameterException - if there is no parameter with name param for this padding scheme implementation.
java.security.InvalidParameterException - if the parameter exists but cannot be set.
InvalidParameterTypeException - if value is the wrong type for this parameter.

engineGetParameter

protected java.lang.Object engineGetParameter(java.lang.String param)
                                       throws NoSuchParameterException,
                                              java.security.InvalidParameterException
SPI: Gets the value of the specified algorithm parameter.

This method supplies a general-purpose mechanism through which it is possible to get the various parameters of this object. A uniform algorithm-specific naming scheme for each parameter is desirable but left unspecified at this time.

The default implementation always throws a NoSuchParameterException.

This method is not supported in JavaSoft's version of JCE.

Parameters:
param - the string name of the parameter.
Returns:
the object that represents the parameter value.
Throws:
NoSuchParameterException - if there is no parameter with name param for this padding scheme implementation.
java.security.InvalidParameterException - if the parameter exists but cannot be read.