From b419a3b50a9b514eae15ef70cacb269c8eb9253d Mon Sep 17 00:00:00 2001 From: Ellis Sarza-Nguyen Date: Sat, 29 Aug 2026 15:36:49 -0700 Subject: [PATCH] [protocol] Convert key_rotation to use the new compact error format Signed-off-by: Ellis Sarza-Nguyen --- examples/htool_key_rotation.c | 77 ++++----- protocol/BUILD | 2 + protocol/key_rotation.c | 314 ++++++++++++++++++++-------------- protocol/key_rotation.h | 40 ++--- protocol/key_rotation_test.cc | 206 ++++++++++++++-------- 5 files changed, 372 insertions(+), 267 deletions(-) diff --git a/examples/htool_key_rotation.c b/examples/htool_key_rotation.c index f8482c6..c77ef51 100644 --- a/examples/htool_key_rotation.c +++ b/examples/htool_key_rotation.c @@ -1,19 +1,18 @@ // Copyright 2025 Google LLC - +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at - +// // https://www.apache.org/licenses/LICENSE-2.0 - +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and -// limitations under the License.guage governing permissions and // limitations under the License. -#include "htool_key_rotation.h" +#include "examples/htool_key_rotation.h" #include #include @@ -26,9 +25,10 @@ #include #include -#include "htool.h" -#include "htool_cmd.h" +#include "examples/htool.h" +#include "examples/htool_cmd.h" #include "protocol/key_rotation.h" +#include "protocol/status.h" static const char* get_validation_method_string(uint32_t validation_method) { switch (validation_method) { @@ -50,9 +50,9 @@ int htool_key_rotation_get_status(const struct htool_invocation* inv) { return -1; } struct hoth_response_key_rotation_status status; - enum key_rotation_err ret = libhoth_key_rotation_get_status(dev, &status); - if (ret) { - fprintf(stderr, "Failed to get key rotation status\n"); + libhoth_error ret = libhoth_key_rotation_get_status(dev, &status); + if (ret != HOTH_SUCCESS) { + htool_report_error("key_rotation get_status", ret); return -1; } @@ -73,9 +73,9 @@ int htool_key_rotation_get_version(const struct htool_invocation* inv) { return -1; } struct hoth_response_key_rotation_record_version version; - enum key_rotation_err ret = libhoth_key_rotation_get_version(dev, &version); - if (ret) { - fprintf(stderr, "Failed to get key rotation version\n"); + libhoth_error ret = libhoth_key_rotation_get_version(dev, &version); + if (ret != HOTH_SUCCESS) { + htool_report_error("key_rotation get_version", ret); return -1; } printf("version : %u\n", version.version); @@ -148,10 +148,10 @@ int htool_key_rotation_update(const struct htool_invocation* inv) { return result; } - enum key_rotation_err key_ret = libhoth_key_rotation_update(dev, image, size); - if (key_ret) { - fprintf(stderr, "Failed to update key rotation record\n"); - result = key_ret; + libhoth_error key_ret = libhoth_key_rotation_update(dev, image, size); + if (key_ret != HOTH_SUCCESS) { + htool_report_error("key_rotation update", key_ret); + result = -1; } if (image != MAP_FAILED) { @@ -170,10 +170,9 @@ int htool_key_rotation_payload_status(const struct htool_invocation* inv) { return -1; } struct hoth_response_key_rotation_payload_status payload_status; - enum key_rotation_err ret = - libhoth_key_rotation_payload_status(dev, &payload_status); - if (ret) { - fprintf(stderr, "Failed to get key rotation payload status\n"); + libhoth_error ret = libhoth_key_rotation_payload_status(dev, &payload_status); + if (ret != HOTH_SUCCESS) { + htool_report_error("key_rotation payload_status", ret); return -1; } printf("validation_method : %s\n", @@ -246,10 +245,10 @@ int htool_key_rotation_read(const struct htool_invocation* inv) { } struct hoth_response_key_rotation_record_read read_response; - enum key_rotation_err ret_read = + libhoth_error ret_read = libhoth_key_rotation_read(dev, offset, size, read_half, &read_response); - if (ret_read) { - fprintf(stderr, "Failed to read key rotation record\n"); + if (ret_read != HOTH_SUCCESS) { + htool_report_error("key_rotation read", ret_read); if (fd != -1) { close(fd); } @@ -344,11 +343,11 @@ int htool_key_rotation_read_chunk_type(const struct htool_invocation* inv) { } uint16_t response_size = 0; struct hoth_response_key_rotation_record_read read_response; - enum key_rotation_err ret_read = libhoth_key_rotation_read_chunk_type( + libhoth_error ret_read = libhoth_key_rotation_read_chunk_type( dev, chunk_typecode, chunk_index, offset, size, &read_response, &response_size); - if (ret_read) { - fprintf(stderr, "Failed to read chunk from key rotation record\n"); + if (ret_read != HOTH_SUCCESS) { + htool_report_error("key_rotation read_chunk_type", ret_read); return -1; } if (size == 0) { @@ -401,10 +400,10 @@ int htool_key_rotation_chunk_type_count(const struct htool_invocation* inv) { return -1; } uint16_t chunk_count = 0; - enum key_rotation_err ret_count = + libhoth_error ret_count = libhoth_key_rotation_chunk_type_count(dev, chunk_typecode, &chunk_count); - if (ret_count) { - fprintf(stderr, "Failed to get chunk type count\n"); + if (ret_count != HOTH_SUCCESS) { + htool_report_error("key_rotation chunk_type_count", ret_count); return -1; } printf("chunk_count: %u\n", chunk_count); @@ -416,9 +415,9 @@ int htool_key_rotation_erase_record(const struct htool_invocation* inv) { if (!dev) { return -1; } - enum key_rotation_err ret = libhoth_key_rotation_erase_record(dev); - if (ret) { - fprintf(stderr, "Failed to erase key rotation record\n"); + libhoth_error ret = libhoth_key_rotation_erase_record(dev); + if (ret != HOTH_SUCCESS) { + htool_report_error("key_rotation erase_record", ret); return -1; } printf("Key rotation record erased successfully\n"); @@ -434,9 +433,9 @@ int htool_key_rotation_set_mauv(const struct htool_invocation* inv) { if (htool_get_param_u32(inv, "mauv", &mauv)) { return -1; } - enum key_rotation_err ret = libhoth_key_rotation_set_mauv(dev, mauv); - if (ret) { - fprintf(stderr, "Failed to set key rotation MAUV\n"); + libhoth_error ret = libhoth_key_rotation_set_mauv(dev, mauv); + if (ret != HOTH_SUCCESS) { + htool_report_error("key_rotation set_mauv", ret); return -1; } printf("Key rotation MAUV set successfully\n"); @@ -449,9 +448,9 @@ int htool_key_rotation_get_mauv(const struct htool_invocation* inv) { return -1; } struct hoth_response_key_rotation_mauv mauv; - enum key_rotation_err ret = libhoth_key_rotation_get_mauv(dev, &mauv); - if (ret) { - fprintf(stderr, "Failed to get key rotation MAUV\n"); + libhoth_error ret = libhoth_key_rotation_get_mauv(dev, &mauv); + if (ret != HOTH_SUCCESS) { + htool_report_error("key_rotation get_mauv", ret); return -1; } printf("Key rotation MAUV: %u\n", mauv.mauv); diff --git a/protocol/BUILD b/protocol/BUILD index 5094ad2..da0f0b3 100644 --- a/protocol/BUILD +++ b/protocol/BUILD @@ -495,6 +495,7 @@ cc_library( hdrs = ["key_rotation.h"], deps = [ ":host_cmd", + ":libhoth_status", "//transports:libhoth_device", ], ) @@ -505,6 +506,7 @@ cc_test( deps = [ ":host_cmd", ":key_rotation", + ":libhoth_status", "//protocol/test:libhoth_device_mock", "//transports:libhoth_device", "@googletest//:gtest", diff --git a/protocol/key_rotation.c b/protocol/key_rotation.c index 6c02e73..a45df0b 100644 --- a/protocol/key_rotation.c +++ b/protocol/key_rotation.c @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "key_rotation.h" +#include "protocol/key_rotation.h" #include #include @@ -20,6 +20,7 @@ #include #include "protocol/host_cmd.h" +#include "protocol/status.h" #include "transports/libhoth_device.h" struct hoth_request_variable_length { @@ -27,8 +28,12 @@ struct hoth_request_variable_length { uint8_t data[KEY_ROTATION_RECORD_WRITE_MAX_SIZE]; } __hoth_align4; -static enum key_rotation_err send_key_rotation_request( - struct libhoth_device* dev, uint16_t command) { +static libhoth_error send_key_rotation_request(struct libhoth_device* dev, + uint16_t command) { + if (dev == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } const struct hoth_request_key_rotation_record request = { .operation = command, .packet_offset = 0, @@ -36,27 +41,32 @@ static enum key_rotation_err send_key_rotation_request( .reserved = 0, }; size_t rlen = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request), NULL, 0, &rlen); - if (ret != 0) { - fprintf(stderr, "HOTH_KEY_ROTATION_COMMAND %d error code: %d\n", command, - ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_KEY_ROTATION_COMMAND %d error code: 0x%016llx\n", + command, (unsigned long long)ret); + return ret; } if (rlen != 0) { fprintf(stderr, "HOTH_KEY_ROTATION_COMMAND: %d expected exactly %d response " - "bytes, got %ld\n", + "bytes, got %zu\n", command, 0, rlen); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_get_version( +libhoth_error libhoth_key_rotation_get_version( struct libhoth_device* dev, struct hoth_response_key_rotation_record_version* record_version) { + if (dev == NULL || record_version == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } const struct hoth_request_key_rotation_record request = { .operation = KEY_ROTATION_RECORD_GET_VERSION, .packet_offset = 0, @@ -65,30 +75,36 @@ enum key_rotation_err libhoth_key_rotation_get_version( }; size_t rlen = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request), record_version, sizeof(*record_version), &rlen); - if (ret != 0) { - fprintf(stderr, "HOTH_KEY_ROTATION_GET_VERSION error code: %d\n", ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_KEY_ROTATION_GET_VERSION error code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } if (rlen != sizeof(*record_version)) { fprintf(stderr, - "HOTH_KEY_ROTATION_GET_VERSION expected exactly %ld response " - "bytes, got %ld\n", + "HOTH_KEY_ROTATION_GET_VERSION expected exactly %zu response " + "bytes, got %zu\n", sizeof(*record_version), rlen); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_get_status( +libhoth_error libhoth_key_rotation_get_status( struct libhoth_device* dev, struct hoth_response_key_rotation_status* record_status) { + if (dev == NULL || record_status == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } const struct hoth_request_key_rotation_record request = { .operation = KEY_ROTATION_RECORD_GET_STATUS, .packet_offset = 0, @@ -97,29 +113,35 @@ enum key_rotation_err libhoth_key_rotation_get_status( }; size_t rlen = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request), record_status, sizeof(*record_status), &rlen); - if (ret != 0) { - fprintf(stderr, "HOTH_KEY_ROTATION_GET_STATUS error code: %d\n", ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_KEY_ROTATION_GET_STATUS error code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } if (rlen != sizeof(*record_status)) { fprintf(stderr, - "HOTH_KEY_ROTATION_GET_STATUS expected exactly %ld response " - "bytes, got %ld\n", + "HOTH_KEY_ROTATION_GET_STATUS expected exactly %zu response " + "bytes, got %zu\n", sizeof(*record_status), rlen); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_payload_status( +libhoth_error libhoth_key_rotation_payload_status( struct libhoth_device* dev, struct hoth_response_key_rotation_payload_status* payload_status) { + if (dev == NULL || payload_status == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } const struct hoth_request_key_rotation_record request = { .operation = KEY_ROTATION_RECORD_PAYLOAD_STATUS, .packet_offset = 0, @@ -128,43 +150,47 @@ enum key_rotation_err libhoth_key_rotation_payload_status( }; size_t rlen = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request), payload_status, sizeof(*payload_status), &rlen); - if (ret != 0) { - fprintf(stderr, "HOTH_KEY_ROTATION_PAYLOAD_STATUS error code: %d\n", ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_KEY_ROTATION_PAYLOAD_STATUS error code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } if (rlen != sizeof(*payload_status)) { fprintf(stderr, - "HOTH_KEY_ROTATION_PAYLOAD_STATUS expected exactly %ld response " - "bytes, got %ld\n", + "HOTH_KEY_ROTATION_PAYLOAD_STATUS expected exactly %zu response " + "bytes, got %zu\n", sizeof(*payload_status), rlen); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_update(struct libhoth_device* dev, - const uint8_t* image, - size_t size) { - if (size <= KEY_ROTATION_RECORD_SIGNATURE_SIZE) { - fprintf(stderr, "Data chunk size invalid.\n"); - return KEY_ROTATION_ERR_INVALID_PARAM; +libhoth_error libhoth_key_rotation_update(struct libhoth_device* dev, + const uint8_t* image, size_t size) { + if (dev == NULL || image == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } - if (size > KEY_ROTATION_FLASH_AREA_SIZE) { + if (size <= KEY_ROTATION_RECORD_SIGNATURE_SIZE || + size > KEY_ROTATION_FLASH_AREA_SIZE) { fprintf(stderr, "Data chunk size invalid.\n"); - return KEY_ROTATION_ERR_INVALID_PARAM; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } fprintf(stderr, "Initiating key rotation update protocol with libhoth.\n"); - if (send_key_rotation_request(dev, KEY_ROTATION_RECORD_INITIATE) != - KEY_ROTATION_CMD_SUCCESS) { + libhoth_error init_err = + send_key_rotation_request(dev, KEY_ROTATION_RECORD_INITIATE); + if (init_err != HOTH_SUCCESS) { fprintf(stderr, "Failed to initiate key rotation.\n"); - return KEY_ROTATION_INITIATE_FAIL; + return init_err; } fprintf(stderr, "Writing the image to hoth.\n"); struct hoth_request_variable_length request; @@ -178,38 +204,45 @@ enum key_rotation_err libhoth_key_rotation_update(struct libhoth_device* dev, request.hdr.packet_offset = offset; request.hdr.packet_size = size_to_send; memcpy(request.data, packet_data, size_to_send); - size_t response_length; - int ret = libhoth_hostcmd_exec( + size_t response_length = 0; + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request.hdr) + request.hdr.packet_size, NULL, 0, &response_length); - if (ret != 0) { - fprintf(stderr, "Error code from hoth: %d\n", ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "Error code from hoth: 0x%016llx\n", + (unsigned long long)ret); + return ret; } if (response_length != 0) { - fprintf(stderr, "Expected exactly %d response bytes, got %ld\n", 0, + fprintf(stderr, "Expected exactly %d response bytes, got %zu\n", 0, response_length); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } offset += size_to_send; size -= size_to_send; packet_data += size_to_send; } fprintf(stderr, "Finalizing key rotation update.\n"); - if (send_key_rotation_request(dev, KEY_ROTATION_RECORD_COMMIT) != - KEY_ROTATION_CMD_SUCCESS) { + libhoth_error commit_err = + send_key_rotation_request(dev, KEY_ROTATION_RECORD_COMMIT); + if (commit_err != HOTH_SUCCESS) { fprintf(stderr, "Failed to commit key rotation.\n"); - return KEY_ROTATION_COMMIT_FAIL; + return commit_err; } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -static enum key_rotation_err send_key_rotation_read_helper( +static libhoth_error send_key_rotation_read_helper( struct libhoth_device* dev, uint8_t operation, uint16_t offset, uint16_t size, const void* request_payload, size_t request_payload_size, size_t* response_length, uint8_t* response_data, size_t response_buffer_size) { + if (dev == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } struct hoth_request_variable_length request; request.hdr.operation = operation; request.hdr.packet_offset = offset; @@ -220,14 +253,16 @@ static enum key_rotation_err send_key_rotation_read_helper( "Request packet size larger than request size: %zu Expected less " "than %zu\n", request_payload_size, sizeof(request.data)); - return KEY_ROTATION_ERR_INVALID_PARAM; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } else if (operation == KEY_ROTATION_RECORD_READ && request_payload_size != sizeof(struct hoth_request_key_rotation_record_read)) { fprintf(stderr, "Request payload size invalid: %zu Expected %zu\n", request_payload_size, sizeof(struct hoth_request_key_rotation_record_read)); - return KEY_ROTATION_ERR_INVALID_PARAM; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } if (operation == KEY_ROTATION_RECORD_READ_CHUNK_TYPE && request_payload_size != @@ -235,30 +270,34 @@ static enum key_rotation_err send_key_rotation_read_helper( fprintf(stderr, "Request payload size invalid: %zu Expected %zu\n", request_payload_size, sizeof(struct hoth_request_key_rotation_record_read_chunk_type)); - return KEY_ROTATION_ERR_INVALID_PARAM; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } memcpy(request.data, request_payload, request_payload_size); } *response_length = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request.hdr) + request_payload_size, response_data, response_buffer_size, response_length); - if (ret != 0) { - fprintf(stderr, "HOTH_KEY_ROTATION_READ error code: %x\n", ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_KEY_ROTATION_READ error code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_read( +libhoth_error libhoth_key_rotation_read( struct libhoth_device* dev, uint16_t record_offset, uint16_t read_size, uint32_t read_half, struct hoth_response_key_rotation_record_read* read_response) { - if (read_size > KEY_ROTATION_FLASH_AREA_SIZE || read_size == 0) { + if (dev == NULL || read_response == NULL || + read_size > KEY_ROTATION_FLASH_AREA_SIZE || read_size == 0) { fprintf(stderr, "Read size invalid. Read size: %d\n", read_size); - return KEY_ROTATION_ERR_INVALID_PARAM; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } uint16_t read_offset = 0; @@ -272,47 +311,45 @@ enum key_rotation_err libhoth_key_rotation_read( stderr, "Read offset + read size invalid. Read offset: %d, read size: %d\n", read_offset, read_size); - return KEY_ROTATION_ERR_INVALID_PARAM; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } uint16_t packet_size = (read_size > KEY_ROTATION_RECORD_READ_MAX_SIZE) ? KEY_ROTATION_RECORD_READ_MAX_SIZE : read_size; size_t response_length = 0; - enum key_rotation_err err = send_key_rotation_read_helper( + libhoth_error err = send_key_rotation_read_helper( dev, KEY_ROTATION_RECORD_READ, read_offset + record_offset, packet_size, &request, sizeof(request), &response_length, &response_data[read_offset], packet_size); - if (err != KEY_ROTATION_CMD_SUCCESS) { + if (err != HOTH_SUCCESS) { return err; } if (response_length != packet_size) { fprintf(stderr, "HOTH_KEY_ROTATION_READ expected exactly %d response " - "bytes, got %ld\n", + "bytes, got %zu\n", packet_size, response_length); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } read_offset += packet_size; read_size -= packet_size; } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_read_chunk_type( +libhoth_error libhoth_key_rotation_read_chunk_type( struct libhoth_device* dev, uint32_t chunk_typecode, uint32_t chunk_index, uint16_t chunk_offset, uint16_t read_size, struct hoth_response_key_rotation_record_read* read_response, uint16_t* response_size) { - if (read_size > KEY_ROTATION_MAX_RECORD_SIZE) { - fprintf(stderr, "Read size invalid: %d Read size must be less than %d\n", - read_size, KEY_ROTATION_MAX_RECORD_SIZE); - return KEY_ROTATION_ERR_INVALID_PARAM; - } - if (chunk_offset > KEY_ROTATION_MAX_RECORD_SIZE) { - fprintf(stderr, - "Chunk offset invalid: %d Chunk offset must be less than %d\n", - chunk_offset, KEY_ROTATION_MAX_RECORD_SIZE); - return KEY_ROTATION_ERR_INVALID_PARAM; + if (dev == NULL || read_response == NULL || response_size == NULL || + read_size > KEY_ROTATION_MAX_RECORD_SIZE || + chunk_offset > KEY_ROTATION_MAX_RECORD_SIZE) { + fprintf(stderr, "Parameters invalid for key rotation read chunk type\n"); + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } struct hoth_request_key_rotation_record_read_chunk_type request = { .chunk_typecode = chunk_typecode, @@ -327,19 +364,20 @@ enum key_rotation_err libhoth_key_rotation_read_chunk_type( stderr, "Read offset + read size invalid. Read offset: %d, read size: %d\n", read_offset, read_size); - return KEY_ROTATION_ERR_INVALID_PARAM; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); } uint16_t packet_size = (read_size > KEY_ROTATION_RECORD_READ_CHUNK_TYPE_MAX_SIZE) ? KEY_ROTATION_RECORD_READ_CHUNK_TYPE_MAX_SIZE : read_size; size_t response_length = 0; - enum key_rotation_err err = send_key_rotation_read_helper( + libhoth_error err = send_key_rotation_read_helper( dev, KEY_ROTATION_RECORD_READ_CHUNK_TYPE, read_offset + chunk_offset, packet_size, &request, sizeof(request), &response_length, &response[read_offset], KEY_ROTATION_RECORD_READ_CHUNK_TYPE_MAX_SIZE + sizeof(uint32_t)); - if (err != KEY_ROTATION_CMD_SUCCESS) { + if (err != HOTH_SUCCESS) { return err; } // The last 4 bytes of the response is the chunk size. This is used to @@ -347,9 +385,10 @@ enum key_rotation_err libhoth_key_rotation_read_chunk_type( if (response_length < sizeof(uint32_t)) { fprintf(stderr, "Unexpected host command response size. Expecting " - "non-zero; Got %lu\n", + "non-zero; Got %zu\n", response_length); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } response_length -= sizeof(uint32_t); memcpy(&chunk_length, &response[response_length], sizeof(uint16_t)); @@ -360,7 +399,8 @@ enum key_rotation_err libhoth_key_rotation_read_chunk_type( fprintf(stderr, "Chunk length invalid: %d Chunk length must be greater than %d\n", chunk_length, STRUCT_CHUNK_SIZE); - return KEY_ROTATION_ERR; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } if (read_size == 0) { read_size = @@ -371,19 +411,23 @@ enum key_rotation_err libhoth_key_rotation_read_chunk_type( } if (response_length != packet_size) { fprintf(stderr, - "Unexpected host command response size. Expecting %u; Got %lu\n", + "Unexpected host command response size. Expecting %u; Got %zu\n", packet_size, response_length); } read_offset += packet_size; read_size -= packet_size; } while (read_size > 0); *response_size = read_offset; - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_chunk_type_count( - struct libhoth_device* dev, uint32_t chunk_typecode, - uint16_t* chunk_count) { +libhoth_error libhoth_key_rotation_chunk_type_count(struct libhoth_device* dev, + uint32_t chunk_typecode, + uint16_t* chunk_count) { + if (dev == NULL || chunk_count == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } struct hoth_request_variable_length request; request.hdr.operation = KEY_ROTATION_RECORD_CHUNK_TYPE_COUNT; request.hdr.packet_offset = 0; @@ -395,31 +439,37 @@ enum key_rotation_err libhoth_key_rotation_chunk_type_count( request_chunk_type_count->chunk_typecode = chunk_typecode; uint32_t response = 0; size_t rlen = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request), &response, sizeof(response), &rlen); - if (ret != 0) { - fprintf(stderr, "HOTH_KEY_ROTATION_CHUNK_TYPE_COUNT error code: %d\n", ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, + "HOTH_KEY_ROTATION_CHUNK_TYPE_COUNT error code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } if (rlen != sizeof(response)) { fprintf(stderr, - "HOTH_KEY_ROTATION_CHUNK_TYPE_COUNT expected exactly %ld response " - "bytes, got %ld\n", + "HOTH_KEY_ROTATION_CHUNK_TYPE_COUNT expected exactly %zu response " + "bytes, got %zu\n", sizeof(response), rlen); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } *chunk_count = response; - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_erase_record( - struct libhoth_device* dev) { +libhoth_error libhoth_key_rotation_erase_record(struct libhoth_device* dev) { return send_key_rotation_request(dev, KEY_ROTATION_RECORD_ERASE_RECORD); } -enum key_rotation_err libhoth_key_rotation_set_mauv(struct libhoth_device* dev, - uint32_t mauv) { +libhoth_error libhoth_key_rotation_set_mauv(struct libhoth_device* dev, + uint32_t mauv) { + if (dev == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } struct hoth_request_variable_length request; request.hdr.operation = KEY_ROTATION_RECORD_SET_MAUV; request.hdr.packet_offset = 0; @@ -428,28 +478,34 @@ enum key_rotation_err libhoth_key_rotation_set_mauv(struct libhoth_device* dev, (struct hoth_request_key_rotation_record_set_mauv*)&(request.data); request_set_mauv->mauv = mauv; size_t rlen = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request.hdr) + sizeof(struct hoth_request_key_rotation_record_set_mauv), NULL, 0, &rlen); - if (ret != 0) { - fprintf(stderr, "HOTH_KEY_ROTATION_SET_MAUV error code: %d\n", ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_KEY_ROTATION_SET_MAUV error code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } if (rlen != 0) { fprintf(stderr, "HOTH_KEY_ROTATION_SET_MAUV expected exactly %d response " - "bytes, got %ld\n", + "bytes, got %zu\n", 0, rlen); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } -enum key_rotation_err libhoth_key_rotation_get_mauv( +libhoth_error libhoth_key_rotation_get_mauv( struct libhoth_device* dev, struct hoth_response_key_rotation_mauv* mauv) { + if (dev == NULL || mauv == NULL) { + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_INVALID_PARAMETER); + } const struct hoth_request_key_rotation_record request = { .operation = KEY_ROTATION_RECORD_GET_MAUV, .packet_offset = 0, @@ -458,22 +514,24 @@ enum key_rotation_err libhoth_key_rotation_get_mauv( }; size_t rlen = 0; - int ret = libhoth_hostcmd_exec( + libhoth_error ret = libhoth_hostcmd_exec_v2( dev, HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HAVEN_KEY_ROTATION_OP, 0, &request, sizeof(request), mauv, sizeof(*mauv), &rlen); - if (ret != 0) { - fprintf(stderr, "HOTH_KEY_ROTATION_GET_MAUV error code: %d\n", ret); - return KEY_ROTATION_ERR; + if (ret != HOTH_SUCCESS) { + fprintf(stderr, "HOTH_KEY_ROTATION_GET_MAUV error code: 0x%016llx\n", + (unsigned long long)ret); + return ret; } if (rlen != sizeof(*mauv)) { fprintf(stderr, - "HOTH_KEY_ROTATION_GET_MAUV expected exactly %ld response " - "bytes, got %ld\n", + "HOTH_KEY_ROTATION_GET_MAUV expected exactly %zu response " + "bytes, got %zu\n", sizeof(*mauv), rlen); - return KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE; + return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH, + LIBHOTH_ERR_FAIL); } - return KEY_ROTATION_CMD_SUCCESS; + return HOTH_SUCCESS; } diff --git a/protocol/key_rotation.h b/protocol/key_rotation.h index bc4c3e9..63a5a6e 100644 --- a/protocol/key_rotation.h +++ b/protocol/key_rotation.h @@ -19,6 +19,7 @@ #include #include "protocol/host_cmd.h" +#include "protocol/status.h" #include "transports/libhoth_device.h" #ifdef __cplusplus @@ -46,16 +47,6 @@ extern "C" { #define KEY_ROTATION_RECORD_SIGNATURE_SIZE 96 #define STRUCT_CHUNK_SIZE 8 -enum key_rotation_err { - KEY_ROTATION_CMD_SUCCESS = 0, - KEY_ROTATION_ERR, - KEY_ROTATION_ERR_INVALID_PARAM, - KEY_ROTATION_ERR_UNIMPLEMENTED, - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE, - KEY_ROTATION_INITIATE_FAIL, - KEY_ROTATION_COMMIT_FAIL, -}; - enum key_rotation_record_read_half { KEY_ROTATION_RECORD_READ_HALF_ACTIVE = 0, KEY_ROTATION_RECORD_READ_HALF_STAGING = 1, @@ -173,34 +164,33 @@ struct bios_allowed_hash_list { sha256 hash_list[]; // only support sha256 hash for bios }; -enum key_rotation_err libhoth_key_rotation_get_version( +libhoth_error libhoth_key_rotation_get_version( struct libhoth_device* dev, struct hoth_response_key_rotation_record_version* record_version); -enum key_rotation_err libhoth_key_rotation_get_status( +libhoth_error libhoth_key_rotation_get_status( struct libhoth_device* dev, struct hoth_response_key_rotation_status* record_status); -enum key_rotation_err libhoth_key_rotation_payload_status( +libhoth_error libhoth_key_rotation_payload_status( struct libhoth_device* dev, struct hoth_response_key_rotation_payload_status* payload_status); -enum key_rotation_err libhoth_key_rotation_update(struct libhoth_device* dev, - const uint8_t* image, - size_t size); -enum key_rotation_err libhoth_key_rotation_read( +libhoth_error libhoth_key_rotation_update(struct libhoth_device* dev, + const uint8_t* image, size_t size); +libhoth_error libhoth_key_rotation_read( struct libhoth_device* dev, uint16_t offset, uint16_t size, uint32_t read_half, struct hoth_response_key_rotation_record_read* read_response); -enum key_rotation_err libhoth_key_rotation_read_chunk_type( +libhoth_error libhoth_key_rotation_read_chunk_type( struct libhoth_device* dev, uint32_t chunk_typecode, uint32_t chunk_index, uint16_t offset, uint16_t size, struct hoth_response_key_rotation_record_read* read_response, uint16_t* response_size); -enum key_rotation_err libhoth_key_rotation_chunk_type_count( - struct libhoth_device* dev, uint32_t chunk_typecode, uint16_t* chunk_count); -enum key_rotation_err libhoth_key_rotation_erase_record( - struct libhoth_device* dev); -enum key_rotation_err libhoth_key_rotation_set_mauv(struct libhoth_device* dev, - uint32_t mauv); -enum key_rotation_err libhoth_key_rotation_get_mauv( +libhoth_error libhoth_key_rotation_chunk_type_count(struct libhoth_device* dev, + uint32_t chunk_typecode, + uint16_t* chunk_count); +libhoth_error libhoth_key_rotation_erase_record(struct libhoth_device* dev); +libhoth_error libhoth_key_rotation_set_mauv(struct libhoth_device* dev, + uint32_t mauv); +libhoth_error libhoth_key_rotation_get_mauv( struct libhoth_device* dev, struct hoth_response_key_rotation_mauv* mauv); #ifdef __cplusplus } diff --git a/protocol/key_rotation_test.cc b/protocol/key_rotation_test.cc index 3f1e441..7157073 100644 --- a/protocol/key_rotation_test.cc +++ b/protocol/key_rotation_test.cc @@ -23,6 +23,7 @@ #include #include "protocol/host_cmd.h" +#include "protocol/status.h" #include "test/libhoth_device_mock.h" #include "transports/libhoth_device.h" @@ -75,7 +76,7 @@ TEST_F(LibHothTest, key_rotation_get_version_success) { struct hoth_response_key_rotation_record_version actual_response; EXPECT_EQ(libhoth_key_rotation_get_version(&hoth_dev_, &actual_response), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); EXPECT_EQ(actual_response.version, kDefaultVersion.version); } @@ -86,8 +87,8 @@ TEST_F(LibHothTest, key_rotation_get_version_failure_io) { EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); struct hoth_response_key_rotation_record_version actual_response; - EXPECT_EQ(libhoth_key_rotation_get_version(&hoth_dev_, &actual_response), - KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_get_version(&hoth_dev_, &actual_response), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_get_version_failure_wrong_size) { @@ -99,8 +100,8 @@ TEST_F(LibHothTest, key_rotation_get_version_failure_wrong_size) { Return(LIBHOTH_OK))); struct hoth_response_key_rotation_record_version actual_response; - EXPECT_EQ(libhoth_key_rotation_get_version(&hoth_dev_, &actual_response), - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE); + EXPECT_NE(libhoth_key_rotation_get_version(&hoth_dev_, &actual_response), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_get_status_success) { @@ -109,11 +110,11 @@ TEST_F(LibHothTest, key_rotation_get_status_success) { EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDefaultStatus, sizeof(kDefaultStatus)), - Return(KEY_ROTATION_CMD_SUCCESS))); + Return(LIBHOTH_OK))); struct hoth_response_key_rotation_status actual_status; EXPECT_EQ(libhoth_key_rotation_get_status(&hoth_dev_, &actual_status), - LIBHOTH_OK); + HOTH_SUCCESS); EXPECT_EQ(actual_status.version, kDefaultStatus.version); EXPECT_EQ(actual_status.image_family, kDefaultStatus.image_family); EXPECT_EQ(actual_status.image_family_variant, @@ -132,8 +133,8 @@ TEST_F(LibHothTest, key_rotation_get_status_failure_io) { EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); struct hoth_response_key_rotation_status actual_status; - EXPECT_EQ(libhoth_key_rotation_get_status(&hoth_dev_, &actual_status), - KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_get_status(&hoth_dev_, &actual_status), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_get_status_failure_wrong_size) { @@ -145,8 +146,8 @@ TEST_F(LibHothTest, key_rotation_get_status_failure_wrong_size) { Return(LIBHOTH_OK))); struct hoth_response_key_rotation_status actual_status; - EXPECT_EQ(libhoth_key_rotation_get_status(&hoth_dev_, &actual_status), - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE); + EXPECT_NE(libhoth_key_rotation_get_status(&hoth_dev_, &actual_status), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_payload_status_success) { @@ -161,7 +162,7 @@ TEST_F(LibHothTest, key_rotation_payload_status_success) { struct hoth_response_key_rotation_payload_status actual_payload_status; EXPECT_EQ( libhoth_key_rotation_payload_status(&hoth_dev_, &actual_payload_status), - LIBHOTH_OK); + HOTH_SUCCESS); EXPECT_EQ(actual_payload_status.validation_method, kDefaultPayloadStatus.validation_method); EXPECT_EQ(actual_payload_status.validation_key_data, @@ -177,9 +178,9 @@ TEST_F(LibHothTest, key_rotation_payload_status_failure_io) { EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); struct hoth_response_key_rotation_payload_status actual_payload_status; - EXPECT_EQ( + EXPECT_NE( libhoth_key_rotation_payload_status(&hoth_dev_, &actual_payload_status), - KEY_ROTATION_ERR); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_payload_status_failure_wrong_size) { @@ -192,46 +193,51 @@ TEST_F(LibHothTest, key_rotation_payload_status_failure_wrong_size) { Return(LIBHOTH_OK))); struct hoth_response_key_rotation_payload_status actual_payload_status; - EXPECT_EQ( + EXPECT_NE( libhoth_key_rotation_payload_status(&hoth_dev_, &actual_payload_status), - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE); + HOTH_SUCCESS); } -TEST_F(LibHothTest, key_rotation_read_success) { +TEST_F(LibHothTest, key_rotation_read_success_single_packet) { EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) .WillOnce(Return(LIBHOTH_OK)); + EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDefaultReadResponse, 8), Return(LIBHOTH_OK))); + struct hoth_response_key_rotation_record_read actual_read_response; EXPECT_EQ(libhoth_key_rotation_read(&hoth_dev_, 0, 8, KEY_ROTATION_RECORD_READ_HALF_ACTIVE, &actual_read_response), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); EXPECT_EQ(memcmp(actual_read_response.data, kDefaultReadResponse.data, 8), 0); } -TEST_F(LibHothTest, key_rotation_read_max_size_success) { +TEST_F(LibHothTest, key_rotation_read_success_max_packet) { uint8_t data[KEY_ROTATION_RECORD_READ_MAX_SIZE] = {0}; fill_with_data(data, KEY_ROTATION_RECORD_READ_MAX_SIZE); EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) .WillOnce(Return(LIBHOTH_OK)); + EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&data, KEY_ROTATION_RECORD_READ_MAX_SIZE), Return(LIBHOTH_OK))); + struct hoth_response_key_rotation_record_read actual_read_response; EXPECT_EQ(libhoth_key_rotation_read( &hoth_dev_, 0, KEY_ROTATION_RECORD_READ_MAX_SIZE, KEY_ROTATION_RECORD_READ_HALF_ACTIVE, &actual_read_response), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); EXPECT_EQ(memcmp(actual_read_response.data, data, KEY_ROTATION_RECORD_READ_MAX_SIZE), 0); } -TEST_F(LibHothTest, key_rotation_read_record_size_success) { +TEST_F(LibHothTest, key_rotation_read_success_multiple_packets) { uint8_t data[KEY_ROTATION_MAX_RECORD_SIZE] = {0}; fill_with_data(data, KEY_ROTATION_MAX_RECORD_SIZE); EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) + .Times(3) .WillRepeatedly(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&data, KEY_ROTATION_RECORD_READ_MAX_SIZE), @@ -241,19 +247,21 @@ TEST_F(LibHothTest, key_rotation_read_record_size_success) { Return(LIBHOTH_OK))) .WillOnce(DoAll(CopyResp(&data[2 * KEY_ROTATION_RECORD_READ_MAX_SIZE], 8), Return(LIBHOTH_OK))); + struct hoth_response_key_rotation_record_read actual_read_response; EXPECT_EQ(libhoth_key_rotation_read( &hoth_dev_, 0, KEY_ROTATION_MAX_RECORD_SIZE, KEY_ROTATION_RECORD_READ_HALF_ACTIVE, &actual_read_response), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); EXPECT_EQ( memcmp(actual_read_response.data, data, KEY_ROTATION_MAX_RECORD_SIZE), 0); } -TEST_F(LibHothTest, key_rotation_read_flash_size_success) { +TEST_F(LibHothTest, key_rotation_read_success_max_size) { uint8_t data[KEY_ROTATION_FLASH_AREA_SIZE] = {0}; fill_with_data(data, KEY_ROTATION_FLASH_AREA_SIZE); EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) + .Times(3) .WillRepeatedly(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&data, KEY_ROTATION_RECORD_READ_MAX_SIZE), @@ -264,11 +272,12 @@ TEST_F(LibHothTest, key_rotation_read_flash_size_success) { .WillOnce( DoAll(CopyResp(&data[2 * KEY_ROTATION_RECORD_READ_MAX_SIZE], 40), Return(LIBHOTH_OK))); + struct hoth_response_key_rotation_record_read actual_read_response; EXPECT_EQ(libhoth_key_rotation_read( &hoth_dev_, 0, KEY_ROTATION_FLASH_AREA_SIZE, KEY_ROTATION_RECORD_READ_HALF_ACTIVE, &actual_read_response), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); EXPECT_EQ( memcmp(actual_read_response.data, data, KEY_ROTATION_FLASH_AREA_SIZE), 0); } @@ -276,42 +285,44 @@ TEST_F(LibHothTest, key_rotation_read_flash_size_success) { TEST_F(LibHothTest, key_rotation_read_failure_io) { EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) .WillOnce(Return(LIBHOTH_OK)); + EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); struct hoth_response_key_rotation_record_read actual_read_response; - EXPECT_EQ(libhoth_key_rotation_read(&hoth_dev_, 0, 8, + EXPECT_NE(libhoth_key_rotation_read(&hoth_dev_, 0, 8, KEY_ROTATION_RECORD_READ_HALF_ACTIVE, &actual_read_response), - KEY_ROTATION_ERR); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_read_failure_wrong_size) { EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) .WillOnce(Return(LIBHOTH_OK)); + EXPECT_CALL(mock_, receive) - .WillOnce( - DoAll(CopyResp(&kDefaultReadResponse, 8 - 1), Return(LIBHOTH_OK))); + .WillOnce(DoAll(CopyResp(&kDefaultReadResponse, 7), Return(LIBHOTH_OK))); + struct hoth_response_key_rotation_record_read actual_read_response; - EXPECT_EQ(libhoth_key_rotation_read(&hoth_dev_, 0, 8, + EXPECT_NE(libhoth_key_rotation_read(&hoth_dev_, 0, 8, KEY_ROTATION_RECORD_READ_HALF_ACTIVE, &actual_read_response), - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_read_failure_invalid_size) { struct hoth_response_key_rotation_record_read actual_read_response; - EXPECT_EQ(libhoth_key_rotation_read(&hoth_dev_, 0, 0, + EXPECT_NE(libhoth_key_rotation_read(&hoth_dev_, 0, 0, KEY_ROTATION_RECORD_READ_HALF_ACTIVE, &actual_read_response), - KEY_ROTATION_ERR_INVALID_PARAM); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_read_failure_invalid_size_too_large) { struct hoth_response_key_rotation_record_read actual_read_response; - EXPECT_EQ(libhoth_key_rotation_read( + EXPECT_NE(libhoth_key_rotation_read( &hoth_dev_, 0, KEY_ROTATION_FLASH_AREA_SIZE + 1, KEY_ROTATION_RECORD_READ_HALF_ACTIVE, &actual_read_response), - KEY_ROTATION_ERR_INVALID_PARAM); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_update_success) { @@ -323,7 +334,7 @@ TEST_F(LibHothTest, key_rotation_update_success) { EXPECT_EQ(libhoth_key_rotation_update(&hoth_dev_, &data[0], KEY_ROTATION_MAX_RECORD_SIZE), - LIBHOTH_OK); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_update_failure) { @@ -333,8 +344,8 @@ TEST_F(LibHothTest, key_rotation_update_failure) { EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(-1))); - EXPECT_EQ(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), - KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_update_initiate_failure) { @@ -344,8 +355,8 @@ TEST_F(LibHothTest, key_rotation_update_initiate_failure) { .WillRepeatedly(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive).WillOnce(Return(-1)); - EXPECT_EQ(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), - KEY_ROTATION_INITIATE_FAIL); + EXPECT_NE(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_update_commit_failure) { @@ -356,20 +367,20 @@ TEST_F(LibHothTest, key_rotation_update_commit_failure) { .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(-1))); - EXPECT_EQ(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), - KEY_ROTATION_COMMIT_FAIL); + EXPECT_NE(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_update_failure_invalid_size_too_large) { uint8_t data[KEY_ROTATION_FLASH_AREA_SIZE + 1] = {0}; - EXPECT_EQ(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), - KEY_ROTATION_ERR_INVALID_PARAM); + EXPECT_NE(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_update_failure_invalid_size_too_small) { uint8_t data[KEY_ROTATION_RECORD_SIGNATURE_SIZE - 1] = {0}; - EXPECT_EQ(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), - KEY_ROTATION_ERR_INVALID_PARAM); + EXPECT_NE(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_update_failure_invalid_response_size) { @@ -379,8 +390,8 @@ TEST_F(LibHothTest, key_rotation_update_failure_invalid_response_size) { EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))) .WillOnce(DoAll(CopyResp(&kDummy, 2), Return(LIBHOTH_OK))); - EXPECT_EQ(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), - KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_update(&hoth_dev_, &data[0], sizeof(data)), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_read_chunk_type_success) { @@ -398,7 +409,7 @@ TEST_F(LibHothTest, key_rotation_read_chunk_type_success) { &hoth_dev_, kCmdRotPublicKey, 0, 0, KEY_ROTATION_RECORD_READ_CHUNK_TYPE_MAX_SIZE, &actual_read_response, &response_size), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); EXPECT_EQ(memcmp(actual_read_response.data, data, KEY_ROTATION_RECORD_READ_CHUNK_TYPE_MAX_SIZE), 0); @@ -411,21 +422,21 @@ TEST_F(LibHothTest, key_rotation_read_chunk_type_failure_io) { EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); struct hoth_response_key_rotation_record_read actual_read_response; uint16_t response_size = 0; - EXPECT_EQ(libhoth_key_rotation_read_chunk_type( + EXPECT_NE(libhoth_key_rotation_read_chunk_type( &hoth_dev_, kCmdRotPublicKey, 0, 0, KEY_ROTATION_RECORD_READ_CHUNK_TYPE_MAX_SIZE, &actual_read_response, &response_size), - KEY_ROTATION_ERR); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_read_chunk_type_failure_invalid_size) { struct hoth_response_key_rotation_record_read actual_read_response; uint16_t response_size = 0; - EXPECT_EQ( + EXPECT_NE( libhoth_key_rotation_read_chunk_type( &hoth_dev_, kCmdRotPublicKey, 0, 0, KEY_ROTATION_FLASH_AREA_SIZE + 1, &actual_read_response, &response_size), - KEY_ROTATION_ERR_INVALID_PARAM); + HOTH_SUCCESS); } TEST_F(LibHothTest, @@ -436,11 +447,11 @@ TEST_F(LibHothTest, .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))); struct hoth_response_key_rotation_record_read actual_read_response; uint16_t response_size = 0; - EXPECT_EQ(libhoth_key_rotation_read_chunk_type( + EXPECT_NE(libhoth_key_rotation_read_chunk_type( &hoth_dev_, kCmdRotPublicKey, 0, 0, KEY_ROTATION_RECORD_READ_CHUNK_TYPE_MAX_SIZE, &actual_read_response, &response_size), - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_read_chunk_type_failure_invalid_chunk_offset) { @@ -450,11 +461,11 @@ TEST_F(LibHothTest, key_rotation_read_chunk_type_failure_invalid_chunk_offset) { .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))); struct hoth_response_key_rotation_record_read actual_read_response; uint16_t response_size = 0; - EXPECT_EQ(libhoth_key_rotation_read_chunk_type( + EXPECT_NE(libhoth_key_rotation_read_chunk_type( &hoth_dev_, kCmdRotPublicKey, KEY_ROTATION_RECORD_READ_CHUNK_TYPE_MAX_SIZE, 0, 0, &actual_read_response, &response_size), - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_chunk_type_count_success) { @@ -465,7 +476,7 @@ TEST_F(LibHothTest, key_rotation_chunk_type_count_success) { uint16_t chunk_count = 0; EXPECT_EQ(libhoth_key_rotation_chunk_type_count(&hoth_dev_, kCmdRotPublicKey, &chunk_count), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); EXPECT_EQ(chunk_count, 2); } @@ -474,9 +485,9 @@ TEST_F(LibHothTest, key_rotation_chunk_type_count_failure_io) { .WillOnce(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); uint16_t chunk_count = 0; - EXPECT_EQ(libhoth_key_rotation_chunk_type_count(&hoth_dev_, kCmdRotPublicKey, + EXPECT_NE(libhoth_key_rotation_chunk_type_count(&hoth_dev_, kCmdRotPublicKey, &chunk_count), - KEY_ROTATION_ERR); + HOTH_SUCCESS); } TEST_F(LibHothTest, @@ -486,9 +497,9 @@ TEST_F(LibHothTest, EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))); uint16_t chunk_count = 0; - EXPECT_EQ(libhoth_key_rotation_chunk_type_count(&hoth_dev_, kCmdRotPublicKey, + EXPECT_NE(libhoth_key_rotation_chunk_type_count(&hoth_dev_, kCmdRotPublicKey, &chunk_count), - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_erase_record_success) { @@ -496,15 +507,14 @@ TEST_F(LibHothTest, key_rotation_erase_record_success) { .WillOnce(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))); - EXPECT_EQ(libhoth_key_rotation_erase_record(&hoth_dev_), - KEY_ROTATION_CMD_SUCCESS); + EXPECT_EQ(libhoth_key_rotation_erase_record(&hoth_dev_), HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_erase_record_failure_io) { EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) .WillOnce(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); - EXPECT_EQ(libhoth_key_rotation_erase_record(&hoth_dev_), KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_erase_record(&hoth_dev_), HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_erase_record_failure_invalid_response_size) { @@ -512,7 +522,7 @@ TEST_F(LibHothTest, key_rotation_erase_record_failure_invalid_response_size) { .WillOnce(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 1), Return(LIBHOTH_OK))); - EXPECT_EQ(libhoth_key_rotation_erase_record(&hoth_dev_), KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_erase_record(&hoth_dev_), HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_set_mauv_success) { @@ -521,15 +531,15 @@ TEST_F(LibHothTest, key_rotation_set_mauv_success) { EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 0), Return(LIBHOTH_OK))); EXPECT_EQ(libhoth_key_rotation_set_mauv(&hoth_dev_, 0x12345678), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_set_mauv_failure_io) { EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) .WillOnce(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); - EXPECT_EQ(libhoth_key_rotation_set_mauv(&hoth_dev_, 0x12345678), - KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_set_mauv(&hoth_dev_, 0x12345678), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_set_mauv_failure_invalid_response_size) { @@ -537,8 +547,8 @@ TEST_F(LibHothTest, key_rotation_set_mauv_failure_invalid_response_size) { .WillOnce(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive) .WillOnce(DoAll(CopyResp(&kDummy, 1), Return(LIBHOTH_OK))); - EXPECT_EQ(libhoth_key_rotation_set_mauv(&hoth_dev_, 0x12345678), - KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_set_mauv(&hoth_dev_, 0x12345678), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_get_mauv_success) { @@ -552,7 +562,7 @@ TEST_F(LibHothTest, key_rotation_get_mauv_success) { Return(LIBHOTH_OK))); struct hoth_response_key_rotation_mauv actual_response; EXPECT_EQ(libhoth_key_rotation_get_mauv(&hoth_dev_, &actual_response), - KEY_ROTATION_CMD_SUCCESS); + HOTH_SUCCESS); EXPECT_EQ(actual_response.mauv, kDefaultMauv.mauv); } @@ -561,8 +571,8 @@ TEST_F(LibHothTest, key_rotation_get_mauv_failure_io) { .WillOnce(Return(LIBHOTH_OK)); EXPECT_CALL(mock_, receive).WillOnce(Return(LIBHOTH_ERR_FAIL)); struct hoth_response_key_rotation_mauv actual_response; - EXPECT_EQ(libhoth_key_rotation_get_mauv(&hoth_dev_, &actual_response), - KEY_ROTATION_ERR); + EXPECT_NE(libhoth_key_rotation_get_mauv(&hoth_dev_, &actual_response), + HOTH_SUCCESS); } TEST_F(LibHothTest, key_rotation_get_mauv_failure_invalid_response_size) { @@ -575,6 +585,52 @@ TEST_F(LibHothTest, key_rotation_get_mauv_failure_invalid_response_size) { .WillOnce(DoAll(CopyResp(&kDefaultMauv, sizeof(kDefaultMauv) - 1), Return(LIBHOTH_OK))); struct hoth_response_key_rotation_mauv actual_response; - EXPECT_EQ(libhoth_key_rotation_get_mauv(&hoth_dev_, &actual_response), - KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE); + EXPECT_NE(libhoth_key_rotation_get_mauv(&hoth_dev_, &actual_response), + HOTH_SUCCESS); +} + +TEST_F(LibHothTest, key_rotation_null_params) { + struct hoth_response_key_rotation_record_version version = {}; + struct hoth_response_key_rotation_status status = {}; + struct hoth_response_key_rotation_payload_status payload_status = {}; + struct hoth_response_key_rotation_record_read read_resp = {}; + uint16_t resp_size = 0; + uint16_t chunk_count = 0; + struct hoth_response_key_rotation_mauv mauv = {}; + uint8_t data[100] = {}; + + EXPECT_NE(libhoth_key_rotation_get_version(nullptr, &version), HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_get_version(&hoth_dev_, nullptr), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_get_status(nullptr, &status), HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_get_status(&hoth_dev_, nullptr), HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_payload_status(nullptr, &payload_status), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_payload_status(&hoth_dev_, nullptr), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_update(nullptr, data, sizeof(data)), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_update(&hoth_dev_, nullptr, sizeof(data)), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_read(nullptr, 0, 8, 0, &read_resp), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_read(&hoth_dev_, 0, 8, 0, nullptr), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_read_chunk_type(nullptr, 0, 0, 0, 8, + &read_resp, &resp_size), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_read_chunk_type(&hoth_dev_, 0, 0, 0, 8, + nullptr, &resp_size), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_read_chunk_type(&hoth_dev_, 0, 0, 0, 8, + &read_resp, nullptr), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_chunk_type_count(nullptr, 0, &chunk_count), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_chunk_type_count(&hoth_dev_, 0, nullptr), + HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_erase_record(nullptr), HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_set_mauv(nullptr, 0), HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_get_mauv(nullptr, &mauv), HOTH_SUCCESS); + EXPECT_NE(libhoth_key_rotation_get_mauv(&hoth_dev_, nullptr), HOTH_SUCCESS); }