Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions | Friends | List of all members
Botan::CT::Mask< T > Class Template Reference

`#include <ct_utils.h>`

## Public Member Functions
T | if_not_set_return (T x) const
T | if_set_return (T x) const
void | if_set_zero_out (T buf[], size_t elems)
bool | is_set () const
| Mask (const Mask< T > &other)=default
template<typename U >
| Mask (Mask< U > o)
Mask< T > & | operator&= (Mask< T > o)
Mask< T > & | operator= (const Mask< T > &other)=default
Mask< T > & | operator^= (Mask< T > o)
Mask< T > & | operator|= (Mask< T > o)
Mask< T > | operator~ () const
T | select (T x, T y) const
T | select_and_unpoison (T x, T y) const
Mask< T > | select_mask (Mask< T > x, Mask< T > y) const
void | select_n (T output[], const T x[], const T y[], size_t len) const
T | unpoisoned_value () const
T | value () const

## Static Public Member Functions
static Mask< T > | cleared ()
static Mask< T > | expand (T v)
template<typename U >
static Mask< T > | expand (Mask< U > m)
static Mask< T > | is_equal (T x, T y)
static Mask< T > | is_gt (T x, T y)
static Mask< T > | is_gte (T x, T y)
static Mask< T > | is_lt (T x, T y)
static Mask< T > | is_lte (T x, T y)
static Mask< T > | is_zero (T x)
static Mask< T > | set ()

## Friends
Mask< T > | operator& (Mask< T > x, Mask< T > y)
Mask< T > | operator^ (Mask< T > x, Mask< T > y)
Mask< T > | operator| (Mask< T > x, Mask< T > y)

## Detailed Description

### template<typename T>
class Botan::CT::Mask< T >

A Mask type used for constant-time operations. A Mask<T> always has value either 0 (all bits cleared) or ~0 (all bits set). All operations in a Mask<T> are intended to compile to code which does not contain conditional jumps. This must be verified with tooling (eg binary disassembly or using valgrind) since you never know what a compiler might do.

Definition at line 87 of file ct_utils.h.

## Constructor & Destructor Documentation

template<typename T>

Botan::CT::Mask< T >::Mask | ( | const Mask< T > & | other | ) |
| default

template<typename T>
template<typename U >

Botan::CT::Mask< T >::Mask | ( | Mask< U > | o | ) |
| inline

Derive a Mask from a Mask of a larger type

Definition at line 99 of file ct_utils.h.

References T.

99 : m_mask(static_cast<T>(o.value()))
100 {
101 static_assert(sizeof(U) > sizeof(T), "sizes ok");
102 }
T
fe T
Definition: ge.cpp:37

## Member Function Documentation

template<typename T>

static Mask<T> Botan::CT::Mask< T >::cleared | ( |  | ) |
| inline static

Return a Mask<T> with all bits cleared

Definition at line 115 of file ct_utils.h.

Referenced by Botan::oaep_find_delim(), Botan::EME_PKCS1v15::unpad(), and Botan::OneAndZeros_Padding::unpad().

116 {
117 return Mask<T>(0);
118 }
template<typename T>

static Mask<T> Botan::CT::Mask< T >::expand | ( | T | v | ) |
| inline static

Return a Mask<T> which is set if v is != 0

Definition at line 123 of file ct_utils.h.

Referenced by Botan::bigint_cnd_abs(), Botan::bigint_cnd_add(), Botan::bigint_cnd_sub(), Botan::bigint_cnd_swap(), Botan::bigint_ct_is_lt(), Botan::bigint_shl1(), Botan::bigint_shl2(), Botan::bigint_shr1(), Botan::bigint_shr2(), Botan::BigInt::cond_flip_sign(), Botan::CT::conditional_copy_mem(), Botan::CT::conditional_swap(), Botan::BigInt::ct_cond_assign(), Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption::finish(), Botan::gcd(), Botan::redc_p521(), Botan::Sodium::sodium_is_zero(), Botan::ANSI_X923_Padding::unpad(), and Botan::OneAndZeros_Padding::unpad().

124 {
125 return ~Mask<T>::is_zero(v);
126 }
template<typename T>
template<typename U >

static Mask<T> Botan::CT::Mask< T >::expand | ( | Mask< U > | m | ) |
| inline static

Return a Mask<T> which is set if m is set

Definition at line 132 of file ct_utils.h.

References T, and Botan::CT::Mask< T >::value().

133 {
134 static_assert(sizeof(U) < sizeof(T), "sizes ok");
135 return ~Mask<T>::is_zero(m.value());
136 }
T
fe T
Definition: ge.cpp:37
template<typename T>

T Botan::CT::Mask< T >::if_not_set_return | ( | T | x | ) | const
| inline

Return x if the mask is cleared, or otherwise zero

Definition at line 256 of file ct_utils.h.

Referenced by Botan::CT::Mask< T >::if_set_zero_out(), and Botan::EME_PKCS1v15::unpad().

257 {
258 return ~m_mask & x;
259 }
template<typename T>

T Botan::CT::Mask< T >::if_set_return | ( | T | x | ) | const
| inline

Return x if the mask is set, or otherwise zero

Definition at line 248 of file ct_utils.h.

Referenced by Botan::oaep_find_delim().

249 {
250 return m_mask & x;
251 }
template<typename T>

void Botan::CT::Mask< T >::if_set_zero_out | ( | T | buf[],
|  | size_t | elems
| ) |  |
| inline

If this mask is set, zero out buf, otherwise do nothing

Definition at line 298 of file ct_utils.h.

References Botan::CT::Mask< T >::if_not_set_return().

Referenced by Botan::CT::copy_output().

299 {
300 for(size_t i = 0; i != elems; ++i)
301 {
302 buf[i] = this->if_not_set_return(buf[i]);
303 }
304 }
Botan::CT::Mask::if_not_set_return
T if_not_set_return(T x) const
Definition: ct_utils.h:256
template<typename T>

static Mask<T> Botan::CT::Mask< T >::is_equal | ( | T | x,
|  | T | y
| ) |  |
| inline static

Return a Mask<T> which is set if x == y

Definition at line 149 of file ct_utils.h.

References Botan::CT::Mask< T >::is_zero().

Referenced by Botan::bigint_cmp(), Botan::bigint_ct_is_lt(), Botan::TLS::check_tls_cbc_padding(), Botan::BigInt::const_time_lookup(), Botan::CT::copy_output(), Botan::PK_Decryptor::decrypt_or_random(), Botan::divide(), Botan::PointGFp_Base_Point_Precompute::mul(), Botan::PointGFp_Var_Point_Precompute::mul(), Botan::oaep_find_delim(), Botan::redc_p521(), Botan::Sodium::sodium_compare(), Botan::PKCS7_Padding::unpad(), Botan::OneAndZeros_Padding::unpad(), and Botan::ESP_Padding::unpad().

150 {
151 return Mask<T>::is_zero(static_cast<T>(x ^ y));
152 }
Botan::CT::Mask::is_zero
static Mask< T > is_zero(T x)
Definition: ct_utils.h:141
template<typename T>

static Mask<T> Botan::CT::Mask< T >::is_gt | ( | T | x,
|  | T | y
| ) |  |
| inline static

Return a Mask<T> which is set if x > y

Definition at line 165 of file ct_utils.h.

References Botan::CT::Mask< T >::is_lt().

Referenced by Botan::PKCS7_Padding::unpad(), Botan::ANSI_X923_Padding::unpad(), and Botan::ESP_Padding::unpad().

166 {
167 return Mask<T>::is_lt(y, x);
168 }
Botan::CT::Mask::is_lt
static Mask< T > is_lt(T x, T y)
Definition: ct_utils.h:157
template<typename T>

static Mask<T> Botan::CT::Mask< T >::is_gte | ( | T | x,
|  | T | y
| ) |  |
| inline static

Return a Mask<T> which is set if x >= y

Definition at line 181 of file ct_utils.h.

Referenced by Botan::ct_divide_u8(), Botan::PKCS7_Padding::unpad(), and Botan::ANSI_X923_Padding::unpad().

182 {
183 return ~Mask<T>::is_lt(x, y);
184 }
template<typename T>

static Mask<T> Botan::CT::Mask< T >::is_lt | ( | T | x,
|  | T | y
| ) |  |
| inline static

Return a Mask<T> which is set if x < y

Definition at line 157 of file ct_utils.h.

Referenced by Botan::bigint_cmp(), Botan::bigint_ct_is_lt(), Botan::TLS::check_tls_cbc_padding(), Botan::CT::Mask< T >::is_gt(), Botan::Sodium::sodium_compare(), and Botan::EME_PKCS1v15::unpad().

158 {
159 return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160 }
template<typename T>

static Mask<T> Botan::CT::Mask< T >::is_lte | ( | T | x,
|  | T | y
| ) |  |
| inline static

Return a Mask<T> which is set if x <= y

Definition at line 173 of file ct_utils.h.

Referenced by Botan::TLS::check_tls_cbc_padding(), Botan::CT::copy_output(), and Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption::finish().

174 {
175 return ~Mask<T>::is_gt(x, y);
176 }
template<typename T>

bool Botan::CT::Mask< T >::is_set | ( |  | ) | const
| inline

Return true iff this mask is set

Definition at line 319 of file ct_utils.h.

References Botan::CT::Mask< T >::unpoisoned_value().

320 {
321 return unpoisoned_value() != 0;
322 }
Botan::CT::Mask::unpoisoned_value
T unpoisoned_value() const
Definition: ct_utils.h:309
template<typename T>

static Mask<T> Botan::CT::Mask< T >::is_zero | ( | T | x | ) |
| inline static

Return a Mask<T> which is set if v is == 0 or cleared otherwise

Definition at line 141 of file ct_utils.h.

Referenced by Botan::bigint_cmp(), Botan::bigint_ct_is_eq(), Botan::bigint_ct_is_lt(), Botan::ct_compare_u8(), Botan::BigInt::ct_reduce_below(), Botan::PK_Decryptor::decrypt_or_random(), Botan::CT::Mask< T >::is_equal(), Botan::oaep_find_delim(), Botan::CT::strip_leading_zeros(), Botan::EME_PKCS1v15::unpad(), Botan::OneAndZeros_Padding::unpad(), and Botan::ESP_Padding::unpad().

142 {
143 return Mask<T>(ct_is_zero<T>(x));
144 }
template<typename T>

Mask<T>& Botan::CT::Mask< T >::operator&= | ( | Mask< T > | o | ) |
| inline

AND-combine two masks

Definition at line 189 of file ct_utils.h.

References Botan::CT::Mask< T >::value().

190 {
191 m_mask &= o.value();
192 return (*this);
193 }
template<typename T>

Mask<T>& Botan::CT::Mask< T >::operator= | ( | const Mask< T > & | other | ) |
| default

template<typename T>

Mask<T>& Botan::CT::Mask< T >::operator^= | ( | Mask< T > | o | ) |
| inline

XOR-combine two masks

Definition at line 198 of file ct_utils.h.

References Botan::CT::Mask< T >::value().

199 {
200 m_mask ^= o.value();
201 return (*this);
202 }
template<typename T>

Mask<T>& Botan::CT::Mask< T >::operator|= | ( | Mask< T > | o | ) |
| inline

OR-combine two masks

Definition at line 207 of file ct_utils.h.

References Botan::CT::Mask< T >::value().

208 {
209 m_mask |= o.value();
210 return (*this);
211 }
template<typename T>

Mask<T> Botan::CT::Mask< T >::operator~ | ( |  | ) | const
| inline

Negate this mask

Definition at line 240 of file ct_utils.h.

References Botan::CT::Mask< T >::value().

241 {
242 return Mask<T>(~value());
243 }
Botan::CT::Mask::value
T value() const
Definition: ct_utils.h:327
template<typename T>

T Botan::CT::Mask< T >::select | ( | T | x,
|  | T | y
| ) |  | const
| inline

If this mask is set, return x, otherwise return y

Definition at line 264 of file ct_utils.h.

References T, and Botan::CT::Mask< T >::value().

Referenced by Botan::bigint_cnd_add_or_sub(), Botan::bigint_cnd_addsub(), Botan::CT::Mask< T >::select_and_unpoison(), Botan::CT::Mask< T >::select_mask(), and Botan::CT::Mask< T >::select_n().

265 {
266 // (x & value()) | (y & ~value())
267 return static_cast<T>(y ^ (value() & (x ^ y)));
268 }
Botan::CT::Mask::value
T value() const
Definition: ct_utils.h:327
T
fe T
Definition: ge.cpp:37
template<typename T>

T Botan::CT::Mask< T >::select_and_unpoison | ( | T | x,
|  | T | y
| ) |  | const
| inline

Definition at line 270 of file ct_utils.h.

References Botan::CT::Mask< T >::select(), T, and Botan::CT::unpoison().

271 {
272 T r = this->select(x, y);
273 CT::unpoison(r);
274 return r;
275 }
Botan::CT::Mask::select
T select(T x, T y) const
Definition: ct_utils.h:264
T
fe T
Definition: ge.cpp:37
Botan::CT::unpoison
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:59
template<typename T>

Mask<T> Botan::CT::Mask< T >::select_mask | ( | Mask< T > | x,
|  | Mask< T > | y
| ) |  | const
| inline

If this mask is set, return x, otherwise return y

Definition at line 280 of file ct_utils.h.

References Botan::CT::Mask< T >::select(), and Botan::CT::Mask< T >::value().

281 {
282 return Mask<T>(select(x.value(), y.value()));
283 }
Botan::CT::Mask::select
T select(T x, T y) const
Definition: ct_utils.h:264
template<typename T>

void Botan::CT::Mask< T >::select_n | ( | T | output[],
|  | const T | x[],
|  | const T | y[],
|  | size_t | len
| ) |  | const
| inline

Conditionally set output to x or y, depending on if mask is set or cleared (resp)

Definition at line 289 of file ct_utils.h.

References Botan::CT::Mask< T >::select().

290 {
291 for(size_t i = 0; i != len; ++i)
292 output[i] = this->select(x[i], y[i]);
293 }
Botan::CT::Mask::select
T select(T x, T y) const
Definition: ct_utils.h:264
template<typename T>

static Mask<T> Botan::CT::Mask< T >::set | ( |  | ) |
| inline static

Return a Mask<T> with all bits set

Definition at line 107 of file ct_utils.h.

References T.

Referenced by Botan::oaep_find_delim(), and Botan::CT::strip_leading_zeros().

108 {
109 return Mask<T>(static_cast<T>(~0));
110 }
T
fe T
Definition: ge.cpp:37
template<typename T>

T Botan::CT::Mask< T >::unpoisoned_value | ( |  | ) | const
| inline

Return the value of the mask, unpoisoned

Definition at line 309 of file ct_utils.h.

References T, Botan::CT::unpoison(), and Botan::CT::Mask< T >::value().

Referenced by Botan::CT::Mask< T >::is_set().

310 {
311 T r = value();
312 CT::unpoison(r);
313 return r;
314 }
Botan::CT::Mask::value
T value() const
Definition: ct_utils.h:327
T
fe T
Definition: ge.cpp:37
Botan::CT::unpoison
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:59
template<typename T>

T Botan::CT::Mask< T >::value | ( |  | ) | const
| inline

Return the underlying value of the mask

Definition at line 327 of file ct_utils.h.

Referenced by Botan::CT::Mask< T >::expand(), Botan::CT::Mask< T >::operator&=(), Botan::CT::Mask< T >::operator^=(), Botan::CT::Mask< T >::operator|=(), Botan::CT::Mask< T >::operator~(), Botan::CT::Mask< T >::select(), Botan::CT::Mask< T >::select_mask(), and Botan::CT::Mask< T >::unpoisoned_value().

328 {
329 return m_mask;
330 }

## Friends And Related Function Documentation

template<typename T>

Mask<T> operator& | ( | Mask< T > | x,
|  | Mask< T > | y
| ) |  |
| friend

AND-combine two masks

Definition at line 216 of file ct_utils.h.

217 {
218 return Mask<T>(x.value() & y.value());
219 }
template<typename T>

Mask<T> operator^ | ( | Mask< T > | x,
|  | Mask< T > | y
| ) |  |
| friend

XOR-combine two masks

Definition at line 224 of file ct_utils.h.

225 {
226 return Mask<T>(x.value() ^ y.value());
227 }
template<typename T>

Mask<T> operator| | ( | Mask< T > | x,
|  | Mask< T > | y
| ) |  |
| friend

OR-combine two masks

Definition at line 232 of file ct_utils.h.

233 {
234 return Mask<T>(x.value() | y.value());
235 }

The documentation for this class was generated from the following file:

- src/lib/utils/ct_utils.h