From 56cf56465857e79e814b1e2d50f13e176170fcb3 Mon Sep 17 00:00:00 2001 From: bigbiff bigbiff Date: Fri, 19 Aug 2016 17:43:45 -0400 Subject: 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 --- twinstall.cpp | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'twinstall.cpp') diff --git a/twinstall.cpp b/twinstall.cpp index 75d9a489e..752957638 100644 --- a/twinstall.cpp +++ b/twinstall.cpp @@ -40,7 +40,9 @@ #include "variables.h" #include "data.hpp" #include "partitions.hpp" -#include "twrpDigest.hpp" +#include "twrpDigestDriver.hpp" +#include "twrpDigest/twrpDigest.hpp" +#include "twrpDigest/twrpMD5.hpp" #include "twrp-functions.hpp" #include "gui/gui.hpp" #include "gui/pages.hpp" @@ -317,22 +319,36 @@ extern "C" int TWinstall_zip(const char* path, int* wipe_cache) { gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path)); if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) { - gui_msg("check_for_md5=Checking for MD5 file..."); - twrpDigest md5sum; - md5sum.setfn(path); - switch (md5sum.verify_md5digest()) { - case MD5_OK: - gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(path)); - break; - case MD5_NOT_FOUND: - gui_msg("no_md5=Skipping MD5 check: no MD5 file found"); - break; - case MD5_FILE_UNREADABLE: - LOGERR("Skipping MD5 check: MD5 file unreadable\n"); - break; - case MD5_MATCH_FAIL: // md5 did not match - LOGERR("Aborting zip install: MD5 verification failed\n"); - return INSTALL_CORRUPT; + string digest_str; + string Full_Filename = path; + string digest_file = path; + digest_file += ".md5"; + + gui_msg("check_for_digest=Checking for Digest file..."); + if (!TWFunc::Path_Exists(digest_file)) { + gui_msg("no_digest=Skipping Digest check: no Digest file found"); + } + else { + if (TWFunc::read_file(digest_file, digest_str) != 0) { + LOGERR("Skipping MD5 check: MD5 file unreadable\n"); + } + else { + twrpDigest *digest = new twrpMD5(); + if (!twrpDigestDriver::stream_file_to_digest(Full_Filename, digest)) { + delete digest; + return INSTALL_CORRUPT; + } + string digest_check = digest->return_digest_string(); + if (digest_str == digest_check) { + gui_msg(Msg("digest_matched=Digest matched for '{1}'.")(path)); + } + else { + LOGERR("Aborting zip install: Digest verification failed\n"); + delete digest; + return INSTALL_CORRUPT; + } + delete digest; + } } } -- cgit v1.2.3