Skip to content

Commit

Permalink
Add entry to keyBackupService to expose deriveKey, which becomes in…
Browse files Browse the repository at this point in the history
…ternal

And format code
  • Loading branch information
bmarty committed Apr 12, 2022
1 parent 1402416 commit 3735ac3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,9 @@ interface KeysBackupService {
fun getKeyBackupRecoveryKeyInfo(): SavedKeyBackupKeyInfo?

fun isValidRecoveryKeyForCurrentVersion(recoveryKey: String, callback: MatrixCallback<Boolean>)

fun computePrivateKey(passphrase: String,
privateKeySalt: String,
privateKeyIterations: Int,
progressListener: ProgressListener): ByteArray
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,29 @@ import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupLastVersio
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupService
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupState
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupStateListener
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupVersionTrust
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersion
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersionResult
import org.matrix.android.sdk.api.session.crypto.keysbackup.MegolmBackupCreationInfo
import org.matrix.android.sdk.api.session.crypto.keysbackup.SavedKeyBackupKeyInfo
import org.matrix.android.sdk.api.session.crypto.keysbackup.computeRecoveryKey
import org.matrix.android.sdk.api.session.crypto.keysbackup.extractCurveKeyFromRecoveryKey
import org.matrix.android.sdk.api.session.crypto.keysbackup.toKeysVersionResult
import org.matrix.android.sdk.api.session.crypto.model.ImportRoomKeysResult
import org.matrix.android.sdk.api.util.awaitCallback
import org.matrix.android.sdk.api.util.fromBase64
import org.matrix.android.sdk.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM_BACKUP
import org.matrix.android.sdk.internal.crypto.MXOlmDevice
import org.matrix.android.sdk.internal.crypto.MegolmSessionData
import org.matrix.android.sdk.internal.crypto.ObjectSigner
import org.matrix.android.sdk.internal.crypto.actions.MegolmSessionDataImporter
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupVersionTrust
import org.matrix.android.sdk.internal.crypto.keysbackup.model.KeysBackupVersionTrustSignature
import org.matrix.android.sdk.internal.crypto.keysbackup.model.MegolmBackupAuthData
import org.matrix.android.sdk.api.session.crypto.keysbackup.MegolmBackupCreationInfo
import org.matrix.android.sdk.internal.crypto.keysbackup.model.SignalableMegolmBackupAuthData
import org.matrix.android.sdk.internal.crypto.keysbackup.model.rest.BackupKeysResult
import org.matrix.android.sdk.internal.crypto.keysbackup.model.rest.CreateKeysBackupVersionBody
import org.matrix.android.sdk.internal.crypto.keysbackup.model.rest.KeyBackupData
import org.matrix.android.sdk.internal.crypto.keysbackup.model.rest.KeysBackupData
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersion
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersionResult
import org.matrix.android.sdk.api.session.crypto.keysbackup.toKeysVersionResult
import org.matrix.android.sdk.internal.crypto.keysbackup.model.rest.RoomKeysBackupData
import org.matrix.android.sdk.internal.crypto.keysbackup.model.rest.UpdateKeysBackupVersionBody
import org.matrix.android.sdk.internal.crypto.keysbackup.tasks.CreateKeysBackupVersionTask
Expand All @@ -74,7 +75,6 @@ import org.matrix.android.sdk.internal.crypto.keysbackup.tasks.StoreRoomSessionD
import org.matrix.android.sdk.internal.crypto.keysbackup.tasks.StoreRoomSessionsDataTask
import org.matrix.android.sdk.internal.crypto.keysbackup.tasks.StoreSessionsDataTask
import org.matrix.android.sdk.internal.crypto.keysbackup.tasks.UpdateKeysBackupVersionTask
import org.matrix.android.sdk.api.session.crypto.model.ImportRoomKeysResult
import org.matrix.android.sdk.internal.crypto.model.OlmInboundGroupSessionWrapper2
import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
import org.matrix.android.sdk.internal.crypto.store.db.model.KeysBackupDataEntity
Expand Down Expand Up @@ -1099,6 +1099,13 @@ internal class DefaultKeysBackupService @Inject constructor(
}
}

override fun computePrivateKey(passphrase: String,
privateKeySalt: String,
privateKeyIterations: Int,
progressListener: ProgressListener): ByteArray {
return deriveKey(passphrase, privateKeySalt, privateKeyIterations, progressListener)
}

/**
* Enable backing up of keys.
* This method will update the state and will start sending keys in nominal case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import kotlin.experimental.xor
private const val SALT_LENGTH = 32
private const val DEFAULT_ITERATION = 500_000

data class GeneratePrivateKeyResult(
internal data class GeneratePrivateKeyResult(
// The private key
val privateKey: ByteArray,
// the salt used to generate the private key
val salt: String,
// number of key derivations done on the generated private key.
val iterations: Int)
val iterations: Int
)

/**
* Compute a private key from a password.
Expand All @@ -46,7 +47,9 @@ data class GeneratePrivateKeyResult(
* @return a {privateKey, salt, iterations} tuple.
*/
@WorkerThread
fun generatePrivateKeyWithPassword(password: String, progressListener: ProgressListener?): GeneratePrivateKeyResult {
internal fun generatePrivateKeyWithPassword(password: String,
progressListener: ProgressListener?
): GeneratePrivateKeyResult {
val salt = generateSalt()
val iterations = DEFAULT_ITERATION
val privateKey = deriveKey(password, salt, iterations, progressListener)
Expand All @@ -65,10 +68,10 @@ fun generatePrivateKeyWithPassword(password: String, progressListener: ProgressL
* @return a private key.
*/
@WorkerThread
fun retrievePrivateKeyWithPassword(password: String,
salt: String,
iterations: Int,
progressListener: ProgressListener? = null): ByteArray {
internal fun retrievePrivateKeyWithPassword(password: String,
salt: String,
iterations: Int,
progressListener: ProgressListener? = null): ByteArray {
return deriveKey(password, salt, iterations, progressListener)
}

Expand All @@ -83,10 +86,10 @@ fun retrievePrivateKeyWithPassword(password: String,
* @return a private key.
*/
@WorkerThread
fun deriveKey(password: String,
salt: String,
iterations: Int,
progressListener: ProgressListener?): ByteArray {
internal fun deriveKey(password: String,
salt: String,
iterations: Int,
progressListener: ProgressListener?): ByteArray {
// Note: copied and adapted from MXMegolmExportEncryption
val t0 = System.currentTimeMillis()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import org.matrix.android.sdk.api.session.securestorage.SharedSecretStorageServi
import org.matrix.android.sdk.api.session.securestorage.SsssKeyCreationInfo
import org.matrix.android.sdk.api.util.awaitCallback
import org.matrix.android.sdk.api.util.toBase64NoPadding
import org.matrix.android.sdk.internal.crypto.keysbackup.deriveKey
import timber.log.Timber
import java.util.UUID
import javax.inject.Inject
Expand Down Expand Up @@ -72,14 +71,18 @@ class BackupToQuadSMigrationTask @Inject constructor(
extractCurveKeyFromRecoveryKey(params.recoveryKey)
} else if (!params.passphrase.isNullOrEmpty() && version.getAuthDataAsMegolmBackupAuthData()?.privateKeySalt != null) {
version.getAuthDataAsMegolmBackupAuthData()?.let { authData ->
deriveKey(params.passphrase, authData.privateKeySalt!!, authData.privateKeyIterations!!, object : ProgressListener {
override fun onProgress(progress: Int, total: Int) {
params.progressListener?.onProgress(WaitingViewData(
stringProvider.getString(R.string.bootstrap_progress_checking_backup_with_info,
"$progress/$total")
))
}
})
keysBackupService.computePrivateKey(
params.passphrase,
authData.privateKeySalt!!,
authData.privateKeyIterations!!,
object : ProgressListener {
override fun onProgress(progress: Int, total: Int) {
params.progressListener?.onProgress(WaitingViewData(
stringProvider.getString(R.string.bootstrap_progress_checking_backup_with_info,
"$progress/$total")
))
}
})
}
} else null)
?: return Result.IllegalParams
Expand Down

0 comments on commit 3735ac3

Please sign in to comment.