Skip to content

fix(ota): Fix OTA decryption when HW acceleration is not available#12320

Open
lucasssvaz wants to merge 1 commit intoespressif:masterfrom
lucasssvaz:fix/ota_mbedtls
Open

fix(ota): Fix OTA decryption when HW acceleration is not available#12320
lucasssvaz wants to merge 1 commit intoespressif:masterfrom
lucasssvaz:fix/ota_mbedtls

Conversation

@lucasssvaz
Copy link
Member

@lucasssvaz lucasssvaz commented Feb 4, 2026

Description of Change

This PR aims to fix decryption of images when using non-Hardware-Accelerated functions form mbedtls by removing a useless mbedtls_aes_setkey_dec call.

This pull request also improves the documentation and clarity of the ESP32 flash encryption and decryption logic in Updater.cpp, making the cryptographic implementation easier to understand and maintain. The changes focus on adding detailed comments explaining the key tweaking and decryption algorithms, clarifying the reasoning behind certain cryptographic choices, and cleaning up the code for readability.

Enhanced documentation and clarity for ESP32 flash encryption

  • Added a comprehensive comment block to the _cryptKeyTweak function, explaining the concept of address-based key tweaking, its security benefits, configuration options via _cryptCfg, and how it matches ESP32's hardware flash encryption scheme.
  • Improved inline comments throughout _cryptKeyTweak to clarify how address bits are mixed into the key, the role of the pattern array, and the logic for partial tweaking based on _cryptCfg.

Improved documentation and explanation for decryption logic

  • Added a detailed comment block to _decryptBuffer, describing the ESP32 flash encryption scheme, the involutory transform (using AES encryption for both encrypt and decrypt), the rationale for byte reversal, and how key tweaking is applied per flash region.
  • Enhanced step-by-step comments within _decryptBuffer to clarify the process: block byte reversal, key tweaking, AES encryption (for decryption), and reversing the result back to plaintext.

Code readability and maintainability improvements

  • Removed redundant calls to mbedtls_aes_setkey_dec and clarified that only mbedtls_aes_setkey_enc is needed due to the involutory nature of the encryption scheme.
  • Cleaned up and reorganized code blocks for better logical flow, making the cryptographic process easier to follow.

Test Scenarios

Tested locally with ESP32 and ESP32-C2

Related links

Closes #12217

@lucasssvaz lucasssvaz self-assigned this Feb 4, 2026
@lucasssvaz lucasssvaz requested a review from me-no-dev as a code owner February 4, 2026 20:59
@lucasssvaz lucasssvaz added the Area: Libraries Issue is related to Library support. label Feb 4, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Messages
📖 🎉 Good Job! All checks are passing!

👋 Hello lucasssvaz, we appreciate your contribution to this project!


📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more.

🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project.

Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by 🚫 dangerJS against ebadd1a

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Test Results

 90 files   90 suites   27m 37s ⏱️
 67 tests  67 ✅ 0 💤 0 ❌
683 runs  683 ✅ 0 💤 0 ❌

Results for commit ebadd1a.

♻️ This comment has been updated with latest results.

@lucasssvaz lucasssvaz added the Status: Review needed Issue or PR is awaiting review label Feb 4, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Memory usage test (comparing PR against master branch)

The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.

MemoryFLASH [bytes]FLASH [%]RAM [bytes]RAM [%]
TargetDECINCDECINCDECINCDECINC
ESP32💚 -2000.000.00000.000.00
ESP32C3💚 -1400.000.00000.000.00
ESP32C5💚 -1400.000.00000.000.00
ESP32C6💚 -1400.000.00000.000.00
ESP32H2💚 -1400.000.00000.000.00
ESP32P4💚 -1600.000.00000.000.00
ESP32S2💚 -2000.000.00000.000.00
ESP32S3💚 -4400.000.00000.000.00
Click to expand the detailed deltas report [usage change in BYTES]
TargetESP32ESP32C3ESP32C5ESP32C6ESP32H2ESP32P4ESP32S2ESP32S3
ExampleFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAM
libraries/ArduinoOTA/examples/BasicOTA💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -200
libraries/ArduinoOTA/examples/SignedOTA💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/HTTPUpdate/examples/httpUpdate💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/HTTPUpdate/examples/httpUpdateSPIFFS💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/HTTPUpdate/examples/httpUpdateSecure💚 -160💚 -140💚 -140💚 -140--💚 -14000💚 -160
libraries/HTTPUpdateServer/examples/WebUpdater💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/Update/examples/AWS_S3_OTA_Update💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/Update/examples/HTTP_Client_AES_OTA_Update💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/Update/examples/HTTP_Server_AES_OTA_Update💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/Update/examples/OTAWebUpdater💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -120
libraries/Update/examples/SD_Update💚 -160💚 -140💚 -140💚 -140💚 -140💚 -160💚 -160💚 -160
libraries/Update/examples/Signed_OTA_Update💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/WebServer/examples/HttpAdvancedAuth💚 -200💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/WebServer/examples/HttpAuthCallback💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -200
libraries/WebServer/examples/HttpAuthCallbackInline💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -200💚 -440
libraries/WebServer/examples/HttpBasicAuth💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/WebServer/examples/HttpBasicAuthSHA1💚 -160💚 -140💚 -140💚 -140--💚 -14000💚 -120
libraries/WebServer/examples/HttpBasicAuthSHA1orBearerToken💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -160
libraries/WebServer/examples/WebUpdate💚 -160💚 -140💚 -140💚 -140--💚 -140💚 -160💚 -240

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Libraries Issue is related to Library support. Status: Review needed Issue or PR is awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ESP32-C2 OTA Update with AES not working

1 participant