Class CipherSpiExt

java.lang.Object
javax.crypto.CipherSpi
org.bouncycastle.pqc.jcajce.provider.util.CipherSpiExt
Direct Known Subclasses:
AsymmetricBlockCipher, AsymmetricHybridCipher

public abstract class CipherSpiExt extends CipherSpi
The CipherSpiExt class extends CipherSpi.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Constant specifying decrypt mode.
    static final int
    Constant specifying encrypt mode.
    protected int
    The operation mode for this cipher (ENCRYPT_MODE or DECRYPT_MODE).
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final byte[]
    Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
    final byte[]
    doFinal(byte[] input)
    Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
    abstract byte[]
    doFinal(byte[] input, int inOff, int inLen)
    Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
    abstract 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 final byte[]
    engineDoFinal(byte[] input, int inOff, int inLen)
    Return the result of the last step of a multi-step en-/decryption operation or the result of a single-step en-/decryption operation by processing the given input data and any remaining buffered data.
    protected final int
    engineDoFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff)
    Perform the last step of a multi-step en-/decryption operation or a single-step en-/decryption operation by processing the given input data and any remaining buffered data.
    protected final int
     
    protected final byte[]
    Return the initialization vector.
    protected final int
    Return the key size of the given key object in bits.
    protected final int
    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 inputLen (in bytes).
    protected final AlgorithmParameters
    Returns the parameters used with this cipher.
    protected final void
    engineInit(int opMode, Key key, AlgorithmParameters algParams, SecureRandom random)
    Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness.
    protected final void
    engineInit(int opMode, Key key, SecureRandom random)
    Initialize this cipher object with a proper key and some random seed.
    protected void
    engineInit(int opMode, Key key, AlgorithmParameterSpec params, SecureRandom javaRand)
    Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness.
    protected final void
    Set the mode of this cipher.
    protected final void
    engineSetPadding(String paddingName)
    Set the padding scheme of this cipher.
    protected final byte[]
    engineUpdate(byte[] input, int inOff, int inLen)
    Return the result of the next step of a multi-step en-/decryption operation.
    protected final int
    engineUpdate(byte[] input, int inOff, int inLen, byte[] output, int outOff)
    Perform the next step of a multi-step en-/decryption operation.
    abstract int
     
    abstract byte[]
    Return the initialization vector.
    abstract int
    Return the key size of the given key object in bits.
    abstract String
     
    abstract int
    getOutputSize(int inputLen)
    Returns 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 inputLen (in bytes).
    Returns the parameters used with this cipher.
    abstract void
    Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness for decryption.
    abstract void
    initEncrypt(Key key, AlgorithmParameterSpec cipherParams, SecureRandom random)
    Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness for encryption.
    protected abstract void
    Set the mode of this cipher.
    protected abstract void
    setPadding(String padding)
    Set the padding mechanism of this cipher.
    final byte[]
    update(byte[] input)
    Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
    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.
    abstract 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 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ENCRYPT_MODE

      public static final int ENCRYPT_MODE
      Constant specifying encrypt mode.
      See Also:
    • DECRYPT_MODE

      public static final int DECRYPT_MODE
      Constant specifying decrypt mode.
      See Also:
    • opMode

      protected int opMode
      The operation mode for this cipher (ENCRYPT_MODE or DECRYPT_MODE).
  • Constructor Details

    • CipherSpiExt

      public CipherSpiExt()
  • Method Details

    • engineInit

      protected final void engineInit(int opMode, Key key, SecureRandom random) throws InvalidKeyException
      Initialize this cipher object with a proper key and some random seed. Before a cipher object is ready for data processing, it has to be initialized according to the desired cryptographic operation, which is specified by the opMode parameter.

      If this cipher (including its underlying mode or padding scheme) requires any random bytes, it will obtain them from random.

      Note: If the mode needs an initialization vector, a blank array is used in this case.

      Specified by:
      engineInit in class CipherSpi
      Parameters:
      opMode - the operation mode (ENCRYPT_MODE or DECRYPT_MODE)
      key - the key
      random - the random seed
      Throws:
      InvalidKeyException - if the key is inappropriate for initializing this cipher.
    • engineInit

      protected final void engineInit(int opMode, Key key, AlgorithmParameters algParams, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException
      Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness. The cipher is initialized for encryption or decryption, depending on the value of opMode.

      If this cipher (including its underlying mode or padding scheme) requires any random bytes, it will obtain 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.

      Note: If the mode needs an initialization vector, a try to retrieve it from the AlgorithmParametersSpec is made.

      Specified by:
      engineInit in class CipherSpi
      Parameters:
      opMode - the operation mode (ENCRYPT_MODE or DECRYPT_MODE)
      key - the key
      algParams - the algorithm parameters
      random - the random seed
      Throws:
      InvalidKeyException - if the key is inappropriate for initializing this block cipher.
      InvalidAlgorithmParameterException - if the parameters are inappropriate for initializing this block cipher.
    • engineInit

      protected void engineInit(int opMode, Key key, AlgorithmParameterSpec params, SecureRandom javaRand) throws InvalidKeyException, InvalidAlgorithmParameterException
      Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness. The cipher is initialized for one of the following four operations: encryption, decryption, key wrapping or key unwrapping, depending on the value of opMode. If this cipher (including its underlying feedback or padding scheme) 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.
      Specified by:
      engineInit in class CipherSpi
      Parameters:
      opMode - the operation mode (ENCRYPT_MODE or DECRYPT_MODE)
      key - the encryption key
      params - the algorithm parameters
      javaRand - the source of randomness
      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 being initialized for decryption and requires algorithm parameters and the parameters are null.
    • engineDoFinal

      protected final byte[] engineDoFinal(byte[] input, int inOff, int inLen) throws IllegalBlockSizeException, BadPaddingException
      Return the result of the last step of a multi-step en-/decryption operation or the result of a single-step en-/decryption operation by processing the given input data and any remaining buffered data. The data to be processed is given in an input byte array. Beginning at inputOffset, only the first inputLen bytes are en-/decrypted, including any buffered bytes of a previous update operation. If necessary, padding is performed. The result is returned as a output byte array.
      Specified by:
      engineDoFinal in class CipherSpi
      Parameters:
      input - the byte array holding the data to be processed
      inOff - the offset indicating the start position within the input byte array
      inLen - the number of bytes to be processed
      Returns:
      the byte array containing the en-/decrypted data
      Throws:
      IllegalBlockSizeException - if the ciphertext length is not a multiple of the blocklength.
      BadPaddingException - if unpadding is not possible.
    • engineDoFinal

      protected final int engineDoFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException
      Perform the last step of a multi-step en-/decryption operation or a single-step en-/decryption operation by processing the given input data and any remaining buffered data. The data to be processed is given in an input byte array. Beginning at inputOffset, only the first inputLen bytes are en-/decrypted, including any buffered bytes of a previous update operation. If necessary, padding is performed. The result is stored in the given output byte array, beginning at outputOffset. The number of bytes stored in this byte array are returned.
      Specified by:
      engineDoFinal in class CipherSpi
      Parameters:
      input - the byte array holding the data to be processed
      inOff - the offset indicating the start position within the input byte array
      inLen - the number of bytes to be processed
      output - the byte array for holding the result
      outOff - the offset indicating the start position within the output byte array to which the en/decrypted data is written
      Returns:
      the number of bytes stored in the output byte array
      Throws:
      ShortBufferException - if the output buffer is too short to hold the output.
      IllegalBlockSizeException - if the ciphertext length is not a multiple of the blocklength.
      BadPaddingException - if unpadding is not possible.
    • engineGetBlockSize

      protected final int engineGetBlockSize()
      Specified by:
      engineGetBlockSize in class CipherSpi
      Returns:
      the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
    • engineGetKeySize

      protected final int engineGetKeySize(Key key) throws InvalidKeyException
      Return the key size of the given key object in bits.
      Overrides:
      engineGetKeySize in class CipherSpi
      Parameters:
      key - the key object
      Returns:
      the key size in bits of the given key object
      Throws:
      InvalidKeyException - if key is invalid.
    • engineGetIV

      protected final byte[] engineGetIV()
      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.
      Specified by:
      engineGetIV in class CipherSpi
      Returns:
      the initialization vector in a new buffer, or null if the underlying algorithm does not use an IV, or if the IV has not yet been set.
    • engineGetOutputSize

      protected final int engineGetOutputSize(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 inputLen (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.

      Specified by:
      engineGetOutputSize in class CipherSpi
      Parameters:
      inLen - the input length (in bytes)
      Returns:
      the required output buffer size (in bytes)
    • engineGetParameters

      protected final AlgorithmParameters engineGetParameters()
      Returns 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).

      Specified by:
      engineGetParameters in class CipherSpi
      Returns:
      the parameters used with this cipher, or null if this cipher does not use any parameters.
    • engineSetMode

      protected final void engineSetMode(String modeName) throws NoSuchAlgorithmException
      Set the mode of this cipher.
      Specified by:
      engineSetMode in class CipherSpi
      Parameters:
      modeName - the cipher mode
      Throws:
      NoSuchAlgorithmException - if neither the mode with the given name nor the default mode can be found
    • engineSetPadding

      protected final void engineSetPadding(String paddingName) throws NoSuchPaddingException
      Set the padding scheme of this cipher.
      Specified by:
      engineSetPadding in class CipherSpi
      Parameters:
      paddingName - the padding scheme
      Throws:
      NoSuchPaddingException - if the requested padding scheme cannot be found.
    • engineUpdate

      protected final byte[] engineUpdate(byte[] input, int inOff, int inLen)
      Return the result of the next step of a multi-step en-/decryption operation. The data to be processed is given in an input byte array. Beginning at inputOffset, only the first inputLen bytes are en-/decrypted. The result is returned as a byte array.
      Specified by:
      engineUpdate in class CipherSpi
      Parameters:
      input - the byte array holding the data to be processed
      inOff - the offset indicating the start position within the input byte array
      inLen - the number of bytes to be processed
      Returns:
      the byte array containing the en-/decrypted data
    • engineUpdate

      protected final int engineUpdate(byte[] input, int inOff, int inLen, byte[] output, int outOff) throws ShortBufferException
      Perform the next step of a multi-step en-/decryption operation. The data to be processed is given in an input byte array. Beginning at inputOffset, only the first inputLen bytes are en-/decrypted. The result is stored in the given output byte array, beginning at outputOffset. The number of bytes stored in this output byte array are returned.
      Specified by:
      engineUpdate in class CipherSpi
      Parameters:
      input - the byte array holding the data to be processed
      inOff - the offset indicating the start position within the input byte array
      inLen - the number of bytes to be processed
      output - the byte array for holding the result
      outOff - the offset indicating the start position within the output byte array to which the en-/decrypted data is written
      Returns:
      the number of bytes that are stored in the output byte array
      Throws:
      ShortBufferException - if the output buffer is too short to hold the output.
    • initEncrypt

      public abstract void initEncrypt(Key key, AlgorithmParameterSpec cipherParams, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException
      Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness for encryption.

      If this cipher requires any algorithm parameters and paramSpec is null, 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 InvalidAlgorithmParameterException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

      If this cipher (including its underlying feedback or padding scheme) 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.

      Parameters:
      key - the encryption key
      cipherParams - the cipher parameters
      random - the source of randomness
      Throws:
      InvalidKeyException - if the given key is inappropriate for initializing this block cipher.
      InvalidAlgorithmParameterException - if the parameters are inappropriate for initializing this block cipher.
    • initDecrypt

      public abstract void initDecrypt(Key key, AlgorithmParameterSpec cipherParams) throws InvalidKeyException, InvalidAlgorithmParameterException
      Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness for decryption.

      If this cipher requires any algorithm parameters and paramSpec is null, 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 throw an InvalidAlgorithmParameterException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

      If this cipher (including its underlying feedback or padding scheme) 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.

      Parameters:
      key - the encryption key
      cipherParams - the cipher parameters
      Throws:
      InvalidKeyException - if the given key is inappropriate for initializing this block cipher.
      InvalidAlgorithmParameterException - if the parameters are inappropriate for initializing this block cipher.
    • getName

      public abstract String getName()
      Returns:
      the name of this cipher
    • getBlockSize

      public abstract int getBlockSize()
      Returns:
      the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
    • getOutputSize

      public abstract int getOutputSize(int inputLen)
      Returns 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 inputLen (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.

      Parameters:
      inputLen - the input length (in bytes)
      Returns:
      the required output buffer size (in bytes)
    • getKeySize

      public abstract int getKeySize(Key key) throws InvalidKeyException
      Return the key size of the given key object in bits.
      Parameters:
      key - the key object
      Returns:
      the key size in bits of the given key object
      Throws:
      InvalidKeyException - if key is invalid.
    • getParameters

      public abstract AlgorithmParameterSpec getParameters()
      Returns 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).

      Returns:
      the parameters used with this cipher, or null if this cipher does not use any parameters.
    • getIV

      public abstract byte[] getIV()
      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.
      Returns:
      the initialization vector in a new buffer, or null if the underlying algorithm does not use an IV, or if the IV has not yet been set.
    • setMode

      protected abstract void setMode(String mode) throws NoSuchAlgorithmException
      Set the mode of this cipher.
      Parameters:
      mode - the cipher mode
      Throws:
      NoSuchAlgorithmException - if the requested mode cannot be found.
    • setPadding

      protected abstract void setPadding(String padding) throws NoSuchPaddingException
      Set the padding mechanism of this cipher.
      Parameters:
      padding - the padding mechanism
      Throws:
      NoSuchPaddingException - if the requested padding scheme cannot be found.
    • update

      public final byte[] update(byte[] input)
      Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.
      Parameters:
      input - the input buffer
      Returns:
      a new buffer with the result (maybe an empty byte array)
    • 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.
      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 abstract 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.
      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 final byte[] doFinal() throws IllegalBlockSizeException, BadPaddingException
      Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
      Returns:
      a new buffer with the result
      Throws:
      IllegalBlockSizeException - if this cipher is a block cipher and the total input length is not a multiple of the block size (for encryption when no padding is used or for decryption).
      BadPaddingException - if this cipher is a block cipher and unpadding fails.
    • doFinal

      public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
      Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
      Parameters:
      input - the input buffer
      Returns:
      a new buffer with the result
      Throws:
      IllegalBlockSizeException - if this cipher is a block cipher and the total input length is not a multiple of the block size (for encryption when no padding is used or for decryption).
      BadPaddingException - if this cipher is a block cipher and unpadding fails.
    • doFinal

      public abstract byte[] doFinal(byte[] input, int inOff, int inLen) throws IllegalBlockSizeException, BadPaddingException
      Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
      Parameters:
      input - the input buffer
      inOff - the offset where the input starts
      inLen - the input length
      Returns:
      a new buffer with the result
      Throws:
      IllegalBlockSizeException - if this cipher is a block cipher and the total input length is not a multiple of the block size (for encryption when no padding is used or for decryption).
      BadPaddingException - if this cipher is a block cipher and unpadding fails.
    • doFinal

      public abstract int doFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException
      Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).
      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.
      IllegalBlockSizeException - if this cipher is a block cipher and the total input length is not a multiple of the block size (for encryption when no padding is used or for decryption).
      BadPaddingException - if this cipher is a block cipher and unpadding fails.