STM32N6 HW memory encryption for XSPI devices#417
STM32N6 HW memory encryption for XSPI devices#417jmaksymowicz wants to merge 9 commits intomasterfrom
Conversation
Summary of ChangesHello @jmaksymowicz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the system's security capabilities by integrating hardware memory encryption for XSPI devices on STM32N6. It introduces a robust mechanism for handling encrypted memory regions through memory-mapped operations, moving away from simpler indirect writing. The changes include new device abstractions for encrypted storage and random number generation, a dedicated command for encryption setup, and a more flexible cache management system. These updates are crucial for distinguishing between encrypted and non-encrypted data access and for securely managing cryptographic keys and initial values. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces hardware memory encryption support for the STM32N6 platform, including a new memcrypt command, an RNG driver, and updates to the XSPI driver. While it also includes positive changes like refactoring cache management in the HAL, critical security vulnerabilities have been identified. Specifically, an infinite loop in the RNG driver can lead to a Denial of Service if the hardware fails, and the memcrypt command allows unrestricted access to OTP memory, which could be exploited to leak sensitive system keys. Additionally, there are potential hangs in the MCE driver due to CRC checks on zero-valued results and hardcoded cache geometry in the HAL that limits portability.
9e98680 to
3e2fbfe
Compare
|
/gemini review |
Fix incorrect offset for register access. JIRA: RTOS-1084
Refactor cache operations to unify common code. Use line size from CCSIDR register instead of hardcoded size. JIRA: RTOS-1084
3e2fbfe to
b0e9400
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces hardware memory encryption for XSPI devices on the STM32N6 platform. The changes are extensive, including a new memcrypt command, a driver for the Memory Cipher Engine (MCE), a hardware RNG driver, and significant updates to the XSPI driver to support encrypted regions and DMA-based writes. The cache management code has also been refactored for better genericity. Overall, this is a substantial and well-structured feature addition. I've found a few issues, including a critical bug in a macro definition that would lead to incorrect behavior, and a potential infinite loop in the DMA handling logic. Please see my detailed comments for suggestions.
Change Flash writing procedure from using indirect mode to using memory-mapped mode in preparation for memory encryption using MCE. Add simple driver for GPDMA peripheral to perform memory-to-memory copying. JIRA: RTOS-1084
b0e9400 to
d19782d
Compare
Allow userspace driver to access Flash configuration parameters easily through a blob in syspage. JIRA: RTOS-1216
JIRA: RTOS-1084 Co-authored-by: winderdoot <krzysztof.radzewicz@phoenix-rtos.com>
JIRA: RTOS-1084 Co-authored-by: winderdoot <krzysztof.radzewicz@phoenix-rtos.com>
Allow setup and access to encrypted regions through encrypted storage device class. JIRA: RTOS-1084
Allow configuration of encrypted memory on STM32N6 devices. JIRA: RTOS-1084 Co-authored-by: winderdoot <krzysztof.radzewicz@phoenix-rtos.com>
d19782d to
a15393b
Compare
Description
This PR is a finished up version of #403
To allow writing to memory with MCE encryption it was necessary to implement memory-mapped writing. Previously writing in indirect mode was used instead, as it was a lot simpler conceptually. After many attempts, the only 100% reliable way to write data turned out to be using DMA. I used the GPDMA peripheral, as using HPDMA would have required setting up RISAF (memory firewalls), which would have been a lot more complicated.
Two new device classes were added - one for encrypted storage devices and another for RNG devices. The first type makes it possible to distinguish between accesses to encrypted and non-encrypted partitions. The second is just so that RNG for generating keys/IVs can be implemented in a platform-independent way.
On STM32N6 a driver for hardware RNG was added and the XSPI driver was modified to provide both standard and encrypted storage devices.
A new
memcryptcommand was added to set up memory encryption according to user parameters. Some effort was put in so that it could be reused on other targets that require memory encryption, but it is not certain if this API will be sufficient. Comments on potential improvements are welcome.Additionally a small fix to a HAL function is included, as I found a bug during testing. Also, the code for cache management was made more generic w.r.t. line size and common code was unified across different cache operations.
Motivation and Context
JIRA: RTOS-1084
Types of changes
How Has This Been Tested?
Checklist:
Special treatment