Skip to content

Commit 2630f6d

Browse files
[nrf toup] Migrate DAC to CRACEN KMU on nRF54L devices
On nRF54L devices we can use KMU to store the DAC private key, and allow using it directly by CRACEN. By default the DAC private key is encrypted so it needs 4 KMU slots. Signed-off-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
1 parent 731aac5 commit 2630f6d

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

config/nrfconnect/chip-module/Kconfig

+33-1
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,47 @@ config CHIP_ENABLE_READ_CLIENT
365365
Disabling this config can save flash and RAM space.
366366

367367
config CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
368-
bool "Migrate DAC private key from factory data to PSA ITS"
368+
bool "Migrate DAC private key from factory data to a secure storage"
369369
depends on CHIP_CRYPTO_PSA
370370
depends on CHIP_FACTORY_DATA
371+
372+
choice CHIP_CRYPTO_PSA_DAC_PRIV_KEY_MIGRATION_DEST
373+
prompt "Destination for DAC private key migration"
374+
default CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS
375+
376+
config CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS
377+
bool "Migrate DAC private key from factory data to PSA ITS"
371378
help
372379
Move DAC private key from the factory data set to the PSA ITS secure storage
373380
and remove it. After the first boot of the device the DAC private key will be moved
374381
to the PSA ITS secure storage and will not be available in the factory data anymore.
375382
It will be overwritten in the factory data set by zeros.
376383

384+
config CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
385+
bool "Migrate DAC private key from factory data to CRACEN KMU"
386+
depends on CRACEN_LIB_KMU
387+
help
388+
Move DAC private key from the factory data set to the CRACEN Key Management Unit (KMU) secure
389+
storage and remove it. After the first boot of the device the DAC private key will be
390+
moved to the CRACEN KMU secure storage and will not be available in the factory data anymore.
391+
It will be overwritten in the factory data set by zeros.
392+
393+
endchoice
394+
395+
config CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_SLOT_ID
396+
int "Destination DAC private key slot ID inside CRACEN KMU"
397+
depends on CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
398+
range 0 179 # Allow using the application usage space only
399+
default 176 if CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_ENCRYPTED
400+
default 178
401+
402+
config CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_ENCRYPTED
403+
bool "Encrypt DAC private key in CRACEN KMU"
404+
default y
405+
depends on CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
406+
help
407+
Encrypt the DAC private key in the CRACEN KMU secure storage.
408+
377409
config CHIP_PERSISTENT_SUBSCRIPTIONS
378410
default n
379411
# selecting experimental for this feature since there is an issue with multiple controllers.

src/platform/nrfconnect/FactoryDataProvider.cpp

+40-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
#include <lib/support/logging/CHIPLogging.h>
2828

29+
#ifdef CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
30+
#include <cracen_psa_kmu.h>
31+
#endif
32+
2933
#ifdef CONFIG_CHIP_CRYPTO_PSA
3034
#include <lib/support/ScopedBuffer.h>
3135
#include <psa/crypto.h>
@@ -139,26 +143,43 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::MoveDACPrivateKeyToSecureStora
139143
{
140144
ChipLogProgress(DeviceLayer, "Found DAC Private Key in factory data set. Copying to secure storage...");
141145

146+
#if defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS)
142147
// Remove the key if any exists and can be corrupted.
143148
psa_destroy_key(mDACPrivKeyId);
149+
#endif
144150

145151
psa_reset_key_attributes(&attributes);
146152
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
147153
psa_set_key_bits(&attributes, kDACPrivateKeyLength * 8);
148-
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
154+
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
155+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
149156
#ifdef CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
157+
#if defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS)
150158
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
151159
psa_set_key_id(&attributes, mDACPrivKeyId);
160+
#elif defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU)
161+
psa_set_key_lifetime(
162+
&attributes,
163+
PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_PERSISTENCE_DEFAULT, PSA_KEY_LOCATION_CRACEN_KMU));
164+
psa_set_key_id(&attributes,
165+
PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(
166+
#ifdef CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_ENCRYPTED
167+
CRACEN_KMU_KEY_USAGE_SCHEME_ENCRYPTED,
168+
#else
169+
CRACEN_KMU_KEY_USAGE_SCHEME_RAW,
170+
#endif // CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_ENCRYPTED
171+
CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_SLOT_ID));
172+
#endif // CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS || CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
152173
#else
153174
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_VOLATILE);
154-
#endif
155-
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
156-
157-
VerifyOrReturnError(psa_import_key(&attributes, reinterpret_cast<uint8_t *>(mFactoryData.dac_priv_key.data),
158-
kDACPrivateKeyLength, &mDACPrivKeyId) == PSA_SUCCESS,
159-
CHIP_ERROR_INTERNAL);
175+
#endif // CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
160176
}
161177

178+
psa_status_t status = psa_import_key(&attributes, reinterpret_cast<uint8_t *>(mFactoryData.dac_priv_key.data),
179+
mFactoryData.dac_priv_key.len, &mDACPrivKeyId);
180+
ChipLogProgress(DeviceLayer, "xD3.5 %d", status);
181+
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL);
182+
162183
#ifdef CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
163184
#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_SETTINGS
164185
#error "Do not use both CONFIG_CHIP_FACTORY_RESET_ERASE_SETTINGS and CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY kconfig options " \
@@ -275,8 +296,19 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::SignWithDeviceAttestationKey(c
275296

276297
#ifdef CONFIG_CHIP_CRYPTO_PSA
277298
size_t outputLen = 0;
299+
#if defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS)
300+
psa_key_id_t keyId = mDACPrivKeyId;
301+
#elif defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU)
302+
psa_key_id_t keyId = static_cast<psa_key_id_t>(PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(
303+
#ifdef CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_ENCRYPTED
304+
CRACEN_KMU_KEY_USAGE_SCHEME_ENCRYPTED,
305+
#else
306+
CRACEN_KMU_KEY_USAGE_SCHEME_RAW,
307+
#endif // CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_ENCRYPTED
308+
CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_SLOT_ID));
309+
#endif // CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS || CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
278310

279-
psa_status_t err = psa_sign_message(mDACPrivKeyId, PSA_ALG_ECDSA(PSA_ALG_SHA_256), messageToSign.data(), messageToSign.size(),
311+
psa_status_t err = psa_sign_message(keyId, PSA_ALG_ECDSA(PSA_ALG_SHA_256), messageToSign.data(), messageToSign.size(),
280312
signature.Bytes(), signature.Capacity(), &outputLen);
281313

282314
VerifyOrReturnError(!err, CHIP_ERROR_INTERNAL);

0 commit comments

Comments
 (0)