Add support for SM3 secure hash
Subsystems (2)
PATCH v2 1/5 lib: Import rol32 function from Linux
In reply to Heiko Schochersm3 crypto algorithm uses rol32 function from linux, so import it. Linux base was:
commit ca91b9500108:("Merge tag 'v6.15-rc4-ksmbd-server-fixes' of git://git.samba.org/ksmbd")
---
Changes in v2:
rebase to6b27b688694: ("Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sh")
add Ilias to Series-cc
include/linux/bitops.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/bitops.h +11 -0
@@ -148,6 +148,17 @@ static inline unsigned long hweight_long(unsigned long w)return sizeof (w) ==4 ?generic_hweight32 (w) :generic_hweight64 (w); } +/** +* rol32 - rotate a 32-bit value left +* @word: value to rotate +* @shift: bits to roll +*/ + +static inline __u32 rol32 (__u32 word,unsigned int shift) +{ +return (word << (shift &31 )) | (word >> ((-shift) &31 )); +} +#include <asm/bitops.h> /* linux/include/asm-generic/bitops/non-atomic.h */
Signature · 1 line
2.20.1
PATCH v2 3/5 test: cmd: hash: add unit test for sm3_256
In reply to Heiko Schocheradd simple test for sm3 256 hash
---
I wonder why this tests are under DM and not under CMD Should we move them to CMD ?
Ignored checkpatch warnings for too long lines.
(no changes since v1)
test/cmd/hash.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
test/cmd/hash.c +46 -0
@@ -103,3 +103,49 @@ static int dm_test_cmd_hash_sha256(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_cmd_hash_sha256,UTF_CONSOLE ); + +static int dm_test_cmd_hash_sm3_256 (struct unit_test_state *uts) +{ +const char *sum ="1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b" ; + +if (!CONFIG_IS_ENABLED (SM3 )) { +ut_assert (run_command ("hash sm3_256 $loadaddr 0" ,0 )); + +return 0 ; + } + +ut_assertok (run_command ("hash sm3_256 $loadaddr 0" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_asserteq_ptr (uts->actual_str, +strstr (uts->actual_str,"sm3_256 for " )); +ut_assert (strstr (uts->actual_str, sum)); +ut_assert_console_end (); + +ut_assertok (run_command ("hash sm3_256 $loadaddr 0 foo; echo $foo" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_asserteq_ptr (uts->actual_str, +strstr (uts->actual_str,"sm3_256 for " )); +ut_assert (strstr (uts->actual_str, sum)); +ut_assertok (ut_check_console_line (uts, sum)); + +if (!CONFIG_IS_ENABLED (HASH_VERIFY )) { +ut_assert (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +ut_assertok (ut_check_console_line (uts, +"hash - compute hash message digest" )); + +return 0 ; + } + +ut_assertok (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +ut_assert_console_end (); + +env_set ("foo" , +"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); +ut_assert (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_assert (strstr (uts->actual_str,"!=" )); +ut_assert_console_end (); + +return 0 ; +} +DM_TEST (dm_test_cmd_hash_sm3_256,UTF_CONSOLE );
Signature · 1 line
2.20.1
PATCH v2 5/5 test: cmd: fix a typo in md5 test
In reply to Heiko SchocherIn dm_test_cmd_hash_md5 accidentially sha256 hash
ist used. Use the correct md5 hash instead.
---
Changes in v2:
Added Reviewed-by tag from Heinrich
test/cmd/hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
test/cmd/hash.c +1 -1
@@ -38,7 +38,7 @@ static int dm_test_cmd_hash_md5(struct unit_test_state *uts) "d41d8cd98f00b204e9800998ecf8427e"));if (!CONFIG_IS_ENABLED (HASH_VERIFY )) { -ut_assert (run_command ("hash -v sha256 $loadaddr 0 foo" ,0 )); +ut_assert (run_command ("hash -v md5 $loadaddr 0 foo" ,0 ));ut_assertok (ut_check_console_line ( uts,"hash - compute hash message digest" ));
Signature · 1 line
2.20.1
PATCH v2 4/5 tpm2: add sm3 256 hash support
In reply to Heiko Schocheradd sm3 256 hash support, so TPM2 chips which report 5 pcrs with sm3 hash do not fail with:
u-boot=> tpm2 autostart
tpm2_get_pcr_info: too many pcrs: 5
Error: -90
---
Changes in v2:
add comments from Ilias
- use ARRAY_SIZE(hash_algo_list) instead of a fix number in tpm2_get_pcr_info() for the count of supported hashes in U-Boot.
- add SM3 hash in tpm_tcg2
cmd/tpm-v2.c | 1 + include/tpm-v2.h | 12 ++++++++++++ lib/tpm-v2.c | 4 ++-- lib/tpm_tcg2.c | 9 +++++++++ 4 files changed, 24 insertions(+), 2 deletions(-)
cmd/tpm-v2.c +1 -0
@@ -589,6 +589,7 @@ U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command"," * sha256\n" " * sha384\n" " * sha512\n" +" * sm3_256\n" " <on|off> is one of:\n" " * on - Select all available PCRs associated with the specified\n" " algorithm (bank)\n"
include/tpm-v2.h +12 -0
@@ -345,6 +345,18 @@ static const struct digest_info hash_algo_list[] = { false, #endif }, + { +"sm3_256" , +TPM2_ALG_SM3_256 , +TCG2_BOOT_HASH_ALG_SM3_256 , +TPM2_SM3_256_DIGEST_SIZE , +#if IS_ENABLED(CONFIG_SM3) + true, +#else + false, +#endif + }, + };/* NV index attributes */
lib/tpm-v2.c +2 -2
@@ -686,10 +686,10 @@ int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs) pcrs->count =get_unaligned_be32 (response);/* -* We only support 4 algorithms for now so check against that +* check against the supported algorithms in hash_algo_list, * instead of TPM2_NUM_PCR_BANKS */ -if (pcrs->count >4 || pcrs->count <1 ) { +if (pcrs->count >ARRAY_SIZE (hash_algo_list) || pcrs->count <1 ) {printf ("%s: too many pcrs: %u\n" , __func__, pcrs->count);return -EMSGSIZE ; }
lib/tpm_tcg2.c +9 -0
@@ -12,6 +12,7 @@#include <u-boot/sha1.h> #include <u-boot/sha256.h> #include <u-boot/sha512.h> +#include <u-boot/sm3.h> #include <version_string.h> #include <asm/io.h> #include <linux/bitops.h>
@@ -143,6 +144,12 @@ int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length,sha512_finish (&ctx_512, final); len =TPM2_SHA512_DIGEST_SIZE ;break ; +#endif +#if IS_ENABLED (CONFIG_SM3 ) +case TPM2_ALG_SM3_256 : +sm3_hash (input, length, final); + len =TPM2_SM3_256_DIGEST_SIZE ; +break ;#endif default : printf("%s: unsupported algorithm %x\n", __func__,
@@ -319,6 +326,7 @@ static int tcg2_replay_eventlog(struct tcg2_event_log *elog,case TPM2_ALG_SHA256 :case TPM2_ALG_SHA384 :case TPM2_ALG_SHA512 : +case TPM2_ALG_SM3_256 : len =tpm2_algorithm_to_len (algo);break ;default :
@@ -431,6 +439,7 @@ static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog,case TPM2_ALG_SHA256 :case TPM2_ALG_SHA384 :case TPM2_ALG_SHA512 : +case TPM2_ALG_SM3_256 : len =get_unaligned_le16 (&event->digest_sizes[i].digest_size);if (tpm2_algorithm_to_len (algo) != len) {log_err ("EventLog invalid algorithm length\n" );
Signature · 1 line
2.20.1
Re: [PATCH v2 3/5] test: cmd: hash: add unit test for sm3_256
In reply to Heiko SchocherAm 11. November 2025 06:48:10 MEZ schrieb Heiko Schocher <[email protected]>:
Quoted 6 lines
add simple test for sm3 256 hash
Signed-off-by Heiko Schocher <[email protected]>---
I wonder why this tests are under DM and not under CMD Should we move them to CMD ?
We should use CMD_TEST() in the test/cmd/ directory.
Best regards
Heinrich
Quoted 62 lines
Ignored checkpatch warnings for too long lines.
(no changes since v1)
test/cmd/hash.c| 46 ++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 46 insertions(+)test/cmd/hash.c +46
-0@@ -103,3 +103,49 @@ static int dm_test_cmd_hash_sha256(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_cmd_hash_sha256,UTF_CONSOLE ); + +static int dm_test_cmd_hash_sm3_256 (struct unit_test_state *uts) +{ +const char *sum ="1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b" ; + +if (!CONFIG_IS_ENABLED (SM3 )) { +ut_assert (run_command ("hash sm3_256 $loadaddr 0" ,0 )); + +return 0 ; + } + +ut_assertok (run_command ("hash sm3_256 $loadaddr 0" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_asserteq_ptr (uts->actual_str, +strstr (uts->actual_str,"sm3_256 for " )); +ut_assert (strstr (uts->actual_str, sum)); +ut_assert_console_end (); + +ut_assertok (run_command ("hash sm3_256 $loadaddr 0 foo; echo $foo" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_asserteq_ptr (uts->actual_str, +strstr (uts->actual_str,"sm3_256 for " )); +ut_assert (strstr (uts->actual_str, sum)); +ut_assertok (ut_check_console_line (uts, sum)); + +if (!CONFIG_IS_ENABLED (HASH_VERIFY )) { +ut_assert (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +ut_assertok (ut_check_console_line (uts, +"hash - compute hash message digest" )); + +return 0 ; + } + +ut_assertok (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +ut_assert_console_end (); + +env_set ("foo" , +"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); +ut_assert (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_assert (strstr (uts->actual_str,"!=" )); +ut_assert_console_end (); + +return 0 ; +} +DM_TEST (dm_test_cmd_hash_sm3_256,UTF_CONSOLE );
Re: [PATCH v2 5/5] test: cmd: fix a typo in md5 test
In reply to Heiko SchocherQuoted 6 lines · Heiko Schocher
In
dm_test_cmd_hash_md5accidentially sha256 hash
ist used. Use the correct md5 hash instead.Signed-off-by Heiko Schocher <[email protected]>Reviewed-by Heinrich Schuchardt <[email protected]>
Quoted 25 lines
---
Changes in v2:
Added Reviewed-by tag from Heinrichtest/cmd/hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)test/cmd/hash.c +1
-2@@ -38,7 +38,7 @@ static int dm_test_cmd_hash_md5(struct unit_test_state *uts) "d41d8cd98f00b204e9800998ecf8427e"));if (!CONFIG_IS_ENABLED (HASH_VERIFY )) { -ut_assert (run_command ("hash -v sha256 $loadaddr 0 foo" ,0 )); +ut_assert (run_command ("hash -v md5 $loadaddr 0 foo" ,0 ));ut_assertok (ut_check_console_line ( uts,"hash - compute hash message digest" )); --2.20.1
Re: [PATCH v2 1/5] lib: Import rol32 function from Linux
In reply to Heiko SchocherQuoted 7 lines · Heiko Schocher
sm3 crypto algorithm uses rol32 function from linux, so import it. Linux base was:
commit ca91b9500108:("Merge tag 'v6.15-rc4-ksmbd-server-fixes' of
git://git.samba.org/ksmbd")Signed-off-by Heiko Schocher <[email protected]>
Quoted 35 lines
---
Changes in v2:
rebase to6b27b688694: ("Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sh")
add Ilias to Series-ccinclude/linux/bitops.h | 11 +++++++++++ 1 file changed, 11 insertions(+)include/linux/bitops.h +11
-1@@ -148,6 +148,17 @@ static inline unsigned long hweight_long(unsigned long w)return sizeof (w) ==4 ?generic_hweight32 (w) :generic_hweight64 (w); } +/** +* rol32 - rotate a 32-bit value left +* @word: value to rotate +* @shift: bits to roll +*/ + +static inline __u32 rol32 (__u32 word,unsigned int shift) +{ +return (word << (shift &31 )) | (word >> ((-shift) &31 )); +} +#include <asm/bitops.h> /* linux/include/asm-generic/bitops/non-atomic.h */ --2.20.1
Re: [PATCH v2 4/5] tpm2: add sm3 256 hash support
In reply to Heiko SchocherHi Heiko,
This all looks reasonable.
There's one place I forgot to mention though. tcg2_hash_pe_image()
also needs SM3 support.
The easier way to test that SM3 is working is boot your device and look at the PCR measurements
- 'tpm2_pcrread' -- The SM3 bank should be != 0
- tpm2_eventlog /sys/kernel/security/tpm0/binary_bios_measurements should also include SM3
Thanks
/Ilias
Quoted 121 lines · Heiko Schocher
add sm3 256 hash support, so TPM2 chips which report 5 pcrs with sm3 hash do not fail with:
u-boot=> tpm2 autostart
tpm2_get_pcr_info: too many pcrs: 5
Error: -90Signed-off-by Heiko Schocher <[email protected]>---
Changes in v2:
add comments from Ilias
- useARRAY_SIZE(hash_algo_list)instead of a fix number intpm2_get_pcr_info()for the count of supported hashes in U-Boot.
- add SM3 hash intpm_tcg2cmd/tpm-v2.c | 1 + include/tpm-v2.h | 12 ++++++++++++ lib/tpm-v2.c | 4 ++-- lib/tpm_tcg2.c | 9 +++++++++ 4 files changed, 24 insertions(+), 2 deletions(-)cmd/tpm-v2.c +1
-0@@ -589,6 +589,7 @@ U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command"," * sha256\n" " * sha384\n" " * sha512\n" +" * sm3_256\n" " <on|off> is one of:\n" " * on - Select all available PCRs associated with the specified\n" " algorithm (bank)\n" include/tpm-v2.h +12
-0@@ -345,6 +345,18 @@ static const struct digest_info hash_algo_list[] = { false, #endif }, + { +"sm3_256" , +TPM2_ALG_SM3_256 , +TCG2_BOOT_HASH_ALG_SM3_256 , +TPM2_SM3_256_DIGEST_SIZE , +#if IS_ENABLED(CONFIG_SM3) + true, +#else + false, +#endif + }, + };/* NV index attributes */ lib/tpm-v2.c +2
-2@@ -686,10 +686,10 @@ int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs) pcrs->count =get_unaligned_be32 (response);/* -* We only support 4 algorithms for now so check against that +* check against the supported algorithms in hash_algo_list, * instead of TPM2_NUM_PCR_BANKS */ -if (pcrs->count >4 || pcrs->count <1 ) { +if (pcrs->count >ARRAY_SIZE (hash_algo_list) || pcrs->count <1 ) {printf ("%s: too many pcrs: %u\n" , __func__, pcrs->count);return -EMSGSIZE ; }lib/tpm_tcg2.c +9
-1@@ -12,6 +12,7 @@#include <u-boot/sha1.h> #include <u-boot/sha256.h> #include <u-boot/sha512.h> +#include <u-boot/sm3.h> #include <version_string.h> #include <asm/io.h> #include <linux/bitops.h> @@ -143,6 +144,12 @@ int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length,sha512_finish (&ctx_512, final); len =TPM2_SHA512_DIGEST_SIZE ;break ; +#endif +#if IS_ENABLED (CONFIG_SM3 ) +case TPM2_ALG_SM3_256 : +sm3_hash (input, length, final); + len =TPM2_SM3_256_DIGEST_SIZE ; +break ;#endif default : printf("%s: unsupported algorithm %x\n", __func__,@@ -319,6 +326,7 @@ static int tcg2_replay_eventlog(struct tcg2_event_log *elog,case TPM2_ALG_SHA256 :case TPM2_ALG_SHA384 :case TPM2_ALG_SHA512 : +case TPM2_ALG_SM3_256 : len =tpm2_algorithm_to_len (algo);break ;default :@@ -431,6 +439,7 @@ static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog,case TPM2_ALG_SHA256 :case TPM2_ALG_SHA384 :case TPM2_ALG_SHA512 : +case TPM2_ALG_SM3_256 : len =get_unaligned_le16 (&event->digest_sizes[i].digest_size);if (tpm2_algorithm_to_len (algo) != len) {log_err ("EventLog invalid algorithm length\n" ); --2.20.1
Re: [PATCH v2 4/5] tpm2: add sm3 256 hash support
[...]
Quoted 8 lines
lib/tpm_tcg2.c int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length, · line 150
Quoted 8 lines
lib/tpm_tcg2.c +4
-0@@ -143,6 +144,12 @@ int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length,sha512_finish (&ctx_512, final); len =TPM2_SHA512_DIGEST_SIZE ;break ; +#endif +#if IS_ENABLED (CONFIG_SM3 ) +case TPM2_ALG_SM3_256 : +sm3_hash (input, length, final);
I haven't looked at the sm3 implementation yet, but it doesn't have the usual start
update
finish
?
Thanks
/Ilias
Quoted 24 lines
Quoted 24 lines
lib/tpm_tcg2.c +4
-1+ len =TPM2_SM3_256_DIGEST_SIZE ; +break ;#endif default : printf("%s: unsupported algorithm %x\n", __func__,@@ -319,6 +326,7 @@ static int tcg2_replay_eventlog(struct tcg2_event_log *elog,case TPM2_ALG_SHA256 :case TPM2_ALG_SHA384 :case TPM2_ALG_SHA512 : +case TPM2_ALG_SM3_256 : len =tpm2_algorithm_to_len (algo);break ;default :@@ -431,6 +439,7 @@ static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog,case TPM2_ALG_SHA256 :case TPM2_ALG_SHA384 :case TPM2_ALG_SHA512 : +case TPM2_ALG_SM3_256 : len =get_unaligned_le16 (&event->digest_sizes[i].digest_size);if (tpm2_algorithm_to_len (algo) != len) {log_err ("EventLog invalid algorithm length\n" ); --2.20.1
v2 cover Add support for SM3 secure hash
Add SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and described at https://datatracker.ietf.org/doc/html/draft-sca-cfrg-sm3-02
TPMv2 defines hash algo sm3_256, which is currently not supported and prevented TPMv2 chip with newer firmware to work with U-Boot. Seen this on a ST33TPHF2XI2C
u-boot=> tpm2 init
u-boot=> tpm2 autostart
tpm2_get_pcr_info: too many pcrs: 5
Error: -90
u-boot=>
Implement sm3 hash, so we can fix this problem.
Azure build:
https://dev.azure.com/hs0298/hs/_build/results?buildId=190&view=results
Changes in v2:
rebase to6b27b688694: ("Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sh")
add Ilias to Series-cc
add sm3_hash to header file, so we can use it.
add comments from Ilias
- use ARRAY_SIZE(hash_algo_list) instead of a fix number
in tpm2_get_pcr_info() for the count of supported hashes
in U-Boot.
- add SM3 hash in tpm_tcg2
Added Reviewed-by tag from Heinrich
Heiko Schocher (5):
lib: Import rol32 function from Linux
lib: implement sm3 256 hash
test: cmd: hash: add unit test for sm3_256
tpm2: add sm3 256 hash support
test: cmd: fix a typo in md5 test
MAINTAINERS | 7 + boot/Kconfig | 1 + cmd/Kconfig | 15 ++ cmd/Makefile | 1 + cmd/sm3sum.c | 48 +++++++ cmd/tpm-v2.c | 1 + common/hash.c | 43 +++++- include/linux/bitops.h | 11 ++ include/tpm-v2.h | 12 ++ include/u-boot/sm3.h | 35 +++++ lib/Kconfig | 7 + lib/Makefile | 1 + lib/sm3.c | 313 +++++++++++++++++++++++++++++++++++++++++ lib/tpm-v2.c | 4 +- lib/tpm_tcg2.c | 9 ++ test/cmd/hash.c | 48 ++++++- 16 files changed, 552 insertions(+), 4 deletions(-) create mode 100644 cmd/sm3sum.c create mode 100644 include/u-boot/sm3.h create mode 100644 lib/sm3.c
Signature · 3 lines
2.20.1 base-commit: 9094482ca7576877b2bfaa57c8e73cfeb536f8b9
PATCH v2 2/5 lib: implement sm3 256 hash
Implement SM3_256 Hash algorithm, based on
linux commit f83a4f2a4d8c: ("Merge tag 'erofs-for-6.17-rc6-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs")
---
This patch drops a lot of checkpatch warnings, ignored them as tried to stay as close as possible with linux code.
Changes in v2:
add sm3_hash to header file, so we can use it.
MAINTAINERS | 7 + boot/Kconfig | 1 + cmd/Kconfig | 15 +++ cmd/Makefile | 1 + cmd/sm3sum.c | 48 +++++++ common/hash.c | 43 +++++- include/u-boot/sm3.h | 35 +++++ lib/Kconfig | 7 + lib/Makefile | 1 + lib/sm3.c | 313 +++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 470 insertions(+), 1 deletion(-) create mode 100644 cmd/sm3sum.c create mode 100644 include/u-boot/sm3.h create mode 100644 lib/sm3.c
MAINTAINERS +7 -0
@@ -1672,6 +1672,13 @@ F: include/slre.h F: lib/slre.c F: test/lib/slre.c +SM3 +M: Heiko Schocher <[email protected]> +S: Maintained +F: cmd/sm3sum.c +F: include/u-boot/sm3.h +F: lib/sm3.c + SMCCC TRNG M: Etienne Carriere <[email protected]> S: Maintained
boot/Kconfig +1 -0
@@ -1024,6 +1024,7 @@ config MEASURED_BOOT select SHA256 select SHA384 select SHA512 + select SM3 help This option enables measurement of the boot process when booting without UEFI . Measurement involves creating cryptographic hashes
cmd/Kconfig +15 -0
@@ -264,6 +264,21 @@ config CMD_SBI help Display information about the SBI implementation. +config CMD_SM3SUM + bool "sm3sum" + select SM3 + select HASH + help + Compute SM3 checksum. + add SM3 hash functionality + +config SM3SUM_VERIFY + bool "sm3sum -v" + depends on CMD_SM3SUM + help + Add for the sm3sum command the -v option + to verify data against an SM3 checksum. + config CMD_SMBIOS bool "smbios" depends on SMBIOS
cmd/Makefile +1 -0
@@ -177,6 +177,7 @@ obj-$(CONFIG_CMD_SETEXPR) += setexpr.o obj-$(CONFIG_CMD_SETEXPR_FMT) += printf.o obj-$(CONFIG_CMD_SPI) += spi.o obj-$(CONFIG_CMD_STRINGS) += strings.o +obj-$(CONFIG_CMD_SM3SUM) += sm3sum.o obj-$(CONFIG_CMD_SMBIOS) += smbios.o obj-$(CONFIG_CMD_SMC) += smccc.o obj-$(CONFIG_CMD_SYSBOOT) += sysboot.o
cmd/sm3sum.c new file +48 -0
48 lines not on this page · raw message
common/hash.c +42 -1
67 lines not on this page · raw message
include/u-boot/sm3.h new file +35 -0
@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0-only +#ifndef _SM3_H +#define _SM3_H + +#define SM3_DIGEST_SIZE 32/* 256 bits */ +#define SM3_BLOCK_SIZE 64/* 512 bits */ +#define SM3_PAD_UNIT 56/* 448 bits */ + +#define SM3_T1 0x79CC4519 +#define SM3_T2 0x7A879D8A + +#define SM3_IVA 0x7380166f +#define SM3_IVB 0x4914b2b9 +#define SM3_IVC 0x172442d7 +#define SM3_IVD 0xda8a0600 +#define SM3_IVE 0xa96f30bc +#define SM3_IVF 0x163138aa +#define SM3_IVG 0xe38dee4d +#define SM3_IVH 0xb0fb0e4e + +struct sm3_context { +uint32_t state[SM3_DIGEST_SIZE /4 ]; +uint64_t count;/* Message length in bits */ +uint8_t buffer[SM3_BLOCK_SIZE ]; +int buflen; +}; + +void sm3_init (struct sm3_context *sctx); +void sm3_update (struct sm3_context *sctx,const uint8_t *input,size_t ilen); +void sm3_final (struct sm3_context *sctx,uint8_t output[SM3_DIGEST_SIZE ]); +void sm3_hash (const uint8_t *input,size_t ilen,uint8_t output[SM3_DIGEST_SIZE ]); + +void sm3_csum_wd (const unsigned char *input,uint32_t len, +unsigned char *output,unsigned int chunk_sz); +#endif
lib/Kconfig +7 -0
@@ -606,6 +606,13 @@ config SHA384 The SHA384 algorithm produces a 384-bit (48-byte) hash value (digest). +config SM3 + bool "Enable SM3 support" + help + This option enables support of hashing using + SM3 (ShangMi 3) secure hash function (OSCCA GM/T 0004-2012, ISO/IEC 10118-3) + The hash is calculated in software. + config SHA_HW_ACCEL bool "Enable hardware acceleration for SHA hash functions" help
lib/Makefile +1 -0
@@ -81,6 +81,7 @@ obj-$(CONFIG_$(PHASE_)SHA1_LEGACY) += sha1.o obj-$(CONFIG_$(PHASE_)SHA256) += sha256_common.o obj-$(CONFIG_$(PHASE_)SHA256_LEGACY) += sha256.o obj-$(CONFIG_$(PHASE_)SHA512_LEGACY) += sha512.o +obj-$(CONFIG_$(PHASE_)SM3) += sm3.o obj-$(CONFIG_CRYPT_PW) += crypt/ obj-$(CONFIG_$(PHASE_)ASN1_DECODER_LEGACY) += asn1_decoder.o
lib/sm3.c new file +313 -0
313 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v2 2/5] lib: implement sm3 256 hash
I think this would be easier to review/ack if you split the cmd implementation from the actual hashing algo.
[...]
lib/sm3.c line 182
Quoted 3 lines
lib/sm3.c new file +3
-0+void sm3_init (struct sm3_context *sctx) +{ +memset (sctx,0 ,sizeof (struct sm3_context ));
I usually prefer sizeof(*sctx), but up to you
Quoted 49 lines
lib/sm3.c new file +49
-0+ +/* Load initial values */ + sctx->state[0 ] =SM3_IVA ; + sctx->state[1 ] =SM3_IVB ; + sctx->state[2 ] =SM3_IVC ; + sctx->state[3 ] =SM3_IVD ; + sctx->state[4 ] =SM3_IVE ; + sctx->state[5 ] =SM3_IVF ; + sctx->state[6 ] =SM3_IVG ; + sctx->state[7 ] =SM3_IVH ; + sctx->count =0 ; +} + +static inline void sm3_block (struct sm3_context *sctx, +u8 const *data,int blocks,u32 W [16 ]) +{ +while (blocks--) { +sm3_transform (sctx, data,W ); + data +=SM3_BLOCK_SIZE ; + } +} + +void sm3_update (struct sm3_context *sctx,const uint8_t *input,size_t ilen) +{ +unsigned int partial = sctx->count %SM3_BLOCK_SIZE ; +u32 W [16 ]; + + sctx->count += ilen; + +if ((partial + ilen) >=SM3_BLOCK_SIZE ) { +int blocks; + +if (partial) { +int p =SM3_BLOCK_SIZE - partial; + +memcpy (sctx->buffer + partial, input, p); + input += p; + ilen -= p; + +sm3_block (sctx, sctx->buffer,1 ,W ); + } + + blocks = ilen /SM3_BLOCK_SIZE ; + ilen %=SM3_BLOCK_SIZE ; + +if (blocks) { +sm3_block (sctx, input, blocks,W ); + input += blocks *SM3_BLOCK_SIZE ; + }
lib/sm3.c line 241
Quoted 10 lines
lib/sm3.c new file +10
-0+ +memset (W ,0 ,sizeof (W )); + + partial =0 ; + } +if (ilen) +memcpy (sctx->buffer + partial, input, ilen); +} + +void sm3_final (struct sm3_context *sctx,uint8_t output[SM3_DIGEST_SIZE ])
I think it's more readable to just define this as output[] [...]
lib/sm3.c line 283
Quoted 13 lines
lib/sm3.c new file +13
-0+/** +* sm3_hash - Calculate SM3 hash of input data +* @input: Input data +* @ilen: Input data length in bytes +* @output: Output buffer for hash (32 bytes) +*/ +void sm3_hash (const uint8_t *input,size_t ilen,uint8_t output[SM3_DIGEST_SIZE ]) +{ +struct sm3_context sctx; + +sm3_init (&sctx); +sm3_update (&sctx, input, ilen); +sm3_final (&sctx, output);
Ah ignore my comment on patch #4
init, update, final is wrapped in this function
[...]
Thanks
/Ilias
Re: [PATCH v2 3/5] test: cmd: hash: add unit test for sm3_256
In reply to Heinrich SchuchardtHello Heinrich,
Quoted 9 lines · Heinrich Schuchardt
Am 11. November 2025 06:48:10 MEZ schrieb Heiko Schocher <[email protected]>:
Quoted 6 lines
add simple test for sm3 256 hash
Signed-off-by Heiko Schocher <[email protected]>---
I wonder why this tests are under DM and not under CMD Should we move them to CMD ?We should use
CMD_TEST()in the test/cmd/ directory.
I aggree... candidates would be:
$ grep -lr DM_TEST test/cmd test/cmd/temperature.c test/cmd/pwm.c test/cmd/hash.c test/cmd/pinmux.c test/cmd/rw.c test/cmd/armffa.c test/cmd/loadm.c test/cmd/i3c.c
but that change should be done in seperate series...
I can make such a series, if this one is acceppted?
Thanks!
bye,
Heiko
Quoted 68 lines
Best regards
Heinrich
Quoted 62 lines
Ignored checkpatch warnings for too long lines.
(no changes since v1)
test/cmd/hash.c| 46 ++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 46 insertions(+)test/cmd/hash.c +46
-0@@ -103,3 +103,49 @@ static int dm_test_cmd_hash_sha256(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_cmd_hash_sha256,UTF_CONSOLE ); + +static int dm_test_cmd_hash_sm3_256 (struct unit_test_state *uts) +{ +const char *sum ="1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b" ; + +if (!CONFIG_IS_ENABLED (SM3 )) { +ut_assert (run_command ("hash sm3_256 $loadaddr 0" ,0 )); + +return 0 ; + } + +ut_assertok (run_command ("hash sm3_256 $loadaddr 0" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_asserteq_ptr (uts->actual_str, +strstr (uts->actual_str,"sm3_256 for " )); +ut_assert (strstr (uts->actual_str, sum)); +ut_assert_console_end (); + +ut_assertok (run_command ("hash sm3_256 $loadaddr 0 foo; echo $foo" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_asserteq_ptr (uts->actual_str, +strstr (uts->actual_str,"sm3_256 for " )); +ut_assert (strstr (uts->actual_str, sum)); +ut_assertok (ut_check_console_line (uts, sum)); + +if (!CONFIG_IS_ENABLED (HASH_VERIFY )) { +ut_assert (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +ut_assertok (ut_check_console_line (uts, +"hash - compute hash message digest" )); + +return 0 ; + } + +ut_assertok (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +ut_assert_console_end (); + +env_set ("foo" , +"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ); +ut_assert (run_command ("hash -v sm3_256 $loadaddr 0 foo" ,0 )); +console_record_readline (uts->actual_str,sizeof (uts->actual_str)); +ut_assert (strstr (uts->actual_str,"!=" )); +ut_assert_console_end (); + +return 0 ; +} +DM_TEST (dm_test_cmd_hash_sm3_256,UTF_CONSOLE );
Signature · 5 lines
Nabla Software Engineering HRB 40522 Augsburg Phone: +49 821 45592596 E-Mail: [email protected] Geschäftsführer : Stefano Babic
Re: [PATCH v2 4/5] tpm2: add sm3 256 hash support
In reply to Ilias ApalodimasHello Ilias,
Quoted 5 lines · Ilias Apalodimas
Hi Heiko,
This all looks reasonable.
There's one place I forgot to mention though.tcg2_hash_pe_image()
also needs SM3 support.
I take a look!
Quoted 5 lines
The easier way to test that SM3 is working is boot your device and look at the PCR measurements
- 'tpm2_pcrread' -- The SM3 bank should be != 0
-tpm2_eventlog/sys/kernel/security/tpm0/binary_bios_measurementsshould also include SM3
I give it a try ...
Thanks!
bye,
Heiko
Quoted 126 lines
Thanks
/IliasQuoted 121 lines · Heiko Schocher
add sm3 256 hash support, so TPM2 chips which report 5 pcrs with sm3 hash do not fail with:
u-boot=> tpm2 autostart
tpm2_get_pcr_info: too many pcrs: 5
Error: -90Signed-off-by Heiko Schocher <[email protected]>---
Changes in v2:
add comments from Ilias
- useARRAY_SIZE(hash_algo_list)instead of a fix number intpm2_get_pcr_info()for the count of supported hashes in U-Boot.
- add SM3 hash intpm_tcg2cmd/tpm-v2.c | 1 + include/tpm-v2.h | 12 ++++++++++++ lib/tpm-v2.c | 4 ++-- lib/tpm_tcg2.c | 9 +++++++++ 4 files changed, 24 insertions(+), 2 deletions(-)cmd/tpm-v2.c +1
-0@@ -589,6 +589,7 @@ U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command"," * sha256\n" " * sha384\n" " * sha512\n" +" * sm3_256\n" " <on|off> is one of:\n" " * on - Select all available PCRs associated with the specified\n" " algorithm (bank)\n" include/tpm-v2.h +12
-0@@ -345,6 +345,18 @@ static const struct digest_info hash_algo_list[] = { false, #endif }, + { +"sm3_256" , +TPM2_ALG_SM3_256 , +TCG2_BOOT_HASH_ALG_SM3_256 , +TPM2_SM3_256_DIGEST_SIZE , +#if IS_ENABLED(CONFIG_SM3) + true, +#else + false, +#endif + }, + };/* NV index attributes */ lib/tpm-v2.c +2
-2@@ -686,10 +686,10 @@ int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs) pcrs->count =get_unaligned_be32 (response);/* -* We only support 4 algorithms for now so check against that +* check against the supported algorithms in hash_algo_list, * instead of TPM2_NUM_PCR_BANKS */ -if (pcrs->count >4 || pcrs->count <1 ) { +if (pcrs->count >ARRAY_SIZE (hash_algo_list) || pcrs->count <1 ) {printf ("%s: too many pcrs: %u\n" , __func__, pcrs->count);return -EMSGSIZE ; }lib/tpm_tcg2.c +9
-1@@ -12,6 +12,7 @@#include <u-boot/sha1.h> #include <u-boot/sha256.h> #include <u-boot/sha512.h> +#include <u-boot/sm3.h> #include <version_string.h> #include <asm/io.h> #include <linux/bitops.h> @@ -143,6 +144,12 @@ int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length,sha512_finish (&ctx_512, final); len =TPM2_SHA512_DIGEST_SIZE ;break ; +#endif +#if IS_ENABLED (CONFIG_SM3 ) +case TPM2_ALG_SM3_256 : +sm3_hash (input, length, final); + len =TPM2_SM3_256_DIGEST_SIZE ; +break ;#endif default : printf("%s: unsupported algorithm %x\n", __func__,@@ -319,6 +326,7 @@ static int tcg2_replay_eventlog(struct tcg2_event_log *elog,case TPM2_ALG_SHA256 :case TPM2_ALG_SHA384 :case TPM2_ALG_SHA512 : +case TPM2_ALG_SM3_256 : len =tpm2_algorithm_to_len (algo);break ;default :@@ -431,6 +439,7 @@ static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog,case TPM2_ALG_SHA256 :case TPM2_ALG_SHA384 :case TPM2_ALG_SHA512 : +case TPM2_ALG_SM3_256 : len =get_unaligned_le16 (&event->digest_sizes[i].digest_size);if (tpm2_algorithm_to_len (algo) != len) {log_err ("EventLog invalid algorithm length\n" ); --2.20.1
Signature · 5 lines
Nabla Software Engineering HRB 40522 Augsburg Phone: +49 821 45592596 E-Mail: [email protected] Geschäftsführer : Stefano Babic
Re: [PATCH v2 2/5] lib: implement sm3 256 hash
In reply to Ilias ApalodimasHello Ilias,
Quoted 2 lines · Ilias Apalodimas
I think this would be easier to review/ack if you split the cmd implementation from the actual hashing algo.
Okay, I will split it in v3 into 2 patches.
Quoted 8 lines
[...]
lib/sm3.c line 182
Quoted 3 lines
lib/sm3.c new file +3
-0+void sm3_init (struct sm3_context *sctx) +{ +memset (sctx,0 ,sizeof (struct sm3_context ));I usually prefer
sizeof(*sctx), but up to you
Hmm.. I have no preference here ... so I change it.
Quoted 54 lines
Quoted 54 lines
lib/sm3.c new file +54
-0+ +/* Load initial values */ + sctx->state[0 ] =SM3_IVA ; + sctx->state[1 ] =SM3_IVB ; + sctx->state[2 ] =SM3_IVC ; + sctx->state[3 ] =SM3_IVD ; + sctx->state[4 ] =SM3_IVE ; + sctx->state[5 ] =SM3_IVF ; + sctx->state[6 ] =SM3_IVG ; + sctx->state[7 ] =SM3_IVH ; + sctx->count =0 ; +} + +static inline void sm3_block (struct sm3_context *sctx, +u8 const *data,int blocks,u32 W [16 ]) +{ +while (blocks--) { +sm3_transform (sctx, data,W ); + data +=SM3_BLOCK_SIZE ; + } +} + +void sm3_update (struct sm3_context *sctx,const uint8_t *input,size_t ilen) +{ +unsigned int partial = sctx->count %SM3_BLOCK_SIZE ; +u32 W [16 ]; + + sctx->count += ilen; + +if ((partial + ilen) >=SM3_BLOCK_SIZE ) { +int blocks; + +if (partial) { +int p =SM3_BLOCK_SIZE - partial; + +memcpy (sctx->buffer + partial, input, p); + input += p; + ilen -= p; + +sm3_block (sctx, sctx->buffer,1 ,W ); + } + + blocks = ilen /SM3_BLOCK_SIZE ; + ilen %=SM3_BLOCK_SIZE ; + +if (blocks) { +sm3_block (sctx, input, blocks,W ); + input += blocks *SM3_BLOCK_SIZE ; + } + +memset (W ,0 ,sizeof (W )); + + partial =0 ; + }
Quoted 8 lines
lib/sm3.c line 241
Quoted 5 lines
lib/sm3.c new file +5
-0+if (ilen) +memcpy (sctx->buffer + partial, input, ilen); +} + +void sm3_final (struct sm3_context *sctx,uint8_t output[SM3_DIGEST_SIZE ])I think it's more readable to just define this as output[] [...]
okay, I also change it in sm3_hash()
Quoted 17 lines
Quoted 4 lines
lib/sm3.c new file +3
-0+/** +* sm3_hash - Calculate SM3 hash of input data +* @input: Input data lib/sm3.c line 283
Quoted 10 lines
lib/sm3.c new file +10
-0+* @ilen: Input data length in bytes +* @output: Output buffer for hash (32 bytes) +*/ +void sm3_hash (const uint8_t *input,size_t ilen,uint8_t output[SM3_DIGEST_SIZE ]) +{ +struct sm3_context sctx; + +sm3_init (&sctx); +sm3_update (&sctx, input, ilen); +sm3_final (&sctx, output);Ah ignore my comment on patch #4
init, update, final is wrapped in this function
Yep, it saves in lib/tpm_tcg2.c tcg2_create_digest() another ifdef in the variable declaration part.
bye,
Heiko
Quoted 6 lines
[...]
Thanks
/Ilias
Signature · 5 lines
Nabla Software Engineering HRB 40522 Augsburg Phone: +49 821 45592596 E-Mail: [email protected] Geschäftsführer : Stefano Babic