diff options
author | Kenny Root <kroot@google.com> | 2016-02-25 23:15:47 -0800 |
---|---|---|
committer | The Android Automerger <android-build@google.com> | 2016-03-25 17:48:02 -0700 |
commit | 156bf0a0237c98bfca6826ac9030b1444c391a50 (patch) | |
tree | 28f21e0ee713f3256d781d88433ec69b25d72be1 | |
parent | 4e6f59996fae4f9a2de077b113be46cd461e4b4d (diff) | |
download | libcore-156bf0a0237c98bfca6826ac9030b1444c391a50.zip libcore-156bf0a0237c98bfca6826ac9030b1444c391a50.tar.gz libcore-156bf0a0237c98bfca6826ac9030b1444c391a50.tar.bz2 |
CipherTest: test instance reuse with updateAAD
AAD was not being reset on each Cipher init or doFinal call, so add
regression tests to make sure that is now the case.
(cherry picked from commit d90a44bf4956d335e2a876015cf258dc46e226ea)
Bug: 27324690
Change-Id: I5f7606efb6dfcd412166eed2bd5f417097a97f1f
-rw-r--r-- | luni/src/test/java/libcore/javax/crypto/CipherTest.java | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/luni/src/test/java/libcore/javax/crypto/CipherTest.java b/luni/src/test/java/libcore/javax/crypto/CipherTest.java index 38d6d8d..7cd6133 100644 --- a/luni/src/test/java/libcore/javax/crypto/CipherTest.java +++ b/luni/src/test/java/libcore/javax/crypto/CipherTest.java @@ -3537,4 +3537,45 @@ public final class CipherTest extends TestCase { cipher.init(Cipher.ENCRYPT_MODE, keyGen.generateKeyPair().getPublic()); cipher.doFinal(new byte[] {1,2,3,4}); } + + /* + * Check that GCM encryption with old and new instances update correctly. + * http://b/26694388 + */ + public void test_AESGCMNoPadding_Reuse_Success() throws Exception { + SecretKeySpec key = new SecretKeySpec(new byte[16], "AES"); + GCMParameterSpec spec = new GCMParameterSpec(128, new byte[12]); + Cipher c1 = Cipher.getInstance("AES/GCM/NoPadding"); + Cipher c2 = Cipher.getInstance("AES/GCM/NoPadding"); + + // Pollute the c1 cipher with AAD + c1.init(Cipher.ENCRYPT_MODE, key, spec); + c1.updateAAD(new byte[] { + 0x01, 0x02, 0x03, 0x04, 0x05, + }); + + // Now init each again and make sure the outputs are the same + c1.init(Cipher.ENCRYPT_MODE, key, spec); + c2.init(Cipher.ENCRYPT_MODE, key, spec); + + byte[] aad = new byte[] { + 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, + }; + c1.updateAAD(aad); + c2.updateAAD(aad); + + assertEquals(Arrays.toString(c1.doFinal()), Arrays.toString(c2.doFinal())); + + // .doFinal should also reset the state, so check that as well. + byte[] aad2 = new byte[] { + 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, + }; + + Cipher c3 = Cipher.getInstance("AES/GCM/NoPadding"); + c3.init(Cipher.ENCRYPT_MODE, key, spec); + + c1.updateAAD(aad2); + c3.updateAAD(aad2); + assertEquals(Arrays.toString(c1.doFinal()), Arrays.toString(c3.doFinal())); + } } |