add the support of sha256_hmac and sha256_hkdf
v2 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.
Philippe Reynes (5):
mbedtls: enable support of hkdf
lib: sha256: add feature sha256_hmac
test: lib: add test for sha256_hamc
lib: mbedtls: sha256: add support of key derivation
test: lib: add test for key derivation
include/u-boot/sha256.h | 9 +++ lib/mbedtls/Kconfig | 14 ++++ lib/mbedtls/Makefile | 2 + lib/mbedtls/mbedtls_def_config.h | 4 ++ lib/mbedtls/sha256.c | 56 ++++++++++++++++ test/lib/Makefile | 2 + test/lib/test_sha256_hkdf.c | 104 +++++++++++++++++++++++++++++ test/lib/test_sha256_hmac.c | 108 +++++++++++++++++++++++++++++++ 8 files changed, 299 insertions(+) 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 v2 1/5 mbedtls: enable support of hkdf
Adds 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
Re: [PATCH v2 1/5] mbedtls: enable support of hkdf
Hi Philippe,
Quoted 77 lines · Philippe Reynes
Adds the support of key derivation using
the scheme hkdf.Signed-off-by Philippe Reynes <[email protected]>--- lib/mbedtls/Kconfig | 14 ++++++++++++++ lib/mbedtls/Makefile | 2 ++ lib/mbedtls/mbedtls_def_config.h | 4 ++++ 3 files changed, 20 insertions(+)lib/mbedtls/Kconfig +5
-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 HKDFalgorithm
lib/mbedtls/Kconfig +7
-0+ 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 HKDFalgorithm
lib/mbedtls/Kconfig +2
-0+ with MbedTLS crypto library. + endif # SPL endif # MBEDTLS_LIB_CRYPTOlib/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
lib/mbedtls/hkdf.cdoes not exist in your patch series.# MbedTLS X509 library obj-$(CONFIG_MBEDTLS_LIB_X509) += mbedtls_lib_x509.o
lib/mbedtls/mbedtls_def_config.h +4
-1@@ -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 ) --
Quoted 3 lines
2.25.1
Regards,
Raymond
Re: [PATCH v2 1/5] mbedtls: enable support of hkdf
Hi Raymond,
Le 05/12/2024 à 18:08, Raymond Mao a écrit :
Quoted 63 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 Wed, 4 Dec 2024 at 12:54, Philippe Reynes <[email protected]> wrote:
Adds the support of key derivation using
the scheme hkdf.Signed-off-by Philippe Reynes <[email protected]>---
lib/mbedtls/Kconfig | 14 ++++++++++++++ lib/mbedtls/Makefile | 2 ++lib/mbedtls/mbedtls_def_config.h| 4 ++++ 3 files changed, 20 insertions(+)diff --git a/lib/mbedtls/Kconfig b/lib/mbedtls/Kconfig index
78167ffa252..aa82336ef14100644
--- a/lib/mbedtls/Kconfig
+++ b/lib/mbedtls/Kconfig
@@ -297,6 +297,13 @@ configMD5_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 onMBEDTLS_LIB_CRYPTO
+ help
+ This option enables support of key derivation using HKDF algorithm
+ with MbedTLS crypto library.
+
if SPLconfig
SPL_SHA1_MBEDTLS
@@ -335,6 +342,13 @@ configSPL_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 onMBEDTLS_LIB_CRYPTO
+ help
+ This option enables support of key derivation using HKDF algorithm
+ with MbedTLS crypto library.
+
endif # SPLendif #
MBEDTLS_LIB_CRYPTO
diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile indexce0a61e4054..e66c2018d97100644
--- a/lib/mbedtls/Makefile
+++ b/lib/mbedtls/Makefile
@@ -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
Quoted 1 line
lib/mbedtls/hkdf.cdoes not exist in your patch series.
I think I have to keep to line because
The definition of MBEDTLS_LIB_DIR is:
MBEDTLS_LIB_DIR = external/mbedtls/library
So $(MBEDTLS_LIB_DIR)/hkdf.o reference the file:
lib/mbedtls/external/mbedtls/library/hkdf.c
and not the file lib/mbedtls//hkdf.c (that don't exist).
Quoted 22 lines
# MbedTLS X509 library
obj-$(CONFIG_MBEDTLS_LIB_X509) += mbedtls_lib_x509.o
diff --git a/lib/mbedtls/mbedtls_def_config.h
b/lib/mbedtls/mbedtls_def_config.h
index d27f017d084..cce0134d527 100644
--- a/lib/mbedtls/mbedtls_def_config.h
+++ b/lib/mbedtls/mbedtls_def_config.h
@@ -56,6 +56,10 @@
#endif
#endif
+#if CONFIG_IS_ENABLED(HKDF_MBEDTLS)
+#define MBEDTLS_HKDF_C
+#endif
+
#if defined CONFIG_MBEDTLS_LIB_X509
#if CONFIG_IS_ENABLED(X509_CERTIFICATE_PARSER)
--
2.25.1Quoted 2 lines
Regards,
Raymond
Regards,
Philippe
PATCH v2 2/5 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 | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)
include/u-boot/sha256.h +4 -0
@@ -44,4 +44,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); +void 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 +38 -0
@@ -8,6 +8,7 @@#ifndef USE_HOSTCC #include <cyclic.h> #endif /* USE_HOSTCC */ +#include <string.h> #include <u-boot/sha256.h> const u8 sha256_der_prefix[SHA256_DER_LEN ] = {
@@ -60,3 +61,40 @@ void sha256_csum_wd(const unsigned char *input, unsigned int ilen,sha256_finish (&ctx, output); } + +void sha256_hmac (const unsigned char *key,int keylen, +const unsigned char *input,unsigned int ilen, +unsigned char *output) +{ +int i; +sha256_context ctx; +unsigned char k_ipad[64 ]; +unsigned char k_opad[64 ]; +unsigned char tmpbuf[32 ]; + +memset (k_ipad,0x36 ,64 ); +memset (k_opad,0x5C ,64 ); + +for (i =0 ; i < keylen; i++) { +if (i >=64 ) +break ; + + k_ipad[i] ^= key[i]; + k_opad[i] ^= key[i]; + } + +sha256_starts (&ctx); +sha256_update (&ctx, k_ipad,sizeof (k_ipad)); +sha256_update (&ctx, input, ilen); +sha256_finish (&ctx, tmpbuf); + +sha256_starts (&ctx); +sha256_update (&ctx, k_opad,sizeof (k_opad)); +sha256_update (&ctx, tmpbuf,sizeof (tmpbuf)); +sha256_finish (&ctx, output); + +memset (k_ipad,0 ,sizeof (k_ipad)); +memset (k_opad,0 ,sizeof (k_opad)); +memset (tmpbuf,0 ,sizeof (tmpbuf)); +memset (&ctx,0 ,sizeof (sha256_context)); +}
Signature · 1 line
2.25.1
Re: [PATCH v2 2/5] lib: sha256: add feature sha256_hmac
Hi Philippe,
Quoted 15 lines · Philippe Reynes
Adds the support of the hmac based on sha256.
This implementation is based on rfc2104.Signed-off-by Philippe Reynes <[email protected]>--- include/u-boot/sha256.h | 4 ++++ lib/mbedtls/sha256.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)include/u-boot/sha256.h +0
-0
include/u-boot/sha256.h void sha256_finish(sha256_context * ctx, uint8_t · line 51
Quoted 8 lines
include/u-boot/sha256.h +4
-0@@ -44,4 +44,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); +void sha256_hmac (const unsigned char *key,int keylen, +const unsigned char *input,unsigned int ilen, +unsigned char *output); +
Shall we have an inline function for the case if mbedtls is not enabled?
Quoted 46 lines
#endif /*
_SHA256_H*/lib/mbedtls/sha256.c +28
-0@@ -8,6 +8,7 @@#ifndef USE_HOSTCC #include <cyclic.h> #endif /* USE_HOSTCC */ +#include <string.h> #include <u-boot/sha256.h> const u8 sha256_der_prefix[SHA256_DER_LEN ] = {@@ -60,3 +61,40 @@ void sha256_csum_wd(const unsigned char *input,unsigned int ilen, sha256_finish(&ctx, output); } + +void sha256_hmac (const unsigned char *key,int keylen, +const unsigned char *input,unsigned int ilen, +unsigned char *output) +{ +int i; +sha256_context ctx; +unsigned char k_ipad[64 ]; +unsigned char k_opad[64 ]; +unsigned char tmpbuf[32 ]; + +memset (k_ipad,0x36 ,64 ); +memset (k_opad,0x5C ,64 ); + +for (i =0 ; i < keylen; i++) { +if (i >=64 ) +break ; + + k_ipad[i] ^= key[i]; + k_opad[i] ^= key[i]; + } + +sha256_starts (&ctx); +sha256_update (&ctx, k_ipad,sizeof (k_ipad)); +sha256_update (&ctx, input, ilen); +sha256_finish (&ctx, tmpbuf); +
lib/mbedtls/sha256.c void sha256_csum_wd(const unsigned char *input, · line 100
Quoted 10 lines
lib/mbedtls/sha256.c +10
-0+sha256_starts (&ctx); +sha256_update (&ctx, k_opad,sizeof (k_opad)); +sha256_update (&ctx, tmpbuf,sizeof (tmpbuf)); +sha256_finish (&ctx, output); + +memset (k_ipad,0 ,sizeof (k_ipad)); +memset (k_opad,0 ,sizeof (k_opad)); +memset (tmpbuf,0 ,sizeof (tmpbuf)); +memset (&ctx,0 ,sizeof (sha256_context)); +}
This function seems to be generic and should be located in lib instead of lib/mbedtls.
Regards,
Raymond
Re: [PATCH v2 2/5] lib: sha256: add feature sha256_hmac
Hi Raymond,
Le 05/12/2024 à 18:11, 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 Wed, 4 Dec 2024 at 12:54, Philippe Reynes <[email protected]> wrote:
Adds the support of the hmac based on sha256. This implementation is based on rfc2104. Signed-off-by: Philippe Reynes <[email protected]> --- include/u-boot/sha256.h | 4 ++++ lib/mbedtls/sha256.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/u-boot/sha256.h b/include/u-boot/sha256.h index b58d5b58d39..77dbcd4553a 100644 --- a/include/u-boot/sha256.h +++ b/include/u-boot/sha256.h @@ -44,4 +44,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); +void sha256_hmac(const unsigned char *key, int keylen, + const unsigned char *input, unsigned int ilen, + unsigned char *output); +
Quoted 1 line
Shall we have an inline function for the case if mbedtls is not enabled?
you're right, I should manage the case where mbedtls is not enabled. But with you're feedback below, the function hmac is always defined (with or without mbedtls).
Quoted 57 lines
#endif /* _SHA256_H */
diff --git a/lib/mbedtls/sha256.c b/lib/mbedtls/sha256.c
index 24aa58fa674..1b9fc1a8503 100644
--- a/lib/mbedtls/sha256.c
+++ b/lib/mbedtls/sha256.c
@@ -8,6 +8,7 @@
#ifndef USE_HOSTCC
#include <cyclic.h>
#endif /* USE_HOSTCC */
+#include <string.h>
#include <u-boot/sha256.h>
const u8 sha256_der_prefix[SHA256_DER_LEN] = {
@@ -60,3 +61,40 @@ void sha256_csum_wd(const unsigned char *input,
unsigned int ilen,
sha256_finish(&ctx, output);
}
+
+void sha256_hmac(const unsigned char *key, int keylen,
+ const unsigned char *input, unsigned int ilen,
+ unsigned char *output)
+{
+ int i;
+ sha256_context ctx;
+ unsigned char k_ipad[64];
+ unsigned char k_opad[64];
+ unsigned char tmpbuf[32];
+
+ memset(k_ipad, 0x36, 64);
+ memset(k_opad, 0x5C, 64);
+
+ for (i = 0; i < keylen; i++) {
+ if (i >= 64)
+ break;
+
+ k_ipad[i] ^= key[i];
+ k_opad[i] ^= key[i];
+ }
+
+ sha256_starts(&ctx);
+ sha256_update(&ctx, k_ipad, sizeof(k_ipad));
+ sha256_update(&ctx, input, ilen);
+ sha256_finish(&ctx, tmpbuf);
+
+ sha256_starts(&ctx);
+ sha256_update(&ctx, k_opad, sizeof(k_opad));
+ sha256_update(&ctx, tmpbuf, sizeof(tmpbuf));
+ sha256_finish(&ctx, output);
+
+ memset(k_ipad, 0, sizeof(k_ipad));
+ memset(k_opad, 0, sizeof(k_opad));
+ memset(tmpbuf, 0, sizeof(tmpbuf));
+ memset(&ctx, 0, sizeof(sha256_context));
+}Quoted 1 line
This function seems to be generic and should be located in lib instead of lib/mbedtls.
I have looked the function sha256_csum_wd and I have seen that this function is defined twice, in the file lib/sha256.c and in lib/mbedtls/sha256.c. I have done the same to have the support of sha256_hmac with or without mbedtls. But I think that we should avoid to duplicate code, and we should clean it.
Quoted 3 lines
Regards,
Raymond
Regards,
Philippe
Re: [PATCH v2 2/5] lib: sha256: add feature sha256_hmac
Hi Philippe,
Quoted 107 lines · Philippe REYNES
Hi Raymond,
Le 05/12/2024 à 18:11, Raymond Mao a écrit :*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,Quoted 15 lines · Philippe Reynes
Adds the support of the hmac based on sha256.
This implementation is based on rfc2104.Signed-off-by Philippe Reynes <[email protected]>--- include/u-boot/sha256.h | 4 ++++ lib/mbedtls/sha256.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)include/u-boot/sha256.h +0
-0include/u-boot/sha256.h void sha256_finish(sha256_context * ctx, uint8_t · line 51
Quoted 8 lines
include/u-boot/sha256.h +4
-0@@ -44,4 +44,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); +void sha256_hmac (const unsigned char *key,int keylen, +const unsigned char *input,unsigned int ilen, +unsigned char *output); +Shall we have an inline function for the case if mbedtls is not enabled?
you're right, I should manage the case where mbedtls is not enabled. But with you're feedback below, the function hmac is always defined (with or without mbedtls).
Quoted 46 lines
#endif /*
_SHA256_H*/lib/mbedtls/sha256.c +28
-0@@ -8,6 +8,7 @@#ifndef USE_HOSTCC #include <cyclic.h> #endif /* USE_HOSTCC */ +#include <string.h> #include <u-boot/sha256.h> const u8 sha256_der_prefix[SHA256_DER_LEN ] = {@@ -60,3 +61,40 @@ void sha256_csum_wd(const unsigned char *input,unsigned int ilen, sha256_finish(&ctx, output); } + +void sha256_hmac (const unsigned char *key,int keylen, +const unsigned char *input,unsigned int ilen, +unsigned char *output) +{ +int i; +sha256_context ctx; +unsigned char k_ipad[64 ]; +unsigned char k_opad[64 ]; +unsigned char tmpbuf[32 ]; + +memset (k_ipad,0x36 ,64 ); +memset (k_opad,0x5C ,64 ); + +for (i =0 ; i < keylen; i++) { +if (i >=64 ) +break ; + + k_ipad[i] ^= key[i]; + k_opad[i] ^= key[i]; + } + +sha256_starts (&ctx); +sha256_update (&ctx, k_ipad,sizeof (k_ipad)); +sha256_update (&ctx, input, ilen); +sha256_finish (&ctx, tmpbuf); +lib/mbedtls/sha256.c void sha256_csum_wd(const unsigned char *input, · line 100
Quoted 10 lines
lib/mbedtls/sha256.c +10
-0+sha256_starts (&ctx); +sha256_update (&ctx, k_opad,sizeof (k_opad)); +sha256_update (&ctx, tmpbuf,sizeof (tmpbuf)); +sha256_finish (&ctx, output); + +memset (k_ipad,0 ,sizeof (k_ipad)); +memset (k_opad,0 ,sizeof (k_opad)); +memset (tmpbuf,0 ,sizeof (tmpbuf)); +memset (&ctx,0 ,sizeof (sha256_context)); +}This function seems to be generic and should be located in lib instead of lib/mbedtls.
Quoted 7 lines
I have looked the function
sha256_csum_wdand I have seen that this function
is defined twice, in the filelib/sha256.cand inlib/mbedtls/sha256.c. I have done
the same to have the support ofsha256_hmacwith or without mbedtls. But I think that we should avoid to duplicate code, and we should clean it.
Yes, common functions like sha256_csum_wd and sha256_hmac should be moved to a new file (e.g. sha256_common.c?) that can be used both enabling/ disabling mbedtls.
Regards,
Raymond
PATCH v2 3/5 test: lib: add test for sha256_hamc
In reply to Philippe ReynesAdds a test for the function sha256_hmac
--- test/lib/Makefile | 1 + test/lib/test_sha256_hmac.c | 108 ++++++++++++++++++++++++++++++++++++ 2 files changed, 109 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_MBEDTLS) += 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 +108 -0
108 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v2 3/5] test: lib: add test for sha256_hamc
Hi Philippe,
Typo "sha256_hamc" in the subject line.
Quoted 137 lines · Philippe Reynes
Adds a test for the function
sha256_hmacSigned-off-by Philippe Reynes <[email protected]>--- test/lib/Makefile | 1 + test/lib/test_sha256_hmac.c | 108 ++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 test/lib/test_sha256_hmac.ctest/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_MBEDTLS) += test_sha256_hmac.o obj-$(CONFIG_GETOPT) += getopt.o obj-$(CONFIG_CRC8) += test_crc8.o obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.otest/lib/test_sha256_hmac.c new file +92
-0@@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2024 Philippe Reynes <[email protected]> +* +* Unit tests for sha256_hmac functions +*/ + +#include <command.h> +#include <test/lib.h> +#include <test/test.h> +#include <test/ut.h> +#include <u-boot/sha256.h> + +struct test_sha256_hmac_s { +unsigned char *key; +int keylen; +unsigned char *input; +int ilen; +unsigned char *expected; +}; + +/* +* data comes from: +* https://datatracker.ietf.org/doc/html/rfc4231 +*/ +static unsigned char key_test1[] = { +0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b , +0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b }; + +static unsigned char input_test1[] = { +0x48 ,0x69 ,0x20 ,0x54 ,0x68 ,0x65 ,0x72 ,0x65 }; + +static unsigned char expected_test1[] = { +0xb0 ,0x34 ,0x4c ,0x61 ,0xd8 ,0xdb ,0x38 ,0x53 , +0x5c ,0xa8 ,0xaf ,0xce ,0xaf ,0x0b ,0xf1 ,0x2b , +0x88 ,0x1d ,0xc2 ,0x00 ,0xc9 ,0x83 ,0x3d ,0xa7 , +0x26 ,0xe9 ,0x37 ,0x6c ,0x2e ,0x32 ,0xcf ,0xf7 }; + +static unsigned char key_test2[] = {0x4a ,0x65 ,0x66 ,0x65 }; + +static unsigned char input_test2[] = { +0x77 ,0x68 ,0x61 ,0x74 ,0x20 ,0x64 ,0x6f ,0x20 , +0x79 ,0x61 ,0x20 ,0x77 ,0x61 ,0x6e ,0x74 ,0x20 , +0x66 ,0x6f ,0x72 ,0x20 ,0x6e ,0x6f ,0x74 ,0x68 , +0x69 ,0x6e ,0x67 ,0x3f }; + +static unsigned char expected_test2[] = { +0x5b ,0xdc ,0xc1 ,0x46 ,0xbf ,0x60 ,0x75 ,0x4e , +0x6a ,0x04 ,0x24 ,0x26 ,0x08 ,0x95 ,0x75 ,0xc7 , +0x5a ,0x00 ,0x3f ,0x08 ,0x9d ,0x27 ,0x39 ,0x83 , +0x9d ,0xec ,0x58 ,0xb9 ,0x64 ,0xec ,0x38 ,0x43 }; + +static struct test_sha256_hmac_s test_sha256_hmac[] = { + { + .key = key_test1, + .keylen =sizeof (key_test1), + .input = input_test1, + .ilen =sizeof (input_test1), + .expected = expected_test1, + }, + { + .key = key_test2, + .keylen =sizeof (key_test2), + .input = input_test2, + .ilen =sizeof (input_test2), + .expected = expected_test2, + }, +}; + +static int _lib_test_sha256_hmac_run (struct unit_test_state *uts, +unsigned char *key,int keylen, +unsigned char *input,int ilen, +unsigned char *expected) +{ +unsigned char output[32 ]; + +sha256_hmac (key, keylen, input, ilen, output); +ut_asserteq_mem (expected, output,32 ); + +return 0 ; +} + +static int lib_test_sha256_hmac_run (struct unit_test_state *uts, +struct test_sha256_hmac_s *test) +{ +unsigned char *key = test->key; +int keylen = test->keylen; +unsigned char *input = test->input; +int ilen = test->ilen; +unsigned char *expected = test->expected; + + return _lib_test_sha256_hmac_run(uts, key, keylen, input, ilen,expected);
test/lib/test_sha256_hmac.c new file +16
-1+} + +static int lib_test_sha256_hmac (struct unit_test_state *uts) +{ +int i, ret =0 ; + +for (i =0 ; i <ARRAY_SIZE (test_sha256_hmac); i++) { + ret =lib_test_sha256_hmac_run (uts, &test_sha256_hmac[i]); +if (ret) +break ; + } + +return ret; +} + +LIB_TEST (lib_test_sha256_hmac,0 ); --
Quoted 2 lines
2.25.1
Regards,
Raymond
Re: [PATCH v2 3/5] test: lib: add test for sha256_hamc
Hi Raymond,
Le 05/12/2024 à 18:14, Raymond Mao a écrit :
Quoted 7 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,
Typo "sha256_hamc" in the subject line.
thanks a lot, I will fix it in v3
Quoted 146 lines · Philippe Reynes
Adds a test for the function sha256_hmac
Signed-off-by: Philippe Reynes <[email protected]>
---
test/lib/Makefile | 1 +
test/lib/test_sha256_hmac.c | 108
++++++++++++++++++++++++++++++++++++
2 files changed, 109 insertions(+)
create mode 100644 test/lib/test_sha256_hmac.c
diff --git a/test/lib/Makefile b/test/lib/Makefile
index f516d001747..5aad81d5a8a 100644
--- a/test/lib/Makefile
+++ b/test/lib/Makefile
@@ -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_MBEDTLS) += test_sha256_hmac.o
obj-$(CONFIG_GETOPT) += getopt.o
obj-$(CONFIG_CRC8) += test_crc8.o
obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.o
diff --git a/test/lib/test_sha256_hmac.c b/test/lib/test_sha256_hmac.c
new file mode 100644
index 00000000000..473922bd9b0
--- /dev/null
+++ b/test/lib/test_sha256_hmac.c
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2024 Philippe Reynes
<[email protected]>
+ *
+ * Unit tests for sha256_hmac functions
+ */
+
+#include <command.h>
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+#include <u-boot/sha256.h>
+
+struct test_sha256_hmac_s {
+ unsigned char *key;
+ int keylen;
+ unsigned char *input;
+ int ilen;
+ unsigned char *expected;
+};
+
+/*
+ * data comes from:
+ * https://datatracker.ietf.org/doc/html/rfc4231
<https://datatracker.ietf.org/doc/html/rfc4231>
+ */
+static unsigned char key_test1[] = {
+ 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
+ 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
+
+static unsigned char input_test1[] = {
+ 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 };
+
+static unsigned char expected_test1[] = {
+ 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
+ 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
+ 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
+ 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7 };
+
+static unsigned char key_test2[] = { 0x4a, 0x65, 0x66, 0x65 };
+
+static unsigned char input_test2[] = {
+ 0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20,
+ 0x79, 0x61, 0x20, 0x77, 0x61, 0x6e, 0x74, 0x20,
+ 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68,
+ 0x69, 0x6e, 0x67, 0x3f };
+
+static unsigned char expected_test2[] = {
+ 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e,
+ 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7,
+ 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83,
+ 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 };
+
+static struct test_sha256_hmac_s test_sha256_hmac[] = {
+ {
+ .key = key_test1,
+ .keylen = sizeof(key_test1),
+ .input = input_test1,
+ .ilen = sizeof(input_test1),
+ .expected = expected_test1,
+ },
+ {
+ .key = key_test2,
+ .keylen = sizeof(key_test2),
+ .input = input_test2,
+ .ilen = sizeof(input_test2),
+ .expected = expected_test2,
+ },
+};
+
+static int _lib_test_sha256_hmac_run(struct unit_test_state *uts,
+ unsigned char *key, int keylen,
+ unsigned char *input, int ilen,
+ unsigned char *expected)
+{
+ unsigned char output[32];
+
+ sha256_hmac(key, keylen, input, ilen, output);
+ ut_asserteq_mem(expected, output, 32);
+
+ return 0;
+}
+
+static int lib_test_sha256_hmac_run(struct unit_test_state *uts,
+ struct test_sha256_hmac_s *test)
+{
+ unsigned char *key = test->key;
+ int keylen = test->keylen;
+ unsigned char *input = test->input;
+ int ilen = test->ilen;
+ unsigned char *expected = test->expected;
+
+ return _lib_test_sha256_hmac_run(uts, key, keylen, input,
ilen, expected);
+}
+
+static int lib_test_sha256_hmac(struct unit_test_state *uts)
+{
+ int i, ret = 0;
+
+ for (i = 0; i < ARRAY_SIZE(test_sha256_hmac); i++) {
+ ret = lib_test_sha256_hmac_run(uts,
&test_sha256_hmac[i]);
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+LIB_TEST(lib_test_sha256_hmac, 0);
--
2.25.1Quoted 2 lines
Regards,
Raymond
Regards,
Philippe
PATCH v2 4/5 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 | 5 +++++ lib/mbedtls/sha256.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+)
include/u-boot/sha256.h +5 -0
@@ -48,4 +48,9 @@ void sha256_hmac(const unsigned char *key, int keylen, const unsigned char *input, unsigned int ilen, unsigned char *output); +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); +#endif /* _SHA256_H */
lib/mbedtls/sha256.c +18 -0
@@ -11,6 +11,9 @@#include <string.h> #include <u-boot/sha256.h> +#include <mbedtls/md.h> +#include <mbedtls/hkdf.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 ,
@@ -98,3 +101,18 @@ void sha256_hmac(const unsigned char *key, int keylen,memset (tmpbuf,0 ,sizeof (tmpbuf));memset (&ctx,0 ,sizeof (sha256_context)); } + +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 ); + +return mbedtls_hkdf (md, salt, saltlen, + ikm, ikmlen, + info, infolen, + output, outputlen); +}
Signature · 1 line
2.25.1
Re: [PATCH v2 4/5] lib: mbedtls: sha256: add support of key derivation
Hi Philippe,
Quoted 14 lines · Philippe Reynes
Adds the support of key derivation using the scheme hkdf. This scheme is defined in rfc5869.
Signed-off-by Philippe Reynes <[email protected]>--- include/u-boot/sha256.h | 5 +++++ lib/mbedtls/sha256.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+)include/u-boot/sha256.h +0
-0
include/u-boot/sha256.h void sha256_hmac(const unsigned char *key, int keylen, · line 56
Quoted 9 lines
include/u-boot/sha256.h +5
-0@@ -48,4 +48,9 @@ void sha256_hmac(const unsigned char *key, int keylen, const unsigned char *input, unsigned int ilen, unsigned char *output); +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); +
An inline function is needed when the build options are not fulfilled.
Quoted 25 lines
#endif /*
_SHA256_H*/lib/mbedtls/sha256.c +8
-0@@ -11,6 +11,9 @@#include <string.h> #include <u-boot/sha256.h> +#include <mbedtls/md.h> +#include <mbedtls/hkdf.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 ,@@ -98,3 +101,18 @@ void sha256_hmac(const unsigned char *key, int keylen,memset (tmpbuf,0 ,sizeof (tmpbuf));memset (&ctx,0 ,sizeof (sha256_context)); } + +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)
lib/mbedtls/sha256.c void sha256_hmac(const unsigned char *key, int keylen, · line 118
Quoted 10 lines
lib/mbedtls/sha256.c +10
-0+{ +const mbedtls_md_info_t *md; + + md =mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 ); + +return mbedtls_hkdf (md, salt, saltlen, + ikm, ikmlen, + info, infolen, + output, outputlen); +}
Missing #if CONFIG_IS_ENABLED(HKDF_MBEDTLS).
Regards,
Raymond
Re: [PATCH v2 4/5] lib: mbedtls: sha256: add support of key derivation
Hi Raymond,
Le 05/12/2024 à 18:18, Raymond Mao a écrit :
Quoted 31 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 Wed, 4 Dec 2024 at 12:54, Philippe Reynes <[email protected]> wrote:
Adds the support of key derivation using the scheme hkdf. This scheme is defined in rfc5869. Signed-off-by: Philippe Reynes <[email protected]> --- include/u-boot/sha256.h | 5 +++++ lib/mbedtls/sha256.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/u-boot/sha256.h b/include/u-boot/sha256.h index 77dbcd4553a..0a6e309e3ab 100644 --- a/include/u-boot/sha256.h +++ b/include/u-boot/sha256.h @@ -48,4 +48,9 @@ void sha256_hmac(const unsigned char *key, int keylen, const unsigned char *input, unsigned int ilen, unsigned char *output); +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); +
Quoted 1 line
An inline function is needed when the build options are not fulfilled.
Ah yes, I forgot to manage the case where sha256_hkdf is not provided. I will be fixed in v3.
Quoted 36 lines
#endif /* _SHA256_H */
diff --git a/lib/mbedtls/sha256.c b/lib/mbedtls/sha256.c
index 1b9fc1a8503..c38e3e5818c 100644
--- a/lib/mbedtls/sha256.c
+++ b/lib/mbedtls/sha256.c
@@ -11,6 +11,9 @@
#include <string.h>
#include <u-boot/sha256.h>
+#include <mbedtls/md.h>
+#include <mbedtls/hkdf.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,
@@ -98,3 +101,18 @@ void sha256_hmac(const unsigned char *key, int
keylen,
memset(tmpbuf, 0, sizeof(tmpbuf));
memset(&ctx, 0, sizeof(sha256_context));
}
+
+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);
+
+ return mbedtls_hkdf(md, salt, saltlen,
+ ikm, ikmlen,
+ info, infolen,
+ output, outputlen);
+}Quoted 1 line
Missing
#if CONFIG_IS_ENABLED(HKDF_MBEDTLS).
Again, you're right, I forgot this #if ...
I add it in the new version v3
Quoted 3 lines
Regards,
Raymond
Regards,
Philippe
PATCH v2 5/5 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 | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 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_MBEDTLS) += 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 +104 -0
104 lines not on this page · raw message
Signature · 1 line
2.25.1