Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for flash encryption #718

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

4rzael
Copy link

@4rzael 4rzael commented Jan 6, 2025

Introduction

This PR adds support for flash encryption, by adding a --encrypt flag to flash and write-bin commands.
Requested by #70

As written below, I still need to perform a few tests (waiting for new chips, I hope to have it done in a few days).

Implementation

When performing encrypted writes, espflash will use non-compressed flashing instructions to the bootloader, as compression + encryption is not supported. Similarly, skip & verify features are disabled on encrypted partitions.

When passed to the write-bin command, espflash perform an encrypted flash directly.

The encrypt flag is stored in WriteBinArgs, and then directly passed to the created RomSegment.

When passed to the flash command, espflash will perform encrypted flashes on bootloader, partition, and application partitions.

The parameter flow is a bit more complex there: we start in FlashArgs, which is used to construct FlashData, which is used to construct IdfBootloaderFormat, which finally populates the appropriate RomSegments.

Tests:

  • write-bin with --encrypt
  • write-bin without --encrypt
  • flash with --encrypt
  • flash without --encrypt
  • cargo-espflash flash with --encrypt
  • cargo-espflash flash without --encrypt

@jessebraham
Copy link
Member

Thanks for the PR. Unfortunately, this include breaking changes and will require a new major release, however we are currently discussing a v4 release anyway, so probably this is not too much of an issue. If I can just ask you to be patient, once we come up with a game plan I can get back to you with regards to merging this. Hopefully within a week or two we will have a better idea of what our plans are moving forward.

@4rzael
Copy link
Author

4rzael commented Jan 8, 2025

Hi ! Thanks for the quick response.
Yes, I understand. I'll wait for your input as to how to get it merged to v4.

@SergioGasquez SergioGasquez linked an issue Jan 9, 2025 that may be closed by this pull request
@jessebraham
Copy link
Member

We did some planning this week for the v4 release. I do not think we will start working on this quite yet, probably not for a few weeks still, but in the meantime I will publish one final v3 release on Monday, and then we can at least unblock this PR (and others).

@ivmarkov
Copy link
Contributor

@4rzael Please excuse my ignorance... I do not understand how espflash can write encrypted payload given that there is no encryption key around?

@ivmarkov
Copy link
Contributor

@4rzael Also I'm a bit lost as to what exactly is requested in #70 ?

My understanding as to what @jessebraham demonstrates in #70, is just one way to do encryption, which is - "first flash plaintext images, and then the bootloader will encrypt them on-the-fly" (that is, given that the bootloader is compiled with encryption support in the first place, which I doubt to be the case with the bootloaders provided by espflash itself and embedded inside it with include_bytes! or whatever). By the way, this is done by the bootloader by it doing a lot of (dangerous!) e-fuse operations, like generating a random encryption key, burning it, then read+write protecting it and a few others.

This is by the way (and to my understanding - I'm still debugging the bootloader so I might very well be wrong!) a one time operation? If you can confirm my understanding is correct?

Like, once it is done, you can't just burn plaintext images and hope the bootloader would encrypt them? Or am I missing something here? My understanding is, once the initial bootloader-driven encryption is done, that's it end of story. The bootloader does not repeat the process anymore, and will assume all new images you upload to be pre-encrypted. and since you don't know the encryption key, you can't pre-encrypt them in the first place? That is, given that the UART downloader was left enabled (which depends on whether you are in DEVELOPMENT or RELEASE mode w.r.t. encryption).

I'm myself following the "other" process, where the encryption key is "externally" provided, and the payload (bootloader, part-table, app image, nvs keys etc.) are pre-encrypted. Seems to be what Espressif recommends as well?:
https://docs.espressif.com/projects/esp-idf/en/v5.4/esp32s3/security/security-features-enablement-workflows.html

If you have an opinion on this stuff - please share. I'm about to enter production with my approach...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Official support for encrypted flash
3 participants