Skip to content

Commit 433836f

Browse files
Small improvement to onion request memory use (#1806)
* Small improvement to onion request memory use * Remove ByteUtil.combine
1 parent 3e9bceb commit 433836f

4 files changed

Lines changed: 25 additions & 31 deletions

File tree

app/src/main/java/org/session/libsession/snode/OnionRequestEncryption.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.session.libsession.utilities.AESGCM
55
import org.session.libsession.utilities.AESGCM.EncryptionResult
66
import org.session.libsignal.utilities.JsonUtil
77
import org.session.libsignal.utilities.toHexString
8+
import java.io.ByteArrayOutputStream
89
import java.nio.Buffer
910
import java.nio.ByteBuffer
1011
import java.nio.ByteOrder
@@ -14,16 +15,15 @@ object OnionRequestEncryption {
1415
internal fun encode(ciphertext: ByteArray, json: Map<*, *>): ByteArray {
1516
// The encoding of V2 onion requests looks like: | 4 bytes: size N of ciphertext | N bytes: ciphertext | json as utf8 |
1617
val jsonAsData = JsonUtil.toJson(json).toByteArray()
17-
val ciphertextSize = ciphertext.size
18-
val buffer = ByteBuffer.allocate(Int.SIZE_BYTES)
19-
buffer.order(ByteOrder.LITTLE_ENDIAN)
20-
buffer.putInt(ciphertextSize)
21-
val ciphertextSizeAsData = ByteArray(buffer.capacity())
22-
// Casting here avoids an issue where this gets compiled down to incorrect byte code. See
23-
// https://github.com/eclipse/jetty.project/issues/3244 for more info
24-
(buffer as Buffer).position(0)
25-
buffer.get(ciphertextSizeAsData)
26-
return ciphertextSizeAsData + ciphertext + jsonAsData
18+
val output = ByteArray(4 + ciphertext.size + jsonAsData.size)
19+
20+
ByteBuffer.wrap(output).apply {
21+
order(ByteOrder.LITTLE_ENDIAN).putInt(ciphertext.size)
22+
put(ciphertext)
23+
put(jsonAsData)
24+
}
25+
26+
return output
2727
}
2828

2929
/**

app/src/main/java/org/session/libsession/utilities/AESGCM.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.session.libsignal.utilities.ByteArraySlice.Companion.view
99
import org.session.libsignal.utilities.ByteUtil
1010
import org.session.libsignal.utilities.Hex
1111
import org.session.libsignal.utilities.Util
12+
import java.nio.ByteBuffer
1213
import javax.crypto.Cipher
1314
import javax.crypto.Mac
1415
import javax.crypto.spec.GCMParameterSpec
@@ -57,10 +58,19 @@ internal object AESGCM {
5758
*/
5859
fun encrypt(plaintext: ByteArraySlice, symmetricKey: ByteArray): ByteArray {
5960
val iv = Util.getSecretBytes(ivSize)
60-
synchronized(CIPHER_LOCK) {
61+
62+
return synchronized(CIPHER_LOCK) {
6163
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
6264
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(symmetricKey, "AES"), GCMParameterSpec(gcmTagSize, iv))
63-
return ByteUtil.combine(iv, cipher.doFinal(plaintext.data, plaintext.offset, plaintext.len))
65+
66+
val output = ByteArray(ivSize + cipher.getOutputSize(plaintext.len))
67+
68+
ByteBuffer.wrap(output).also { buffer ->
69+
buffer.put(iv)
70+
cipher.doFinal(ByteBuffer.wrap(plaintext.data, plaintext.offset, plaintext.len), buffer)
71+
}
72+
73+
output
6474
}
6575
}
6676

app/src/main/java/org/session/libsignal/crypto/ecc/DjbECPublicKey.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.math.BigInteger;
1111
import java.util.Arrays;
1212

13+
import kotlin.collections.ArraysKt;
14+
1315
public class DjbECPublicKey implements ECPublicKey {
1416

1517
private final byte[] publicKey;
@@ -21,7 +23,7 @@ public DjbECPublicKey(byte[] publicKey) {
2123
@Override
2224
public byte[] serialize() {
2325
byte[] type = {Curve.DJB_TYPE};
24-
return ByteUtil.combine(type, publicKey);
26+
return ArraysKt.plus(type, publicKey);
2527
}
2628

2729
@Override

app/src/main/java/org/session/libsignal/utilities/ByteUtil.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,10 @@
55
*/
66
package org.session.libsignal.utilities;
77

8-
import org.session.libsignal.utilities.Hex;
9-
10-
import java.io.ByteArrayOutputStream;
11-
import java.io.IOException;
128
import java.text.ParseException;
139

1410
public class ByteUtil {
1511

16-
public static byte[] combine(byte[]... elements) {
17-
try {
18-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
19-
20-
for (byte[] element : elements) {
21-
baos.write(element);
22-
}
23-
24-
return baos.toByteArray();
25-
} catch (IOException e) {
26-
throw new AssertionError(e);
27-
}
28-
}
29-
3012
public static byte[][] split(byte[] input, int firstLength, int secondLength) {
3113
byte[][] parts = new byte[2][];
3214

0 commit comments

Comments
 (0)