Skip to content

Commit 976f56e

Browse files
zainarbanirezaadi0105
authored andcommitted
vold: Decrypt: Simplify CE decrypt
- Create separated functions Signed-off-by: zainarbani <[email protected]> Change-Id: I6992a2b5f3277a59f93131a214d4d59cc6484319
1 parent d3b689e commit 976f56e

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

Decrypt.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,22 @@ bool Free_Return(bool retval, void* weaver_key, password_data_struct* pwd) {
621621
return retval;
622622
}
623623

624+
bool Decrypt_CE_storage(const userid_t user_id, int token, const std::string& secret) {
625+
printf("Attempting to unlock user storage\n");
626+
int flags = android::os::IVold::STORAGE_FLAG_CE;
627+
if (!fscrypt_unlock_user_key(user_id, token, secret)) {
628+
printf("fscrypt_unlock_user_key returned fail\n");
629+
return false;
630+
}
631+
printf("Attempting to prepare user storage\n");
632+
if (!fscrypt_prepare_user_storage("", user_id, 0, flags)) {
633+
printf("failed to fscrypt_prepare_user_storage\n");
634+
return false;
635+
}
636+
printf("User %i Decrypted Successfully!\n", user_id);
637+
return true;
638+
}
639+
624640
// /* Decrypt_User_Synth_Pass is the TWRP C++ equivalent to spBasedDoVerifyCredential
625641
// * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/LockSettingsService.java#1998 */
626642
bool Decrypt_User_Synth_Pass(const userid_t user_id, const std::string& Password) {
@@ -636,7 +652,6 @@ bool Decrypt_User_Synth_Pass(const userid_t user_id, const std::string& Password
636652
uint32_t auth_token_len = 0;
637653
std::string secret; // this will be the disk decryption key that is sent to vold
638654
int token = 0; // there is no token used for this kind of decrypt, key escrow is handled by weaver
639-
int flags = android::os::IVold::STORAGE_FLAG_CE;
640655
char spblob_path_char[PATH_MAX];
641656
sprintf(spblob_path_char, "/data/system_de/%d/spblob/", user_id);
642657
std::string spblob_path = spblob_path_char;
@@ -804,18 +819,10 @@ bool Decrypt_User_Synth_Pass(const userid_t user_id, const std::string& Password
804819
return Free_Return(retval, weaver_key, &pwd);
805820
}
806821

807-
printf("Attempting to unlock user storage\n");
808-
if (!fscrypt_unlock_user_key(user_id, token, secret)) {
809-
printf("fscrypt_unlock_user_key returned fail\n");
822+
if (!Decrypt_CE_storage(user_id, token, secret)) {
810823
return Free_Return(retval, weaver_key, &pwd);
811824
}
812825

813-
printf("Attempting to prepare user storage\n");
814-
if (!fscrypt_prepare_user_storage("", user_id, 0, flags)) {
815-
printf("failed to fscrypt_prepare_user_storage\n");
816-
return Free_Return(retval, weaver_key, &pwd);
817-
}
818-
printf("User %i Decrypted Successfully!\n", user_id);
819826
retval = true;
820827
return Free_Return(retval, weaver_key, &pwd);
821828
}
@@ -892,19 +899,10 @@ extern "C" bool Decrypt_User(const userid_t user_id, const std::string& Password
892899
return false;
893900
}
894901

895-
int flags = android::os::IVold::STORAGE_FLAG_CE;
896-
897902
if (Default_Password) {
898-
if (!fscrypt_unlock_user_key(user_id, 0, "!")) {
899-
printf("unlock_user_key returned fail\n");
903+
if (!Decrypt_CE_storage(user_id, 0, "!")) {
900904
return Decrypt_User_Synth_Pass(user_id, Password);
901905
}
902-
printf("Attempting to prepare user storage\n");
903-
if (!fscrypt_prepare_user_storage("", user_id, 0, flags)) {
904-
printf("failed to fscrypt_prepare_user_storage\n");
905-
return false;
906-
}
907-
printf("User %i Decrypted Successfully!\n", user_id);
908906
return true;
909907
}
910908
if (stat("/data/system_de/0/spblob", &st) == 0) {
@@ -962,17 +960,9 @@ extern "C" bool Decrypt_User(const userid_t user_id, const std::string& Password
962960
}
963961
// The secret is "Android FBE credential hash" plus appended 0x00 to reach 128 bytes then append the user's password then feed that to sha512sum
964962
std::string secret = HashPassword(Password);
965-
if (!fscrypt_unlock_user_key(user_id, 0, secret)) {
966-
printf("fscrypt_unlock_user_key returned fail\n");
963+
if (!Decrypt_CE_storage(user_id, 0, secret)) {
967964
return false;
968965
}
969-
970-
printf("Attempting to prepare user storage\n");
971-
if (!fscrypt_prepare_user_storage("", user_id, 0, flags)) {
972-
printf("failed to fscrypt_prepare_user_storage\n");
973-
return false;
974-
}
975-
printf("User %i Decrypted Successfully!\n", user_id);
976966
return true;
977967
}
978968
}

0 commit comments

Comments
 (0)