forked from wirenboard/atecc-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atecc-config.c
390 lines (317 loc) · 9.89 KB
/
atecc-config.c
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
#include "atecc-config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "helpers.h"
#include "basic/atca_basic.h"
#include "atecc_config_zone.h"
#define CONFIG_ZONE_SLOTLOCKED_OFFSET 88
void atecc_dump_config_help(const char *cmdname)
{
eprintf("%s: dump ATECC config in human-readable format\n", cmdname);
eprintf("Usage: %s output.txt|- [config.bin]\n", cmdname);
eprintf("If optional third argument is set, "
"dumps config from binary file.\n");
}
void dump_config(uint8_t config_zone[ATCA_ECC_CONFIG_SIZE])
{
printf("============= Config zone dump: ============= \n");
for (size_t i = 0; i < ATCA_ECC_CONFIG_SIZE; ++i) {
if (i % 4 == 0) {
printf("\n");
}
printf("%03lu: %02X\t\t", i, config_zone[i]);
}
printf("\n");
// printf("============= Some parsed values: ============= \n");
// printf("Counter 0 value: %u [", parse_counter_value(config_zone + CONFIG_ZONE_COUNTER0_OFFSET));
// for (size_t i=0; i< CONFIG_ZONE_COUNTER0_LENGTH; ++i) {
// printf("%02X ", config_zone[CONFIG_ZONE_COUNTER0_OFFSET + i]);
// }
// printf("]\n");
// printf("Counter 1 value: %u [", parse_counter_value(config_zone + CONFIG_ZONE_COUNTER1_OFFSET));
// for (size_t i=0; i< CONFIG_ZONE_COUNTER1_LENGTH; ++i) {
// printf("%02X ", config_zone[CONFIG_ZONE_COUNTER1_OFFSET + i]);
// }
// printf("]\n");
uint16_t locks = config_zone[CONFIG_ZONE_SLOTLOCKED_OFFSET] | config_zone[CONFIG_ZONE_SLOTLOCKED_OFFSET + 1] << 8;
printf("===== Individual locks configuration ==\n");
for (size_t slot = 0; slot < ATCA_KEY_COUNT; ++slot) {
printf(" Slot %02u: %s\n", (unsigned) slot, (locks & (1 << slot)) ? "unlocked" : "LOCKED");
}
printf("\n");
printf("===== Slot configurations============\n");
for (size_t slot = 0; slot < ATCA_KEY_COUNT; ++slot) {
printf("========================= Slot: %lu ================== \n", slot);
dump_slot_config((config_zone[CONFIG_ZONE_SLOT_CONFIG_OFFSET + slot * 2 + 1] << 8) | config_zone[CONFIG_ZONE_SLOT_CONFIG_OFFSET + slot * 2]);
printf("-------------\n");
dump_key_config((config_zone[CONFIG_ZONE_KEY_CONFIG_OFFSET + slot * 2 + 1] << 8) | config_zone[CONFIG_ZONE_KEY_CONFIG_OFFSET + slot * 2]);
}
}
int do_atecc_dump_config(int argc, char **argv)
{
ATCA_STATUS status;
uint8_t config_zone[ATCA_ECC_CONFIG_SIZE];
if (argc != 2 && argc != 3) {
atecc_dump_config_help(argv[0]);
return 1;
}
const char *outputfilename = argv[1];
if (strlen(outputfilename) == 0) {
outputfilename = "-";
}
if (argc == 3) { /* read from binary file */
FILE *input = maybe_fopen(argv[2], "rb");
if (!input) {
perror("open config blob file for reading");
return 1;
}
if (sizeof (config_zone) !=
fread(config_zone, 1, sizeof (config_zone), input)) {
perror("read config blob");
maybe_fclose(input);
return 1;
}
maybe_fclose(input);
} else {
if ((status = atcab_read_config_zone(config_zone)) != ATCA_SUCCESS) {
eprintf("Command atcab_read_config_zone failed with status 0x%x\n",
status);
return 2;
}
}
int saved_stdout = maybe_set_stdout(outputfilename);
if (saved_stdout < 0) {
perror("open config dump file for writing");
return 1;
}
dump_config(config_zone);
maybe_restore_stdout(saved_stdout);
return 0;
}
void atecc_read_config_help(const char *cmdname)
{
eprintf("%s: read ATECC config zone blob into file\n", cmdname);
eprintf("Usage: %s output.bin|-\n", cmdname);
}
int do_atecc_read_config(int argc, char **argv)
{
int ret = 0;
if (argc < 2) {
atecc_read_config_help(argv[0]);
return 1;
}
const char *filename = argv[1];
if (strlen(filename) == 0) {
filename = "-";
}
FILE *out = maybe_fopen(filename, "w");
if (!out) {
perror("open config blob file for writing");
return 1;
}
ATCA_STATUS status;
uint8_t config_zone[ATCA_ECC_CONFIG_SIZE];
if ((status = atcab_read_config_zone(config_zone)) != ATCA_SUCCESS) {
eprintf("Command atcab_read_config_zone failed with status 0x%x\n",
status);
ret = 2;
goto _rcexit;
}
if (fwrite(config_zone, 1, sizeof (config_zone), out) !=
sizeof (config_zone)) {
perror("write config zone blob to file");
ret = 1;
goto _rcexit;
}
_rcexit:
maybe_fclose(out);
return ret;
}
void atecc_write_config_help(const char *cmdname)
{
eprintf("%s: write ATECC config zone blob from file\n", cmdname);
eprintf("Usage: %s input.bin|-\n", cmdname);
}
int do_atecc_write_config(int argc, char **argv)
{
int ret = 0;
if (argc < 2) {
atecc_write_config_help(argv[0]);
return 1;
}
const char *filename = argv[1];
if (strlen(filename) == 0) {
filename = "-";
}
FILE *in = maybe_fopen(filename, "r");
if (!in) {
perror("open config blob file for reading");
return 1;
}
ATCA_STATUS status;
uint8_t config_zone[ATCA_ECC_CONFIG_SIZE];
if (fread(config_zone, 1, sizeof (config_zone), in) !=
sizeof (config_zone)) {
perror("read config zone blob from file");
ret = 1;
goto _wcexit;
}
bool is_locked;
status = atcab_is_locked(ATCA_ZONE_CONFIG, &is_locked);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_is_locked is failed with status 0x%x\n", status);
ret = 2;
goto _wcexit;
}
if (is_locked) {
eprintf("Cannot configure Config Zone - Config Zone is already locked.\n");
ret = 2;
goto _wcexit;
}
status = atcab_write_config_zone(config_zone);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_write_config_zone is failed with status 0x%x\n", status);
ret = 2;
goto _wcexit;
}
// Set both counters to zero
status = atcab_write_config_counter(0, 0);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_write_config_counter is failed with status %d\n", status);
ret = 2;
goto _wcexit;
}
status = atcab_write_config_counter(1, 0);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_write_config_counter is failed with status %d\n", status);
ret = 2;
goto _wcexit;
}
_wcexit:
maybe_fclose(in);
return ret;
}
void atecc_lock_config_help(const char *cmdname)
{
eprintf("%s: lock ATECC config zone. This can't be undone!\n", cmdname);
eprintf("Usage: %s\n", cmdname);
}
int do_atecc_lock_config(int argc, char **argv)
{
(void) argc;
(void) argv;
ATCA_STATUS status;
status = atcab_lock_config_zone();
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_lock_config_zone is failed with status 0x%x\n", status);
return 2;
}
return 0;
}
void atecc_lock_slot_help(const char *cmdname)
{
eprintf("%s: lock ATECC slot zone. This can't be undone!\n", cmdname);
eprintf("Usage: %s <slot_id>\n", cmdname);
}
int do_atecc_lock_slot(int argc, char **argv)
{
if (argc < 2) {
atecc_lock_slot_help(argv[0]);
return 1;
}
uint16_t slot_id = atoi(argv[1]);
ATCA_STATUS status;
status = atcab_lock_data_slot(slot_id);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_lock_data_slot is failed with status 0x%x\n", status);
return 2;
}
return 0;
}
int do_atecc_config_is_locked(int argc, char **argv)
{
(void) argc;
(void) argv;
ATCA_STATUS status;
bool state;
status = atcab_is_locked(LOCK_ZONE_CONFIG, &state);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_is_locked is failed with status 0x%x\n", status);
return 2;
}
if (state) {
eprintf("Config zone is locked\n");
return 0;
} else {
eprintf("Config zone is unlocked\n");
return 1;
}
}
void atecc_config_is_locked_help(const char *cmdname)
{
eprintf("Usage: %s\nReturns 0 if config is locked, "
"1 if unlocked, 2 on error\n", cmdname);
}
int do_atecc_extra_set(int argc, char **argv)
{
if (argc < 3) {
atecc_extra_set_help(argv[0]);
return 1;
}
ATCA_STATUS status;
int address = atoi(argv[1]);
uint16_t value = atoi(argv[2]);
value &= 0xFF;
uint8_t mode = 0;
if (address == 84) {
mode = 0;
} else if (address == 85) {
mode = 1;
} else {
atecc_extra_set_help(argv[0]);
return 1;
}
status = atcab_updateextra(mode, value);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_updateextra is failed with status 0x%x\n",
status);
return 2;
}
return 0;
}
void atecc_extra_set_help(const char *cmdname)
{
eprintf("Usage: %s <address> <value>\n"
"Writes extra byte in specific address.\n"
"Correct addresses are 84 and 85.\n", cmdname);
}
int do_atecc_extra_get(int argc, char **argv)
{
if (argc < 2) {
atecc_extra_get_help(argv[0]);
return 1;
}
ATCA_STATUS status;
int address = atoi(argv[1]);
uint8_t value;
if (address != 84 && address != 85) {
atecc_extra_get_help(argv[0]);
return 1;
}
status = atcab_read_bytes_zone(ATCA_ZONE_CONFIG, 0, address, &value, 1);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_read_bytes_zone is failed with status 0x%x\n",
status);
return 2;
}
printf("Extra %02d: 0x%02hhx\n", address, value);
return 0;
}
void atecc_extra_get_help(const char *cmdname)
{
eprintf("Usage: %s <address>\n"
"Reads extra byte from specific address.\n"
"Correct addresses are 84 and 85.\n", cmdname);
}