lib: sha256: add feature sha256_hmac
PATCH 1/4 lib: sha256: add feature sha256_hmac
Adds the support of the hmac based on sha256.
This implementation is based on rfc2104.
--- include/u-boot/sha256.h | 4 ++++ lib/sha256.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+)
include/u-boot/sha256.h +4 -0
@@ -24,4 +24,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/sha256.c +40 -0
@@ -298,3 +298,43 @@ void sha256_csum_wd(const unsigned char *input, unsigned int ilen,sha256_finish (&ctx, output); } + +/* +* Output = HMAC-SHA-256( input buffer, hmac key ) +*/ +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,64 ); +sha256_update (&ctx, input, ilen); +sha256_finish (&ctx, tmpbuf); + +sha256_starts (&ctx); +sha256_update (&ctx, k_opad,64 ); +sha256_update (&ctx, tmpbuf,32 ); +sha256_finish (&ctx, output); + +memset (k_ipad,0 ,64 ); +memset (k_opad,0 ,64 ); +memset (tmpbuf,0 ,32 ); +memset (&ctx,0 ,sizeof (sha256_context)); +}
Signature · 1 line
2.25.1
PATCH 2/4 test: lib: add test for sha256_hamc
Adds 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
@@ -19,6 +19,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 +108 -0
108 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH 2/4] test: lib: add test for sha256_hamc
Quoted 10 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.c
PATCH 3/4 lib: 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 | 8 ++++++++ lib/sha256.c | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+)
include/u-boot/sha256.h +8 -0
@@ -6,6 +6,9 @@#define SHA256_SUM_LEN 32#define SHA256_DER_LEN 19 +#define SHA256_HKDF_MAX_INFO_LEN 256 +#define SHA256_HKDF_MAX_DATA_LEN (SHA256_HKDF_MAX_INFO_LEN + SHA256_SUM_LEN + 1) +extern const uint8_t sha256_der_prefix[];/* Reset watchdog each time we process this many bytes */
@@ -28,4 +31,9 @@ void sha256_hmac(const unsigned char *key, int keylen, const unsigned char *input, unsigned int ilen, unsigned char *output); +void 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/sha256.c +42 -0
45 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH 3/4] lib: sha256: add support of key derivation
Quoted 9 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 | 8 ++++++++ lib/sha256.c | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+)
include/u-boot/sha256.h line 10
Quoted 11 lines
include/u-boot/sha256.h +2
-0@@ -6,6 +6,9 @@#define SHA256_SUM_LEN 32#define SHA256_DER_LEN 19 +#define SHA256_HKDF_MAX_INFO_LEN 256 +#define SHA256_HKDF_MAX_DATA_LEN (SHA256_HKDF_MAX_INFO_LEN + SHA256_SUM_LEN + 1)
long line?
[..]
PATCH 4/4 test: lib: add test for key derivation
In reply to Philippe ReynesAdds a test for the function sha256_hkdf.
--- test/lib/Makefile | 2 +- test/lib/test_sha256_hkdf.c | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 test/lib/test_sha256_hkdf.c
test/lib/Makefile +1 -1
@@ -19,7 +19,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_SHA256) += test_sha256_hmac.o 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
Re: [PATCH 4/4] test: lib: add test for key derivation
Hi Philippe,
Quoted 10 lines · Philippe Reynes
Adds a test for the function
sha256_hkdf.Signed-off-by Philippe Reynes <[email protected]>--- test/lib/Makefile | 2 +- test/lib/test_sha256_hkdf.c | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 test/lib/test_sha256_hkdf.c
Possible nit below
Quoted 97 lines
test/lib/Makefile +1
-1@@ -19,7 +19,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_SHA256) += test_sha256_hmac.o test_sha256_hkdf.o obj-$(CONFIG_GETOPT) += getopt.o obj-$(CONFIG_CRC8) += test_crc8.o obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.otest/lib/test_sha256_hkdf.c new file +78
-0@@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2024 Philippe Reynes <[email protected]> +* +* Unit tests for sha256_hkdf functions +*/ + +#include <command.h> +#include <test/lib.h> +#include <test/test.h> +#include <test/ut.h> +#include <u-boot/sha256.h> + +struct test_sha256_hkdf_s { +unsigned char *salt; +int saltlen; +unsigned char *ikm; +int ikmlen; +unsigned char *info; +int infolen; +unsigned char *expected; +int expectedlen; +}; + +/* +* data comes from: +* https://www.rfc-editor.org/rfc/rfc5869 +*/ +static unsigned char salt_test1[] = { +0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 , +0x07 ,0x08 ,0x09 ,0x0a ,0x0b ,0x0c }; + +static unsigned char ikm_test1[] = { +0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b , +0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b ,0x0b }; + +static unsigned char info_test1[] = { +0xf0 ,0xf1 ,0xf2 ,0xf3 ,0xf4 ,0xf5 ,0xf6 ,0xf7 ,0xf8 ,0xf9 }; + +static unsigned char expected_test1[] = { +0x3c ,0xb2 ,0x5f ,0x25 ,0xfa ,0xac ,0xd5 ,0x7a , +0x90 ,0x43 ,0x4f ,0x64 ,0xd0 ,0x36 ,0x2f ,0x2a , +0x2d ,0x2d ,0x0a ,0x90 ,0xcf ,0x1a ,0x5a ,0x4c , +0x5d ,0xb0 ,0x2d ,0x56 ,0xec ,0xc4 ,0xc5 ,0xbf , +0x34 ,0x00 ,0x72 ,0x08 ,0xd5 ,0xb8 ,0x87 ,0x18 , +0x58 ,0x65 }; + +static struct test_sha256_hkdf_s test_sha256_hkdf[] = { + { + .salt = salt_test1, + .saltlen =sizeof (salt_test1), + .ikm = ikm_test1, + .ikmlen =sizeof (ikm_test1), + .info = info_test1, + .infolen =sizeof (info_test1), + .expected = expected_test1, + .expectedlen =sizeof (expected_test1), + }, +}; + +static int _lib_test_sha256_hkdf_run (struct unit_test_state *uts, +unsigned char *salt,int saltlen, +unsigned char *ikm,int ikmlen, +unsigned char *info,int infolen, +unsigned char *expected,int expectedlen) +{ +unsigned char output[64 ]; + +sha256_hkdf (salt, saltlen, ikm, ikmlen, info, infolen, output, expectedlen); +ut_asserteq_mem (expected, output, expectedlen); + +return 0 ; +} + +static int lib_test_sha256_hkdf_run (struct unit_test_state *uts, +struct test_sha256_hkdf_s *test) +{ +unsigned char *salt = test->salt;
test/lib/test_sha256_hkdf.c line 88
Quoted 10 lines
test/lib/test_sha256_hkdf.c new file +10
-0+int saltlen = test->saltlen; +unsigned char *ikm = test->ikm; +int ikmlen = test->ikmlen; +unsigned char *info = test->info; +int infolen = test->infolen; +unsigned char *expected = test->expected; +int expectedlen = test->expectedlen; + +return _lib_test_sha256_hkdf_run (uts, salt, saltlen, ikm, ikmlen, + info, infolen, expected, expectedlen);
It is common to use ut_assertok() on functions called from tests, so that any error report shows the full call trace back from the failure.
Quoted 19 lines
test/lib/test_sha256_hkdf.c new file +16
-1+} + +static int lib_test_sha256_hkdf (struct unit_test_state *uts) +{ +int i, ret =0 ; + +for (i =0 ; i <ARRAY_SIZE (test_sha256_hkdf); i++) { + ret =lib_test_sha256_hkdf_run (uts, &test_sha256_hkdf[i]); +if (ret) +break ; + } + +return ret; +} + +LIB_TEST (lib_test_sha256_hkdf,0 ); --2.25.1
Quoted 1 line
-- This message and any attachments herein are confidential, intended solely for the addressees and are SoftAtHome’s ownership. Any unauthorized use or dissemination is prohibited. If you are not the intended addressee of this message, please cancel it immediately and inform the sender.
This seems to be incorrect.
Regards,
Simon
Hi Philippe,
It might be useful to have a cover letter explaining what the plans for this code are, great that there are tests but adding code in without it being used isn't always a feature so a cover letter with some details often helps with the context.
Also if you're not aware there's work to integrate MBedTLS [1] and I'm not sure if that also may provide the functionality.
Peter
[1] https://lists.denx.de/pipermail/u-boot/2024-July/557832.html
Quoted 74 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/sha256.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+)include/u-boot/sha256.h +4
-0@@ -24,4 +24,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/sha256.c +40
-1@@ -298,3 +298,43 @@ void sha256_csum_wd(const unsigned char *input, unsigned int ilen,sha256_finish (&ctx, output); } + +/* +* Output = HMAC-SHA-256( input buffer, hmac key ) +*/ +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,64 ); +sha256_update (&ctx, input, ilen); +sha256_finish (&ctx, tmpbuf); + +sha256_starts (&ctx); +sha256_update (&ctx, k_opad,64 ); +sha256_update (&ctx, tmpbuf,32 ); +sha256_finish (&ctx, output); + +memset (k_ipad,0 ,64 ); +memset (k_opad,0 ,64 ); +memset (tmpbuf,0 ,32 ); +memset (&ctx,0 ,sizeof (sha256_context)); +} --2.25.1
Hi Peter,
Le 16/07/2024 à 18:56, Peter Robinson a écrit :
Quoted 8 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,
It might be useful to have a cover letter explaining what the plans for this code are, great that there are tests but adding code in without it being used isn't always a feature so a cover letter with some details often helps with the context.
You right, I should have added a cover letter.
My goal was to add key derivation and use this feature to fill a key manager, and then provide those keys (or some of them) to the kernel. So the kernel may (for example) add them in the KRS.
Do you know if there are some work or interest in a key manager for u-boot please ?
Quoted 3 lines
Also if you're not aware there's work to integrate MBedTLS [1] and I'm not sure if that also may provide the functionality.
Good point, I miss it. MBedTLS has the feature of key derivation.
https://mbed-tls.readthedocs.io/en/latest/getting_started/psa/#deriving-a-new-key-from-an-existing-key
So unless someone wants to use key derivation without all MBedTLS, this serie is not very useful.
Quoted 2 lines
Peter
Regards,
Philippe
Quoted 78 lines
[1] https://lists.denx.de/pipermail/u-boot/2024-July/557832.html
Quoted 73 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/sha256.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+)include/u-boot/sha256.h +4
-0@@ -24,4 +24,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/sha256.c +40
-1@@ -298,3 +298,43 @@ void sha256_csum_wd(const unsigned char *input, unsigned int ilen,sha256_finish (&ctx, output); } + +/* +* Output = HMAC-SHA-256( input buffer, hmac key ) +*/ +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,64 ); +sha256_update (&ctx, input, ilen); +sha256_finish (&ctx, tmpbuf); + +sha256_starts (&ctx); +sha256_update (&ctx, k_opad,64 ); +sha256_update (&ctx, tmpbuf,32 ); +sha256_finish (&ctx, output); + +memset (k_ipad,0 ,64 ); +memset (k_opad,0 ,64 ); +memset (tmpbuf,0 ,32 ); +memset (&ctx,0 ,sizeof (sha256_context)); +} --2.25.1
Quoted 22 lines · Philippe REYNES
Hi Peter,
Le 16/07/2024 à 18:56, Peter Robinson a écrit :
Quoted 8 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,
It might be useful to have a cover letter explaining what the plans for this code are, great that there are tests but adding code in without it being used isn't always a feature so a cover letter with some details often helps with the context.
You right, I should have added a cover letter.
My goal was to add key derivation and use this feature to fill a key manager,
and then provide those keys (or some of them) to the kernel. So the kernel may (for example) add them in the KRS.Do you know if there are some work or interest in a key manager for u-boot please ?
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
Also if you're not aware there's work to integrate MBedTLS [1] and I'm not sure if that also may provide the functionality.
Good point, I miss it. MBedTLS has the feature of key derivation.
https://mbed-tls.readthedocs.io/en/latest/getting_started/psa/#deriving-a-new-key-from-an-existing-key
So unless someone wants to use key derivation without all MBedTLS, this serie is not very useful.
Unless you object, I would really prefer to have this been a feature U-Boot only has with MBedTLS enabled as one of the goals with that integration is to have U-Boot leverage existing and well audited/monitored codebases for security sensitive code paths when possible.
Signature · 1 line
Tom
Hi Tom,
Le 17/07/2024 à 19:58, Tom Rini a écrit :
Quoted 27 lines · Philippe REYNES
Quoted 20 lines
Hi Peter,
Le 16/07/2024 à 18:56, Peter Robinson a écrit :
Quoted 8 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,
It might be useful to have a cover letter explaining what the plans for this code are, great that there are tests but adding code in without it being used isn't always a feature so a cover letter with some details often helps with the context.
You right, I should have added a cover letter.
My goal was to add key derivation and use this feature to fill a key manager,
and then provide those keys (or some of them) to the kernel. So the kernel may (for example) add them in the KRS.Do you know if there are some work or interest in a key manager for u-boot please ?
Quoted 6 lines
Quoted 2 lines
Also if you're not aware there's work to integrate MBedTLS [1] and I'm not sure if that also may provide the functionality.
Good point, I miss it. MBedTLS has the feature of key derivation.
https://mbed-tls.readthedocs.io/en/latest/getting_started/psa/#deriving-a-new-key-from-an-existing-key
So unless someone wants to use key derivation without all MBedTLS, this serie is not very useful.
Quoted 6 lines
Unless you object, I would really prefer to have this been a feature U-Boot only has with MBedTLS enabled as one of the goals with that integration is to have U-Boot leverage existing and well audited/monitored codebases for security sensitive code paths when possible.
I don't object, I also think that a feature should be only implemented once.
I just have a question on this topic, I am planning to use a key manager in u-boot. Do you think a key manager would be nice in u-boot, and if someone has already planned to work on this topic please ?
Regards,
Philippe