Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency issue when decrypting/encrypting by keystore keys #46

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

jayjaykim
Copy link

@jayjaykim jayjaykim commented Dec 27, 2018

there's a concurrency issue while decrypting/encrypting something by keystore keys at the same time like below. So I added some code fragments that you can test it and added synchronization and retry logic to resolve the issue. Please review it seriously.
It is my app's most serious issue.
you can calibrate sleep time in retry logic. It seems like just 1 nano seconds also works like charm...

2018-12-27 14:12:20.278 14509-14624/devliving.online.securedpreferencestoresample W/System.err: javax.crypto.IllegalBlockSizeException
2018-12-27 14:12:20.278 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:519)
2018-12-27 14:12:20.278 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at javax.crypto.Cipher.doFinal(Cipher.java:1736)
2018-12-27 14:12:20.278 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at devliving.online.securedpreferencestore.EncryptionManager.encryptAES(EncryptionManager.java:404)
2018-12-27 14:12:20.278 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at devliving.online.securedpreferencestore.EncryptionManager.encrypt(EncryptionManager.java:245)
2018-12-27 14:12:20.279 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at devliving.online.securedpreferencestore.EncryptionManager.tryEncrypt(EncryptionManager.java:178)
2018-12-27 14:12:20.279 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at devliving.online.securedpreferencestore.EncryptionManager.encrypt(EncryptionManager.java:290)
2018-12-27 14:12:20.279 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at devliving.online.securedpreferencestore.SecuredPreferenceStore$Editor.putString(SecuredPreferenceStore.java:299)
2018-12-27 14:12:20.279 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at devliving.online.securedpreferencestoresample.MainActivity.saveData(MainActivity.java:161)
2018-12-27 14:12:20.279 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at devliving.online.securedpreferencestoresample.MainActivity$3$1.run(MainActivity.java:84)
2018-12-27 14:12:20.279 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at java.lang.Thread.run(Thread.java:764)
2018-12-27 14:12:20.279 14509-14624/devliving.online.securedpreferencestoresample W/System.err: Caused by: android.security.KeyStoreException: Invalid operation handle
2018-12-27 14:12:20.279 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at android.security.KeyStore.getKeyStoreException(KeyStore.java:1137)
2018-12-27 14:12:20.280 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.update(KeyStoreCryptoOperationChunkedStreamer.java:132)
2018-12-27 14:12:20.280 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:217)
2018-12-27 14:12:20.280 14509-14624/devliving.online.securedpreferencestoresample W/System.err: at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
2018-12-27 14:12:20.280 14509-14624/devliving.online.securedpreferencestoresample W/System.err: ... 9 more (edited)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant