56
56
#define TEST_CLASSIFICATION_MACRO( flag, alg, flags ) \
57
57
TEST_ASSERT( PSA_##flag( alg ) == !! ( ( flags ) & flag ) )
58
58
59
- /* Check the parity of value.
60
- * Return 0 if value has even parity and a nonzero value otherwise. */
59
+ /* Test the parity of value.
60
+ * Numerical encodings of key types and related values such as EC curves and
61
+ * DH groups should all have the same parity. This guarantees that a
62
+ * single-bit error will be detected.
63
+ * The expected parity is even because this makes all-bits-zero valid.
64
+ * The function returns a nonzero value if the parity is even, zero otherwise.
65
+ */
61
66
int test_parity( uint32_t value )
62
67
{
63
68
value ^= value >> 16;
64
69
value ^= value >> 8;
65
70
value ^= value >> 4;
66
71
return( 0x9669 & 1 << ( value & 0xf ) );
67
72
}
68
- #define TEST_PARITY( value ) \
69
- TEST_ASSERT( test_parity( value ) )
70
73
71
74
void algorithm_classification( psa_algorithm_t alg, unsigned flags )
72
75
{
@@ -125,7 +128,7 @@ void key_type_classification( psa_key_type_t type, unsigned flags )
125
128
( PSA_KEY_TYPE_IS_DH( type ) &&
126
129
PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) );
127
130
128
- TEST_PARITY( type );
131
+ TEST_ASSERT( test_parity( type ) );
129
132
130
133
exit: ;
131
134
}
@@ -497,7 +500,7 @@ void ecc_key_family( int curve_arg )
497
500
psa_key_type_t public_type = PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve );
498
501
psa_key_type_t pair_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve );
499
502
500
- test_parity( curve );
503
+ TEST_ASSERT( test_parity( curve ) );
501
504
502
505
test_key_type( public_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_PUBLIC_KEY );
503
506
test_key_type( pair_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_KEY_PAIR );
@@ -514,7 +517,7 @@ void dh_key_family( int group_arg )
514
517
psa_key_type_t public_type = PSA_KEY_TYPE_DH_PUBLIC_KEY( group );
515
518
psa_key_type_t pair_type = PSA_KEY_TYPE_DH_KEY_PAIR( group );
516
519
517
- test_parity( group );
520
+ TEST_ASSERT( test_parity( group ) );
518
521
519
522
test_key_type( public_type, KEY_TYPE_IS_DH | KEY_TYPE_IS_PUBLIC_KEY );
520
523
test_key_type( pair_type, KEY_TYPE_IS_DH | KEY_TYPE_IS_KEY_PAIR );
0 commit comments