Skip to content

Commit f53a4d0

Browse files
committed
Encrypt scratch: Fix key initialization
1 parent 30391a6 commit f53a4d0

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

boot/bootutil/src/loader.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -1279,15 +1279,15 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
12791279
} else {
12801280
rc = 0;
12811281
}
1282-
12831282
#ifdef MCUBOOT_ENC_SCRATCH
1284-
rc = boot_enc_init(BOOT_CURR_ENC(state), 2);
1285-
assert(rc == 0);
1286-
1287-
rc = boot_enc_set_key(BOOT_CURR_ENC(state), 2, bs);
1288-
assert(rc == 0);
1283+
if(BOOT_CURR_ENC(state)[1].valid) {
1284+
memcpy(bs->enckey[2], bs->enckey[1], BOOT_ENC_KEY_SIZE);
1285+
rc = boot_enc_init(BOOT_CURR_ENC(state), 2);
1286+
assert(rc == 0);
1287+
rc = boot_enc_set_key(BOOT_CURR_ENC(state), 2, bs);
1288+
assert(rc == 0);
1289+
}
12891290
#endif
1290-
12911291
} else {
12921292
memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
12931293
}
@@ -1323,6 +1323,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
13231323
boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
13241324
#ifdef MCUBOOT_ENC_SCRATCH
13251325
if(slot == BOOT_SECONDARY_SLOT) {
1326+
memcpy(bs->enckey[2], bs->enckey[1], BOOT_ENC_KEY_SIZE);
13261327
boot_enc_set_key(BOOT_CURR_ENC(state), 2, bs);
13271328
}
13281329
#endif

0 commit comments

Comments
 (0)