diff options
Diffstat (limited to 'include/internal/constant_time.h')
-rw-r--r-- | include/internal/constant_time.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/include/internal/constant_time.h b/include/internal/constant_time.h index 0ed6f823c1..9ffa4399a3 100644 --- a/include/internal/constant_time.h +++ b/include/internal/constant_time.h @@ -140,6 +140,47 @@ static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b) return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b))); } +#ifdef BN_ULONG +static ossl_inline BN_ULONG value_barrier_bn(BN_ULONG a) +{ +#if !defined(OPENSSL_NO_ASM) && defined(__GNUC__) + BN_ULONG r; + __asm__("" : "=r"(r) : "0"(a)); +#else + volatile BN_ULONG r = a; +#endif + return r; +} + +static ossl_inline BN_ULONG constant_time_msb_bn(BN_ULONG a) +{ + return 0 - (a >> (sizeof(a) * 8 - 1)); +} + +static ossl_inline BN_ULONG constant_time_lt_bn(BN_ULONG a, BN_ULONG b) +{ + return constant_time_msb_bn(a ^ ((a ^ b) | ((a - b) ^ b))); +} + +static ossl_inline BN_ULONG constant_time_is_zero_bn(BN_ULONG a) +{ + return constant_time_msb_bn(~a & (a - 1)); +} + +static ossl_inline BN_ULONG constant_time_eq_bn(BN_ULONG a, + BN_ULONG b) +{ + return constant_time_is_zero_bn(a ^ b); +} + +static ossl_inline BN_ULONG constant_time_select_bn(BN_ULONG mask, + BN_ULONG a, + BN_ULONG b) +{ + return (value_barrier_bn(mask) & a) | (value_barrier_bn(~mask) & b); +} +#endif + static ossl_inline unsigned int constant_time_ge(unsigned int a, unsigned int b) { |