@@ -35,17 +35,6 @@ class KeyAttributesBase
35
35
psa_set_key_bits (&mAttrs , bits);
36
36
}
37
37
38
- CHIP_ERROR SetKeyPersistence (psa_key_id_t keyId)
39
- {
40
- VerifyOrReturnError (to_underlying (KeyIdBase::Maximum) >= keyId && keyId >= to_underlying (KeyIdBase::Minimum),
41
- CHIP_ERROR_INVALID_ARGUMENT);
42
-
43
- psa_set_key_lifetime (&mAttrs , PSA_KEY_LIFETIME_PERSISTENT);
44
- psa_set_key_id (&mAttrs , keyId);
45
-
46
- return CHIP_NO_ERROR;
47
- }
48
-
49
38
~KeyAttributesBase () { psa_reset_key_attributes (&mAttrs ); }
50
39
51
40
const psa_key_attributes_t & Get () { return mAttrs ; }
@@ -79,6 +68,12 @@ class HkdfKeyAttributes : public KeyAttributesBase
79
68
HkdfKeyAttributes () : KeyAttributesBase(PSA_KEY_TYPE_DERIVE, PSA_ALG_HKDF(PSA_ALG_SHA_256), PSA_KEY_USAGE_DERIVE, 0 ) {}
80
69
};
81
70
71
+ void SetKeyId (Symmetric128BitsKeyHandle & key, psa_key_id_t newKeyId)
72
+ {
73
+ auto & KeyId = key.AsMutable <psa_key_id_t >();
74
+
75
+ KeyId = newKeyId;
76
+ }
82
77
} // namespace
83
78
84
79
CHIP_ERROR PSASessionKeystore::CreateKey (const Symmetric128BitsKeyByteArray & keyMaterial, Aes128KeyHandle & key)
@@ -190,66 +185,33 @@ void PSASessionKeystore::DestroyKey(HkdfKeyHandle & key)
190
185
}
191
186
192
187
#if CHIP_CONFIG_ENABLE_ICD_CIP
193
- CHIP_ERROR PSASessionKeystore::PersistICDKey (Aes128KeyHandle & key)
188
+ CHIP_ERROR PSASessionKeystore::PersistICDKey (Symmetric128BitsKeyHandle & key)
194
189
{
195
190
CHIP_ERROR err;
196
- AesKeyAttributes attrs;
197
- psa_key_id_t previousKeyId = key.As <psa_key_id_t >();
198
- psa_key_attributes_t previousKeyAttrs;
199
-
200
- psa_get_key_attributes (previousKeyId, &previousKeyAttrs);
201
- // Exit early if key is already persistent
202
- if (psa_get_key_lifetime (&previousKeyAttrs) == PSA_KEY_LIFETIME_PERSISTENT)
203
- {
204
- ExitNow (err = CHIP_NO_ERROR);
205
- }
206
-
207
- SuccessOrExit (err = Crypto::FindFreeKeySlotInRange (key.AsMutable <psa_key_id_t >(), to_underlying (KeyIdBase::ICDAesKeyRangeStart),
208
- kMaxICDClientKeys ));
209
-
210
- SuccessOrExit (err = attrs.SetKeyPersistence (key.As <psa_key_id_t >()));
211
- VerifyOrExit (psa_copy_key (previousKeyId, &attrs.Get (), &key.AsMutable <psa_key_id_t >()) == PSA_SUCCESS,
212
- err = CHIP_ERROR_INTERNAL);
191
+ psa_key_id_t newKeyId = PSA_KEY_ID_NULL;
192
+ psa_key_attributes_t attrs;
213
193
214
- psa_destroy_key (previousKeyId );
194
+ psa_get_key_attributes (key. As < psa_key_id_t >(), &attrs );
215
195
216
- exit :
217
- if (err != CHIP_NO_ERROR)
218
- {
219
- psa_destroy_key (previousKeyId);
220
- psa_destroy_key (key.As <psa_key_id_t >());
221
- }
222
-
223
- return err;
224
- }
225
-
226
- CHIP_ERROR PSASessionKeystore::PersistICDKey (Hmac128KeyHandle & key)
227
- {
228
- CHIP_ERROR err;
229
- HmacKeyAttributes attrs;
230
- psa_key_id_t previousKeyId = key.As <psa_key_id_t >();
231
- psa_key_attributes_t previousKeyAttrs;
232
-
233
- psa_get_key_attributes (previousKeyId, &previousKeyAttrs);
234
196
// Exit early if key is already persistent
235
- if (psa_get_key_lifetime (&previousKeyAttrs ) == PSA_KEY_LIFETIME_PERSISTENT)
197
+ if (psa_get_key_lifetime (&attrs ) == PSA_KEY_LIFETIME_PERSISTENT)
236
198
{
237
- ExitNow (err = CHIP_NO_ERROR);
199
+ psa_reset_key_attributes (&attrs);
200
+ return CHIP_NO_ERROR;
238
201
}
239
202
240
- SuccessOrExit (err = Crypto::FindFreeKeySlotInRange (key.AsMutable <psa_key_id_t >(),
241
- to_underlying (KeyIdBase::ICDHmacKeyRangeStart), kMaxICDClientKeys ));
242
- SuccessOrExit (err = attrs.SetKeyPersistence (key.As <psa_key_id_t >()));
243
- VerifyOrExit (psa_copy_key (previousKeyId, &attrs.Get (), &key.AsMutable <psa_key_id_t >()) == PSA_SUCCESS,
244
- err = CHIP_ERROR_INTERNAL);
245
-
246
- psa_destroy_key (previousKeyId);
203
+ SuccessOrExit (err = Crypto::FindFreeKeySlotInRange (newKeyId, to_underlying (KeyIdBase::ICDKeyRangeStart), kMaxICDClientKeys ));
204
+ psa_set_key_lifetime (&attrs, PSA_KEY_LIFETIME_PERSISTENT);
205
+ psa_set_key_id (&attrs, newKeyId);
206
+ VerifyOrExit (psa_copy_key (key.As <psa_key_id_t >(), &attrs, &newKeyId) == PSA_SUCCESS, err = CHIP_ERROR_INTERNAL);
247
207
248
208
exit :
249
- if (err != CHIP_NO_ERROR)
209
+ DestroyKey (key);
210
+ psa_reset_key_attributes (&attrs);
211
+
212
+ if (err == CHIP_NO_ERROR)
250
213
{
251
- psa_destroy_key (previousKeyId);
252
- psa_destroy_key (key.As <psa_key_id_t >());
214
+ SetKeyId (key, newKeyId);
253
215
}
254
216
255
217
return err;
0 commit comments