Crypto: PBKDF2
The PBKDF2 sample demonstrates how to use the PSA Crypto API to derive keys using the PBKDF2 algorithm with HMAC-SHA-256 as the underlying pseudorandom function. The sample uses a sample password, salt, and iteration count to derive a new key.
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 |
|
||
PCA10175 |
|
||
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_ALG_PBKDF2_HMAC- Used to enable support for the PBKDF2 key derivation algorithm from among the supported cryptographic operations for KDF algorithms.CONFIG_PSA_WANT_ALG_HMAC- Used to enable support for the HMAC algorithm from among the supported cryptographic operations for MAC 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.
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().The input password is imported into the PSA crypto keystore using
psa_import_key(). The password is configured with usage flags for key derivation.
Key derivation:
The PBKDF2 key derivation operation is set up using
psa_key_derivation_setup()with thePSA_ALG_PBKDF2_HMAC(PSA_ALG_SHA_256)algorithm.The iteration count is provided using
psa_key_derivation_input_integer().The salt is provided using
psa_key_derivation_input_bytes().The input password is provided using
psa_key_derivation_input_key().The derived key is generated using
psa_key_derivation_output_bytes().The derivation operation is cleaned up using
psa_key_derivation_abort().
Cleanup:
The input password is removed from the PSA crypto keystore using
psa_destroy_key().
Building and running
This sample can be found under samples/crypto/pbkdf2 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> pkbdf2: Starting PBKDF2 example...
[00:00:00.251,190] <inf> pkbdf2: Compare derived key with expected value...
[00:00:00.251,342] <inf> pkbdf2: Key derivation successful!
[00:00:00.251,373] <inf> pkbdf2: ---- Password (len: 6): ----
[00:00:00.251,404] <inf> pkbdf2: Content:
70 61 73 73 77 64 |passwd|
[00:00:00.251,434] <inf> pkbdf2: ---- Password end ----
[00:00:00.251,465] <inf> pkbdf2: ---- Salt (len: 4): ----
[00:00:00.251,495] <inf> pkbdf2: Content:
73 61 6c 74 |salt|
[00:00:00.251,526] <inf> pkbdf2: ---- Salt end ----
[00:00:00.251,556] <inf> pkbdf2: Iteration count: 1
[00:00:00.251,587] <inf> pkbdf2: ---- Derived key (len: 64): ----
[00:00:00.251,617] <inf> pkbdf2: Content:
55 ac 04 6e 56 e3 08 9f ec 16 91 c2 25 44 b6 05 |U..nV.....%D..|
f9 41 85 21 6d de 04 65 e6 8b 9d 57 c2 0d ac bc |.A.!m..e...W....|
49 ca 9c cc f1 79 b6 45 99 16 64 b3 9d 77 ef 31 |I....y.E..d..w.1|
7c 71 b8 45 b1 e3 0b d5 09 11 20 41 d3 a1 97 83 |.q.E...... A....|
[00:00:00.251,648] <inf> pkbdf2: ---- Derived key end ----
[00:00:00.251,678] <inf> pkbdf2: Example finished successfully!