From 73bb3b00653584da5c013f8f60aa67422e50bfa6 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 18 Dec 2015 14:06:47 +0000
Subject: [PATCH] Fixed return value of process().

---
 .../briarproject/crypto/AuthenticatedCipher.java | 16 ++++++++--------
 .../XSalsa20Poly1305AuthenticatedCipher.java     |  2 +-
 .../XSalsa20Poly1305AuthenticatedCipherTest.java |  7 +++++--
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/briar-core/src/org/briarproject/crypto/AuthenticatedCipher.java b/briar-core/src/org/briarproject/crypto/AuthenticatedCipher.java
index 5ee21e3e4c..fe4f8bee00 100644
--- a/briar-core/src/org/briarproject/crypto/AuthenticatedCipher.java
+++ b/briar-core/src/org/briarproject/crypto/AuthenticatedCipher.java
@@ -26,14 +26,14 @@ interface AuthenticatedCipher {
 	 *              including the MAC.
 	 * @param inputOff the offset into the input array where the data to be
 	 *                 processed starts.
-	 * @param len the number of bytes to be processed. If decrypting, includes
-	 *            the MAC length.
-	 * @param output the output buffer the processed bytes go into. If
-	 *               encrypting, the ciphertext including the MAC. If
-	 *               decrypting, the plaintext.
-	 * @param outputOff the offset into the output byte array the processed
-	 *                  data starts at.
-	 * @return  the number of bytes processed.
+	 * @param len the length of the input. If decrypting, includes the MAC
+	 *            length.
+	 * @param output the output byte array. If encrypting, the ciphertext
+	 *               including the MAC. If decrypting, the plaintext.
+	 * @param outputOff the offset into the output byte array where the
+	 *                  processed data starts.
+	 * @return  the length of the output. If encrypting, includes the MAC
+	 *          length.
 	 * @throws GeneralSecurityException on invalid input.
 	 */
 	int process(byte[] input, int inputOff, int len, byte[] output,
diff --git a/briar-core/src/org/briarproject/crypto/XSalsa20Poly1305AuthenticatedCipher.java b/briar-core/src/org/briarproject/crypto/XSalsa20Poly1305AuthenticatedCipher.java
index bdfe361697..8143b696ad 100644
--- a/briar-core/src/org/briarproject/crypto/XSalsa20Poly1305AuthenticatedCipher.java
+++ b/briar-core/src/org/briarproject/crypto/XSalsa20Poly1305AuthenticatedCipher.java
@@ -112,7 +112,7 @@ public class XSalsa20Poly1305AuthenticatedCipher
 				poly1305.doFinal(output, outputOff);
 			}
 
-			return processed;
+			return encrypting ? processed + MAC_LENGTH : processed;
 		} catch (DataLengthException e) {
 			throw new GeneralSecurityException(e.getMessage());
 		}
diff --git a/briar-tests/src/org/briarproject/crypto/XSalsa20Poly1305AuthenticatedCipherTest.java b/briar-tests/src/org/briarproject/crypto/XSalsa20Poly1305AuthenticatedCipherTest.java
index 8828667699..6d45fc9757 100644
--- a/briar-tests/src/org/briarproject/crypto/XSalsa20Poly1305AuthenticatedCipherTest.java
+++ b/briar-tests/src/org/briarproject/crypto/XSalsa20Poly1305AuthenticatedCipherTest.java
@@ -9,6 +9,7 @@ import java.security.GeneralSecurityException;
 import java.util.Random;
 
 import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
 
 public class XSalsa20Poly1305AuthenticatedCipherTest extends BriarTestCase {
 
@@ -51,7 +52,8 @@ public class XSalsa20Poly1305AuthenticatedCipherTest extends BriarTestCase {
 		AuthenticatedCipher cipher = new XSalsa20Poly1305AuthenticatedCipher();
 		cipher.init(true, k, TEST_IV);
 		byte[] output = new byte[TEST_CIPHERTEXT.length];
-		cipher.process(TEST_PLAINTEXT, 0, TEST_PLAINTEXT.length, output, 0);
+		assertEquals(TEST_CIPHERTEXT.length, cipher.process(TEST_PLAINTEXT, 0,
+						TEST_PLAINTEXT.length, output, 0));
 		assertArrayEquals(TEST_CIPHERTEXT, output);
 	}
 
@@ -61,7 +63,8 @@ public class XSalsa20Poly1305AuthenticatedCipherTest extends BriarTestCase {
 		AuthenticatedCipher cipher = new XSalsa20Poly1305AuthenticatedCipher();
 		cipher.init(false, k, TEST_IV);
 		byte[] output = new byte[TEST_PLAINTEXT.length];
-		cipher.process(TEST_CIPHERTEXT, 0, TEST_CIPHERTEXT.length, output, 0);
+		assertEquals(TEST_PLAINTEXT.length, cipher.process(TEST_CIPHERTEXT, 0,
+				TEST_CIPHERTEXT.length, output, 0));
 		assertArrayEquals(TEST_PLAINTEXT, output);
 	}
 
-- 
GitLab