diff --git a/include/psa/crypto.h b/include/psa/crypto.h index 48c971351..99c4b523d 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -803,6 +803,21 @@ typedef uint32_t psa_algorithm_t; (((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \ ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK)) +/** Macro to build the base MAC algorithm corresponding to a truncated + * MAC algorithm. + * + * \param alg A MAC algorithm identifier (value of type + * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg) + * is true). This may be a truncated or untruncated + * MAC algorithm. + * + * \return The corresponding base MAC algorithm. + * \return Unspecified if \p alg is not a supported + * MAC algorithm. + */ +#define PSA_ALG_FULL_LENGTH_MAC(alg) \ + ((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) + /** Length to which a MAC algorithm is truncated. * * \param alg A MAC algorithm identifier (value of type diff --git a/library/psa_crypto.c b/library/psa_crypto.c index ab9ec725e..6b01c13f0 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1527,7 +1527,7 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, psa_key_usage_t usage = is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY; unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg ); - psa_algorithm_t full_length_alg = alg & ~PSA_ALG_MAC_TRUNCATION_MASK; + psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg ); status = psa_mac_init( operation, full_length_alg ); if( status != PSA_SUCCESS ) diff --git a/tests/suites/test_suite_psa_crypto_metadata.function b/tests/suites/test_suite_psa_crypto_metadata.function index 9cb68b9fa..215110a32 100644 --- a/tests/suites/test_suite_psa_crypto_metadata.function +++ b/tests/suites/test_suite_psa_crypto_metadata.function @@ -198,6 +198,7 @@ void mac_algorithm( int alg_arg, int classification_flags, mac_algorithm_core( alg, classification_flags, key_type, key_bits, length ); + TEST_ASSERT( PSA_ALG_FULL_LENGTH_MAC( alg ) == alg ); TEST_ASSERT( length <= PSA_MAC_MAX_SIZE ); /* Truncated versions */ @@ -206,6 +207,7 @@ void mac_algorithm( int alg_arg, int classification_flags, psa_algorithm_t truncated_alg = PSA_ALG_TRUNCATED_MAC( alg, n ); mac_algorithm_core( truncated_alg, classification_flags, key_type, key_bits, n ); + TEST_ASSERT( PSA_ALG_FULL_LENGTH_MAC( truncated_alg ) == alg ); /* Check that calling PSA_ALG_TRUNCATED_MAC twice gives the length * of the outer truncation (even if the outer length is smaller than * the inner length). */