de.flexiprovider.api
Class AsymmetricHybridCipher

java.lang.Object
  |
  +--javax.crypto.CipherSpi
        |
        +--de.flexiprovider.api.Cipher
              |
              +--de.flexiprovider.api.AsymmetricHybridCipher
Direct Known Subclasses:
IES, McElieceFujisakiCipher, McElieceKobaraImaiCipher, McEliecePointchevalCipher

public abstract class AsymmetricHybridCipher
extends Cipher

The AsymmetricHybridCipher class extends CipherSpi. An instance of this class will be created by the Cipher.getInstance(String)-mechanism of the Cipher class.
NOTE: Some Ciphers are using Padding. OneAndZeroesPadding is used as default padding. However padding can still be specified, but mode is not supported: Example: Cipher.getInstance("SomeCipher/NONE/SomePadding"); If you try to intantiate the cipher with something else than "NONE" as mode, NoSuchAlgorithmException is thrown.

Author:
Thomas Wahrenbruch, Hristo Indzhov

Field Summary
protected  AlgorithmParameterSpec paramSpec
          ParameterSpec used with this cipher
 
Fields inherited from class de.flexiprovider.api.Cipher
DECRYPT_MODE, ENCRYPT_MODE, opMode
 
Constructor Summary
AsymmetricHybridCipher()
           
 
Method Summary
protected abstract  int decryptOutputSize(int inLen)
          Compute the output size of an update() or doFinal() operation of a hybrid asymmetric cipher in decryption mode when given input of the specified length.
abstract  byte[] doFinal(byte[] input, int inOff, int inLen)
          Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
 int doFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff)
          Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
protected abstract  int encryptOutputSize(int inLen)
          Compute the output size of an update() or doFinal() operation of a hybrid asymmetric cipher in encryption mode when given input of the specified length.
 int getBlockSize()
           
 byte[] getIV()
          Return the initialization vector.
 int getOutputSize(int inLen)
          Return the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inLen (in bytes).
 AlgorithmParameterSpec getParameters()
          Return the parameters used with this cipher.
protected abstract  void initCipherDecrypt(Key key, AlgorithmParameterSpec params)
          Initialize the AsymmetricHybridCipher with a certain key for data encryption.
protected abstract  void initCipherEncrypt(Key key, AlgorithmParameterSpec params, SecureRandom sr)
          Initialize the AsymmetricHybridCipher with a certain key for data encryption.
 void initDecrypt(Key key)
          Initialize the cipher for decryption by forwarding it to initDecrypt(Key, FlexiSecureRandom).
 void initDecrypt(Key key, AlgorithmParameterSpec params)
          Initialize the cipher with a certain key for data decryption.
 void initEncrypt(Key key)
          Initialize the cipher for encryption by forwarding it to initEncrypt(Key, AlgorithmParameterSpec, SecureRandom).
 void initEncrypt(Key key, AlgorithmParameterSpec params)
          Initialize the cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).
 void initEncrypt(Key key, AlgorithmParameterSpec params, SecureRandom random)
          Initialize the cipher with a certain key for data encryption.
 void initEncrypt(Key key, SecureRandom random)
          Initialize this cipher for encryption by forwarding it to initEncrypt(Key, AlgorithmParameterSpec, SecureRandom).
protected  void setMode(java.lang.String modeName)
          Since asymmetric hybrid ciphers do not support modes, this method does nothing.
protected  void setPadding(java.lang.String paddingName)
          Since asymmetric hybrid ciphers do not support padding, this method does nothing.
abstract  byte[] update(byte[] input, int inOff, int inLen)
          Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
 int update(byte[] input, int inOff, int inLen, byte[] output, int outOff)
          Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
 
Methods inherited from class de.flexiprovider.api.Cipher
doFinal, doFinal, engineDoFinal, engineDoFinal, engineGetBlockSize, engineGetIV, engineGetKeySize, engineGetOutputSize, engineGetParameters, engineInit, engineInit, engineInit, engineSetMode, engineSetPadding, engineUpdate, engineUpdate, getKeySize, getName, update
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

paramSpec

protected AlgorithmParameterSpec paramSpec
ParameterSpec used with this cipher
Constructor Detail

AsymmetricHybridCipher

public AsymmetricHybridCipher()
Method Detail

setMode

protected final void setMode(java.lang.String modeName)
Since asymmetric hybrid ciphers do not support modes, this method does nothing.
Overrides:
setMode in class Cipher
Parameters:
modeName - the cipher mode (unused)

setPadding

protected final void setPadding(java.lang.String paddingName)
Since asymmetric hybrid ciphers do not support padding, this method does nothing.
Overrides:
setPadding in class Cipher
Parameters:
paddingName - the name of the padding scheme (not used)

getIV

public final byte[] getIV()
Description copied from class: Cipher
Return the initialization vector. This is useful in the context of password-based encryption or decryption, where the IV is derived from a user-provided passphrase.
Overrides:
getIV in class Cipher
Returns:
null since no initialization vector is used.

getBlockSize

public final int getBlockSize()
Overrides:
getBlockSize in class Cipher
Returns:
0 since the implementing algorithms are not block ciphers

getParameters

public final AlgorithmParameterSpec getParameters()
Return the parameters used with this cipher.

The returned parameters may be the same that were used to initialize this cipher, or may contain the default set of parameters or a set of randomly generated parameters used by the underlying cipher implementation (provided that the underlying cipher implementation uses a default set of parameters or creates new parameters if it needs parameters but was not initialized with any).

Overrides:
getParameters in class Cipher
Returns:
the parameters used with this cipher, or null if this cipher does not use any parameters.

getOutputSize

public final int getOutputSize(int inLen)
Return the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inLen (in bytes). This call takes into account any unprocessed (buffered) data from a previous update call, and padding. The actual output length of the next update() or doFinal() call may be smaller than the length returned by this method.
Overrides:
getOutputSize in class Cipher
Parameters:
inLen - the length of the input
Returns:
the length of the output of the next update() or doFinal() call

initEncrypt

public final void initEncrypt(Key key)
                       throws InvalidKeyException
Initialize the cipher for encryption by forwarding it to initEncrypt(Key, AlgorithmParameterSpec, SecureRandom).

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using getParameters().

Parameters:
key - the encryption key
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher.
InvalidParameterException - if this cipher needs algorithm parameters for initialization and cannot generate parameters itself.

initEncrypt

public final void initEncrypt(Key key,
                              SecureRandom random)
                       throws InvalidKeyException
Initialize this cipher for encryption by forwarding it to initEncrypt(Key, AlgorithmParameterSpec, SecureRandom).

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using getParameters().

Parameters:
key - the encryption key
random - the source of randomness
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher.
InvalidParameterException - if this cipher needs algorithm parameters for initialization and cannot generate parameters itself.

initEncrypt

public final void initEncrypt(Key key,
                              AlgorithmParameterSpec params)
                       throws InvalidKeyException,
                              InvalidAlgorithmParameterException
Initialize the cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).
Parameters:
key - the encryption key
params - the algorithm parameters
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher.
InvalidAlgorithmParameterException - if the given algorithm parameters are inappropriate for this cipher, or if this cipher is initialized with null parameters and cannot generate parameters itself.

initEncrypt

public final void initEncrypt(Key key,
                              AlgorithmParameterSpec params,
                              SecureRandom random)
                       throws InvalidKeyException,
                              InvalidAlgorithmParameterException
Initialize the cipher with a certain key for data encryption.

If this cipher requires any random bytes (e.g., for parameter generation), it will get them from random.

Note that when a Cipher object is initialized, it loses all previously-acquired state. In other words, initializing a Cipher is equivalent to creating a new instance of that Cipher and initializing it.

Overrides:
initEncrypt in class Cipher
Parameters:
key - the encryption key
random - the source of randomness
params - the algorithm parameters
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher
InvalidAlgorithmParameterException - if the given algorithm parameters are inappropriate for this cipher, or if this cipher is initialized with null parameters and cannot generate parameters itself.

initDecrypt

public final void initDecrypt(Key key)
                       throws InvalidKeyException
Initialize the cipher for decryption by forwarding it to initDecrypt(Key, FlexiSecureRandom).

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using getParameters().

Parameters:
key - the decryption key
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher.

initDecrypt

public final void initDecrypt(Key key,
                              AlgorithmParameterSpec params)
                       throws InvalidKeyException,
                              InvalidAlgorithmParameterException
Initialize the cipher with a certain key for data decryption.

If this cipher requires any random bytes (e.g., for parameter generation), it will get them from random.

Note that when a Cipher object is initialized, it loses all previously-acquired state. In other words, initializing a Cipher is equivalent to creating a new instance of that Cipher and initializing it

Overrides:
initDecrypt in class Cipher
Parameters:
key - the decryption key
params - the algorithm parameters
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher
InvalidAlgorithmParameterException - if the given algorithm parameters are inappropriate for this cipher, or if this cipher is initialized with null parameters and cannot generate parameters itself.

update

public abstract byte[] update(byte[] input,
                              int inOff,
                              int inLen)
Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
Overrides:
update in class Cipher
Parameters:
input - the input buffer
inOff - the offset where the input starts
inLen - the input length
Returns:
a new buffer with the result (maybe an empty byte array)

update

public final int update(byte[] input,
                        int inOff,
                        int inLen,
                        byte[] output,
                        int outOff)
                 throws ShortBufferException
Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
Overrides:
update in class Cipher
Parameters:
input - the input buffer
inOff - the offset where the input starts
inLen - the input length
output - the output buffer
outOff - the offset where the result is stored
Returns:
the length of the output
Throws:
ShortBufferException - if the output buffer is too small to hold the result.

doFinal

public abstract byte[] doFinal(byte[] input,
                               int inOff,
                               int inLen)
                        throws BadPaddingException
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
Overrides:
doFinal in class Cipher
Parameters:
input - the input buffer
inOff - the offset where the input starts
inLen - the input length
Returns:
a new buffer with the result
Throws:
BadPaddingException - if the ciphertext is invalid.

doFinal

public final int doFinal(byte[] input,
                         int inOff,
                         int inLen,
                         byte[] output,
                         int outOff)
                  throws ShortBufferException,
                         BadPaddingException
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
Overrides:
doFinal in class Cipher
Parameters:
input - the input buffer
inOff - the offset where the input starts
inLen - the input length
output - the buffer for the result
outOff - the offset where the result is stored
Returns:
the output length
Throws:
ShortBufferException - if the output buffer is too small to hold the result.
BadPaddingException - if the ciphertext is invalid.

encryptOutputSize

protected abstract int encryptOutputSize(int inLen)
Compute the output size of an update() or doFinal() operation of a hybrid asymmetric cipher in encryption mode when given input of the specified length.
Parameters:
inLen - the length of the input
Returns:
the output size

decryptOutputSize

protected abstract int decryptOutputSize(int inLen)
Compute the output size of an update() or doFinal() operation of a hybrid asymmetric cipher in decryption mode when given input of the specified length.
Parameters:
inLen - the length of the input
Returns:
the output size

initCipherEncrypt

protected abstract void initCipherEncrypt(Key key,
                                          AlgorithmParameterSpec params,
                                          SecureRandom sr)
                                   throws InvalidKeyException,
                                          InvalidAlgorithmParameterException
Initialize the AsymmetricHybridCipher with a certain key for data encryption.
Parameters:
key - the key which has to be used to encrypt data
params - the algorithm parameters
sr - the source of randomness
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher.
InvalidAlgorithmParameterException - if the given parameters are inappropriate for initializing this cipher.

initCipherDecrypt

protected abstract void initCipherDecrypt(Key key,
                                          AlgorithmParameterSpec params)
                                   throws InvalidKeyException,
                                          InvalidAlgorithmParameterException
Initialize the AsymmetricHybridCipher with a certain key for data encryption.
Parameters:
key - the key which has to be used to decrypt data
params - the algorithm parameters
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher
InvalidAlgorithmParameterException - if the given parameters are inappropriate for initializing this cipher.