@@ -24,62 +24,65 @@ 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_id_t keyId, psa_key_type_t type, psa_algorithm_t algorithm,
31
+ psa_key_usage_t usageFlags, size_t bits)
31
32
{
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 );
33
+ psa_set_key_type (&mAttrs , type);
34
+ psa_set_key_algorithm (&mAttrs , algorithm);
35
+ psa_set_key_usage_flags (&mAttrs , usageFlags);
36
+ psa_set_key_bits (&mAttrs , bits);
37
+ if (to_underlying (KeyIdBase::Maximum) >= keyId && keyId >= to_underlying (KeyIdBase::Minimum))
38
+ {
39
+ psa_set_key_lifetime (&mAttrs , PSA_KEY_LIFETIME_PERSISTENT);
40
+ psa_set_key_id (&mAttrs , keyId);
41
+ }
38
42
}
39
43
40
- ~AesKeyAttributes () { psa_reset_key_attributes (&mAttrs ); }
44
+ ~keyAttributesBase () { psa_reset_key_attributes (&mAttrs ); }
41
45
42
46
const psa_key_attributes_t & Get () { return mAttrs ; }
43
47
44
48
private:
45
49
psa_key_attributes_t mAttrs = PSA_KEY_ATTRIBUTES_INIT;
46
50
};
47
51
48
- class HmacKeyAttributes
52
+ class AesKeyAttributes : public keyAttributesBase
49
53
{
50
54
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;
55
+ AesKeyAttributes (psa_key_id_t keyId = 0 )
56
+ : keyAttributesBase(keyId,
57
+ PSA_KEY_TYPE_AES,
58
+ PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG (PSA_ALG_CCM, 8 ),
59
+ PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
60
+ CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES * 8)
61
+ {}
65
62
};
66
63
67
- class HkdfKeyAttributes
64
+ class HmacKeyAttributes : public keyAttributesBase
68
65
{
69
66
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 ; }
67
+ HmacKeyAttributes (psa_key_id_t keyId = 0 )
68
+ : keyAttributesBase(keyId,
69
+ PSA_KEY_TYPE_HMAC,
70
+ PSA_ALG_HMAC (PSA_ALG_SHA_256),
71
+ PSA_KEY_USAGE_SIGN_MESSAGE,
72
+ CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES * 8)
73
+ {}
74
+ };
80
75
81
- private:
82
- psa_key_attributes_t mAttrs = PSA_KEY_ATTRIBUTES_INIT;
76
+ class HkdfKeyAttributes : public keyAttributesBase
77
+ {
78
+ public:
79
+ HkdfKeyAttributes (psa_key_id_t keyId = 0 )
80
+ : keyAttributesBase(keyId,
81
+ PSA_KEY_TYPE_DERIVE,
82
+ PSA_ALG_HKDF (PSA_ALG_SHA_256),
83
+ PSA_KEY_USAGE_DERIVE,
84
+ 0)
85
+ {}
83
86
};
84
87
85
88
} // namespace
@@ -89,7 +92,7 @@ CHIP_ERROR PSASessionKeystore::CreateKey(const Symmetric128BitsKeyByteArray & ke
89
92
// Destroy the old key if already allocated
90
93
psa_destroy_key (key.As <psa_key_id_t >());
91
94
92
- AesKeyAttributes attrs;
95
+ AesKeyAttributes attrs (key. As < psa_key_id_t >()) ;
93
96
psa_status_t status =
94
97
psa_import_key (&attrs.Get (), keyMaterial, sizeof (Symmetric128BitsKeyByteArray), &key.AsMutable <psa_key_id_t >());
95
98
LogPsaError (status);
@@ -103,7 +106,7 @@ CHIP_ERROR PSASessionKeystore::CreateKey(const Symmetric128BitsKeyByteArray & ke
103
106
// Destroy the old key if already allocated
104
107
psa_destroy_key (key.As <psa_key_id_t >());
105
108
106
- HmacKeyAttributes attrs;
109
+ HmacKeyAttributes attrs (key. As < psa_key_id_t >()) ;
107
110
psa_status_t status =
108
111
psa_import_key (&attrs.Get (), keyMaterial, sizeof (Symmetric128BitsKeyByteArray), &key.AsMutable <psa_key_id_t >());
109
112
LogPsaError (status);
0 commit comments