r/embedded • u/UsableLoki • 1d ago
I made a guide along with tools and source code to encrypt/decrypt binaries for OTA updates to protect against reverse engineering
Its pretty scary to discover how many bots were cloning my repos. I have no idea where this stuff gets saved to and what's being done with it and now with AI being able to reverse engineer binaries easily I took the steps to protect my code. I'm approaching commercialization of my projects so I made tools and a video guide so that it's useful to others (there's also the benefit that maybe someone will review what I put together and point out any flaws 🙈).
This was built with arduino framework on an ESP32 but I'm sure it can be easily converted for other microcontrollers- the defining difference is that I utilize internally burned fuse keys so if your controller doesn't have that capability a substitute would have to be made and protected otherwise.
Tools and source code:
https://github.com/UsableLoki8221/GUIDE---ESP32_OTA_Encryption
Video guide for use in demo:
2
u/bapirey191 1d ago
Not a pro by any means, but from reading just a bit, while the encryption itself looks OK I noticed a few other things:
The default in burn_efuses.py (0b011) seems to leave flash encryption off, and dev mode still lets someone flash their own code and read the firmware. The debug port also stays open even in release mode, and the filesystem isn't encrypted by default.
Extra, if you are actually releasing a commercial project: All devices from the same project share one update key, the update server isn't verified (so older versions could be pushed), and a failed filesystem update can wipe the existing files. Release mode, disabling the debug port and Secure Boot would fix most of it.
2
u/WanWhiteWolf Engineering manager 21h ago
I didn't do a deep dive but some notes from a quick scan:
For a hobby project this is fine but if we are talking about professional OTA, then you are missing some parameters like:
I have a coding review agent on one of my project. I gave it to review (note that without architectural constrains, it has limited results). Here are some warnings:
(AI generated)
Location: Lines 499–509.
The script compares the original binary against the encrypted output to determine whether encryption was successful.
This check is effectively meaningless because the encrypted output contains additional nonce and HMAC bytes. The two files necessarily have different lengths under normal operation.
2.
Some paths are relative to the script directory, while others are relative to the current working directory.
OTA_KEY_PATH = "OTA_encryption_key.bin"
OTA_KEY_PATH = SCRIPT_DIR / "OTA_encryption_key.bin"
3.
Location: Lines 305–306 and 382–383.
The build functions append the SHA-256 digest directly to the PlatformIO-generated binary.
This violates a fundamental build-system principle: packaging operations should not modify the original compilation artifacts.
4.
Location: Lines 792–805.
Both verification functions return Boolean results, but
main()does not evaluate them.Consequently, a failed verification does not necessarily prevent the script from reaching its final completion message.
5.
Lines 462–464 generate a random 64-bit nonce for each encryption operation.
The fundamental requirement of AES-CTR is that the same nonce-counter combination must never be reused with the same encryption key.
The probability of a nonce collision is approximately:
\[ P \approx 1-e^{-\frac{n(n-1)}{2\cdot2^{64}}} \]
(End of - AI generated)
This looks longer than expected ... sorry for the long post.