add the support of sha256_hmac and sha256_hkdf
v6 cover add the support of sha256_hmac and sha256_hkdf
This serie adds the support of sha256_hmac and sha256_hkdf. A first version was sent several months ago just before the integration of mbedtls. This new version is based on mbedtls.
The first patch of this serie add the support of hkdf using mbedtls.
v6:
- use const for test
- set buffer to 0 before using to to compute hmac
v5:
- use mbedtls to compute hmac
- add tests for hkdf
- enabled mbedtls in sandbox_defconfig
- some cleanup
v4:
- fix computation of the hmac
- add more test for hmac
- move sha256 common functions to sha256_common.c
- some cleanup
v3:
- also define sha256_hmac for legacy sha256
- add some #if to define function only when needed
Philippe Reynes (9):
tools: kwbimage.h: use linux/compiler_attributes.h
tools: renesas_spkgimage.h: use linux/compiler_attributes.h
mbedtls: enable support of hkdf
lib: sha256: move common function to sha256_common.c
lib: sha256: add feature sha256_hmac
test: lib: add test for sha256_hmac
lib: mbedtls: sha256: add support of key derivation
test: lib: add test for key derivation
configs: sandbox: enable mbedtls
configs/sandbox_defconfig | 2 + include/u-boot/sha256.h | 24 +++ lib/Makefile | 1 + lib/mbedtls/Kconfig | 14 ++ lib/mbedtls/Makefile | 2 + lib/mbedtls/mbedtls_def_config.h | 4 + lib/mbedtls/sha256.c | 59 ++++--- lib/sha256.c | 67 ++++--- lib/sha256_common.c | 50 ++++++ test/lib/Makefile | 2 + test/lib/test_sha256_hkdf.c | 198 +++++++++++++++++++++ test/lib/test_sha256_hmac.c | 294 +++++++++++++++++++++++++++++++ tools/Makefile | 1 + tools/kwbimage.h | 6 +- tools/renesas_spkgimage.h | 6 +- 15 files changed, 670 insertions(+), 60 deletions(-) create mode 100644 lib/sha256_common.c create mode 100644 test/lib/test_sha256_hkdf.c create mode 100644 test/lib/test_sha256_hmac.c
Signature · 1 line
2.25.1
PATCH v6 1/9 tools: kwbimage.h: use linux/compiler_attributes.h
The attribute __packed was defined in the filetools/kwbimage.h but this attribute is already
defined in linux/compiler_attributes.h. So we
include <linux/compiler_attributes.h.h> and
do not define __packed.
--- tools/kwbimage.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
tools/kwbimage.h +1 -5
@@ -11,11 +11,7 @@#include <compiler.h> #include <stdint.h> -#ifdef __GNUC__ -#define __packed __attribute((packed)) -#else -#define __packed -#endif +#include <linux/compiler_attributes.h> #define KWBIMAGE_MAX_CONFIG ((0x1dc - 0x20)/sizeof(struct reg_config))#define MAX_TEMPBUF_LEN 32
Signature · 1 line
2.25.1
PATCH v6 2/9 tools: renesas_spkgimage.h: use linux/compiler_attributes.h
In reply to Philippe ReynesThe attribute __packed was defined in the file tools/renesas_spkgimage.h but this attribute is already defined in linux/compiler_attributes.h. So we include <linux/compiler_attributes.h.h> and do not define __packed.
--- tools/renesas_spkgimage.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
tools/renesas_spkgimage.h +1 -5
@@ -11,11 +11,7 @@#ifndef _SPKGIMAGE_H_#define _SPKGIMAGE_H_ -#ifdef __GNUC__ -#define __packed __attribute((packed)) -#else -#define __packed -#endif +#include <linux/compiler_attributes.h> #define SPKG_HEADER_MARKER {'R', 'Z', 'N', '1'}#define SPKG_HEADER_SIZE 24
Signature · 1 line
2.25.1
PATCH v6 3/9 mbedtls: enable support of hkdf
In reply to Philippe ReynesAdds the support of key derivation using
the scheme hkdf.
--- lib/mbedtls/Kconfig | 14 ++++++++++++++ lib/mbedtls/Makefile | 2 ++ lib/mbedtls/mbedtls_def_config.h | 4 ++++ 3 files changed, 20 insertions(+)
lib/mbedtls/Kconfig +14 -0
@@ -297,6 +297,13 @@ config MD5_MBEDTLS This option enables support of hashing using MD5 algorithm with MbedTLS crypto library. +config HKDF_MBEDTLS + bool "Enable HKDF support with MbedTLS crypto library" + depends on MBEDTLS_LIB_CRYPTO + help + This option enables support of key derivation using HKDF algorithm + with MbedTLS crypto library. + if SPL config SPL_SHA1_MBEDTLS
@@ -335,6 +342,13 @@ config SPL_MD5_MBEDTLS This option enables support of hashing using MD5 algorithm with MbedTLS crypto library. +config SPL_HKDF_MBEDTLS + bool "Enable HKDF support in SPL with MbedTLS crypto library" + depends on MBEDTLS_LIB_CRYPTO + help + This option enables support of key derivation using HKDF algorithm + with MbedTLS crypto library. + endif # SPL endif # MBEDTLS_LIB_CRYPTO
lib/mbedtls/Makefile +2 -0
@@ -33,6 +33,8 @@ mbedtls_lib_crypto-$(CONFIG_$(SPL_)SHA256_MBEDTLS) += \ $(MBEDTLS_LIB_DIR)/sha256.o mbedtls_lib_crypto-$(CONFIG_$(SPL_)SHA512_MBEDTLS) += \ $(MBEDTLS_LIB_DIR)/sha512.o +mbedtls_lib_crypto-$(CONFIG_$(SPL_)HKDF_MBEDTLS) += \ + $(MBEDTLS_LIB_DIR)/hkdf.o # MbedTLS X509 library obj-$(CONFIG_MBEDTLS_LIB_X509) += mbedtls_lib_x509.o
lib/mbedtls/mbedtls_def_config.h +4 -0
@@ -56,6 +56,10 @@#endif #endif +#if CONFIG_IS_ENABLED (HKDF_MBEDTLS ) +#define MBEDTLS_HKDF_C +#endif +#if definedCONFIG_MBEDTLS_LIB_X509 #if CONFIG_IS_ENABLED (X509_CERTIFICATE_PARSER )
Signature · 1 line
2.25.1
PATCH v6 4/9 lib: sha256: move common function to sha256_common.c
In reply to Philippe ReynesThe function sha256_csum_wd is defined in lib/sha256.c and in lib/mbedtls/sha256.c. To avoid duplicating this function (and future function), we move this function to the file lib/sha256_common.c
--- lib/Makefile | 1 + lib/mbedtls/sha256.c | 27 ------------------------ lib/sha256.c | 36 ------------------------------- lib/sha256_common.c | 50 ++++++++++++++++++++++++++++++++++++++++++++ tools/Makefile | 1 + 5 files changed, 52 insertions(+), 63 deletions(-) create mode 100644 lib/sha256_common.c
lib/Makefile +1 -0
@@ -77,6 +77,7 @@ obj-$(CONFIG_BLAKE2) += blake2/blake2b.o obj-$(CONFIG_$(XPL_)MD5_LEGACY) += md5.o obj-$(CONFIG_$(XPL_)SHA1_LEGACY) += sha1.o +obj-$(CONFIG_$(XPL_)SHA256) += sha256_common.o obj-$(CONFIG_$(XPL_)SHA256_LEGACY) += sha256.o obj-$(CONFIG_$(XPL_)SHA512_LEGACY) += sha512.o
lib/mbedtls/sha256.c +0 -27
@@ -33,30 +33,3 @@ void sha256_finish(sha256_context *ctx, uint8_t digest[SHA256_SUM_LEN])mbedtls_sha256_finish (ctx, digest);mbedtls_sha256_free (ctx); } - -void sha256_csum_wd (const unsigned char *input,unsigned int ilen, -unsigned char *output,unsigned int chunk_sz) -{ -sha256_context ctx; - -sha256_starts (&ctx); - -if (IS_ENABLED (CONFIG_HW_WATCHDOG ) ||IS_ENABLED (CONFIG_WATCHDOG )) { -const unsigned char *curr = input; -const unsigned char *end = input + ilen; -int chunk; - -while (curr < end) { - chunk = end - curr; -if (chunk > chunk_sz) - chunk = chunk_sz; -sha256_update (&ctx, curr, chunk); - curr += chunk; -schedule (); - } - }else { -sha256_update (&ctx, input, ilen); - } - -sha256_finish (&ctx, output); -}
lib/sha256.c +0 -36
@@ -264,39 +264,3 @@ void sha256_finish(sha256_context * ctx, uint8_t digest[32])PUT_UINT32_BE (ctx->state[6 ], digest,24 );PUT_UINT32_BE (ctx->state[7 ], digest,28 ); } - -/* -* Output = SHA-256( input buffer ). Trigger the watchdog every 'chunk_sz' -* bytes of input processed. -*/ -void sha256_csum_wd (const unsigned char *input,unsigned int ilen, -unsigned char *output,unsigned int chunk_sz) -{ -sha256_context ctx; -#if !defined(USE_HOSTCC ) && \ - (defined(CONFIG_HW_WATCHDOG ) || defined(CONFIG_WATCHDOG )) -const unsigned char *end; -unsigned char *curr; -int chunk; -#endif - -sha256_starts (&ctx); - -#if !defined(USE_HOSTCC ) && \ - (defined(CONFIG_HW_WATCHDOG ) || defined(CONFIG_WATCHDOG )) - curr = (unsigned char *)input; - end = input + ilen; -while (curr < end) { - chunk = end - curr; -if (chunk > chunk_sz) - chunk = chunk_sz; -sha256_update (&ctx, curr, chunk); - curr += chunk; -schedule (); - } -#else -sha256_update (&ctx, input, ilen); -#endif - -sha256_finish (&ctx, output); -}
lib/sha256_common.c new file +50 -0
50 lines not on this page · raw message
tools/Makefile +1 -0
@@ -135,6 +135,7 @@ dumpimage-mkimage-objs := aisimage.o \ generated/lib/hash-checksum.o \ generated/lib/sha1.o \ generated/lib/sha256.o \ + generated/lib/sha256_common.o \ generated/lib/sha512.o \ generated/common/hash.o \ ublimage.o \
Signature · 1 line
2.25.1
PATCH v6 5/9 lib: sha256: add feature sha256_hmac
In reply to Philippe ReynesAdds the support of the hmac based on sha256.
This implementation is based on rfc2104.
--- include/u-boot/sha256.h | 4 ++++ lib/mbedtls/sha256.c | 15 ++++++++++++ lib/sha256.c | 51 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+)
include/u-boot/sha256.h +4 -0
@@ -45,4 +45,8 @@ void sha256_finish(sha256_context * ctx, uint8_t digest[SHA256_SUM_LEN]);void sha256_csum_wd (const unsigned char *input,unsigned int ilen,unsigned char *output,unsigned int chunk_sz); +int sha256_hmac (const unsigned char *key,int keylen, +const unsigned char *input,unsigned int ilen, +unsigned char *output); +#endif /* _SHA256_H */
lib/mbedtls/sha256.c +15 -0
@@ -10,6 +10,8 @@#endif /* USE_HOSTCC */ #include <u-boot/sha256.h> +#include <mbedtls/md.h> +const u8 sha256_der_prefix[SHA256_DER_LEN ] = {0x30 ,0x31 ,0x30 ,0x0d ,0x06 ,0x09 ,0x60 ,0x86 ,0x48 ,0x01 ,0x65 ,0x03 ,0x04 ,0x02 ,0x01 ,0x05 ,
@@ -33,3 +35,16 @@ void sha256_finish(sha256_context *ctx, uint8_t digest[SHA256_SUM_LEN])mbedtls_sha256_finish (ctx, digest);mbedtls_sha256_free (ctx); } + +int sha256_hmac (const unsigned char *key,int keylen, +const unsigned char *input,unsigned int ilen, +unsigned char *output) +{ +const mbedtls_md_info_t *md; + + md =mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 ); +if (!md) +return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ; + +return mbedtls_md_hmac (md, key, keylen, input, ilen, output); +}
lib/sha256.c +51 -0
54 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v6 6/9 test: lib: add test for sha256_hmac
In reply to Philippe ReynesAdds a test for the function sha256_hmac
--- test/lib/Makefile | 1 + test/lib/test_sha256_hmac.c | 294 ++++++++++++++++++++++++++++++++++++ 2 files changed, 295 insertions(+) create mode 100644 test/lib/test_sha256_hmac.c
test/lib/Makefile +1 -0
@@ -24,6 +24,7 @@ obj-$(CONFIG_ERRNO_STR) += test_errno_str.o obj-$(CONFIG_UT_LIB_ASN1) += asn1.o obj-$(CONFIG_UT_LIB_RSA) += rsa.o obj-$(CONFIG_AES) += test_aes.o +obj-$(CONFIG_SHA256) += test_sha256_hmac.o obj-$(CONFIG_GETOPT) += getopt.o obj-$(CONFIG_CRC8) += test_crc8.o obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.o
test/lib/test_sha256_hmac.c new file +294 -0
294 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v6 7/9 lib: mbedtls: sha256: add support of key derivation
In reply to Philippe ReynesAdds the support of key derivation using the scheme hkdf. This scheme is defined in rfc5869.
--- include/u-boot/sha256.h | 20 ++++++++++++++++++++ lib/mbedtls/sha256.c | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+)
include/u-boot/sha256.h +20 -0
@@ -1,6 +1,8 @@#ifndef _SHA256_H#define _SHA256_H +#include <linux/compiler_attributes.h> +#include <linux/errno.h> #include <linux/kconfig.h> #include <linux/types.h>
@@ -49,4 +51,22 @@ int sha256_hmac(const unsigned char *key, int keylen, const unsigned char *input, unsigned int ilen, unsigned char *output); +#if CONFIG_IS_ENABLED (HKDF_MBEDTLS ) +int sha256_hkdf (const unsigned char *salt,int saltlen, +const unsigned char *ikm,int ikmlen, +const unsigned char *info,int infolen, +unsigned char *output,int outputlen); +#else +static inline int sha256_hkdf (const unsigned char __always_unused *salt, +int __always_unused saltlen, +const unsigned char __always_unused *ikm, +int __always_unused ikmlen, +const unsigned char __always_unused *info, +int __always_unused infolen, +unsigned char __always_unused *output, +int __always_unused outputlen) { +return -EOPNOTSUPP ; +} +#endif +#endif /* _SHA256_H */
lib/mbedtls/sha256.c +23 -0
@@ -12,6 +12,10 @@#include <mbedtls/md.h> +#if CONFIG_IS_ENABLED (HKDF_MBEDTLS ) +#include <mbedtls/hkdf.h> +#endif +const u8 sha256_der_prefix[SHA256_DER_LEN ] = {0x30 ,0x31 ,0x30 ,0x0d ,0x06 ,0x09 ,0x60 ,0x86 ,0x48 ,0x01 ,0x65 ,0x03 ,0x04 ,0x02 ,0x01 ,0x05 ,
@@ -48,3 +52,22 @@ int sha256_hmac(const unsigned char *key, int keylen,return mbedtls_md_hmac (md, key, keylen, input, ilen, output); } + +#if CONFIG_IS_ENABLED (HKDF_MBEDTLS ) +int sha256_hkdf (const unsigned char *salt,int saltlen, +const unsigned char *ikm,int ikmlen, +const unsigned char *info,int infolen, +unsigned char *output,int outputlen) +{ +const mbedtls_md_info_t *md; + + md =mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 ); +if (!md) +return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ; + +return mbedtls_hkdf (md, salt, saltlen, + ikm, ikmlen, + info, infolen, + output, outputlen); +} +#endif
Signature · 1 line
2.25.1
PATCH v6 8/9 test: lib: add test for key derivation
In reply to Philippe ReynesAdds a test for the function sha256_hkdf.
--- test/lib/Makefile | 1 + test/lib/test_sha256_hkdf.c | 198 ++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 test/lib/test_sha256_hkdf.c
test/lib/Makefile +1 -0
@@ -25,6 +25,7 @@ obj-$(CONFIG_UT_LIB_ASN1) += asn1.o obj-$(CONFIG_UT_LIB_RSA) += rsa.o obj-$(CONFIG_AES) += test_aes.o obj-$(CONFIG_SHA256) += test_sha256_hmac.o +obj-$(CONFIG_HKDF_MBEDTLS) += test_sha256_hkdf.o obj-$(CONFIG_GETOPT) += getopt.o obj-$(CONFIG_CRC8) += test_crc8.o obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.o
test/lib/test_sha256_hkdf.c new file +198 -0
198 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v6 9/9 configs: sandbox: enable mbedtls
In reply to Philippe ReynesEnable mbedtls to have at least one config with
mbedtls enabled.
--- configs/sandbox_defconfig | 2 ++ 1 file changed, 2 insertions(+)
configs/sandbox_defconfig +2 -0
@@ -352,6 +352,8 @@ CONFIG_FS_CRAMFS=y CONFIG_ADDR_MAP=y CONFIG_CMD_DHRYSTONE=y CONFIG_MBEDTLS_LIB=y +CONFIG_MBEDTLS_LIB_CRYPTO=y +CONFIG_HKDF_MBEDTLS=y CONFIG_ECDSA=y CONFIG_ECDSA_VERIFY=y CONFIG_TPM=y
Signature · 1 line
2.25.1
Hi Philippe,
Quoted 23 lines · Philippe Reynes
This serie adds the support of
sha256_hmacandsha256_hkdf. A first version was sent several months ago just before the integration of mbedtls. This new version is based on mbedtls.The first patch of this serie add the support of hkdf using mbedtls.
v6:
- use const for test
- set buffer to 0 before using to to compute hmacv5:
- use mbedtls to compute hmac
- add tests for hkdf
- enabled mbedtls insandbox_defconfig
- some cleanupv4:
- fix computation of the hmac
- add more test for hmac
- move sha256 common functions tosha256_common.c
- some cleanup
Quoted 5 lines
v3:
- also definesha256_hmacfor legacy sha256
- add some #if to define function only when needed
Can you add my review tag to the commit messages for those ones I stated if there are no new changes?
So that I can skip them in the next round.
Quoted 31 lines
Philippe Reynes (9):
tools:kwbimage.h: uselinux/compiler_attributes.h
tools:renesas_spkgimage.h: uselinux/compiler_attributes.h
mbedtls: enable support of hkdf
lib: sha256: move common function tosha256_common.c
lib: sha256: add featuresha256_hmac
test: lib: add test forsha256_hmac
lib: mbedtls: sha256: add support of key derivation
test: lib: add test for key derivation
configs: sandbox: enable mbedtlsconfigs/sandbox_defconfig | 2 + include/u-boot/sha256.h | 24 +++ lib/Makefile | 1 + lib/mbedtls/Kconfig | 14 ++ lib/mbedtls/Makefile | 2 + lib/mbedtls/mbedtls_def_config.h | 4 + lib/mbedtls/sha256.c | 59 ++++--- lib/sha256.c | 67 ++++--- lib/sha256_common.c | 50 ++++++ test/lib/Makefile | 2 + test/lib/test_sha256_hkdf.c | 198 +++++++++++++++++++++ test/lib/test_sha256_hmac.c | 294 +++++++++++++++++++++++++++++++ tools/Makefile | 1 + tools/kwbimage.h | 6 +- tools/renesas_spkgimage.h | 6 +- 15 files changed, 670 insertions(+), 60 deletions(-) create mode 100644 lib/sha256_common.c create mode 100644 test/lib/test_sha256_hkdf.c create mode 100644 test/lib/test_sha256_hmac.c
Quoted 4 lines
--
2.25.1
Thanks and regards,
Raymond
Hi Raymond,
Le 18/12/2024 à 16:23, Raymond Mao a écrit :
Quoted 30 lines
*This Mail comes from Outside of SoftAtHome: *Do not answer, click links or open attachments unless you recognize the sender and know the content is safe.**
Hi Philippe,
On Tue, 17 Dec 2024 at 16:36, Philippe Reynes <[email protected]> wrote:
This serie adds the support of
sha256_hmacandsha256_hkdf. A first version was sent several months ago just before the integration of mbedtls. This new version is based on mbedtls.The first patch of this serie add the support of hkdf using mbedtls.
v6:
- use const for test
- set buffer to 0 before using to to compute hmacv5:
- use mbedtls to compute hmac
- add tests for hkdf
- enabled mbedtls insandbox_defconfig
- some cleanupv4:
- fix computation of the hmac
- add more test for hmac
- move sha256 common functions tosha256_common.c
- some cleanup
Quoted 7 lines
v3:
- also definesha256_hmacfor legacy sha256
- add some #if to define function only when neededCan you add my review tag to the commit messages for those ones I stated if there are no new changes? So that I can skip them in the next round.
Oh sorry, I missed to add this tag on my patches.
I add them and I send a v7.
Quoted 32 lines
Philippe Reynes (9):
tools:kwbimage.h: uselinux/compiler_attributes.h
tools:renesas_spkgimage.h: uselinux/compiler_attributes.h
mbedtls: enable support of hkdf
lib: sha256: move common function tosha256_common.c
lib: sha256: add featuresha256_hmac
test: lib: add test forsha256_hmac
lib: mbedtls: sha256: add support of key derivation
test: lib: add test for key derivation
configs: sandbox: enable mbedtlsconfigs/sandbox_defconfig | 2 + include/u-boot/sha256.h | 24 +++ lib/Makefile | 1 + lib/mbedtls/Kconfig | 14 ++ lib/mbedtls/Makefile | 2 +
lib/mbedtls/mbedtls_def_config.h| 4 + lib/mbedtls/sha256.c | 59 ++++--- lib/sha256.c | 67 ++++--- lib/sha256_common.c | 50 ++++++ test/lib/Makefile | 2 + test/lib/test_sha256_hkdf.c | 198 +++++++++++++++++++++ test/lib/test_sha256_hmac.c | 294
+++++++++++++++++++++++++++++++
tools/Makefile | 1 + tools/kwbimage.h | 6 +- tools/renesas_spkgimage.h | 6 +-
15 files changed, 670 insertions(+), 60 deletions(-) create mode 100644lib/sha256_common.c
create mode 100644test/lib/test_sha256_hkdf.ccreate mode 100644test/lib/test_sha256_hmac.c
Quoted 6 lines
--
2.25.1Thanks and regards,
Raymond
Regards,
Philippe