Class OpenSslJnaCipher
java.lang.Object
org.apache.commons.crypto.jna.OpenSslJnaCipher
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoCipher
Implements the CryptoCipher using JNA into OpenSSL.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enumAlgorithmMode of JNA. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate com.sun.jna.ptr.PointerByReferenceprivate final OpenSslJnaCipher.AlgorithmModeprivate final com.sun.jna.ptr.PointerByReferenceprivate final intprivate final intprivate final String -
Constructor Summary
ConstructorsConstructorDescriptionOpenSslJnaCipher(Properties props, String transformation) Constructs aCryptoCipherusing JNA into OpenSSL -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the OpenSSL cipher.intdoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.intdoFinal(ByteBuffer inBuffer, ByteBuffer outBuffer) Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.protected voidfinalize()Returns the algorithm name of thisCryptoCipherobject.intReturns the block size (in bytes).voidinit(int mode, Key key, AlgorithmParameterSpec params) Initializes the cipher with mode, key and iv.private voidthrowOnError(int retVal) intupdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) Continues a multiple-part encryption/decryption operation.intupdate(ByteBuffer inBuffer, ByteBuffer outBuffer) Continues a multiple-part encryption/decryption operation.voidupdateAAD(byte[] aad) Continues a multi-part update of the Additional Authentication Data (AAD).voidupdateAAD(ByteBuffer aad) Continues a multi-part update of the Additional Authentication Data (AAD).
-
Field Details
-
algo
private com.sun.jna.ptr.PointerByReference algo -
context
private final com.sun.jna.ptr.PointerByReference context -
algorithmMode
-
padding
private final int padding -
transformation
-
IV_LENGTH
private final int IV_LENGTH- See Also:
-
-
Constructor Details
-
OpenSslJnaCipher
Constructs aCryptoCipherusing JNA into OpenSSL- Parameters:
props- properties for OpenSSL ciphertransformation- transformation for OpenSSL cipher- Throws:
GeneralSecurityException- if OpenSSL cipher initialize failed
-
-
Method Details
-
close
public void close()Closes the OpenSSL cipher. Clean the OpenSsl native context.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
doFinal
public int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.- Specified by:
doFinalin interfaceCryptoCipher- 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 inBuffer, ByteBuffer outBuffer) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Specified by:
doFinalin interfaceCryptoCipher- Parameters:
inBuffer- the input ByteBufferoutBuffer- the output ByteBuffer- Returns:
- int number of bytes stored in
output - Throws:
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 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.ShortBufferException- if the given output buffer is too small to hold the result
-
finalize
-
getAlgorithm
Description copied from interface:CryptoCipherReturns the algorithm name of thisCryptoCipherobject.This is the same name that was specified in one of the
CryptoCipherFactory#getInstancecalls that created thisCryptoCipherobject..- Specified by:
getAlgorithmin interfaceCryptoCipher- Returns:
- the algorithm name of this
CryptoCipherobject.
-
getBlockSize
public int getBlockSize()Description copied from interface:CryptoCipherReturns the block size (in bytes).- Specified by:
getBlockSizein interfaceCryptoCipher- Returns:
- the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
-
init
public void init(int mode, Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException Initializes the cipher with mode, key and iv.- Specified by:
initin interfaceCryptoCipher- Parameters:
mode-Cipher.ENCRYPT_MODEorCipher.DECRYPT_MODEkey- crypto key for the cipherparams- the algorithm parameters- Throws:
InvalidKeyException- If key length is invalidInvalidAlgorithmParameterException- if IV length is wrong
-
throwOnError
private void throwOnError(int retVal) - Parameters:
retVal- the result value of error.
-
update
public int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Specified by:
updatein interfaceCryptoCipher- 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
Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Specified by:
updatein interfaceCryptoCipher- Parameters:
inBuffer- the input ByteBufferoutBuffer- 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) throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException Continues a multi-part update of the Additional Authentication Data (AAD).Calls to this method provide AAD to the opensslEngine when operating in modes such as AEAD (GCM). If this opensslEngine is operating in either GCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the
updateanddoFinalmethods).- Specified by:
updateAADin interfaceCryptoCipher- Parameters:
aad- the buffer containing the Additional Authentication Data- Throws:
IllegalArgumentException- if theaadbyte array is nullIllegalStateException- if this opensslEngine is in a wrong state (e.g., has not been initialized), does not accept AAD, or if operating in either GCM mode and one of theupdatemethods has already been called for the active encryption/decryption operationUnsupportedOperationException- if the implementationopensslEnginedoesn't support this operation.
-
updateAAD
public void updateAAD(ByteBuffer aad) throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException Continues a multi-part update of the Additional Authentication Data (AAD).Calls to this method provide AAD to the opensslEngine when operating in modes such as AEAD (GCM). If this opensslEngine is operating in either GCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the
updateanddoFinalmethods).- Specified by:
updateAADin interfaceCryptoCipher- Parameters:
aad- the buffer containing the Additional Authentication Data- Throws:
IllegalArgumentException- if theaadbyte array is nullIllegalStateException- if this opensslEngine is in a wrong state (e.g., has not been initialized), does not accept AAD, or if operating in either GCM mode and one of theupdatemethods has already been called for the active encryption/decryption operationUnsupportedOperationException- if the implementationopensslEnginedoesn't support this operation.
-