ARM TrustZone for Cortex-M: What It Is and How It Works
How It Works
ARM TrustZone for Cortex-M works by dividing the processor into two distinct environments: Secure and Non-secure. Imagine your processor as a house with two rooms separated by a locked door. The Secure room holds valuable items like sensitive data and security functions, while the Non-secure room runs regular applications.
The processor hardware enforces this separation, so code running in the Non-secure world cannot directly access Secure resources. When Non-secure code needs to use a Secure service, it must ask through a controlled gateway, ensuring safety and preventing accidental or malicious access.
This setup helps developers build safer embedded systems by protecting critical operations from bugs or attacks in less trusted software.
Example
This example shows how to define a Secure function callable from Non-secure code using the ARM TrustZone mechanism on Cortex-M.
#include "arm_cmse.h" // Secure function attribute to allow Non-secure calls __attribute__((cmse_nonsecure_entry)) int SecureAdd(int a, int b) { return a + b; } int main() { // Non-secure code calls SecureAdd via a pointer int (*NonSecureAdd)(int, int) = (int (*)(int, int))cmse_nsfptr_create(&SecureAdd); int result = NonSecureAdd(3, 4); // result should be 7 return result; }
When to Use
Use ARM TrustZone for Cortex-M when you need to protect sensitive parts of your embedded system, such as cryptographic keys, secure boot code, or payment data. It is ideal for devices like IoT sensors, medical devices, and smart home controllers where security is critical.
TrustZone helps separate trusted software from regular applications, reducing the risk that bugs or attacks in normal code can compromise security. It is especially useful when running third-party or less trusted code alongside secure functions on the same chip.
Key Points
- TrustZone splits Cortex-M into Secure and Non-secure worlds.
- Hardware enforces isolation between these worlds.
- Secure code protects sensitive data and functions.
- Non-secure code accesses Secure services via controlled gateways.
- Commonly used in IoT and embedded security applications.