This commit is contained in:
Stanislav Dmitrenko 2025-06-27 17:32:26 +00:00 committed by GitHub
commit 8cfb9bb698
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,8 +1,8 @@
package chat.simplex.common.platform package chat.simplex.common.platform
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.security.keystore.KeyGenParameterSpec import android.os.Build
import android.security.keystore.KeyProperties import android.security.keystore.*
import chat.simplex.common.views.helpers.AlertManager import chat.simplex.common.views.helpers.AlertManager
import chat.simplex.common.views.helpers.generalGetString import chat.simplex.common.views.helpers.generalGetString
import chat.simplex.res.MR import chat.simplex.res.MR
@ -71,13 +71,24 @@ internal class Cryptor: CryptorInterface {
private fun createSecretKey(alias: String): SecretKey? { private fun createSecretKey(alias: String): SecretKey? {
if (keyStore.containsAlias(alias)) return getSecretKey(alias) if (keyStore.containsAlias(alias)) return getSecretKey(alias)
val keyGenerator: KeyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM, "AndroidKeyStore") val keyGenerator: KeyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM, "AndroidKeyStore")
keyGenerator.init( val builder = KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT) .setBlockModes(BLOCK_MODE)
.setBlockModes(BLOCK_MODE) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
.build() try {
) keyGenerator.init(builder.setIsStrongBoxBacked(true).build())
return keyGenerator.generateKey() val key = keyGenerator.generateKey()
Log.w(TAG, "StrongBox support is present")
key
} catch (e: StrongBoxUnavailableException) {
Log.w(TAG, "No StrongBox support")
keyGenerator.init(builder.setIsStrongBoxBacked(false).build())
keyGenerator.generateKey()
}
} else {
keyGenerator.init(builder.build())
keyGenerator.generateKey()
}
} }
private fun getSecretKey(alias: String): SecretKey? { private fun getSecretKey(alias: String): SecretKey? {