Package org.apache.commons.crypto.jna
Class OpenSslJnaCipher
- java.lang.Object
-
- org.apache.commons.crypto.jna.OpenSslJnaCipher
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,CryptoCipher
final class OpenSslJnaCipher extends java.lang.Object implements CryptoCipher
Implements the CryptoCipher using JNA into OpenSSL.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
OpenSslJnaCipher.AlgorithmMode
AlgorithmMode of JNA.
-
Field Summary
Fields Modifier and Type Field Description private com.sun.jna.ptr.PointerByReference
algo
private OpenSslJnaCipher.AlgorithmMode
algorithmMode
private com.sun.jna.ptr.PointerByReference
context
private int
IV_LENGTH
private int
padding
private java.lang.String
transformation
-
Constructor Summary
Constructors Constructor Description OpenSslJnaCipher(java.util.Properties props, java.lang.String transformation)
Constructs aCryptoCipher
using JNA into OpenSSL
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the OpenSSL cipher.int
doFinal(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.int
doFinal(java.nio.ByteBuffer inBuffer, java.nio.ByteBuffer outBuffer)
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.protected void
finalize()
java.lang.String
getAlgorithm()
Returns the algorithm name of thisCryptoCipher
object.int
getBlockSize()
Returns the block size (in bytes).void
init(int mode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
Initializes the cipher with mode, key and iv.private void
throwOnError(int retVal)
int
update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
Continues a multiple-part encryption/decryption operation.int
update(java.nio.ByteBuffer inBuffer, java.nio.ByteBuffer outBuffer)
Continues a multiple-part encryption/decryption operation.void
updateAAD(byte[] aad)
Continues a multi-part update of the Additional Authentication Data (AAD).void
updateAAD(java.nio.ByteBuffer aad)
Continues a multi-part update of the Additional Authentication Data (AAD).
-
-
-
Field Detail
-
algo
private com.sun.jna.ptr.PointerByReference algo
-
context
private final com.sun.jna.ptr.PointerByReference context
-
algorithmMode
private final OpenSslJnaCipher.AlgorithmMode algorithmMode
-
padding
private final int padding
-
transformation
private final java.lang.String transformation
-
IV_LENGTH
private final int IV_LENGTH
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
OpenSslJnaCipher
public OpenSslJnaCipher(java.util.Properties props, java.lang.String transformation) throws java.security.GeneralSecurityException
Constructs aCryptoCipher
using JNA into OpenSSL- Parameters:
props
- properties for OpenSSL ciphertransformation
- transformation for OpenSSL cipher- Throws:
java.security.GeneralSecurityException
- if OpenSSL cipher initialize failed
-
-
Method Detail
-
close
public void close()
Closes the OpenSSL cipher. Clean the OpenSsl native context.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
doFinal
public int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.- Specified by:
doFinal
in 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:
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 inBuffer, java.nio.ByteBuffer outBuffer) throws javax.crypto.ShortBufferException, javax.crypto.IllegalBlockSizeException, javax.crypto.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:
doFinal
in interfaceCryptoCipher
- Parameters:
inBuffer
- the input ByteBufferoutBuffer
- the output ByteBuffer- Returns:
- int number of bytes stored in
output
- Throws:
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 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.javax.crypto.ShortBufferException
- if the given output buffer is too small to hold the result
-
finalize
protected void finalize() throws java.lang.Throwable
- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.lang.Throwable
-
getAlgorithm
public java.lang.String getAlgorithm()
Description copied from interface:CryptoCipher
Returns the algorithm name of thisCryptoCipher
object.This is the same name that was specified in one of the
CryptoCipherFactory#getInstance
calls that created thisCryptoCipher
object..- Specified by:
getAlgorithm
in interfaceCryptoCipher
- Returns:
- the algorithm name of this
CryptoCipher
object.
-
getBlockSize
public int getBlockSize()
Description copied from interface:CryptoCipher
Returns the block size (in bytes).- Specified by:
getBlockSize
in interfaceCryptoCipher
- Returns:
- the block size (in bytes), or 0 if the underlying algorithm is not a block cipher
-
init
public void init(int mode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
Initializes the cipher with mode, key and iv.- Specified by:
init
in interfaceCryptoCipher
- Parameters:
mode
-Cipher.ENCRYPT_MODE
orCipher.DECRYPT_MODE
key
- crypto key for the cipherparams
- the algorithm parameters- Throws:
java.security.InvalidKeyException
- If key length is invalidjava.security.InvalidAlgorithmParameterException
- 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 javax.crypto.ShortBufferException
Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Specified by:
update
in 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:
javax.crypto.ShortBufferException
- if there is insufficient space in the output byte array
-
update
public int update(java.nio.ByteBuffer inBuffer, java.nio.ByteBuffer outBuffer) throws javax.crypto.ShortBufferException
Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.- Specified by:
update
in interfaceCryptoCipher
- Parameters:
inBuffer
- the input ByteBufferoutBuffer
- 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) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, java.lang.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
update
anddoFinal
methods).- Specified by:
updateAAD
in interfaceCryptoCipher
- Parameters:
aad
- the buffer containing the Additional Authentication Data- Throws:
java.lang.IllegalArgumentException
- if theaad
byte array is nulljava.lang.IllegalStateException
- 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 theupdate
methods has already been called for the active encryption/decryption operationjava.lang.UnsupportedOperationException
- if the implementationopensslEngine
doesn't support this operation.
-
updateAAD
public void updateAAD(java.nio.ByteBuffer aad) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, java.lang.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
update
anddoFinal
methods).- Specified by:
updateAAD
in interfaceCryptoCipher
- Parameters:
aad
- the buffer containing the Additional Authentication Data- Throws:
java.lang.IllegalArgumentException
- if theaad
byte array is nulljava.lang.IllegalStateException
- 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 theupdate
methods has already been called for the active encryption/decryption operationjava.lang.UnsupportedOperationException
- if the implementationopensslEngine
doesn't support this operation.
-
-