Class OpenSsl
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enum
Currently only support AES/CTR/NoPadding. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
private static final Throwable
private final AbstractOpenSslFeedbackCipher
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clean()
Forcibly clean the context.int
doFinal
(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) Finalizes to encrypt or decrypt data in a single-part operation, or finishes a multiple-part operation.int
doFinal
(ByteBuffer input, ByteBuffer output) Finishes a multiple-part operation.protected void
finalize()
static OpenSsl
getInstance
(String transformation) Gets anOpenSslCipher
that implements the specified transformation.static Throwable
Gets the failure reason when loading OpenSsl native.void
init
(int mode, byte[] key, AlgorithmParameterSpec params) Initializes this cipher with a key and IV.int
update
(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) Updates a multiple-part encryption/decryption operation.int
update
(ByteBuffer input, ByteBuffer output) Updates a multiple-part encryption or decryption operation.void
updateAAD
(byte[] aad) Continues a multi-part update of the Additional Authentication Data (AAD).
-
Field Details
-
ENCRYPT_MODE
public static final int ENCRYPT_MODE- See Also:
-
DECRYPT_MODE
public static final int DECRYPT_MODE- See Also:
-
loadingFailureReason
-
opensslBlockCipher
-
-
Constructor Details
-
OpenSsl
private OpenSsl(long context, int algorithm, int padding) Constructs aOpenSsl
instance based on context, algorithm and padding.- Parameters:
context
- the context.algorithm
- the algorithm.padding
- the padding.
-
-
Method Details
-
getInstance
public static OpenSsl getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException Gets anOpenSslCipher
that implements the specified transformation.- Parameters:
transformation
- the name of the transformation, e.g., AES/CTR/NoPadding.- Returns:
- OpenSslCipher an
OpenSslCipher
object - Throws:
NoSuchAlgorithmException
- iftransformation
is null, empty, in an invalid format, or if OpenSsl doesn't implement the specified algorithm.NoSuchPaddingException
- iftransformation
contains a padding scheme that is not available.IllegalStateException
- if native code cannot be initialized
-
getLoadingFailureReason
Gets the failure reason when loading OpenSsl native.- Returns:
- the failure reason; null if it was loaded and initialized successfully
-
clean
public void clean()Forcibly clean the context. -
doFinal
public int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException Finalizes to encrypt or decrypt data in a single-part operation, or finishes a multiple-part operation.- Parameters:
input
- the input byte arrayinputOffset
- the offset in input where the input startsinputLen
- the input lengthoutput
- the byte array for the resultoutputOffset
- the offset in output where the result is stored- Returns:
- the number of bytes stored in output
- Throws:
ShortBufferException
- if the given output byte array is too small to hold the resultBadPaddingException
- if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytesIllegalBlockSizeException
- if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided.
-
doFinal
public int doFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException Finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.The result is stored in the output buffer. Upon return, the output buffer's position will have advanced by n, where n is the value returned by this method; the output buffer's limit will not have changed.
If
output.remaining()
bytes are insufficient to hold the result, aShortBufferException
is thrown.Upon finishing, this method resets this cipher object to the state it was in when previously initialized. That is, the object is available to encrypt or decrypt more data.
If any exception is thrown, this cipher object need to be reset before it can be used again.- Parameters:
input
- the input ByteBufferoutput
- the output ByteBuffer- Returns:
- int number of bytes stored in
output
- Throws:
ShortBufferException
- if the given output byte array is too small to hold the result.IllegalBlockSizeException
- if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided.BadPaddingException
- if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes
-
finalize
-
init
public void init(int mode, byte[] key, AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException Initializes this cipher with a key and IV.- Parameters:
mode
-ENCRYPT_MODE
orDECRYPT_MODE
key
- crypto keyparams
- the algorithm parameters- Throws:
InvalidAlgorithmParameterException
- if IV length is wrong
-
update
public int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException Updates a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Parameters:
input
- the input byte arrayinputOffset
- the offset in input where the input startsinputLen
- the input lengthoutput
- the byte array for the resultoutputOffset
- the offset in output where the result is stored- Returns:
- the number of bytes stored in output
- Throws:
ShortBufferException
- if there is insufficient space in the output byte array
-
update
Updates a multiple-part encryption or decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.All
input.remaining()
bytes starting atinput.position()
are processed. The result is stored in the output buffer.Upon return, the input buffer's position will be equal to its limit; its limit will not have changed. The output buffer's position will have advanced by n, when n is the value returned by this method; the output buffer's limit will not have changed.
Ifoutput.remaining()
bytes are insufficient to hold the result, aShortBufferException
is thrown.- Parameters:
input
- the input ByteBufferoutput
- the output ByteBuffer- Returns:
- int number of bytes stored in
output
- Throws:
ShortBufferException
- if there is insufficient space in the output buffer
-
updateAAD
public void updateAAD(byte[] aad) Continues a multi-part update of the Additional Authentication Data (AAD).Calls to this method provide AAD to the cipher when operating in modes such as AEAD (GCM). If this cipher is operating in either GCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the
update
anddoFinal
methods).- Parameters:
aad
- the buffer containing the Additional Authentication Data
-