From 7bfded6aae175ebe28f301a5737e73ecde672198 Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Wed, 23 Oct 2024 13:58:08 +0200 Subject: [PATCH 1/8] 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. --- libraries/Update/src/Update.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index 7ae5e980751..80274cd98d6 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -137,8 +137,9 @@ class UpdateClass { /* sets the expected MD5 for the firmware (hexString) + calc_post_decryption if true che update library calc MD5 after decryption, if false the calc is before decryption */ - bool setMD5(const char *expected_md5); + bool setMD5(const char *expected_md5, bool calc_post_decryption); /* returns the MD5 String of the successfully ended firmware @@ -256,8 +257,9 @@ class UpdateClass { uint32_t _command; const esp_partition_t *_partition; - String _target_md5; - MD5Builder _md5; + String _target_md5; + bool _target_md5_decrypted=true; + MD5Builder _md5; int _ledPin; uint8_t _ledOn; From 0166ae3b48769a3b0fb80535b79f6dbefd145170 Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:01:06 +0200 Subject: [PATCH 2/8] md5 --- libraries/Update/src/Updater.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index 78f93602cde..70b7abcdb99 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -5,8 +5,7 @@ */ #include "Update.h" -#include "Arduino.h" -#include "spi_flash_mmap.h" +#include "Arduino.h" #include "esp_ota_ops.h" #include "esp_image_format.h" #include "mbedtls/aes.h" @@ -124,7 +123,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con _reset(); _error = 0; _target_md5 = emptyString; - _md5 = MD5Builder(); + _md5 = MD5Builder(); if (size == 0) { _error = UPDATE_ERROR_SIZE; @@ -171,7 +170,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con } _size = size; _command = command; - _md5.begin(); + _md5.begin(); return true; } @@ -348,6 +347,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 +408,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 +452,14 @@ bool UpdateClass::_verifyEnd() { return false; } -bool UpdateClass::setMD5(const char *expected_md5) { +bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption=true) { if (strlen(expected_md5) != 32) { return false; } _target_md5 = expected_md5; _target_md5.toLowerCase(); + + _target_md5_decrypted=calc_post_decryption; return true; } @@ -473,7 +481,8 @@ bool UpdateClass::end(bool evenIfRemaining) { _size = progress(); } - _md5.calculate(); + _md5.calculate(); + if (_target_md5.length()) { if (_target_md5 != _md5.toString()) { _abort(UPDATE_ERROR_MD5); From 97e6c5df19a1cf63019c8a1aa8b82d42d09a5130 Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:14:20 +0200 Subject: [PATCH 3/8] Update Updater.cpp --- libraries/Update/src/Updater.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index 70b7abcdb99..df977d397b6 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -6,6 +6,7 @@ #include "Update.h" #include "Arduino.h" +#include "spi_flash_mmap.h" #include "esp_ota_ops.h" #include "esp_image_format.h" #include "mbedtls/aes.h" @@ -123,7 +124,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con _reset(); _error = 0; _target_md5 = emptyString; - _md5 = MD5Builder(); + _md5 = MD5Builder(); if (size == 0) { _error = UPDATE_ERROR_SIZE; @@ -170,7 +171,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con } _size = size; _command = command; - _md5.begin(); + _md5.begin(); return true; } @@ -458,7 +459,6 @@ bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption=tru } _target_md5 = expected_md5; _target_md5.toLowerCase(); - _target_md5_decrypted=calc_post_decryption; return true; } @@ -481,8 +481,7 @@ bool UpdateClass::end(bool evenIfRemaining) { _size = progress(); } - _md5.calculate(); - + _md5.calculate(); if (_target_md5.length()) { if (_target_md5 != _md5.toString()) { _abort(UPDATE_ERROR_MD5); From 1b8976f1003ab1f305958c9eeac66eccfde92ff6 Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Thu, 24 Oct 2024 08:55:27 +0200 Subject: [PATCH 4/8] Update libraries/Update/src/Update.h Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> --- libraries/Update/src/Update.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index 80274cd98d6..a09ec4da009 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -137,7 +137,7 @@ class UpdateClass { /* sets the expected MD5 for the firmware (hexString) - calc_post_decryption if true che update library calc MD5 after decryption, if false the calc is before decryption + 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 calc_post_decryption); From dbe17d22b206e7e320059c1edae42955ab4a1af8 Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Thu, 24 Oct 2024 08:55:33 +0200 Subject: [PATCH 5/8] Update libraries/Update/src/Update.h Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> --- libraries/Update/src/Update.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index a09ec4da009..d9b3323516c 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -139,7 +139,7 @@ class UpdateClass { 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 calc_post_decryption); + bool setMD5(const char *expected_md5, bool calc_post_decryption=true); /* returns the MD5 String of the successfully ended firmware From 8bb73b9707be1413eb0d258f263447ac8692d3b0 Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Thu, 24 Oct 2024 08:55:43 +0200 Subject: [PATCH 6/8] Update libraries/Update/src/Updater.cpp Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> --- libraries/Update/src/Updater.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index df977d397b6..6fdef5e9a0a 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -5,7 +5,7 @@ */ #include "Update.h" -#include "Arduino.h" +#include "Arduino.h" #include "spi_flash_mmap.h" #include "esp_ota_ops.h" #include "esp_image_format.h" From 11ce7528c99c7bff2c250ca4fe44903c21b8400d Mon Sep 17 00:00:00 2001 From: zinkett <96108358+zinkett@users.noreply.github.com> Date: Thu, 24 Oct 2024 08:55:52 +0200 Subject: [PATCH 7/8] Update libraries/Update/src/Updater.cpp Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> --- libraries/Update/src/Updater.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index 6fdef5e9a0a..dae5d40e9c5 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -453,7 +453,7 @@ bool UpdateClass::_verifyEnd() { return false; } -bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption=true) { +bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption) { if (strlen(expected_md5) != 32) { return false; } From 120edca6248b2bd24e67e7c247d8118676a8fb68 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:19:10 +0000 Subject: [PATCH 8/8] ci(pre-commit): Apply automatic fixes --- libraries/Update/src/Update.h | 8 ++++---- libraries/Update/src/Updater.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index d9b3323516c..5832846fd28 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -139,7 +139,7 @@ class UpdateClass { 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 calc_post_decryption=true); + bool setMD5(const char *expected_md5, bool calc_post_decryption = true); /* returns the MD5 String of the successfully ended firmware @@ -257,9 +257,9 @@ class UpdateClass { uint32_t _command; const esp_partition_t *_partition; - String _target_md5; - bool _target_md5_decrypted=true; - MD5Builder _md5; + String _target_md5; + bool _target_md5_decrypted = true; + MD5Builder _md5; int _ledPin; uint8_t _ledOn; diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index dae5d40e9c5..e92f84d4599 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -349,7 +349,7 @@ bool UpdateClass::_writeBuffer() { } } - if(!_target_md5_decrypted){ + if (!_target_md5_decrypted) { _md5.add(_buffer, _bufferLen); } @@ -409,9 +409,9 @@ bool UpdateClass::_writeBuffer() { if (!_progress && _command == U_FLASH) { _buffer[0] = ESP_IMAGE_HEADER_MAGIC; } - if(_target_md5_decrypted){ + if (_target_md5_decrypted) { _md5.add(_buffer, _bufferLen); - } + } _progress += _bufferLen; _bufferLen = 0; if (_progress_callback) { @@ -459,7 +459,7 @@ bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption) { } _target_md5 = expected_md5; _target_md5.toLowerCase(); - _target_md5_decrypted=calc_post_decryption; + _target_md5_decrypted = calc_post_decryption; return true; }