Skip to content

Commit aaf316d

Browse files
dac in kmu
1 parent 731aac5 commit aaf316d

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

config/nrfconnect/chip-module/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ target_compile_definitions(chip INTERFACE _POSIX_C_SOURCE=200809)
235235
# Make sure that kernel symbols that are only referenced by the Matter libraries are resolved.
236236
target_link_libraries(chip INTERFACE $<TARGET_FILE:kernel>)
237237

238+
if(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU)
239+
include(cracenpsa/cracenpsa.cmake)
240+
target_link_libraries(chip INTERFACE cracen_psa_driver)
241+
endif()
242+
238243
if (CONFIG_CHIP_MALLOC_SYS_HEAP_OVERRIDE)
239244
target_link_options(chip INTERFACE
240245
-Wl,--wrap=malloc

config/nrfconnect/chip-module/Kconfig

+26-1
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,40 @@ 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 Migrate DAC private key from factory data to 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+
select EXPERIMENTAL
388+
help
389+
Move DAC private key from the factory data set to the CRACEN Key Management Unit (KMU) secure
390+
storage and remove it. After the first boot of the device the DAC private key will be
391+
moved to the CRACEN KMU secure storage and will not be available in the factory data anymore.
392+
It will be overwritten in the factory data set by zeros.
393+
394+
endchoice
395+
396+
config CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_SLOT_ID
397+
int "Destination DAC private key slot ID inside CRACEN KMU"
398+
depends on CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU
399+
range 0 179 # Allow using the application usage space only
400+
default 179
401+
377402
config CHIP_PERSISTENT_SUBSCRIPTIONS
378403
default n
379404
# selecting experimental for this feature since there is an issue with multiple controllers.

src/platform/nrfconnect/FactoryDataProvider.cpp

+37-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626

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

29+
#if defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU)
30+
#include <cracen_psa.h>
31+
#include <cracen_psa_kmu.h>
32+
#endif
33+
2934
#ifdef CONFIG_CHIP_CRYPTO_PSA
3035
#include <lib/support/ScopedBuffer.h>
3136
#include <psa/crypto.h>
@@ -139,24 +144,46 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::MoveDACPrivateKeyToSecureStora
139144
{
140145
ChipLogProgress(DeviceLayer, "Found DAC Private Key in factory data set. Copying to secure storage...");
141146

147+
#if defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS)
142148
// Remove the key if any exists and can be corrupted.
143149
psa_destroy_key(mDACPrivKeyId);
150+
#endif
144151

145152
psa_reset_key_attributes(&attributes);
146153
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
147154
psa_set_key_bits(&attributes, kDACPrivateKeyLength * 8);
148155
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
156+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
149157
#ifdef CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
158+
#if defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS)
150159
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
151160
psa_set_key_id(&attributes, mDACPrivKeyId);
161+
VerifyOrReturnError(psa_import_key(&attributes, reinterpret_cast<uint8_t *>(mFactoryData.dac_priv_key.data),
162+
kDACPrivateKeyLength, &mDACPrivKeyId) == PSA_SUCCESS,
163+
CHIP_ERROR_INTERNAL);
164+
#elif defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU)
165+
size_t key_bits;
166+
uint8_t opaque_buffer[2];
167+
size_t outlen;
168+
169+
psa_set_key_lifetime(
170+
&attributes,
171+
PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_PERSISTENCE_DEFAULT, PSA_KEY_LOCATION_CRACEN_KMU));
172+
psa_set_key_id(&attributes,
173+
PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW,
174+
CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_SLOT_ID));
175+
VerifyOrReturnError(cracen_import_key(&attributes, reinterpret_cast<uint8_t *>(mFactoryData.dac_priv_key.data),
176+
kDACPrivateKeyLength, opaque_buffer, sizeof(opaque_buffer), &outlen,
177+
&key_bits) == PSA_SUCCESS,
178+
CHIP_ERROR_INTERNAL);
179+
180+
#endif
152181
#else
153182
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_VOLATILE);
154-
#endif
155-
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
156-
157183
VerifyOrReturnError(psa_import_key(&attributes, reinterpret_cast<uint8_t *>(mFactoryData.dac_priv_key.data),
158184
kDACPrivateKeyLength, &mDACPrivKeyId) == PSA_SUCCESS,
159185
CHIP_ERROR_INTERNAL);
186+
#endif // CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
160187
}
161188

162189
#ifdef CONFIG_CHIP_CRYPTO_PSA_MIGRATE_DAC_PRIV_KEY
@@ -275,8 +302,14 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::SignWithDeviceAttestationKey(c
275302

276303
#ifdef CONFIG_CHIP_CRYPTO_PSA
277304
size_t outputLen = 0;
305+
#if defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_ITS)
306+
psa_key_id_t keyId = mDACPrivKeyId;
307+
#elif defined(CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU)
308+
psa_key_id_t keyId = static_cast<psa_key_id_t>(
309+
PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW, CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_SLOT_ID));
310+
#endif
278311

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

282315
VerifyOrReturnError(!err, CHIP_ERROR_INTERNAL);

0 commit comments

Comments
 (0)