From fcdfeb2a45bf0045e5d23104259de68b1da7b16e Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Sun, 11 Oct 2020 14:10:12 +0200 Subject: [PATCH] nwztools: re-implement MD5 on Windows I forgot to fixup the windows up and missed it because of conditional compilation Change-Id: I526c765b9d56508815941ecb9b9dbac7ea407cf0 --- utils/nwztools/upgtools/mg.cpp | 25 +++++++++++++++++++++++++ utils/nwztools/upgtools/upgtool.c | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/utils/nwztools/upgtools/mg.cpp b/utils/nwztools/upgtools/mg.cpp index a4d06cd77f..79107e96bb 100644 --- a/utils/nwztools/upgtools/mg.cpp +++ b/utils/nwztools/upgtools/mg.cpp @@ -78,6 +78,31 @@ void MD5_CalculateDigest(void *digest, const void *input, size_t length) CryptDestroyHash(hHash); } +void *md5_start() +{ + if(!check_context()) + return NULL; + HCRYPTHASH hHash; + if(!CryptCreateHash(g_hCryptProv, CALG_MD5, 0, 0, &hHash)) + return NULL; + return reinterpret_cast(hHash); +} + +void md5_update(void *md5_obj, const void *input, size_t length) +{ + HCRYPTHASH hHash = reinterpret_cast(md5_obj); + CryptHashData(hHash, (const BYTE *)input, length, 0); +} + +void md5_final(void *md5_obj, void *digest) +{ + HCRYPTHASH hHash = reinterpret_cast(md5_obj); + DWORD dwSize = 16; + if(!CryptGetHashParam(hHash, HP_HASHVAL, (BYTE *)digest, &dwSize, 0)) + return; + CryptDestroyHash(hHash); +} + void mg_decrypt_fw(void *in, int size, void *out, uint8_t *key) { if(!check_context() || (size % 8) != 0) diff --git a/utils/nwztools/upgtools/upgtool.c b/utils/nwztools/upgtools/upgtool.c index 03d7d706d7..8235e487bf 100644 --- a/utils/nwztools/upgtools/upgtool.c +++ b/utils/nwztools/upgtools/upgtool.c @@ -225,7 +225,7 @@ static void compare_md5(struct upg_file_t *file, int idx, size_t filesize, uint8 cprintf_field(" Name: ", "%s ", g_md5name[idx]); cprintf(RED, found ? "Found" : " Not found"); printf("\n"); - cprintf_field(" Size: ", "%lu", filesize); + cprintf_field(" Size: ", "%lu", (unsigned long)filesize); cprintf(RED, " %s", !found ? "Cannot check" : filesize == expected_size ? "Ok" : "Mismatch"); printf("\n"); cprintf_field(" MD5:", " "); @@ -423,7 +423,7 @@ static int create_upg(int argc, char **argv) MD5_CalculateDigest(md5, buf, size); size_t inc_sz = 16 + NWZ_MD5_SIZE * 2 + strlen(g_md5name[i]); md5_prepend = realloc(md5_prepend, md5_prepend_sz + inc_sz); - md5_prepend_sz += sprintf(md5_prepend + md5_prepend_sz, "%lu ", size); + md5_prepend_sz += sprintf(md5_prepend + md5_prepend_sz, "%lu ", (unsigned long)size); for(int i = 0; i < NWZ_MD5_SIZE; i++) md5_prepend_sz += sprintf(md5_prepend + md5_prepend_sz, "%02x", md5[i]); md5_prepend_sz += sprintf(md5_prepend + md5_prepend_sz, " %s\n", g_md5name[i]);