Class EncryptionHandler
- All Implemented Interfaces:
CryptoHandler,MessageCryptoHandler
This class creates the ciphertext headers and delegates the encryption of the plaintext to the
BlockEncryptionHandler or FrameEncryptionHandler based on the content type.
-
Constructor Summary
ConstructorsConstructorDescriptionEncryptionHandler(int frameSize, EncryptionMaterials result, CommitmentPolicy commitmentPolicy)Create an encryption handler using the provided master key and encryption context. -
Method Summary
Modifier and TypeMethodDescriptionintdoFinal(byte[] out, int outOff)Finish encryption of the plaintext bytes.intReturn the size of the output buffer required for a call toCryptoHandler.doFinal(byte[], int).intestimateOutputSize(int inLen)Return the size of the output buffer required for aprocessBytesplus adoFinalwith an input of inLen bytes.intestimatePartialOutputSize(int inLen)Return the size of the output buffer required for a call toCryptoHandler.processBytes(byte[], int, int, byte[], int).Return the encryption context.All usedMasterKeys.booleanFor decrypt and parsing flows returnstruewhen this has handled as many bytes as it can.processBytes(byte[] in, int off, int len, byte[] out, int outOff)Encrypt a block of bytes frominputting the plaintext result intoout.voidsetMaxInputLength(long size)Informs this handler of an upper bound on the input data size.
-
Constructor Details
-
EncryptionHandler
public EncryptionHandler(int frameSize, EncryptionMaterials result, CommitmentPolicy commitmentPolicy) throws AwsCryptoExceptionCreate an encryption handler using the provided master key and encryption context.- Parameters:
frameSize- The encryption frame size, or zero for a one-shot encryption taskresult- The EncryptionMaterials with the crypto materials for this encryption job- Throws:
AwsCryptoException- if the encryption context or master key is null.
-
-
Method Details
-
processBytes
public ProcessingSummary processBytes(byte[] in, int off, int len, byte[] out, int outOff) throws AwsCryptoException, BadCiphertextExceptionEncrypt a block of bytes frominputting the plaintext result intoout.It encrypts by performing the following operations:
- if this is the first call to encrypt, write the ciphertext headers to the output being returned.
- else, pass off the input data to underlying content cryptohandler.
- Specified by:
processBytesin interfaceCryptoHandler- Parameters:
in- the input byte array.off- the offset into the in array where the data to be encrypted starts.len- the number of bytes to be encrypted.out- the output buffer the encrypted bytes go into.outOff- the offset into the output byte array the encrypted data starts at.- Returns:
- the number of bytes written to out and processed
- Throws:
AwsCryptoException- if len or offset values are negative.BadCiphertextException- thrown by the underlying cipher handler.
-
doFinal
Finish encryption of the plaintext bytes.- Specified by:
doFinalin interfaceCryptoHandler- Parameters:
out- space for any resulting output data.outOff- offset into out to start copying the data at.- Returns:
- number of bytes written into out.
- Throws:
BadCiphertextException- thrown by the underlying cipher handler.
-
estimateOutputSize
public int estimateOutputSize(int inLen)Return the size of the output buffer required for aprocessBytesplus adoFinalwith an input of inLen bytes.- Specified by:
estimateOutputSizein interfaceCryptoHandler- Parameters:
inLen- the length of the input.- Returns:
- the space required to accommodate a call to processBytes and doFinal with len bytes of input.
-
estimatePartialOutputSize
public int estimatePartialOutputSize(int inLen)Description copied from interface:CryptoHandlerReturn the size of the output buffer required for a call toCryptoHandler.processBytes(byte[], int, int, byte[], int).Note this method is allowed to return an estimation of the output size that is greater than the actual size of the output. Returning an estimate that is lesser than the actual size of the output will result in underflow exceptions.
- Specified by:
estimatePartialOutputSizein interfaceCryptoHandler- Parameters:
inLen- the length of the input.- Returns:
- the space required to accommodate a call to
CryptoHandler.processBytes(byte[], int, int, byte[], int)with an input of sizeinLenbytes.
-
estimateFinalOutputSize
public int estimateFinalOutputSize()Description copied from interface:CryptoHandlerReturn the size of the output buffer required for a call toCryptoHandler.doFinal(byte[], int).Note this method is allowed to return an estimation of the output size that is greater than the actual size of the output. Returning an estimate that is lesser than the actual size of the output will result in underflow exceptions.
- Specified by:
estimateFinalOutputSizein interfaceCryptoHandler- Returns:
- the space required to accomodate a call to
CryptoHandler.doFinal(byte[], int)
-
getEncryptionContext
Return the encryption context.- Specified by:
getEncryptionContextin interfaceMessageCryptoHandler- Returns:
- the key-value map containing encryption context.
-
getHeaders
- Specified by:
getHeadersin interfaceMessageCryptoHandler
-
setMaxInputLength
public void setMaxInputLength(long size)Description copied from interface:MessageCryptoHandlerInforms this handler of an upper bound on the input data size. The handler will throw an exception if this bound is exceeded, and may use it to perform performance optimizations as well.If this method is called multiple times, the smallest bound will be used.
- Specified by:
setMaxInputLengthin interfaceMessageCryptoHandler- Parameters:
size- An upper bound on the input data size.
-
getMasterKeys
Description copied from interface:MessageCryptoHandlerAll usedMasterKeys. For encryption flows, these are all theMasterKeys used to protect the data. In the decryption flow, it is the singleMasterKeyactually used to decrypt the data.- Specified by:
getMasterKeysin interfaceMessageCryptoHandler
-
isComplete
public boolean isComplete()Description copied from interface:CryptoHandlerFor decrypt and parsing flows returnstruewhen this has handled as many bytes as it can. This usually means that it has reached the end of an object, file, or other delimited stream.- Specified by:
isCompletein interfaceCryptoHandler
-