diff --git a/src/main/java/com/amazonaws/encryptionsdk/internal/EncryptionHandler.java b/src/main/java/com/amazonaws/encryptionsdk/internal/EncryptionHandler.java index ea144af73..6f012353f 100644 --- a/src/main/java/com/amazonaws/encryptionsdk/internal/EncryptionHandler.java +++ b/src/main/java/com/amazonaws/encryptionsdk/internal/EncryptionHandler.java @@ -271,12 +271,19 @@ public int doFinal(final byte[] out, final int outOff) throws BadCiphertextExcep throw new IllegalStateException("Attempted to call doFinal twice"); } + int written = 0; + if (firstOperation_) { + // An empty message still needs its header written and included in the signature. + written = processBytes(new byte[0], 0, 0, out, outOff).getBytesWritten(); + } + complete_ = true; checkPlaintextSizeLimit(0); - int written = contentCryptoHandler_.doFinal(out, outOff); - updateTrailingSignature(out, outOff, written); + final int finalWritten = contentCryptoHandler_.doFinal(out, outOff + written); + updateTrailingSignature(out, outOff + written, finalWritten); + written += finalWritten; if (cryptoAlgo_.getTrailingSignatureLength() > 0) { try { CiphertextFooters footer = new CiphertextFooters(signContent()); diff --git a/src/test/java/com/amazonaws/encryptionsdk/CryptoOutputStreamTest.java b/src/test/java/com/amazonaws/encryptionsdk/CryptoOutputStreamTest.java index e0e6db8dc..24698d481 100644 --- a/src/test/java/com/amazonaws/encryptionsdk/CryptoOutputStreamTest.java +++ b/src/test/java/com/amazonaws/encryptionsdk/CryptoOutputStreamTest.java @@ -457,7 +457,6 @@ public void checkInvalidValues() throws Exception { final int frameSize = frameSizeToTest[i]; invalidWriteLen(frameSize); invalidWriteOffset(frameSize); - noOpStream(frameSize); } } @@ -494,22 +493,39 @@ private void invalidWriteOffset(final int frameSize) () -> encryptionOutStream.write(writeBytes, writeBytes.length + 1, writeBytes.length)); } - private void noOpStream(final int frameSize) throws Exception { + @Test + public void closeWithoutWrite() throws Exception { + for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) { + for (final int frameSize : new int[] {0, getDefaultFrameSize()}) { + noOpStream(cryptoAlg, frameSize); + } + } + } + + private void noOpStream(final CryptoAlgorithm cryptoAlg, final int frameSize) throws Exception { + final AwsCrypto awsCrypto = + AwsCrypto.builder() + .withCommitmentPolicy( + cryptoAlg.isCommitting() + ? CommitmentPolicy.RequireEncryptRequireDecrypt + : CommitmentPolicy.ForbidEncryptAllowDecrypt) + .withEncryptionAlgorithm(cryptoAlg) + .withEncryptionFrameSize(frameSize) + .build(); final Map encryptionContext = new HashMap(1); encryptionContext.put("ENC", "noOpStream size " + frameSize); final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); final OutputStream encryptionOutStream = - encryptionClient_.createEncryptingStream(customerMasterKey, outStream, encryptionContext); + awsCrypto.createEncryptingStream(customerMasterKey, outStream, encryptionContext); encryptionOutStream.close(); - testRoundTrip( - 0, - crypto -> crypto.setEncryptionFrameSize(frameSize), - encryptWithContext(encryptionContext), - basicDecrypt(), - CommitmentPolicy.RequireEncryptRequireDecrypt); + final CryptoResult result = + awsCrypto.decryptData(customerMasterKey, outStream.toByteArray()); + assertArrayEquals(new byte[0], result.getResult()); + assertEquals(cryptoAlg, result.getCryptoAlgorithm()); + assertEquals(encryptionContext.get("ENC"), result.getEncryptionContext().get("ENC")); } @Test