diff --git a/core/embed/lib/image.c b/core/embed/lib/image.c index b2bce0f5ad..c2eaad3ab8 100644 --- a/core/embed/lib/image.c +++ b/core/embed/lib/image.c @@ -280,23 +280,42 @@ secbool check_image_contents(const image_header *const hdr, uint32_t firstskip, return secfalse; } + uint32_t expected_word = expected_byte << 24 | expected_byte << 16 | + expected_byte << 8 | expected_byte; + while (offset < end_offset) { size_t bytes_to_check = MIN( IMAGE_CHUNK_SIZE - (offset % IMAGE_CHUNK_SIZE), end_offset - offset); + size_t words_to_check = bytes_to_check / sizeof(uint32_t); + size_t single_bytes_to_check = bytes_to_check % sizeof(uint32_t); - const uint8_t *data = - (const uint8_t *)flash_area_get_address(area, offset, bytes_to_check); - if (!data) { + const uint8_t *bytes = (const uint8_t *)flash_area_get_address( + area, offset, single_bytes_to_check); + if (!bytes) { return secfalse; } - for (size_t i = 0; i < bytes_to_check; i++) { - if (data[i] != expected_byte) { + for (size_t i = 0; i < single_bytes_to_check; i++) { + if (bytes[i] != expected_byte) { return secfalse; } } - offset += bytes_to_check; + offset += single_bytes_to_check; + + const uint32_t *data = (const uint32_t *)flash_area_get_address( + area, offset, bytes_to_check - single_bytes_to_check); + if (!data) { + return secfalse; + } + + for (size_t i = 0; i < words_to_check; i++) { + if (data[i] != expected_word) { + return secfalse; + } + } + + offset += words_to_check * sizeof(uint32_t); } }