forked from wirenboard/atecc-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atecc-asymm.c
376 lines (308 loc) · 9.83 KB
/
atecc-asymm.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
#include "atecc-asymm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "helpers.h"
#include "util.h"
#include "basic/atca_basic.h"
int do_atecc_gen_private(int argc, char **argv)
{
if (argc < 2) {
atecc_gen_private_help(argv[0]);
return 1;
}
ATCA_STATUS status;
int key_id = atoi(argv[1]);
const char *pubkeyfilename = NULL;
FILE *pubkeyfile = NULL;
uint8_t pubkey[ATCA_PUB_KEY_SIZE];
if (argc == 3) {
pubkeyfilename = argv[2];
}
ATECC_RETRY(status, atcab_genkey(key_id, pubkey));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_genkey is failed with status 0x%x\n", status);
if (pubkeyfile) {
maybe_fclose(pubkeyfile);
}
return 2;
}
if (pubkeyfilename) {
pubkeyfile = maybe_fopen(pubkeyfilename, "wb");
if (!pubkeyfile) {
perror("open public key file for writing");
return 1;
}
if (fwrite(pubkey, 1, sizeof (pubkey), pubkeyfile) !=
sizeof (pubkey)) {
perror("write public key to file");
maybe_fclose(pubkeyfile);
return 1;
}
maybe_fclose(pubkeyfile);
}
return 0;
}
void atecc_gen_private_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> [pubkey_file]\n", cmdname);
eprintf("Generates an ECDSA private key in given slot.\n");
eprintf("If pubkey_file is set, also writes public key into file.\n");
}
int do_atecc_write_private(int argc, char **argv)
{
if (argc != 3 && argc != 5) {
atecc_write_private_help(argv[0]);
return 1;
}
ATCA_STATUS status;
int key_id = atoi(argv[1]);
uint8_t privatekey[ATCA_PRIV_KEY_SIZE];
const char *privatekeyfilename = argv[2];
FILE *privatekeyfile = NULL, *writekeyfile = NULL;
int writekey_id = -1;
uint8_t writekeybuffer[ATCA_KEY_SIZE];
uint8_t *writekey = NULL;
const char *writekeyfilename = NULL;
if (argc == 5) {
writekey = writekeybuffer;
writekey_id = atoi(argv[3]);
writekeyfilename = argv[4];
/* read write key */
writekeyfile = maybe_fopen(writekeyfilename, "rb");
if (!writekeyfile) {
perror("open write key for reading");
return 1;
}
if (sizeof (writekeybuffer) !=
fread(writekeybuffer, 1, sizeof (writekeybuffer), writekeyfile)) {
perror("read write key from file");
maybe_fclose(writekeyfile);
return 1;
}
maybe_fclose(writekeyfile);
}
/* read private key */
privatekeyfile = maybe_fopen(privatekeyfilename, "rb");
if (!privatekeyfile) {
perror("open private key file for reading");
return 1;
}
if (sizeof (privatekey) !=
fread(privatekey, 1, sizeof (privatekey), privatekeyfile)) {
perror("read private key from file");
maybe_fclose(privatekeyfile);
return 1;
}
maybe_fclose(privatekeyfile);
// atcab_priv_write expects 36 bytes total with four leading zero bytes
assert (ATCA_PRIV_KEY_SIZE == 32);
uint8_t privatekey_payload[ATCA_PRIV_KEY_SIZE + 4] = {};
memset(privatekey_payload, 0x00, 4);
memcpy(privatekey_payload + 4, privatekey, sizeof(privatekey));
ATECC_RETRY(status, atcab_priv_write(key_id, privatekey_payload, writekey_id, writekey));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_priv_write is failed with status 0x%x\n", status);
return 2;
}
return 0;
}
void atecc_write_private_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> private_key_file "
"[<write_key_slot> write_key_file]\n", cmdname);
eprintf("Writes an ECDSA private key in given slot.\n");
eprintf("Private key is 32 bytes in length, without 4 leading zeroes.\n");
eprintf("If data section is locked, you also need to "
"determine write key.\n");
}
int do_atecc_read_pub(int argc, char **argv)
{
ATCA_STATUS status;
if (argc < 3) {
atecc_read_pub_help(argv[0]);
return 1;
}
int slot_id = atoi(argv[1]);
const char *outfile = argv[2];
uint8_t buffer[ATCA_PUB_KEY_SIZE];
FILE *pubkey = maybe_fopen(outfile, "wb");
if (!pubkey) {
perror("open public key file for writing");
return 1;
}
ATECC_RETRY(status, atcab_read_pubkey(slot_id, buffer));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_read_pubkey is failed with status 0x%x\n", status);
maybe_fclose(pubkey);
return 2;
}
if (sizeof (buffer) !=
fwrite(buffer, 1, sizeof (buffer), pubkey)) {
perror("write public key in file");
maybe_fclose(pubkey);
return 1;
}
maybe_fclose(pubkey);
return 0;
}
void atecc_read_pub_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> pubkey_file\n", cmdname);
eprintf("Reads a public key from selected slot. Note that \n"
"only slots 8 to 15 are large enough for a public key.\n");
eprintf("Output format: 32 bytes of X and Y, big-endian\n");
}
int do_atecc_gen_pub(int argc, char **argv)
{
ATCA_STATUS status;
if (argc < 3) {
atecc_gen_pub_help(argv[0]);
return 1;
}
int slot_id = atoi(argv[1]);
const char *outfile = argv[2];
uint8_t buffer[ATCA_PUB_KEY_SIZE];
FILE *pubkey = maybe_fopen(outfile, "wb");
if (!pubkey) {
perror("open public key file for writing");
return 1;
}
ATECC_RETRY(status, atcab_get_pubkey(slot_id, buffer));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_get_pubkey is failed with status 0x%x\n", status);
maybe_fclose(pubkey);
return 2;
}
if (sizeof (buffer) !=
fwrite(buffer, 1, sizeof (buffer), pubkey)) {
perror("write public key in file");
maybe_fclose(pubkey);
return 1;
}
maybe_fclose(pubkey);
return 0;
}
void atecc_gen_pub_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> pubkey_file\n", cmdname);
eprintf("Generates a public key from private in selected slot.\n");
eprintf("Output format: 32 bytes of X and Y, big-endian\n");
}
int do_atecc_sign(int argc, char **argv)
{
if (argc < 4) {
atecc_sign_help(argv[0]);
return 1;
}
int slot_id = atoi(argv[1]);
const char *messagefilename = argv[2];
const char *signaturefilename = argv[3];
ATCA_STATUS status;
FILE *signaturefile = NULL;
uint8_t signature[ATCA_SIG_SIZE];
uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE];
int sha_status = sha256_file(messagefilename, digest);
if (sha_status != 0) {
return sha_status;
}
ATECC_RETRY(status, atcab_sign(slot_id, digest, signature));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_sign is failed with status 0x%x\n", status);
return 2;
}
signaturefile = maybe_fopen(signaturefilename, "wb");
if (!signaturefile) {
perror("open signature file for writing");
return 1;
}
if (fwrite(signature, 1, sizeof (signature), signaturefile) !=
sizeof (signature)) {
perror("write signature to file");
maybe_fclose(signaturefile);
return 1;
}
maybe_fclose(signaturefile);
return 0;
}
void atecc_sign_help(const char *cmdname)
{
eprintf("Usage %s <slot_id> message_file signature_file\n", cmdname);
eprintf("Calculates a signature for message using "
"private key in given slot\n");
}
int do_atecc_verify(int argc, char **argv)
{
if (argc < 4) {
atecc_verify_help(argv[0]);
return 2;
}
uint16_t slot_id = atoi(argv[1]);
const char *messagefilename = argv[2];
const char *signaturefilename = argv[3];
const char *pubkeyfilename = NULL;
uint8_t pubkey[ATCA_PUB_KEY_SIZE];
if (argc == 5) {
pubkeyfilename = argv[4];
FILE *pubkeyfile = fopen(pubkeyfilename, "rb");
if (!pubkeyfile) {
perror("open public key file for reading");
return 2;
}
if (fread(pubkey, 1, sizeof (pubkey), pubkeyfile) !=
sizeof (pubkey)) {
perror("read pubkey from file");
fclose(pubkeyfile);
return 2;
}
fclose(pubkeyfile);
}
uint8_t signature[ATCA_SIG_SIZE];
uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE];
bool verified = false;
ATCA_STATUS status;
int sha_status = sha256_file(messagefilename, digest);
if (sha_status != 0) {
return sha_status;
}
FILE *signaturefile = maybe_fopen(signaturefilename, "rb");
if (!signaturefile) {
perror("open signature file for reading");
return 2;
}
if (fread(signature, 1, sizeof (signature), signaturefile) !=
sizeof (signature)) {
perror("read signature from file");
return 2;
}
maybe_fclose(signaturefile);
if (!pubkeyfilename) {
ATECC_RETRY(status, atcab_verify_stored(digest, signature, slot_id, &verified));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_verify_stored is failed with status 0x%x\n",
status);
return 2;
}
} else {
ATECC_RETRY(status, atcab_verify_extern(digest, signature, pubkey, &verified));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_verify_extern is failed with status 0x%x\n",
status);
return 2;
}
}
if (verified) {
eprintf("Verification OK\n");
return 0;
} else {
eprintf("Verification FAILED\n");
return 1;
}
}
void atecc_verify_help(const char *cmdname)
{
eprintf("Usage %s <slot_id> message_file signature_file [pubkey]\n", cmdname);
eprintf("Verifies a signature for message using "
"public key in given slot\n");
}