Reduce side channels from single-bit reads

Co-authored-by: Tim Ruffing <crypto@timruffing.de>
This commit is contained in:
Peter Dettman
2021-12-28 18:25:04 -05:00
committed by Pieter Wuille
parent a0d32b597d
commit 07810d9abb
2 changed files with 37 additions and 2 deletions

View File

@@ -394,4 +394,16 @@ SECP256K1_INLINE static void secp256k1_write_be64(unsigned char* p, uint64_t x)
p[0] = x >> 56;
}
/* Rotate a uint32_t to the right. */
SECP256K1_INLINE static uint32_t secp256k1_rotr32(const uint32_t x, const unsigned int by) {
#if defined(_MSC_VER)
return _rotr(x, by); /* needs <stdlib.h> */
#else
/* Reduce rotation amount to avoid UB when shifting. */
const unsigned int mask = CHAR_BIT * sizeof(x) - 1;
/* Turned into a rot instruction by GCC and clang. */
return (x >> (by & mask)) | (x << ((-by) & mask));
#endif
}
#endif /* SECP256K1_UTIL_H */