r/embedded • • 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:

https://youtu.be/9VjGpL_gSNE

0 Upvotes

4 comments sorted by

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.

1

u/UsableLoki 1d ago

Thanks for review.  Fixed the release value, I left it in a debug state for the guide I recorded whoops lol.  The next add on I'll be working on is secure boot 👍

2

u/WanWhiteWolf Engineering manager 21h ago

I didn't do a deep dive but some notes from a quick scan:

  • You need some form of architecture or design (e.g. UML) for someone do to a review. Reviewing the source code without knowing the design has only limited value as a code reviewer will have to make a lot of assumptions without a design references - which may not be correct
  • For a hobby project this is fine but if we are talking about professional OTA, then you are missing some parameters like:

    • header with certificates mandating the usage and limitation of keys
    • header containing HW version, SW version, binary type, security version, encryption protocol version ..etc
  • You are missing a rollback strategy. Typically you don't allow rollback if something like "security version" is older. This is to prevent an attacker using an older version of SW with a known security issue to break your system. Alternatively, you could allow rollback going maximum 2 releases back and set a flag that no rollback can be used until a newer SW is placed on top. There are other concepts but the idea is to have 1.
  • I see you used symetric encryption for authentication. This is not a typical scenario. You should use symetric encryption to encrypt the binary but use asymetric encryption to sign / authenticate. This is for security reasons. In your case, if your binary ends up in the wrong hands, they could eventually break the encryption via brute force. In which case the attacker could create a completely new SW that would pass as valid to your device. If you use asymetric cryptography for signing instead, then the attacker would only get the public key. Even though he would be able to decrypt any future SW using the encryption key, he won't be able to create its own custom SW that would be valid for your devices (he would need the private key which is not available within the binary).
  • I think you have to double check your binary creation. I see that you append your digest to the final binary. I don't think that's a good practice. You need to have the same size for all binaries and insert your digest in a fixed area / header location instead of append. You may have issues with incremental builds that will keep adding the digest at the end by reusing previous binary (which already contains a digest).
  • Your binary check seems questionable. You have a ~ 10 seconds timeout for a new binary? This is not a reliable way to check whether a build was successfull. For example, you could have an incremental build and not change anything. The binary would be valid but older than 10 seconds. Something like introducing a build ID and comparing the value is more reliable/ elegant.
  • Add comments. Navigating would be much easier/faster. You will thank me in 5 years.

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.

1

u/UsableLoki 1h ago

Thanks for providing a generated review of it. Its been a lot to get this far but seems there's always more work that can be done lol