Line data Source code
1 : /*
2 : * Copyright (c) 2024 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 : /**
19 : * @brief The file implements the Matter Check-In counter
20 : *
21 : */
22 : #pragma once
23 :
24 : #include <crypto/RandUtils.h>
25 : #include <lib/core/CHIPError.h>
26 : #include <lib/support/PersistedCounter.h>
27 :
28 : namespace chip {
29 : namespace Protocols {
30 : namespace SecureChannel {
31 :
32 : /**
33 : * @brief
34 : *
35 : */
36 : class CheckInCounter : public PersistedCounter<uint32_t>
37 : {
38 : public:
39 75 : CheckInCounter(){};
40 75 : ~CheckInCounter() override{};
41 :
42 : /**
43 : * @brief Returns the next Check-In counter Value to use
44 : *
45 : * @return uint32_t Check-in Counter value to use
46 : */
47 : inline uint32_t GetNextCheckInCounterValue() { return (mCounterValue + 1); };
48 :
49 : /**
50 : * @brief Advances the current Check-In counter value to invalidate half of the values.
51 : * Function adds UINT32_MAX / 2 to the current counter value. Next time a Check-In message is sent, 1 is added to the
52 : * current value.
53 : *
54 : * @return CHIP_ERROR Any error returned by a write to persisted storage.
55 : */
56 : CHIP_ERROR InvalidateHalfCheckInCounterValues();
57 :
58 : /**
59 : * @brief Invalidates all the Check-In counter values. After this function is called, the new Check-In counter value will be
60 : * equal to current value minus 1 (with rollover) to mimic N -> UINT32_MAX -> N-1. Next time a Check-In message is sent,
61 : * 1 is added to the current value which will be an invalid counter value.
62 : *
63 : * @return CHIP_ERROR Any error returned by a write to persisted storage.
64 : */
65 : CHIP_ERROR InvalidateAllCheckInCounterValues();
66 :
67 : private:
68 : /**
69 : * @brief Get the Initial Counter Value for the Check-In counter
70 : * Initial value is randomized for the Check-In counter
71 : *
72 : * @return uint32_t random value
73 : */
74 0 : inline uint32_t GetInitialCounterValue() override { return chip::Crypto::GetRandU32(); }
75 : };
76 :
77 : } // namespace SecureChannel
78 : } // namespace Protocols
79 : } // namespace chip
|