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 TypeMethodDescriptionint
doFinal(byte[] out, int outOff)
Finish encryption of the plaintext bytes.int
Return the size of the output buffer required for a call toCryptoHandler.doFinal(byte[], int)
.int
estimateOutputSize(int inLen)
Return the size of the output buffer required for aprocessBytes
plus adoFinal
with an input of inLen bytes.int
estimatePartialOutputSize(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 usedMasterKey
s.boolean
For decrypt and parsing flows returnstrue
when 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 fromin
putting the plaintext result intoout
.void
setMaxInputLength(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 fromin
putting 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:
processBytes
in 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:
doFinal
in 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 aprocessBytes
plus adoFinal
with an input of inLen bytes.- Specified by:
estimateOutputSize
in 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:CryptoHandler
Return 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:
estimatePartialOutputSize
in 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 sizeinLen
bytes.
-
estimateFinalOutputSize
public int estimateFinalOutputSize()Description copied from interface:CryptoHandler
Return 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:
estimateFinalOutputSize
in interfaceCryptoHandler
- Returns:
- the space required to accomodate a call to
CryptoHandler.doFinal(byte[], int)
-
getEncryptionContext
Return the encryption context.- Specified by:
getEncryptionContext
in interfaceMessageCryptoHandler
- Returns:
- the key-value map containing encryption context.
-
getHeaders
- Specified by:
getHeaders
in interfaceMessageCryptoHandler
-
setMaxInputLength
public void setMaxInputLength(long size)Description copied from interface:MessageCryptoHandler
Informs 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:
setMaxInputLength
in interfaceMessageCryptoHandler
- Parameters:
size
- An upper bound on the input data size.
-
getMasterKeys
Description copied from interface:MessageCryptoHandler
All usedMasterKey
s. For encryption flows, these are all theMasterKey
s used to protect the data. In the decryption flow, it is the singleMasterKey
actually used to decrypt the data.- Specified by:
getMasterKeys
in interfaceMessageCryptoHandler
-
isComplete
public boolean isComplete()Description copied from interface:CryptoHandler
For decrypt and parsing flows returnstrue
when 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:
isComplete
in interfaceCryptoHandler
-