Skip to content

Commit f55f833

Browse files
committed
Improve key rotation error reporting in htool.
PiperOrigin-RevId: 808344093
1 parent 3521bfc commit f55f833

7 files changed

Lines changed: 174 additions & 33 deletions

File tree

examples/htool_key_rotation.c

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,80 @@
2828

2929
#include "htool.h"
3030
#include "htool_cmd.h"
31+
#include "protocol/host_cmd.h"
3132
#include "protocol/key_rotation.h"
3233

33-
static const char *get_validation_method_string(uint32_t validation_method) {
34+
static const char* get_error_code_string(enum key_rotation_err err) {
35+
if (err < KEY_ROTATION_ERR_HOTH_BASE) {
36+
switch (err) {
37+
case KEY_ROTATION_CMD_SUCCESS:
38+
return "Success";
39+
case KEY_ROTATION_INTERNAL_ERR:
40+
return "Internal Error";
41+
case KEY_ROTATION_ERR_INVALID_PARAM:
42+
return "Invalid Parameter";
43+
case KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE:
44+
return "Invalid Response Size";
45+
case KEY_ROTATION_INITIATE_FAIL:
46+
return "Key Rotation Record Initiate Failed";
47+
case KEY_ROTATION_COMMIT_FAIL:
48+
return "Key Rotation Record Commit Failed";
49+
case KEY_ROTATION_ROOT_OF_TRUST_UNAVAILABLE:
50+
return "Root of Trust Unavailable";
51+
default:
52+
return "Unknown";
53+
}
54+
}
55+
int ret = err - KEY_ROTATION_ERR_HOTH_BASE;
56+
switch (ret) {
57+
case HOTH_RES_SUCCESS:
58+
return "Success";
59+
case HOTH_RES_INVALID_COMMAND:
60+
return "Key Rotation Feature Not Supported";
61+
case HOTH_RES_ERROR:
62+
return "Hoth Error";
63+
case HOTH_RES_INVALID_PARAM:
64+
return "Invalid Parameter";
65+
case HOTH_RES_ACCESS_DENIED:
66+
return "Access Denied";
67+
case HOTH_RES_INVALID_RESPONSE:
68+
return "Invalid Response";
69+
case HOTH_RES_INVALID_VERSION:
70+
return "Invalid Version";
71+
case HOTH_RES_INVALID_CHECKSUM:
72+
return "Invalid Checksum";
73+
case HOTH_RES_IN_PROGRESS:
74+
return "In Progress";
75+
case HOTH_RES_UNAVAILABLE:
76+
return "Unavailable";
77+
case HOTH_RES_TIMEOUT:
78+
return "Timeout";
79+
case HOTH_RES_OVERFLOW:
80+
return "Overflow";
81+
case HOTH_RES_INVALID_HEADER:
82+
return "Invalid Header";
83+
case HOTH_RES_REQUEST_TRUNCATED:
84+
return "Request Truncated";
85+
case HOTH_RES_RESPONSE_TOO_BIG:
86+
return "Response Too Big";
87+
case HOTH_RES_BUS_ERROR:
88+
return "Bus Error";
89+
case HOTH_RES_BUSY:
90+
return "Busy";
91+
case HOTH_RES_INVALID_HEADER_VERSION:
92+
return "Invalid Header Version";
93+
case HOTH_RES_INVALID_HEADER_CRC:
94+
return "Invalid Header CRC";
95+
case HOTH_RES_INVALID_DATA_CRC:
96+
return "Invalid Data CRC";
97+
case HOTH_RES_DUP_UNAVAILABLE:
98+
return "Duplicate Request Unavailable";
99+
default:
100+
return "Unknown";
101+
}
102+
}
103+
104+
static const char* get_validation_method_string(uint32_t validation_method) {
34105
switch (validation_method) {
35106
case 1:
36107
return "Embedded Key";
@@ -46,11 +117,14 @@ static const char *get_validation_method_string(uint32_t validation_method) {
46117
int htool_key_rotation_get_status(void) {
47118
struct libhoth_device *dev = htool_libhoth_device();
48119
if (!dev) {
120+
fprintf(stderr, "Failed to get libhoth device\n");
49121
return -1;
50122
}
51123
struct hoth_response_key_rotation_status status;
52124
enum key_rotation_err ret = libhoth_key_rotation_get_status(dev, &status);
53125
if (ret) {
126+
fprintf(stderr, "HOTH_KEY_ROTATION_GET_STATUS error: %s\n",
127+
get_error_code_string(ret));
54128
fprintf(stderr, "Failed to get key rotation status\n");
55129
return -1;
56130
}
@@ -68,11 +142,14 @@ int htool_key_rotation_get_status(void) {
68142
int htool_key_rotation_get_version(void) {
69143
struct libhoth_device *dev = htool_libhoth_device();
70144
if (!dev) {
145+
fprintf(stderr, "Failed to get libhoth device\n");
71146
return -1;
72147
}
73148
struct hoth_response_key_rotation_record_version version;
74149
enum key_rotation_err ret = libhoth_key_rotation_get_version(dev, &version);
75150
if (ret) {
151+
fprintf(stderr, "HOTH_KEY_ROTATION_GET_VERSION error: %s\n",
152+
get_error_code_string(ret));
76153
fprintf(stderr, "Failed to get key rotation version\n");
77154
return -1;
78155
}
@@ -132,6 +209,7 @@ static int read_image_file(const char *image_file, uint8_t **image,
132209
int htool_key_rotation_update(const struct htool_invocation *inv) {
133210
struct libhoth_device *dev = htool_libhoth_device();
134211
if (!dev) {
212+
fprintf(stderr, "Failed to get libhoth device\n");
135213
return -1;
136214
}
137215
const char *image_file;
@@ -148,6 +226,8 @@ int htool_key_rotation_update(const struct htool_invocation *inv) {
148226

149227
enum key_rotation_err key_ret = libhoth_key_rotation_update(dev, image, size);
150228
if (key_ret) {
229+
fprintf(stderr, "HOTH_KEY_ROTATION_UPDATE error: %s\n",
230+
get_error_code_string(key_ret));
151231
fprintf(stderr, "Failed to update key rotation record\n");
152232
result = key_ret;
153233
}
@@ -164,12 +244,15 @@ int htool_key_rotation_update(const struct htool_invocation *inv) {
164244
int htool_key_rotation_payload_status() {
165245
struct libhoth_device *dev = htool_libhoth_device();
166246
if (!dev) {
247+
fprintf(stderr, "Failed to get libhoth device\n");
167248
return -1;
168249
}
169250
struct hoth_response_key_rotation_payload_status payload_status;
170251
enum key_rotation_err ret =
171252
libhoth_key_rotation_payload_status(dev, &payload_status);
172253
if (ret) {
254+
fprintf(stderr, "HOTH_KEY_ROTATION_PAYLOAD_STATUS error: %s\n",
255+
get_error_code_string(ret));
173256
fprintf(stderr, "Failed to get key rotation payload status\n");
174257
return -1;
175258
}
@@ -201,6 +284,7 @@ static int get_key_rotation_read_half(const char *read_half,
201284
int htool_key_rotation_read(const struct htool_invocation *inv) {
202285
struct libhoth_device *dev = htool_libhoth_device();
203286
if (!dev) {
287+
fprintf(stderr, "Failed to get libhoth device\n");
204288
return -1;
205289
}
206290
uint32_t offset = 0;
@@ -246,6 +330,8 @@ int htool_key_rotation_read(const struct htool_invocation *inv) {
246330
enum key_rotation_err ret_read =
247331
libhoth_key_rotation_read(dev, offset, size, read_half, &read_response);
248332
if (ret_read) {
333+
fprintf(stderr, "HOTH_KEY_ROTATION_READ error: %s\n",
334+
get_error_code_string(ret_read));
249335
fprintf(stderr, "Failed to read key rotation record\n");
250336
if (fd != -1) {
251337
close(fd);
@@ -296,6 +382,7 @@ static int get_key_rotation_chunk_type(const char *chunk_type_string,
296382
int htool_key_rotation_read_chunk_type(const struct htool_invocation *inv) {
297383
struct libhoth_device *dev = htool_libhoth_device();
298384
if (!dev) {
385+
fprintf(stderr, "Failed to get libhoth device\n");
299386
return -1;
300387
}
301388
uint32_t offset = 0;
@@ -345,6 +432,8 @@ int htool_key_rotation_read_chunk_type(const struct htool_invocation *inv) {
345432
dev, chunk_typecode, chunk_index, offset, size, &read_response,
346433
&response_size);
347434
if (ret_read) {
435+
fprintf(stderr, "HOTH_KEY_ROTATION_READ_CHUNK_TYPE error: %s\n",
436+
get_error_code_string(ret_read));
348437
fprintf(stderr, "Failed to read chunk from key rotation record\n");
349438
return -1;
350439
}
@@ -385,6 +474,7 @@ int htool_key_rotation_read_chunk_type(const struct htool_invocation *inv) {
385474
int htool_key_rotation_chunk_type_count(const struct htool_invocation *inv) {
386475
struct libhoth_device *dev = htool_libhoth_device();
387476
if (!dev) {
477+
fprintf(stderr, "Failed to get libhoth device\n");
388478
return -1;
389479
}
390480
const char *chunk_type_string;
@@ -401,6 +491,8 @@ int htool_key_rotation_chunk_type_count(const struct htool_invocation *inv) {
401491
enum key_rotation_err ret_count =
402492
libhoth_key_rotation_chunk_type_count(dev, chunk_typecode, &chunk_count);
403493
if (ret_count) {
494+
fprintf(stderr, "HOTH_KEY_ROTATION_CHUNK_TYPE_COUNT error: %s\n",
495+
get_error_code_string(ret_count));
404496
fprintf(stderr, "Failed to get chunk type count\n");
405497
return -1;
406498
}

examples/htool_secure_boot.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
<<<<<<< HEAD
1516
#include "examples/htool_secure_boot.h"
17+
=======
18+
#include "htool_secure_boot.h"
19+
>>>>>>> b127e59 (Improve key rotation error reporting in htool.)
1620

1721
#include <stdint.h>
1822
#include <stdio.h>
1923

2024
#include "examples/htool.h"
25+
<<<<<<< HEAD
2126
#include "examples/htool_cmd.h"
27+
=======
28+
#include "htool_cmd.h"
29+
>>>>>>> b127e59 (Improve key rotation error reporting in htool.)
2230
#include "protocol/secure_boot.h"
2331

2432
int htool_secure_boot_get_enforcement(const struct htool_invocation* inv) {

examples/htool_secure_boot.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
#ifndef LIBHOTH_EXAMPLES_HTOOL_SECURE_BOOT_H_
1616
#define LIBHOTH_EXAMPLES_HTOOL_SECURE_BOOT_H_
1717

18+
<<<<<<< HEAD
1819
#include "examples/htool_cmd.h"
20+
=======
21+
#include "htool_cmd.h"
22+
>>>>>>> b127e59 (Improve key rotation error reporting in htool.)
1923

2024
#ifdef __cplusplus
2125
extern "C" {

protocol/key_rotation.c

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,39 @@ struct hoth_request_variable_length {
2727
uint8_t data[KEY_ROTATION_RECORD_WRITE_MAX_SIZE];
2828
} __hoth_align4;
2929

30+
int get_command_version(struct libhoth_device* dev, uint16_t command,
31+
uint8_t* version) {
32+
fprintf(stderr, "HOTH_GET_CMD_VERSIONS\n");
33+
size_t rlen = 0;
34+
int ret = libhoth_hostcmd_exec(
35+
dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_GET_CMD_VERSIONS,
36+
0, &command, sizeof(command), NULL, 0, &rlen);
37+
if (ret != 0) {
38+
return ret;
39+
}
40+
if (rlen != 0) {
41+
fprintf(stderr,
42+
"HOTH_GET_CMD_VERSIONS expected exactly %d response "
43+
"bytes, got %ld\n",
44+
0, rlen);
45+
return ret;
46+
}
47+
*version = rlen;
48+
return 0;
49+
}
50+
51+
enum key_rotation_err get_key_rotation_error(int ret) {
52+
fprintf(stderr, "HTOOL_ERROR_HOST_COMMAND return code: %d\n", ret);
53+
int result = ret - HTOOL_ERROR_HOST_COMMAND_START;
54+
if (result < 0) {
55+
return KEY_ROTATION_ROOT_OF_TRUST_UNAVAILABLE;
56+
}
57+
if (result == HOTH_RES_SUCCESS) {
58+
return KEY_ROTATION_CMD_SUCCESS;
59+
}
60+
return KEY_ROTATION_ERR_HOTH_BASE + result;
61+
}
62+
3063
static enum key_rotation_err send_key_rotation_request(
3164
struct libhoth_device* dev, uint16_t command) {
3265
const struct hoth_request_key_rotation_record request = {
@@ -40,9 +73,7 @@ static enum key_rotation_err send_key_rotation_request(
4073
dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0,
4174
&request, sizeof(request), NULL, 0, &rlen);
4275
if (ret != 0) {
43-
fprintf(stderr, "HOTH_KEY_ROTATION_COMMAND %d error code: %d\n", command,
44-
ret);
45-
return KEY_ROTATION_ERR;
76+
return get_key_rotation_error(ret);
4677
}
4778
if (rlen != 0) {
4879
fprintf(stderr,
@@ -71,8 +102,7 @@ enum key_rotation_err libhoth_key_rotation_get_version(
71102
&rlen);
72103

73104
if (ret != 0) {
74-
fprintf(stderr, "HOTH_KEY_ROTATION_GET_VERSION error code: %d\n", ret);
75-
return KEY_ROTATION_ERR;
105+
return get_key_rotation_error(ret);
76106
}
77107

78108
if (rlen != sizeof(*record_version)) {
@@ -89,6 +119,12 @@ enum key_rotation_err libhoth_key_rotation_get_version(
89119
enum key_rotation_err libhoth_key_rotation_get_status(
90120
struct libhoth_device* dev,
91121
struct hoth_response_key_rotation_status* record_status) {
122+
uint8_t version = 0;
123+
if (get_command_version(dev, HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, &version) !=
124+
0) {
125+
fprintf(stderr, "Failed to get command version.\n");
126+
return KEY_ROTATION_ROOT_OF_TRUST_UNAVAILABLE;
127+
}
92128
const struct hoth_request_key_rotation_record request = {
93129
.operation = KEY_ROTATION_RECORD_GET_STATUS,
94130
.packet_offset = 0,
@@ -102,8 +138,7 @@ enum key_rotation_err libhoth_key_rotation_get_status(
102138
&request, sizeof(request), record_status, sizeof(*record_status), &rlen);
103139

104140
if (ret != 0) {
105-
fprintf(stderr, "HOTH_KEY_ROTATION_GET_STATUS error code: %d\n", ret);
106-
return KEY_ROTATION_ERR;
141+
return get_key_rotation_error(ret);
107142
}
108143

109144
if (rlen != sizeof(*record_status)) {
@@ -134,8 +169,7 @@ enum key_rotation_err libhoth_key_rotation_payload_status(
134169
&rlen);
135170

136171
if (ret != 0) {
137-
fprintf(stderr, "HOTH_KEY_ROTATION_PAYLOAD_STATUS error code: %d\n", ret);
138-
return KEY_ROTATION_ERR;
172+
return get_key_rotation_error(ret);
139173
}
140174

141175
if (rlen != sizeof(*payload_status)) {
@@ -184,8 +218,7 @@ enum key_rotation_err libhoth_key_rotation_update(struct libhoth_device* dev,
184218
0, &request, sizeof(request.hdr) + request.hdr.packet_size, NULL, 0,
185219
&response_length);
186220
if (ret != 0) {
187-
fprintf(stderr, "Error code from hoth: %d\n", ret);
188-
return KEY_ROTATION_ERR;
221+
return get_key_rotation_error(ret);
189222
}
190223
if (response_length != 0) {
191224
fprintf(stderr, "Expected exactly %d response bytes, got %ld\n", 0,
@@ -246,8 +279,7 @@ static enum key_rotation_err send_key_rotation_read_helper(
246279
&request, sizeof(request.hdr) + request_payload_size, response_data,
247280
response_buffer_size, response_length);
248281
if (ret != 0) {
249-
fprintf(stderr, "HOTH_KEY_ROTATION_READ error code: %x\n", ret);
250-
return KEY_ROTATION_ERR;
282+
return get_key_rotation_error(ret);
251283
}
252284
return KEY_ROTATION_CMD_SUCCESS;
253285
}
@@ -360,7 +392,7 @@ enum key_rotation_err libhoth_key_rotation_read_chunk_type(
360392
fprintf(stderr,
361393
"Chunk length invalid: %d Chunk length must be greater than %d\n",
362394
chunk_length, STRUCT_CHUNK_SIZE);
363-
return KEY_ROTATION_ERR;
395+
return KEY_ROTATION_INTERNAL_ERR;
364396
}
365397
if (read_size == 0) {
366398
read_size =
@@ -399,8 +431,7 @@ enum key_rotation_err libhoth_key_rotation_chunk_type_count(
399431
dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0,
400432
&request, sizeof(request), &response, sizeof(response), &rlen);
401433
if (ret != 0) {
402-
fprintf(stderr, "HOTH_KEY_ROTATION_CHUNK_TYPE_COUNT error code: %d\n", ret);
403-
return KEY_ROTATION_ERR;
434+
return get_key_rotation_error(ret);
404435
}
405436
if (rlen != sizeof(response)) {
406437
fprintf(stderr,

protocol/key_rotation.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#ifndef _LIBHOTH_PROTOCOL_KEY_ROTATION_H_
1616
#define _LIBHOTH_PROTOCOL_KEY_ROTATION_H_
1717

18-
#include <stdint.h>
1918
#include <assert.h>
19+
#include <stdint.h>
2020

2121
#include "protocol/host_cmd.h"
2222
#include "transports/libhoth_device.h"
@@ -26,6 +26,8 @@ extern "C" {
2626
#endif
2727

2828
#define HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP 0x004d
29+
#define HOTH_PRV_CMD_HAVEN_GET_CMD_VERSIONS 0x0008
30+
2931
#define KEY_ROTATION_HASH_DIGEST_SIZE 32
3032
#define KEY_ROTATION_FLASH_AREA_SIZE 2048
3133
#define KEY_ROTATION_MAX_RECORD_SIZE \
@@ -48,12 +50,13 @@ extern "C" {
4850

4951
enum key_rotation_err {
5052
KEY_ROTATION_CMD_SUCCESS = 0,
51-
KEY_ROTATION_ERR,
53+
KEY_ROTATION_INTERNAL_ERR,
5254
KEY_ROTATION_ERR_INVALID_PARAM,
53-
KEY_ROTATION_ERR_UNIMPLEMENTED,
5455
KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE,
5556
KEY_ROTATION_INITIATE_FAIL,
5657
KEY_ROTATION_COMMIT_FAIL,
58+
KEY_ROTATION_ROOT_OF_TRUST_UNAVAILABLE,
59+
KEY_ROTATION_ERR_HOTH_BASE = 1000,
5760
};
5861

5962
enum key_rotation_record_read_half {

0 commit comments

Comments
 (0)