forked from wirenboard/atecc-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atecc-hmac.c
174 lines (147 loc) · 4.96 KB
/
atecc-hmac.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
#include "atecc-hmac.h"
#include "helpers.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include "basic/atca_basic.h"
int do_atecc_hmac_write_key(int argc, char **argv)
{
if (argc < 4 || argc == 5) {
atecc_hmac_write_key_help(argv[0]);
return 1;
}
int slot_id = atoi(argv[1]);
int offset = atoi(argv[2]);
const char *keyfilename = argv[3];
const char *writekeyfilename = NULL;
uint16_t key_id = 0;
if (argc == 6) {
writekeyfilename = argv[4];
key_id = atoi(argv[5]);
}
size_t readlen = 0;
uint8_t writekey[HMAC_KEY_SIZE];
if (writekeyfilename) {
FILE *writekeyfile = maybe_fopen(writekeyfilename, "rb");
if (!writekeyfile) {
perror("open writekey file");
return 1;
}
readlen = fread(writekey, 1, sizeof (writekey), writekeyfile);
if (readlen != sizeof (writekey)) {
perror("read write key from file");
maybe_fclose(writekeyfile);
return 1;
}
maybe_fclose(writekeyfile);
}
FILE *keyfile = maybe_fopen(keyfilename, "rb");
if (!keyfile) {
perror("open keyfile");
return 1;
}
uint8_t key[HMAC_KEY_SIZE];
readlen = fread(key, 1, sizeof (key), keyfile);
if (readlen != sizeof (key)) {
perror("read key from file");
maybe_fclose(keyfile);
return 1;
}
maybe_fclose(keyfile);
ATCA_STATUS status;
const char *cmd;
if (!writekeyfilename) {
cmd = "atcab_write_zone";
ATECC_RETRY(status, atcab_write_zone(ATCA_ZONE_DATA, slot_id, offset, 0, key, 32));
if (status != ATCA_SUCCESS) {
eprintf("Command %s is failed with status 0x%x\n", cmd, status);
return 2;
}
} else {
cmd = "atcab_write_enc";
ATECC_RETRY(status, atcab_write_enc(slot_id, offset, key, writekey, key_id));
if (status != ATCA_SUCCESS) {
eprintf("Command %s is failed with status 0x%x\n", cmd, status);
return 2;
}
}
return 0;
}
void atecc_hmac_write_key_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> <offset> data_file [write_key <write_key_id>]\n", cmdname);
eprintf("\tslot_id\tID of slot to write data block to\n");
eprintf("\toffset\tOffset (in 32-byte blocks) to write data block to\n");
eprintf("\tkeyfile\tFile containing data block to write (32 bytes long)\n");
eprintf("\twrite_key\tFile containing write-guarding key (32 bytes long)\n");
eprintf("\twrite_key_id\tID of write key on device\n");
eprintf("If both data and readkey must be read from stdin, "
"key is read first.");
}
int do_atecc_hmac_dgst(int argc, char **argv)
{
if (argc < 4) {
atecc_hmac_dgst_help(argv[0]);
return 1;
}
int slot_id = atoi(argv[1]);
const char *payloadfilename = argv[2];
const char *hmacfilename = argv[3];
uint8_t buffer[PAYLOAD_BUFFER_SIZE];
uint8_t hmac[ATCA_SHA_DIGEST_SIZE];
ATCA_STATUS status;
atca_hmac_sha256_ctx_t ctx;
FILE *payloadfile = maybe_fopen(payloadfilename, "rb");
if (!payloadfile) {
perror("open payload file for reading");
return 1;
}
/* send payload to ATECC */
ATECC_RETRY(status, atcab_sha_hmac_init(&ctx, slot_id));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_sha_hmac_init is failed with status 0x%x\n", status);
maybe_fclose(payloadfile);
return 2;
}
while (!feof(payloadfile)) {
size_t sz;
if (sizeof (buffer) !=
(sz = fread(buffer, 1, sizeof (buffer), payloadfile))) {
if (!feof(payloadfile)) {
perror("reading from payload file");
maybe_fclose(payloadfile);
return 1;
}
}
ATECC_RETRY(status, atcab_sha_hmac_update(&ctx, buffer, sz));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_sha_hmac_update is failed with status 0x%x\n", status);
return status;
}
}
maybe_fclose(payloadfile);
ATECC_RETRY(status, atcab_sha_hmac_finish(&ctx, hmac, SHA_MODE_TARGET_TEMPKEY));
if (status != ATCA_SUCCESS) {
eprintf("Command atcab_sha_hmac_finish is failed with status %d\n", status);
return status;
}
FILE *hmacfile = maybe_fopen(hmacfilename, "wb");
if (!hmacfile) {
perror("open hmac file for writing");
return 1;
}
if (fwrite(hmac, 1, sizeof (hmac), hmacfile) != sizeof (hmac)) {
perror("write hmac to file");
maybe_fclose(hmacfile);
return 1;
}
maybe_fclose(hmacfile);
return 0;
}
void atecc_hmac_dgst_help(const char *cmdname)
{
eprintf("Usage: %s <slot_id> <payload_file> <hmac_output>\n", cmdname);
eprintf("\tslot_id\tID of slot to use for HMAC dgstulation\n");
eprintf("\tpayload_file\tFile with payload (or - for stdin)\n");
eprintf("\thmac_output\tHMAC output file (or - for stdout)\n");
}