From a02003babe4e5145d37f47f42a2565b33e8534ea Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Wed, 7 Jul 2021 17:20:06 +0100 Subject: [PATCH] Fix divide by zero if macro used with wrong key If PSA_CIPHER_ENCRYPT_OUTPUT_SIZE was called on a non symmetric key, then a divide by zero could happen, as PSA_CIPHER_BLOCK_LENGTH will return 0 for such a key, and PSA_ROUND_UP_TO_MULTIPLE will divide by the block length. Signed-off-by: Paul Elliott --- include/psa/crypto_sizes.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/psa/crypto_sizes.h b/include/psa/crypto_sizes.h index 2811c237af..836f9ee147 100644 --- a/include/psa/crypto_sizes.h +++ b/include/psa/crypto_sizes.h @@ -992,9 +992,11 @@ */ #define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ (alg == PSA_ALG_CBC_PKCS7 ? \ + (((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) \ + == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \ PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ (input_length) + 1) + \ - PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \ + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \ (PSA_ALG_IS_CIPHER(alg) ? \ (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \ 0))