-Update:
Looked into TWRP log, and it seems like it has key problems:
I:Setting up '/data' as data/media emulated storage.
I:mount -o bind '/data/media' '/sdcard' process ended with RC=0
I:mount -o bind '/data/media/0' '/sdcard' process ended with RC=0
I:File Based Encryption is present
e4crypt_initialize_global_de
Determining wrapped-key support for /data
fbe.data.wrappedkey = true
Wrapped key supported on /data
calling retrieveAndInstallKey
Key exists, using: /data/unencrypted/key
Using Keymaster HAL: 4 from QTI for encryption. Security level: TRUSTED_ENVIRONMENT, HAL: [email protected]::IKeymasterDevice/default
begin failed, code -62
Upgrading key in memory only: /data/unencrypted/key
upgrade_key failed, code -38
e4crypt_initialize_global_de returned fail
And this is after I wanted to decrypt it:
I:Command 'decrypt *********' received
I:Set page: 'singleaction_page'
I:operation_start: 'TWRP CLI Command'
Attempting to decrypt data partition or user data via command line.
E:Unexpected value for crypto key location
E:Error getting crypt footer and key
E:Could not get footer
Failed to decrypt data.
I:Done reading ORS command from command line
I:operation_end - status=0
Checked the location at /data/unencrypted/key
and the files are there for sure.
Currently I'm on the same ROM as the one that on I could access my internal storage, but the issue could be that I went FDE -> FBE -> FDE as it's mentioned in the comments.
Keymaster error codes are respectively:
KEY_REQUIRES_UPGRADE = -62
INVALID_ARGUMENT = -38
How to fix the keys is the answer I might looking for.
Long story short, I screwed up, and now I have an encrypted storage (with my files that I want back) but TWRP and the OS thinks it is not.
It goes like this:
- The Magisk v22.0 update screwed up my phone, which stuck in a bootloop.
- After countless tries with Magisk (updating, uninstalling. installing, removing mods, adding mods) in TWRP, I read somewhere to dirty flash the ROM.
- Due to an HDD failure in my PC, I did not have any storage to back up my internal storage from the phone.
- Because I wanted to update the ROM anyways (from MIUI 11 to 12) I decided to flash the newer one. That was the first mistake. This made the bootloop worse, and now it stuck in fastboot mode, no recovery. After many sleepless nights I found out that not every USB-C cable is equal, and I couldn't reach my phone via fastboot because of the cable itself.
- Due to an HDD failure in my PC, I did not have any storage to back up my internal storage from the phone.
- So now I was able to flash an original fastboot ROM which still gave me bootloop, but after wiping (not formatting, so the /sdcard fs doesn't gets erased) Data and Cache in TWRP.
- Then reflashed the fastboot ROM, and somehow it worked. When It booted up, I had to type in my screenlock pin again, and after setup all my files were available.
- Now that the phone worked again, I wanted to finish what I started, to update to latest EU (recovery) ROM. This was the second mistake. It did not work (because it required formatting data, which I did not wanted to, and just wiped it), and came bootloop again.
- So I went back to the fastboot ROM again, which fixed the phone again.
- But this time, it thinks the internal storage is not encrypted.
Now I can't access the internal storage. Even the camera app says that first I need an SD Card to take photos.
It's clearly still encrypted because when I browse it in TWRP, the structure and files are there, but the names are like "74t7Z1,dnvgIIexr1QAfhD".
The problem being neither the OS, nor TWRP knows the storage is encrypted, so it doesn't even tries to decrypt it, and there are no options to do it.
Tried to set encryption in the running OS, but it gave me the same results: even tho TWRP asks for decryption key, and says it successfully decrypted, the storage is still a mess like it's in encrypted state.
Is there any way to manually force some flag, so I can access my files again? I have the encryption key, I just need some way to decrypt the storage. I think of something like an ADB command to flag the partition as encrypted, so TWRP and OS would try to decrypt it.
Thanks in advance!
TWRP log showed this:
Determining wrapped-key support for /data fbe.data.wrappedkey = true Wrapped key supported on /data calling retrieveAndInstallKey Key exists, using: /data/unencrypted/key Using Keymaster HAL: 4 from QTI for encryption. Security level: TRUSTED_ENVIRONMENT, HAL: [email protected]::IKeymasterDevice/default begin failed, code -62 Upgrading key in memory only: /data/unencrypted/key upgrade_key failed, code -38 e4crypt_initialize_global_de returned fail
– Levente Zilahy Apr 15 '21 at 12:39How do I decrypt a double encryption? This is the question I cant find an answer to :/
– Levente Zilahy Apr 18 '21 at 11:13This is the current state after pulling, formatting data, then installing latest OS wich already has force encryption. Just push back data? Because the img is like 230GB :D
– Levente Zilahy Apr 18 '21 at 16:06I will look into the forums you linked, much appreciated. Thanks for you effort so far, I learned a lot.
– Levente Zilahy Apr 20 '21 at 01:33So now I'm pretty sure that its only about FBE.
– Levente Zilahy Apr 20 '21 at 01:36The master key is itself derived from user's passcode and SHA-512 of 16KB random file. Resetting the device wipes this file and seed which makes it impossible to reconstruct the same master key again even if user sets the same passcode again after factory reset."
Well, I guess this is the end. Even if I could retrieve deleted master key from a formatted /data, I already formatted and wiped it like 10 times, so no chance :(
– Levente Zilahy Apr 20 '21 at 01:41forceencrypt=
is for FDE only (and the value is pointing to crypto footer. FBE keys are wiped on factory reset because stock recovery is sending deleteKey instruction to TEE. Are you 100% sure TWRP recovery does the same? simply formatting userdata partition does not affect keystore – alecxs Apr 20 '21 at 06:1574t7Z1,dnvgIIexr1QAfhD
file last (key is in xattrs). then, and only then, TWRP is able to decrypt, because TWRP uses other partitions for decrypting (assuming you did not factory reset from stock ROM/recovery and keystore is not wiped yet) – alecxs Apr 20 '21 at 06:22