Skip to content

bootutil/crypto: Handle hashing errors and refactorings#2579

Open
kkrentz wants to merge 5 commits into
mcu-tools:mainfrom
kkrentz:hashing-pr
Open

bootutil/crypto: Handle hashing errors and refactorings#2579
kkrentz wants to merge 5 commits into
mcu-tools:mainfrom
kkrentz:hashing-pr

Conversation

@kkrentz
Copy link
Copy Markdown
Contributor

@kkrentz kkrentz commented Dec 9, 2025

This PR complements invocations of hashing-related function with error handling. This is important in various occasions, such as when verifying the signature of an image in "pure" mode. In this case, an error during hashing may cause the computed hash digest to become the all-zero string. However, an arbitrary ECDSA signature over the all-zero hash digest will turn out authentic.

Also, this PR adds a new file called sha.c, which encapsulates shorthands for common invocation patterns of SHA and HMAC, as well as an HKDF implementation. This simplifies matters in other files and having HKDF at a reusable place helped us integrate TCG's Device Identifier Composition Engine with MCUboot, too. This is because DICE uses the "expand" subroutine of HKDF.

Besides, this PR makes HMAC available regardless of the crypto backend. Currently, only mbedTLS and TinyCrypt provide this function, thereby limiting firmware encryption to configurations with one of these crypto backends.

@kkrentz kkrentz force-pushed the hashing-pr branch 7 times, most recently from 11cd7cc to 59140a8 Compare December 9, 2025 21:00
@kkrentz kkrentz requested a review from d3zd3z as a code owner December 9, 2025 21:00
@kkrentz kkrentz force-pushed the hashing-pr branch 4 times, most recently from 27a1a66 to 7298a73 Compare December 9, 2025 22:38
@kkrentz kkrentz marked this pull request as draft December 10, 2025 07:30
@kkrentz kkrentz marked this pull request as ready for review December 10, 2025 17:30
Copy link
Copy Markdown
Member

@d3zd3z d3zd3z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this will need to be rebased on a recent version to be workable. I apologize for missing it back when it was fresher.

Some concerns here:

  • The included target where error handling might make sense (cc310) doesn't return status on it's hash functions. Presumably, this Pr is to support out of tree HW devices?
  • The new bootutil_sha_finish doesn't have any testing. Admittedly, this is probably best checked by inspection, though.

uint8_t tag[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
uint8_t tag[IMAGE_HASH_SIZE];
uint8_t shared[EC_SHARED_LEN];
uint8_t derived_key[BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a concern about still having the SHA256 digest size here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review. The new bootutil_sha_hmac creates tags of length IMAGE_HASH_SIZE. Nevertheless, we could leave BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE because the encryption and signature mechanism must share the same hash function. On the other hand, I now chose to declare tag more appropriately as uint8_t tag[EC_TAG_LEN]. Besides, I added an assertion to ensure that the encryption and signature mechanisms are sharing the same hash function.

@kkrentz kkrentz force-pushed the hashing-pr branch 2 times, most recently from 377be1d to 083f12b Compare May 9, 2026 18:14
kkrentz added 5 commits May 9, 2026 22:48
The current API actually requires checking each call to bootutil_sha_init,
bootutil_sha_update, bootutil_sha_finish, and bootutil_sha_abort for errors.
This would result in a lot of clutter and is not done at the moment. This
commit wraps these functions so as to silently ignore subsequent calls in
a series upon errors. As a result, only bootutil_sha_finish needs error
checking.

Signed-off-by: kkrentz <konrad.krentz@gmail.com>
This simplifies the API for generating HMACs and deriving keys.

Signed-off-by: kkrentz <konrad.krentz@gmail.com>
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>
Using the currently configured crypto back end, this implements HMAC and
HKDF. This implementation serves as a fallback solution for crypto back
ends that do not provide these functions. As there are plans to remove
TinyCrypt from MCUboot, no effort is made to use TinyCrypt when
MCUBOOT_USE_TINYCRYPT is set. Instead, this configuration falls back on
the general-purpose HMAC and HKDF implementations.

Signed-off-by: kkrentz <konrad.krentz@gmail.com>
Thus far, hashing errors were silently ignored when hashing images.

Signed-off-by: kkrentz <konrad.krentz@gmail.com>
@kkrentz
Copy link
Copy Markdown
Contributor Author

kkrentz commented May 9, 2026

  • The included target where error handling might make sense (cc310) doesn't return status on it's hash functions. Presumably, this Pr is to support out of tree HW devices?

If Mbed TLS is configured to leverage HW acceleration, this might cause issues in MCUboot. However, I did not track this down. Anyway, I would like to have error handling to make proper use of the HW acceleration of my TI chips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants