From a936a3bf05d1775e2bfa62533db2e30fdb003bfb Mon Sep 17 00:00:00 2001 From: jsing Date: Tue, 14 Feb 2023 17:58:26 +0000 Subject: [PATCH] Provide bn_ct_{eq,ne}_zero{,_mask}() inline functions. These will be used to test a BN_ULONG in cases where constant time style behaviour is required. ok tb@ --- lib/libcrypto/bn/bn_internal.h | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/libcrypto/bn/bn_internal.h b/lib/libcrypto/bn/bn_internal.h index de5cd224242..003d8cac2d3 100644 --- a/lib/libcrypto/bn/bn_internal.h +++ b/lib/libcrypto/bn/bn_internal.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_internal.h,v 1.1 2023/01/31 05:48:39 jsing Exp $ */ +/* $OpenBSD: bn_internal.h,v 1.2 2023/02/14 17:58:26 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -22,6 +22,38 @@ #ifndef HEADER_BN_INTERNAL_H #define HEADER_BN_INTERNAL_H +#ifndef HAVE_BN_CT_NE_ZERO +static inline int +bn_ct_ne_zero(BN_ULONG w) +{ + return (w | ~(w - 1)) >> (BN_BITS2 - 1); +} +#endif + +#ifndef HAVE_BN_CT_NE_ZERO_MASK +static inline BN_ULONG +bn_ct_ne_zero_mask(BN_ULONG w) +{ + return 0 - bn_ct_ne_zero(w); +} +#endif + +#ifndef HAVE_BN_CT_EQ_ZERO +static inline int +bn_ct_eq_zero(BN_ULONG w) +{ + return 1 - bn_ct_ne_zero(w); +} +#endif + +#ifndef HAVE_BN_CT_EQ_ZERO_MASK +static inline BN_ULONG +bn_ct_eq_zero_mask(BN_ULONG w) +{ + return 0 - bn_ct_eq_zero(w); +} +#endif + #ifndef HAVE_BN_UMUL_HILO #ifdef BN_LLONG static inline void -- 2.20.1