summaryrefslogtreecommitdiffstats
path: root/luni
diff options
context:
space:
mode:
authorAlex Klyubin <klyubin@google.com>2015-01-21 17:49:00 -0800
committerAlex Klyubin <klyubin@google.com>2015-01-23 08:52:57 -0800
commitb8211a7b902b559da234264f5fa1fcf09677f54b (patch)
treed25484687ded4f43bb673287121070f3c038943a /luni
parent194940746a618d361b26838e2c6ff04c358adff7 (diff)
downloadlibcore-b8211a7b902b559da234264f5fa1fcf09677f54b.zip
libcore-b8211a7b902b559da234264f5fa1fcf09677f54b.tar.gz
libcore-b8211a7b902b559da234264f5fa1fcf09677f54b.tar.bz2
Make Cipher.update return null for empty input.
Cipher.update(byte[], int, int inputLen) is supposed to return null when inputLen is zero. This CL makes it so. Prior to this CL, this method returned an empty byte array. Bug: 19100173 Change-Id: I5698f11f76a17dd8fc2509be5d8ec9369a888eaf
Diffstat (limited to 'luni')
-rw-r--r--luni/src/main/java/javax/crypto/Cipher.java5
-rw-r--r--luni/src/test/java/libcore/javax/crypto/CipherTest.java11
2 files changed, 13 insertions, 3 deletions
diff --git a/luni/src/main/java/javax/crypto/Cipher.java b/luni/src/main/java/javax/crypto/Cipher.java
index 2e3b341..889ac9f 100644
--- a/luni/src/main/java/javax/crypto/Cipher.java
+++ b/luni/src/main/java/javax/crypto/Cipher.java
@@ -1005,8 +1005,7 @@ public class Cipher {
* the offset in the input to start.
* @param inputLen
* the length of the input to transform.
- * @return the transformed bytes in a new buffer, or {@code null} if the
- * input has zero length.
+ * @return the transformed bytes in a new buffer, or {@code null} if {@code inputLen} is zero.
* @throws IllegalStateException
* if this cipher instance is not initialized for encryption or
* decryption.
@@ -1023,7 +1022,7 @@ public class Cipher {
throw new IllegalArgumentException("input == null");
}
checkInputOffsetAndCount(input.length, inputOffset, inputLen);
- if (input.length == 0) {
+ if (inputLen == 0) {
return null;
}
return getSpi().engineUpdate(input, inputOffset, inputLen);
diff --git a/luni/src/test/java/libcore/javax/crypto/CipherTest.java b/luni/src/test/java/libcore/javax/crypto/CipherTest.java
index c89886c..29112fb 100644
--- a/luni/src/test/java/libcore/javax/crypto/CipherTest.java
+++ b/luni/src/test/java/libcore/javax/crypto/CipherTest.java
@@ -2797,6 +2797,17 @@ public final class CipherTest extends TestCase {
}
}
+ public void testCipher_Update_WithZeroLengthInput_ReturnsNull() throws Exception {
+ SecretKey key = new SecretKeySpec(AES_128_KEY, "AES");
+ Cipher c = Cipher.getInstance("AES/ECB/NoPadding");
+ c.init(Cipher.ENCRYPT_MODE, key);
+ assertNull(c.update(new byte[0]));
+ assertNull(c.update(new byte[c.getBlockSize() * 2], 0, 0));
+
+ // Try with non-zero offset just in case the implementation mixes up offset and inputLen
+ assertNull(c.update(new byte[c.getBlockSize() * 2], 16, 0));
+ }
+
private void checkCipher_ShortBlock_Failure(CipherTestParam p, String provider) throws Exception {
SecretKey key = new SecretKeySpec(p.key, "AES");
Cipher c = Cipher.getInstance(