Crypto: Persistent key storage

The persistent key sample shows how to generate a persistent key using the Platform Security Architecture (PSA) APIs. Persistent keys are stored in the Internal Trusted Storage (ITS) of the device and retain their value between resets. A persistent key becomes unusable when the psa_destroy_key function is called.


The sample supports the following development kits:

Hardware platforms


Board name

Build target

nRF9160 DK



nrf9160dk_nrf9160_ns nrf9160dk_nrf9160

nRF5340 DK



nrf5340dk_nrf5340_cpuapp_ns nrf5340dk_nrf5340_cpuapp

nRF52840 DK




When built for an _ns build target, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.


In this sample, an AES 128-bit key is created. Persistent keys can be of any type supported by the PSA APIs.

The sample performs the following operations:

  1. Initialization of the Platform Security Architecture (PSA) API.

  2. Generation of a persistent AES 128-bit key.

  3. Cleanup. The AES key is removed from the PSA crypto keystore.


The read-only type of persistent keys cannot be destroyed with the psa_destroy_key function. The PSA_KEY_PERSISTENCE_READ_ONLY macro is used for read-only keys. The key ID of a read-only key is writable again after a full erase of the device memory. Use the west -v flash --erase command for the full erase.

Building and running

This sample can be found under samples/crypto/persistent_key_usage in the nRF Connect SDK folder structure.

When built as firmware image for the _ns build target, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample with Visual Studio Code, follow the steps listed on the How to build an application page in the nRF Connect for VS Code extension documentation. See Building and programming an application for other building and programming scenarios and Testing and debugging an application for general information about testing and debugging in the nRF Connect SDK.


After programming the sample to your development kit, complete the following steps to test it:

  1. Connect to the kit with a terminal emulator (for example, PuTTY). See How to connect with PuTTY for the required settings.

  2. Compile and program the application.

  3. Observe the logs from the application using an RTT Viewer or a terminal emulator.


By default, the sample is configured to use both RTT and UART for logging. If you are using RTT, skip the first step of the testing procedure.