forked from wirenboard/atecc-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atecc-ecdh.c
73 lines (60 loc) · 1.85 KB
/
atecc-ecdh.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
#include "atecc-ecdh.h"
#include "helpers.h"
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include "basic/atca_basic.h"
int do_atecc_ecdh(int argc, char **argv)
{
if (argc < 3) {
atecc_ecdh_help(argv[0]);
return 1;
}
uint16_t slot_id = atoi(argv[1]);
uint8_t pms[ATCA_KEY_SIZE];
uint8_t pubkey[ATCA_PUB_KEY_SIZE];
size_t read_size = 0;
ATCA_STATUS status;
FILE *pubkeyfile = maybe_fopen(argv[2], "rb");
if (!pubkeyfile) {
perror("open public key file for reading");
return 1;
}
read_size = fread(pubkey, 1, sizeof (pubkey), pubkeyfile);
if (read_size != sizeof (pubkey)) {
perror("read pubkey from file");
maybe_fclose(pubkeyfile);
return 1;
}
maybe_fclose(pubkeyfile);
ATECC_RETRY(status, atcab_ecdh(slot_id, pubkey, pms));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_ecdh is failed with status 0x%x\n", status);
return 2;
}
if (argc == 4) {
size_t write_size;
FILE *pmsfile = maybe_fopen(argv[3], "wb");
if (!pmsfile) {
perror("open secret file for writing");
return 1;
}
write_size = fwrite(pms, 1, sizeof (pms), pmsfile);
if (write_size != sizeof (pms)) {
perror("write secret to file");
maybe_fclose(pmsfile);
return 1;
}
maybe_fclose(pmsfile);
}
return 0;
}
void atecc_ecdh_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> public_key [secret_file]\n"
"\tslot_id\tID of slot with private key\n"
"\tpublic_key\tFile with public key (64 bytes, big-endian\n"
"\tsecret_file\tOptional file to store shared secret\n"
"If slot is configured to save secret in next slot, "
"no secret is returned.\n", cmdname);
}