Skip to content

Fix catching authentication errors on Android 14 Pixel devices #756

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

janicduplessis
Copy link

@janicduplessis janicduplessis commented May 16, 2025

When encrypting values on Android 14 Pixel devices using AES_GCM it sometimes throw a different exception sometimes (I assume it is some bug in that sdk version). Here's the stack trace:

javax.crypto.IllegalBlockSizeException
java.io.IOException: javax.crypto.IllegalBlockSizeException
at javax.crypto.CipherOutputStream.close(CipherOutputStream.java:214)
at kotlin.io.CloseableKt.closeFinally(Closeable.kt:56)
at com.oblador.keychain.cipherStorage.CipherStorageBase.encryptString(CipherStorageBase.kt:373)
at com.oblador.keychain.cipherStorage.CipherStorageKeystoreAesGcm.encryptString(CipherStorageKeystoreAesGcm.kt:262)
at com.oblador.keychain.cipherStorage.CipherStorageKeystoreAesGcm.encrypt(CipherStorageKeystoreAesGcm.kt:97)
at com.oblador.keychain.KeychainModule.encryptToResult(KeychainModule.kt:587)
at com.oblador.keychain.KeychainModule.access$encryptToResult(KeychainModule.kt:38)
at com.oblador.keychain.KeychainModule$setGenericPassword$1.invokeSuspend(KeychainModule.kt:228)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:589)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:832)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:720)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:707)
Caused by: javax.crypto.IllegalBlockSizeException
at android.security.keystore2.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:605)
at javax.crypto.Cipher.doFinal(Cipher.java:1957)
at javax.crypto.CipherOutputStream.close(CipherOutputStream.java:210)
at kotlin.io.CloseableKt.closeFinally(Closeable.kt:56) 
at com.oblador.keychain.cipherStorage.CipherStorageBase.encryptString(CipherStorageBase.kt:373) 
at com.oblador.keychain.cipherStorage.CipherStorageKeystoreAesGcm.encryptString(CipherStorageKeystoreAesGcm.kt:262) 
at com.oblador.keychain.cipherStorage.CipherStorageKeystoreAesGcm.encrypt(CipherStorageKeystoreAesGcm.kt:97) 
at com.oblador.keychain.KeychainModule.encryptToResult(KeychainModule.kt:587) 
at com.oblador.keychain.KeychainModule.access$encryptToResult(KeychainModule.kt:38) 
at com.oblador.keychain.KeychainModule$setGenericPassword$1.invokeSuspend(KeychainModule.kt:228) 
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101) 
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:589) 
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:832) 
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:720) 
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:707) 
Caused by: android.security.KeyStoreException: Key user not authenticated (internal Keystore code: -26 message: system/security/keystore2/src/operation.rs:836: KeystoreOperation::update

Caused by:
0: system/security/keystore2/src/operation.rs:390: Update failed.
1: Error::Km(r#KEY_USER_NOT_AUTHENTICATED)) (public error code: 2 internal Keystore code: -26)
at android.security.KeyStore2.getKeyStoreException(KeyStore2.java:386)
at android.security.KeyStoreOperation.handleExceptions(KeyStoreOperation.java:78)
at android.security.KeyStoreOperation.update(KeyStoreOperation.java:115)
at android.security.keystore2.KeyStoreCryptoOperationChunkedStreamer$MainDataStream.update(KeyStoreCryptoOperationChunkedStreamer.java:222)
at android.security.keystore2.KeyStoreCryptoOperationChunkedStreamer.update(KeyStoreCryptoOperationChunkedStreamer.java:156)
at android.security.keystore2.AndroidKeyStoreCipherSpiBase.engineUpdate(AndroidKeyStoreCipherSpiBase.java:430)
at javax.crypto.Cipher.update(Cipher.java:1741)
at javax.crypto.CipherOutputStream.write(CipherOutputStream.java:158)
at javax.crypto.CipherOutputStream.write(CipherOutputStream.java:144)
... 12 more

To detect it I added a recursive check on cause to find the KeyStoreException containing Key user not authenticated.

Tested that this works on Pixel 5 using Android 14.

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