Crypto: RSA
The RSA sample demonstrates how to use the PSA Crypto API to sign and verify messages using the RSA signature algorithm with SHA-256 as the hashing algorithm and a 4096-bit RSA key pair.
Requirements
The sample supports the following development kits:
Hardware platforms |
PCA |
Board name |
|
|---|---|---|---|
PCA10153 |
|
||
PCA10090 |
|
||
PCA10171 |
|
||
nRF7120 DK |
nrf7120dk |
|
|
nRF54LV10 DK |
PCA10188 |
|
|
nRF54LS05 DK |
PCA10214 |
nrf54ls05dk |
|
PCA10184 |
|
||
nRF54LC10 DK |
PCA10226 |
nrf54lc10dk |
|
PCA10156 |
|
||
PCA10156 |
|
||
PCA10156 |
|
||
PCA10095 |
|
||
PCA10056 |
|
For more security, it is recommended to use the */ns variant of the board target.
When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation.
Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.
Overview
The sample enables PSA Crypto API and configures the following Kconfig options for the cryptographic features:
CONFIG_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORTandCONFIG_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_EXPORT- Used to enable support for RSA key pair types from among the supported cryptographic operations for Key pair operations.CONFIG_PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY- Used to enable support for RSA public key types from among the supported cryptographic operations for Key types and key management.CONFIG_PSA_WANT_ALG_RSA_PKCS1V15_SIGN- Used to enable support for the RSA PKCS#1 v1.5 signature algorithm from among the supported cryptographic operations for Asymmetric signature algorithms.CONFIG_PSA_WANT_ALG_SHA_256- Used to enable support for the SHA-256 hash algorithm from among the supported cryptographic operations for Hash algorithms.CONFIG_PSA_WANT_RSA_KEY_SIZE_4096- Used to enable support for 4096-bit RSA key sizes from among the supported cryptographic operations for RSA key size configuration.
The sample also configures the cryptographic drivers for each board target using Kconfig options in the overlay files in the boards directory.
These Kconfig options are then used by the build system to compile the required cryptographic PSA directives and make the configured cryptographic drivers available at runtime. See Driver selection for more information about this process.
Once built and run, the sample performs the following operations:
Initialization:
The PSA Crypto API is initialized using
psa_crypto_init().An RSA key pair is imported into the PSA crypto keystore using
psa_import_key(). The key pair is configured with usage flags for hash signing.Note
The keys are provided in the
key.hfile in the sample directory. These are for demonstration purposes only and must never be used in a product.The public key is imported into the PSA crypto keystore using
psa_import_key(). The public key is configured with usage flags for hash verification.
RSA signing and verification:
The SHA-256 hash of the plaintext is computed using
psa_hash_compute().The hash is signed using
psa_sign_hash()with thePSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256)algorithm.The signature is verified using
psa_verify_hash()with the imported public key.
Cleanup:
The RSA key pair and public key are removed from the PSA crypto keystore using
psa_destroy_key().
Building and running
This sample can be found under samples/crypto/rsa in the nRF Connect SDK folder structure.
For more security, it is recommended to use the */ns variant of the board target (see the Requirements section above.)
When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation.
Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.
To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.
Note
When building repository applications in the SDK repositories, building with sysbuild is enabled by default.
If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.
Testing
After programming the sample to your development kit, complete the following steps to test it:
Connect to the kit with a terminal emulator (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.
Build and program the application.
Observe the logs from the application using the terminal emulator. For example, the log output should look like this:
*** Booting nRF Connect SDK v3.1.0-6c6e5b32496e ***
*** Using Zephyr OS v4.1.99-1612683d4010 ***
[00:00:00.251,159] <inf> rsa: Starting RSA example...
[00:00:00.251,190] <inf> rsa: Importing RSA key pair...
[00:00:00.251,342] <inf> rsa: RSA private key imported successfully!
[00:00:00.251,373] <inf> rsa: RSA public key imported successfully!
[00:00:00.251,404] <inf> rsa: Signing a message using RSA...
[00:00:00.251,708] <inf> rsa: Signing was successful!
[00:00:00.251,739] <inf> rsa: ---- Plaintext (len: 100): ----
[00:00:00.251,770] <inf> rsa: Content:
Example string to demonstrate basic usage of RSA.
[00:00:00.251,800] <inf> rsa: ---- Plaintext end ----
[00:00:00.251,831] <inf> rsa: ---- SHA256 hash (len: 32): ----
[00:00:00.251,861] <inf> rsa: Content:
12 34 56 78 9a bc de f0 12 34 56 78 9a bc de f0 |.4Vx.... .4Vx....|
12 34 56 78 9a bc de f0 12 34 56 78 9a bc de f0 |.4Vx.... .4Vx....|
[00:00:00.251,892] <inf> rsa: ---- SHA256 hash end ----
[00:00:00.251,922] <inf> rsa: ---- Signature (len: 512): ----
[00:00:00.251,953] <inf> rsa: Content:
12 34 56 78 9a bc de f0 12 34 56 78 9a bc de f0 |.4Vx.... .4Vx....|
[additional signature bytes...]
[00:00:00.252,075] <inf> rsa: ---- Signature end ----
[00:00:00.252,105] <inf> rsa: Verifying RSA signature...
[00:00:00.252,136] <inf> rsa: Signature verification was successful!
[00:00:00.252,166] <inf> rsa: Example finished successfully!