crypto: robustness fixes to handling of SM3 hash
v2 cover crypto: robustness fixes to handling of SM3 hash
Note that patch 4 is best reviewed by applying to git and then using 'git show -b' to hide the huge whitespace indentation change.
Changed in v2:
* Use array range initializer for setting default value
* Drop redundant countermeasures now we can gurantee a correct default initializer value
* Drop SM3 conditions in tests
* Refactor tests to register one test per algorithm
Daniel P. Berrangé (4):
crypto: set default initializer hmac/hash arrays to -1
crypto: unconditionally report SM3 hash length tests/unit: drop conditionals for SM3 tests/unit: register one test per hash/hmac algorithm
crypto/hash-glib.c | 3 +- crypto/hash.c | 4 +- crypto/hmac-glib.c | 4 +- tests/unit/test-crypto-hash.c | 325 +++++++++++++++------------------- tests/unit/test-crypto-hmac.c | 272 +++++++++++++--------------- 5 files changed, 275 insertions(+), 333 deletions(-)
Signature · 1 line
2.56.0
PATCH v2 1/4 crypto: set default initializer hmac/hash arrays to -1
The default static array initializer of 0 is not suitable for the glib hash/hmac backends, as G_CHECKSUM_MD5 has a value of
0. Instead elements must be initialized to -1, but remembering todo that as new algorithms are defined is errorprone. The SM3 algorithm initializer is missing, leading to it being mapped to MD5. This could potentially be a security flaw through use of the wrong (much weaker) hashing algorithm, if a further bug elsewhere didn't mask the issue.
Set a default array initializer so unlisted algorithms are always set to -1 instead of 0.
--- crypto/hash-glib.c | 3 +-- crypto/hmac-glib.c | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-)
crypto/hash-glib.c +1 -2
@@ -26,13 +26,12 @@static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALGO__MAX ] = { + [0 ...QCRYPTO_HASH_ALGO__MAX -1 ] =-1 , [QCRYPTO_HASH_ALGO_MD5 ] =G_CHECKSUM_MD5 , [QCRYPTO_HASH_ALGO_SHA1 ] =G_CHECKSUM_SHA1 , - [QCRYPTO_HASH_ALGO_SHA224 ] =-1 , [QCRYPTO_HASH_ALGO_SHA256 ] =G_CHECKSUM_SHA256 , [QCRYPTO_HASH_ALGO_SHA384 ] =G_CHECKSUM_SHA384 , [QCRYPTO_HASH_ALGO_SHA512 ] =G_CHECKSUM_SHA512 , - [QCRYPTO_HASH_ALGO_RIPEMD160 ] =-1 , };gboolean qcrypto_hash_supports (QCryptoHashAlgo alg)
crypto/hmac-glib.c +1 -3
@@ -18,13 +18,11 @@#include "hmacpriv.h" static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX ] = { + [0 ...QCRYPTO_HASH_ALGO__MAX -1 ] =-1 , [QCRYPTO_HASH_ALGO_MD5 ] =G_CHECKSUM_MD5 , [QCRYPTO_HASH_ALGO_SHA1 ] =G_CHECKSUM_SHA1 , [QCRYPTO_HASH_ALGO_SHA256 ] =G_CHECKSUM_SHA256 , [QCRYPTO_HASH_ALGO_SHA512 ] =G_CHECKSUM_SHA512 , - [QCRYPTO_HASH_ALGO_SHA224 ] =-1 , - [QCRYPTO_HASH_ALGO_SHA384 ] =-1 , - [QCRYPTO_HASH_ALGO_RIPEMD160 ] =-1 , };typedef struct QCryptoHmacGlib QCryptoHmacGlib ;
Signature · 1 line
2.56.0
PATCH v2 2/4 crypto: unconditionally report SM3 hash length
In reply to Daniel P. BerrangéCallers may query the hash digest length before getting to the code path that performs hashing, which is where the lack of support for SM3 in the backend will be reported.
Reporting a hash length of 0 is liable to cause divide by zero errors in callers.
--- crypto/hash.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
crypto/hash.c +1 -3
@@ -33,9 +33,7 @@ static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALGO__MAX] = { [QCRYPTO_HASH_ALGO_SHA384] = QCRYPTO_HASH_DIGEST_LEN_SHA384, [QCRYPTO_HASH_ALGO_SHA512] = QCRYPTO_HASH_DIGEST_LEN_SHA512, [QCRYPTO_HASH_ALGO_RIPEMD160] = QCRYPTO_HASH_DIGEST_LEN_RIPEMD160, -#ifdef CONFIG_CRYPTO_SM3 - [QCRYPTO_HASH_ALGO_SM3] = QCRYPTO_HASH_DIGEST_LEN_SM3, -#endif + [QCRYPTO_HASH_ALGO_SM3] = QCRYPTO_HASH_DIGEST_LEN_SM3, };size_t qcrypto_hash_digest_len (QCryptoHashAlgo alg)
Signature · 1 line
2.56.0
PATCH v2 3/4 tests/unit: drop conditionals for SM3
In reply to Daniel P. BerrangéThere is no need to conditionalize test data for SM3, since the test explicitly checks whether each algorithm is supported.
--- tests/unit/test-crypto-hash.c | 10 ---------- tests/unit/test-crypto-hmac.c | 4 +--- 2 files changed, 1 insertion(+), 13 deletions(-)
tests/unit/test-crypto-hash.c +0 -10
@@ -43,9 +43,7 @@"63b54e4cb2d2032b393994aa263c0dbb" \ "e00a9f2fe9ef6037352232a1eec55ee7"#define OUTPUT_RIPEMD160 "f3d658fad3fdfb2b52c9369cf0d441249ddfa8a0" -#ifdef CONFIG_CRYPTO_SM3 #define OUTPUT_SM3 "d4a97db105b477b84c4f20ec9c31a6c814e2705a0b83a5a89748d75f0ef456a1" -#endif #define OUTPUT_MD5_B64 "Yo0gY3FWMDWrjvYvSSveyQ=="#define OUTPUT_SHA1_B64 "sudPJnWKOkIeUJzuBFJEt4dTzAI="
@@ -58,9 +56,7 @@ "7sVe5w=="#define OUTPUT_RIPEMD160_B64 "89ZY+tP9+ytSyTac8NRBJJ3fqKA=" -#ifdef CONFIG_CRYPTO_SM3 #define OUTPUT_SM3_B64 "1Kl9sQW0d7hMTyDsnDGmyBTicFoLg6Wol0jXXw70VqE=" -#endif static const char *expected_outputs[] = { [QCRYPTO_HASH_ALGO_MD5 ] =OUTPUT_MD5 ,
@@ -70,9 +66,7 @@ static const char *expected_outputs[] = { [QCRYPTO_HASH_ALGO_SHA384] = OUTPUT_SHA384, [QCRYPTO_HASH_ALGO_SHA512] = OUTPUT_SHA512, [QCRYPTO_HASH_ALGO_RIPEMD160] = OUTPUT_RIPEMD160, -#ifdef CONFIG_CRYPTO_SM3 [QCRYPTO_HASH_ALGO_SM3] = OUTPUT_SM3, -#endif };static const char *expected_outputs_b64[] = { [QCRYPTO_HASH_ALGO_MD5 ] =OUTPUT_MD5_B64 ,
@@ -82,9 +76,7 @@ static const char *expected_outputs_b64[] = { [QCRYPTO_HASH_ALGO_SHA384] = OUTPUT_SHA384_B64, [QCRYPTO_HASH_ALGO_SHA512] = OUTPUT_SHA512_B64, [QCRYPTO_HASH_ALGO_RIPEMD160] = OUTPUT_RIPEMD160_B64, -#ifdef CONFIG_CRYPTO_SM3 [QCRYPTO_HASH_ALGO_SM3] = OUTPUT_SM3_B64, -#endif };static const int expected_lens[] = { [QCRYPTO_HASH_ALGO_MD5 ] =16 ,
@@ -94,9 +86,7 @@ static const int expected_lens[] = { [QCRYPTO_HASH_ALGO_SHA384] = 48, [QCRYPTO_HASH_ALGO_SHA512] = 64, [QCRYPTO_HASH_ALGO_RIPEMD160] = 20, -#ifdef CONFIG_CRYPTO_SM3 [QCRYPTO_HASH_ALGO_SM3] = 32, -#endif };static const char hex[] ="0123456789abcdef" ;
tests/unit/test-crypto-hmac.c +1 -3
@@ -76,14 +76,12 @@ static QCryptoHmacTestData test_data[] = {"94964ed4c1155b62b668c241d67279e5" "8a711676", }, -#ifdef CONFIG_CRYPTO_SM3 { .alg = QCRYPTO_HASH_ALGO_SM3, .hex_digest ="760e3799332bc913819b930085360ddb" - "c05529261313d5b15b75bab4fd7ae91e", + "c05529261313d5b15b75bab4fd7ae91e", }, -#endif };static const char hex[] ="0123456789abcdef" ;
Signature · 1 line
2.56.0
PATCH v2 4/4 tests/unit: register one test per hash/hmac algorithm
In reply to Daniel P. BerrangéThe hash/hmac tests iterate over all support algorithms testing each one in turn, but this is not visible from the test output. Register one test per algorithm so the test result directly reflect what algorithms were tested. This is useful information since different crypto backends support different subsets of algorithms.
--- tests/unit/test-crypto-hash.c | 315 ++++++++++++++++------------------ tests/unit/test-crypto-hmac.c | 268 ++++++++++++++--------------- 2 files changed, 271 insertions(+), 312 deletions(-)
tests/unit/test-crypto-hash.c +144 -171
@@ -92,218 +92,191 @@ static const int expected_lens[] = {static const char hex[] ="0123456789abcdef" ;/* Test with dynamic allocation */ -static void test_hash_alloc (void ) +static void test_hash_alloc (const void *opaque) { -size_t i; - -for (i =0 ; i <G_N_ELEMENTS (expected_outputs) ; i++) { -uint8_t *result =NULL ; -size_t resultlen =0 ; -int ret; -size_t j; - -if (!qcrypto_hash_supports (i)) { -continue ; - } - - ret =qcrypto_hash_bytes (i, -INPUT_TEXT , -strlen (INPUT_TEXT ), - &result, - &resultlen, - &error_fatal); -g_assert (ret ==0 ); -g_assert (resultlen == expected_lens[i]); +QCryptoHashAlgo alg =GPOINTER_TO_INT (opaque); +uint8_t *result =NULL ; +size_t resultlen =0 ; +int ret; +size_t j; + + ret =qcrypto_hash_bytes (alg, +INPUT_TEXT , +strlen (INPUT_TEXT ), + &result, + &resultlen, + &error_fatal); +g_assert (ret ==0 ); +g_assert (resultlen == expected_lens[alg]); -for (j =0 ; j < resultlen; j++) { -g_assert (expected_outputs[i][j *2 ] == hex[(result[j] >>4 ) &0xf ]); -g_assert (expected_outputs[i][j *2 +1 ] == hex[result[j] &0xf ]); - } -g_free (result); +for (j =0 ; j < resultlen; j++) { +g_assert (expected_outputs[alg][j *2 ] == hex[(result[j] >>4 ) &0xf ]); +g_assert (expected_outputs[alg][j *2 +1 ] == hex[result[j] &0xf ]); } +g_free (result); }/* Test with caller preallocating */ -static void test_hash_prealloc (void ) +static void test_hash_prealloc (const void *opaque) { -size_t i; - -for (i =0 ; i <G_N_ELEMENTS (expected_outputs) ; i++) { -uint8_t *result, *origresult; -size_t resultlen; -int ret; -size_t j; - -if (!qcrypto_hash_supports (i)) { -continue ; - } - - resultlen = expected_lens[i]; - origresult = result =g_new0 (uint8_t, resultlen); - - ret =qcrypto_hash_bytes (i, -INPUT_TEXT , -strlen (INPUT_TEXT ), - &result, - &resultlen, - &error_fatal); -g_assert (ret ==0 ); -/* Validate that our pre-allocated pointer was not replaced */ -g_assert (result == origresult); -g_assert (resultlen == expected_lens[i]); -for (j =0 ; j < resultlen; j++) { -g_assert (expected_outputs[i][j *2 ] == hex[(result[j] >>4 ) &0xf ]); -g_assert (expected_outputs[i][j *2 +1 ] == hex[result[j] &0xf ]); - } -g_free (result); +QCryptoHashAlgo alg =GPOINTER_TO_INT (opaque); +uint8_t *result, *origresult; +size_t resultlen; +int ret; +size_t j; + + resultlen = expected_lens[alg]; + origresult = result =g_new0 (uint8_t, resultlen); + + ret =qcrypto_hash_bytes (alg, +INPUT_TEXT , +strlen (INPUT_TEXT ), + &result, + &resultlen, + &error_fatal); +g_assert (ret ==0 ); +/* Validate that our pre-allocated pointer was not replaced */ +g_assert (result == origresult); +g_assert (resultlen == expected_lens[alg]); +for (j =0 ; j < resultlen; j++) { +g_assert (expected_outputs[alg][j *2 ] == hex[(result[j] >>4 ) &0xf ]); +g_assert (expected_outputs[alg][j *2 +1 ] == hex[result[j] &0xf ]); } +g_free (result); }/* Test with dynamic allocation */ -static void test_hash_iov (void ) +static void test_hash_iov (const void *opaque) { -size_t i; - -for (i =0 ; i <G_N_ELEMENTS (expected_outputs) ; i++) { -struct iovec iov[3 ] = { - { .iov_base = (char *)INPUT_TEXT1 , .iov_len =strlen (INPUT_TEXT1 ) }, - { .iov_base = (char *)INPUT_TEXT2 , .iov_len =strlen (INPUT_TEXT2 ) }, - { .iov_base = (char *)INPUT_TEXT3 , .iov_len =strlen (INPUT_TEXT3 ) }, - }; -uint8_t *result =NULL ; -size_t resultlen =0 ; -int ret; -size_t j; - -if (!qcrypto_hash_supports (i)) { -continue ; - } - - ret =qcrypto_hash_bytesv (i, - iov,3 , - &result, - &resultlen, - &error_fatal); -g_assert (ret ==0 ); -g_assert (resultlen == expected_lens[i]); -for (j =0 ; j < resultlen; j++) { -g_assert (expected_outputs[i][j *2 ] == hex[(result[j] >>4 ) &0xf ]); -g_assert (expected_outputs[i][j *2 +1 ] == hex[result[j] &0xf ]); - } -g_free (result); +QCryptoHashAlgo alg =GPOINTER_TO_INT (opaque); +struct iovec iov[3 ] = { + { .iov_base = (char *)INPUT_TEXT1 , .iov_len =strlen (INPUT_TEXT1 ) }, + { .iov_base = (char *)INPUT_TEXT2 , .iov_len =strlen (INPUT_TEXT2 ) }, + { .iov_base = (char *)INPUT_TEXT3 , .iov_len =strlen (INPUT_TEXT3 ) }, + }; +uint8_t *result =NULL ; +size_t resultlen =0 ; +int ret; +size_t j; + + ret =qcrypto_hash_bytesv (alg, + iov,3 , + &result, + &resultlen, + &error_fatal); +g_assert (ret ==0 ); +g_assert (resultlen == expected_lens[alg]); +for (j =0 ; j < resultlen; j++) { +g_assert (expected_outputs[alg][j *2 ] == hex[(result[j] >>4 ) &0xf ]); +g_assert (expected_outputs[alg][j *2 +1 ] == hex[result[j] &0xf ]); } +g_free (result); }/* Test with printable hashing */ -static void test_hash_digest (void ) +static void test_hash_digest (const void *opaque) { -size_t i; - -for (i =0 ; i <G_N_ELEMENTS (expected_outputs) ; i++) { -int ret; -char *digest; -size_t digestsize; - -if (!qcrypto_hash_supports (i)) { -continue ; - } +QCryptoHashAlgo alg =GPOINTER_TO_INT (opaque); +int ret; +char *digest; +size_t digestsize; - digestsize =qcrypto_hash_digest_len (i); + digestsize =qcrypto_hash_digest_len (alg); - g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[i])); + g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[alg])); - ret =qcrypto_hash_digest (i, -INPUT_TEXT , -strlen (INPUT_TEXT ), - &digest, - &error_fatal); -g_assert (ret ==0 ); - g_assert_cmpstr(digest, ==, expected_outputs[i]); -g_free (digest); - } + ret =qcrypto_hash_digest (alg, +INPUT_TEXT , +strlen (INPUT_TEXT ), + &digest, + &error_fatal); +g_assert (ret ==0 ); + g_assert_cmpstr(digest, ==, expected_outputs[alg]); +g_free (digest); }/* Test with base64 encoding */ -static void test_hash_base64 (void ) +static void test_hash_base64 (const void *opaque) { -size_t i; - -for (i =0 ; i <G_N_ELEMENTS (expected_outputs) ; i++) { -int ret; -char *digest; - -if (!qcrypto_hash_supports (i)) { -continue ; - } - - ret =qcrypto_hash_base64 (i, -INPUT_TEXT , -strlen (INPUT_TEXT ), - &digest, - &error_fatal); -g_assert (ret ==0 ); - g_assert_cmpstr(digest, ==, expected_outputs_b64[i]); -g_free (digest); - } +QCryptoHashAlgo alg =GPOINTER_TO_INT (opaque); +int ret; +char *digest; + + ret =qcrypto_hash_base64 (alg, +INPUT_TEXT , +strlen (INPUT_TEXT ), + &digest, + &error_fatal); +g_assert (ret ==0 ); + g_assert_cmpstr(digest, ==, expected_outputs_b64[alg]); +g_free (digest); } -static void test_hash_accumulate (void ) +static void test_hash_accumulate (const void *opaque) { -size_t i; - -for (i =0 ; i <G_N_ELEMENTS (expected_outputs) ; i++) { -g_autoptr (QCryptoHash) hash =NULL ; -struct iovec iov[] = { - { .iov_base = (char *)INPUT_TEXT1 , .iov_len =strlen (INPUT_TEXT1 ) }, - { .iov_base = (char *)INPUT_TEXT2 , .iov_len =strlen (INPUT_TEXT2 ) }, - { .iov_base = (char *)INPUT_TEXT3 , .iov_len =strlen (INPUT_TEXT3 ) }, - }; - g_autofree uint8_t *result = NULL; -size_t resultlen =0 ; -int ret; -size_t j; - -if (!qcrypto_hash_supports (i)) { -continue ; - } +QCryptoHashAlgo alg =GPOINTER_TO_INT (opaque); +g_autoptr (QCryptoHash) hash =NULL ; +struct iovec iov[] = { + { .iov_base = (char *)INPUT_TEXT1 , .iov_len =strlen (INPUT_TEXT1 ) }, + { .iov_base = (char *)INPUT_TEXT2 , .iov_len =strlen (INPUT_TEXT2 ) }, + { .iov_base = (char *)INPUT_TEXT3 , .iov_len =strlen (INPUT_TEXT3 ) }, + }; + g_autofree uint8_t *result = NULL; +size_t resultlen =0 ; +int ret; +size_t j; + + hash =qcrypto_hash_new (alg, &error_fatal); +g_assert (hash !=NULL ); + +/* Add each iovec to the hash context separately */ +for (j =0 ; j <G_N_ELEMENTS (iov); j++) { + ret =qcrypto_hash_updatev (hash, + &iov[j],1 , + &error_fatal); - hash =qcrypto_hash_new (i, &error_fatal); -g_assert (hash !=NULL ); +g_assert (ret ==0 ); + } -/* Add each iovec to the hash context separately */ -for (j =0 ; j <G_N_ELEMENTS (iov); j++) { - ret =qcrypto_hash_updatev (hash, - &iov[j],1 , + ret =qcrypto_hash_finalize_bytes (hash, &result, &resultlen, &error_fatal); -g_assert (ret ==0 ); - } - - ret =qcrypto_hash_finalize_bytes (hash, &result, &resultlen, - &error_fatal); - -g_assert (ret ==0 ); -g_assert (resultlen == expected_lens[i]); -for (j =0 ; j < resultlen; j++) { -g_assert (expected_outputs[i][j *2 ] == hex[(result[j] >>4 ) &0xf ]); -g_assert (expected_outputs[i][j *2 +1 ] == hex[result[j] &0xf ]); - } +g_assert (ret ==0 ); +g_assert (resultlen == expected_lens[alg]); +for (j =0 ; j < resultlen; j++) { +g_assert (expected_outputs[alg][j *2 ] == hex[(result[j] >>4 ) &0xf ]); +g_assert (expected_outputs[alg][j *2 +1 ] == hex[result[j] &0xf ]); } }int main (int argc,char **argv) { +size_t i;int ret =qcrypto_init (&error_fatal);g_assert (ret ==0 );g_test_init (&argc, &argv,NULL ); -g_test_add_func ("/crypto/hash/iov" , test_hash_iov); -g_test_add_func ("/crypto/hash/alloc" , test_hash_alloc); -g_test_add_func ("/crypto/hash/prealloc" , test_hash_prealloc); -g_test_add_func ("/crypto/hash/digest" , test_hash_digest); -g_test_add_func ("/crypto/hash/base64" , test_hash_base64); -g_test_add_func ("/crypto/hash/accumulate" , test_hash_accumulate); + +#define ADD_TEST (alg, prefix, meth) \ + { \ + g_autofree char *name = g_strdup_printf( \ + "%s/%s", prefix, QCryptoHashAlgo_str(alg)); \ + g_test_add_data_func(name, GINT_TO_POINTER(alg), meth); \ + } + +for (i =0 ; i <QCRYPTO_HASH_ALGO__MAX ; i++) { +if (!qcrypto_hash_supports (i)) { +continue ; + } + +ADD_TEST (i,"/crypto/hash/iov" , test_hash_iov); +ADD_TEST (i,"/crypto/hash/alloc" , test_hash_alloc); +ADD_TEST (i,"/crypto/hash/prealloc" , test_hash_prealloc); +ADD_TEST (i,"/crypto/hash/digest" , test_hash_digest); +ADD_TEST (i,"/crypto/hash/base64" , test_hash_base64); +ADD_TEST (i,"/crypto/hash/accumulate" , test_hash_accumulate); + }return g_test_run (); }
tests/unit/test-crypto-hmac.c +127 -141
304 lines not on this page · raw message
Signature · 1 line
2.56.0
ping: i'd like to get this fix into 11.2
Quoted 17 lines · Daniel P. Berrangé
Note that patch 4 is best reviewed by applying to git and then using 'git show -b' to hide the huge whitespace indentation change.
Changed in v2:
* Use array range initializer for setting default value
* Drop redundant countermeasures now we can gurantee a correct default initializer value
* Drop SM3 conditions in tests
* Refactor tests to register one test per algorithmDaniel P. Berrangé (4):
crypto: set default initializer hmac/hash arrays to -1
crypto: unconditionally report SM3 hash length tests/unit: drop conditionals for SM3 tests/unit: register one test per hash/hmac algorithm
Quoted 10 lines
crypto/hash-glib.c | 3 +- crypto/hash.c | 4 +- crypto/hmac-glib.c | 4 +- tests/unit/test-crypto-hash.c | 325 +++++++++++++++------------------- tests/unit/test-crypto-hmac.c | 272 +++++++++++++--------------- 5 files changed, 275 insertions(+), 333 deletions(-)Signature · 1 line
2.56.0
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|