forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPeerMessageCounter.h
403 lines (361 loc) · 12 KB
/
PeerMessageCounter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file defines the CHIP message counters of remote nodes.
*
*/
#pragma once
#include <array>
#include <bitset>
#include <lib/support/Span.h>
namespace chip {
namespace Transport {
class PeerMessageCounter
{
public:
static constexpr size_t kChallengeSize = 8;
static constexpr uint32_t kInitialSyncValue = 0;
PeerMessageCounter() : mStatus(Status::NotSynced) {}
~PeerMessageCounter() { Reset(); }
void Reset()
{
switch (mStatus)
{
case Status::NotSynced:
break;
case Status::SyncInProcess:
mSyncInProcess.~SyncInProcess();
break;
case Status::Synced:
mSynced.~Synced();
break;
}
mStatus = Status::NotSynced;
}
bool IsSynchronizing() { return mStatus == Status::SyncInProcess; }
bool IsSynchronized() { return mStatus == Status::Synced; }
void SyncStarting(FixedByteSpan<kChallengeSize> challenge)
{
VerifyOrDie(mStatus == Status::NotSynced);
mStatus = Status::SyncInProcess;
new (&mSyncInProcess) SyncInProcess();
::memcpy(mSyncInProcess.mChallenge.data(), challenge.data(), kChallengeSize);
}
void SyncFailed() { Reset(); }
CHIP_ERROR VerifyChallenge(uint32_t counter, FixedByteSpan<kChallengeSize> challenge)
{
if (mStatus != Status::SyncInProcess)
{
return CHIP_ERROR_INCORRECT_STATE;
}
if (::memcmp(mSyncInProcess.mChallenge.data(), challenge.data(), kChallengeSize) != 0)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
mSyncInProcess.~SyncInProcess();
mStatus = Status::Synced;
new (&mSynced) Synced();
mSynced.mMaxCounter = counter;
mSynced.mWindow.reset(); // reset all bits, accept all packets in the window
return CHIP_NO_ERROR;
}
/**
* @brief Implementation of spec 4.5.4.2
*
* For encrypted messages of Group Session Type, any arriving message with a counter in the range
* [(max_message_counter + 1) to (max_message_counter + 2^31 - 1)] (modulo 2^32) SHALL be considered
* new, and cause the max_message_counter value to be updated. Messages with counters from
* [(max_message_counter - 2^31) to (max_message_counter - MSG_COUNTER_WINDOW_SIZE - 1)] (modulo 2^
* 32) SHALL be considered duplicate. Message counters within the range of the bitmap SHALL be
* considered duplicate if the corresponding bit offset is set to true.
*
*/
CHIP_ERROR VerifyGroup(uint32_t counter) const
{
if (mStatus != Status::Synced)
{
return CHIP_ERROR_INCORRECT_STATE;
}
Position pos = ClassifyWithRollover(counter);
return VerifyPositionEncrypted(pos, counter);
}
CHIP_ERROR VerifyOrTrustFirstGroup(uint32_t counter)
{
switch (mStatus)
{
case Status::NotSynced: {
// Trust and set the counter when not synced
SetCounter(counter);
return CHIP_NO_ERROR;
}
case Status::Synced: {
return VerifyGroup(counter);
}
default:
VerifyOrDie(false);
return CHIP_ERROR_INTERNAL;
}
}
/**
* @brief
* With the group counter verified and the packet MIC also verified by the secure key, we can trust the packet and adjust
* counter states.
*
* @pre counter has been verified via VerifyGroup or VerifyOrTrustFirstGroup
*/
void CommitGroup(uint32_t counter) { CommitWithRollover(counter); }
CHIP_ERROR VerifyEncryptedUnicast(uint32_t counter) const
{
if (mStatus != Status::Synced)
{
return CHIP_ERROR_INCORRECT_STATE;
}
Position pos = ClassifyWithoutRollover(counter);
return VerifyPositionEncrypted(pos, counter);
}
/**
* @brief
* With the counter verified and the packet MIC also verified by the secure key, we can trust the packet and adjust
* counter states.
*
* @pre counter has been verified via VerifyEncryptedUnicast
*/
void CommitEncryptedUnicast(uint32_t counter) { CommitWithoutRollover(counter); }
CHIP_ERROR VerifyUnencrypted(uint32_t counter)
{
switch (mStatus)
{
case Status::NotSynced: {
// Trust and set the counter when not synced
SetCounter(counter);
return CHIP_NO_ERROR;
}
case Status::Synced: {
Position pos = ClassifyWithRollover(counter);
return VerifyPositionUnencrypted(pos, counter);
}
default: {
VerifyOrDie(false);
return CHIP_ERROR_INTERNAL;
}
}
}
/**
* @brief
* With the unencrypted counter verified we can trust the packet and adjust
* counter states.
*
* @pre counter has been verified via VerifyUnencrypted
*/
void CommitUnencrypted(uint32_t counter) { CommitWithRollover(counter); }
void SetCounter(uint32_t value)
{
Reset();
mStatus = Status::Synced;
new (&mSynced) Synced();
mSynced.mMaxCounter = value;
mSynced.mWindow.reset();
}
uint32_t GetCounter() const { return mSynced.mMaxCounter; }
private:
// Counter position indicator with respect to our current
// mSynced.mMaxCounter.
enum class Position
{
BeforeWindow,
InWindow,
MaxCounter,
FutureCounter,
};
// Classify an incoming counter value's position. Must be used only if
// mStatus is Status::Synced.
Position ClassifyWithoutRollover(uint32_t counter) const
{
if (counter > mSynced.mMaxCounter)
{
return Position::FutureCounter;
}
return ClassifyNonFutureCounter(counter);
}
/**
* Classify an incoming counter value's position for the cases when counters
* are allowed to roll over. Must be used only if mStatus is
* Status::Synced.
*
* This can be used as the basis for implementing section 4.5.4.2 in the
* spec:
*
* For encrypted messages of Group Session Type, any arriving message with a counter in the range
* [(max_message_counter + 1) to (max_message_counter + 2^31 - 1)] (modulo 2^32) SHALL be considered
* new, and cause the max_message_counter value to be updated. Messages with counters from
* [(max_message_counter - 2^31) to (max_message_counter - MSG_COUNTER_WINDOW_SIZE - 1)] (modulo 2^
* 32) SHALL be considered duplicate. Message counters within the range of the bitmap SHALL be
* considered duplicate if the corresponding bit offset is set to true.
*/
Position ClassifyWithRollover(uint32_t counter) const
{
uint32_t counterIncrease = counter - mSynced.mMaxCounter;
constexpr uint32_t futureCounterWindow = (static_cast<uint32_t>(1 << 31)) - 1;
if (counterIncrease >= 1 && counterIncrease <= futureCounterWindow)
{
return Position::FutureCounter;
}
return ClassifyNonFutureCounter(counter);
}
/**
* Classify a counter that's known to not be future counter. This works
* identically whether we are doing rollover or not.
*/
Position ClassifyNonFutureCounter(uint32_t counter) const
{
if (counter == mSynced.mMaxCounter)
{
return Position::MaxCounter;
}
uint32_t offset = mSynced.mMaxCounter - counter;
if (offset <= CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
{
return Position::InWindow;
}
return Position::BeforeWindow;
}
/**
* Given an encrypted (group or unicast) counter position and the counter
* value, verify whether we should accept it.
*/
CHIP_ERROR VerifyPositionEncrypted(Position position, uint32_t counter) const
{
switch (position)
{
case Position::FutureCounter:
return CHIP_NO_ERROR;
case Position::InWindow: {
uint32_t offset = mSynced.mMaxCounter - counter;
if (mSynced.mWindow.test(offset - 1))
{
return CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED;
}
return CHIP_NO_ERROR;
}
default: {
// Equal to max counter, or before window.
return CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED;
}
}
}
/**
* Given an unencrypted counter position and value, verify whether we should
* accept it.
*/
CHIP_ERROR VerifyPositionUnencrypted(Position position, uint32_t counter) const
{
switch (position)
{
case Position::MaxCounter:
return CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED;
case Position::InWindow: {
uint32_t offset = mSynced.mMaxCounter - counter;
if (mSynced.mWindow.test(offset - 1))
{
return CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED;
}
return CHIP_NO_ERROR;
}
default: {
// Future counter or before window; all of these are accepted. The
// before-window case is accepted because the peer may have reset
// and is using a new randomized initial value.
return CHIP_NO_ERROR;
}
}
}
void CommitWithRollover(uint32_t counter)
{
Position pos = ClassifyWithRollover(counter);
CommitWithPosition(pos, counter);
}
void CommitWithoutRollover(uint32_t counter)
{
Position pos = ClassifyWithoutRollover(counter);
CommitWithPosition(pos, counter);
}
/**
* Commit a counter value that is known to be at the given position with
* respect to our max counter.
*/
void CommitWithPosition(Position position, uint32_t counter)
{
switch (position)
{
case Position::InWindow: {
uint32_t offset = mSynced.mMaxCounter - counter;
mSynced.mWindow.set(offset - 1);
break;
}
case Position::MaxCounter: {
// Nothing to do
break;
}
default: {
// Since we are committing, this becomes a new max-counter value.
uint32_t shift = counter - mSynced.mMaxCounter;
mSynced.mMaxCounter = counter;
if (shift > CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE)
{
mSynced.mWindow.reset();
}
else
{
mSynced.mWindow <<= shift;
mSynced.mWindow.set(shift - 1);
}
break;
}
}
}
enum class Status
{
NotSynced, // No state associated
SyncInProcess, // mSyncInProcess will be active
Synced, // mSynced will be active
} mStatus;
struct SyncInProcess
{
std::array<uint8_t, kChallengeSize> mChallenge;
};
struct Synced
{
/*
* Past <-- --> Future
* MaxCounter - 1
* |
* v
* | <-- mWindow -->|
* |[n]| ... |[0]|
*/
uint32_t mMaxCounter = 0; // The most recent counter we have seen
std::bitset<CHIP_CONFIG_MESSAGE_COUNTER_WINDOW_SIZE> mWindow;
};
// We should use std::variant here when migrated to C++17
union
{
SyncInProcess mSyncInProcess;
Synced mSynced;
};
};
} // namespace Transport
} // namespace chip