From 330beb40a501f2006abfbc9241fd81dc306d676b Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Thu, 24 Oct 2024 13:31:54 +0200 Subject: [PATCH] Update lib - User can choose if calc MD5 from encrypted or decrypted file (#10510) * User can choose if calc MD5 from decrypted file At the present moment, if user want use OTA, the function calculate MD5 of the decrypted file, but if file is encrypted from source, could be more useful to know the MD5 of the encrypted file. * md5 * Update Updater.cpp * Update libraries/Update/src/Update.h Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> * Update libraries/Update/src/Update.h Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> * Update libraries/Update/src/Updater.cpp Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> * Update libraries/Update/src/Updater.cpp Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- libraries/Update/src/Update.h | 4 +++- libraries/Update/src/Updater.cpp | 12 ++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index 7ae5e9807..5832846fd 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -137,8 +137,9 @@ public: /* sets the expected MD5 for the firmware (hexString) + If calc_post_decryption is true, the update library will calculate the MD5 after the decryption, if false the calculation occurs before the decryption */ - bool setMD5(const char *expected_md5); + bool setMD5(const char *expected_md5, bool calc_post_decryption = true); /* returns the MD5 String of the successfully ended firmware @@ -257,6 +258,7 @@ private: const esp_partition_t *_partition; String _target_md5; + bool _target_md5_decrypted = true; MD5Builder _md5; int _ledPin; diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index 78f93602c..e92f84d45 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -348,6 +348,11 @@ bool UpdateClass::_writeBuffer() { log_d("Decrypting OTA Image"); } } + + if (!_target_md5_decrypted) { + _md5.add(_buffer, _bufferLen); + } + //check if data in buffer needs decrypting if (_cryptMode & U_AES_IMAGE_DECRYPTING_BIT) { if (!_decryptBuffer()) { @@ -404,7 +409,9 @@ bool UpdateClass::_writeBuffer() { if (!_progress && _command == U_FLASH) { _buffer[0] = ESP_IMAGE_HEADER_MAGIC; } - _md5.add(_buffer, _bufferLen); + if (_target_md5_decrypted) { + _md5.add(_buffer, _bufferLen); + } _progress += _bufferLen; _bufferLen = 0; if (_progress_callback) { @@ -446,12 +453,13 @@ bool UpdateClass::_verifyEnd() { return false; } -bool UpdateClass::setMD5(const char *expected_md5) { +bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption) { if (strlen(expected_md5) != 32) { return false; } _target_md5 = expected_md5; _target_md5.toLowerCase(); + _target_md5_decrypted = calc_post_decryption; return true; }