-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
west sign: imgtool: zephyr.signed.hex and zephyr.signed.bin do not have the same contents #52271
Comments
@JordanYates, when during the signing a some kind of random is used to generate a signature or to generate an encryption key each call to imgtool will create a different hex/bin. As both are generated by a call to imgtool this behavior is to be expected. Both images however should be ok. As this behavior is intentionally this is not a bug. If you really need to have the same images (I don't know why) you should indeed create convert the generated hex to a bin. |
I agree that the behaviour is expected from the perspective of how the tool is being used, I disagree that it is expected from the perspective of the end user. I spent an hour or so trying to figure out why the two weren't the same before I realised the difference was only in the signed TLV and that the behaviour came from Everything I know about build systems would tell me that if I see two files with the same name, one |
The difference is much larger for encrypted images. There has been a similar issue regarding reproducible builds. As soon as the image build process includes some kind of randomness inside (e.g. for signing), repeated builds will generate different artifacts. |
@JordanYates should this issue be moved to mcuboot? |
@JordanYates I agree with @stephanosio that this is likely an MCUboot issue? And one more thing, is this not related to the fact that signature is non-deterministic? For example ECDSA will use a random number every time you sign: |
@stephanosio, @carlescufi, no this is not a mcuboot issue. It is due to how a hex and bin are generated by west. By calling imgtool twice the signature being non-deterministic is different for the hex and the bin. If west would create the bin from the hex this would not be the case. |
I don't see any fix for this, a build can output a hex file, and a build can output a bin file - those 2 files are optional via Kconfig, unless you force generating both then you have to assume that one might not be there and so both are going to get signed individually, thus have different signature contents. |
(just spotted this issue by sheer chance while searching something else)
I experienced basically the same issue with rimage's salt: As long as salt is required for security reasons there is no "solution" to this problem; only mitigations and education: everyone must simply get used to signatures being not deterministic. C'est la vie. CMake comments and logging can help. If you're interested in reproducibility then focus on stripped files BEFORE signing, it's been working great for us. As a coincidence I just finished fixing SOF build reproducibility issues, now our Windows CI and Linux CI are about to produce the same binaries BEFORE signing. See early demo at: thesofproject/sof#6920 (comment) See also #51954 (which I have not forgotten) |
This issue is slightly different from what you are describing I think. |
Yes
Can it really? Looking at Kconfig and CMake (I recommend |
Sure they can, |
Interesting, I didn't expect
I just had a look at https://github.com/mcu-tools/mcuboot/blob/main/scripts/imgtool/image.py and that helped. The
So the reason while Make My 2 cents. |
The signatures here are nothing special, they are just normal hex/binary data at a specific location, to any hex viewer it is just "program code". |
Maybe there is a different reason to modify the generation as proposed by @JordanYates: it is never a good idea to generate a different signature for the same binary data as this might leak the private key used for signing. To mitigate this either create the bin from the hex as proposed (if this is supported by all toolchains) or add an extra random value to the image that ensures the binary data is different (in imgtool). |
But the idea of cryptographic salt is exactly that?! Creating a different signature for the same binary data for various security reasons. I have no idea "how" but this is "why": https://crypto.stackexchange.com/questions/25182/why-use-randomness-in-digital-signature-algorithms
As mentioned above,
Unlike GPL projects, the license of the Zephyr project allows re-use in closed source software. So it's very difficult to tell what "all" toolchains support. Here's a quote of the second sentence in the issue that discussed the toolchain abstraction layer that implements these BIN and HEX formats (among many others)
This is happening already and why this issue was created? You lost me sorry. |
This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time. |
This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time. |
I'll leave a few comments before closing the issue. Feel free to open if needed. To summarize, several signature and encryption algorithms used by imgtool/mcuboot are non-deterministic. There are situations where west is used to build both a .bin and a .hex file, and because each is a separate invocation of imgtool, the output ends up different. My question is, what are the situations where there is a need to generate both a .hex and a .bin file? Shouldn't one be the preferred output type for a given build configuration. If this is really important, it might be worthwhile changing west to generate just a hex file, and build a .bin from that, but I'm not sure how well that would fit with the given architecture. As an aside, if you really want deterministic signatures, Ed25519 should be deterministic. RSA-PSS uses a random value to increase the security properties of the signature. A random value is fundamental to how ECDSA works. Another option would be to add support for rfc6979 to imgtool. The resulting signatures are compatible with existing ECDSA verification, but the signatures are deterministic. Note that there are security implications of deterministic ECDSA, even though there are also benefits. |
Describe the bug
When creating signed images, the generated files
zephyr.signed.hex
andzephyr.signed.bin
do not have the same logical contents.west sign
works by running two independent calls toimgtool
, once to create the.hex
, and again to create the.bin
.Unfortunately even running
imgtool
with the exact same arguments twice does not result in the same binary.Both files are valid, they are just different due to the different generated values of the
IMAGE_TLV_RSA2048_PSS
.To Reproduce
Expected behavior
I would expect
zephyr.signed.bin
andzephyr.signed.hex
to have the same contents, differing only in the file format.Impact
The actual binary on the device depends on which file format was flashed.
Additional context
Given this behaviour is in
imgtool
, the only solution I see is runningimgtool
once and then converting the.hex
to a.bin
The text was updated successfully, but these errors were encountered: