@@ -24,70 +24,62 @@ namespace Crypto {
24
24
25
25
namespace {
26
26
27
- class AesKeyAttributes
27
+ class KeyAttributesBase
28
28
{
29
29
public:
30
- AesKeyAttributes ( )
30
+ KeyAttributesBase ( psa_key_type_t type, psa_algorithm_t algorithm, psa_key_usage_t usageFlags, size_t bits )
31
31
{
32
- constexpr psa_algorithm_t kAlgorithm = PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG (PSA_ALG_CCM, 8 );
33
-
34
- psa_set_key_type (&mAttrs , PSA_KEY_TYPE_AES);
35
- psa_set_key_algorithm (&mAttrs , kAlgorithm );
36
- psa_set_key_usage_flags (&mAttrs , PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
37
- psa_set_key_bits (&mAttrs , CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES * 8 );
32
+ psa_set_key_type (&mAttrs , type);
33
+ psa_set_key_algorithm (&mAttrs , algorithm);
34
+ psa_set_key_usage_flags (&mAttrs , usageFlags);
35
+ psa_set_key_bits (&mAttrs , bits);
38
36
}
39
37
40
- ~AesKeyAttributes () { psa_reset_key_attributes (&mAttrs ); }
38
+ ~KeyAttributesBase () { psa_reset_key_attributes (&mAttrs ); }
41
39
42
40
const psa_key_attributes_t & Get () { return mAttrs ; }
43
41
44
42
private:
45
43
psa_key_attributes_t mAttrs = PSA_KEY_ATTRIBUTES_INIT;
46
44
};
47
45
48
- class HmacKeyAttributes
46
+ class AesKeyAttributes : public KeyAttributesBase
49
47
{
50
48
public:
51
- HmacKeyAttributes ()
52
- {
53
- psa_set_key_type (&mAttrs , PSA_KEY_TYPE_HMAC);
54
- psa_set_key_algorithm (&mAttrs , PSA_ALG_HMAC (PSA_ALG_SHA_256));
55
- psa_set_key_usage_flags (&mAttrs , PSA_KEY_USAGE_SIGN_MESSAGE);
56
- psa_set_key_bits (&mAttrs , CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES * 8 );
57
- }
58
-
59
- ~HmacKeyAttributes () { psa_reset_key_attributes (&mAttrs ); }
60
-
61
- const psa_key_attributes_t & Get () { return mAttrs ; }
62
-
63
- private:
64
- psa_key_attributes_t mAttrs = PSA_KEY_ATTRIBUTES_INIT;
49
+ AesKeyAttributes () :
50
+ KeyAttributesBase (PSA_KEY_TYPE_AES, PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(PSA_ALG_CCM, 8 ),
51
+ PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_COPY,
52
+ CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES * 8 )
53
+ {}
65
54
};
66
55
67
- class HkdfKeyAttributes
56
+ class HmacKeyAttributes : public KeyAttributesBase
68
57
{
69
58
public:
70
- HkdfKeyAttributes ()
71
- {
72
- psa_set_key_type (&mAttrs , PSA_KEY_TYPE_DERIVE);
73
- psa_set_key_algorithm (&mAttrs , PSA_ALG_HKDF (PSA_ALG_SHA_256));
74
- psa_set_key_usage_flags (&mAttrs , PSA_KEY_USAGE_DERIVE);
75
- }
76
-
77
- ~HkdfKeyAttributes () { psa_reset_key_attributes (&mAttrs ); }
78
-
79
- const psa_key_attributes_t & Get () { return mAttrs ; }
59
+ HmacKeyAttributes () :
60
+ KeyAttributesBase (PSA_KEY_TYPE_HMAC, PSA_ALG_HMAC(PSA_ALG_SHA_256), PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_COPY,
61
+ CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES * 8 )
62
+ {}
63
+ };
80
64
81
- private:
82
- psa_key_attributes_t mAttrs = PSA_KEY_ATTRIBUTES_INIT;
65
+ class HkdfKeyAttributes : public KeyAttributesBase
66
+ {
67
+ public:
68
+ HkdfKeyAttributes () : KeyAttributesBase(PSA_KEY_TYPE_DERIVE, PSA_ALG_HKDF(PSA_ALG_SHA_256), PSA_KEY_USAGE_DERIVE, 0 ) {}
83
69
};
84
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
+ }
85
77
} // namespace
86
78
87
79
CHIP_ERROR PSASessionKeystore::CreateKey (const Symmetric128BitsKeyByteArray & keyMaterial, Aes128KeyHandle & key)
88
80
{
89
81
// Destroy the old key if already allocated
90
- psa_destroy_key (key. As < psa_key_id_t >() );
82
+ DestroyKey (key);
91
83
92
84
AesKeyAttributes attrs;
93
85
psa_status_t status =
@@ -101,7 +93,7 @@ CHIP_ERROR PSASessionKeystore::CreateKey(const Symmetric128BitsKeyByteArray & ke
101
93
CHIP_ERROR PSASessionKeystore::CreateKey (const Symmetric128BitsKeyByteArray & keyMaterial, Hmac128KeyHandle & key)
102
94
{
103
95
// Destroy the old key if already allocated
104
- psa_destroy_key (key. As < psa_key_id_t >() );
96
+ DestroyKey (key);
105
97
106
98
HmacKeyAttributes attrs;
107
99
psa_status_t status =
@@ -192,5 +184,39 @@ void PSASessionKeystore::DestroyKey(HkdfKeyHandle & key)
192
184
keyId = PSA_KEY_ID_NULL;
193
185
}
194
186
187
+ #if CHIP_CONFIG_ENABLE_ICD_CIP
188
+ CHIP_ERROR PSASessionKeystore::PersistICDKey (Symmetric128BitsKeyHandle & key)
189
+ {
190
+ CHIP_ERROR err;
191
+ psa_key_id_t newKeyId = PSA_KEY_ID_NULL;
192
+ psa_key_attributes_t attrs;
193
+
194
+ psa_get_key_attributes (key.As <psa_key_id_t >(), &attrs);
195
+
196
+ // Exit early if key is already persistent
197
+ if (psa_get_key_lifetime (&attrs) == PSA_KEY_LIFETIME_PERSISTENT)
198
+ {
199
+ psa_reset_key_attributes (&attrs);
200
+ return CHIP_NO_ERROR;
201
+ }
202
+
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);
207
+
208
+ exit :
209
+ DestroyKey (key);
210
+ psa_reset_key_attributes (&attrs);
211
+
212
+ if (err == CHIP_NO_ERROR)
213
+ {
214
+ SetKeyId (key, newKeyId);
215
+ }
216
+
217
+ return err;
218
+ }
219
+ #endif
220
+
195
221
} // namespace Crypto
196
222
} // namespace chip
0 commit comments