Botan: Botan::CT Namespace Reference

Botan 1.10.17

Functions
Botan::CT Namespace Reference

## Functions
template<typename T >
void | cond_zero_mem (T cond, T *array, size_t elems)
template<typename T >
void | conditional_copy_mem (T value, T *to, const T *from0, const T *from1, size_t elems)
template<typename T >
T | expand_mask (T x)
template<typename T >
T | expand_top_bit (T a)
template<typename T >
T | is_equal (T x, T y)
template<typename T >
T | is_less (T x, T y)
template<typename T >
T | is_lte (T x, T y)
template<typename T >
T | is_zero (T x)
template<typename T >
T | max (T a, T b)
template<typename T >
T | min (T a, T b)
template<typename T >
T | select (T mask, T from0, T from1)
template<typename PredT , typename ValT >
ValT | val_or_zero (PredT pred_val, ValT val)

## Function Documentation

## ◆ cond_zero_mem()

template<typename T >

void Botan::CT::cond_zero_mem | ( | T | cond,
|  | T * | array,
|  | size_t | elems
| ) |  |
| inline

Definition at line 100 of file ct_utils.h.

References expand_mask(), and select().

103 {
104 const T mask = CT::expand_mask(cond);
105 const T zero(0);
106
107 for(size_t i = 0; i != elems; ++i)
108 {
109 array[i] = CT::select(mask, zero, array[i]);
110 }
111 }
Botan::CT::expand_mask
T expand_mask(T x)
Definition: ct_utils.h:33
Botan::CT::select
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45

## ◆ conditional_copy_mem()

template<typename T >

void Botan::CT::conditional_copy_mem | ( | T | value,
|  | T * | to,
|  | const T * | from0,
|  | const T * | from1,
|  | size_t | elems
| ) |  |
| inline

Definition at line 85 of file ct_utils.h.

References expand_mask(), and select().

90 {
91 const T mask = CT::expand_mask(value);
92
93 for(size_t i = 0; i != elems; ++i)
94 {
95 to[i] = CT::select(mask, from0[i], from1[i]);
96 }
97 }
Botan::CT::expand_mask
T expand_mask(T x)
Definition: ct_utils.h:33
Botan::CT::select
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45

## ◆ expand_mask()

template<typename T >

T Botan::CT::expand_mask | ( | T | x | ) |
| inline

Definition at line 33 of file ct_utils.h.

Referenced by Botan::bigint_cnd_abs(), Botan::bigint_cnd_add(), Botan::bigint_cnd_sub(), Botan::bigint_cnd_swap(), cond_zero_mem(), conditional_copy_mem(), and is_zero().

34 {
35 T r = x;
36 // First fold r down to a single bit
37 for(size_t i = 1; i != sizeof(T)*8; i *= 2)
38 r |= r >> i;
39 r &= 1;
40 r = ~(r - 1);
41 return r;
42 }

## ◆ expand_top_bit()

template<typename T >

T Botan::CT::expand_top_bit | ( | T | a | ) |
| inline

Definition at line 114 of file ct_utils.h.

Referenced by max(), and min().

115 {
116 return expand_mask<T>(a >> (sizeof(T)*8-1));
117 }

## ◆ is_equal()

template<typename T >

T Botan::CT::is_equal | ( | T | x,
|  | T | y
| ) |  |
| inline

Definition at line 63 of file ct_utils.h.

References is_zero().

Referenced by Botan::BigInt::const_time_lookup().

64 {
65 return is_zero(x ^ y);
66 }
Botan::CT::is_zero
T is_zero(T x)
Definition: ct_utils.h:57

## ◆ is_less()

template<typename T >

T Botan::CT::is_less | ( | T | x,
|  | T | y
| ) |  |
| inline

Definition at line 69 of file ct_utils.h.

70 {
71 /*
72 This expands to a constant time sequence with GCC 5.2.0 on x86-64
73 but something more complicated may be needed for portable const time.
74 */
75 return expand_mask<T>(x < y);
76 }

## ◆ is_lte()

template<typename T >

T Botan::CT::is_lte | ( | T | x,
|  | T | y
| ) |  |
| inline

Definition at line 79 of file ct_utils.h.

80 {
81 return expand_mask<T>(x <= y);
82 }

## ◆ is_zero()

template<typename T >

T Botan::CT::is_zero | ( | T | x | ) |
| inline

Definition at line 57 of file ct_utils.h.

References expand_mask().

Referenced by is_equal(), Botan::BigInt::is_nonzero(), and Botan::PointGFp::negate().

58 {
59 return ~expand_mask(x);
60 }
Botan::CT::expand_mask
T expand_mask(T x)
Definition: ct_utils.h:33

## ◆ max()

template<typename T >

T Botan::CT::max | ( | T | a,
|  | T | b
| ) |  |
| inline

Definition at line 120 of file ct_utils.h.

References expand_top_bit(), and select().

Referenced by Botan::OpenPGP_S2K::derive_key(), Botan::dl_work_factor(), Botan::multi_exponentiate(), Botan::operator+(), Botan::BigInt::operator+=(), Botan::operator-(), Botan::BigInt::operator-=(), Botan::operator^(), and Botan::Device_EntropySource::poll().

121 {
122 const T a_larger = b - a; // negative if a is larger
123 return select(expand_top_bit(a), a, b);
124 }
Botan::CT::expand_top_bit
T expand_top_bit(T a)
Definition: ct_utils.h:114
Botan::CT::select
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45

## ◆ min()

template<typename T >

T Botan::CT::min | ( | T | a,
|  | T | b
| ) |  |
| inline

Definition at line 127 of file ct_utils.h.

References expand_top_bit(), and select().

Referenced by Botan::X942_PRF::derive(), Botan::KDF2::derive(), Botan::gcd(), Botan::MemoryRegion< word >::operator<(), Botan::OctetString::operator^=(), Botan::DataSource_Memory::peek(), and Botan::Buffered_Filter::write().

128 {
129 const T a_larger = b - a; // negative if a is larger
130 return select(expand_top_bit(b), b, a);
131 }
Botan::CT::expand_top_bit
T expand_top_bit(T a)
Definition: ct_utils.h:114
Botan::CT::select
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45

## ◆ select()

template<typename T >

T Botan::CT::select | ( | T | mask,
|  | T | from0,
|  | T | from1
| ) |  |
| inline

Definition at line 45 of file ct_utils.h.

Referenced by Botan::bigint_cnd_abs(), Botan::bigint_cnd_add(), Botan::bigint_cnd_sub(), Botan::bigint_cnd_swap(), cond_zero_mem(), conditional_copy_mem(), max(), min(), Botan::Device_EntropySource::poll(), Botan::DataSource_Command::read(), and val_or_zero().

46 {
47 return (from0 & mask) | (from1 & ~mask);
48 }

## ◆ val_or_zero()

template<typename PredT , typename ValT >

ValT Botan::CT::val_or_zero | ( | PredT | pred_val,
|  | ValT | val
| ) |  |
| inline

Definition at line 51 of file ct_utils.h.

References select().

52 {
53 return select(CT::expand_mask<ValT>(pred_val), val, static_cast<ValT>(0));
54 }
Botan::CT::select
T select(T mask, T from0, T from1)
Definition: ct_utils.h:45