-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathatecc-auth.c
370 lines (316 loc) · 10.7 KB
/
atecc-auth.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
#include "atecc-auth.h"
#include "helpers.h"
#include "util.h"
#include "basic/atca_basic.h"
#include "host/atca_host.h"
#include "crypto/atca_crypto_sw.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PASSWD_LEN 256
#define PASSWD_SALT "atecc-salt123"
static int read_passwd(const char *filename, char *passwd)
{
FILE *f = maybe_fopen(filename, "r");
if (!f) {
perror("open password file for reading");
return 0;
}
if (fgets(passwd, MAX_PASSWD_LEN + 2, f) == NULL) {
perror("read password from file");
maybe_fclose(f);
return 0;
}
maybe_fclose(f);
/* remove trailing newline if it is there */
int tail = strlen(passwd) - 1;
if (passwd[tail] == '\n') {
passwd[tail] = '\0';
}
return 1;
}
static int make_key_from_password(const char *password, uint8_t key[ATCA_KEY_SIZE])
{
uint8_t buffer[MAX_PASSWD_LEN + sizeof (PASSWD_SALT) + 2];
strcpy((char *) buffer, password);
strcpy((char *) buffer + strlen(password), PASSWD_SALT);
if (atcac_sw_sha2_256((const uint8_t *) buffer, strlen((char *) buffer), key) != 0) {
return 0;
}
return 1;
}
static int read_key(const char *filename, uint8_t key[ATCA_KEY_SIZE])
{
FILE *f = maybe_fopen(filename, "rb");
if (!f) {
perror("open key file for reading");
return 0;
}
if (ATCA_KEY_SIZE != fread(key, 1, ATCA_KEY_SIZE, f)) {
perror("read key from file");
maybe_fclose(f);
return 0;
}
maybe_fclose(f);
return 1;
}
int do_atecc_auth_passwd(int argc, char **argv)
{
if (argc < 3) {
atecc_auth_passwd_help(argv[0]);
return 2;
}
uint16_t slot_id = atoi(argv[1]);
const char *passwd_file = argv[2];
/* read password */
char passwd[MAX_PASSWD_LEN + 2];
if (!read_passwd(passwd_file, passwd)) {
return 2;
}
/* make a key from password */
uint8_t key[ATCA_KEY_SIZE];
if (!make_key_from_password(passwd, key)) {
return 2;
}
/* generate MAC of specific message */
uint8_t sn[9];
uint8_t num_in[NONCE_NUMIN_SIZE] = { 0 };
uint8_t rand_out[RANDOM_NUM_SIZE] = { 0 };
uint8_t other_data[12] = { 0 };
uint8_t resp[32];
ATCA_STATUS status;
ATECC_RETRY(status, atcab_read_serial_number(sn));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_read_serial_number is failed with status 0x%x\n",
status);
return 2;
}
ATECC_RETRY(status, atcab_nonce_rand(num_in, rand_out));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_nonce_rand is failed with status 0x%x\n",
status);
return 2;
}
struct atca_temp_key temp_key;
memset(&temp_key, 0, sizeof (temp_key));
struct atca_nonce_in_out nonce_params = {
.mode = NONCE_MODE_SEED_UPDATE,
.zero = 0,
.num_in = num_in,
.rand_out = rand_out,
.temp_key = &temp_key,
};
status = atcah_nonce(&nonce_params);
if (status != ATCA_SUCCESS) {
eprintf("Command atcah_nonce is failed with status 0x%x\n", status);
return 2;
}
struct atca_check_mac_in_out check_mac = {
.mode = 1,
.key_id = slot_id,
.sn = sn,
.client_chal = NULL,
.client_resp = resp,
.other_data = other_data,
.otp = NULL,
.slot_key = key,
.target_key = NULL,
.temp_key = &temp_key,
};
status = atcah_check_mac(&check_mac);
if (status != ATCA_SUCCESS) {
eprintf("Command atcah_check_mac is failed with status 0x%x\n", status);
return 2;
}
/* check MAC */
ATECC_RETRY(status, atcab_checkmac(1, slot_id, NULL, resp, other_data));
if (status == ATCA_CHECKMAC_VERIFY_FAILED) {
eprintf("Authentication failure\n");
return 1;
}
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_checkmac is failed with status 0x%x\n",
status);
return 2;
}
return 0;
}
void atecc_auth_passwd_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> password_file\n"
"Authorizes key to use in next commands in row using password.\n"
"\tslot_id\tID of slot with authorizing key.\n"
"\tpassword_file\tInput stream with password (file or stdin).\n"
"Password ends with EOF or newline. Max length of password is %d\n",
cmdname, MAX_PASSWD_LEN);
}
int do_atecc_auth_make_passwd(int argc, char **argv)
{
if (argc < 3) {
atecc_auth_make_passwd_help(argv[0]);
return 2;
}
uint16_t slot_id = atoi(argv[1]);
const char *passwd_file = argv[2];
/* read password */
char passwd[MAX_PASSWD_LEN + 2];
if (!read_passwd(passwd_file, passwd)) {
return 2;
}
/* make a key from password */
uint8_t key[ATCA_KEY_SIZE];
if (!make_key_from_password(passwd, key)) {
return 2;
}
/* write key unencrypted */
ATCA_STATUS status;
ATECC_RETRY(status, atcab_write_bytes_zone(ATCA_ZONE_DATA, slot_id, 0, key, ATCA_KEY_SIZE));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_write_bytes_zone is failed with status 0x%x\n",
status);
return 2;
}
return 0;
}
void atecc_auth_make_passwd_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> password_file\n"
"Makes a key from password.\n"
"\tslot_id\tID of slot to write a key.\n"
"\tpassword_file\tInput stream with password (file or stdin).\n"
"Password ends with EOF or newline. Max length of password is %d\n",
cmdname, MAX_PASSWD_LEN);
}
int do_atecc_auth_check_gendig(int argc, char **argv)
{
if (argc < 3) {
atecc_auth_check_gendig_help(argv[0]);
return 2;
}
uint16_t gendig_slot = atoi(argv[1]);
const char *keyfilename = argv[2];
uint8_t key[ATCA_KEY_SIZE];
if (!read_key(keyfilename, key)) {
return 2;
}
ATCA_STATUS status;
atca_nonce_in_out_t nonce_params;
atca_gen_dig_in_out_t gen_dig_param;
atca_temp_key_t temp_key;
uint8_t serial_num[32];
uint8_t num_in[NONCE_NUMIN_SIZE] = { 0 };
uint8_t rand_out[RANDOM_NUM_SIZE] = { 0 };
uint8_t other_data[4] = { 0 };
uint8_t resp[32];
uint8_t challenge[32];
// 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);
}
do {
if ((status = atcab_random(challenge)) != ATCA_SUCCESS)
{
eprintf("Command atcab_random is failed with status 0x%x\n", status);
continue;
}
// Read the device SN
if ((status = atcab_read_zone(ATCA_ZONE_CONFIG, 0, 0, 0, serial_num, 32)) != ATCA_SUCCESS)
{
eprintf("Command atcab_read_zone is failed with status 0x%x\n", status);
continue;
}
// Make the SN continuous by moving SN[4:8] right after SN[0:3]
memmove(&serial_num[4], &serial_num[8], 5);
// Random Nonce inputs
memset(&temp_key, 0, sizeof(temp_key));
memset(&nonce_params, 0, sizeof(nonce_params));
nonce_params.mode = NONCE_MODE_SEED_UPDATE;
nonce_params.zero = 0;
nonce_params.num_in = (uint8_t*)&num_in;
nonce_params.rand_out = (uint8_t*)&rand_out;
nonce_params.temp_key = &temp_key;
// Send the random Nonce command
if ((status = atcab_nonce_rand(num_in, rand_out)) != ATCA_SUCCESS)
{
eprintf("Command atcab_nonce_rand is failed with status 0x%x\n", status);
continue;
}
// Calculate Tempkey
if ((status = atcah_nonce(&nonce_params)) != ATCA_SUCCESS)
{
eprintf("Command atcah_nonce is failed with status 0x%x\n", status);
continue;
}
// Supply OtherData so GenDig behavior is the same for keys with SlotConfig.NoMac set
other_data[0] = ATCA_GENDIG;
other_data[1] = GENDIG_ZONE_DATA;
other_data[2] = (uint8_t)(gendig_slot);
other_data[3] = (uint8_t)(gendig_slot >> 8);
// Send the GenDig command
if ((status = atcab_gendig(GENDIG_ZONE_DATA, gendig_slot, other_data, sizeof(other_data))) != ATCA_SUCCESS)
{
eprintf("Command atcab_gendig is failed with status 0x%x\n", status);
continue;
}
// Calculate Tempkey
// NoMac bit isn't being considered here on purpose to remove having to read SlotConfig.
// OtherData is built to get the same result regardless of the NoMac bit.
memset(&gen_dig_param, 0, sizeof(gen_dig_param));
gen_dig_param.key_id = gendig_slot;
gen_dig_param.is_key_nomac = false;
gen_dig_param.sn = serial_num;
gen_dig_param.stored_value = key;
gen_dig_param.zone = GENDIG_ZONE_DATA;
gen_dig_param.other_data = other_data;
gen_dig_param.temp_key = &temp_key;
if ((status = atcah_gen_dig(&gen_dig_param)) != ATCA_SUCCESS)
{
eprintf("Command atcah_gen_dig is failed with status 0x%x\n", status);
continue;
}
// Now perform CheckMac to validate keys
struct atca_check_mac_in_out check_mac = {
.mode = 2,
.key_id = 0, // TempKey used
.sn = serial_num,
.client_chal = challenge,
.client_resp = resp,
.other_data = other_data,
.otp = NULL,
.slot_key = key,
.target_key = NULL,
.temp_key = &temp_key,
};
status = atcah_check_mac(&check_mac);
if (status != ATCA_SUCCESS) {
eprintf("Command atcah_check_mac is failed with status 0x%x\n", status);
continue;
}
/* check MAC */
status = atcab_checkmac(2, 0, challenge, resp, other_data);
if (status == ATCA_CHECKMAC_VERIFY_FAILED) {
eprintf("Keys doesn't match\n");
return 1;
}
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_checkmac is failed with status 0x%x\n",
status);
continue;
}
} while (should_retry(status));
if (status != ATCA_SUCCESS) {
return 2;
}
eprintf("Keys match\n");
return 0;
}
void atecc_auth_check_gendig_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> key_file\n"
"Checks key in selected slot matches key in file using GenDig.\n",
cmdname);
}