-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathatecc-data.c
311 lines (256 loc) · 8 KB
/
atecc-data.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
#include "atecc-data.h"
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"
#include "util.h"
#include "basic/atca_basic.h"
int do_atecc_write_data(int argc, char **argv)
{
if (argc < 4) {
atecc_write_data_help(argv[0]);
return 1;
}
int ret = 0;
int slot_id = atoi(argv[1]);
int offset = atoi(argv[2]);
const char *inputfilename = argv[3];
FILE *inputfile = NULL;
uint8_t *inputbuffer = NULL;
size_t slotsize = 0, inputsize = 0, readsize = 0;
ATCA_STATUS status;
/* check offset */
if (offset % ATCA_WORD_SIZE != 0) {
eprintf("Offset must be aligned to %d!\n", ATCA_WORD_SIZE);
return 1;
}
/* get size of specific slot */
ATECC_RETRY(status, atcab_get_zone_size(ATCA_ZONE_DATA, slot_id, &slotsize));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_get_zone_size is failed with status 0x%x\n",
status);
return 2;
}
inputbuffer = (uint8_t *) malloc(slotsize);
if (!inputbuffer) {
perror("allocating slot buffer");
return 1;
}
inputfile = maybe_fopen(inputfilename, "rb");
if (!inputfile) {
perror("open data file for reading");
free(inputbuffer);
return 1;
}
/* read as much data as possible */
while ((readsize = fread(inputbuffer + inputsize, 1,
slotsize - offset - inputsize, inputfile)) != 0) {
inputsize += readsize;
}
/* check input size */
if (!feof(inputfile)) {
eprintf("File doesn't fit in slot area: "
"slot size %lu\n", slotsize);
ret = 1;
goto _exit;
}
if (inputsize % ATCA_WORD_SIZE != 0) {
eprintf("Input size must be aligned to word size %d\n",
ATCA_WORD_SIZE);
ret = 1;
goto _exit;
}
// Force send ATECC to idle mode, so watchdog won't be triggered
// in the middle of command execution.
// This may happen because of I/O in fread slow enough,
// it adds delay between ATECC init sequence in main() and this operation.
status = atcab_idle();
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_idle is failed with status 0x%x, but let's continue\n", status);
}
/* try to write data to chip */
status = atcab_write_bytes_zone(ATCA_ZONE_DATA, slot_id, offset,
inputbuffer, inputsize);
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_write_bytes_zone is failed with status 0x%x\n",
status);
ret = 2;
}
_exit:
if (inputbuffer) {
free(inputbuffer);
}
maybe_fclose(inputfile);
return ret;
}
void atecc_write_data_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> <offset> input_file\n", cmdname);
eprintf("Writes data from file to specific slot with offset.\n"
"Data is written as plaintext.\n");
}
int do_atecc_read_data(int argc, char **argv)
{
if (argc < 5) {
atecc_read_data_help(argv[0]);
return 1;
}
int ret = 0;
int slot_id = atoi(argv[1]);
int offset = atoi(argv[2]);
size_t outputsize = atoi(argv[3]);
const char *outputfilename = argv[4];
FILE *outputfile = NULL;
uint8_t *outputbuffer = NULL;
ATCA_STATUS status;
uint8_t readkey[ATCA_KEY_SIZE];
const char *readkeyfilename = NULL;
uint16_t readkey_slot;
if (argc == 7) {
readkeyfilename = argv[5];
readkey_slot = atoi(argv[6]);
size_t readsize;
FILE *readkeyfile = maybe_fopen(readkeyfilename, "rb");
if (!readkeyfile) {
perror("open read key file for reading");
return 1;
}
readsize = fread(readkey, 1, sizeof (readkey), readkeyfile);
if (readsize != sizeof (readkey)) {
perror("read read key from file");
maybe_fclose(readkeyfile);
return 1;
}
maybe_fclose(readkeyfile);
}
// Force send ATECC to idle mode, so watchdog won't be triggered
// in the middle of command execution.
// This may happen because of I/O in fread slow enough,
// it adds delay between ATECC init sequence in main() and this operation.
status = atcab_idle();
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_idle is failed with status 0x%x, but let's continue\n", status);
}
/* read data from ATECC */
if (readkeyfilename != NULL) {
outputsize = min(outputsize, ATCA_KEY_SIZE);
outputbuffer = (uint8_t *) malloc(ATCA_KEY_SIZE);
if (!outputbuffer) {
perror("allocating slot buffer");
return 1;
}
ATECC_RETRY(status, atcab_read_enc(slot_id, offset, outputbuffer,
readkey, readkey_slot));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_read_enc is failed with status 0x%x\n",
status);
free(outputbuffer);
return 2;
}
} else {
outputbuffer = (uint8_t *) malloc(outputsize);
if (!outputbuffer) {
perror("allocating slot buffer");
return 1;
}
ATECC_RETRY(status, atcab_read_bytes_zone(ATCA_ZONE_DATA, slot_id, offset,
outputbuffer, outputsize));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_read_bytes_zone is failed with status 0x%x\n",
status);
free(outputbuffer);
return 2;
}
}
outputfile = maybe_fopen(outputfilename, "wb");
if (!outputfile) {
perror("open data file for writing");
free(outputbuffer);
return 1;
}
if (outputsize != fwrite(outputbuffer, 1, outputsize,
outputfile)) {
perror("write data to output file");
ret = 1;
}
if (outputbuffer) {
free(outputbuffer);
}
maybe_fclose(outputfile);
return ret;
}
void atecc_read_data_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> <offset> <size> output_file "
"[readkey_file <readkey_slot>]\n", cmdname);
eprintf("Reads data from specific slot with offset.\n"
"If keys are not set, data is read as plaintext.\n");
}
int do_atecc_lock_data(int argc, char **argv)
{
(void) argc;
(void) argv;
ATCA_STATUS status;
ATECC_RETRY(status, atcab_lock_data_zone());
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_lock_data_zone is failed with status 0x%x\n",
status);
return 2;
}
return 0;
}
void atecc_lock_data_help(const char *cmdname)
{
eprintf("Usage: %s\nLocks data zone. This can't be undone!\n",
cmdname);
}
int do_atecc_data_is_locked(int argc, char **argv)
{
(void) argc;
(void) argv;
ATCA_STATUS status;
bool state;
ATECC_RETRY(status, atcab_is_locked(LOCK_ZONE_DATA, &state));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_is_locked is failed with status 0x%x\n", status);
return 2;
}
if (state) {
eprintf("Data zone is locked\n");
return 0;
} else {
eprintf("Data zone is unlocked\n");
return 1;
}
}
void atecc_data_is_locked_help(const char *cmdname)
{
eprintf("Usage: %s\nReturns 0 if data is locked, "
"1 if unlocked, 2 on error\n", cmdname);
}
int do_atecc_slot_is_locked(int argc, char **argv)
{
if (argc < 2) {
atecc_slot_is_locked_help(argv[0]);
return 2;
}
uint16_t slot = atoi(argv[1]);
ATCA_STATUS status;
bool state;
ATECC_RETRY(status, atcab_is_slot_locked(slot, &state));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_is_slot_locked is failed with status 0x%x\n", status);
return 2;
}
if (state) {
eprintf("Slot %hu is locked\n", slot);
return 0;
} else {
eprintf("Slot %hu is unlocked\n", slot);
return 1;
}
}
void atecc_slot_is_locked_help(const char *cmdname)
{
eprintf("Usage: %s <slot>\nReturns 0 if slot is locked, "
"1 if unlocked, 2 on error\n", cmdname);
}