crypto: ecdsa - Harden against integer overflows in DIV_ROUND_UP()
authorLukas Wunner <lukas@wunner.de>
Sun, 2 Feb 2025 19:00:52 +0000 (20:00 +0100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Sun, 9 Feb 2025 10:08:12 +0000 (18:08 +0800)
commitb16510a530d1e6ab9683f04f8fb34f2e0f538275
tree6547e4783565512c3632de5a961866ae01e43a34
parentf4144b6bb74cc358054041e7b062bc9354c59e6c
crypto: ecdsa - Harden against integer overflows in DIV_ROUND_UP()

Herbert notes that DIV_ROUND_UP() may overflow unnecessarily if an ecdsa
implementation's ->key_size() callback returns an unusually large value.
Herbert instead suggests (for a division by 8):

  X / 8 + !!(X & 7)

Based on this formula, introduce a generic DIV_ROUND_UP_POW2() macro and
use it in lieu of DIV_ROUND_UP() for ->key_size() return values.

Additionally, use the macro in ecc_digits_from_bytes(), whose "nbytes"
parameter is a ->key_size() return value in some instances, or a
user-specified ASN.1 length in the case of ecdsa_get_signature_rs().

Link: https://lore.kernel.org/r/Z3iElsILmoSu6FuC@gondor.apana.org.au/
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/ecc.c
crypto/ecdsa-p1363.c
crypto/ecdsa-x962.c
include/linux/math.h