|
33 | 33 | import java.io.InputStream;
|
34 | 34 | import java.io.OutputStream;
|
35 | 35 | import java.nio.charset.StandardCharsets;
|
| 36 | +import java.util.Arrays; |
36 | 37 | import java.util.EnumSet;
|
37 | 38 | import java.util.HashMap;
|
38 | 39 | import java.util.Map;
|
@@ -150,6 +151,26 @@ private void doTamperedEncryptDecryptWithKeyring(final CryptoAlgorithm cryptoAlg
|
150 | 151 | .ciphertext(cipherText).build()));
|
151 | 152 | }
|
152 | 153 |
|
| 154 | + private void doTruncatedEncryptDecrypt(final CryptoAlgorithm cryptoAlg, final int byteSize, final int frameSize) { |
| 155 | + final byte[] plaintextBytes = new byte[byteSize]; |
| 156 | + |
| 157 | + final Map<String, String> encryptionContext = new HashMap<>(1); |
| 158 | + encryptionContext.put("ENC1", "Encrypt-decrypt test with %d" + byteSize); |
| 159 | + |
| 160 | + encryptionClient_.setEncryptionAlgorithm(cryptoAlg); |
| 161 | + encryptionClient_.setEncryptionFrameSize(frameSize); |
| 162 | + |
| 163 | + final byte[] cipherText = encryptionClient_.encryptData( |
| 164 | + masterKeyProvider, |
| 165 | + plaintextBytes, |
| 166 | + encryptionContext).getResult(); |
| 167 | + final byte[] truncatedCipherText = Arrays.copyOf(cipherText, cipherText.length - 1); |
| 168 | + |
| 169 | + assertThrows(BadCiphertextException.class, () -> encryptionClient_.decryptData( |
| 170 | + masterKeyProvider, |
| 171 | + truncatedCipherText)); |
| 172 | + } |
| 173 | + |
153 | 174 | private void doEncryptDecryptWithParsedCiphertext(final int byteSize, final int frameSize) {
|
154 | 175 | final byte[] plaintextBytes = new byte[byteSize];
|
155 | 176 |
|
@@ -235,6 +256,31 @@ public void encryptDecryptWithBadSignature() {
|
235 | 256 | }
|
236 | 257 | }
|
237 | 258 |
|
| 259 | + @Test |
| 260 | + public void encryptDecryptWithTruncatedCiphertext() { |
| 261 | + for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) { |
| 262 | + final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg); |
| 263 | + |
| 264 | + for (int i = 0; i < frameSizeToTest.length; i++) { |
| 265 | + final int frameSize = frameSizeToTest[i]; |
| 266 | + int[] bytesToTest = { 0, 1, frameSize - 1, frameSize, frameSize + 1, (int) (frameSize * 1.5), |
| 267 | + frameSize * 2, 1000000 }; |
| 268 | + |
| 269 | + for (int j = 0; j < bytesToTest.length; j++) { |
| 270 | + final int byteSize = bytesToTest[j]; |
| 271 | + |
| 272 | + if (byteSize > 500_000) { |
| 273 | + continue; |
| 274 | + } |
| 275 | + |
| 276 | + if (byteSize >= 0) { |
| 277 | + doTruncatedEncryptDecrypt(cryptoAlg, byteSize, frameSize); |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + |
238 | 284 | @Test
|
239 | 285 | public void encryptDecryptWithParsedCiphertext() {
|
240 | 286 | for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
|
|
0 commit comments