Skip to content

Commit 5580536

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 1fb9792 commit 5580536

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

config/nrfconnect/chip-module/Kconfig

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

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

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

src/platform/nrfconnect/FactoryDataProvider.cpp

+31-11
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>
@@ -129,6 +133,17 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::MoveDACPrivateKeyToSecureStora
129133
uint8_t clearedDACPrivKey[kDACPrivateKeyLength];
130134
memset(clearedDACPrivKey, 0x00, sizeof(clearedDACPrivKey));
131135

136+
// If key should be migrated to KMU save the KMU key slot to keyId.
137+
#ifdef CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
138+
mDACPrivKeyId = static_cast<psa_key_id_t>(PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(
139+
#ifdef CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_ENCRYPTED
140+
CRACEN_KMU_KEY_USAGE_SCHEME_ENCRYPTED,
141+
#else
142+
CRACEN_KMU_KEY_USAGE_SCHEME_RAW,
143+
#endif // CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_ENCRYPTED
144+
CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_SLOT_ID));
145+
#endif // CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
146+
132147
// Check if factory data contains DAC private key
133148
if (memcmp(mFactoryData.dac_priv_key.data, clearedDACPrivKey, kDACPrivateKeyLength) != 0)
134149
{
@@ -145,19 +160,24 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::MoveDACPrivateKeyToSecureStora
145160
psa_reset_key_attributes(&attributes);
146161
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
147162
psa_set_key_bits(&attributes, kDACPrivateKeyLength * 8);
148-
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
163+
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
164+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
165+
psa_set_key_id(&attributes, mDACPrivKeyId);
149166
#ifdef CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
167+
#if defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS)
150168
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
151-
psa_set_key_id(&attributes, mDACPrivKeyId);
169+
#elif defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU)
170+
psa_set_key_lifetime(
171+
&attributes,
172+
PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_PERSISTENCE_DEFAULT, PSA_KEY_LOCATION_CRACEN_KMU));
173+
#endif // CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS || CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
152174
#else
153175
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);
176+
#endif // CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
160177
}
178+
VerifyOrReturnError(psa_import_key(&attributes, reinterpret_cast<uint8_t *>(mFactoryData.dac_priv_key.data),
179+
mFactoryData.dac_priv_key.len, &mDACPrivKeyId) == PSA_SUCCESS,
180+
CHIP_ERROR_INTERNAL);
161181

162182
#ifdef CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
163183
#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_SETTINGS
@@ -203,12 +223,12 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::MoveDACPrivateKeyToSecureStora
203223
// Verify if the factory data does not contain the DAC private key anymore.
204224
VerifyOrReturnError(memcmp(mFactoryData.dac_priv_key.data, clearedDACPrivKey, kDACPrivateKeyLength) == 0,
205225
CHIP_ERROR_INTERNAL);
206-
#endif
226+
#endif // CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
207227
}
208228

209229
return CHIP_NO_ERROR;
210230
}
211-
#endif
231+
#endif // CONFIG_CHIP_CRYPTO_PSA
212232

213233
template <class FlashFactoryData>
214234
CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetCertificationDeclaration(MutableByteSpan & outBuffer)
@@ -293,7 +313,7 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::SignWithDeviceAttestationKey(c
293313
LoadKeypairFromRaw(ByteSpan(reinterpret_cast<uint8_t *>(mFactoryData.dac_priv_key.data), mFactoryData.dac_priv_key.len),
294314
ByteSpan(dacPublicKey.Bytes(), dacPublicKey.Length()), keypair));
295315
ReturnErrorOnFailure(keypair.ECDSA_sign_msg(messageToSign.data(), messageToSign.size(), signature));
296-
#endif
316+
#endif // CONFIG_CHIP_CRYPTO_PSA
297317

298318
return CopySpanToMutableSpan(ByteSpan{ signature.ConstBytes(), signature.Length() }, outSignBuffer);
299319
}

0 commit comments

Comments
 (0)