-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.ino
309 lines (252 loc) · 9.69 KB
/
session.ino
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
#include "session.hpp"
#include "detail/error.hpp"
using namespace nfc;
// Hardcoded MFRC522 handler, because the program only deals with one reader
static MFRC522 mfrc522{10, 9};
// Indicates if a PICC is in ACTIVE state
static bool is_any_picc_active = false;
// Indicates if the PCD is currently authenticated to a PICC
static bool is_authenticated = false;
// Sector to which the PCD is authenticated
static uint8_t authenticated_to_trailing_block_index;
// Type of the key the PCD is currently authenticated with
static KeyType authenticated_key_type;
// Terminate the current session
// It should only be called when an unrecoverable error has occured.
static void terminate_session() {
is_any_picc_active = false;
is_authenticated = false;
mfrc522.PCD_StopCrypto1();
}
// Check if the PCD is already authenticated to the given sector of the handled PICC
static bool is_authenticated_to(uint8_t trailing_block_index, nfc::KeyType key_type) {
return is_authenticated && authenticated_to_trailing_block_index == trailing_block_index &&
key_type == authenticated_key_type;
}
// Authenticate to an ACTIVE PICC
static nfc::Status authenticate(uint8_t trailing_block_index, const uint8_t *key, nfc::KeyType key_type) {
MFRC522::MIFARE_Key mf_key;
memcpy(mf_key.keyByte, key, sizeof mf_key.keyByte);
auto status = mfrc522.PCD_Authenticate(key_type == KeyType::KEY_A ? MFRC522::PICC_CMD_MF_AUTH_KEY_A
: MFRC522::PICC_CMD_MF_AUTH_KEY_B,
trailing_block_index,
&mf_key,
&mfrc522.uid);
switch (status) {
case MFRC522::STATUS_OK:
is_authenticated = true;
authenticated_to_trailing_block_index = trailing_block_index;
authenticated_key_type = key_type;
return Status::OK;
case MFRC522::STATUS_TIMEOUT:
is_authenticated = false;
return Status::BAD_KEY;
default:
terminate_session();
return Status::FATAL;
}
}
// Attempt to recover from an error
// If it fails, the session is terminated
static nfc::Status recover_session() {
is_any_picc_active = false;
is_authenticated = false;
mfrc522.PCD_Reset();
if (select() != Status::OK) {
terminate_session();
return Status::FATAL;
}
return Status::OK;
}
// Initialize the remote MFRC522 chip
void nfc::init_mfrc522() { mfrc522.PCD_Init(); }
// Select a PICC and make it available to read / write operation
Status nfc::select() {
MFRC522::MIFARE_Key mf_key;
// Halting the ACTIVE PICC
if (is_any_picc_active) {
ASSERT(mfrc522.PICC_HaltA() != MFRC522::STATUS_ERROR, Status::HALT_DENIED);
terminate_session();
}
// Inviting idling PICCs
ASSERT(mfrc522.PICC_IsNewCardPresent(), Status::NO_CARD_DETECTED);
// Try to handle a PICC in READY state, if any available
ASSERT(mfrc522.PICC_ReadCardSerial(), Status::NO_UID_RECEIVED);
is_any_picc_active = true;
return Status::OK;
}
// Get the UID of the ACTIVE PICC
Optional<uint8_t[uid_size]> nfc::get_uid() {
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
return mfrc522.uid.uidByte;
}
// Read the content of a block on the PICC
Optional<uint8_t[block_size + crc_size]> nfc::read(uint8_t block_index, const uint8_t *key, KeyType key_type) {
uint8_t buf[block_size + crc_size];
uint8_t size = sizeof buf;
uint8_t trailing_block_index = block_index - (block_index % blocks_per_sector_nb) + 3;
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
if (!is_authenticated_to(trailing_block_index, key_type))
PROPAGATE(authenticate(trailing_block_index, key, key_type));
switch (mfrc522.MIFARE_Read(block_index, buf, &size)) {
case MFRC522::STATUS_OK:
return buf;
case MFRC522::STATUS_MIFARE_NACK:
PROPAGATE(recover_session());
return Status::ACCESS_DENIED;
default:
terminate_session();
return Status::FATAL;
}
}
// Write a block of data to the PICC
Status nfc::write(uint8_t block_index, const uint8_t *key, KeyType key_type, const uint8_t *data) {
uint8_t buf[block_size];
uint8_t trailing_block_index = block_index - (block_index % blocks_per_sector_nb) + 3;
memcpy(buf, data, sizeof buf);
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
if (!is_authenticated_to(trailing_block_index, key_type))
PROPAGATE(authenticate(trailing_block_index, key, key_type));
switch (mfrc522.MIFARE_Write(block_index, buf, block_size)) {
case MFRC522::STATUS_OK:
return Status::OK;
case MFRC522::STATUS_MIFARE_NACK:
PROPAGATE(recover_session());
return Status::ACCESS_DENIED;
default:
terminate_session();
return Status::FATAL;
}
}
// Read the content of a value block
Optional<int32_t> nfc::read_value(uint8_t block_index, const uint8_t *key, KeyType key_type) {
int32_t value;
uint8_t trailing_block_index = block_index - (block_index % blocks_per_sector_nb) + 3;
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
if (!is_authenticated_to(trailing_block_index, key_type))
PROPAGATE(authenticate(trailing_block_index, key, key_type));
switch (mfrc522.MIFARE_GetValue(block_index, &value)) {
case MFRC522::STATUS_OK:
return value;
case MFRC522::STATUS_MIFARE_NACK:
PROPAGATE(recover_session());
return Status::ACCESS_DENIED;
default:
terminate_session();
return Status::FATAL;
}
}
// Format a block as a value block
Status nfc::write_value(uint8_t block_index, const uint8_t *key, KeyType key_type, int32_t value) {
uint8_t trailing_block_index = block_index - (block_index % blocks_per_sector_nb) + 3;
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
if (!is_authenticated_to(trailing_block_index, key_type))
PROPAGATE(authenticate(trailing_block_index, key, key_type));
switch (mfrc522.MIFARE_SetValue(block_index, value)) {
case MFRC522::STATUS_OK:
return Status::OK;
case MFRC522::STATUS_MIFARE_NACK:
PROPAGATE(recover_session());
return Status::ACCESS_DENIED;
default:
terminate_session();
return Status::FATAL;
}
return Status::OK;
}
// Increment a value block
Status nfc::increment(uint8_t block_index, const uint8_t *key, KeyType key_type, int32_t value) {
uint8_t trailing_block_index = block_index - (block_index % blocks_per_sector_nb) + 3;
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
if (!is_authenticated_to(trailing_block_index, key_type))
PROPAGATE(authenticate(trailing_block_index, key, key_type));
switch (mfrc522.MIFARE_Increment(block_index, value)) {
case MFRC522::STATUS_OK:
break;
case MFRC522::STATUS_MIFARE_NACK:
PROPAGATE(recover_session());
return Status::ACCESS_DENIED;
default:
terminate_session();
return Status::FATAL;
}
switch (mfrc522.MIFARE_Transfer(block_index)) {
case MFRC522::STATUS_OK:
return Status::OK;
case MFRC522::STATUS_MIFARE_NACK:
PROPAGATE(recover_session());
return Status::ACCESS_DENIED;
default:
terminate_session();
return Status::FATAL;
}
return Status::OK;
}
// Decrement a value block
Status nfc::decrement(uint8_t block_index, const uint8_t *key, KeyType key_type, int32_t value) {
uint8_t trailing_block_index = block_index - (block_index % blocks_per_sector_nb) + 3;
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
if (!is_authenticated_to(trailing_block_index, key_type))
PROPAGATE(authenticate(trailing_block_index, key, key_type));
switch (mfrc522.MIFARE_Decrement(block_index, value)) {
case MFRC522::STATUS_OK:
break;
case MFRC522::STATUS_MIFARE_NACK:
PROPAGATE(recover_session());
return Status::ACCESS_DENIED;
default:
terminate_session();
return Status::FATAL;
}
switch (mfrc522.MIFARE_Transfer(block_index)) {
case MFRC522::STATUS_OK:
return Status::OK;
case MFRC522::STATUS_MIFARE_NACK:
PROPAGATE(recover_session());
return Status::ACCESS_DENIED;
default:
terminate_session();
return Status::FATAL;
}
return Status::OK;
}
// Set access right of a sector
// Trailing block rights can't be modified.
Status nfc::set_access(uint8_t sector_index, const uint8_t *key, KeyType key_type, const Access *access_bytes) {
uint8_t buf[block_size + crc_size];
uint8_t size = sizeof buf;
uint8_t trailing_block_index = blocks_per_sector_nb * sector_index + 3;
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
memset(buf, 0xff, key_size);
memset(buf + block_size - key_size, 0xff, key_size);
mfrc522.MIFARE_SetAccessBits(buf + key_size,
static_cast<uint8_t>(access_bytes[0]),
static_cast<uint8_t>(access_bytes[1]),
static_cast<uint8_t>(access_bytes[2]),
0b011);
if (mfrc522.MIFARE_Write(trailing_block_index, buf, block_size) != MFRC522::STATUS_OK) {
terminate_session();
return Status::FATAL;
}
return Status::OK;
}
// Seal the sector current configuration and set up keys
Status nfc::seal_sector(
uint8_t sector_index, uint8_t *key, KeyType key_type, uint8_t *key_a, uint8_t *key_b, const Access *access_bytes) {
uint8_t buf[block_size + crc_size];
uint8_t size = sizeof buf;
uint8_t trailing_block_index = blocks_per_sector_nb * sector_index + 3;
ASSERT(is_any_picc_active, Status::NO_ACTIVE_PICC);
memcpy(buf, key_a, key_size);
memcpy(buf + key_size + 4, key_b, key_size);
mfrc522.MIFARE_SetAccessBits(buf + key_size,
static_cast<uint8_t>(access_bytes[0]),
static_cast<uint8_t>(access_bytes[1]),
static_cast<uint8_t>(access_bytes[2]),
0b111);
if (mfrc522.MIFARE_Write(trailing_block_index, buf, block_size) != MFRC522::STATUS_OK) {
terminate_session();
return Status::FATAL;
}
return Status::OK;
}