mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2026-09-21 17:47:24 +00:00
ecp: fix implementation-defined behaviour
The comment about optimizing compiler's likely behaviour has been checked with GCC and Clang on x86-64, 32-bit Arm and 64-bit Arm. Signed-off-by: Manuel Pégourié-Gonnard <[email protected]>
This commit is contained in:
+10
-1
@@ -5018,7 +5018,16 @@ int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn)
|
||||
|
||||
static inline int8_t extract_carry(int64_t cur)
|
||||
{
|
||||
return (int8_t) (cur >> 32);
|
||||
/*
|
||||
* Extract the signed carry without relying on `cur >> 32` for negative `cur`,
|
||||
* since signed right shift is implementation-defined. Shift as uint64_t, then
|
||||
* sign-extend explicitly.
|
||||
*
|
||||
* Modern optimizing compilers will likely generate the same code
|
||||
* as the less portable (int8_t) (cur >> 32) anyway.
|
||||
*/
|
||||
uint32_t hi = (uint32_t) (((uint64_t) cur) >> 32);
|
||||
return (int8_t) ((int64_t) hi - ((int64_t) (hi >> 31) << 32));
|
||||
}
|
||||
|
||||
#define ADD(j) cur += A(j)
|
||||
|
||||
Reference in New Issue
Block a user