Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 56cf5646 authored by bigbiff bigbiff's avatar bigbiff bigbiff Committed by Ethan Yonker
Browse files

twrpDigest refactor

This patch is to refactor twrpDigest using polymorphism
and inheritance to use the same call patterns for creating and
reading a digest. Now a library.
Use SHA2 from libcrypto. SHA2 is default if device has libcrypto.

Change string MD5 everywhere to use digest or Digest instead. Updated
string tags to digest. Translation will be required.

Switch out digest code into a driver class from partitionmanager.

SHA2 is better for digest creation due to decreased collision space
compared to MD5 and SHA1.

See https://en.wikipedia.org/wiki/SHA-2

Change-Id: I74b5546789990b12aa4ce2e389d25f80a3fe213f
parent 92e2531d
Loading
Loading
Loading
Loading
+9 −10
Original line number Diff line number Diff line
@@ -51,18 +51,15 @@ LOCAL_SRC_FILES := \
    fixContexts.cpp \
    twrpTar.cpp \
    exclude.cpp \
    twrpDigest.cpp \
    digest/md5.c \
    find_file.cpp \
    infomanager.cpp

LOCAL_SRC_FILES += \
    infomanager.cpp \
    data.cpp \
    partition.cpp \
    partitionmanager.cpp \
    progresstracking.cpp \
    twinstall.cpp \
    twrp-functions.cpp \
    twrpDigestDriver.cpp \
    openrecoveryscript.cpp \
    tarWrite.c

@@ -112,9 +109,11 @@ LOCAL_C_INCLUDES += \
    system/core/libsparse \
    external/zlib

LOCAL_C_INCLUDES += bionic external/openssl/include
LOCAL_C_INCLUDES += bionic
ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
    LOCAL_C_INCLUDES += external/stlport/stlport
    LOCAL_C_INCLUDES += external/stlport/stlport external/openssl/include
else
    LOCAL_C_INCLUDES += external/boringssl/include
endif

LOCAL_STATIC_LIBRARIES :=
@@ -122,10 +121,11 @@ LOCAL_SHARED_LIBRARIES :=

LOCAL_STATIC_LIBRARIES += libguitwrp
LOCAL_SHARED_LIBRARIES += libaosprecovery libz libc libcutils libstdc++ libtar libblkid libminuitwrp libminadbd libmtdutils libminzip libtwadbbu libbootloader_message
LOCAL_SHARED_LIBRARIES += libcrecovery
LOCAL_SHARED_LIBRARIES += libcrecovery libtwadbbu libtwrpdigest

ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
    LOCAL_SHARED_LIBRARIES += libstlport
    LOCAL_CFLAGS += -DTW_NO_SHA2_LIBRARY
endif
ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 24; echo $$?),0)
    LOCAL_SHARED_LIBRARIES += libmincrypttwrp
@@ -159,10 +159,8 @@ ifeq ($(TARGET_USERIMAGES_USE_EXT4), true)
        #LOCAL_STATIC_LIBRARIES += liblz4
    endif
endif

LOCAL_C_INCLUDES += external/libselinux/include
LOCAL_SHARED_LIBRARIES += libselinux
LOCAL_CFLAGS += -g
ifneq ($(TARGET_USERIMAGES_USE_EXT4), true)
    LOCAL_CFLAGS += -DUSE_EXT4
    LOCAL_C_INCLUDES += system/extras/ext4_utils
@@ -682,6 +680,7 @@ include $(commands_recovery_local_path)/injecttwrp/Android.mk \
    $(commands_recovery_local_path)/simg2img/Android.mk \
    $(commands_recovery_local_path)/adbbu/Android.mk \
    $(commands_recovery_local_path)/libpixelflinger/Android.mk \
    $(commands_recovery_local_path)/twrpDigest/Android.mk \
    $(commands_recovery_local_path)/attr/Android.mk

ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 24; echo $$?),0)
+3 −4
Original line number Diff line number Diff line
@@ -2,10 +2,9 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
	twrpback.cpp \
	../twrpDigest.cpp \
	../digest/md5.c
LOCAL_SHARED_LIBRARIES += libstdc++ libz libselinux
	twrpback.cpp

LOCAL_SHARED_LIBRARIES += libstdc++ libz libtwrpdigest
ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
    LOCAL_C_INCLUDES += external/stlport/stlport
    LOCAL_SHARED_LIBRARIES += libstlport
+15 −23
Original line number Diff line number Diff line
@@ -34,9 +34,8 @@

#include "twadbstream.h"
#include "twrpback.hpp"
#include "../variables.h"
#include "../twcommon.h"
#include "../twrpDigest.hpp"
#include "../twrpDigest/twrpDigest.hpp"
#include "../twrpDigest/twrpMD5.hpp"

twrpback::twrpback(void) {
	read_fd = 0;
@@ -94,7 +93,7 @@ void twrpback::close_restore_fds() {
}

int twrpback::backup(std::string command) {
	twrpDigest adb_md5;
	twrpMD5 digest;
	bool breakloop = false;
	int bytes = 0, errctr = 0;
	char result[MAX_ADB_READ];
@@ -215,8 +214,7 @@ int twrpback::backup(std::string command) {
				struct twfilehdr twimghdr;

				adblogwrite("Writing TWIMG\n");
				adb_md5.initMD5();

				digest.init();
				memset(&twimghdr, 0, sizeof(twimghdr));
				memcpy(&twimghdr, cmd, sizeof(cmd));
				md5fnsize = twimghdr.size;
@@ -234,7 +232,7 @@ int twrpback::backup(std::string command) {
				struct twfilehdr twfilehdr;

				adblogwrite("Writing TWFN\n");
				adb_md5.initMD5();
				digest.init();

				ADBSTRUCT_STATIC_ASSERT(sizeof(twfilehdr) == MAX_ADB_READ);

@@ -269,8 +267,7 @@ int twrpback::backup(std::string command) {
					totalbytes += bytes;
					char *writeresult = new char [bytes];
					memcpy(writeresult, result, bytes);
					if (adb_md5.updateMD5stream((unsigned char *) writeresult, bytes) == -1)
						adblogwrite("failed to update md5 stream\n");
					digest.update((unsigned char *) writeresult, bytes);
					if (fwrite(writeresult, 1, bytes, adbd_fp) != bytes) {
						adblogwrite("Error writing backup data to adbd\n");
						close_backup_fds();
@@ -290,8 +287,7 @@ int twrpback::backup(std::string command) {
						close_backup_fds();
						return -1;
					}
					if (adb_md5.updateMD5stream((unsigned char *) padding, sizeof(padding)) == -1)
						adblogwrite("failed to update md5 stream\n");
					digest.update((unsigned char *) padding, sizeof(padding));
					fflush(adbd_fp);
					totalbytes = 0;
				}
@@ -299,9 +295,8 @@ int twrpback::backup(std::string command) {
				AdbBackupFileTrailer md5trailer;

				memset(&md5trailer, 0, sizeof(md5trailer));
				adb_md5.finalizeMD5stream();

				std::string md5string = adb_md5.createMD5string();
				std::string md5string = digest.return_digest_string();

				strncpy(md5trailer.start_of_trailer, TWRP, sizeof(md5trailer.start_of_trailer));
				strncpy(md5trailer.type, MD5TRAILER, sizeof(md5trailer.type));
@@ -350,8 +345,7 @@ int twrpback::backup(std::string command) {
				char *writeresult = new char [bytes];
				memcpy(writeresult, result, bytes);

				if (adb_md5.updateMD5stream((unsigned char *) writeresult, bytes) == -1)
					adblogwrite("failed to update md5 stream\n");
				digest.update((unsigned char *) writeresult, bytes);

				totalbytes += bytes;
				dataChunkBytes += bytes;
@@ -399,7 +393,8 @@ int twrpback::backup(std::string command) {
}

int twrpback::restore(void) {
	twrpDigest adb_md5;
	twrpMD5 digest;
	char cmd[MAX_ADB_READ];
	char result[MAX_ADB_READ];
	struct AdbBackupControlType structcmd;
	int adb_control_twrp_fd, errctr = 0;
@@ -591,8 +586,8 @@ int twrpback::restore(void) {
					struct twfilehdr twimghdr;
					uint32_t crc, twimghdrcrc;

					digest.init();
					totalbytes -= sizeof(result);
					adb_md5.initMD5();
					adblogwrite("Restoring TWIMG\n");
					memset(&twimghdr, 0, sizeof(twimghdr));
					memcpy(&twimghdr, result, sizeof(result));
@@ -623,9 +618,9 @@ int twrpback::restore(void) {
				else if (cmdtype == TWFN) {
					struct twfilehdr twfilehdr;
					uint32_t crc, twfilehdrcrc;
					digest.init();

					totalbytes -= sizeof(result);
					adb_md5.initMD5();
					adblogwrite("Restoring TWFN\n");
					memset(&twfilehdr, 0, sizeof(twfilehdr));
					memcpy(&twfilehdr, result, sizeof(result));
@@ -703,15 +698,13 @@ int twrpback::restore(void) {
									close_restore_fds();
									return -1;
								}
								adblogwrite("md5 finalize stream\n");
								adb_md5.finalizeMD5stream();

								AdbBackupFileTrailer md5;

								memset(&md5, 0, sizeof(md5));
								strncpy(md5.start_of_trailer, TWRP, sizeof(md5.start_of_trailer));
								strncpy(md5.type, TWMD5, sizeof(md5.type));
								std::string md5string = adb_md5.createMD5string();
								std::string md5string = digest.return_digest_string();
								strncpy(md5.md5, md5string.c_str(), sizeof(md5.md5));

								adblogwrite("Sending MD5Check\n");
@@ -726,8 +719,7 @@ int twrpback::restore(void) {
								break;
							}
						}
						if (adb_md5.updateMD5stream((unsigned char*)result, sizeof(result)) == -1)
							adblogwrite("failed to update md5 stream\n");
						digest.update((unsigned char*)result, sizeof(result));
						dataChunkBytes += readbytes;

						if (write(adb_write_fd, result, sizeof(result)) < 0) {
+11 −3
Original line number Diff line number Diff line
@@ -679,14 +679,14 @@ void DataManager::SetDefaultValues()

	mPersist.SetValue(TW_INSTALL_REBOOT_VAR, "0");
	mPersist.SetValue(TW_SIGNED_ZIP_VERIFY_VAR, "0");
	mPersist.SetValue(TW_FORCE_MD5_CHECK_VAR, "0");
	mPersist.SetValue(TW_DISABLE_FREE_SPACE_VAR, "0");
	mPersist.SetValue(TW_FORCE_DIGEST_CHECK_VAR, "0");
	mPersist.SetValue(TW_USE_COMPRESSION_VAR, "0");
	mPersist.SetValue(TW_TIME_ZONE_VAR, "CST6CDT,M3.2.0,M11.1.0");
	mPersist.SetValue(TW_GUI_SORT_ORDER, "1");
	mPersist.SetValue(TW_RM_RF_VAR, "0");
	mPersist.SetValue(TW_SKIP_MD5_CHECK_VAR, "0");
	mPersist.SetValue(TW_SKIP_MD5_GENERATE_VAR, "0");
	mPersist.SetValue(TW_SKIP_DIGEST_CHECK_VAR, "0");
	mPersist.SetValue(TW_SKIP_DIGEST_GENERATE_VAR, "0");
	mPersist.SetValue(TW_SDEXT_SIZE, "0");
	mPersist.SetValue(TW_SWAP_SIZE, "0");
	mPersist.SetValue(TW_SDPART_FILE_SYSTEM, "ext3");
@@ -708,6 +708,14 @@ void DataManager::SetDefaultValues()
	mData.SetValue("tw_background_thread_running", "0");
	mData.SetValue(TW_RESTORE_FILE_DATE, "0");
	mPersist.SetValue("tw_military_time", "0");

#ifdef TW_INCLUDE_CRYPTO
	mConst.SetValue(TW_USE_SHA2, "1");
	mConst.SetValue(TW_NO_SHA2, "0");
#else
	mConst.SetValue(TW_NO_SHA2, "1");
#endif

#ifdef TW_NO_SCREEN_TIMEOUT
	mConst.SetValue("tw_screen_timeout_secs", "0");
	mConst.SetValue("tw_no_screen_timeout", "1");
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ extern "C" {
#include "blanktimer.hpp"

// version 2 requires theme to handle power button as action togglebacklight
#define TW_THEME_VERSION 2
#define TW_THEME_VERSION 3

#define TW_THEME_VER_ERR -2

Loading