iamMehedi / Secured-Preference-Store

A cryptography library and a SharedPreferences wrapper for Android that encrypts the content with 256 bit AES encryption. The Encryption key is securely stored in device's KeyStore.
562 stars 97 forks source link

anr when generateAESKey and call mStore.containsAlias(AES_KEY_ALIAS) #53

Open risechen opened 4 years ago

risechen commented 4 years ago

1.device and version: Samsung Galaxy J7 Prime (on7xelte), Android 8.1 2.anr log "main" prio=5 tid=1 Native | group="main" sCount=1 dsCount=0 flags=1 obj=0x732abec0 self=0xee07b000 | sysTid=19949 nice=0 cgrp=default sched=0/0 handle=0xf23304b8 | state=S schedstat=( 0 0 0 ) utm=9 stm=8 core=2 HZ=100 | stack=0xff334000-0xff336000 stackSize=8MB | held mutexes=

00 pc 0000000000049ff8 /system/lib/libc.so (__ioctl+8)

01 pc 000000000001e215 /system/lib/libc.so (ioctl+40)

02 pc 0000000000042839 /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+204)

03 pc 000000000004324f /system/lib/libbinder.so (android::IPCThreadState::waitForResponse(android::Parcel, int)+246)

04 pc 000000000003d45d /system/lib/libbinder.so (android::BpBinder::transact(unsigned int, android::Parcel const&, android::Parcel*, unsigned int)+36)

05 pc 00000000000c608d /system/lib/libandroid_runtime.so (???)

06 pc 0000000000791c65 /system/framework/arm/boot-framework.oat (Java_android_os_BinderProxy_transactNative__ILandroid_os_Parcel_2Landroid_os_Parcel_2I+132)

at android.os.BinderProxy.transactNative (Native method) at android.os.BinderProxy.transact (Binder.java:784) at android.security.IKeystoreService$Stub$Proxy.exist (IKeystoreService.java:846) at android.security.KeyStore.contains (KeyStore.java:527) at android.security.keystore.AndroidKeyStoreSpi.engineContainsAlias (AndroidKeyStoreSpi.java:951) at java.security.KeyStore.containsAlias (KeyStore.java:1293) at ai.totok.chat.fqz.e (EncryptionManager.java:730) at ai.totok.chat.fqz.a (EncryptionManager.java:717) at ai.totok.chat.fqz.a (EncryptionManager.java:228) at ai.totok.chat.fqz. (EncryptionManager.java:200) at ai.totok.chat.frd. (SecuredPreferenceStoreExt.java:85) at ai.totok.chat.frd.a (SecuredPreferenceStoreExt.java:161) at ai.totok.chat.etm.a (SecureStoreUtil.java:86) at ai.totok.chat.etm.b (SecureStoreUtil.java:146) at ai.totok.chat.etm.b (SecureStoreUtil.java:292) at ai.totok.chat.ehi.a (ZayhuPref.java:29) at com.zayhu.app.ZayhuApplication.b (ZayhuApplication.java:403) at com.zayhu.app.ZayhuApplication.onCreate (ZayhuApplication.java:190)

3.this is the code: @TargetApi(Build.VERSION_CODES.M) boolean generateAESKey(@Nullable byte[] seed) throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { if (!mStore.containsAlias(AES_KEY_ALIAS)) { KeyGenerator keyGen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, KEYSTORE_PROVIDER);

        KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(AES_KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                .setCertificateSubject(new X500Principal("CN = Secured Preference Store, O = Devliving Online"))
                .setCertificateSerialNumber(BigInteger.ONE)
                .setKeySize(AES_BIT_LENGTH)
                .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                .setRandomizedEncryptionRequired(false) //TODO: set to true and let the Cipher generate a secured IV
                .build();
        if(seed != null && seed.length > 0){
            SecureRandom random = new SecureRandom(seed);
            keyGen.init(spec, random);
        } else {
            keyGen.init(spec);
        }

        keyGen.generateKey();

        return true;
    }

    return false;
}