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 javax.crypto.CipherSpi
The CipherSpiExt class extends CipherSpi.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DECRYPT_MODE
Constant specifying decrypt mode.static int
ENCRYPT_MODE
Constant specifying encrypt mode.protected int
opMode
The operation mode for this cipher (ENCRYPT_MODE
orDECRYPT_MODE
).
-
Constructor Summary
Constructors Constructor Description CipherSpiExt()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description byte[]
doFinal()
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).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 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 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 int
engineGetBlockSize()
protected byte[]
engineGetIV()
Return the initialization vector.protected int
engineGetKeySize(java.security.Key key)
Return the key size of the given key object in bits.protected 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).protected java.security.AlgorithmParameters
engineGetParameters()
Returns the parameters used with this cipher.protected void
engineInit(int opMode, java.security.Key key, java.security.AlgorithmParameters algParams, java.security.SecureRandom random)
Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness.protected void
engineInit(int opMode, java.security.Key key, java.security.SecureRandom random)
Initialize this cipher object with a proper key and some random seed.protected void
engineInit(int opMode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom javaRand)
Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness.protected void
engineSetMode(java.lang.String modeName)
Set the mode of this cipher.protected void
engineSetPadding(java.lang.String paddingName)
Set the padding scheme of this cipher.protected byte[]
engineUpdate(byte[] input, int inOff, int inLen)
Return the result of the next step of a multi-step en-/decryption operation.protected 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
getBlockSize()
abstract byte[]
getIV()
Return the initialization vector.abstract int
getKeySize(java.security.Key key)
Return the key size of the given key object in bits.abstract java.lang.String
getName()
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).abstract java.security.spec.AlgorithmParameterSpec
getParameters()
Returns the parameters used with this cipher.abstract void
initDecrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec cipherParams)
Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness for decryption.abstract void
initEncrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec cipherParams, java.security.SecureRandom random)
Initialize this cipher with a key, a set of algorithm parameters, and a source of randomness for encryption.protected abstract void
setMode(java.lang.String mode)
Set the mode of this cipher.protected abstract void
setPadding(java.lang.String padding)
Set the padding mechanism of this cipher.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.
-
-
-
Field Detail
-
ENCRYPT_MODE
public static final int ENCRYPT_MODE
Constant specifying encrypt mode.- See Also:
- Constant Field Values
-
DECRYPT_MODE
public static final int DECRYPT_MODE
Constant specifying decrypt mode.- See Also:
- Constant Field Values
-
opMode
protected int opMode
The operation mode for this cipher (ENCRYPT_MODE
orDECRYPT_MODE
).
-
-
Method Detail
-
engineInit
protected final void engineInit(int opMode, java.security.Key key, java.security.SecureRandom random) throws java.security.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 classjavax.crypto.CipherSpi
- Parameters:
opMode
- the operation mode (ENCRYPT_MODE
orDECRYPT_MODE
)key
- the keyrandom
- the random seed- Throws:
java.security.InvalidKeyException
- if the key is inappropriate for initializing this cipher.
-
engineInit
protected final void engineInit(int opMode, java.security.Key key, java.security.AlgorithmParameters algParams, java.security.SecureRandom random) throws java.security.InvalidKeyException, java.security.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 classjavax.crypto.CipherSpi
- Parameters:
opMode
- the operation mode (ENCRYPT_MODE
orDECRYPT_MODE
)key
- the keyalgParams
- the algorithm parametersrandom
- the random seed- Throws:
java.security.InvalidKeyException
- if the key is inappropriate for initializing this block cipher.java.security.InvalidAlgorithmParameterException
- if the parameters are inappropriate for initializing this block cipher.
-
engineInit
protected void engineInit(int opMode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom javaRand) throws java.security.InvalidKeyException, java.security.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 classjavax.crypto.CipherSpi
- Parameters:
opMode
- the operation mode (ENCRYPT_MODE
orDECRYPT_MODE
)key
- the encryption keyparams
- the algorithm parametersjavaRand
- the source of randomness- Throws:
java.security.InvalidKeyException
- if the given key is inappropriate for initializing this cipherjava.security.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 javax.crypto.IllegalBlockSizeException, javax.crypto.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 classjavax.crypto.CipherSpi
- Parameters:
input
- the byte array holding the data to be processedinOff
- the offset indicating the start position within the input byte arrayinLen
- the number of bytes to be processed- Returns:
- the byte array containing the en-/decrypted data
- Throws:
javax.crypto.IllegalBlockSizeException
- if the ciphertext length is not a multiple of the blocklength.javax.crypto.BadPaddingException
- if unpadding is not possible.
-
engineDoFinal
protected final int engineDoFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.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 classjavax.crypto.CipherSpi
- Parameters:
input
- the byte array holding the data to be processedinOff
- the offset indicating the start position within the input byte arrayinLen
- the number of bytes to be processedoutput
- the byte array for holding the resultoutOff
- 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:
javax.crypto.ShortBufferException
- if the output buffer is too short to hold the output.javax.crypto.IllegalBlockSizeException
- if the ciphertext length is not a multiple of the blocklength.javax.crypto.BadPaddingException
- if unpadding is not possible.
-
engineGetBlockSize
protected final int engineGetBlockSize()
- Specified by:
engineGetBlockSize
in classjavax.crypto.CipherSpi
- Returns:
- the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
-
engineGetKeySize
protected final int engineGetKeySize(java.security.Key key) throws java.security.InvalidKeyException
Return the key size of the given key object in bits.- Overrides:
engineGetKeySize
in classjavax.crypto.CipherSpi
- Parameters:
key
- the key object- Returns:
- the key size in bits of the given key object
- Throws:
java.security.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 classjavax.crypto.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 classjavax.crypto.CipherSpi
- Parameters:
inLen
- the input length (in bytes)- Returns:
- the required output buffer size (in bytes)
-
engineGetParameters
protected final java.security.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 classjavax.crypto.CipherSpi
- Returns:
- the parameters used with this cipher, or null if this cipher does not use any parameters.
-
engineSetMode
protected final void engineSetMode(java.lang.String modeName) throws java.security.NoSuchAlgorithmException
Set the mode of this cipher.- Specified by:
engineSetMode
in classjavax.crypto.CipherSpi
- Parameters:
modeName
- the cipher mode- Throws:
java.security.NoSuchAlgorithmException
- if neither the mode with the given name nor the default mode can be found
-
engineSetPadding
protected final void engineSetPadding(java.lang.String paddingName) throws javax.crypto.NoSuchPaddingException
Set the padding scheme of this cipher.- Specified by:
engineSetPadding
in classjavax.crypto.CipherSpi
- Parameters:
paddingName
- the padding scheme- Throws:
javax.crypto.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 classjavax.crypto.CipherSpi
- Parameters:
input
- the byte array holding the data to be processedinOff
- the offset indicating the start position within the input byte arrayinLen
- 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 javax.crypto.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 classjavax.crypto.CipherSpi
- Parameters:
input
- the byte array holding the data to be processedinOff
- the offset indicating the start position within the input byte arrayinLen
- the number of bytes to be processedoutput
- the byte array for holding the resultoutOff
- 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:
javax.crypto.ShortBufferException
- if the output buffer is too short to hold the output.
-
initEncrypt
public abstract void initEncrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec cipherParams, java.security.SecureRandom random) throws java.security.InvalidKeyException, java.security.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 keycipherParams
- the cipher parametersrandom
- the source of randomness- Throws:
java.security.InvalidKeyException
- if the given key is inappropriate for initializing this block cipher.java.security.InvalidAlgorithmParameterException
- if the parameters are inappropriate for initializing this block cipher.
-
initDecrypt
public abstract void initDecrypt(java.security.Key key, java.security.spec.AlgorithmParameterSpec cipherParams) throws java.security.InvalidKeyException, java.security.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 keycipherParams
- the cipher parameters- Throws:
java.security.InvalidKeyException
- if the given key is inappropriate for initializing this block cipher.java.security.InvalidAlgorithmParameterException
- if the parameters are inappropriate for initializing this block cipher.
-
getName
public abstract java.lang.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(java.security.Key key) throws java.security.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:
java.security.InvalidKeyException
- if key is invalid.
-
getParameters
public abstract java.security.spec.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(java.lang.String mode) throws java.security.NoSuchAlgorithmException
Set the mode of this cipher.- Parameters:
mode
- the cipher mode- Throws:
java.security.NoSuchAlgorithmException
- if the requested mode cannot be found.
-
setPadding
protected abstract void setPadding(java.lang.String padding) throws javax.crypto.NoSuchPaddingException
Set the padding mechanism of this cipher.- Parameters:
padding
- the padding mechanism- Throws:
javax.crypto.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 bufferinOff
- the offset where the input startsinLen
- 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 javax.crypto.ShortBufferException
Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.- Parameters:
input
- the input bufferinOff
- the offset where the input startsinLen
- the input lengthoutput
- the output bufferoutOff
- the offset where the result is stored- Returns:
- the length of the output
- Throws:
javax.crypto.ShortBufferException
- if the output buffer is too small to hold the result.
-
doFinal
public final byte[] doFinal() throws javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).- Returns:
- a new buffer with the result
- Throws:
javax.crypto.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).javax.crypto.BadPaddingException
- if this cipher is a block cipher and unpadding fails.
-
doFinal
public final byte[] doFinal(byte[] input) throws javax.crypto.IllegalBlockSizeException, javax.crypto.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:
javax.crypto.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).javax.crypto.BadPaddingException
- if this cipher is a block cipher and unpadding fails.
-
doFinal
public abstract byte[] doFinal(byte[] input, int inOff, int inLen) throws javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).- Parameters:
input
- the input bufferinOff
- the offset where the input startsinLen
- the input length- Returns:
- a new buffer with the result
- Throws:
javax.crypto.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).javax.crypto.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 javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).- Parameters:
input
- the input bufferinOff
- the offset where the input startsinLen
- the input lengthoutput
- the buffer for the resultoutOff
- the offset where the result is stored- Returns:
- the output length
- Throws:
javax.crypto.ShortBufferException
- if the output buffer is too small to hold the result.javax.crypto.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).javax.crypto.BadPaddingException
- if this cipher is a block cipher and unpadding fails.
-
-