Skip to content

Commit

Permalink
ota: store component hash as binary array
Browse files Browse the repository at this point in the history
Component hash is received from the server as a hex string. Convert it to a
binary array when decoding the manifest so that it's ready to compare to a
locally generated hash.

Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed Nov 15, 2024
1 parent 7af79bb commit 7b79cdf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
include `block_buffer_len` for the actual length of data and
`negotiated_block_size` to indicate the maximum block size (may be
used along with 'block_idx' to calculate a byte offset).
- `golioth_ota_component->hash` is now stored as an array of bytes
instead of as a hex string.

### Removed:

Expand Down
8 changes: 5 additions & 3 deletions include/golioth/ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ extern "C"
/// https://docs.golioth.io/reference/protocols/coap/ota
/// @{

/// Size of a SHA256 of Artifact Binary in bytes
#define GOLIOTH_OTA_COMPONENT_HASH_LEN 64
/// Size of a SHA256 of Artifact hex string in bytes
#define GOLIOTH_OTA_COMPONENT_HEX_HASH_LEN 64
/// Size of a SHA256 of Artifact bin array in bytes
#define GOLIOTH_OTA_COMPONENT_BIN_HASH_LEN 32
/// Maximum size of Binary Detected Type in bytes
#define GOLIOTH_OTA_MAX_COMPONENT_BOOTLOADER_NAME_LEN 7
/// Maximum size of Relative URI to download binary (+ 7 bytes for Path)
Expand Down Expand Up @@ -78,7 +80,7 @@ struct golioth_ota_component
/// Size of the artifact, in bytes
int32_t size;
/// Artifact Hash
char hash[GOLIOTH_OTA_COMPONENT_HASH_LEN + 1];
uint8_t hash[GOLIOTH_OTA_COMPONENT_BIN_HASH_LEN];
/// Artifact uri (e.g. "/.u/c/[email protected]")
char uri[GOLIOTH_OTA_MAX_COMPONENT_URI_LEN + 1];
/// Artifact bootloader ("mcuboot" or "default"")
Expand Down
7 changes: 5 additions & 2 deletions src/ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "coap_blockwise.h"
#include "coap_client.h"
#include <golioth/golioth_debug.h>
#include <golioth/golioth_sys.h>
#include "golioth_util.h"
#include <golioth/zcbor_utils.h>

Expand Down Expand Up @@ -216,9 +217,10 @@ static int components_decode(zcbor_state_t *zsd, void *value)
component->version,
sizeof(component->version) - 1,
};
char hash_string[GOLIOTH_OTA_COMPONENT_HEX_HASH_LEN + 1];
struct component_tstr_value hash = {
component->hash,
sizeof(component->hash) - 1,
hash_string,
sizeof(hash_string) - 1,
};
struct component_tstr_value uri = {
component->uri,
Expand Down Expand Up @@ -248,6 +250,7 @@ static int components_decode(zcbor_state_t *zsd, void *value)
return err;
}

hex2bin(hash_string, strlen(hash_string), component->hash, sizeof(component->hash));
component->size = component_size;

manifest->num_components++;
Expand Down
15 changes: 14 additions & 1 deletion tests/hil/tests/ota/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,23 @@ struct golioth_client *client;

static void log_component_members(const struct golioth_ota_component *component)
{
char hash_string[GOLIOTH_OTA_COMPONENT_HEX_HASH_LEN + 1];
for (int i = 0; i < sizeof(component->hash); i++)
{
int write_idx = i * 2;
if (write_idx > (GOLIOTH_OTA_COMPONENT_HEX_HASH_LEN - 2))
{
GLTH_LOGE(TAG, "Error converting component hash to string");
return;
}

sprintf(hash_string + write_idx, "%02x", component->hash[i]);
}

GLTH_LOGI(TAG, "component.package: %s", component->package);
GLTH_LOGI(TAG, "component.version: %s", component->version);
GLTH_LOGI(TAG, "component.size: %u", (unsigned int) component->size);
GLTH_LOGI(TAG, "component.hash: %s", component->hash);
GLTH_LOGI(TAG, "component.hash: %s", hash_string);
GLTH_LOGI(TAG, "component.uri: %s", component->uri);
GLTH_LOGI(TAG, "component.bootloader: %s", component->bootloader);
}
Expand Down

0 comments on commit 7b79cdf

Please sign in to comment.