From b8211a7b902b559da234264f5fa1fcf09677f54b Mon Sep 17 00:00:00 2001 From: Alex Klyubin Date: Wed, 21 Jan 2015 17:49:00 -0800 Subject: 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 --- luni/src/main/java/javax/crypto/Cipher.java | 5 ++--- luni/src/test/java/libcore/javax/crypto/CipherTest.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'luni') 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( -- cgit v1.1