Skip to content

Commit d12c688

Browse files
nordicjmhenrikbrixandersen
authored andcommitted
mgmt: mcumgr: grp: img_mgmt: Add support for SHA512 in images
Adds support for images signed with SHA512. Signed-off-by: Dominik Ermel <[email protected]> Signed-off-by: Jamie McCrae <[email protected]>
1 parent 7e92b70 commit d12c688

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

subsys/mgmt/mcumgr/grp/img_mgmt/include/mgmt/mcumgr/grp/img_mgmt/img_mgmt_priv.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
extern "C" {
1919
#endif
2020

21+
#ifdef CONFIG_MCUBOOT_BOOTLOADER_USES_SHA512
22+
#define IMAGE_TLV_SHA IMAGE_TLV_SHA512
23+
#define IMAGE_SHA_LEN 64
24+
#else
25+
#define IMAGE_TLV_SHA IMAGE_TLV_SHA256
26+
#define IMAGE_SHA_LEN 32
27+
#endif
28+
2129
/**
2230
* @brief Ensures the spare slot (slot 1) is fully erased.
2331
*

subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ int img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
322322
if (tlv.it_type == 0xff && tlv.it_len == 0xffff) {
323323
return IMG_MGMT_ERR_INVALID_TLV;
324324
}
325-
if (tlv.it_type != IMAGE_TLV_SHA256 || tlv.it_len != IMAGE_HASH_LEN) {
325+
if (tlv.it_type != IMAGE_TLV_SHA || tlv.it_len != IMAGE_SHA_LEN) {
326326
/* Non-hash TLV. Skip it. */
327327
data_off += sizeof(tlv) + tlv.it_len;
328328
continue;
@@ -336,10 +336,10 @@ int img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
336336

337337
data_off += sizeof(tlv);
338338
if (hash != NULL) {
339-
if (data_off + IMAGE_HASH_LEN > data_end) {
339+
if (data_off + IMAGE_SHA_LEN > data_end) {
340340
return IMG_MGMT_ERR_TLV_INVALID_SIZE;
341341
}
342-
rc = img_mgmt_read(image_slot, data_off, hash, IMAGE_HASH_LEN);
342+
rc = img_mgmt_read(image_slot, data_off, hash, IMAGE_SHA_LEN);
343343
if (rc != 0) {
344344
return rc;
345345
}
@@ -382,13 +382,13 @@ int
382382
img_mgmt_find_by_hash(uint8_t *find, struct image_version *ver)
383383
{
384384
int i;
385-
uint8_t hash[IMAGE_HASH_LEN];
385+
uint8_t hash[IMAGE_SHA_LEN];
386386

387387
for (i = 0; i < SLOTS_PER_IMAGE * CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER; i++) {
388388
if (img_mgmt_read_info(i, ver, hash, NULL) != 0) {
389389
continue;
390390
}
391-
if (!memcmp(hash, find, IMAGE_HASH_LEN)) {
391+
if (!memcmp(hash, find, IMAGE_SHA_LEN)) {
392392
return i;
393393
}
394394
}
@@ -698,7 +698,7 @@ img_mgmt_upload_good_rsp(struct smp_streamer *ctxt)
698698
static int
699699
img_mgmt_upload_log(bool is_first, bool is_last, int status)
700700
{
701-
uint8_t hash[IMAGE_HASH_LEN];
701+
uint8_t hash[IMAGE_SHA_LEN];
702702
const uint8_t *hashp;
703703
int rc;
704704

subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt_state.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,11 @@ static bool img_mgmt_state_encode_slot(struct smp_streamer *ctxt, uint32_t slot,
435435
zcbor_state_t *zse = ctxt->writer->zs;
436436
uint32_t flags;
437437
char vers_str[IMG_MGMT_VER_MAX_STR_LEN];
438-
uint8_t hash[IMAGE_HASH_LEN]; /* SHA256 hash */
439-
struct zcbor_string zhash = { .value = hash, .len = IMAGE_HASH_LEN };
438+
uint8_t hash[IMAGE_SHA_LEN];
439+
struct zcbor_string zhash = {
440+
.value = hash,
441+
.len = IMAGE_SHA_LEN,
442+
};
440443
struct image_version ver;
441444
bool ok;
442445
int rc = img_mgmt_read_info(slot, &ver, hash, &flags);
@@ -780,14 +783,14 @@ img_mgmt_state_write(struct smp_streamer *ctxt)
780783
IMG_MGMT_ERR_INVALID_HASH);
781784
goto end;
782785
}
783-
} else if (zhash.len != IMAGE_HASH_LEN) {
786+
} else if (zhash.len != IMAGE_SHA_LEN) {
784787
/* The img_mgmt_find_by_hash does exact length compare
785788
* so just fail here.
786789
*/
787790
ok = smp_add_cmd_err(zse, MGMT_GROUP_ID_IMAGE, IMG_MGMT_ERR_INVALID_HASH);
788791
goto end;
789792
} else {
790-
uint8_t hash[IMAGE_HASH_LEN];
793+
uint8_t hash[IMAGE_SHA_LEN];
791794

792795
memcpy(hash, zhash.value, zhash.len);
793796

0 commit comments

Comments
 (0)