From f37bbe53a01a8a41aa0313accddb721d16c1617c Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Tue, 6 Jul 2021 10:42:54 +0100 Subject: [PATCH 1/6] Extend psa_crypto_metadata tests to check for powers of 2 Add a check to ensure the block_size is or is not a power of 2 Add a new parameter to verify the expected pass/fail when a block_size is or is not a power of 2. Add new sets of input data to verify these tests Fixes #4228 Signed-off-by: Joe Subbiani --- .../test_suite_psa_crypto_metadata.data | 18 +++++++++++--- .../test_suite_psa_crypto_metadata.function | 24 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto_metadata.data b/tests/suites/test_suite_psa_crypto_metadata.data index a3668fcc94..f5f6327200 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.data +++ b/tests/suites/test_suite_psa_crypto_metadata.data @@ -293,15 +293,27 @@ key_type:PSA_KEY_TYPE_DERIVE:KEY_TYPE_IS_UNSTRUCTURED Block cipher key type: AES depends_on:PSA_WANT_KEY_TYPE_AES -block_cipher_key_type:PSA_KEY_TYPE_AES:16 +block_cipher_key_type:PSA_KEY_TYPE_AES:16:0 Block cipher key type: DES depends_on:PSA_WANT_KEY_TYPE_DES -block_cipher_key_type:PSA_KEY_TYPE_DES:8 +block_cipher_key_type:PSA_KEY_TYPE_DES:8:0 Block cipher key type: Camellia depends_on:PSA_WANT_KEY_TYPE_CAMELLIA -block_cipher_key_type:PSA_KEY_TYPE_CAMELLIA:16 +block_cipher_key_type:PSA_KEY_TYPE_CAMELLIA:16:0 + +Block cipher key type: AES +depends_on:PSA_WANT_KEY_TYPE_AES +block_cipher_key_type:PSA_KEY_TYPE_AES:24:1 + +Block cipher key type: AES +depends_on:PSA_WANT_KEY_TYPE_AES +block_cipher_key_type:PSA_KEY_TYPE_AES:12:1 + +Block cipher key type: DES +depends_on:PSA_WANT_KEY_TYPE_DES +block_cipher_key_type:PSA_KEY_TYPE_DES:24:1 Stream cipher key type: ChaCha20 depends_on:PSA_WANT_KEY_TYPE_CHACHA20 diff --git a/tests/suites/test_suite_psa_crypto_metadata.function b/tests/suites/test_suite_psa_crypto_metadata.function index 4790be6de1..fbcc83a260 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.function +++ b/tests/suites/test_suite_psa_crypto_metadata.function @@ -619,7 +619,7 @@ void key_type( int type_arg, int classification_flags ) /* END_CASE */ /* BEGIN_CASE */ -void block_cipher_key_type( int type_arg, int block_size_arg ) +void block_cipher_key_type( int type_arg, int block_size_arg, int expecting_power_2 ) { psa_key_type_t type = type_arg; size_t block_size = block_size_arg; @@ -628,7 +628,27 @@ void block_cipher_key_type( int type_arg, int block_size_arg ) TEST_EQUAL( type & PSA_KEY_TYPE_CATEGORY_MASK, PSA_KEY_TYPE_CATEGORY_SYMMETRIC ); - TEST_EQUAL( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ), block_size ); + + if (expecting_power_2 == 0) + TEST_EQUAL( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ), block_size ); + + /* PSA_ROUND_UP_TO_MULTIPLE(block_size, length) in crypto_sizes.h + * Requires block sizes to be a power of 2. The following test ensures + * the block sizes are indeed powers of 2. + */ + int check = 0; + + while( block_size > 1) + { + if ( block_size % 2 != 0 ) + { + check = 1; + break; + } + block_size = block_size / 2; + } + /* expecting_power_2 should be 0 if true (e.g 16, 32 etc.) or 1 otherwise */ + TEST_EQUAL( check, expecting_power_2 ); } /* END_CASE */ From fc463187923d27bc1851ceeab01607c97a989fbc Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Tue, 6 Jul 2021 14:44:20 +0100 Subject: [PATCH 2/6] Change test suite duplicate descriptions tests/scripts/check_test_cases.py located duplicate descriptions for the negative tests checking that the check fails when a block size is not a power of 2 Signed-off-by: Joe Subbiani --- tests/suites/test_suite_psa_crypto_metadata.data | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto_metadata.data b/tests/suites/test_suite_psa_crypto_metadata.data index f5f6327200..4e47e0c578 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.data +++ b/tests/suites/test_suite_psa_crypto_metadata.data @@ -303,15 +303,15 @@ Block cipher key type: Camellia depends_on:PSA_WANT_KEY_TYPE_CAMELLIA block_cipher_key_type:PSA_KEY_TYPE_CAMELLIA:16:0 -Block cipher key type: AES +Block cipher key type: AES non power 2 block_size (24) depends_on:PSA_WANT_KEY_TYPE_AES block_cipher_key_type:PSA_KEY_TYPE_AES:24:1 -Block cipher key type: AES +Block cipher key type: AES non power 2 block_size (12) depends_on:PSA_WANT_KEY_TYPE_AES block_cipher_key_type:PSA_KEY_TYPE_AES:12:1 -Block cipher key type: DES +Block cipher key type: DES non power 2 block_size (24) depends_on:PSA_WANT_KEY_TYPE_DES block_cipher_key_type:PSA_KEY_TYPE_DES:24:1 From 93213f66497a2250b9285ccbff7dd30064a5e335 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Thu, 8 Jul 2021 15:32:52 +0100 Subject: [PATCH 3/6] Remove test cases and update power 2 check The power 2 check now uses a looping bit shift to try match with the block sizes and will escape the loop when appropriate The test cases, as pointed out by Gilles, could be harmful in the future and testing a test case is not generally necessary Signed-off-by: Joe Subbiani --- .../test_suite_psa_crypto_metadata.data | 18 +++------------ .../test_suite_psa_crypto_metadata.function | 23 +++++++++++-------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto_metadata.data b/tests/suites/test_suite_psa_crypto_metadata.data index 4e47e0c578..a3668fcc94 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.data +++ b/tests/suites/test_suite_psa_crypto_metadata.data @@ -293,27 +293,15 @@ key_type:PSA_KEY_TYPE_DERIVE:KEY_TYPE_IS_UNSTRUCTURED Block cipher key type: AES depends_on:PSA_WANT_KEY_TYPE_AES -block_cipher_key_type:PSA_KEY_TYPE_AES:16:0 +block_cipher_key_type:PSA_KEY_TYPE_AES:16 Block cipher key type: DES depends_on:PSA_WANT_KEY_TYPE_DES -block_cipher_key_type:PSA_KEY_TYPE_DES:8:0 +block_cipher_key_type:PSA_KEY_TYPE_DES:8 Block cipher key type: Camellia depends_on:PSA_WANT_KEY_TYPE_CAMELLIA -block_cipher_key_type:PSA_KEY_TYPE_CAMELLIA:16:0 - -Block cipher key type: AES non power 2 block_size (24) -depends_on:PSA_WANT_KEY_TYPE_AES -block_cipher_key_type:PSA_KEY_TYPE_AES:24:1 - -Block cipher key type: AES non power 2 block_size (12) -depends_on:PSA_WANT_KEY_TYPE_AES -block_cipher_key_type:PSA_KEY_TYPE_AES:12:1 - -Block cipher key type: DES non power 2 block_size (24) -depends_on:PSA_WANT_KEY_TYPE_DES -block_cipher_key_type:PSA_KEY_TYPE_DES:24:1 +block_cipher_key_type:PSA_KEY_TYPE_CAMELLIA:16 Stream cipher key type: ChaCha20 depends_on:PSA_WANT_KEY_TYPE_CHACHA20 diff --git a/tests/suites/test_suite_psa_crypto_metadata.function b/tests/suites/test_suite_psa_crypto_metadata.function index fbcc83a260..2722aea4c1 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.function +++ b/tests/suites/test_suite_psa_crypto_metadata.function @@ -619,7 +619,7 @@ void key_type( int type_arg, int classification_flags ) /* END_CASE */ /* BEGIN_CASE */ -void block_cipher_key_type( int type_arg, int block_size_arg, int expecting_power_2 ) +void block_cipher_key_type( int type_arg, int block_size_arg) { psa_key_type_t type = type_arg; size_t block_size = block_size_arg; @@ -629,26 +629,29 @@ void block_cipher_key_type( int type_arg, int block_size_arg, int expecting_powe TEST_EQUAL( type & PSA_KEY_TYPE_CATEGORY_MASK, PSA_KEY_TYPE_CATEGORY_SYMMETRIC ); - if (expecting_power_2 == 0) - TEST_EQUAL( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ), block_size ); + TEST_EQUAL( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ), block_size ); /* PSA_ROUND_UP_TO_MULTIPLE(block_size, length) in crypto_sizes.h - * Requires block sizes to be a power of 2. The following test ensures - * the block sizes are indeed powers of 2. + * Requires block sizes to be a power of 2. + * The following creates a bit and shifts along until it finds a + * match or a mismatch. */ int check = 0; - while( block_size > 1) + for (size_t index = 1; index > 0; index = index << 1) { - if ( block_size % 2 != 0 ) + if (index == block_size) + { + check = 0; + break; + } + if (index > block_size) { check = 1; break; } - block_size = block_size / 2; } - /* expecting_power_2 should be 0 if true (e.g 16, 32 etc.) or 1 otherwise */ - TEST_EQUAL( check, expecting_power_2 ); + TEST_EQUAL( check, 0); } /* END_CASE */ From da36c9f2ce4fffddc99cc5178912e62049ec59a7 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 9 Jul 2021 12:03:06 +0100 Subject: [PATCH 4/6] Improve Code Style add space between bracket and a newline that had occured through changes but do not match the original file style Signed-off-by: Joe Subbiani --- tests/suites/test_suite_psa_crypto_metadata.function | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto_metadata.function b/tests/suites/test_suite_psa_crypto_metadata.function index 2722aea4c1..f2dcfca2dd 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.function +++ b/tests/suites/test_suite_psa_crypto_metadata.function @@ -619,7 +619,7 @@ void key_type( int type_arg, int classification_flags ) /* END_CASE */ /* BEGIN_CASE */ -void block_cipher_key_type( int type_arg, int block_size_arg) +void block_cipher_key_type( int type_arg, int block_size_arg ) { psa_key_type_t type = type_arg; size_t block_size = block_size_arg; @@ -628,7 +628,6 @@ void block_cipher_key_type( int type_arg, int block_size_arg) TEST_EQUAL( type & PSA_KEY_TYPE_CATEGORY_MASK, PSA_KEY_TYPE_CATEGORY_SYMMETRIC ); - TEST_EQUAL( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ), block_size ); /* PSA_ROUND_UP_TO_MULTIPLE(block_size, length) in crypto_sizes.h From 113d80c219deabc10d67309a5e831c9db7f12e3f Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 14 Jul 2021 15:16:09 +0100 Subject: [PATCH 5/6] Simplify the test and description Previously the check was convoluted. This has been simplified and given a more appropriate suggestion as per gilles suggestion Signed-off-by: Joe Subbiani --- .../test_suite_psa_crypto_metadata.function | 24 +++---------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto_metadata.function b/tests/suites/test_suite_psa_crypto_metadata.function index f2dcfca2dd..1db3ca6e3b 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.function +++ b/tests/suites/test_suite_psa_crypto_metadata.function @@ -630,27 +630,9 @@ void block_cipher_key_type( int type_arg, int block_size_arg ) PSA_KEY_TYPE_CATEGORY_SYMMETRIC ); TEST_EQUAL( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ), block_size ); - /* PSA_ROUND_UP_TO_MULTIPLE(block_size, length) in crypto_sizes.h - * Requires block sizes to be a power of 2. - * The following creates a bit and shifts along until it finds a - * match or a mismatch. - */ - int check = 0; - - for (size_t index = 1; index > 0; index = index << 1) - { - if (index == block_size) - { - check = 0; - break; - } - if (index > block_size) - { - check = 1; - break; - } - } - TEST_EQUAL( check, 0); + /* Check that the block size is a power of 2. This is required, at least, + for PSA_ROUND_UP_TO_MULTIPLE(block_size, length) in crypto_sizes.h. */ + TEST_ASSERT( ( ( block_size - 1 ) & block_size ) == 0 ); } /* END_CASE */ From 5ccdc556b1d40dd1deb9a688bf6aaffe0a621656 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Thu, 15 Jul 2021 09:03:51 +0100 Subject: [PATCH 6/6] Remove trailing whitespace Signed-off-by: Joe Subbiani --- tests/suites/test_suite_psa_crypto_metadata.function | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/suites/test_suite_psa_crypto_metadata.function b/tests/suites/test_suite_psa_crypto_metadata.function index 1db3ca6e3b..9f4fc75495 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.function +++ b/tests/suites/test_suite_psa_crypto_metadata.function @@ -630,7 +630,7 @@ void block_cipher_key_type( int type_arg, int block_size_arg ) PSA_KEY_TYPE_CATEGORY_SYMMETRIC ); TEST_EQUAL( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ), block_size ); - /* Check that the block size is a power of 2. This is required, at least, + /* Check that the block size is a power of 2. This is required, at least, for PSA_ROUND_UP_TO_MULTIPLE(block_size, length) in crypto_sizes.h. */ TEST_ASSERT( ( ( block_size - 1 ) & block_size ) == 0 ); }