From 0f98c5c0e07e4cbbd073eee1c699077891209f60 Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Mon, 28 Sep 2026 17:32:42 +0200 Subject: [PATCH] Migrate `SqliteStoreBuilder.passphrase` usage to use `highEntropyPassphrase` (#7802) * Migrate `SqliteStoreBuilder.passphrase` usage to use `highEntropyPassphrase` This is the first step in migrating existing sessions from using a passphrase that needs key derivation, slowing down opening SDK DBs to just a raw key using the already random keys of the 'passphrase' * Remove no longer necessary `keyOfSize` extension function --- .../matrix/impl/storage/SqliteStoreBuilder.kt | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/storage/SqliteStoreBuilder.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/storage/SqliteStoreBuilder.kt index 4cfcf1a95e..394e918903 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/storage/SqliteStoreBuilder.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/storage/SqliteStoreBuilder.kt @@ -7,11 +7,13 @@ package io.element.android.libraries.matrix.impl.storage +import android.util.Base64 import io.element.android.libraries.androidutils.crypto.ClientSecret import io.element.android.libraries.core.data.ByteUnit import io.element.android.libraries.core.data.megaBytes import io.element.android.libraries.matrix.api.paths.SessionPaths import org.matrix.rustcomponents.sdk.ClientBuilder +import uniffi.matrix_sdk_sqlite.Base64Variant import org.matrix.rustcomponents.sdk.SqliteStoreBuilder as SdkSqliteStoreBuilder /** @@ -40,10 +42,15 @@ class RustSqliteStoreBuilder( override fun secret(clientSecret: ClientSecret?): SqliteStoreBuilder { when (clientSecret) { null -> Unit - is ClientSecret.Passphrase -> inner = inner.passphrase(clientSecret.value) + is ClientSecret.Passphrase -> { + // Use high entropy passphrase to migrate a passphrase to a raw key. + val base64Passphrase = Base64.decode(clientSecret.value, Base64.NO_WRAP or Base64.NO_PADDING) + inner = inner.highEntropyPassphrase(base64Passphrase, Base64Variant.UNPADDED) + // Once the passphrase is migrated, we can derive a raw key from it and use that for the SDK, replacing the line above. +// inner = inner.key(base64Passphrase) + } is ClientSecret.RawKey -> { - // Ensure the key is 32 bytes long, as required by the SDK - inner = inner.key(clientSecret.keyOfSize(32)) + inner = inner.key(clientSecret.bytes) } } return this @@ -53,15 +60,3 @@ class RustSqliteStoreBuilder( return clientBuilder.sqliteStore(this.inner) } } - -private fun ClientSecret.RawKey.keyOfSize(size: Int): ByteArray { - return if (bytes.size == 32) { - bytes - } else if (bytes.size < 32) { - // If the key is shorter than 32 bytes, pad it with zeros - bytes + ByteArray(32 - bytes.size) - } else { - // Otherwise, take the first 32 bytes of the key - bytes.copyOfRange(0, 32) - } -}