Top Fields Constructors Methods
org.conscrypt

public abstract Class OpenSSLAeadCipher

extends OpenSSLCipher
Class Inheritance
Known Direct Subclasses
org.conscrypt.OpenSSLAeadCipherChaCha20, org.conscrypt.OpenSSLAeadCipherAES
Annotations
@Internal
Imports
java.lang.reflect.Constructor, .InvocationTargetException, java.nio.ByteBuffer, java.security.InvalidAlgorithmParameterException, .InvalidKeyException, .SecureRandom, java.security.spec.AlgorithmParameterSpec, java.util.Arrays, javax.crypto.BadPaddingException, .IllegalBlockSizeException, .NoSuchPaddingException, .ShortBufferException, javax.crypto.spec.IvParameterSpec

Field Summary

Modifier and TypeField and Description
private byte[]
aad

Additional authenticated data.

pack-priv byte[]
buf

The byte array containing the bytes written.

pack-priv int
bufCount

The number of bytes written.

pack-priv static final int
DEFAULT_TAG_SIZE_BITS

The default tag size when one is not specified.

private static final boolean
ENABLE_BYTEBUFFER_OPTIMIZATIONS

Controls whether no-copy optimizations for direct ByteBuffers are enabled.

pack-priv long
evpAead

AEAD cipher reference.

private static int
lastGlobalMessageSize

Keeps track of the last used block size.

private boolean
mustInitialize

When set this instance must be initialized before use again.

private byte[]
previousIv

The previously used nonce (IV) to prevent key + nonce reuse.

private byte[]
previousKey

The previously used key to prevent key + nonce (IV) reuse.

pack-priv int
tagLengthInBytes

The length of the AEAD cipher tag in bytes.

Inherited from org.conscrypt.OpenSSLCipher:
encodedKeyivmode

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean
allowsNonceReuse()

Returns whether reusing nonces is allowed (aka, whether this is nonce misuse-resistant).

private boolean
arraysAreEqual(byte[] a, byte[] b)

Constant-time array comparison.

private void
pack-priv void
checkSupportedPadding(OpenSSLCipher.Padding padding)

Implements abstract org.conscrypt.OpenSSLCipher.checkSupportedPadding.

Checks whether the cipher supports this particular cipher padding and throws NoSuchPaddingException if it doesn't.
pack-priv void
checkSupportedTagLength(int tagLenBits)

pack-priv int
pack-priv int
doFinalInternal(byte[] output, int outputOffset, int maximumLen)

Implements abstract org.conscrypt.OpenSSLCipher.doFinalInternal.

API-specific implementation of the final block.
protected int
engineDoFinal(ByteBuffer
the input ByteBuffer
input
,
ByteBuffer
the output ByteBuffer
output
)

Overrides javax.crypto.CipherSpi.engineDoFinal.

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
protected int
engineDoFinal(byte[]
the input buffer
input
,
int
the offset in input where the input starts
inputOffset
,
int
the input length
inputLen
,
byte[]
the buffer for the result
output
,
int
the offset in output where the result is stored
outputOffset
)

Overrides org.conscrypt.OpenSSLCipher.engineDoFinal.

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
pack-priv void
engineInitInternal(byte[] encodedKey, AlgorithmParameterSpec params, SecureRandom random)

Implements abstract org.conscrypt.OpenSSLCipher.engineInitInternal.

API-specific implementation of initializing the cipher.
protected void
engineUpdateAAD(byte[]
the buffer containing the AAD
input
,
int
the offset in src where the AAD input starts
inputOffset
,
int
the number of AAD bytes
inputLen
)

Overrides javax.crypto.CipherSpi.engineUpdateAAD.

Continues a multipart update of the Additional Authentication Data (AAD), using a subset of the provided buffer.
protected void
engineUpdateAAD(ByteBuffer
the buffer containing the AAD
buf
)

Overrides javax.crypto.CipherSpi.engineUpdateAAD.

Continues a multipart update of the Additional Authentication Data (AAD).
private void
expand(int i)

pack-priv abstract long
getEVP_AEAD(int keyLength)

pack-priv int
getOutputSizeForFinal(int inputLen)

Implements abstract org.conscrypt.OpenSSLCipher.getOutputSizeForFinal.

The size of output if doFinal() is called with this inputLen.
pack-priv int
getOutputSizeForUpdate(int inputLen)

Implements abstract org.conscrypt.OpenSSLCipher.getOutputSizeForUpdate.

AEAD buffers everything until a final output.
private void
private void
pack-priv int
updateInternal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset, int maximumLen)

Implements abstract org.conscrypt.OpenSSLCipher.updateInternal.

API-specific implementation of updating the cipher.
Inherited from org.conscrypt.OpenSSLCipher:
checkSupportedKeySizecheckSupportedModeengineDoFinalengineGetBlockSizeengineGetIVengineGetKeySizeengineGetOutputSizeengineGetParametersengineInitengineInitengineInitengineSetModeengineSetPaddingengineUnwrapengineUpdateengineUpdateengineWrapgetBaseCipherNamegetCipherBlockSizegetPaddinggetParameterSpecisEncryptingsupportsVariableSizeIvsupportsVariableSizeKey

Field Detail

aadback to summary
private byte[] aad

Additional authenticated data.

bufback to summary
pack-priv byte[] buf

The byte array containing the bytes written.

bufCountback to summary
pack-priv int bufCount

The number of bytes written.

DEFAULT_TAG_SIZE_BITSback to summary
pack-priv static final int DEFAULT_TAG_SIZE_BITS

The default tag size when one is not specified. Default to full-length tags (128-bits or 16 octets).

ENABLE_BYTEBUFFER_OPTIMIZATIONSback to summary
private static final boolean ENABLE_BYTEBUFFER_OPTIMIZATIONS

Controls whether no-copy optimizations for direct ByteBuffers are enabled.

evpAeadback to summary
pack-priv long evpAead

AEAD cipher reference.

lastGlobalMessageSizeback to summary
private static int lastGlobalMessageSize

Keeps track of the last used block size.

mustInitializeback to summary
private boolean mustInitialize

When set this instance must be initialized before use again. This prevents key and IV reuse.

previousIvback to summary
private byte[] previousIv

The previously used nonce (IV) to prevent key + nonce reuse.

previousKeyback to summary
private byte[] previousKey

The previously used key to prevent key + nonce (IV) reuse.

tagLengthInBytesback to summary
pack-priv int tagLengthInBytes

The length of the AEAD cipher tag in bytes.

Constructor Detail

OpenSSLAeadCipherback to summary
public OpenSSLAeadCipher(OpenSSLCipher.Mode mode)

Method Detail

allowsNonceReuseback to summary
pack-priv boolean allowsNonceReuse()

Returns whether reusing nonces is allowed (aka, whether this is nonce misuse-resistant). Most AEAD ciphers are not, but some are specially constructed so that reusing a key/nonce pair is safe.

arraysAreEqualback to summary
private boolean arraysAreEqual(byte[] a, byte[] b)

Constant-time array comparison. Since we are using this to compare keys, we want to ensure there's no opportunity for a timing attack.

checkInitializationback to summary
private void checkInitialization()
checkSupportedPaddingback to summary
pack-priv void checkSupportedPadding(OpenSSLCipher.Padding padding) throws NoSuchPaddingException

Implements abstract org.conscrypt.OpenSSLCipher.checkSupportedPadding.

Doc from org.conscrypt.OpenSSLCipher.checkSupportedPadding.

Checks whether the cipher supports this particular cipher padding and throws NoSuchPaddingException if it doesn't.

Annotations
@Override
checkSupportedTagLengthback to summary
pack-priv void checkSupportedTagLength(int tagLenBits) throws InvalidAlgorithmParameterException
doFinalInternalback to summary
pack-priv int doFinalInternal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException
doFinalInternalback to summary
pack-priv int doFinalInternal(byte[] output, int outputOffset, int maximumLen) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException

Implements abstract org.conscrypt.OpenSSLCipher.doFinalInternal.

Doc from org.conscrypt.OpenSSLCipher.doFinalInternal.

API-specific implementation of the final block. The maximumLen will be the maximum length of the possible output as returned by getOutputSizeForFinal(int). The return value must be the number of bytes processed and placed into output. On error, an exception must be thrown.

Annotations
@Override
engineDoFinalback to summary
protected int engineDoFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException

Overrides javax.crypto.CipherSpi.engineDoFinal.

Doc from javax.crypto.CipherSpi.engineDoFinal.

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 CipherSpi object was initialized.

All input.remaining() bytes starting at input.position() are processed. If an AEAD mode such as GCM or CCM is being used, the authentication tag is appended in the case of encryption, or verified in the case of decryption. 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, 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, a ShortBufferException is thrown.

Upon finishing, this method resets this CipherSpi object to the state it was in when previously initialized via a call to engineInit. That is, the object is reset and available to encrypt or decrypt (depending on the operation mode that was specified in the call to engineInit more data.

Note

if any exception is thrown, this CipherSpi object may need to be reset before it can be used again.

Subclasses should consider overriding this method if they can process ByteBuffers more efficiently than byte arrays.

Parameters
input:ByteBuffer

the input ByteBuffer

output:ByteBuffer

the output ByteBuffer

Returns:int

the number of bytes stored in output

Annotations
@Override
Exceptions
ShortBufferException:
if there is insufficient space in the output buffer
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 CipherSpi object is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes
engineDoFinalback to summary
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException

Overrides org.conscrypt.OpenSSLCipher.engineDoFinal.

Doc from javax.crypto.CipherSpi.engineDoFinal.

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 CipherSpi object was initialized.

The first inputLen bytes in the input buffer, starting at inputOffset inclusive, and any input bytes that may have been buffered during a previous update operation, are processed, with padding (if requested) being applied. If an AEAD mode such as GCM or CCM is being used, the authentication tag is appended in the case of encryption, or verified in the case of decryption. The result is stored in the output buffer, starting at outputOffset inclusive.

If the output buffer is too small to hold the result, a ShortBufferException is thrown.

Upon finishing, this method resets this CipherSpi object to the state it was in when previously initialized via a call to engineInit. That is, the object is reset and available to encrypt or decrypt (depending on the operation mode that was specified in the call to engineInit) more data.

Note

if any exception is thrown, this CipherSpi object may need to be reset before it can be used again.

Parameters
input:byte[]

the input buffer

inputOffset:int

the offset in input where the input starts

inputLen:int

the input length

output:byte[]

the buffer for the result

outputOffset:int

the offset in output where the result is stored

Returns:int

the number of bytes stored in output

Annotations
@Override
Exceptions
ShortBufferException:
if the given output buffer 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 CipherSpi object is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes
engineInitInternalback to summary
pack-priv void engineInitInternal(byte[] encodedKey, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException

Implements abstract org.conscrypt.OpenSSLCipher.engineInitInternal.

Doc from org.conscrypt.OpenSSLCipher.engineInitInternal.

API-specific implementation of initializing the cipher. The isEncrypting() function will tell whether it should be initialized for encryption or decryption. The encodedKey will be the bytes of a supported key size.

Annotations
@Override
engineUpdateAADback to summary
protected void engineUpdateAAD(byte[] input, int inputOffset, int inputLen)

Overrides javax.crypto.CipherSpi.engineUpdateAAD.

Doc from javax.crypto.CipherSpi.engineUpdateAAD.

Continues a multipart update of the Additional Authentication Data (AAD), using a subset of the provided buffer.

Calls to this method provide AAD to the cipher when operating in modes such as AEAD (GCM or CCM). If this cipher is operating in either GCM or CCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the update and doFinal methods).

Parameters
input:byte[]

the buffer containing the AAD

inputOffset:int

the offset in src where the AAD input starts

inputLen:int

the number of AAD bytes

Annotations
@SuppressWarnings:MissingOverride
engineUpdateAADback to summary
protected void engineUpdateAAD(ByteBuffer buf)

Overrides javax.crypto.CipherSpi.engineUpdateAAD.

Doc from javax.crypto.CipherSpi.engineUpdateAAD.

Continues a multipart update of the Additional Authentication Data (AAD).

Calls to this method provide AAD to the cipher when operating in modes such as AEAD (GCM or CCM). If this cipher is operating in either GCM or CCM mode, all AAD must be supplied before beginning operations on the ciphertext (via the update and doFinal methods).

All src.remaining() bytes starting at src.position() are processed. Upon return, the input buffer's position will be equal to its limit; its limit will not have changed.

Parameters
buf:ByteBuffer

the buffer containing the AAD

Annotations
@SuppressWarnings:MissingOverride
expandback to summary
private void expand(int i)
getEVP_AEADback to summary
pack-priv abstract long getEVP_AEAD(int keyLength) throws InvalidKeyException
getOutputSizeForFinalback to summary
pack-priv int getOutputSizeForFinal(int inputLen)

Implements abstract org.conscrypt.OpenSSLCipher.getOutputSizeForFinal.

Doc from org.conscrypt.OpenSSLCipher.getOutputSizeForFinal.

The size of output if doFinal() is called with this inputLen. If padding is enabled and the size of the input puts it right at the block size, it will add another block for the padding.

Annotations
@Override
getOutputSizeForUpdateback to summary
pack-priv int getOutputSizeForUpdate(int inputLen)

Implements abstract org.conscrypt.OpenSSLCipher.getOutputSizeForUpdate.

AEAD buffers everything until a final output.

Annotations
@Override
resetback to summary
private void reset()
throwAEADBadTagExceptionIfAvailableback to summary
private void throwAEADBadTagExceptionIfAvailable(String message, Throwable cause) throws BadPaddingException
Annotations
@SuppressWarnings:LiteralClassName
updateInternalback to summary
pack-priv int updateInternal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset, int maximumLen) throws ShortBufferException

Implements abstract org.conscrypt.OpenSSLCipher.updateInternal.

Doc from org.conscrypt.OpenSSLCipher.updateInternal.

API-specific implementation of updating the cipher. The maximumLen will be the maximum length of the output as returned by getOutputSizeForUpdate(int). The return value must be the number of bytes processed and placed into output. On error, an exception must be thrown.

Annotations
@Override