|
6 | 6 | import java.security.InvalidKeyException; |
7 | 7 | import java.security.NoSuchAlgorithmException; |
8 | 8 | import java.security.NoSuchProviderException; |
| 9 | +import java.security.SecureRandom; |
9 | 10 | import java.security.Security; |
10 | 11 | import java.security.spec.InvalidParameterSpecException; |
11 | 12 |
|
@@ -55,6 +56,7 @@ public void performTest() throws Exception |
55 | 56 | testGCMParameterSpec(K2, N2, A2, P2, C2); |
56 | 57 | testGCMParameterSpecWithRepeatKey(K2, N2, A2, P2, C2); |
57 | 58 | testGCMGeneric(KGCM, NGCM, new byte[0], new byte[0], CGCM); |
| 59 | + testGCMParameterSpecWithMultipleUpdates(K2, N2, A2, P2, C2); |
58 | 60 | } |
59 | 61 | catch (ClassNotFoundException e) |
60 | 62 | { |
@@ -141,6 +143,62 @@ private void testGCMParameterSpec(byte[] K, |
141 | 143 | } |
142 | 144 | } |
143 | 145 |
|
| 146 | + private void testGCMParameterSpecWithMultipleUpdates(byte[] K, |
| 147 | + byte[] N, |
| 148 | + byte[] A, |
| 149 | + byte[] P, |
| 150 | + byte[] C) |
| 151 | + throws Exception |
| 152 | + { |
| 153 | + Cipher eax = Cipher.getInstance("AES/EAX/NoPadding", "BC"); |
| 154 | + SecretKeySpec key = new SecretKeySpec(K, "AES"); |
| 155 | + SecureRandom random = new SecureRandom(); |
| 156 | + |
| 157 | + // GCMParameterSpec mapped to AEADParameters and overrides default MAC |
| 158 | + // size |
| 159 | + GCMParameterSpec spec = new GCMParameterSpec(128, N); |
| 160 | + |
| 161 | + for (int i = 900; i != 1024; i++) |
| 162 | + { |
| 163 | + byte[] message = new byte[i]; |
| 164 | + |
| 165 | + random.nextBytes(message); |
| 166 | + |
| 167 | + eax.init(Cipher.ENCRYPT_MODE, key, spec); |
| 168 | + |
| 169 | + byte[] out = new byte[eax.getOutputSize(i)]; |
| 170 | + |
| 171 | + int offSet = 0; |
| 172 | + |
| 173 | + int count; |
| 174 | + for (count = 0; count < i / 21; count++) |
| 175 | + { |
| 176 | + offSet += eax.update(message, count * 21, 21, out, offSet); |
| 177 | + } |
| 178 | + |
| 179 | + offSet += eax.doFinal(message, count * 21, i - (count * 21), out, offSet); |
| 180 | + |
| 181 | + byte[] dec = new byte[i]; |
| 182 | + int len = offSet; |
| 183 | + |
| 184 | + eax.init(Cipher.DECRYPT_MODE, key, spec); |
| 185 | + |
| 186 | + offSet = 0; |
| 187 | + for (count = 0; count < len / 10; count++) |
| 188 | + { |
| 189 | + offSet += eax.update(out, count * 10, 10, dec, offSet); |
| 190 | + } |
| 191 | + |
| 192 | + offSet += eax.doFinal(out, count * 10, len - (count * 10), dec, offSet); |
| 193 | + |
| 194 | + if (!Arrays.areEqual(message, dec) || offSet != message.length) |
| 195 | + { |
| 196 | + fail("message mismatch"); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + |
144 | 202 | private void testGCMParameterSpecWithRepeatKey(byte[] K, |
145 | 203 | byte[] N, |
146 | 204 | byte[] A, |
|
0 commit comments