diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 162f9f2ba5..f40b4e78aa 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -55,7 +55,7 @@ import org.session.libsignal.service.loki.utilities.mentions.MentionsManager; import org.session.libsignal.utilities.logging.Log; import org.signal.aesgcmprovider.AesGcmProvider; import org.thoughtcrime.securesms.components.TypingStatusSender; -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil; +import org.session.libsession.utilities.IdentityKeyUtil; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.GroupDatabase; import org.thoughtcrime.securesms.dependencies.InjectableType; diff --git a/app/src/main/java/org/thoughtcrime/securesms/backup/FullBackupExporter.kt b/app/src/main/java/org/thoughtcrime/securesms/backup/FullBackupExporter.kt index 1e2738db2c..9d1c012fb5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/backup/FullBackupExporter.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/backup/FullBackupExporter.kt @@ -18,7 +18,6 @@ import org.session.libsession.utilities.Conversions import org.thoughtcrime.securesms.backup.BackupProtos.* import org.thoughtcrime.securesms.crypto.AttachmentSecret import org.thoughtcrime.securesms.crypto.ClassicDecryptingPartInputStream -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream import org.thoughtcrime.securesms.database.* import org.session.libsignal.utilities.logging.Log @@ -91,7 +90,7 @@ object FullBackupExporter { } } } - for (preference in IdentityKeyUtil.getBackupRecords(context)) { + for (preference in BackupUtil.getBackupRecords(context)) { EventBus.getDefault().post(BackupEvent.createProgress(++count)) outputStream.writePreferenceEntry(preference) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/crypto/storage/SignalProtocolStoreImpl.java b/app/src/main/java/org/thoughtcrime/securesms/crypto/storage/SignalProtocolStoreImpl.java index 6752d6be6c..0b36e7ec44 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/crypto/storage/SignalProtocolStoreImpl.java +++ b/app/src/main/java/org/thoughtcrime/securesms/crypto/storage/SignalProtocolStoreImpl.java @@ -4,7 +4,7 @@ import android.content.Context; import org.session.libsignal.libsignal.IdentityKeyPair; import org.session.libsignal.libsignal.state.IdentityKeyStore; -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil; +import org.session.libsession.utilities.IdentityKeyUtil; public class SignalProtocolStoreImpl implements IdentityKeyStore { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt index 86eaceadcd..4ce1ac8356 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -29,7 +29,7 @@ import org.session.libsignal.service.api.messages.SignalServiceGroup import org.session.libsignal.service.internal.push.SignalServiceProtos import org.session.libsignal.service.loki.api.opengroups.PublicChat import org.session.libsignal.utilities.logging.Log -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil +import org.session.libsession.utilities.IdentityKeyUtil import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper import org.thoughtcrime.securesms.loki.database.LokiThreadDatabase import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 1657515782..6f32459c3d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -34,7 +34,7 @@ import org.session.libsession.utilities.GroupUtil; import org.session.libsession.utilities.TextSecurePreferences; import org.thoughtcrime.securesms.contactshare.ContactModelMapper; -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil; +import org.session.libsession.utilities.IdentityKeyUtil; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.GroupDatabase; import org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult; diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt index c695ae54a7..bcbdc18ef8 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt @@ -30,10 +30,7 @@ import org.session.libsession.messaging.sending_receiving.MessageSender import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode -import org.session.libsession.utilities.GroupUtil -import org.session.libsession.utilities.ProfilePictureModifiedEvent -import org.session.libsession.utilities.TextSecurePreferences -import org.session.libsession.utilities.Util +import org.session.libsession.utilities.* import org.session.libsignal.service.loki.utilities.mentions.MentionsManager import org.session.libsignal.service.loki.utilities.toHexString import org.session.libsignal.utilities.ThreadUtils @@ -43,7 +40,6 @@ import org.thoughtcrime.securesms.conversation.ConversationActivity import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.model.ThreadRecord import org.thoughtcrime.securesms.loki.dialogs.* -import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocolV2 import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol import org.thoughtcrime.securesms.loki.utilities.* import org.thoughtcrime.securesms.loki.views.ConversationView diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/LandingActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/LandingActivity.kt index 882671bd67..50080956ba 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/LandingActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/LandingActivity.kt @@ -3,11 +3,9 @@ package org.thoughtcrime.securesms.loki.activities import android.content.Intent import android.os.Bundle import android.view.View -import android.widget.Toast import network.loki.messenger.R -import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.BaseActionBarActivity -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil +import org.session.libsession.utilities.IdentityKeyUtil import org.thoughtcrime.securesms.loki.utilities.push import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo import org.thoughtcrime.securesms.loki.views.FakeChatView diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/LinkDeviceActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/LinkDeviceActivity.kt index 6d6255e788..6f3ef464bc 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/LinkDeviceActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/LinkDeviceActivity.kt @@ -35,7 +35,7 @@ import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.loki.fragments.ScanQRCodeWrapperFragment import org.thoughtcrime.securesms.loki.fragments.ScanQRCodeWrapperFragmentDelegate -import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities +import org.session.libsession.utilities.KeyPairUtilities import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities import org.thoughtcrime.securesms.loki.utilities.push import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/RecoveryPhraseRestoreActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/RecoveryPhraseRestoreActivity.kt index 2a3633729a..24d160f880 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/RecoveryPhraseRestoreActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/RecoveryPhraseRestoreActivity.kt @@ -19,7 +19,7 @@ import org.session.libsignal.service.loki.crypto.MnemonicCodec import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey import org.session.libsignal.utilities.Hex import org.thoughtcrime.securesms.BaseActionBarActivity -import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities +import org.session.libsession.utilities.KeyPairUtilities import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities import org.thoughtcrime.securesms.loki.utilities.push import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/RegisterActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/RegisterActivity.kt index 4b38eedf6a..229edab444 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/RegisterActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/RegisterActivity.kt @@ -23,7 +23,7 @@ import org.session.libsignal.libsignal.ecc.ECKeyPair import org.session.libsignal.libsignal.util.KeyHelper import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey import org.thoughtcrime.securesms.BaseActionBarActivity -import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities +import org.session.libsession.utilities.KeyPairUtilities import org.thoughtcrime.securesms.loki.utilities.push import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo import java.util.* diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/SeedActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/SeedActivity.kt index 07d8d8f9db..2c79b4fc87 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/SeedActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/SeedActivity.kt @@ -12,7 +12,7 @@ import android.widget.Toast import kotlinx.android.synthetic.main.activity_seed.* import network.loki.messenger.R import org.thoughtcrime.securesms.BaseActionBarActivity -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil +import org.session.libsession.utilities.IdentityKeyUtil import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities import org.thoughtcrime.securesms.loki.utilities.getColorWithID import org.session.libsession.utilities.TextSecurePreferences diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/api/PublicChatPoller.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/api/PublicChatPoller.kt index 989cb2a6fa..4b69f0019f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/api/PublicChatPoller.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/api/PublicChatPoller.kt @@ -7,12 +7,11 @@ import nl.komponents.kovenant.Promise import nl.komponents.kovenant.functional.bind import nl.komponents.kovenant.functional.map import org.thoughtcrime.securesms.ApplicationContext -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil +import org.session.libsession.utilities.IdentityKeyUtil import org.session.libsession.messaging.threads.Address import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.jobs.PushDecryptJob import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob -import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol import org.session.libsession.messaging.threads.recipients.Recipient import org.session.libsession.utilities.TextSecurePreferences import org.session.libsignal.libsignal.util.guava.Optional diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/api/SessionProtocolImpl.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/api/SessionProtocolImpl.kt index e3c5b0aa6a..9be7b3e461 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/api/SessionProtocolImpl.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/api/SessionProtocolImpl.kt @@ -14,36 +14,12 @@ import org.session.libsignal.service.loki.api.crypto.SessionProtocol import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded import org.session.libsignal.service.loki.utilities.toHexString -import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities +import org.session.libsession.utilities.KeyPairUtilities class SessionProtocolImpl(private val context: Context) : SessionProtocol { private val sodium by lazy { LazySodiumAndroid(SodiumAndroid()) } - override fun encrypt(plaintext: ByteArray, recipientHexEncodedX25519PublicKey: String): ByteArray { - val userED25519KeyPair = KeyPairUtilities.getUserED25519KeyPair(context) ?: throw SessionProtocol.Exception.NoUserED25519KeyPair - val recipientX25519PublicKey = Hex.fromStringCondensed(recipientHexEncodedX25519PublicKey.removing05PrefixIfNeeded()) - - val verificationData = plaintext + userED25519KeyPair.publicKey.asBytes + recipientX25519PublicKey - val signature = ByteArray(Sign.BYTES) - try { - sodium.cryptoSignDetached(signature, verificationData, verificationData.size.toLong(), userED25519KeyPair.secretKey.asBytes) - } catch (exception: Exception) { - Log.d("Loki", "Couldn't sign message due to error: $exception.") - throw SessionProtocol.Exception.SigningFailed - } - val plaintextWithMetadata = plaintext + userED25519KeyPair.publicKey.asBytes + signature - val ciphertext = ByteArray(plaintextWithMetadata.size + Box.SEALBYTES) - try { - sodium.cryptoBoxSeal(ciphertext, plaintextWithMetadata, plaintextWithMetadata.size.toLong(), recipientX25519PublicKey) - } catch (exception: Exception) { - Log.d("Loki", "Couldn't encrypt message due to error: $exception.") - throw SessionProtocol.Exception.EncryptionFailed - } - - return ciphertext - } - override fun decrypt(ciphertext: ByteArray, x25519KeyPair: ECKeyPair): Pair { val recipientX25519PrivateKey = x25519KeyPair.privateKey.serialize() val recipientX25519PublicKey = Hex.fromStringCondensed(x25519KeyPair.hexEncodedPublicKey.removing05PrefixIfNeeded()) diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt index 077715f7e6..ccd5fa88b3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt @@ -13,7 +13,7 @@ import org.session.libsignal.service.loki.api.Snode import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded import org.session.libsignal.service.loki.utilities.toHexString -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil +import org.session.libsession.utilities.IdentityKeyUtil import org.session.libsignal.utilities.Hex import org.session.libsession.utilities.TextSecurePreferences import org.session.libsignal.service.loki.utilities.PublicKeyValidation diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/dialogs/ClearAllDataDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/dialogs/ClearAllDataDialog.kt index 4a14d23ced..cb1bf22566 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/dialogs/ClearAllDataDialog.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/dialogs/ClearAllDataDialog.kt @@ -11,7 +11,7 @@ import kotlinx.android.synthetic.main.dialog_clear_all_data.view.* import network.loki.messenger.R import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol -import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities +import org.session.libsession.utilities.KeyPairUtilities class ClearAllDataDialog : DialogFragment() { diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/dialogs/SeedDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/dialogs/SeedDialog.kt index dd97e832e9..12446cf57b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/dialogs/SeedDialog.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/dialogs/SeedDialog.kt @@ -13,7 +13,7 @@ import androidx.appcompat.app.AlertDialog import androidx.fragment.app.DialogFragment import kotlinx.android.synthetic.main.dialog_seed.view.* import network.loki.messenger.R -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil +import org.session.libsession.utilities.IdentityKeyUtil import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities import org.session.libsignal.service.loki.crypto.MnemonicCodec import org.session.libsignal.service.loki.utilities.hexEncodedPrivateKey diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.kt b/app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.kt index 8b8d899b72..236b37a8a2 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.kt @@ -7,35 +7,73 @@ import android.net.Uri import android.os.Build import android.os.Environment import android.provider.DocumentsContract -import org.session.libsignal.utilities.logging.Log import android.widget.Toast import androidx.annotation.WorkerThread import androidx.documentfile.provider.DocumentFile import androidx.fragment.app.Fragment import network.loki.messenger.R import org.greenrobot.eventbus.EventBus +import org.session.libsession.utilities.TextSecurePreferences +import org.session.libsignal.libsignal.util.ByteUtil +import org.session.libsignal.utilities.logging.Log import org.thoughtcrime.securesms.backup.BackupEvent import org.thoughtcrime.securesms.backup.BackupPassphrase +import org.thoughtcrime.securesms.backup.BackupProtos.SharedPreference import org.thoughtcrime.securesms.backup.FullBackupExporter import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider +import org.session.libsession.utilities.IdentityKeyUtil import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.loki.database.BackupFileRecord import org.thoughtcrime.securesms.service.LocalBackupListener -import org.session.libsession.utilities.TextSecurePreferences -import org.session.libsignal.libsignal.util.ByteUtil import java.io.IOException import java.security.MessageDigest import java.security.NoSuchAlgorithmException import java.security.SecureRandom import java.text.SimpleDateFormat import java.util.* -import kotlin.jvm.Throws object BackupUtil { + private const val MASTER_SECRET_UTIL_PREFERENCES_NAME = "SecureSMS-Preferences" private const val TAG = "BackupUtil" const val BACKUP_FILE_MIME_TYPE = "application/session-backup" const val BACKUP_PASSPHRASE_LENGTH = 30 + fun getBackupRecords(context: Context): List { + val prefName = MASTER_SECRET_UTIL_PREFERENCES_NAME + val preferences = context.getSharedPreferences(prefName, 0) + val prefList = LinkedList() + prefList.add(SharedPreference.newBuilder() + .setFile(prefName) + .setKey(IdentityKeyUtil.IDENTITY_PUBLIC_KEY_PREF) + .setValue(preferences.getString(IdentityKeyUtil.IDENTITY_PUBLIC_KEY_PREF, null)) + .build()) + prefList.add(SharedPreference.newBuilder() + .setFile(prefName) + .setKey(IdentityKeyUtil.IDENTITY_PRIVATE_KEY_PREF) + .setValue(preferences.getString(IdentityKeyUtil.IDENTITY_PRIVATE_KEY_PREF, null)) + .build()) + if (preferences.contains(IdentityKeyUtil.ED25519_PUBLIC_KEY)) { + prefList.add(SharedPreference.newBuilder() + .setFile(prefName) + .setKey(IdentityKeyUtil.ED25519_PUBLIC_KEY) + .setValue(preferences.getString(IdentityKeyUtil.ED25519_PUBLIC_KEY, null)) + .build()) + } + if (preferences.contains(IdentityKeyUtil.ED25519_SECRET_KEY)) { + prefList.add(SharedPreference.newBuilder() + .setFile(prefName) + .setKey(IdentityKeyUtil.ED25519_SECRET_KEY) + .setValue(preferences.getString(IdentityKeyUtil.ED25519_SECRET_KEY, null)) + .build()) + } + prefList.add(SharedPreference.newBuilder() + .setFile(prefName) + .setKey(IdentityKeyUtil.LOKI_SEED) + .setValue(preferences.getString(IdentityKeyUtil.LOKI_SEED, null)) + .build()) + return prefList + } + /** * Set app-wide configuration to enable the backups and schedule them. * @@ -91,7 +129,7 @@ object BackupUtil { @JvmStatic fun generateBackupPassphrase(): Array { val random = ByteArray(BACKUP_PASSPHRASE_LENGTH).also { SecureRandom().nextBytes(it) } - return Array(6) {i -> + return Array(6) { i -> String.format("%05d", ByteUtil.byteArray5ToLong(random, i * 5) % 100000) } } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderEncryption.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderEncryption.kt index 43a61a0d25..a108724e9c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderEncryption.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderEncryption.kt @@ -1,11 +1,53 @@ package org.session.libsession.messaging.sending_receiving +import com.goterl.lazycode.lazysodium.LazySodiumAndroid +import com.goterl.lazycode.lazysodium.SodiumAndroid +import com.goterl.lazycode.lazysodium.interfaces.Box +import com.goterl.lazycode.lazysodium.interfaces.Sign + import org.session.libsession.messaging.MessagingConfiguration +import org.session.libsession.messaging.sending_receiving.MessageSender.Error +import org.session.libsession.utilities.KeyPairUtilities + +import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded +import org.session.libsignal.utilities.Hex +import org.session.libsignal.utilities.logging.Log object MessageSenderEncryption { - internal fun encryptWithSessionProtocol(plaintext: ByteArray, recipientPublicKey: String): ByteArray{ - return MessagingConfiguration.shared.sessionProtocol.encrypt(plaintext, recipientPublicKey) + private val sodium by lazy { LazySodiumAndroid(SodiumAndroid()) } + + /** + * Encrypts `plaintext` using the Session protocol for `hexEncodedX25519PublicKey`. + * + * @param plaintext the plaintext to encrypt. Must already be padded. + * @param recipientHexEncodedX25519PublicKey the X25519 public key to encrypt for. Could be the Session ID of a user, or the public key of a closed group. + * + * @return the encrypted message. + */ + internal fun encryptWithSessionProtocol(plaintext: ByteArray, recipientHexEncodedX25519PublicKey: String): ByteArray{ + val context = MessagingConfiguration.shared.context + val userED25519KeyPair = KeyPairUtilities.getUserED25519KeyPair(context) ?: throw Error.NoUserED25519KeyPair + val recipientX25519PublicKey = Hex.fromStringCondensed(recipientHexEncodedX25519PublicKey.removing05PrefixIfNeeded()) + + val verificationData = plaintext + userED25519KeyPair.publicKey.asBytes + recipientX25519PublicKey + val signature = ByteArray(Sign.BYTES) + try { + sodium.cryptoSignDetached(signature, verificationData, verificationData.size.toLong(), userED25519KeyPair.secretKey.asBytes) + } catch (exception: Exception) { + Log.d("Loki", "Couldn't sign message due to error: $exception.") + throw Error.SigningFailed + } + val plaintextWithMetadata = plaintext + userED25519KeyPair.publicKey.asBytes + signature + val ciphertext = ByteArray(plaintextWithMetadata.size + Box.SEALBYTES) + try { + sodium.cryptoBoxSeal(ciphertext, plaintextWithMetadata, plaintextWithMetadata.size.toLong(), recipientX25519PublicKey) + } catch (exception: Exception) { + Log.d("Loki", "Couldn't encrypt message due to error: $exception.") + throw Error.EncryptionFailed + } + + return ciphertext } } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java b/libsession/src/main/java/org/session/libsession/utilities/IdentityKeyUtil.java similarity index 72% rename from app/src/main/java/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java rename to libsession/src/main/java/org/session/libsession/utilities/IdentityKeyUtil.java index 275e7ac1bd..06de335b4c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java +++ b/libsession/src/main/java/org/session/libsession/utilities/IdentityKeyUtil.java @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.thoughtcrime.securesms.crypto; +package org.session.libsession.utilities; import android.content.Context; import android.content.SharedPreferences; @@ -23,7 +23,6 @@ import android.content.SharedPreferences.Editor; import androidx.annotation.NonNull; import org.session.libsignal.libsignal.ecc.ECPublicKey; -import org.thoughtcrime.securesms.backup.BackupProtos; import org.session.libsignal.libsignal.IdentityKey; import org.session.libsignal.libsignal.IdentityKeyPair; import org.session.libsignal.libsignal.InvalidKeyException; @@ -34,8 +33,6 @@ import org.session.libsignal.libsignal.ecc.ECPrivateKey; import org.session.libsignal.utilities.Base64; import java.io.IOException; -import java.util.LinkedList; -import java.util.List; /** * Utility class for working with identity keys. @@ -95,45 +92,6 @@ public class IdentityKeyUtil { save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(privateKey.serialize())); } - public static List getBackupRecords(@NonNull Context context) { - final String prefName = MASTER_SECRET_UTIL_PREFERENCES_NAME; - SharedPreferences preferences = context.getSharedPreferences(prefName, 0); - - LinkedList prefList = new LinkedList<>(); - - prefList.add(BackupProtos.SharedPreference.newBuilder() - .setFile(prefName) - .setKey(IDENTITY_PUBLIC_KEY_PREF) - .setValue(preferences.getString(IDENTITY_PUBLIC_KEY_PREF, null)) - .build()); - prefList.add(BackupProtos.SharedPreference.newBuilder() - .setFile(prefName) - .setKey(IDENTITY_PRIVATE_KEY_PREF) - .setValue(preferences.getString(IDENTITY_PRIVATE_KEY_PREF, null)) - .build()); - if (preferences.contains(ED25519_PUBLIC_KEY)) { - prefList.add(BackupProtos.SharedPreference.newBuilder() - .setFile(prefName) - .setKey(ED25519_PUBLIC_KEY) - .setValue(preferences.getString(ED25519_PUBLIC_KEY, null)) - .build()); - } - if (preferences.contains(ED25519_SECRET_KEY)) { - prefList.add(BackupProtos.SharedPreference.newBuilder() - .setFile(prefName) - .setKey(ED25519_SECRET_KEY) - .setValue(preferences.getString(ED25519_SECRET_KEY, null)) - .build()); - } - prefList.add(BackupProtos.SharedPreference.newBuilder() - .setFile(prefName) - .setKey(LOKI_SEED) - .setValue(preferences.getString(LOKI_SEED, null)) - .build()); - - return prefList; - } - public static String retrieve(Context context, String key) { SharedPreferences preferences = context.getSharedPreferences(MASTER_SECRET_UTIL_PREFERENCES_NAME, 0); return preferences.getString(key, null); diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/KeyPairUtilities.kt b/libsession/src/main/java/org/session/libsession/utilities/KeyPairUtilities.kt similarity index 96% rename from app/src/main/java/org/thoughtcrime/securesms/loki/utilities/KeyPairUtilities.kt rename to libsession/src/main/java/org/session/libsession/utilities/KeyPairUtilities.kt index 8acd255597..92326cfc68 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/KeyPairUtilities.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/KeyPairUtilities.kt @@ -1,11 +1,10 @@ -package org.thoughtcrime.securesms.loki.utilities +package org.session.libsession.utilities import android.content.Context import com.goterl.lazycode.lazysodium.LazySodiumAndroid import com.goterl.lazycode.lazysodium.SodiumAndroid import com.goterl.lazycode.lazysodium.utils.Key import com.goterl.lazycode.lazysodium.utils.KeyPair -import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.session.libsignal.utilities.Base64 import org.session.libsignal.utilities.Hex import org.session.libsignal.libsignal.ecc.DjbECPrivateKey diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/crypto/SessionProtocol.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/crypto/SessionProtocol.kt index c095a1cd76..3851919052 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/crypto/SessionProtocol.kt +++ b/libsignal/src/main/java/org/session/libsignal/service/loki/api/crypto/SessionProtocol.kt @@ -17,17 +17,6 @@ interface SessionProtocol { object DecryptionFailed : Exception("Couldn't decrypt message.") object InvalidSignature : Exception("Invalid message signature.") } - - /** - * Encrypts `plaintext` using the Session protocol for `hexEncodedX25519PublicKey`. - * - * @param plaintext the plaintext to encrypt. Must already be padded. - * @param recipientHexEncodedX25519PublicKey the X25519 public key to encrypt for. Could be the Session ID of a user, or the public key of a closed group. - * - * @return the encrypted message. - */ - fun encrypt(plaintext: ByteArray, recipientHexEncodedX25519PublicKey: String): ByteArray - /** * Decrypts `ciphertext` using the Session protocol and `x25519KeyPair`. *