Skip to content

Commit 61555a3

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 <dominik.ermel@nordicsemi.no>
1 parent dcb8c6a commit 61555a3

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
@@ -253,7 +253,7 @@ int img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
253253
if (tlv.it_type == 0xff && tlv.it_len == 0xffff) {
254254
return IMG_MGMT_ERR_INVALID_TLV;
255255
}
256-
if (tlv.it_type != IMAGE_TLV_SHA256 || tlv.it_len != IMAGE_HASH_LEN) {
256+
if (tlv.it_type != IMAGE_TLV_SHA || tlv.it_len != IMAGE_SHA_LEN) {
257257
/* Non-hash TLV. Skip it. */
258258
data_off += sizeof(tlv) + tlv.it_len;
259259
continue;
@@ -267,10 +267,10 @@ int img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
267267

268268
data_off += sizeof(tlv);
269269
if (hash != NULL) {
270-
if (data_off + IMAGE_HASH_LEN > data_end) {
270+
if (data_off + IMAGE_SHA_LEN > data_end) {
271271
return IMG_MGMT_ERR_TLV_INVALID_SIZE;
272272
}
273-
rc = img_mgmt_read(image_slot, data_off, hash, IMAGE_HASH_LEN);
273+
rc = img_mgmt_read(image_slot, data_off, hash, IMAGE_SHA_LEN);
274274
if (rc != 0) {
275275
return rc;
276276
}
@@ -313,13 +313,13 @@ int
313313
img_mgmt_find_by_hash(uint8_t *find, struct image_version *ver)
314314
{
315315
int i;
316-
uint8_t hash[IMAGE_HASH_LEN];
316+
uint8_t hash[IMAGE_SHA_LEN];
317317

318318
for (i = 0; i < 2 * CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER; i++) {
319319
if (img_mgmt_read_info(i, ver, hash, NULL) != 0) {
320320
continue;
321321
}
322-
if (!memcmp(hash, find, IMAGE_HASH_LEN)) {
322+
if (!memcmp(hash, find, IMAGE_SHA_LEN)) {
323323
return i;
324324
}
325325
}
@@ -441,7 +441,7 @@ img_mgmt_upload_good_rsp(struct smp_streamer *ctxt)
441441
static int
442442
img_mgmt_upload_log(bool is_first, bool is_last, int status)
443443
{
444-
uint8_t hash[IMAGE_HASH_LEN];
444+
uint8_t hash[IMAGE_SHA_LEN];
445445
const uint8_t *hashp;
446446
int rc;
447447

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
@@ -415,8 +415,8 @@ static bool img_mgmt_state_encode_slot(struct smp_streamer *ctxt, uint32_t slot,
415415
zcbor_state_t *zse = ctxt->writer->zs;
416416
uint32_t flags;
417417
char vers_str[IMG_MGMT_VER_MAX_STR_LEN];
418-
uint8_t hash[IMAGE_HASH_LEN]; /* SHA256 hash */
419-
struct zcbor_string zhash = { .value = hash, .len = IMAGE_HASH_LEN };
418+
uint8_t hash[IMAGE_SHA_LEN];
419+
struct zcbor_string zhash = { .value = hash, .len = IMAGE_SHA_LEN};
420420
struct image_version ver;
421421
bool ok;
422422
int rc = img_mgmt_read_info(slot, &ver, hash, &flags);
@@ -719,14 +719,14 @@ img_mgmt_state_write(struct smp_streamer *ctxt)
719719
IMG_MGMT_ERR_INVALID_HASH);
720720
goto end;
721721
}
722-
} else if (zhash.len != IMAGE_HASH_LEN) {
722+
} else if (zhash.len != IMAGE_SHA_LEN) {
723723
/* The img_mgmt_find_by_hash does exact length compare
724724
* so just fail here.
725725
*/
726726
ok = smp_add_cmd_err(zse, MGMT_GROUP_ID_IMAGE, IMG_MGMT_ERR_INVALID_HASH);
727727
goto end;
728728
} else {
729-
uint8_t hash[IMAGE_HASH_LEN];
729+
uint8_t hash[IMAGE_SHA_LEN];
730730

731731
memcpy(hash, zhash.value, zhash.len);
732732

0 commit comments

Comments
 (0)