in src/main/java/com/amazonaws/encryptionsdk/internal/BlockEncryptionHandler.java [143:176]
private int writeEncryptedBlock(
final byte[] input, final int off, final int len, final byte[] out, final int outOff)
throws BadCiphertextException {
if (out.length == 0) {
return 0;
}
int outLen = 0;
final int seqNum = 1; // always 1 for single block case
final byte[] contentAad =
Utils.generateContentAad(messageId_, Constants.SINGLE_BLOCK_STRING_ID, seqNum, len);
final byte[] nonce = getNonce();
final byte[] encryptedBytes =
new CipherHandler(encryptionKey_, Cipher.ENCRYPT_MODE, cryptoAlgo_)
.cipherData(nonce, contentAad, input, off, len);
// create the cipherblock headers now for the encrypted data
final int encryptedContentLen = encryptedBytes.length - tagLenBytes_;
final CipherBlockHeaders cipherBlockHeaders =
new CipherBlockHeaders(nonce, encryptedContentLen);
final byte[] cipherBlockHeaderBytes = cipherBlockHeaders.toByteArray();
// assemble the headers and the encrypted bytes into a single block
System.arraycopy(
cipherBlockHeaderBytes, 0, out, outOff + outLen, cipherBlockHeaderBytes.length);
outLen += cipherBlockHeaderBytes.length;
System.arraycopy(encryptedBytes, 0, out, outOff + outLen, encryptedBytes.length);
outLen += encryptedBytes.length;
return outLen;
}