Class AsymmetricBlockCipher
- Direct Known Subclasses:
McEliecePKCSCipherSpi
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ByteArrayOutputStreamInternal bufferprotected intThe maximum number of bytes the cipher can encrypt.protected intThe maximum number of bytes the cipher can decrypt.protected AlgorithmParameterSpecParameterSpec used with this cipherFields inherited from class org.bouncycastle.pqc.jcajce.provider.util.CipherSpiExt
DECRYPT_MODE, ENCRYPT_MODE, opMode -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcheckLength(int inLen) Check if the message length plus the length of the input length can be en/decrypted.final byte[]doFinal(byte[] input, int inOff, int inLen) Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).final intdoFinal(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).final intReturn the block size (in bytes).final byte[]getIV()Return the initialization vector.final intgetOutputSize(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).final AlgorithmParameterSpecReturns the parameters used with this cipher.protected abstract voidinitCipherDecrypt(Key key, AlgorithmParameterSpec params) Initialize the AsymmetricBlockCipher with a certain key for data encryption.protected abstract voidinitCipherEncrypt(Key key, AlgorithmParameterSpec params, SecureRandom sr) Initialize the AsymmetricBlockCipher with a certain key for data encryption.final voidinitDecrypt(Key key) Initialize the cipher for decryption by forwarding it toinitDecrypt(Key, AlgorithmParameterSpec).final voidinitDecrypt(Key key, AlgorithmParameterSpec params) This method initializes the AsymmetricBlockCipher with a certain key for data decryption.final voidinitEncrypt(Key key) Initializes the cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom).final voidinitEncrypt(Key key, SecureRandom random) Initialize this cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).final voidinitEncrypt(Key key, AlgorithmParameterSpec params) Initializes the cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).final voidinitEncrypt(Key key, AlgorithmParameterSpec params, SecureRandom secureRandom) This method initializes the AsymmetricBlockCipher with a certain key for data encryption.protected abstract byte[]messageDecrypt(byte[] input) Decrypt the ciphertext stored in input.protected abstract byte[]messageEncrypt(byte[] input) Encrypt the message stored in input.protected final voidSince asymmetric block ciphers do not support modes, this method does nothing.protected final voidsetPadding(String paddingName) Since asymmetric block ciphers do not support padding, this method does nothing.final byte[]update(byte[] input, int inOff, int inLen) Continue a multiple-part encryption or decryption operation.final intupdate(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 org.bouncycastle.pqc.jcajce.provider.util.CipherSpiExt
doFinal, doFinal, engineDoFinal, engineDoFinal, engineGetBlockSize, engineGetIV, engineGetKeySize, engineGetOutputSize, engineGetParameters, engineInit, engineInit, engineInit, engineSetMode, engineSetPadding, engineUpdate, engineUpdate, getKeySize, getName, updateMethods inherited from class javax.crypto.CipherSpi
engineDoFinal, engineUnwrap, engineUpdate, engineUpdateAAD, engineUpdateAAD, engineWrap
-
Field Details
-
paramSpec
ParameterSpec used with this cipher -
buf
Internal buffer -
maxPlainTextSize
protected int maxPlainTextSizeThe maximum number of bytes the cipher can decrypt. -
cipherTextSize
protected int cipherTextSizeThe maximum number of bytes the cipher can encrypt.
-
-
Constructor Details
-
AsymmetricBlockCipher
public AsymmetricBlockCipher()The AsymmetricBlockCipher() constructor
-
-
Method Details
-
getBlockSize
public final int getBlockSize()Return the block size (in bytes). Note: although the ciphers extending this class are not block ciphers, the method was adopted to return the maximal plaintext and ciphertext sizes for non hybrid ciphers. If the cipher is hybrid, it returns 0.- Specified by:
getBlockSizein classCipherSpiExt- Returns:
- if the cipher is not a hybrid one the max plain/cipher text size is returned, otherwise 0 is returned
-
getIV
public final byte[] getIV()Description copied from class:CipherSpiExtReturn 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:
getIVin classCipherSpiExt- Returns:
- null since no initialization vector is used.
-
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.If the input length plus the length of the buffered data exceeds the maximum length, 0 is returned.
- Specified by:
getOutputSizein classCipherSpiExt- Parameters:
inLen- the length of the input- Returns:
- the length of the ciphertext or 0 if the input is too long.
-
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).
- Specified by:
getParametersin classCipherSpiExt- Returns:
- the parameters used with this cipher, or null if this cipher does not use any parameters.
-
initEncrypt
Initializes the cipher for encryption by forwarding it to initEncrypt(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 engineGetParameters or engineGetIV (if the parameter is an IV).
- Parameters:
key- the encryption or decryption key.- Throws:
InvalidKeyException- if the given key is inappropriate for initializing this cipher.
-
initEncrypt
Initialize this cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).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 engineGetParameters or engineGetIV (if the parameter is an IV).
- Parameters:
key- the encryption or decryption key.random- the source of randomness.- Throws:
InvalidKeyException- if the given key is inappropriate for initializing this cipher.
-
initEncrypt
public final void initEncrypt(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException Initializes the cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).- Parameters:
key- the encryption or decryption key.params- the algorithm parameters.- Throws:
InvalidKeyException- if the given key is inappropriate for initializing this cipher.InvalidAlgorithmParameterException- if the given algortihm parameters are inappropriate for this cipher, or if this cipher is being initialized for decryption and requires algorithm parameters and params is null.
-
initEncrypt
public final void initEncrypt(Key key, AlgorithmParameterSpec params, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException This method initializes the AsymmetricBlockCipher with a certain key for data encryption.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:
initEncryptin classCipherSpiExt- Parameters:
key- the key which has to be used to encrypt data.params- the algorithm parameters.secureRandom- the source of randomness.- Throws:
InvalidKeyException- if the given key is inappropriate for initializing this cipherInvalidAlgorithmParameterException- if the given algorithm parameters are inappropriate for this cipher, or if this cipher is being initialized for decryption and requires algorithm parameters and params is null.
-
initDecrypt
Initialize the cipher for decryption by forwarding it toinitDecrypt(Key, AlgorithmParameterSpec).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 engineGetParameters or engineGetIV (if the parameter is an IV).
- Parameters:
key- the encryption or 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 This method initializes the AsymmetricBlockCipher with a certain key for data decryption.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:
initDecryptin classCipherSpiExt- 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 cipherInvalidAlgorithmParameterException- if the given algorithm parameters are inappropriate for this cipher, or if this cipher is being initialized for decryption and requires algorithm parameters and params is null.
-
update
public final byte[] update(byte[] input, int inOff, int inLen) Continue a multiple-part encryption or decryption operation. This method just writes the input into an internal buffer.- Specified by:
updatein classCipherSpiExt- Parameters:
input- byte array containing the next part of the inputinOff- index in the array where the input startsinLen- length of the input- Returns:
- a new buffer with the result (always empty)
-
update
public final 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.- Specified by:
updatein classCipherSpiExt- 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 (always 0)
-
doFinal
public final 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).- Specified by:
doFinalin classCipherSpiExt- Parameters:
input- the input bufferinOff- the offset where the input startsinLen- the input length- Returns:
- a new buffer with the result
- Throws:
IllegalBlockSizeException- if the plaintext or ciphertext size is too large.BadPaddingException- if the ciphertext is invalid.
-
doFinal
public final 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).- Specified by:
doFinalin classCipherSpiExt- 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:
ShortBufferException- if the output buffer is too small to hold the result.IllegalBlockSizeException- if the plaintext or ciphertext size is too large.BadPaddingException- if the ciphertext is invalid.
-
setMode
Since asymmetric block ciphers do not support modes, this method does nothing.- Specified by:
setModein classCipherSpiExt- Parameters:
modeName- the cipher mode (unused)
-
setPadding
Since asymmetric block ciphers do not support padding, this method does nothing.- Specified by:
setPaddingin classCipherSpiExt- Parameters:
paddingName- the name of the padding scheme (not used)
-
checkLength
Check if the message length plus the length of the input length can be en/decrypted. This method uses the specific valuesmaxPlainTextSizeandcipherTextSizewhich are set by the implementations. If the input length plus the length of the internal buffer is greater thanmaxPlainTextSizefor encryption or not equal tocipherTextSizefor decryption, anIllegalBlockSizeExceptionwill be thrown.- Parameters:
inLen- length of the input to check- Throws:
IllegalBlockSizeException- if the input length is invalid.
-
initCipherEncrypt
protected abstract void initCipherEncrypt(Key key, AlgorithmParameterSpec params, SecureRandom sr) throws InvalidKeyException, InvalidAlgorithmParameterException Initialize the AsymmetricBlockCipher with a certain key for data encryption.- Parameters:
key- the key which has to be used to encrypt dataparams- the algorithm parameterssr- 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 AsymmetricBlockCipher with a certain key for data encryption.- Parameters:
key- the key which has to be used to decrypt dataparams- the algorithm parameters- Throws:
InvalidKeyException- if the given key is inappropriate for initializing this cipherInvalidAlgorithmParameterException- if the given parameters are inappropriate for initializing this cipher.
-
messageEncrypt
protected abstract byte[] messageEncrypt(byte[] input) throws IllegalBlockSizeException, BadPaddingException Encrypt the message stored in input. The method should also perform an additional length check.- Parameters:
input- the message to be encrypted (usually the message length is less than or equal to maxPlainTextSize)- Returns:
- the encrypted message (it has length equal to maxCipherTextSize_)
- Throws:
IllegalBlockSizeException- if the input is inappropriate for this cipher.BadPaddingException- if the input format is invalid.
-
messageDecrypt
protected abstract byte[] messageDecrypt(byte[] input) throws IllegalBlockSizeException, BadPaddingException Decrypt the ciphertext stored in input. The method should also perform an additional length check.- Parameters:
input- the ciphertext to be decrypted (the ciphertext length is less than or equal to maxCipherTextSize)- Returns:
- the decrypted message
- Throws:
IllegalBlockSizeException- if the input is inappropriate for this cipher.BadPaddingException- if the input format is invalid.
-