|
7 | 7 | import org.bouncycastle.crypto.params.AEADParameters; |
8 | 8 | import org.bouncycastle.crypto.params.KeyParameter; |
9 | 9 | import org.bouncycastle.crypto.params.ParametersWithIV; |
| 10 | +import org.bouncycastle.util.Arrays; |
10 | 11 | import org.bouncycastle.util.Strings; |
11 | 12 | import org.bouncycastle.util.encoders.Hex; |
12 | 13 | import org.bouncycastle.util.test.SimpleTest; |
@@ -150,6 +151,51 @@ public void performTest() |
150 | 151 | // expected |
151 | 152 | } |
152 | 153 |
|
| 154 | + // For small number of allowed blocks, validate boundary |
| 155 | + // conditions are properly handled. Zero and greater will |
| 156 | + // fail as size bound is a strict inequality. |
| 157 | + int[] offsets = new int[]{-10, -2, -1, 0, 1, 10}; |
| 158 | + int[] ns = new int[]{13, 12}; |
| 159 | + for (int n_len : ns) |
| 160 | + { |
| 161 | + for (int offset : offsets) |
| 162 | + { |
| 163 | + try |
| 164 | + { |
| 165 | + ccm.init(true, new AEADParameters(new KeyParameter(K1), 128, new byte[n_len])); |
| 166 | + |
| 167 | + // Encrypt up to 2^(8q) + offset. Note that message length |
| 168 | + // must be strictly less than 2^(8q) so offset=0 will not |
| 169 | + // work (per SP 800-38C Section A.1 Length Requirements). |
| 170 | + int q = 15 - n_len; |
| 171 | + int size = 1 << (8*q); |
| 172 | + inBuf = new byte[size + offset]; |
| 173 | + |
| 174 | + outBuf = new byte[ccm.getOutputSize(inBuf.length)]; |
| 175 | + len = ccm.processPacket(inBuf, 0, inBuf.length, outBuf, 0); |
| 176 | + |
| 177 | + if (offset >= 0) { |
| 178 | + fail("expected to fail to encrypt boundary bytes n=" + n_len + "size=" + size + " offset=" + offset); |
| 179 | + } else { |
| 180 | + // Decrypt should also succeed if encryption succeeded. |
| 181 | + ccm.init(false, new AEADParameters(new KeyParameter(K1), 128, new byte[n_len])); |
| 182 | + out = ccm.processPacket(outBuf, 0, outBuf.length); |
| 183 | + |
| 184 | + if (out.length != inBuf.length || !Arrays.areEqual(inBuf, out)) |
| 185 | + { |
| 186 | + fail("encryption output incorrect"); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + catch (Exception e) |
| 191 | + { |
| 192 | + if (offset < 0) { |
| 193 | + fail("unexpected failure to encrypt boundary bytes n=" + n_len + " offset=" + offset + " msg=" + e.getMessage()); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
153 | 199 | AEADTestUtil.testReset(this, new CCMBlockCipher(AESEngine.newInstance()), new CCMBlockCipher(AESEngine.newInstance()), new AEADParameters(new KeyParameter(K1), 32, N2)); |
154 | 200 | AEADTestUtil.testTampering(this, ccm, new AEADParameters(new KeyParameter(K1), 32, N2)); |
155 | 201 | AEADTestUtil.testOutputSizes(this, new CCMBlockCipher(AESEngine.newInstance()), new AEADParameters( |
|
0 commit comments