Skip to content

feat: use page size for key/trailer manipulation - #965

Open
vhdirk wants to merge 1 commit into
wolfSSL:masterfrom
vhdirk:hal-set-key-page-size
Open

vhdirk wants to merge 1 commit into
wolfSSL:masterfrom
vhdirk:hal-set-key-page-size

Conversation

@vhdirk

@vhdirk vhdirk commented Oct 30, 2026 •

Copy link
Copy Markdown

This commit introduces FLASH_ERASE_UNIT as the smallest possible erase unit.
Together with a smaller sized ENCRYPT_CACHE size, this lowers the amount of ram and copies needed for key/trailer manipulation

This was (re)written with extensive help from AI tools.

Copilot AI balanced review requested due to automatic review settings October 30, 2026 16:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The changes can write before ENCRYPT_CACHE when trailer data does not fit and lose encryption keys on write-once flash because page-based placement conflicts with sector-based key selection and readers.

3 open issues
What changed in this PR

This PR introduces page-sized flash operations for encryption-key and trailer-magic updates, as a step toward reducing wolfBoot’s staging RAM requirements.

Changes:

  • Adds WOLFBOOT_PAGE_SIZE, defaulting to WOLFBOOT_SECTOR_SIZE.
  • Uses page-sized alignment, copying, erasing, and writing in hal_set_key().
  • Changes write-once redundant-copy offsets to use page size.
File Description
src/​libwolfboot.c Adds the page-size fallback and applies page sizing to encryption-key updates.

Review effort: Balanced

Comment thread src/libwolfboot.c Resolved
Comment thread src/libwolfboot.c Resolved
Comment thread src/libwolfboot.c Resolved
@dgarske

dgarske commented Oct 30, 2026

Copy link
Copy Markdown
Member

Hi @vhdirk thanks for this pull request. I do not see you on our contributor agreement list. To get setup please send an email to support at wolfssl dot com and reference this pull request and include details about your project and location. Are you planning any additional contributions? Thanks, David Garske, wolfSSL

@vhdirk

vhdirk commented Nov 2, 2026 •

Copy link
Copy Markdown
Author

@dgarske we (aloxy) should be on the contributer list; we've contributed before. I myself may not be on there. On zendesk, we've mostly been in contact with @danielinux. While I did not reference this PR exactly, Daniele is aware that I have a series of patches coming up.

@vhdirk
vhdirk force-pushed the hal-set-key-page-size branch from bf273e9 to ae7fb81 Compare November 2, 2026 14:01
Add FLASH_ERASE_UNIT / WOLFBOOT_FLASH_ERASE_UNIT independently of the
logical sector size, with a sector-sized fallback and a 128-byte default
for STM32L0.

Update non-write-once hal_set_key() to cache and rewrite only the erase
units touched by the key, nonce, and trailer magic. Preserve surrounding
data, handle fields spanning units and FLAGS_HOME, and validate geometry
and partition bounds before accessing flash.

Default NVM_CACHE_SIZE to the erase unit in non-write-once builds while
keeping write-once caches and redundant trailer copies sector-sized.
On STM32L0 with 4 KiB sectors, the cache shrinks to 128 bytes.

Add erase-geometry, data-preservation, error-handling, and cache-scrubbing
tests for ChaCha20, AES-256, straddling fields, FLAGS_HOME, and the sector
fallback. Document the configuration and HAL erase contract.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vhdirk
vhdirk force-pushed the hal-set-key-page-size branch from ae7fb81 to 3458db7 Compare November 2, 2026 14:08
@dgarske

dgarske commented Nov 2, 2026

Copy link
Copy Markdown
Member

Okay to test. Contributor agreement on file for company.

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.

5 participants