Skip to content

Commit b57a6bd

Browse files
SessionHero01SessionHero01
authored andcommitted
Add encryption stream
1 parent 795aef8 commit b57a6bd

File tree

6 files changed

+68
-3
lines changed

6 files changed

+68
-3
lines changed

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ agp = "8.12.0"
33
kotlin = "2.2.0"
44

55
[libraries]
6+
junit = { module = "junit:junit", version = "4.13.2" }
7+
androidx-test-runner = { module = "androidx.test:runner", version = "1.7.0" }
8+
androidx-test-rules = { module = "androidx.test:rules", version = "1.7.0" }
9+
androidx-test-ext = { module = "androidx.test.ext:junit", version = "1.3.0" }
610

711
[plugins]
812
android-library = { id = "com.android.library", version.ref = "agp" }

library/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,8 @@ publishing {
9494
}
9595

9696
dependencies {
97+
androidTestImplementation(libs.junit)
98+
androidTestImplementation(libs.androidx.test.runner)
99+
androidTestImplementation(libs.androidx.test.rules)
100+
androidTestImplementation(libs.androidx.test.ext)
97101
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package network.loki.messenger.libsession_util
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import network.loki.messenger.libsession_util.encrypt.DecryptionStream
5+
import network.loki.messenger.libsession_util.encrypt.EncryptionStream
6+
import org.junit.Assert.assertArrayEquals
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import java.io.ByteArrayInputStream
10+
import java.io.ByteArrayOutputStream
11+
import java.security.SecureRandom
12+
13+
@RunWith(AndroidJUnit4::class)
14+
class EncryptionStreamTest {
15+
16+
private fun testEncryptionCase(
17+
chunkSize: Int,
18+
dataSize: Int,
19+
) {
20+
try {
21+
val key = ByteArray(32)
22+
SecureRandom().nextBytes(key)
23+
24+
val expectData = ByteArray(dataSize)
25+
SecureRandom().nextBytes(expectData)
26+
27+
val encrypted = ByteArrayOutputStream().let { outputStream ->
28+
EncryptionStream(outputStream, key, chunkSize).use {
29+
it.write(expectData)
30+
}
31+
32+
outputStream.toByteArray()
33+
}
34+
35+
val actualData = DecryptionStream(
36+
ByteArrayInputStream(encrypted),
37+
key
38+
).use { it.readAllBytes() }
39+
40+
assertArrayEquals(expectData, actualData)
41+
} catch (e: Exception) {
42+
throw RuntimeException("Encryption/Decryption failed for chunkSize: $chunkSize, dataSize: $dataSize", e)
43+
}
44+
}
45+
46+
@Test
47+
fun shouldEncryptDecrypt() {
48+
testEncryptionCase(chunkSize = 24, dataSize = 25)
49+
testEncryptionCase(chunkSize = 24, dataSize = 24)
50+
testEncryptionCase(chunkSize = 24, dataSize = 12)
51+
testEncryptionCase(chunkSize = 24, dataSize = 48)
52+
}
53+
}

library/src/main/cpp/encryption.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Java_network_loki_messenger_libsession_1util_encrypt_EncryptionStream_00024Compa
179179

180180
auto state = std::make_unique<crypto_secretstream_xchacha20poly1305_state>();
181181
crypto_secretstream_xchacha20poly1305_init_push(state.get(),
182-
reinterpret_cast<unsigned char*>(env->GetDirectBufferAddress(javaHeaderOut)),
182+
headerOut.get().data(),
183183
key.get().data());
184184

185185
return reinterpret_cast<jlong>(state.release());
@@ -272,12 +272,11 @@ Java_network_loki_messenger_libsession_1util_encrypt_DecryptionStream_00024Compa
272272
JavaByteArrayRef in_buf(env, java_in_buf);
273273

274274
unsigned long long mlen = out_buf.get().size();
275-
unsigned char tag;
276275

277276
if (crypto_secretstream_xchacha20poly1305_pull(
278277
reinterpret_cast<crypto_secretstream_xchacha20poly1305_state*>(native_state_ptr),
279278
out_buf.get().data(), &mlen, // Plaintext data out
280-
&tag,
279+
nullptr, // Tag (not used here)
281280
in_buf.get().data(), in_buf_len, // Ciphertext data in
282281
nullptr, 0 // Additional data (not used here)
283282
)) {

library/src/main/java/network/loki/messenger/libsession_util/encrypt/DecryptionStream.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class DecryptionStream(
2828
))
2929

3030
plaintextBuffer = ByteBuffer.allocate(chunkSize)
31+
plaintextBuffer.limit(0) // Should be empty initially
3132

3233
// Read the initial header from the input stream
3334
check(inStream.read(cipherBuffer, 0, EncryptionStream.encryptionStreamHeaderSize())

library/src/main/java/network/loki/messenger/libsession_util/encrypt/EncryptionStream.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class EncryptionStream(
8787

8888

8989
companion object {
90+
init {
91+
System.loadLibrary("session_util")
92+
}
93+
9094
external fun encryptionStreamHeaderSize(): Int
9195
external fun encryptionStreamChunkOverhead(): Int
9296

0 commit comments

Comments
 (0)