Avoid SQLCipher key derivation (#6774)

* Avoid SQLCipher key derivation by manually providing the generated as a raw key: this needs a series of DB re-key operations, performed as part of a 'dummy' DB migration

* Set key generation length to 32 bytes in `RandomSecretPassphraseProvider`.

* Use `ClientSecret` for handling the keys for SQLCipher. Move `ClientSecret` to `:libraries:androidutils` so it's shared.
This commit is contained in:
Jorge Martin Espinosa
2026-05-27 17:25:51 +02:00
committed by GitHub
parent 4cafdae22d
commit 1e00353f29
21 changed files with 185 additions and 43 deletions
@@ -17,7 +17,9 @@ import dev.zacsweers.metro.SingleIn
import io.element.android.libraries.di.annotations.ApplicationContext
import io.element.android.libraries.sessionstorage.impl.SessionDatabase
import io.element.encrypteddb.SqlCipherDriverFactory
import io.element.encrypteddb.passphrase.RandomSecretPassphraseProvider
import io.element.encrypteddb.passphrase.RandomDatabaseSecretProvider
import io.element.encrypteddb.utils.ReplaceDatabaseKey
import timber.log.Timber
@BindingContainer
@ContributesTo(AppScope::class)
@@ -36,9 +38,20 @@ object SessionStorageModule {
parentDir.mkdirs()
}
val passphraseProvider = RandomSecretPassphraseProvider(context, secretFile)
val rekeyMigrationVersion = 11L
val passphraseProvider = RandomDatabaseSecretProvider(context, secretFile)
val driver = SqlCipherDriverFactory(passphraseProvider)
.create(SessionDatabase.Schema, "$name.db", context)
.create(
schema = SessionDatabase.Schema,
name = "$name.db",
context = context,
) { db, oldVersion, newVersion ->
Timber.d("Migrating $name database from version $oldVersion to $newVersion")
if (rekeyMigrationVersion in oldVersion..newVersion) {
ReplaceDatabaseKey(passphraseProvider).replaceKey(name, db)
}
}
return SessionDatabase(driver)
}
}
@@ -0,0 +1,2 @@
-- Migrate DB from version 11
-- Dummy migration to trigger a re-key