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 2d14017
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
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
10 changes: 7 additions & 3 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,7 +250,9 @@ static int components_decode(zcbor_state_t *zsd, void *value)
return err;
}

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

manifest->num_components++;
}
Expand Down

0 comments on commit 2d14017

Please sign in to comment.