Class OpenSsl
- java.lang.Object
-
- org.apache.commons.crypto.cipher.OpenSsl
-
final class OpenSsl extends java.lang.ObjectOpenSSL cryptographic wrapper using JNI. Currently only AES-CTR is supported. It's flexible to add other crypto algorithms/modes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classOpenSsl.AlgorithmModeCurrently only support AES/CTR/NoPadding.
-
Field Summary
Fields Modifier and Type Field Description static intDECRYPT_MODEstatic intENCRYPT_MODEprivate static java.lang.ThrowableloadingFailureReasonprivate AbstractOpenSslFeedbackCipheropensslBlockCipher
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclean()Forcibly clean the context.intdoFinal(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.intdoFinal(java.nio.ByteBuffer input, java.nio.ByteBuffer output)Finishes a multiple-part operation.protected voidfinalize()static OpenSslgetInstance(java.lang.String transformation)Gets anOpenSslCipherthat implements the specified transformation.static java.lang.ThrowablegetLoadingFailureReason()Gets the failure reason when loading OpenSsl native.voidinit(int mode, byte[] key, java.security.spec.AlgorithmParameterSpec params)Initializes this cipher with a key and IV.intupdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)Updates a multiple-part encryption/decryption operation.intupdate(java.nio.ByteBuffer input, java.nio.ByteBuffer output)Updates a multiple-part encryption or decryption operation.voidupdateAAD(byte[] aad)Continues a multi-part update of the Additional Authentication Data (AAD).
-
-
-
Field Detail
-
ENCRYPT_MODE
public static final int ENCRYPT_MODE
- See Also:
- Constant Field Values
-
DECRYPT_MODE
public static final int DECRYPT_MODE
- See Also:
- Constant Field Values
-
loadingFailureReason
private static final java.lang.Throwable loadingFailureReason
-
opensslBlockCipher
private final AbstractOpenSslFeedbackCipher opensslBlockCipher
-
-
Constructor Detail
-
OpenSsl
private OpenSsl(long context, int algorithm, int padding)Constructs aOpenSslinstance based on context, algorithm and padding.- Parameters:
context- the context.algorithm- the algorithm.padding- the padding.
-
-
Method Detail
-
getInstance
public static OpenSsl getInstance(java.lang.String transformation) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException
Gets anOpenSslCipherthat implements the specified transformation.- Parameters:
transformation- the name of the transformation, e.g., AES/CTR/NoPadding.- Returns:
- OpenSslCipher an
OpenSslCipherobject - Throws:
java.security.NoSuchAlgorithmException- iftransformationis null, empty, in an invalid format, or if OpenSsl doesn't implement the specified algorithm.javax.crypto.NoSuchPaddingException- iftransformationcontains a padding scheme that is not available.java.lang.IllegalStateException- if native code cannot be initialized
-
getLoadingFailureReason
public static java.lang.Throwable 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 javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingExceptionFinalizes 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:
javax.crypto.ShortBufferException- if the given output byte array is too small to hold the resultjavax.crypto.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 bytesjavax.crypto.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.
-
doFinal
public int doFinal(java.nio.ByteBuffer input, java.nio.ByteBuffer output) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingExceptionFinishes 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, aShortBufferExceptionis 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:
javax.crypto.ShortBufferException- if the given output byte array is too small to hold the result.javax.crypto.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.javax.crypto.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
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
init
public void init(int mode, byte[] key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidAlgorithmParameterExceptionInitializes this cipher with a key and IV.- Parameters:
mode-ENCRYPT_MODEorDECRYPT_MODEkey- crypto keyparams- the algorithm parameters- Throws:
java.security.InvalidAlgorithmParameterException- if IV length is wrong
-
update
public int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws javax.crypto.ShortBufferExceptionUpdates 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:
javax.crypto.ShortBufferException- if there is insufficient space in the output byte array
-
update
public int update(java.nio.ByteBuffer input, java.nio.ByteBuffer output) throws javax.crypto.ShortBufferExceptionUpdates 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, aShortBufferExceptionis thrown.- Parameters:
input- the input ByteBufferoutput- the output ByteBuffer- Returns:
- int number of bytes stored in
output - Throws:
javax.crypto.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
updateanddoFinalmethods).- Parameters:
aad- the buffer containing the Additional Authentication Data
-
-