Skip to content

Commit 8c8dc35

Browse files
Remove frame-size restrictions. Issue #42
1 parent aaa11f6 commit 8c8dc35

File tree

5 files changed

+26
-27
lines changed

5 files changed

+26
-27
lines changed

src/main/java/com/amazonaws/encryptionsdk/AwsCrypto.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,7 @@ public void setEncryptionFrameSize(final int frameSize) {
137137
if (frameSize < 0) {
138138
throw new IllegalArgumentException("frameSize must be non-negative");
139139
}
140-
if (frameSize % 16 != 0) {
141-
// For compatibility reasons we'll still enforce this restriction for now.
142-
// TODO: Investigate whether this can be removed.
143-
throw new IllegalArgumentException("frameSize must be a multiple of 16");
144-
}
140+
145141
encryptionFrameSize_ = frameSize;
146142
}
147143

src/test/java/com/amazonaws/encryptionsdk/AwsCryptoTest.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ private void doEncryptDecryptWithParsedCiphertext(final int byteSize, final int
145145
@Test
146146
public void encryptDecrypt() {
147147
for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
148-
final int blockSize = cryptoAlg.getBlockSize();
149-
final int[] frameSizeToTest = { 0, blockSize, blockSize * 2, blockSize * 10,
150-
AwsCrypto.getDefaultFrameSize() };
148+
final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg);
151149

152150
for (int i = 0; i < frameSizeToTest.length; i++) {
153151
final int frameSize = frameSizeToTest[i];
@@ -175,9 +173,7 @@ public void encryptDecryptWithBadSignature() {
175173
if (cryptoAlg.getTrailingSignatureAlgo() == null) {
176174
continue;
177175
}
178-
final int blockSize = cryptoAlg.getBlockSize();
179-
final int[] frameSizeToTest = { 0, blockSize, blockSize * 2, blockSize * 10,
180-
AwsCrypto.getDefaultFrameSize() };
176+
final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg);
181177

182178
for (int i = 0; i < frameSizeToTest.length; i++) {
183179
final int frameSize = frameSizeToTest[i];
@@ -202,9 +198,7 @@ public void encryptDecryptWithBadSignature() {
202198
@Test
203199
public void encryptDecryptWithParsedCiphertext() {
204200
for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
205-
final int blockSize = cryptoAlg.getBlockSize();
206-
final int[] frameSizeToTest = { 0, blockSize, blockSize * 2, blockSize * 10,
207-
AwsCrypto.getDefaultFrameSize() };
201+
final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg);
208202

209203
for (int i = 0; i < frameSizeToTest.length; i++) {
210204
final int frameSize = frameSizeToTest[i];
@@ -336,9 +330,7 @@ private void doEstimateCiphertextSize(final CryptoAlgorithm cryptoAlg, final int
336330
@Test
337331
public void estimateCiphertextSize() {
338332
for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
339-
final int blockSize = cryptoAlg.getBlockSize();
340-
final int[] frameSizeToTest = { 0, blockSize, blockSize * 2, blockSize * 10,
341-
AwsCrypto.getDefaultFrameSize() };
333+
final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg);
342334

343335
for (int i = 0; i < frameSizeToTest.length; i++) {
344336
final int frameSize = frameSizeToTest[i];
@@ -664,10 +656,12 @@ public void setValidFrameSize() throws IOException {
664656
assertEquals(setFrameSize, getFrameSize);
665657
}
666658

667-
@Test(expected = IllegalArgumentException.class)
668-
public void unalignedFrameSizesAreRejected() throws IOException {
659+
660+
public void unalignedFrameSizesAreAccepted() throws IOException {
669661
final int frameSize = AwsCrypto.getDefaultCryptoAlgorithm().getBlockSize() - 1;
670662
encryptionClient_.setEncryptionFrameSize(frameSize);
663+
664+
assertEquals(frameSize, encryptionClient_.getEncryptionFrameSize());
671665
}
672666

673667
@Test(expected = IllegalArgumentException.class)

src/test/java/com/amazonaws/encryptionsdk/CryptoInputStreamTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ public static Collection<Object[]> encryptDecryptParams() {
158158
boolean firstAlgorithm = true;
159159

160160
for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
161-
final int blockSize = cryptoAlg.getBlockSize();
162-
final int[] frameSizeToTest = {
163-
0, blockSize, blockSize * 2, blockSize * 10,
164-
AwsCrypto.getDefaultFrameSize()
165-
};
161+
final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg);
166162

167163
// Our bytesToTest and readLenVals arrays tend to have the bigger numbers towards the end - we'll chop off
168164
// the last few as they take the longest and don't really add that much more coverage.

src/test/java/com/amazonaws/encryptionsdk/CryptoOutputStreamTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ public static Collection<Object[]> encryptDecryptParams() {
155155
ArrayList<Object[]> cases = new ArrayList<>();
156156

157157
for (final CryptoAlgorithm cryptoAlg : EnumSet.allOf(CryptoAlgorithm.class)) {
158-
final int blockSize = cryptoAlg.getBlockSize();
159-
final int[] frameSizeToTest = { 0, blockSize, blockSize * 2, blockSize * 10,
160-
AwsCrypto.getDefaultFrameSize() };
158+
final int[] frameSizeToTest = TestUtils.getFrameSizesToTest(cryptoAlg);
161159

162160
// iterate over frame size to test
163161
for (int i = 0; i < frameSizeToTest.length; i++) {

src/test/java/com/amazonaws/encryptionsdk/TestUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,19 @@ public static byte[] insecureRandomBytes(int length) {
159159
public static ByteArrayInputStream insecureRandomStream(int length) {
160160
return new ByteArrayInputStream(ensureRandomCached(length), 0, length);
161161
}
162+
163+
public static int[] getFrameSizesToTest(final CryptoAlgorithm cryptoAlg) {
164+
final int blockSize = cryptoAlg.getBlockSize();
165+
final int[] frameSizeToTest = {
166+
0,
167+
blockSize - 1,
168+
blockSize,
169+
blockSize + 1,
170+
blockSize * 2,
171+
blockSize * 10,
172+
blockSize * 10 + 1,
173+
AwsCrypto.getDefaultFrameSize()
174+
};
175+
return frameSizeToTest;
176+
}
162177
}

0 commit comments

Comments
 (0)