From 7e31009bdb2e75c7f22e2d0eb4a5d5a8fe454a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Sz=C3=A9pk=C3=BAti?= Date: Thu, 8 Apr 2021 12:05:18 +0200 Subject: [PATCH] Further reduce macro expansion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bence Szépkúti --- include/psa/crypto_sizes.h | 15 ++++++--------- include/psa/crypto_values.h | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/psa/crypto_sizes.h b/include/psa/crypto_sizes.h index 205bd0b55d..12bbf6e615 100644 --- a/include/psa/crypto_sizes.h +++ b/include/psa/crypto_sizes.h @@ -140,7 +140,7 @@ */ #define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \ (PSA_AEAD_NONCE_LENGTH(key_type, alg) ? \ - ((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET : \ + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ ((void) (key_bits), 0)) /** The maximum tag size for all supported AEAD algorithms, in bytes. @@ -271,7 +271,7 @@ */ #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \ (PSA_AEAD_NONCE_LENGTH(key_type, alg) ? \ - (plaintext_length) + PSA_AEAD_TAG_LENGTH(key_type, 0, alg) : \ + (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 0) /** A sufficient output buffer size for psa_aead_encrypt(), for any of the @@ -324,7 +324,7 @@ */ #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \ (PSA_AEAD_NONCE_LENGTH(key_type, alg) ? \ - (ciphertext_length) - PSA_AEAD_TAG_LENGTH(key_type, 0, alg) : \ + (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \ 0) /** A sufficient output buffer size for psa_aead_decrypt(), for any of the @@ -375,14 +375,11 @@ */ #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \ (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \ - PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0) == \ - PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0) ? 13 : \ - PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0) == \ - PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0) ? 12 : \ + PSA_ALG_AEAD_IS_BASE_EQUAL(alg, PSA_ALG_CCM) ? 13 : \ + PSA_ALG_AEAD_IS_BASE_EQUAL(alg, PSA_ALG_GCM) ? 12 : \ 0 : \ (key_type) == PSA_KEY_TYPE_CHACHA20 && \ - PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0) == \ - PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0) ? 12 : \ + PSA_ALG_AEAD_IS_BASE_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \ 0) /** The maximum default nonce size among all supported pairs of key types and diff --git a/include/psa/crypto_values.h b/include/psa/crypto_values.h index 5e865c9315..de5a3c8d3e 100644 --- a/include/psa/crypto_values.h +++ b/include/psa/crypto_values.h @@ -1175,6 +1175,20 @@ * encoded in #PSA_ALG_AEAD_TAG_LENGTH_MASK. */ #define PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ((psa_algorithm_t)0x00008000) +/** Macro to test whether two AEAD algorithms correspond to the same base algorithm. + * + * \param aead_alg_1 An AEAD algorithm identifier. + * \param aead_alg_2 An AEAD algorithm identifier. + * + * \return 1 if the base both arguments correspond to the same base + * algorithm, 0 otherwise. + * Unspecified if \p aead_alg_1 or \p aead_alg_2 are not + * supported AEAD algorithms. + */ +#define PSA_ALG_AEAD_IS_BASE_EQUAL(aead_alg_1, aead_alg_2) \ + (!(((aead_alg_1) ^ (aead_alg_2)) & \ + ~(PSA_ALG_AEAD_TAG_LENGTH_MASK | PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG))) + /** Macro to build a shortened AEAD algorithm. * * A shortened AEAD algorithm is similar to the corresponding AEAD