|
4 | 4 | import org.junit.Test;
|
5 | 5 | import org.whispersystems.libsignal.InvalidMessageException;
|
6 | 6 | import org.whispersystems.libsignal.kdf.HKDFv3;
|
| 7 | +import org.whispersystems.signalservice.internal.crypto.PaddingInputStream; |
| 8 | +import org.whispersystems.signalservice.internal.push.http.AttachmentCipherOutputStreamFactory; |
7 | 9 | import org.whispersystems.signalservice.internal.util.Util;
|
8 | 10 |
|
| 11 | +import java.io.ByteArrayInputStream; |
9 | 12 | import java.io.ByteArrayOutputStream;
|
10 | 13 | import java.io.File;
|
11 | 14 | import java.io.FileOutputStream;
|
@@ -103,6 +106,43 @@ public void attachment_decryptFailOnBadDigest() throws IOException{
|
103 | 106 | assertTrue(hitCorrectException);
|
104 | 107 | }
|
105 | 108 |
|
| 109 | + @Test |
| 110 | + public void attachment_encryptDecryptPaddedContent() throws IOException, InvalidMessageException { |
| 111 | + int[] lengths = { 531, 600, 724, 1019, 1024 }; |
| 112 | + |
| 113 | + for (int length : lengths) { |
| 114 | + byte[] plaintextInput = new byte[length]; |
| 115 | + |
| 116 | + for (int i = 0; i < length; i++) { |
| 117 | + plaintextInput[i] = (byte) 0x97; |
| 118 | + } |
| 119 | + |
| 120 | + byte[] key = Util.getSecretBytes(64); |
| 121 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(plaintextInput); |
| 122 | + InputStream dataStream = new PaddingInputStream(inputStream, length); |
| 123 | + ByteArrayOutputStream encryptedStream = new ByteArrayOutputStream(); |
| 124 | + DigestingOutputStream digestStream = new AttachmentCipherOutputStreamFactory(key, null).createFor(encryptedStream); |
| 125 | + |
| 126 | + Util.copy(dataStream, digestStream); |
| 127 | + digestStream.flush(); |
| 128 | + |
| 129 | + byte[] digest = digestStream.getTransmittedDigest(); |
| 130 | + byte[] encryptedData = encryptedStream.toByteArray(); |
| 131 | + |
| 132 | + encryptedStream.close(); |
| 133 | + inputStream.close(); |
| 134 | + |
| 135 | + File cipherFile = writeToFile(encryptedData); |
| 136 | + |
| 137 | + InputStream decryptedStream = AttachmentCipherInputStream.createForAttachment(cipherFile, length, key, digest); |
| 138 | + byte[] plaintextOutput = readInputStreamFully(decryptedStream); |
| 139 | + |
| 140 | + assertArrayEquals(plaintextInput, plaintextOutput); |
| 141 | + |
| 142 | + cipherFile.delete(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
106 | 146 | @Test
|
107 | 147 | public void attachment_decryptFailOnNullDigest() throws IOException {
|
108 | 148 | File cipherFile = null;
|
|
0 commit comments