Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.session.libsession.utilities.AESGCM
import org.session.libsession.utilities.AESGCM.EncryptionResult
import org.session.libsignal.utilities.JsonUtil
import org.session.libsignal.utilities.toHexString
import java.io.ByteArrayOutputStream
import java.nio.Buffer
import java.nio.ByteBuffer
import java.nio.ByteOrder
Expand All @@ -14,16 +15,15 @@ object OnionRequestEncryption {
internal fun encode(ciphertext: ByteArray, json: Map<*, *>): ByteArray {
// The encoding of V2 onion requests looks like: | 4 bytes: size N of ciphertext | N bytes: ciphertext | json as utf8 |
val jsonAsData = JsonUtil.toJson(json).toByteArray()
val ciphertextSize = ciphertext.size
val buffer = ByteBuffer.allocate(Int.SIZE_BYTES)
buffer.order(ByteOrder.LITTLE_ENDIAN)
buffer.putInt(ciphertextSize)
val ciphertextSizeAsData = ByteArray(buffer.capacity())
// Casting here avoids an issue where this gets compiled down to incorrect byte code. See
// https://github.com/eclipse/jetty.project/issues/3244 for more info
(buffer as Buffer).position(0)
buffer.get(ciphertextSizeAsData)
return ciphertextSizeAsData + ciphertext + jsonAsData
val output = ByteArray(4 + ciphertext.size + jsonAsData.size)

ByteBuffer.wrap(output).apply {
order(ByteOrder.LITTLE_ENDIAN).putInt(ciphertext.size)
put(ciphertext)
put(jsonAsData)
}

return output
}

/**
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/org/session/libsession/utilities/AESGCM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.session.libsignal.utilities.ByteArraySlice.Companion.view
import org.session.libsignal.utilities.ByteUtil
import org.session.libsignal.utilities.Hex
import org.session.libsignal.utilities.Util
import java.nio.ByteBuffer
import javax.crypto.Cipher
import javax.crypto.Mac
import javax.crypto.spec.GCMParameterSpec
Expand Down Expand Up @@ -57,10 +58,19 @@ internal object AESGCM {
*/
fun encrypt(plaintext: ByteArraySlice, symmetricKey: ByteArray): ByteArray {
val iv = Util.getSecretBytes(ivSize)
synchronized(CIPHER_LOCK) {

return synchronized(CIPHER_LOCK) {
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(symmetricKey, "AES"), GCMParameterSpec(gcmTagSize, iv))
return ByteUtil.combine(iv, cipher.doFinal(plaintext.data, plaintext.offset, plaintext.len))

val output = ByteArray(ivSize + cipher.getOutputSize(plaintext.len))

ByteBuffer.wrap(output).also { buffer ->
buffer.put(iv)
cipher.doFinal(ByteBuffer.wrap(plaintext.data, plaintext.offset, plaintext.len), buffer)
}

output
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.math.BigInteger;
import java.util.Arrays;

import kotlin.collections.ArraysKt;

public class DjbECPublicKey implements ECPublicKey {

private final byte[] publicKey;
Expand All @@ -21,7 +23,7 @@ public DjbECPublicKey(byte[] publicKey) {
@Override
public byte[] serialize() {
byte[] type = {Curve.DJB_TYPE};
return ByteUtil.combine(type, publicKey);
return ArraysKt.plus(type, publicKey);
}

@Override
Expand Down
18 changes: 0 additions & 18 deletions app/src/main/java/org/session/libsignal/utilities/ByteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,10 @@
*/
package org.session.libsignal.utilities;

import org.session.libsignal.utilities.Hex;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;

public class ByteUtil {

public static byte[] combine(byte[]... elements) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

for (byte[] element : elements) {
baos.write(element);
}

return baos.toByteArray();
} catch (IOException e) {
throw new AssertionError(e);
}
}

public static byte[][] split(byte[] input, int firstLength, int secondLength) {
byte[][] parts = new byte[2][];

Expand Down
Loading