From a9cde704a480b32f85cb6293e5dc18bd7793f4cf Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Wed, 20 Nov 2024 11:44:13 +0100 Subject: [PATCH 1/5] guard crypt update --- libraries/Update/src/Update.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index 5832846fd28..ed791501c64 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -63,6 +63,7 @@ class UpdateClass { */ bool begin(size_t size = UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW, const char *label = NULL); +#ifdef UPDATE_CRYPT /* Setup decryption configuration Crypt Key is 32bytes(256bits) block of data, use the same key as used to encrypt image file @@ -71,6 +72,7 @@ class UpdateClass { Crypt Mode, used to select if image files should be decrypted or not */ bool setupCrypt(const uint8_t *cryptKey = 0, size_t cryptAddress = 0, uint8_t cryptConfig = 0xf, int cryptMode = U_AES_DECRYPT_AUTO); +#endif /* UPDATE_CRYPT */ /* Writes a buffer to the flash and increments the address @@ -99,6 +101,7 @@ class UpdateClass { */ bool end(bool evenIfRemaining = false); +#ifdef UPDATE_CRYPT /* sets AES256 key(32 bytes) used for decrypting image file */ @@ -122,6 +125,7 @@ class UpdateClass { void setCryptConfig(const uint8_t cryptConfig) { _cryptCfg = cryptConfig & 0x0f; } +#endif /* UPDATE_CRYPT */ /* Aborts the running update @@ -139,7 +143,11 @@ 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 +#ifdef UPDATE_CRYPT +, bool calc_post_decryption = true +#endif /* #ifdef UPDATE_CRYPT */ +); /* returns the MD5 String of the successfully ended firmware @@ -236,8 +244,10 @@ class UpdateClass { private: void _reset(); void _abort(uint8_t err); +#ifdef UPDATE_CRYPT void _cryptKeyTweak(size_t cryptAddress, uint8_t *tweaked_key); bool _decryptBuffer(); +#endif /* UPDATE_CRYPT */ bool _writeBuffer(); bool _verifyHeader(uint8_t data); bool _verifyEnd(); @@ -245,8 +255,10 @@ class UpdateClass { bool _chkDataInBlock(const uint8_t *data, size_t len) const; // check if block contains any data or is empty uint8_t _error; +#ifdef UPDATE_CRYPT uint8_t *_cryptKey; uint8_t *_cryptBuffer; +#endif /* UPDATE_CRYPT */ uint8_t *_buffer; uint8_t *_skipBuffer; size_t _bufferLen; @@ -258,15 +270,19 @@ class UpdateClass { const esp_partition_t *_partition; String _target_md5; +#ifdef UPDATE_CRYPT bool _target_md5_decrypted = true; +#endif /* UPDATE_CRYPT */ MD5Builder _md5; int _ledPin; uint8_t _ledOn; +#ifdef UPDATE_CRYPT uint8_t _cryptMode; size_t _cryptAddress; uint8_t _cryptCfg; +#endif /* UPDATE_CRYPT */ }; #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_UPDATE) From 442531a196054e7985009917b6b0ea5b20ddb315 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:13:55 +0100 Subject: [PATCH 2/5] guard update crypt --- libraries/Update/src/Updater.cpp | 39 +++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index e92f84d4599..13867904e59 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -9,7 +9,9 @@ #include "spi_flash_mmap.h" #include "esp_ota_ops.h" #include "esp_image_format.h" +#ifdef UPDATE_CRYPT #include "mbedtls/aes.h" +#endif /* UPDATE_CRYPT */ static const char *_err2str(uint8_t _error) { if (_error == UPDATE_ERROR_OK) { @@ -38,8 +40,10 @@ static const char *_err2str(uint8_t _error) { return ("Bad Argument"); } else if (_error == UPDATE_ERROR_ABORT) { return ("Aborted"); +#ifdef UPDATE_CRYPT } else if (_error == UPDATE_ERROR_DECRYPT) { return ("Decryption error"); +#endif /* UPDATE_CRYPT */ } return ("UNKNOWN"); } @@ -67,8 +71,15 @@ bool UpdateClass::_enablePartition(const esp_partition_t *partition) { } UpdateClass::UpdateClass() - : _error(0), _cryptKey(0), _cryptBuffer(0), _buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), - _command(U_FLASH), _partition(NULL), _cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf) {} + : _error(0), +#ifdef UPDATE_CRYPT +_cryptKey(0), _cryptBuffer(0), +#endif /* UPDATE_CRYPT */ +_buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL), +#ifdef UPDATE_CRYPT +_cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf) +#endif /* UPDATE_CRYPT */ +{} UpdateClass &UpdateClass::onProgress(THandlerFunction_Progress fn) { _progress_callback = fn; @@ -83,7 +94,9 @@ void UpdateClass::_reset() { delete[] _skipBuffer; } +#ifdef UPDATE_CRYPT _cryptBuffer = nullptr; +#endif /* UPDATE_CRYPT */ _buffer = nullptr; _skipBuffer = nullptr; _bufferLen = 0; @@ -175,6 +188,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con return true; } +#ifdef UPDATE_CRYPT bool UpdateClass::setupCrypt(const uint8_t *cryptKey, size_t cryptAddress, uint8_t cryptConfig, int cryptMode) { if (setCryptKey(cryptKey)) { if (setCryptMode(cryptMode)) { @@ -216,6 +230,7 @@ bool UpdateClass::setCryptMode(const int cryptMode) { } return true; } +#endif /* UPDATE_CRYPT */ void UpdateClass::_abort(uint8_t err) { _reset(); @@ -226,6 +241,7 @@ void UpdateClass::abort() { _abort(UPDATE_ERROR_ABORT); } +#ifdef UPDATE_CRYPT void UpdateClass::_cryptKeyTweak(size_t cryptAddress, uint8_t *tweaked_key) { memcpy(tweaked_key, _cryptKey, ENCRYPTED_KEY_SIZE); if (_cryptCfg == 0) { @@ -338,8 +354,10 @@ bool UpdateClass::_decryptBuffer() { } return true; } +#endif /* UPDATE_CRYPT */ bool UpdateClass::_writeBuffer() { +#ifdef UPDATE_CRYPT //first bytes of loading image, check to see if loading image needs decrypting if (!_progress) { _cryptMode &= U_AES_DECRYPT_MODE_MASK; @@ -360,6 +378,7 @@ bool UpdateClass::_writeBuffer() { return false; } } + #endif /* UPDATE_CRYPT */ //first bytes of new firmware uint8_t skip = 0; if (!_progress && _command == U_FLASH) { @@ -409,9 +428,13 @@ bool UpdateClass::_writeBuffer() { if (!_progress && _command == U_FLASH) { _buffer[0] = ESP_IMAGE_HEADER_MAGIC; } +#ifdef UPDATE_CRYPT if (_target_md5_decrypted) { +#endif /* UPDATE_CRYPT */ _md5.add(_buffer, _bufferLen); +#ifdef UPDATE_CRYPT } +#endif /* UPDATE_CRYPT */ _progress += _bufferLen; _bufferLen = 0; if (_progress_callback) { @@ -453,13 +476,19 @@ bool UpdateClass::_verifyEnd() { return false; } -bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption) { +bool UpdateClass::setMD5(const char *expected_md5 +#ifdef UPDATE_CRYPT +,bool calc_post_decryption +#endif /* UPDATE_CRYPT */ +) { if (strlen(expected_md5) != 32) { return false; } _target_md5 = expected_md5; _target_md5.toLowerCase(); +#ifdef UPDATE_CRYPT _target_md5_decrypted = calc_post_decryption; +#endif /* UPDATE_CRYPT */ return true; } @@ -532,12 +561,16 @@ size_t UpdateClass::writeStream(Stream &data) { return 0; } +#ifdef UPDATE_CRYPT if (_command == U_FLASH && !_cryptMode) { +#endif /* UPDATE_CRYPT */ if (!_verifyHeader(data.peek())) { _reset(); return 0; } +#ifdef UPDATE_CRYPT } +#endif /* UPDATE_CRYPT */ if (_ledPin != -1) { pinMode(_ledPin, OUTPUT); From f92abd18b1b01e5ab291611110ff19b378e6f6d6 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:19:57 +0100 Subject: [PATCH 3/5] Update Updater.cpp --- libraries/Update/src/Updater.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index 13867904e59..6a13741b9f4 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -75,9 +75,9 @@ UpdateClass::UpdateClass() #ifdef UPDATE_CRYPT _cryptKey(0), _cryptBuffer(0), #endif /* UPDATE_CRYPT */ -_buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL), +_buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL) #ifdef UPDATE_CRYPT -_cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf) +, _cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf) #endif /* UPDATE_CRYPT */ {} From 1f40aeea129d39da1331a8dd27584e0b42ae859a Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:30:59 +0100 Subject: [PATCH 4/5] revert logic to disable --- libraries/Update/src/Update.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index ed791501c64..d23b1d93037 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -63,7 +63,7 @@ class UpdateClass { */ bool begin(size_t size = UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW, const char *label = NULL); -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT /* Setup decryption configuration Crypt Key is 32bytes(256bits) block of data, use the same key as used to encrypt image file @@ -72,7 +72,7 @@ class UpdateClass { Crypt Mode, used to select if image files should be decrypted or not */ bool setupCrypt(const uint8_t *cryptKey = 0, size_t cryptAddress = 0, uint8_t cryptConfig = 0xf, int cryptMode = U_AES_DECRYPT_AUTO); -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ /* Writes a buffer to the flash and increments the address @@ -101,7 +101,7 @@ class UpdateClass { */ bool end(bool evenIfRemaining = false); -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT /* sets AES256 key(32 bytes) used for decrypting image file */ @@ -125,7 +125,7 @@ class UpdateClass { void setCryptConfig(const uint8_t cryptConfig) { _cryptCfg = cryptConfig & 0x0f; } -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ /* Aborts the running update @@ -144,9 +144,9 @@ class UpdateClass { 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 -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT , bool calc_post_decryption = true -#endif /* #ifdef UPDATE_CRYPT */ +#endif /* #ifdef UPDATE_NOCRYPT */ ); /* @@ -244,10 +244,10 @@ class UpdateClass { private: void _reset(); void _abort(uint8_t err); -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT void _cryptKeyTweak(size_t cryptAddress, uint8_t *tweaked_key); bool _decryptBuffer(); -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ bool _writeBuffer(); bool _verifyHeader(uint8_t data); bool _verifyEnd(); @@ -255,10 +255,10 @@ class UpdateClass { bool _chkDataInBlock(const uint8_t *data, size_t len) const; // check if block contains any data or is empty uint8_t _error; -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT uint8_t *_cryptKey; uint8_t *_cryptBuffer; -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ uint8_t *_buffer; uint8_t *_skipBuffer; size_t _bufferLen; @@ -270,19 +270,19 @@ class UpdateClass { const esp_partition_t *_partition; String _target_md5; -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT bool _target_md5_decrypted = true; -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ MD5Builder _md5; int _ledPin; uint8_t _ledOn; -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT uint8_t _cryptMode; size_t _cryptAddress; uint8_t _cryptCfg; -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ }; #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_UPDATE) From cdc7ca1328c0d8f377921b5a7e08662f1624cf8d Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:41:14 +0100 Subject: [PATCH 5/5] change disable logic --- libraries/Update/src/Updater.cpp | 56 ++++++++++++++++---------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index 6a13741b9f4..6f4d3c08210 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -9,9 +9,9 @@ #include "spi_flash_mmap.h" #include "esp_ota_ops.h" #include "esp_image_format.h" -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT #include "mbedtls/aes.h" -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ static const char *_err2str(uint8_t _error) { if (_error == UPDATE_ERROR_OK) { @@ -40,10 +40,10 @@ static const char *_err2str(uint8_t _error) { return ("Bad Argument"); } else if (_error == UPDATE_ERROR_ABORT) { return ("Aborted"); -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT } else if (_error == UPDATE_ERROR_DECRYPT) { return ("Decryption error"); -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ } return ("UNKNOWN"); } @@ -72,13 +72,13 @@ bool UpdateClass::_enablePartition(const esp_partition_t *partition) { UpdateClass::UpdateClass() : _error(0), -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT _cryptKey(0), _cryptBuffer(0), -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ _buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL) -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT , _cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf) -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ {} UpdateClass &UpdateClass::onProgress(THandlerFunction_Progress fn) { @@ -94,9 +94,9 @@ void UpdateClass::_reset() { delete[] _skipBuffer; } -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT _cryptBuffer = nullptr; -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ _buffer = nullptr; _skipBuffer = nullptr; _bufferLen = 0; @@ -188,7 +188,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con return true; } -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT bool UpdateClass::setupCrypt(const uint8_t *cryptKey, size_t cryptAddress, uint8_t cryptConfig, int cryptMode) { if (setCryptKey(cryptKey)) { if (setCryptMode(cryptMode)) { @@ -230,7 +230,7 @@ bool UpdateClass::setCryptMode(const int cryptMode) { } return true; } -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ void UpdateClass::_abort(uint8_t err) { _reset(); @@ -241,7 +241,7 @@ void UpdateClass::abort() { _abort(UPDATE_ERROR_ABORT); } -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT void UpdateClass::_cryptKeyTweak(size_t cryptAddress, uint8_t *tweaked_key) { memcpy(tweaked_key, _cryptKey, ENCRYPTED_KEY_SIZE); if (_cryptCfg == 0) { @@ -354,10 +354,10 @@ bool UpdateClass::_decryptBuffer() { } return true; } -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ bool UpdateClass::_writeBuffer() { -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT //first bytes of loading image, check to see if loading image needs decrypting if (!_progress) { _cryptMode &= U_AES_DECRYPT_MODE_MASK; @@ -378,7 +378,7 @@ bool UpdateClass::_writeBuffer() { return false; } } - #endif /* UPDATE_CRYPT */ + #endif /* UPDATE_NOCRYPT */ //first bytes of new firmware uint8_t skip = 0; if (!_progress && _command == U_FLASH) { @@ -428,13 +428,13 @@ bool UpdateClass::_writeBuffer() { if (!_progress && _command == U_FLASH) { _buffer[0] = ESP_IMAGE_HEADER_MAGIC; } -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT if (_target_md5_decrypted) { -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ _md5.add(_buffer, _bufferLen); -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT } -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ _progress += _bufferLen; _bufferLen = 0; if (_progress_callback) { @@ -477,18 +477,18 @@ bool UpdateClass::_verifyEnd() { } bool UpdateClass::setMD5(const char *expected_md5 -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT ,bool calc_post_decryption -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ ) { if (strlen(expected_md5) != 32) { return false; } _target_md5 = expected_md5; _target_md5.toLowerCase(); -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT _target_md5_decrypted = calc_post_decryption; -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ return true; } @@ -561,16 +561,16 @@ size_t UpdateClass::writeStream(Stream &data) { return 0; } -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT if (_command == U_FLASH && !_cryptMode) { -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ if (!_verifyHeader(data.peek())) { _reset(); return 0; } -#ifdef UPDATE_CRYPT +#ifndef UPDATE_NOCRYPT } -#endif /* UPDATE_CRYPT */ +#endif /* UPDATE_NOCRYPT */ if (_ledPin != -1) { pinMode(_ledPin, OUTPUT);