-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sys/riotboot: add initial image digest verification #11805
Merged
kaspar030
merged 1 commit into
RIOT-OS:master
from
kaspar030:riotboot_add_sha256_verify
Jul 10, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (C) 2019 Inria | ||
* 2019 Freie Universität Berlin | ||
* 2019 Kaspar Schleiser <[email protected]> | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup sys_riotboot_flashwrite | ||
* @{ | ||
* | ||
* @file | ||
* @brief Firmware update sha256 verification helper functions | ||
* | ||
* @author Kaspar Schleiser <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
#include "hashes/sha256.h" | ||
#include "log.h" | ||
#include "riotboot/slot.h" | ||
|
||
int riotboot_flashwrite_verify_sha256(const uint8_t *sha256_digest, size_t img_len, int target_slot) | ||
{ | ||
char digest[SHA256_DIGEST_LENGTH]; | ||
|
||
sha256_context_t sha256; | ||
|
||
if (img_len < 4) { | ||
LOG_INFO("riotboot: verify_sha256(): image too small\n"); | ||
return -1; | ||
} | ||
|
||
uint8_t *img_start = (uint8_t *)riotboot_slot_get_hdr(target_slot); | ||
|
||
LOG_INFO("riotboot: verifying digest at %p (img at: %p size: %u)\n", sha256_digest, img_start, img_len); | ||
|
||
sha256_init(&sha256); | ||
|
||
/* add RIOTBOOT_MAGIC since it isn't written into flash until | ||
* riotboot_flashwrite_finish()" */ | ||
sha256_update(&sha256, "RIOT", 4); | ||
|
||
/* account for injected RIOTBOOT_MAGIC by skipping RIOTBOOT_MAGIC_LEN */ | ||
sha256_update(&sha256, img_start + 4, img_len - 4); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as in upper comment. |
||
|
||
sha256_final(&sha256, digest); | ||
|
||
return memcmp(sha256_digest, digest, SHA256_DIGEST_LENGTH) != 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this
RIOTBOOT_MAGIC
? If it is I would replace "RIOT" by RIOTBOOT_MAGIC, and 4 bysizeof(RIOTBOOT_MAGIC)
to avoid magic numbers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm this can't be done since RIOTBOOT_MAGIC is a numerical value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup. Maybe
("RIOT", 4)
is clear enough for now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaspar030 I can't think of a workaround for this. Can you just add a comment like:
"Add RIOTBOOT_MAGIC since it isn't written into flash until riotboot_flashwrite_finnish()"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, also added a comment explaining the "+4, -4".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, please squash