Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / K32W / GroupKeyStoreImpl.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2020 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *          Provides an implementation of the Chip Group Key Store interface
23  *          for platforms based on the NXP SDK.
24  */
25
26 #include <core/CHIPKeyIds.h>
27 #include <platform/K32W/K32WConfig.h>
28 #include <platform/internal/CHIPDeviceLayerInternal.h>
29
30 namespace chip {
31 namespace DeviceLayer {
32 namespace Internal {
33
34 class ChipGroupKey
35 {
36 public:
37     enum
38     {
39         MaxKeySize = 36
40     };
41     uint32_t KeyId;          /**< The key ID. */
42     uint8_t KeyLen;          /**< The key length. */
43     uint8_t Key[MaxKeySize]; /**< The secret key material. */
44     union
45     {
46         uint32_t StartTime; /**< The epoch key start time. */
47         uint32_t GlobalId;  /**< The application group key global ID. */
48     };
49 };
50
51 /**
52  * An implementation of the Chip GroupKeyStoreBase API for platforms based
53  * on the NXP SDK.
54  */
55 class GroupKeyStoreImpl final : public ::chip::Profiles::Security::AppKeys::GroupKeyStoreBase, private K32WConfig
56 {
57     using ChipGroupKey = ::chip::Profiles::Security::AppKeys::ChipGroupKey;
58
59 public:
60     CHIP_ERROR Init();
61
62     CHIP_ERROR RetrieveGroupKey(uint32_t keyId, ChipGroupKey & key) override;
63     CHIP_ERROR StoreGroupKey(const ChipGroupKey & key) override;
64     CHIP_ERROR DeleteGroupKey(uint32_t keyId) override;
65     CHIP_ERROR DeleteGroupKeysOfAType(uint32_t keyType) override;
66     CHIP_ERROR EnumerateGroupKeys(uint32_t keyType, uint32_t * keyIds, uint8_t keyIdsArraySize, uint8_t & keyCount) override;
67     CHIP_ERROR Clear(void) override;
68     CHIP_ERROR RetrieveLastUsedEpochKeyId(void) override;
69     CHIP_ERROR StoreLastUsedEpochKeyId(void) override;
70
71 private:
72     static constexpr size_t kFixedEncodedKeySize = 4U + // key id
73         4U +                                            // start time / global id
74         1U;                                             // key data length
75
76     static constexpr size_t kMaxEncodedKeySize = kFixedEncodedKeySize + ChipGroupKey::MaxKeySize;
77
78     /* Not used
79     static constexpr uint16_t kGroupKeyFileId =     GetFileId(kConfigKey_GroupKey);
80     static constexpr uint16_t kGroupKeyRecordKey =  GetRecordKey(kConfigKey_GroupKey);
81     */
82
83     static CHIP_ERROR EncodeGroupKey(const ChipGroupKey & key, uint8_t * buf, size_t bufSize, size_t & encodedKeyLen);
84     static CHIP_ERROR DecodeGroupKey(const uint8_t * encodedKey, size_t encodedKeyLen, ChipGroupKey & key);
85     static CHIP_ERROR DecodeGroupKeyId(const uint8_t * encodedKey, size_t encodedKeyLen, uint32_t & keyId);
86 };
87
88 } // namespace Internal
89 } // namespace DeviceLayer
90 } // namespace chip