Skip to content

Commit 7e5849d

Browse files
samples: matter: Set custom keystore manager during the init.
We need to set a custom keystore manager in Matter server while using KMU and assign the KMUKeyAllocator. Signed-off-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
1 parent 5b9dd75 commit 7e5849d

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

doc/nrf/protocols/matter/end_product/security.rst

+129
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,135 @@ This is a reference configuration that can be modified in the production firmwar
161161
To use the Oberon backend for specific cryptographic operations supported by both drivers, disable those operations in the CRACEN driver, as it takes priority when both are enabled.
162162
See the :ref:`nrf_security_drivers` documentation for more information.
163163
164+
.. _matter_platforms_security_kmu:
165+
166+
nRF54L Key Management Unit (KMU)
167+
================================
168+
169+
nRF54L Series devices include :ref:`ug_nrf54l_crypto_kmu_cracen_peripherals` that can be used to store cryptographic keys in Matter.
170+
In this solution, the keys are stored within the available slots in the :ref:`ug_nrf54l_crypto_kmu_slots` range that are not reserved for current and future |NCS| use cases.
171+
172+
The default slots range used for Matter is from ``100`` to ``180``, excluding the DAC private key.
173+
For details on the DAC private key configuration, see :ref:`matter_platforms_security_dac_priv_key_kmu`.
174+
To change the slots range, set the :kconfig:option:`CONFIG_CHIP_KMU_SLOT_RANGE_START` and :kconfig:option:`CONFIG_CHIP_KMU_SLOT_RANGE_END` Kconfig options.
175+
The Raw usage scheme defined in the :ref:`ug_nrf54l_crypto_kmu_key_usage_schemes` section is used for all Matter keys.
176+
177+
To use this feature, set the :kconfig:option:`CONFIG_CHIP_STORE_KEYS_IN_KMU` Kconfig option to ``y``, and switch to the ``KMUKeyAllocator`` by calling the ``chip::Crypto::SetPSAKeyAllocator`` method in your code during the Matter stack initialization.
178+
179+
For example:
180+
181+
.. code-block:: cpp
182+
183+
#include <platform/nrfconnect/KMUKeyAllocator.h>
184+
185+
static KMUKeyAllocator kmuAllocator;
186+
Crypto::SetPSAKeyAllocator(&kmuAllocator);
187+
188+
See the :file:`samples/matter/common/src/app/matter_init.cpp` file for a usage example.
189+
190+
Due to limited slots available in the KMU, the maximum number of Matter fabric is limited.
191+
The following table shows the all crypto materials used in Matter, the number of slots needed for each key and a Kconfig option that can be used to adjust the number of each key type:
192+
193+
.. list-table:: KMU slots used by Matter crypto materials
194+
:widths: auto
195+
:header-rows: 1
196+
197+
* - Crypto material in Matter
198+
- Key type
199+
- Number of slots needed for a key
200+
- Multiplication
201+
- Minimum number of keys
202+
- Kconfig option
203+
* - Node Operational Certificate (NOC) private key
204+
- ECC secp256r1 key pair
205+
- 2
206+
- For each Matter fabric
207+
- 5 (5 Matter fabrics for each device)
208+
- :kconfig:option:`CONFIG_CHIP_MAX_FABRICS`
209+
* - Intermittently Connected Device (ICD) Token
210+
- HMAC SHA-256 128-bit keys
211+
- 1
212+
- For each ICD user
213+
- 10 (2 ICD users for each Matter fabric)
214+
- :kconfig:option:`CONFIG_CHIP_ICD_CLIENTS_PER_FABRIC`
215+
* - Intermittently Connected Device (ICD) symmetric Key
216+
- AES 128-bit keys
217+
- 1
218+
- For each ICD user
219+
- 10 (2 ICD users for each Matter fabric)
220+
- :kconfig:option:`CONFIG_CHIP_ICD_CLIENTS_PER_FABRIC`
221+
* - Group key [3]_
222+
- AES 128-bit keys
223+
- 1
224+
- For each group
225+
- 15 (3 groups for each Matter fabric)
226+
- Not a Kconfig option, see ``CHIP_CONFIG_MAX_GROUP_ENDPOINTS_PER_FABRIC`` Matter config.
227+
* - Device Attestation Certificate (DAC) private key
228+
- ECC secp256r1 key pair
229+
- 2
230+
- For each device
231+
- 1
232+
- Not configurable
233+
234+
.. [3] Group keys are not stored in the KMU yet, but the slots are reserved for the future usage.
235+
A key may be shared between multiple groups, so the number of slots needed for group keys may be lower than the number of groups.
236+
Three group keys are assumed for each Matter fabric.
237+
238+
The default slots range allows storing more cryptographic materials than the minimum required according to the Matter specification.
239+
A minimum of 46 slots is required for cryptographic materials, including the DAC private key to support 5 fabrics (minimum required by the specification).
240+
A single Matter fabric requires at least nine KMU slots.
241+
242+
The default range has been chosen to support up to eight Matter fabrics, 32 ICD keys (16 ICD users), and 24 group keys.
243+
You can change the default Kconfig option for each crypto material to adjust the number of slots, but you must ensure that the total number of slots fits within the defined range.
244+
If you overlap the slots range, the appropriate compilation error will be shown:
245+
246+
.. code-block:: console
247+
248+
error: "The number of slots exceeds the range of the KMU defined in CONFIG_CHIP_KMU_SLOT_RANGE_START and CONFIG_CHIP_KMU_SLOT_RANGE_END"
249+
250+
For example, to fill the default slots range and support 8 Matter fabrics, 3 group keys per fabric, and 2 ICD users per fabric, set the :kconfig:option:`CONFIG_CHIP_MAX_FABRICS` Kconfig option to 8.
251+
252+
Slots arrangement within the slot range defined by :kconfig:option:`CONFIG_CHIP_KMU_SLOT_RANGE_START` and :kconfig:option:`CONFIG_CHIP_KMU_SLOT_RANGE_END` Kconfig options is calculated automatically in the code in the following way:
253+
254+
.. list-table:: KMU slots arrangement for Matter crypto materials
255+
:widths: auto
256+
:header-rows: 1
257+
258+
* - Crypto material
259+
- Slot range start
260+
- Slot range end
261+
- Default slot range for 8 Matter fabrics to fit the default range (100-180)
262+
* - Node Operational Certificate (NOC) private key
263+
- :kconfig:option:`CONFIG_CHIP_KMU_SLOT_RANGE_START`
264+
- :kconfig:option:`CONFIG_CHIP_KMU_SLOT_RANGE_START` + (:kconfig:option:`CONFIG_CHIP_MAX_FABRICS` * 2)
265+
- 100 - 115
266+
* - Intermittently Connected Device (ICD) Token + ICD symmetric Key
267+
- End of NOC slots
268+
- End of NOC slots + (:kconfig:option:`CONFIG_CHIP_ICD_CLIENTS_PER_FABRIC` * :kconfig:option:`CONFIG_CHIP_MAX_FABRICS` * 2)
269+
- 116 - 147
270+
* - Group keys
271+
- End of ICD slots
272+
- End of ICD slots + (3 * :kconfig:option:`CONFIG_CHIP_MAX_FABRICS`)
273+
- 148 - 171
274+
* - Device Attestation Certificate (DAC) private key
275+
- Configurable with :kconfig:option:`CONFIG_CHIP_CRYPTO_PSA_DAC_PRIV_KEY_KMU_SLOT_ID`
276+
- DAC private key start slot + 1 (non-encrypted) or + 3 (encrypted)
277+
- 176 - 179 (encrypted) or 178 - 179 (non-encrypted)
278+
279+
.. important::
280+
281+
Once a slot range is defined, it should not be changed during the firmware update process.
282+
Changing the slot range will result in the loss of all cryptographic materials stored in the KMU.
283+
You can safely change slot ranges only when the device is in the factory state and no cryptographic materials are stored in the KMU.
284+
Otherwise, to extend the slot range beyond the previously defined range and keep the existing cryptographic materials stored in the KMU complete the following steps:
285+
286+
1. Locate the :file:`src/platform/nrfconnect/KMUKeyAllocator.h` file in the Matter repository, or create a new one with the same functionality.
287+
#. Modify the file content to redirect the keys to the new slot range by implementing the ``GetDacKeyId``, ``GetOpKeyId``, and ``AllocateICDKeyId`` functions.
288+
#. If you want to use your own implementation of the KMUKeyAllocator, assign it in your application code by calling the ``Crypto::SetPSAKeyAllocator`` method.
289+
#. Ensure that you have chosen an available slot range that does not overlap with any existing one.
290+
#. Build an application with the new configuration.
291+
#. Perform a firmware update on the device.
292+
164293
.. _matter_platforms_security_dac_priv_key:
165294

166295
Storing Device Attestation Certificate private key

doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Matter
163163
* A description for the new :ref:`ug_matter_gs_tools_matter_west_commands_append` within the :ref:`ug_matter_gs_tools_matter_west_commands` page.
164164
* New arguments to the :ref:`ug_matter_gs_tools_matter_west_commands_zap_tool_gui` to provide a custom cache directory and add new clusters to Matter Data Model.
165165
* :ref:`ug_matter_debug_snippet`.
166+
* Storing Matter key materials in the :ref:`matter_platforms_security_kmu`.
166167

167168
* Disabled the :ref:`mpsl` before performing factory reset to speed up the process.
168169

samples/matter/common/src/app/matter_init.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#include <ram_pwrdn.h>
4747
#endif
4848

49+
#ifdef CONFIG_CHIP_STORE_KEYS_IN_KMU
50+
#include <platform/nrfconnect/KMUKeyAllocator.h>
51+
#endif
52+
4953
#include <app/InteractionModelEngine.h>
5054
#include <app/clusters/network-commissioning/network-commissioning.h>
5155
#include <credentials/examples/DeviceAttestationCredsExample.h>
@@ -72,6 +76,10 @@ Clusters::NetworkCommissioning::Instance Nrf::Matter::InitData::sWiFiCommissioni
7276
chip::Crypto::PSAOperationalKeystore Nrf::Matter::InitData::sOperationalKeystoreDefault{};
7377
#endif
7478

79+
#ifdef CONFIG_CHIP_STORE_KEYS_IN_KMU
80+
chip::DeviceLayer::KMUSessionKeystore Nrf::Matter::InitData::sKMUSessionKeystoreDefault{};
81+
#endif
82+
7583
#ifdef CONFIG_CHIP_FACTORY_DATA
7684
FactoryDataProvider<InternalFlashFactoryData> Nrf::Matter::InitData::sFactoryDataProviderDefault{};
7785
#endif
@@ -87,6 +95,9 @@ Nrf::Matter::InitData sLocalInitData{ .mNetworkingInstance = nullptr,
8795
#endif
8896
#ifdef CONFIG_CHIP_CRYPTO_PSA
8997
.mOperationalKeyStore = nullptr,
98+
#endif
99+
#ifdef CONFIG_CHIP_STORE_KEYS_IN_KMU
100+
.mSessionKeystore = nullptr,
90101
#endif
91102
.mPreServerInitClbk = nullptr,
92103
.mPostServerInitClbk = nullptr };
@@ -277,6 +288,13 @@ void DoInitChipServer(intptr_t /* unused */)
277288
sLocalInitData.mServerInitParams->operationalKeystore = sLocalInitData.mOperationalKeyStore;
278289
#endif
279290

291+
/* Set KMUKeyAllocator for devices that supports KMU */
292+
#ifdef CONFIG_CHIP_STORE_KEYS_IN_KMU
293+
static KMUKeyAllocator kmuAllocator;
294+
Crypto::SetPSAKeyAllocator(&kmuAllocator);
295+
sLocalInitData.mServerInitParams->sessionKeystore = sLocalInitData.mSessionKeystore;
296+
#endif
297+
280298
VerifyOrReturn(sLocalInitData.mServerInitParams, LOG_ERR("No valid server initialization parameters"));
281299
sInitResult = sLocalInitData.mServerInitParams->InitializeStaticResourcesBeforeServerInit();
282300
VerifyInitResultOrReturn(sInitResult, "InitializeStaticResourcesBeforeServerInit() failed");

samples/matter/common/src/app/matter_init.h

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include <crypto/PSAOperationalKeystore.h>
2424
#endif
2525

26+
#ifdef CONFIG_CHIP_STORE_KEYS_IN_KMU
27+
#include <platform/nrfconnect/KMUSessionKeystore.h>
28+
#endif
29+
2630
#ifdef CONFIG_CHIP_FACTORY_DATA
2731
#include <platform/nrfconnect/FactoryDataProvider.h>
2832
#else
@@ -58,6 +62,10 @@ struct InitData {
5862
#ifdef CONFIG_CHIP_CRYPTO_PSA
5963
/** @brief Pointer to the user provided OperationalKeystore implementation. */
6064
chip::Crypto::OperationalKeystore *mOperationalKeyStore{ &sOperationalKeystoreDefault };
65+
#endif
66+
#ifdef CONFIG_CHIP_STORE_KEYS_IN_KMU
67+
/** @brief Pointer to the user provided SessionKeystore implementation. */
68+
chip::Crypto::SessionKeystore *mSessionKeystore{ &sKMUSessionKeystoreDefault };
6169
#endif
6270
/** @brief Custom code to execute in the Matter main event loop before the server initialization. */
6371
CustomInit mPreServerInitClbk{ nullptr };
@@ -77,6 +85,9 @@ struct InitData {
7785
#ifdef CONFIG_CHIP_CRYPTO_PSA
7886
static chip::Crypto::PSAOperationalKeystore sOperationalKeystoreDefault;
7987
#endif
88+
#ifdef CONFIG_CHIP_STORE_KEYS_IN_KMU
89+
static chip::DeviceLayer::KMUSessionKeystore sKMUSessionKeystoreDefault;
90+
#endif
8091
};
8192

8293
/**

0 commit comments

Comments
 (0)