|
19 | 19 |
|
20 | 20 | import com.amazonaws.encryptionsdk.AwsCrypto;
|
21 | 21 | import com.amazonaws.encryptionsdk.CryptoAlgorithm;
|
| 22 | +import com.amazonaws.encryptionsdk.CryptoInputStream; |
| 23 | +import com.amazonaws.encryptionsdk.CryptoOutputStream; |
| 24 | +import com.amazonaws.encryptionsdk.CryptoResult; |
22 | 25 | import com.amazonaws.encryptionsdk.TestUtils;
|
23 | 26 | import com.amazonaws.encryptionsdk.model.CipherFrameHeaders;
|
| 27 | +import java.io.ByteArrayInputStream; |
| 28 | +import java.io.ByteArrayOutputStream; |
24 | 29 | import java.lang.reflect.Field;
|
| 30 | +import java.nio.ByteBuffer; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.security.SecureRandom; |
| 33 | +import java.util.Collections; |
25 | 34 | import javax.crypto.SecretKey;
|
26 | 35 | import javax.crypto.spec.SecretKeySpec;
|
27 | 36 | import org.bouncycastle.util.encoders.Hex;
|
28 | 37 | import org.junit.Before;
|
29 | 38 | import org.junit.Test;
|
| 39 | +import software.amazon.awssdk.utils.StringUtils; |
| 40 | +import software.amazon.cryptography.materialproviders.IKeyring; |
| 41 | +import software.amazon.cryptography.materialproviders.MaterialProviders; |
| 42 | +import software.amazon.cryptography.materialproviders.model.AesWrappingAlg; |
| 43 | +import software.amazon.cryptography.materialproviders.model.CreateRawAesKeyringInput; |
| 44 | +import software.amazon.cryptography.materialproviders.model.MaterialProvidersConfig; |
30 | 45 |
|
31 | 46 | public class FrameEncryptionHandlerTest {
|
32 | 47 | private final CryptoAlgorithm cryptoAlgorithm_ = TestUtils.DEFAULT_TEST_CRYPTO_ALG;
|
@@ -117,4 +132,86 @@ private void assertHeaderNonce(byte[] expectedNonce, byte[] buf) {
|
117 | 132 | private void generateTestBlock(byte[] buf) {
|
118 | 133 | frameEncryptionHandler_.processBytes(new byte[frameSize_], 0, frameSize_, buf, 0);
|
119 | 134 | }
|
| 135 | + |
| 136 | + /** |
| 137 | + * This isn't a unit test, but it reproduces a bug in the FrameEncryptionHandler where the stream |
| 138 | + * would be truncated when the offset is >0 |
| 139 | + * |
| 140 | + * @throws Exception |
| 141 | + */ |
| 142 | + @Test |
| 143 | + public void testStreamTruncation() throws Exception { |
| 144 | + // Initialize AES key and keyring |
| 145 | + SecureRandom rnd = new SecureRandom(); |
| 146 | + byte[] rawKey = new byte[16]; |
| 147 | + rnd.nextBytes(rawKey); |
| 148 | + SecretKeySpec cryptoKey = new SecretKeySpec(rawKey, "AES"); |
| 149 | + MaterialProviders materialProviders = |
| 150 | + MaterialProviders.builder() |
| 151 | + .MaterialProvidersConfig(MaterialProvidersConfig.builder().build()) |
| 152 | + .build(); |
| 153 | + CreateRawAesKeyringInput keyringInput = |
| 154 | + CreateRawAesKeyringInput.builder() |
| 155 | + .wrappingKey(ByteBuffer.wrap(cryptoKey.getEncoded())) |
| 156 | + .keyNamespace("Example") |
| 157 | + .keyName("RandomKey") |
| 158 | + .wrappingAlg(AesWrappingAlg.ALG_AES128_GCM_IV12_TAG16) |
| 159 | + .build(); |
| 160 | + IKeyring keyring = materialProviders.CreateRawAesKeyring(keyringInput); |
| 161 | + AwsCrypto crypto = AwsCrypto.standard(); |
| 162 | + |
| 163 | + String testDataString = StringUtils.repeat("Hello, World! ", 5_000); |
| 164 | + |
| 165 | + int startOffset = 100; // The data will start from this offset |
| 166 | + byte[] inputDataWithOffset = new byte[10_000]; |
| 167 | + // the length of the actual data |
| 168 | + int dataLength = inputDataWithOffset.length - startOffset; |
| 169 | + // copy some data, starting at the startOffset |
| 170 | + // so the first |startOffset| bytes are 0s |
| 171 | + System.arraycopy( |
| 172 | + testDataString.getBytes(StandardCharsets.UTF_8), |
| 173 | + 0, |
| 174 | + inputDataWithOffset, |
| 175 | + startOffset, |
| 176 | + dataLength); |
| 177 | + // decryptData (non-streaming) doesn't know about the offset |
| 178 | + // it will strip out the original 0s |
| 179 | + byte[] expectedOutput = new byte[10_000 - startOffset]; |
| 180 | + System.arraycopy( |
| 181 | + testDataString.getBytes(StandardCharsets.UTF_8), 0, expectedOutput, 0, dataLength); |
| 182 | + |
| 183 | + // Encrypt the data |
| 184 | + byte[] encryptedData; |
| 185 | + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { |
| 186 | + try (CryptoOutputStream cryptoOutput = |
| 187 | + crypto.createEncryptingStream(keyring, os, Collections.emptyMap())) { |
| 188 | + cryptoOutput.write(inputDataWithOffset, startOffset, dataLength); |
| 189 | + } |
| 190 | + encryptedData = os.toByteArray(); |
| 191 | + } |
| 192 | + |
| 193 | + // Check non-streaming decrypt |
| 194 | + CryptoResult<byte[], ?> nonStreamDecrypt = crypto.decryptData(keyring, encryptedData); |
| 195 | + assertEquals(dataLength, nonStreamDecrypt.getResult().length); |
| 196 | + assertArrayEquals(expectedOutput, nonStreamDecrypt.getResult()); |
| 197 | + |
| 198 | + // Check streaming decrypt |
| 199 | + int decryptedLength = 0; |
| 200 | + byte[] decryptedData = new byte[inputDataWithOffset.length]; |
| 201 | + try (ByteArrayInputStream is = new ByteArrayInputStream(encryptedData); |
| 202 | + CryptoInputStream cryptoInput = crypto.createDecryptingStream(keyring, is)) { |
| 203 | + int offset = startOffset; |
| 204 | + do { |
| 205 | + int bytesRead = cryptoInput.read(decryptedData, offset, decryptedData.length - offset); |
| 206 | + if (bytesRead <= 0) { |
| 207 | + break; // End of stream |
| 208 | + } |
| 209 | + offset += bytesRead; |
| 210 | + decryptedLength += bytesRead; |
| 211 | + } while (true); |
| 212 | + } |
| 213 | + assertEquals(dataLength, decryptedLength); |
| 214 | + // These arrays will be offset, i.e. the first |startOffset| bytes are 0s |
| 215 | + assertArrayEquals(inputDataWithOffset, decryptedData); |
| 216 | + } |
120 | 217 | }
|
0 commit comments