Skip to content

Commit a56cf02

Browse files
de-nordicrlubos
authored andcommitted
[nrf noup] mgmt/mcumgr: Add support for SHA512 in images
Adds support for images signed with SHA512. Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit b00a959)
1 parent 6f1c879 commit a56cf02

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-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
@@ -348,7 +348,7 @@ int img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
348348
if (tlv.it_type == 0xff && tlv.it_len == 0xffff) {
349349
return IMG_MGMT_ERR_INVALID_TLV;
350350
}
351-
if (tlv.it_type != IMAGE_TLV_SHA256 || tlv.it_len != IMAGE_HASH_LEN) {
351+
if (tlv.it_type != IMAGE_TLV_SHA || tlv.it_len != IMAGE_SHA_LEN) {
352352
/* Non-hash TLV. Skip it. */
353353
data_off += sizeof(tlv) + tlv.it_len;
354354
continue;
@@ -362,10 +362,10 @@ int img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
362362

363363
data_off += sizeof(tlv);
364364
if (hash != NULL) {
365-
if (data_off + IMAGE_HASH_LEN > data_end) {
365+
if (data_off + IMAGE_SHA_LEN > data_end) {
366366
return IMG_MGMT_ERR_TLV_INVALID_SIZE;
367367
}
368-
rc = img_mgmt_read(image_slot, data_off, hash, IMAGE_HASH_LEN);
368+
rc = img_mgmt_read(image_slot, data_off, hash, IMAGE_SHA_LEN);
369369
if (rc != 0) {
370370
return rc;
371371
}
@@ -408,13 +408,13 @@ int
408408
img_mgmt_find_by_hash(uint8_t *find, struct image_version *ver)
409409
{
410410
int i;
411-
uint8_t hash[IMAGE_HASH_LEN];
411+
uint8_t hash[IMAGE_SHA_LEN];
412412

413413
for (i = 0; i < SLOTS_PER_IMAGE * CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER; i++) {
414414
if (img_mgmt_read_info(i, ver, hash, NULL) != 0) {
415415
continue;
416416
}
417-
if (!memcmp(hash, find, IMAGE_HASH_LEN)) {
417+
if (!memcmp(hash, find, IMAGE_SHA_LEN)) {
418418
return i;
419419
}
420420
}
@@ -724,7 +724,7 @@ img_mgmt_upload_good_rsp(struct smp_streamer *ctxt)
724724
static int
725725
img_mgmt_upload_log(bool is_first, bool is_last, int status)
726726
{
727-
uint8_t hash[IMAGE_HASH_LEN];
727+
uint8_t hash[IMAGE_SHA_LEN];
728728
const uint8_t *hashp;
729729
int rc;
730730

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ static bool img_mgmt_state_encode_slot(struct smp_streamer *ctxt, uint32_t slot,
434434
zcbor_state_t *zse = ctxt->writer->zs;
435435
uint32_t flags;
436436
char vers_str[IMG_MGMT_VER_MAX_STR_LEN];
437-
uint8_t hash[IMAGE_HASH_LEN]; /* SHA256 hash */
438-
struct zcbor_string zhash = { .value = hash, .len = IMAGE_HASH_LEN };
437+
uint8_t hash[IMAGE_SHA_LEN];
438+
struct zcbor_string zhash = { .value = hash, .len = IMAGE_SHA_LEN};
439439
struct image_version ver;
440440
bool ok;
441441
int rc = img_mgmt_read_info(slot, &ver, hash, &flags);
@@ -779,14 +779,14 @@ img_mgmt_state_write(struct smp_streamer *ctxt)
779779
IMG_MGMT_ERR_INVALID_HASH);
780780
goto end;
781781
}
782-
} else if (zhash.len != IMAGE_HASH_LEN) {
782+
} else if (zhash.len != IMAGE_SHA_LEN) {
783783
/* The img_mgmt_find_by_hash does exact length compare
784784
* so just fail here.
785785
*/
786786
ok = smp_add_cmd_err(zse, MGMT_GROUP_ID_IMAGE, IMG_MGMT_ERR_INVALID_HASH);
787787
goto end;
788788
} else {
789-
uint8_t hash[IMAGE_HASH_LEN];
789+
uint8_t hash[IMAGE_SHA_LEN];
790790

791791
memcpy(hash, zhash.value, zhash.len);
792792

0 commit comments

Comments
 (0)