crypto: ecc - Fix carry overflow in vli multiplication
Subsystems (3)
PATCH v2 1/1 crypto: ecc - Fix carry overflow in vli multiplication
The carry flag calculation fails when r01.m_high is saturated (0xFFFFFFFFFFFFFFFF) and addition of lower bits overflows.
The condition (r01.m_high < product.m_high) doesn't handle the case where r01.m_high == product.m_high and an additional carry exists from lower-bit overflow.
When commit 3c4b23901a0c ("crypto: ecdh - Add ECDH software support") introduced crypto/ecc.c, it split the muladd() function in the micro-ecc library into separate mul_64_64() and add_128_128() helpers. It seems the check got lost in translation.
Add proper handling for this boundary by accounting for the carry from the lower addition.
---
Changes v1 -> v2:
* Rename add_128_128() to check_add_128_128_overflow() and let it return a bool indicating whether an overflow occurred
* Rewrite an explicit if-else statement using constant-time bitwise arithmetic to avoid a timing side-channel
Link to v1:
https://lore.kernel.org/r/[email protected]/
--- crypto/ecc.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-)
crypto/ecc.c +20 -11
@@ -393,14 +393,26 @@ static uint128_t mul_64_64(u64 left, u64 right)return result; } -static uint128_t add_128_128 (uint128_t a,uint128_t b) +/* Calculate addition with overflow checking. Returns true on wrap-around, +* false otherwise. +*/ +static bool check_add_128_128_overflow (uint128_t *result,uint128_t a, +uint128_t b) { -uint128_t result; +bool carry; - result.m_low = a.m_low + b.m_low; - result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low); + result->m_low = a.m_low + b.m_low; + carry = (result->m_low < a.m_low); -return result; + result->m_high = a.m_high + b.m_high + carry; + +/* Using constant-time bitwise arithmetic to prevent timing +* side-channels. +*/ + carry = (result->m_high < a.m_high) | + ((result->m_high == a.m_high) & carry); + +return carry; } static void vli_mult(u64 *result, const u64 *left, const u64 *right,
@@ -425,9 +437,7 @@ static void vli_mult(u64 *result, const u64 *left, const u64 *right,uint128_t product; product =mul_64_64 (left[i], right[k - i]); - - r01 =add_128_128 (r01, product); - r2 += (r01.m_high < product.m_high); + r2 +=check_add_128_128_overflow (&r01, r01, product); } result[k] = r01.m_low;
@@ -450,7 +460,7 @@ static void vli_umult(u64 *result, const u64 *left, u32 right,uint128_t product; product =mul_64_64 (left[k], right); - r01 =add_128_128 (r01, product); +check_add_128_128_overflow (&r01, r01, product);/* no carry */ result[k] = r01.m_low; r01.m_low = r01.m_high;
@@ -487,8 +497,7 @@ static void vli_square(u64 *result, const u64 *left, unsigned int ndigits) product.m_low <<=1 ; } - r01 =add_128_128 (r01, product); - r2 += (r01.m_high < product.m_high); + r2 +=check_add_128_128_overflow (&r01, r01, product); } result[k] = r01.m_low;
Signature · 1 line
2.43.0
Quoted 15 lines · Anastasia Tishchenko
crypto/ecc.c +5
-1@@ -393,14 +393,26 @@ static uint128_t mul_64_64(u64 left, u64 right)return result; } -static uint128_t add_128_128 (uint128_t a,uint128_t b) +/* Calculate addition with overflow checking. Returns true on wrap-around, +* false otherwise. +*/ +static bool check_add_128_128_overflow (uint128_t *result,uint128_t a, +uint128_t b) {
crypto/ecc.c static uint128_t mul_64_64(u64 left, u64 right) · line 407
Quoted 10 lines
crypto/ecc.c +4
-4-uint128_t result; +bool carry; - result.m_low = a.m_low + b.m_low; - result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low); + result->m_low = a.m_low + b.m_low; + carry = (result->m_low < a.m_low); -return result; + result->m_high = a.m_high + b.m_high + carry;
If CONFIG_ARCH_SUPPORTS_INT128 is defined, you can convert them to "unsigned __int128" as done in mul_64_64(), and use check_add_overflow() to get the carry.
crypto/ecc.c static uint128_t mul_64_64(u64 left, u64 right) · line 417
Quoted 10 lines
crypto/ecc.c +8
-0+ +/* Using constant-time bitwise arithmetic to prevent timing +* side-channels. +*/ + carry = (result->m_high < a.m_high) | + ((result->m_high == a.m_high) & carry); + +return carry; }
Regards,
Qingfang
Lukas Wunner
maintainerQuoted 21 lines · Qingfang Deng
Quoted 20 lines · Anastasia Tishchenko
+++
b/crypto/ecc.ccrypto/ecc.c +8
-4@@ -393,14 +393,26 @@ static uint128_t mul_64_64(u64 left, u64 right)return result; } -static uint128_t add_128_128 (uint128_t a,uint128_t b) +/* Calculate addition with overflow checking. Returns true on wrap-around, +* false otherwise. +*/ +static bool check_add_128_128_overflow (uint128_t *result,uint128_t a, +uint128_t b) { -uint128_t result; +bool carry; - result.m_low = a.m_low + b.m_low; - result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low); + result->m_low = a.m_low + b.m_low; + carry = (result->m_low < a.m_low);
Quoted 6 lines
crypto/ecc.c static uint128_t mul_64_64(u64 left, u64 right) · line 407
Quoted 2 lines
crypto/ecc.c +1
-1-return result; + result->m_high = a.m_high + b.m_high + carry;If
CONFIG_ARCH_SUPPORTS_INT128is defined, you can convert them to "unsigned__int128" as done inmul_64_64(), and usecheck_add_overflow()to get the carry.
Okay, but that would be a separate feature on top of this fix. This fix applies to *all* architectures whereas using native 128-bit integers would only solve the problem on arches which support such integers.
ARCH_SUPPORTS_INT128 actually depends on CC_HAS_INT128 on all arches that support it (arm64, loongarch, riscv, s390, x86). Interestingly, s390 additionally requires CC_IS_CLANG. Commit fbac266f095d ("s390:
select ARCH_SUPPORTS_INT128") explains that "gcc generates inefficient code, which may lead to stack overflows" when handling 128-bit integers. It goes on to say: "The gcc generated functions have 6kb stack frames, compared to only 1kb of the code generated with clang."
That reminds me of the high stack usage issue we're seeing when compiling crypto/ecc.c on arm 32-bit:
https://lore.kernel.org/r/7e3d64a53efb28740b32d1f934e78c10086208ab.1778073318.git.lukas@wunner.de/
I'm fine with adding native 128-bit usage to check_add_128_128_overflow() on top of this fix if somebody wants to submit a patch. But I suspect it might only actually be useful on s390 with clang.
Thanks,
Lukas
Quoted 12 lines · Anastasia Tishchenko
The carry flag calculation fails when r01.m_high is saturated (
0xFFFFFFFFFFFFFFFF) and addition of lower bits overflows.The condition (r01.m_high < product.m_high) doesn't handle the case where r01.m_high == product.m_high and an additional carry exists from lower-bit overflow.
When commit
3c4b23901a0c("crypto: ecdh - Add ECDH software support") introducedcrypto/ecc.c, it split themuladd()function in the micro-ecc library into separatemul_64_64()andadd_128_128()helpers. It seems the check got lost in translation.
Quoted 6 lines
Add proper handling for this boundary by accounting for the carry from the lower addition.
Fixes 3c4b23901a0c ("crypto: ecdh - Add ECDH software support")Signed-off-by Anastasia Tishchenko <[email protected]>Cc [email protected] # v4.8+
Quoted 24 lines · On Wed, 13 May 2026 20:39:48 +0800 Qingfang Deng <[email protected]>…
Quoted 23 lines · Anastasia Tishchenko
crypto/ecc.c +8
-4@@ -393,14 +393,26 @@ static uint128_t mul_64_64(u64 left, u64 right)return result; } -static uint128_t add_128_128 (uint128_t a,uint128_t b) +/* Calculate addition with overflow checking. Returns true on wrap-around, +* false otherwise. +*/ +static bool check_add_128_128_overflow (uint128_t *result,uint128_t a, +uint128_t b) { -uint128_t result; +bool carry; - result.m_low = a.m_low + b.m_low; - result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low); + result->m_low = a.m_low + b.m_low; + carry = (result->m_low < a.m_low);
Quoted 6 lines
crypto/ecc.c static uint128_t mul_64_64(u64 left, u64 right) · line 407
Quoted 2 lines
crypto/ecc.c +1
-1-return result; + result->m_high = a.m_high + b.m_high + carry;If
CONFIG_ARCH_SUPPORTS_INT128is defined, you can convert them to "unsigned__int128" as done inmul_64_64(), and usecheck_add_overflow()to get the carry.
Can you guarantee the compiler generates 'constant time' code for any of this?
If you care then relying on compiler support for anything that might generate a conditional jump isn't a good idea.
Just writing 'bitwise' arithmetic doesn't mean the compiler won't use branches.
Even if you don't get one today, someone else might get one tomorrow. IIRC even on x86 'x += (a < b)' can generate a branch rather than the obvious 'cmp a, b; adc $0, x', or the longer cmov or setc sequences.
You pretty much have to use asm for anything that isn't trivial arithmetic.
-- David
Quoted 15 lines
crypto/ecc.c static uint128_t mul_64_64(u64 left, u64 right) · line 417
Quoted 11 lines
crypto/ecc.c +8
-0+ +/* Using constant-time bitwise arithmetic to prevent timing +* side-channels. +*/ + carry = (result->m_high < a.m_high) | + ((result->m_high == a.m_high) & carry); + +return carry; }Regards,
Qingfang
Herbert Xu
maintainerQuoted 25 lines · Anastasia Tishchenko
The carry flag calculation fails when r01.m_high is saturated (
0xFFFFFFFFFFFFFFFF) and addition of lower bits overflows.The condition (r01.m_high < product.m_high) doesn't handle the case where r01.m_high == product.m_high and an additional carry exists from lower-bit overflow.
When commit
3c4b23901a0c("crypto: ecdh - Add ECDH software support") introducedcrypto/ecc.c, it split themuladd()function in the micro-ecc library into separatemul_64_64()andadd_128_128()helpers. It seems the check got lost in translation.Add proper handling for this boundary by accounting for the carry from the lower addition.
Fixes 3c4b23901a0c ("crypto: ecdh - Add ECDH software support")Signed-off-by Anastasia Tishchenko <[email protected]>Cc [email protected] # v4.8+---
Changes v1 -> v2:
* Renameadd_128_128()tocheck_add_128_128_overflow()and let it return a bool indicating whether an overflow occurred
* Rewrite an explicit if-else statement using constant-time bitwise arithmetic to avoid a timing side-channel
Quoted 5 lines
Link to v1:
https://lore.kernel.org/r/[email protected]/--- crypto/ecc.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-)
Patch applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt