Skip to content

Commit

Permalink
AND-6517 [Biometrics] Added locking when storing data to "Authenticat…
Browse files Browse the repository at this point in the history
…edStorage"
  • Loading branch information
iiiburnyiii committed Mar 22, 2024
1 parent 6d08130 commit 9d5726f
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.tangem.common.authentication.keystore.MasterKeyConfigs
import com.tangem.common.services.secure.SecureStorage
import com.tangem.crypto.operations.AESCipherOperations
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import javax.crypto.SecretKey

Expand All @@ -20,6 +22,8 @@ class AuthenticatedStorage(
private val keystoreManager: KeystoreManager,
) {

private val storeMutex = Mutex()

/**
* Retrieves and decrypts data from the storage after necessary user authentication.
*
Expand Down Expand Up @@ -166,10 +170,12 @@ class AuthenticatedStorage(
* @param keyAlias The unique identifier which will be associated with the encrypted data.
* @param data The plain data to be encrypted and stored.
*/
suspend fun store(keyAlias: String, data: ByteArray) = withContext(Dispatchers.IO) {
val encryptedData = encrypt(keyAlias, data)
suspend fun store(keyAlias: String, data: ByteArray) = storeMutex.withLock {
withContext(Dispatchers.IO) {
val encryptedData = encrypt(keyAlias, data)

secureStorage.store(encryptedData, keyAlias)
secureStorage.store(encryptedData, keyAlias)
}
}

/**
Expand Down

0 comments on commit 9d5726f

Please # to comment.