2019-07-01 02:18 AM
Hi,
I tried to follow setup for secure boot as detailed here: https://wiki.st.com/stm32mpu/wiki/STM32MP15_secure_boot
I'm working with Linux host PC running Ubuntu 18.04
The 1st thing was to try generating keys.
For this I tried to use "STM32MP_KeyGen_CLI" previously installed with STM32CubeProgrammer. (Note: STM32_Programmer_CLI installed is v2.1.0 )
A first note here, https://wiki.st.com/stm32mpu/wiki/KeyGen_tool indicates keygen tool is named "STM32AP_KeyGen_CLI.sh" and is a script whereas it is "STM32MP_KeyGen_CLI" available in STM32CubeProgrammer bin folder and it is a Linux executable.
Simply running STM32MP_KeyGen_CLI with --version gives the following ouput:
~~~~
$ ./STM32MP_KeyGen_CLI -v
-------------------------------------------------------------------
STM32MP Key Generator v1.0.0
-------------------------------------------------------------------
Version: v1.0.0
~~~~
Then, following usage fails:
~~~~
$ ./STM32MP_KeyGen_CLI -abs /home/me/stm32mpu/secure_boot/keys/ -pwd mypassword
-------------------------------------------------------------------
STM32MP Key Generator v1.0.0
-------------------------------------------------------------------
Prime256v1 curve is selected.
AES_256_cbc algorithm is selected for private key encryption
Generating Prime256v1 keys...
Error: creating Key File fails
Error occured while creating PEM file!
Error: An error occured while generating key files
~~~~
It simply creates empty "keys" subfolder and the subfolder is only writeable...
I then tried the following usage:
~~~~
$ ./STM32MP_KeyGen_CLI -pwd mypassword -pubk keys/public.pem -prvk keys/private.pem -hash keys/public_hash.bin
-------------------------------------------------------------------
STM32MP Key Generator v1.0.0
-------------------------------------------------------------------
keys
Warning: File path does not exist. Do you want to create it?
(y/n)y
Prime256v1 curve is selected.
AES_256_cbc algorithm is selected for private key encryption
Generating Prime256v1 keys...
Private key PEM file created
Public key PEM file created
public key hash file created
Keys generated successfully.
+ public key: keys/public.pem
+ private key: keys/private.pem
+ public hash key: keys/public_hash.bin
~~~~
Now this is successfull, but "public_hash.bin" is an empty file.
I'm stuck here without possibility to continue secure boot setup, e.g burning of OTP with public_hash.bin content.
Thanks in advance for any useful feedback.
Alexandre.
2019-07-01 07:32 AM
Hello Alexandre,
In fact we are able to reproduce your issue on KeyGen from STM32CubeProgrammer Linux version.
This problem has been raised internally and is currently under investigation.
For the time being, If you able to find a Windows PC and install STM32CubeProgrammer in Windows version, and use STM32MP_KeyGen_CLI.exe as you did, it works fine. It is a temporarly workaround until we fix the problem in Linux Host.
Best Regards,
Olivier
2019-07-01 07:40 AM
Hello Olivier,
Thanks a lot for the clarification and I'll try to figure out what I can do with a Windows installed PC.
Can I expect somebody in ST to post on this thread as soon as the Linux issue is fixed ? Or is there any other way to track progress about this issue ?
Indeed, I appreciate the workaround in order to continue secure boot integration on my platform but of course I may wait for Linux issue fixed in order to provide my customer with a "simpler to use" full-Linux solution.
Best Regards,
Alexandre.
2019-07-01 07:46 AM
Hello Alexandre,
Yes of course, as soon as we will find and validate the fix, It will be published in this thread. Regarding your case, you will be also be notified by mail.
Best Regards,
Olivier
2020-03-25 09:16 AM
Hello Alexandre,
I have the same issue.
Did you succeed in solving it ?
Br,
Sebastien
2020-03-26 03:34 AM
Hi Sebastien,
I followed @OlivierK recommendation: I used a Windows installed PC. It was OK.
Still waiting for official answer about fix for the Linux base solution.
2020-12-29 11:43 AM
This is a little hacky, but you can do something like:
${CUBE_BIN}/STM32MP_KeyGen_CLI \
--private-key "${priv}" \
--public-key "${pub}" \
--public-key-hash "${hash}" \
--absolute-path "${signed_images}" \
--password ${pass1} \
--prvkey-enc aes256 \
--ecc-algo prime256v1
if [ "${?}x" != "0x" ]
then
printf "Failed to generate key material\n"
exit 1
fi
[ -f "${signed_images}/${hash}" ] || {
rm -f "${signed_images}/publicKeyhash.bin"
printf "\"%s\" not generated by STM32 tool, generating it manually (requires OpenSSL installed)\n" "${signed_images}/${hash}"
# Extract the key from certificate, and hash it
openssl enc -in "${signed_images}/${pub}" -out /dev/stdout -d -a | \
dd if=/dev/stdin of=/dev/stdout bs=1 skip=27 count=64 | \
openssl dgst -binary -sha256 -out "${signed_images}/${hash}"
if [ "${?}x" != "0x" ]
then
printf "Failed to generate key material\n"
exit 1
fi
}
Roughly, you can extract the public key from the PEM file (first OpenSSL command).
Then for ECC key, you should have a DER encoded "SEQUENCE { SEQUENCE { … } BITSTRING PublicKey }", and drop the two first bytes of PublicKey, to get something of length 64 bytes (well, this might depend on your algorithm), this is what will need to be hashed (with sha256sum for instance, or with the OpenSSL command line I put in the snippet).
Note that if you fear to wrongly extract the public key, an alternative is to sign a random image, using `STM32MP_SigningTool_CLI`, then look at the header, you will find the ecc key in it.
On the stm32 header, the ECC algorithm used is a 32 bits integer at offset 104.
The key itself starts at offset 108 and is a 64 bytes length. It is followed by 83 0x00 bytes, and the `binary_type` byte, ending the 256 bytes long header.
If you have access to a Windows machine, you can test if the generated file matches the one generated by Windows. If so, then you should be able to easily write a script doing that.