forked from TeamWin/android_bootable_recovery
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ozip decryption for Oppo/Realme device
This function will automatically decrypt .ozip file in a .zip and it will save the .zip in the same folder of the .ozip and with the same name. After the decryption, the flash will start. To enable this function you have to set a Boardconfig flag with the decryption key like i did here: mauronofrio/android_device_realme_RMX1901@a30625f Original projects: https://github.com/sabpprook/ozip2zip https://github.com/bkerler/oppo_ozip_decrypt Change-Id: Ic93243a565008c442bcf2992069b2801532ac7c1
- Loading branch information
1 parent
2702094
commit 0ff5984
Showing
11 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
LOCAL_PATH := $(call my-dir) | ||
|
||
include $(CLEAR_VARS) | ||
LOCAL_MODULE := ozip_decrypt | ||
LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin | ||
LOCAL_MODULE_TAGS := optional | ||
|
||
LOCAL_SRC_FILES := ozip_decrypt.cpp | ||
LOCAL_C_INCLUDES := external/boringssl/src/include | ||
LOCAL_SHARED_LIBRARIES := libcrypto | ||
include $(BUILD_EXECUTABLE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
Copyright 2020 Mauronofrio | ||
This file is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This file is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
GNU General Public License <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <string.h> | ||
#include <openssl/evp.h> | ||
#include <openssl/ssl.h> | ||
#define _FILE_OFFSET_BITS 64 | ||
//extern "C" __int64 __cdecl _ftelli64(FILE*); | ||
|
||
using namespace std; | ||
typedef std::basic_string<unsigned char> u_string; | ||
|
||
int decrypt(unsigned char* ciphertext, int ciphertext_len, unsigned char* key, | ||
unsigned char* iv, unsigned char* plaintext) | ||
{ | ||
EVP_CIPHER_CTX* ctx; | ||
int len; | ||
int plaintext_len; | ||
ctx = EVP_CIPHER_CTX_new(); | ||
EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv); | ||
EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len); | ||
plaintext_len = len; | ||
EVP_DecryptFinal_ex(ctx, plaintext + len, &len); | ||
plaintext_len += len; | ||
EVP_CIPHER_CTX_free(ctx); | ||
return plaintext_len; | ||
} | ||
|
||
std::string hexToASCII(string hex) | ||
{ | ||
int len = hex.length(); | ||
std::string newString; | ||
for (int i = 0; i < len; i += 2) | ||
{ | ||
string byte = hex.substr(i, 2); | ||
char chr = (char)(int)strtol(byte.c_str(), nullptr, 16); | ||
newString.push_back(chr); | ||
} | ||
return newString; | ||
} | ||
|
||
bool testkey(const char* keyf, const char* path) { | ||
u_string key = (unsigned char*)(hexToASCII(keyf)).c_str(); | ||
int data[17]; | ||
FILE* fps = fopen(path, "rb"); | ||
fseek(fps, 4176, SEEK_SET); | ||
fread(data, sizeof(char), 16, fps); | ||
fclose(fps); | ||
u_string udata = (unsigned char*)data; | ||
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); | ||
EVP_CIPHER_CTX_init(ctx); | ||
EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key.c_str(), NULL); | ||
EVP_CIPHER_CTX_set_padding(ctx, false); | ||
unsigned char buffer[1024], * pointer = buffer; | ||
int outlen; | ||
EVP_DecryptUpdate(ctx, pointer, &outlen, udata.c_str(), udata.length()); | ||
pointer += outlen; | ||
EVP_DecryptFinal_ex(ctx, pointer, &outlen); | ||
pointer += outlen; | ||
EVP_CIPHER_CTX_free(ctx); | ||
u_string test= u_string(buffer, pointer - buffer); | ||
u_string checktest = test.substr(0, 4); | ||
if (checktest == ((unsigned char*) "\x50\x4B\x03\x04") || checktest == ((unsigned char*) "\x41\x4E\x44\x52")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
|
||
if (argc != 3) | ||
{ | ||
printf("Usage: ozipdecrypt key [*.ozip]\n"); | ||
return 0; | ||
} | ||
const char* key = argv[1]; | ||
const char* path = argv[2]; | ||
FILE* fp = fopen(path, "rb"); | ||
char magic[13]; | ||
fgets(magic, sizeof(magic), fp); | ||
string temp(path); | ||
temp = (temp.substr(0, temp.size() - 5)).append(".zip"); | ||
const char* destpath= temp.c_str(); | ||
if (strcmp(magic, "OPPOENCRYPT!") != 0) | ||
{ | ||
printf("This is not an .ozip file!\n"); | ||
fclose(fp); | ||
int rencheck = rename(path, destpath); | ||
if (rencheck == 0) { | ||
printf("Renamed .ozip file in .zip file\n"); | ||
} | ||
else | ||
{ | ||
printf("Unable to rename .ozip file in .zip file\n"); | ||
} | ||
return 0; | ||
} | ||
if (testkey(key, path) == false) | ||
{ | ||
printf("Key is not good!\n"); | ||
fclose(fp); | ||
return 0; | ||
} | ||
else { | ||
printf("Key is good!\n"); | ||
} | ||
FILE* fp2 = fopen(destpath, "wb"); | ||
fseek(fp, 0L, SEEK_END); | ||
unsigned long int sizetot = ftello(fp); | ||
fseek(fp, 4176, SEEK_SET); | ||
int bdata[16384]; | ||
unsigned long int sizeseek; | ||
printf("Decrypting...\n"); | ||
while (true) | ||
{ | ||
unsigned char data[17]; | ||
fread(data, sizeof(char), 16, fp); | ||
decrypt(data, sizeof(data), (unsigned char*)(hexToASCII(key)).c_str(), NULL, data); | ||
fwrite(data, sizeof(char), 16, fp2); | ||
sizeseek = ftello(fp); | ||
if ((sizetot - sizeseek) <= 16384) { | ||
fread(bdata, sizeof(char), (sizetot - sizeseek), fp); | ||
fwrite(bdata, sizeof(char), (sizetot - sizeseek), fp2); | ||
break; | ||
} | ||
else | ||
{ | ||
fread(bdata, sizeof(char), 16384, fp); | ||
fwrite(bdata, sizeof(char), 16384, fp2); | ||
} | ||
} | ||
printf("File succesfully decrypted, saved in %s\n", destpath); | ||
fclose(fp2); | ||
fclose(fp); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters