Merge pull request #5871 from vector-im/feature/bca/fix_freeze_setup_recovery

fix doing password stretching on main thread
This commit is contained in:
Benoit Marty 2022-04-29 11:21:30 +02:00 committed by GitHub
commit 271eb31162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

1
changelog.d/5871.bugfix Normal file
View File

@ -0,0 +1 @@
Fix UX freezing when creating secure backup

View File

@ -66,7 +66,7 @@ internal class DefaultSharedSecretStorageService @Inject constructor(
key: SsssKeySpec?, key: SsssKeySpec?,
keyName: String, keyName: String,
keySigner: KeySigner?): SsssKeyCreationInfo { keySigner: KeySigner?): SsssKeyCreationInfo {
return withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.main) { return withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.computation) {
val bytes = (key as? RawBytesKeySpec)?.privateKey val bytes = (key as? RawBytesKeySpec)?.privateKey
?: ByteArray(32).also { ?: ByteArray(32).also {
SecureRandom().nextBytes(it) SecureRandom().nextBytes(it)
@ -99,7 +99,7 @@ internal class DefaultSharedSecretStorageService @Inject constructor(
passphrase: String, passphrase: String,
keySigner: KeySigner, keySigner: KeySigner,
progressListener: ProgressListener?): SsssKeyCreationInfo { progressListener: ProgressListener?): SsssKeyCreationInfo {
return withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.main) { return withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.computation) {
val privatePart = generatePrivateKeyWithPassword(passphrase, progressListener) val privatePart = generatePrivateKeyWithPassword(passphrase, progressListener)
val storageKeyContent = SecretStorageKeyContent( val storageKeyContent = SecretStorageKeyContent(
@ -158,7 +158,7 @@ internal class DefaultSharedSecretStorageService @Inject constructor(
} }
override suspend fun storeSecret(name: String, secretBase64: String, keys: List<SharedSecretStorageService.KeyRef>) { override suspend fun storeSecret(name: String, secretBase64: String, keys: List<SharedSecretStorageService.KeyRef>) {
withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.main) { withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.computation) {
val encryptedContents = HashMap<String, EncryptedSecretContent>() val encryptedContents = HashMap<String, EncryptedSecretContent>()
keys.forEach { keys.forEach {
val keyId = it.keyId val keyId = it.keyId
@ -316,7 +316,7 @@ internal class DefaultSharedSecretStorageService @Inject constructor(
val algorithm = key.keyInfo.content val algorithm = key.keyInfo.content
if (SSSS_ALGORITHM_CURVE25519_AES_SHA2 == algorithm.algorithm) { if (SSSS_ALGORITHM_CURVE25519_AES_SHA2 == algorithm.algorithm) {
val keySpec = secretKey as? RawBytesKeySpec ?: throw SharedSecretStorageError.BadKeyFormat val keySpec = secretKey as? RawBytesKeySpec ?: throw SharedSecretStorageError.BadKeyFormat
return withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.main) { return withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.computation) {
// decrypt from recovery key // decrypt from recovery key
withOlmDecryption { olmPkDecryption -> withOlmDecryption { olmPkDecryption ->
olmPkDecryption.setPrivateKey(keySpec.privateKey) olmPkDecryption.setPrivateKey(keySpec.privateKey)
@ -331,7 +331,7 @@ internal class DefaultSharedSecretStorageService @Inject constructor(
} }
} else if (SSSS_ALGORITHM_AES_HMAC_SHA2 == algorithm.algorithm) { } else if (SSSS_ALGORITHM_AES_HMAC_SHA2 == algorithm.algorithm) {
val keySpec = secretKey as? RawBytesKeySpec ?: throw SharedSecretStorageError.BadKeyFormat val keySpec = secretKey as? RawBytesKeySpec ?: throw SharedSecretStorageError.BadKeyFormat
return withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.main) { return withContext(cryptoCoroutineScope.coroutineContext + coroutineDispatchers.computation) {
decryptAesHmacSha2(keySpec, name, secretContent) decryptAesHmacSha2(keySpec, name, secretContent)
} }
} else { } else {