Skip to content

Commit 8088674

Browse files
committed
bootutil/crypto: Add shorthand for hashing
This introduces a shorthand for doing bootutil_sha_init, bootutil_sha_update, and bootutil_sha_finish at once. This simplifies matters in bootutil_find_key. Besides, there was no error handling in bootutil_find_key, which is also fixed. Signed-off-by: kkrentz <konrad.krentz@gmail.com>
1 parent aa35d7b commit 8088674

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

boot/bootutil/include/bootutil/crypto/sha.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,18 @@ static inline int bootutil_sha_finish(bootutil_sha_context *ctx,
274274
}
275275
#endif /* MCUBOOT_USE_CC310 */
276276

277+
/**
278+
* Does bootutil_sha_init, bootutil_sha_update, and bootutil_sha_finish at once.
279+
*
280+
* @param data Pointer to the data to hash.
281+
* @param data_len Length of @c data in bytes.
282+
* @param digest Pointer to where the resulting digest shall be stored.
283+
*
284+
* @return @c 0 on success and nonzero otherwise.
285+
*/
286+
int bootutil_sha(const uint8_t *data, size_t data_len,
287+
uint8_t digest[static IMAGE_HASH_SIZE]);
288+
277289
/**
278290
* Computes an HMAC as per RFC 2104.
279291
*

boot/bootutil/src/bootutil_find_key.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
5555
#if !defined(MCUBOOT_HW_KEY)
5656
int bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
5757
{
58-
bootutil_sha_context sha_ctx;
5958
int i;
6059
const struct bootutil_key *key;
60+
int rc;
6161
uint8_t hash[IMAGE_HASH_SIZE];
6262

6363
BOOT_LOG_DBG("bootutil_find_key");
@@ -68,10 +68,11 @@ int bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
6868

6969
for (i = 0; i < bootutil_key_cnt; i++) {
7070
key = &bootutil_keys[i];
71-
bootutil_sha_init(&sha_ctx);
72-
bootutil_sha_update(&sha_ctx, key->key, *key->len);
73-
bootutil_sha_finish(&sha_ctx, hash);
74-
bootutil_sha_drop(&sha_ctx);
71+
rc = bootutil_sha(key->key, *key->len, hash);
72+
if (rc) {
73+
BOOT_LOG_ERR("bootutil_find_key: bootutil_sha failed: %d", rc);
74+
return -1;
75+
}
7576
if (!memcmp(hash, keyhash, keyhash_len)) {
7677
return i;
7778
}
@@ -82,7 +83,6 @@ int bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
8283
extern unsigned int pub_key_len;
8384
int bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
8485
{
85-
bootutil_sha_context sha_ctx;
8686
uint8_t hash[IMAGE_HASH_SIZE];
8787
uint8_t key_hash[IMAGE_HASH_SIZE];
8888
size_t key_hash_size = sizeof(key_hash);
@@ -91,10 +91,11 @@ int bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
9191

9292
BOOT_LOG_DBG("bootutil_find_key: image_index %d", image_index);
9393

94-
bootutil_sha_init(&sha_ctx);
95-
bootutil_sha_update(&sha_ctx, key, key_len);
96-
bootutil_sha_finish(&sha_ctx, hash);
97-
bootutil_sha_drop(&sha_ctx);
94+
rc = bootutil_sha(key, key_len, hash);
95+
if (rc) {
96+
BOOT_LOG_ERR("bootutil_find_key: bootutil_sha failed: %d", rc);
97+
return -1;
98+
}
9899

99100
rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
100101
if (rc) {

boot/bootutil/src/sha.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
#include "mbedtls/md.h"
1515
#endif
1616

17+
int
18+
bootutil_sha(const uint8_t *const data, size_t data_len,
19+
uint8_t digest[static IMAGE_HASH_SIZE])
20+
{
21+
bootutil_sha_context ctx;
22+
int rc;
23+
24+
bootutil_sha_init(&ctx);
25+
bootutil_sha_update(&ctx, data, data_len);
26+
rc = bootutil_sha_finish(&ctx, digest);
27+
bootutil_sha_drop(&ctx);
28+
return rc;
29+
}
30+
1731
#if defined(MCUBOOT_USE_MBED_TLS)
1832

1933
static const mbedtls_md_type_t md_type =

0 commit comments

Comments
 (0)