|
32 | 32 | import java.io.InputStream;
|
33 | 33 | import java.io.OutputStream;
|
34 | 34 | import java.nio.charset.StandardCharsets;
|
| 35 | +import java.util.Arrays; |
35 | 36 | import java.util.EnumSet;
|
36 | 37 | import java.util.HashMap;
|
37 | 38 | import java.util.Map;
|
@@ -112,6 +113,31 @@ private void doTamperedEncryptDecrypt(final CryptoAlgorithm cryptoAlg, final int
|
112 | 113 | }
|
113 | 114 | }
|
114 | 115 |
|
| 116 | + private void doTruncatedEncryptDecrypt(final CryptoAlgorithm cryptoAlg, final int byteSize, final int frameSize) { |
| 117 | + final byte[] plaintextBytes = new byte[byteSize]; |
| 118 | + |
| 119 | + final Map<String, String> encryptionContext = new HashMap<>(1); |
| 120 | + encryptionContext.put("ENC1", "Encrypt-decrypt test with %d" + byteSize); |
| 121 | + |
| 122 | + encryptionClient_.setEncryptionAlgorithm(cryptoAlg); |
| 123 | + encryptionClient_.setEncryptionFrameSize(frameSize); |
| 124 | + |
| 125 | + final byte[] cipherText = encryptionClient_.encryptData( |
| 126 | + masterKeyProvider, |
| 127 | + plaintextBytes, |
| 128 | + encryptionContext).getResult(); |
| 129 | + final byte[] truncatedCipherText = Arrays.copyOf(cipherText, cipherText.length - 1); |
| 130 | + try { |
| 131 | + encryptionClient_.decryptData( |
| 132 | + masterKeyProvider, |
| 133 | + truncatedCipherText |
| 134 | + ).getResult(); |
| 135 | + Assert.fail("Expected BadCiphertextException"); |
| 136 | + } catch (final BadCiphertextException ex) { |
| 137 | + // Expected exception |
| 138 | + } |
| 139 | + } |
| 140 | + |
115 | 141 | private void doEncryptDecryptWithParsedCiphertext(final int byteSize, final int frameSize) {
|
116 | 142 | final byte[] plaintextBytes = new byte[byteSize];
|
117 | 143 |
|
@@ -195,6 +221,31 @@ public void encryptDecryptWithBadSignature() {
|
195 | 221 | }
|
196 | 222 | }
|
197 | 223 |
|
| 224 | + @Test |
| 225 | + public void encryptDecryptWithTruncatedCiphertext() { |
| 226 | + for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) { |
| 227 | + final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg); |
| 228 | + |
| 229 | + for (int i = 0; i < frameSizeToTest.length; i++) { |
| 230 | + final int frameSize = frameSizeToTest[i]; |
| 231 | + int[] bytesToTest = { 0, 1, frameSize - 1, frameSize, frameSize + 1, (int) (frameSize * 1.5), |
| 232 | + frameSize * 2, 1000000 }; |
| 233 | + |
| 234 | + for (int j = 0; j < bytesToTest.length; j++) { |
| 235 | + final int byteSize = bytesToTest[j]; |
| 236 | + |
| 237 | + if (byteSize > 500_000) { |
| 238 | + continue; |
| 239 | + } |
| 240 | + |
| 241 | + if (byteSize >= 0) { |
| 242 | + doTruncatedEncryptDecrypt(cryptoAlg, byteSize, frameSize); |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + |
198 | 249 | @Test
|
199 | 250 | public void encryptDecryptWithParsedCiphertext() {
|
200 | 251 | for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
|
|
0 commit comments