forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEPENDENCY MERGE: riotboot_add_sha256_verify (RIOT-OS#11805)
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
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,51 @@ | ||
/* | ||
* 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); | ||
sha256_update(&sha256, "RIOT", 4); | ||
sha256_update(&sha256, img_start + 4, img_len - 4); | ||
sha256_final(&sha256, digest); | ||
|
||
return memcmp(sha256_digest, digest, SHA256_DIGEST_LENGTH) != 0; | ||
} |