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

getGenericPassword is too slow on android 10 #337

Closed
sm2017 opened this issue May 2, 2020 · 82 comments
Closed

getGenericPassword is too slow on android 10 #337

sm2017 opened this issue May 2, 2020 · 82 comments
Assignees

Comments

@sm2017
Copy link

sm2017 commented May 2, 2020

I have slow startup on android 10 , After checking , I understand that getGenericPassword methods takes 7-8 seconds on android 10
I run on android 7 and 6 too , It takes about 600-700ms

@sm2017
Copy link
Author

sm2017 commented May 2, 2020

I downgraded to v4.0.5 , it is fast on android 10 , just 30ms

@patrickschmelter
Copy link

patrickschmelter commented May 4, 2020

react-native-keychain: 6.0.0
react-native: 0.62.2

Same issue for me. In Simulator (API28) and on a Huawei P30 Pro (API29) it works super fast, on a Samsung SM-J730F (API28) it takes way beyond 10 seconds sometimes and fails with following exception. I experience it only happening the very first time after installing the app or wiping app data.

2020-05-04 15:31:18.866 30385-30539/? W/CipherStorageBase: StrongBox security storage is not available.
    android.security.keystore.StrongBoxUnavailableException: Failed to generate key pair
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:554)
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:499)
        at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:727)
        at com.oblador.keychain.cipherStorage.CipherStorageKeystoreRsaEcb.generateKey(CipherStorageKeystoreRsaEcb.java:256)
        at com.oblador.keychain.cipherStorage.CipherStorageBase.tryGenerateStrongBoxSecurityKey(CipherStorageBase.java:444)
        at com.oblador.keychain.cipherStorage.CipherStorageBase.generateKeyAndStoreUnderAlias(CipherStorageBase.java:391)
        at com.oblador.keychain.KeychainModule.internalWarmingBestCipher(KeychainModule.java:171)
        at com.oblador.keychain.KeychainModule.lambda$NuQDyTTfZc67dTNiVeEDbYNRCJw(Unknown Source:0)
        at com.oblador.keychain.-$$Lambda$KeychainModule$NuQDyTTfZc67dTNiVeEDbYNRCJw.run(Unknown Source:2)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: android.security.KeyStoreException: No StrongBox available
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:554) 
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:499) 
        at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:727) 
        at com.oblador.keychain.cipherStorage.CipherStorageKeystoreRsaEcb.generateKey(CipherStorageKeystoreRsaEcb.java:256) 
        at com.oblador.keychain.cipherStorage.CipherStorageBase.tryGenerateStrongBoxSecurityKey(CipherStorageBase.java:444) 
        at com.oblador.keychain.cipherStorage.CipherStorageBase.generateKeyAndStoreUnderAlias(CipherStorageBase.java:391) 
        at com.oblador.keychain.KeychainModule.internalWarmingBestCipher(KeychainModule.java:171) 
        at com.oblador.keychain.KeychainModule.lambda$NuQDyTTfZc67dTNiVeEDbYNRCJw(Unknown Source:0) 
        at com.oblador.keychain.-$$Lambda$KeychainModule$NuQDyTTfZc67dTNiVeEDbYNRCJw.run(Unknown Source:2) 
        at java.lang.Thread.run(Thread.java:764) 

@patrickschmelter
Copy link

I can confirm that downgrading to 4.0.5 solves the issue. Maybe linked to #314

@sm2017
Copy link
Author

sm2017 commented May 5, 2020

@oblador @vonovak take a look please

@sm2017
Copy link
Author

sm2017 commented May 11, 2020

@oblador @vonovak reply please

@vonovak
Copy link
Collaborator

vonovak commented May 11, 2020

@sm2017 given more than one person is seeing this, I guess the issue is indeed present.
I'm dealing with other stuff now and this is not a priority for me.

Cc @OleksandrKucherenko as he might have more insight.

As always, if you have an issue, I recommend you contribute a PR with a fix. Thank you!

@tommeier
Copy link

We've also found the same - downgraded has solved it - thanks for spotting this @sm2017 !

@deecewan
Copy link

Did a bit of digging - KeystoreAESCBC seems to be very slow when running CipherStorageBase#extractKey, specifically when doing keyStore.getKey(safeAlias, null). I'm not too sure why there is a slowdown between 4.0.5 and 6.0.0 yet, because they seem to do roughly the same thing when encrypting/decrypyting for storage.

@deecewan
Copy link

deecewan commented May 18, 2020

from 4.0.5:

// CipherStorageKeystoreAESCBC.java line 101
key = keyStore.getKey(service, null);
// 5ms average over 5 runs

from 6.0.0

// CipherStorageBase.java line 234
key = keyStore.getKey(safeAlias, null);
// 3637ms average over 5 runs

Measurements taken using System.nanoTime() right before/after the call. Samsung Galaxy S10+, running Android 10.

Edit: yup, i'm stumped. The only thing that looks different is that there is now a cache on the keystore (

public KeyStore getKeyStoreAndLoad() throws KeyStoreAccessException {
) whereas that didn't used to be there

Edit edit: Tried making it not a cached instance, tho i am not sure quite how or why that would fix it. It didn't fix it. So it seems that the same method call is taking longer, when the only variable is the package version 😅

Edit: looking into this on a bit further, based on @patrickschmelter's comment. Looking in the android source, it seems like StrongBox is not available on the particular hardware. and throwing early in the tryGenerateStrongBoxSecurityKey (before actually attempting to generate the key) makes the behaviour fast again.

OK. Looks like that's a bit of a red herring. I've been playing around a lot and it seems like the "warm up" happens, and it also gets stuck. Until a log line appears that says CertificatePolicyCache: Creating new instance of CertificatePolicyCache. Then that completes, and it seems to work fast again. Googling around for CertificatePolicyCache doesn't really yield any results 😞.

Final edit for a while: seems like the new RSA type is what's doing it. There seems to be a lock involved, and the system hangs until it's ready. I managed to get the setGenericPassword to work fairly quickly by explicitly setting storage: Keychain.STORAGE_TYPE.AES,, but unfortunately the getGenericPassword is still slow 😞

@sm2017
Copy link
Author

sm2017 commented May 27, 2020

@oblador @vonovak sorry for mention , As I am not an Android developer , I cannot fix this problem and make a PR
I think @deecewan's comment can help to fix it , Please take a look again

@OleksandrKucherenko
Copy link
Contributor

Possible optimization:

boolean hasStrongBox() {
       return mActivity.getPackageManager()
           .hasSystemFeature(PackageManager.FEATURE_STRONGBOX_KEYSTORE)
}

Instead of trying and catching the exception we can check the hardware features availability.

src: https://stackoverflow.com/questions/58502299/how-to-check-for-strongbox-keymaster-hardware-availability-before-key-generation

@deecewan
Copy link

@OleksandrKucherenko I tried doing that, too, and it was not noticeably quicker. There is a good chance that I got it mixed up with other tests I was trying, tho.

@OleksandrKucherenko
Copy link
Contributor

https://github.com/oblador/react-native-keychain/compare/master...OleksandrKucherenko:fix-storngbox-detect?expand=1

quick implementation... I don't think it will improve anything significantly, but it's a good code to have in lib.

@john-y-pazekha
Copy link
Contributor

john-y-pazekha commented May 28, 2020

@sm2017
@patrickschmelter
@deecewan
@tommeier
@vonovak

Gentlemen, I need your help to reproduce this issue. Since it happens only on specific combination of phone and Android version, I need to know what exactly you're running.

Could you please use this APK and attach here the report it produced?
get-device-info.apk.zip

Best regards,
John

@sm2017
Copy link
Author

sm2017 commented May 28, 2020

@john-y-pazekha as you joined 5 hours ago to github, and send us an APK file
I cannot trust you and I don't install that apk

I can send you exact details about model of device

@sm2017
Copy link
Author

sm2017 commented May 28, 2020

@john-y-pazekha here you are
IMG-20200528-WA0014

@john-y-pazekha
Copy link
Contributor

@sm2017 Thanks a lot. Could you please run the APK I provided and attach the log? I'm particularly interested in VENDOR and PRODUCT fields to identify the device precisely.

I need this information because we couldn't reproduce this issue on the devices we have in-house; now we're going to order new devices but we must know what exactly to order.

@john-y-pazekha
Copy link
Contributor

@john-y-pazekha as you joined 5 hours ago to github, and send us an APK file
I cannot trust you and I don't install that apk

I totally understand your concern. Please rest assured that this APK is not malicious.

When installing an APK, you have a chance to review the permissions it requests. You can see that this one requests no permissions at all. There is no virus in it (sorry, I was too lazy, maybe in the next version :))))

Alternatively, you can clone the source from this repo and run it.

@sm2017
@patrickschmelter
@deecewan
@tommeier
@vonovak
Please use either way to produce the log. I really need this information.

@oblador
Copy link
Owner

oblador commented May 29, 2020

@sm2017: I can vouch for @john-y-pazekha

@sm2017
Copy link
Author

sm2017 commented May 29, 2020

@john-y-pazekha I tested on mi a2
Currently I have no access to the phone, may be later

@forkeer
Copy link

forkeer commented May 29, 2020

I also have this problem, has anyone reached a conclusion?
Why did you update, it's corrupted and the previous versions are correct

@john-y-pazekha
Copy link
Contributor

john-y-pazekha commented May 29, 2020

@forkeer Would you please include your device specs as requested here?

Gentlemen, I need your help to reproduce this issue. Since it happens only on specific combination of phone and Android version, I need to know what exactly you're running.

Could you please use this APK and attach here the report it produced?
get-device-info.apk.zip

Alternatively, you can clone the source from this repo and run it.

@forkeer
Copy link

forkeer commented May 29, 2020

@forkeer Would you please include your device specs as requested here?

Gentlemen, I need your help to reproduce this issue. Since it happens only on specific combination of phone and Android version, I need to know what exactly you're running.
Could you please use this APK and attach here the report it produced?
get-device-info.apk.zip
Alternatively, you can clone the source from this repo and run it.

i download but cant install
show error

@john-y-pazekha
Copy link
Contributor

i download but cant install
show error

I updated the APK, should work now. Sorry for the inconvenience.

Alternatively, you can clone the source from this repo and run it in Android Studio.

@forkeer
Copy link

forkeer commented May 29, 2020

{
"BOARD": "sdm660",
"LCT_BSP_VERSION": "0.0.1_180722",
"LCT_SINGALCARD_DULEMODE": "unknown",
"CPU_ABI2": "",
"HOST": "c4-miui-ota-bd46.bj",
"LQI_PRODUCT_DEVICE": "unknown",
"LCT_WATER_MARK": "unknown",
"SUPPORTED_64_BIT_ABIS": [
"arm64-v8a"
],
"CPU_ABI": "arm64-v8a",
"PERMISSIONS_REVIEW_REQUIRED": true,
"FINGERPRINT": "xiaomi/jasmine/jasmine_sprout:10/QKQ1.190910.002/V11.0.6.0.QDIMIXM:user/release-keys",
"PRODUCT": "jasmine",
"INTERNAL": "unknown",
"ID": "QKQ1.190910.002",
"LCT_WIFI_BRAND": "unknown",
"TYPE": "user",
"LCT_AUTOREGISTER_NAME": "unknown",
"LCT_BUILD_TYPE": "unknown",
"VERSION_TYPE": "unknown",
"DEVICE": "jasmine_sprout",
"BRAND": "xiaomi",
"LCT_HARDWARE_PLATFORM": "unknown",
"SUPPORTED_32_BIT_ABIS": [
"armeabi-v7a",
"armeabi"
],
"LCT_DEVICE_BLACK_UI": "unknown",
"BOOTLOADER": "unknown",
"CUSTOM_NAME": "unknown",
"IS_EMULATOR": false,
"LCT_BLUETOOTH_BRAND": "unknown",
"TAGS": "release-keys",
"CUSTOM_INTERNAL": "unknown",
"DISPLAY": "QKQ1.190910.002 V11.0.6.0.QDIMIXM",
"SUPPORTED_ABIS": [
"arm64-v8a",
"armeabi-v7a",
"armeabi"
],
"LCT_PROJECT_NAME": "jasmine",
"EXTERNAL": "unknown",
"LCT_NETWORK_TYPE": "unknown",
"LCT_PHONENUMBER_MINMATCH": "unknown",
"SERIAL": "unknown",
"BUILDDATE": "Tue Mar 10 19:55:26 WIB 2020",
"DISPLAY_HARDWARE": "unknown",
"LCT_ACCELERATOR_BRAND": "unknown",
"LCT_DEVICE_NAME": "unknown",
"LCT_OPERATOR_NAME": "unknown",
"TIME": 1583844926000,
"MODEL": "Mi A2",
"MANUFACTURER": "Xiaomi",
"USER": "builder",
"HARDWARE": "qcom",
"IS_DEBUGGABLE": false,
"IDEAFRIEND_NEED": "unknown",
"RADIO": "unknown",
"UNKNOWN": "unknown",
"LCT_EXTSTORAGE_TYPE": "unknown"
}

@john-y-pazekha
Copy link
Contributor

@forkeer
Thanks a lot!
What was the startup time?

@forkeer
Copy link

forkeer commented May 29, 2020

@forkeer
Thanks a lot!
What was the startup time?

From 6 to 8 seconds but averages 7 seconds

@cladjules
Copy link

I am using OnePlus 7 Pro with Android 10, but I am pretty sure we had the same issue with a Galaxy S8, I will confirm.

Thanks.

@rares-lupascu
Copy link

+1

@HLeshan
Copy link

HLeshan commented Jun 5, 2021

any updates ?

@genie09
Copy link

genie09 commented Jul 13, 2021

I also downgraded to 4.0.5.

@haryelramalho
Copy link

haryelramalho commented Aug 8, 2021

react-native-keychain: 6.0.0
react-native: 0.62.2

Same issue for me. In Simulator (API28) and on a Huawei P30 Pro (API29) it works super fast, on a Samsung SM-J730F (API28) it takes way beyond 10 seconds sometimes and fails with following exception. I experience it only happening the very first time after installing the app or wiping app data.

2020-05-04 15:31:18.866 30385-30539/? W/CipherStorageBase: StrongBox security storage is not available.
    android.security.keystore.StrongBoxUnavailableException: Failed to generate key pair
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:554)
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:499)
        at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:727)
        at com.oblador.keychain.cipherStorage.CipherStorageKeystoreRsaEcb.generateKey(CipherStorageKeystoreRsaEcb.java:256)
        at com.oblador.keychain.cipherStorage.CipherStorageBase.tryGenerateStrongBoxSecurityKey(CipherStorageBase.java:444)
        at com.oblador.keychain.cipherStorage.CipherStorageBase.generateKeyAndStoreUnderAlias(CipherStorageBase.java:391)
        at com.oblador.keychain.KeychainModule.internalWarmingBestCipher(KeychainModule.java:171)
        at com.oblador.keychain.KeychainModule.lambda$NuQDyTTfZc67dTNiVeEDbYNRCJw(Unknown Source:0)
        at com.oblador.keychain.-$$Lambda$KeychainModule$NuQDyTTfZc67dTNiVeEDbYNRCJw.run(Unknown Source:2)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: android.security.KeyStoreException: No StrongBox available
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:554) 
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:499) 
        at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:727) 
        at com.oblador.keychain.cipherStorage.CipherStorageKeystoreRsaEcb.generateKey(CipherStorageKeystoreRsaEcb.java:256) 
        at com.oblador.keychain.cipherStorage.CipherStorageBase.tryGenerateStrongBoxSecurityKey(CipherStorageBase.java:444) 
        at com.oblador.keychain.cipherStorage.CipherStorageBase.generateKeyAndStoreUnderAlias(CipherStorageBase.java:391) 
        at com.oblador.keychain.KeychainModule.internalWarmingBestCipher(KeychainModule.java:171) 
        at com.oblador.keychain.KeychainModule.lambda$NuQDyTTfZc67dTNiVeEDbYNRCJw(Unknown Source:0) 
        at com.oblador.keychain.-$$Lambda$KeychainModule$NuQDyTTfZc67dTNiVeEDbYNRCJw.run(Unknown Source:2) 
        at java.lang.Thread.run(Thread.java:764) 

Same error, how did you solve it? My app crashes in 4-5 seconds after opening and always on white screen.
I caught this error in a logcat

@ludovic-noirault
Copy link

react-native-keychain: 6.0.0
react-native: 0.62.2
Same issue for me. In Simulator (API28) and on a Huawei P30 Pro (API29) it works super fast, on a Samsung SM-J730F (API28) it takes way beyond 10 seconds sometimes and fails with following exception. I experience it only happening the very first time after installing the app or wiping app data.

2020-05-04 15:31:18.866 30385-30539/? W/CipherStorageBase: StrongBox security storage is not available.
    android.security.keystore.StrongBoxUnavailableException: Failed to generate key pair
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:554)
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:499)
        at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:727)
        at com.oblador.keychain.cipherStorage.CipherStorageKeystoreRsaEcb.generateKey(CipherStorageKeystoreRsaEcb.java:256)
        at com.oblador.keychain.cipherStorage.CipherStorageBase.tryGenerateStrongBoxSecurityKey(CipherStorageBase.java:444)
        at com.oblador.keychain.cipherStorage.CipherStorageBase.generateKeyAndStoreUnderAlias(CipherStorageBase.java:391)
        at com.oblador.keychain.KeychainModule.internalWarmingBestCipher(KeychainModule.java:171)
        at com.oblador.keychain.KeychainModule.lambda$NuQDyTTfZc67dTNiVeEDbYNRCJw(Unknown Source:0)
        at com.oblador.keychain.-$$Lambda$KeychainModule$NuQDyTTfZc67dTNiVeEDbYNRCJw.run(Unknown Source:2)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: android.security.KeyStoreException: No StrongBox available
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:554) 
        at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:499) 
        at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:727) 
        at com.oblador.keychain.cipherStorage.CipherStorageKeystoreRsaEcb.generateKey(CipherStorageKeystoreRsaEcb.java:256) 
        at com.oblador.keychain.cipherStorage.CipherStorageBase.tryGenerateStrongBoxSecurityKey(CipherStorageBase.java:444) 
        at com.oblador.keychain.cipherStorage.CipherStorageBase.generateKeyAndStoreUnderAlias(CipherStorageBase.java:391) 
        at com.oblador.keychain.KeychainModule.internalWarmingBestCipher(KeychainModule.java:171) 
        at com.oblador.keychain.KeychainModule.lambda$NuQDyTTfZc67dTNiVeEDbYNRCJw(Unknown Source:0) 
        at com.oblador.keychain.-$$Lambda$KeychainModule$NuQDyTTfZc67dTNiVeEDbYNRCJw.run(Unknown Source:2) 
        at java.lang.Thread.run(Thread.java:764) 

Same error, how did you solve it? My app crashes in 4-5 seconds after opening and always on white screen.
I caught this error in a logcat

Hello,
We're experiencing the same issue on physical android Tv's on app initialization. Trying on emulated android tv's doesn't generate this error. On TvOS emulators, the functionality works as intended.

Reverting to 4.0.5 ends up not solving the issue as others have mentioned above in the thread, the whole build of the app not compiling.

Any idea on how to fix this issue ?

Ludovic - Codekraft

@ruairioliverwv
Copy link

I ended up forking this repo, using v4.0.5 on android and v7 on iOS. v4.0.5 performance is dramatically better. I was testing on a Galaxy J7. On v7 I was getting times ranging from 400ms to 18 seconds for getGenericPassword. Consistently < 40ms on v4.0.5

@nicolaslazzos
Copy link

Thats because the v4.0.5 version does not have biometry to unlock the keychain. My fork is working ok with the latest version by making the changes i mentioned in the other comments.

@musti-91
Copy link

For me is getGenericPassword creates a different password for the second time user opens the app and only in IOS.

@oblador
Copy link
Owner

oblador commented Oct 2, 2021

Please try out 8.0.0 which has performance improvements

@HamidOsouli-zz
Copy link

HamidOsouli-zz commented Oct 7, 2021

@nicolaslazzos
@aranda-adapptor
I am trying on a Samsung Galaxy S7 your method as:

...

      // get the best storage
      // final String accessControl = getAccessControlOrDefault(options);
      // final boolean useBiometry = getUseBiometry(accessControl);
      // final CipherStorage current = getCipherStorageForCurrentAPILevel(useBiometry);
      final CipherStorage current = getSelectedStorage(options);
      final String rules = getSecurityRulesOrDefault(options);

...

And disabled the warmup as the docs suggest here Link, but the code changed and I call .withoutWarmUp() in this file https://github.com/oblador/react-native-keychain/blob/master/android/src/main/java/com/oblador/keychain/KeychainPackage.java as:

  public KeychainPackage() {
    this(new KeychainModuleBuilder().withoutWarmUp());
  }

I delete the build folder of the node package and run: react-native run-android, but it still takes about 7 seconds while in iPhone, it is instantly.
Any advice on this? - Sorry if I specify too much, I am new to react native.
Thanks

I dont know if i understood correctly, but you dont have to call .withoutWarmUp() in this file: https://github.com/oblador/react-native-keychain/blob/master/android/src/main/java/com/oblador/keychain/KeychainPackage.java, you have to do it in your MainApplication.java. So for that you have to import:

import com.oblador.keychain.KeychainPackage;
import com.oblador.keychain.KeychainModuleBuilder;

And then, in the getPackages() method, yo have to add the package manually like that:

packages.add(new KeychainPackage(new KeychainModuleBuilder().withoutWarmUp()));

And because react-native-keychain has autolinking, i had to disable it in order to add the package manually. For that, i created a react-native.config.js file in the root of my project with the following code:

module.exports = {
  dependencies: {
    'react-native-keychain': {
      platforms: {
        android: null,
      },
    },
  },
};

I hope it works for you!

Moving to version 8.0.0 didn't really help with biometric performance, but this solution finally worked ✅🎉
Thank you

@MaTToX3
Copy link

MaTToX3 commented Apr 4, 2023

Still an issue in 8.1.1.

@tomerh2001
Copy link

Still an issue

@tomerh2001
Copy link

@nicolaslazzos
@aranda-adapptor
I am trying on a Samsung Galaxy S7 your method as:

...

      // get the best storage
      // final String accessControl = getAccessControlOrDefault(options);
      // final boolean useBiometry = getUseBiometry(accessControl);
      // final CipherStorage current = getCipherStorageForCurrentAPILevel(useBiometry);
      final CipherStorage current = getSelectedStorage(options);
      final String rules = getSecurityRulesOrDefault(options);

...

And disabled the warmup as the docs suggest here Link, but the code changed and I call .withoutWarmUp() in this file https://github.com/oblador/react-native-keychain/blob/master/android/src/main/java/com/oblador/keychain/KeychainPackage.java as:

  public KeychainPackage() {
    this(new KeychainModuleBuilder().withoutWarmUp());
  }

I delete the build folder of the node package and run: react-native run-android, but it still takes about 7 seconds while in iPhone, it is instantly.
Any advice on this? - Sorry if I specify too much, I am new to react native.
Thanks

I dont know if i understood correctly, but you dont have to call .withoutWarmUp() in this file: https://github.com/oblador/react-native-keychain/blob/master/android/src/main/java/com/oblador/keychain/KeychainPackage.java, you have to do it in your MainApplication.java. So for that you have to import:

import com.oblador.keychain.KeychainPackage;
import com.oblador.keychain.KeychainModuleBuilder;

And then, in the getPackages() method, yo have to add the package manually like that:

packages.add(new KeychainPackage(new KeychainModuleBuilder().withoutWarmUp()));

And because react-native-keychain has autolinking, i had to disable it in order to add the package manually. For that, i created a react-native.config.js file in the root of my project with the following code:

module.exports = {
  dependencies: {
    'react-native-keychain': {
      platforms: {
        android: null,
      },
    },
  },
};

I hope it works for you!

I'm getting unresolved reference 'keychain':
image

This is my keychain version:
image

And I also added this file as specified:
image

@farwayer
Copy link

@tomerh2001 check this https://github.com/oblador/react-native-keychain?tab=readme-ov-file#option-manually-1

@chizhkov422
Copy link

chizhkov422 commented Mar 18, 2024

Hi, same problem with samsung s10e (android 12).
I tried to downgrade to 4.0.5 version and manual integration with packages.add(new KeychainPackage(new KeychainModuleBuilder().withoutWarmUp()));
But it didn't help me.

@vafada
Copy link

vafada commented Apr 4, 2024

does this PR fixes this issue?

#577

@owav
Copy link

owav commented Apr 18, 2024

does this PR fixes this issue?

#577

I still have the issue with v8.2.0 (which has this PR).
getGenericPassword takes ~10s on my pixel 6a.

With v4.0.5, it takes less than 100 milliseconds.

@humaidk2
Copy link

humaidk2 commented Jun 6, 2024

Still having this issue on 8.2.0, but the fix with removing warm up fixes it.

Would be nice if someone added a small gradle variable and enabled warm up only need it.

Then all the manual install steps for android wouldn't be necessary.

@c-goettert
Copy link

does this PR fixes this issue?

#577

In fact, for us this PR seems to introduce performance issues on android. We are facing the same performance drop as described in #630. If I revert this change locally, the app starts faster again.

@DorianMazur
Copy link
Collaborator

Hi @c-goettert @humaidk2 @owav @vafada

I’ve recently migrated the project to use Android DataStore Preferences in the latest release 1.2.0. Could you please test and confirm if the bug is still present?

@owav
Copy link

owav commented Sep 16, 2024

Hey @DorianMazur

I tried your version as a drop-in replacement but I get the following error when trying to getGenericPassword (setGenericPassword seems to work fine) :
[Error: There are multiple DataStores active for the same file: /data/user/0/com.myapp/files/datastore/RN_KEYCHAIN.preferences_pb. You should either maintain your DataStore as a singleton or confirm that there is no two DataStore's active on the same file (by confirming that the scope is cancelled).]

Edit : it seems this error has something to do with reloading the app without killing it. Anyway, with a clean cold start getGenericPassword takes approx 12sec, same as with v8.2.0 (as for v4.0.5, less than 100ms).

Edit 2 : if it is of any help, here is my quick test code :

test code
import React, { useEffect } from 'react';
import {
  SafeAreaView,
  Text,
  View,
} from 'react-native';

//import * as Keychain from 'react-native-keychain';
import * as Keychain from 'react-native-keychain-manager';

function App(): React.JSX.Element {

  useEffect(() => {

    const testKeychain = async () => {
      try{
        console.debug("testKeychain")
        const perf1 = Date.now();
        const credentials = await Keychain.getGenericPassword();

        if(credentials){
          console.debug("credentials = ", credentials);
          const perf2 = Date.now();
          const perfDuration = (perf2-perf1)/1000;
          // r-n-keychain performance issue
          // https://github.com/oblador/react-native-keychain/issues/337
          // versions [5.0.0, 8.2.0] is very slow (~10s on my pixel 6a)
          // stick with version 4.0.5 for the time being (<100ms)
          console.debug("Loaded keychain data in %d seconds", perfDuration);
        }
        else{
          console.debug("setGenericPassword")
          await Keychain.setGenericPassword("myUsername", "myPassword");
        }

      }
      catch(err){
        console.warn(err);
        await Keychain.resetGenericPassword();
      }
    }

    testKeychain();
    
    
  }, []);

  return (
    <SafeAreaView>
        <View >
            <Text>Hi !</Text>
        </View>
    </SafeAreaView>
  );
}

export default App;

@DorianMazur
Copy link
Collaborator

I tested react-native-keychain 9.0.0 on Android 10 (API 29):

Loaded keychain data in 0.017 seconds

Since the issue no longer occurs, I’m closing this. Feel free to reopen if needed.

@DorianMazur
Copy link
Collaborator

After looking into it more, this happens because of StrongBox on some devices. It's not a bug, but I'll add an option to turn off StrongBox for those who don't need the extra security.

@ovitrif
Copy link
Contributor

ovitrif commented Nov 5, 2024

[Error: There are multiple DataStores active for the same file: /data/user/0/com.myapp/files/datastore/RN_KEYCHAIN.preferences_pb. You should either maintain your DataStore as a singleton or confirm that there is no two DataStore's active on the same file (by confirming that the scope is cancelled).]
Edit : it seems this error has something to do with reloading the app without killing it. Anyway, with a clean cold start getGenericPassword takes approx 12sec, same as with v8.2.0 (as for v4.0.5, less than 100ms).

Indeed hot reloading was broken because of this issue, needed a CoroutineScope to cancel it on each stop.

Should be fixed on the latest updates in PR #629

jivanovic pushed a commit to SowaLabs/react-native-keychain that referenced this issue Dec 24, 2024
Speeds up strongbox detection on certain devices:
 oblador#337 (comment)
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

No branches or pull requests