|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#if CHIP_HAVE_CONFIG_H |
| 21 | +#include <crypto/CryptoBuildConfig.h> |
| 22 | +#endif // CHIP_HAVE_CONFIG_H |
| 23 | + |
| 24 | +#include <lib/core/CHIPError.h> |
| 25 | +#include <lib/core/DataModelTypes.h> |
| 26 | +#include <lib/support/TypeTraits.h> |
| 27 | + |
| 28 | +namespace chip { |
| 29 | +namespace Crypto { |
| 30 | + |
| 31 | +static_assert(PSA_KEY_ID_USER_MIN <= CHIP_CONFIG_CRYPTO_PSA_KEY_ID_BASE && CHIP_CONFIG_CRYPTO_PSA_KEY_ID_END <= PSA_KEY_ID_USER_MAX, |
| 32 | + "Matter specific PSA key range doesn't fit within PSA allowed range"); |
| 33 | + |
| 34 | +// Each ICD client requires storing two keys- AES and HMAC |
| 35 | +static constexpr uint32_t kMaxICDClientKeys = 2 * CHIP_CONFIG_CRYPTO_PSA_ICD_MAX_CLIENTS; |
| 36 | + |
| 37 | +static_assert(kMaxICDClientKeys >= CHIP_CONFIG_ICD_CLIENTS_SUPPORTED_PER_FABRIC * CHIP_CONFIG_MAX_FABRICS, |
| 38 | + "Number of allocated ICD key slots is lower than maximum number of supported ICD clients"); |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Defines subranges of the PSA key identifier space used by Matter. |
| 42 | + */ |
| 43 | +enum class KeyIdBase : psa_key_id_t |
| 44 | +{ |
| 45 | + Minimum = CHIP_CONFIG_CRYPTO_PSA_KEY_ID_BASE, |
| 46 | + Operational = Minimum, ///< Base of the PSA key ID range for Node Operational Certificate private keys |
| 47 | + DACPrivKey = Operational + kMaxValidFabricIndex + 1, |
| 48 | + ICDKeyRangeStart = DACPrivKey + 1, |
| 49 | + Maximum = ICDKeyRangeStart + kMaxICDClientKeys, |
| 50 | +}; |
| 51 | + |
| 52 | +static_assert(to_underlying(KeyIdBase::Minimum) >= CHIP_CONFIG_CRYPTO_PSA_KEY_ID_BASE && |
| 53 | + to_underlying(KeyIdBase::Maximum) <= CHIP_CONFIG_CRYPTO_PSA_KEY_ID_END, |
| 54 | + "PSA key ID base out of allowed range"); |
| 55 | + |
| 56 | +/** |
| 57 | + * @brief Finds first free persistent Key slot ID within range. |
| 58 | + * |
| 59 | + * @param[out] keyId Key ID handler to which free ID will be set. |
| 60 | + * @param[in] start Starting ID in search range. |
| 61 | + * @param[in] range Search range. |
| 62 | + * |
| 63 | + * @retval CHIP_NO_ERROR On success. |
| 64 | + * @retval CHIP_ERROR_INTERNAL On PSA crypto API error. |
| 65 | + * @retval CHIP_ERROR_NOT_FOUND On no free Key ID within range. |
| 66 | + * @retval CHIP_ERROR_INVALID_ARGUMENT On search arguments out of PSA allowed range. |
| 67 | + */ |
| 68 | +CHIP_ERROR FindFreeKeySlotInRange(psa_key_id_t & keyId, psa_key_id_t start, uint32_t range); |
| 69 | + |
| 70 | +/** |
| 71 | + * @brief Calculates PSA key ID for Node Operational Certificate private key for the given fabric. |
| 72 | + */ |
| 73 | +constexpr psa_key_id_t MakeOperationalKeyId(FabricIndex fabricIndex) |
| 74 | +{ |
| 75 | + return to_underlying(KeyIdBase::Operational) + static_cast<psa_key_id_t>(fabricIndex); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * @brief Interface for PSA key allocation. |
| 80 | + * |
| 81 | + * The PSA Key Allocator interface provides an abstraction that allows the application to |
| 82 | + * allocate PSA keys in a secure environment. This class uses a concept that isolates the |
| 83 | + * application from the actual key material. The secure location may vary depending on the |
| 84 | + * cryptographic hardware used. Using this class a platform can implement this interface to |
| 85 | + * allocate keys in the specific secure location. |
| 86 | + * |
| 87 | + * In some cases key attributes must be redefined to match the specific requirements of the |
| 88 | + * secure location and the cryptographic hardware. |
| 89 | + * |
| 90 | + * This class keeps the static instance and uses it in the all the places where the key |
| 91 | + * should be persisted. You can add the usage of this class anywhere in the code by calling the |
| 92 | + * GetPSAKeyAllocator() function. |
| 93 | + * |
| 94 | + * If the static instance is not set, the default implementation is used. |
| 95 | + * |
| 96 | + * To change the static instance of the PSAKeyAllocator, you can call the SetPSAKeyAllocator function. |
| 97 | + */ |
| 98 | +class PSAKeyAllocator |
| 99 | +{ |
| 100 | +public: |
| 101 | + /** |
| 102 | + * @brief Destructor for PSAKeyAllocator. |
| 103 | + */ |
| 104 | + virtual ~PSAKeyAllocator() = default; |
| 105 | + |
| 106 | + /** |
| 107 | + * @brief Get the Device Attestation Key (DAC) ID. |
| 108 | + * |
| 109 | + * @return psa_key_id_t The DAC key ID. |
| 110 | + */ |
| 111 | + virtual psa_key_id_t GetDacKeyId() = 0; |
| 112 | + |
| 113 | + /** |
| 114 | + * @brief Get the Node Operational Certificate key ID for a given fabric index. |
| 115 | + * |
| 116 | + * @param fabricIndex The fabric index for which the operational key ID is requested. |
| 117 | + * @return psa_key_id_t The operational key ID. |
| 118 | + */ |
| 119 | + virtual psa_key_id_t GetOpKeyId(FabricIndex fabricIndex) = 0; |
| 120 | + |
| 121 | + /** |
| 122 | + * @brief Allocate a new Intermittently Connected Devices (ICD) key ID. |
| 123 | + * |
| 124 | + * This method is used to allocate both AES-CCM and HMAC (SHA-256) keys independently. |
| 125 | + * The caller is responsible for storing the key ID in non-volatile memory |
| 126 | + * and setting the appropriate key type. |
| 127 | + * |
| 128 | + * @return psa_key_id_t The newly allocated ICD key ID. |
| 129 | + */ |
| 130 | + virtual psa_key_id_t AllocateICDKeyId() = 0; |
| 131 | + |
| 132 | + /** |
| 133 | + * @brief Update the key attributes before storing the key. |
| 134 | + * |
| 135 | + * In some cases the key attributes must be redefined to match the specific requirements of the |
| 136 | + * secure location and the cryptographic hardware. This method allows the platform to update the |
| 137 | + * key attributes before storing the key. |
| 138 | + * |
| 139 | + * Read the current key attributes to determine the key type, algorithm, and usage flags. Update |
| 140 | + * the key attributes as needed. |
| 141 | + * |
| 142 | + * @param attrs Reference to the key attributes structure to be updated. |
| 143 | + */ |
| 144 | + virtual void UpdateKeyAttributes(psa_key_attributes_t & attrs) = 0; |
| 145 | + |
| 146 | + // Allow setting and getting the static instance of the PSAKeyAllocator by external functions |
| 147 | + friend PSAKeyAllocator & GetPSAKeyAllocator(); |
| 148 | + friend void SetPSAKeyAllocator(PSAKeyAllocator * keyAllocator); |
| 149 | + |
| 150 | +private: |
| 151 | + static PSAKeyAllocator * sInstance; |
| 152 | +}; |
| 153 | + |
| 154 | +/** |
| 155 | + * @brief Default implementation of PSAKeyAllocator. |
| 156 | + * |
| 157 | + * This default implementation allocates key IDs according to the KeyIdBase enum. |
| 158 | + * The operational key ID is calculated as the base operational key ID plus the fabric index. |
| 159 | + * The DAC key ID is calculated as the base DAC key ID. |
| 160 | + * The ICD key ID is allocated from the range starting from the ICDKeyRangeStart. |
| 161 | + * The key attributes are not updated. |
| 162 | + */ |
| 163 | +class DefaultPSAKeyAllocator : public PSAKeyAllocator |
| 164 | +{ |
| 165 | +public: |
| 166 | + // implementations of the PSAKeyAllocator interface |
| 167 | + psa_key_id_t GetDacKeyId() override { return to_underlying(KeyIdBase::DACPrivKey); } |
| 168 | + psa_key_id_t GetOpKeyId(FabricIndex fabricIndex) override { return MakeOperationalKeyId(fabricIndex); } |
| 169 | + psa_key_id_t AllocateICDKeyId() override |
| 170 | + { |
| 171 | + psa_key_id_t newKeyId = PSA_KEY_ID_NULL; |
| 172 | + if (CHIP_NO_ERROR != |
| 173 | + Crypto::FindFreeKeySlotInRange(newKeyId, to_underlying(KeyIdBase::ICDKeyRangeStart), kMaxICDClientKeys)) |
| 174 | + { |
| 175 | + newKeyId = PSA_KEY_ID_NULL; |
| 176 | + } |
| 177 | + return newKeyId; |
| 178 | + } |
| 179 | + void UpdateKeyAttributes(psa_key_attributes_t & attrs) override |
| 180 | + { |
| 181 | + // Do nothing |
| 182 | + } |
| 183 | +}; |
| 184 | + |
| 185 | +/** |
| 186 | + * @brief Static function to get the instance of PSAKeyAllocator. |
| 187 | + * |
| 188 | + * If the static instance is not set, the default implementation is returned. |
| 189 | + * |
| 190 | + * @return PSAKeyAllocator reference to the instance of PSAKeyAllocator. |
| 191 | + */ |
| 192 | +inline PSAKeyAllocator & GetPSAKeyAllocator() |
| 193 | +{ |
| 194 | + if (!PSAKeyAllocator::sInstance) |
| 195 | + { |
| 196 | + static DefaultPSAKeyAllocator defaultAllocator; |
| 197 | + return defaultAllocator; |
| 198 | + } |
| 199 | + return *PSAKeyAllocator::sInstance; |
| 200 | +} |
| 201 | + |
| 202 | +/** |
| 203 | + * @brief Set the static implementation of the PSAKeyAllocator. |
| 204 | + * |
| 205 | + * Providing nullptr as an argument will revert to the default implementation. |
| 206 | + * |
| 207 | + * @param keyAllocator Pointer to the PSAKeyAllocator instance to be set. |
| 208 | + */ |
| 209 | +inline void SetPSAKeyAllocator(PSAKeyAllocator * keyAllocator) |
| 210 | +{ |
| 211 | + PSAKeyAllocator::sInstance = keyAllocator; |
| 212 | +} |
| 213 | + |
| 214 | +// Initialize the static global PSAKeyAllocator instance |
| 215 | +// To avoid the need for an additional source file, we initialize the static instance here using the 'inline' keyword. |
| 216 | +// This is possible due to a C++17 feature, which is available starting from the 201606L standard. |
| 217 | +// The functionality is verified in the TestPSAOpKeyStore/ TestKeyAllocation test case. |
| 218 | +inline PSAKeyAllocator * PSAKeyAllocator::sInstance = nullptr; |
| 219 | + |
| 220 | +} // namespace Crypto |
| 221 | +} // namespace chip |
0 commit comments