14#ifndef BOTAN_CT_UTILS_H_
15#define BOTAN_CT_UTILS_H_
17#include <botan/secmem.h>
18#include <botan/internal/bit_ops.h>
22#if defined(BOTAN_HAS_VALGRIND)
23 #include <valgrind/memcheck.h>
50#if defined(BOTAN_HAS_VALGRIND)
51 VALGRIND_MAKE_MEM_UNDEFINED(p, n *
sizeof(
T));
60#if defined(BOTAN_HAS_VALGRIND)
61 VALGRIND_MAKE_MEM_DEFINED(p, n *
sizeof(
T));
70#if defined(BOTAN_HAS_VALGRIND)
71 VALGRIND_MAKE_MEM_DEFINED(&p,
sizeof(
T));
88 static_assert(std::is_unsigned<T>::value,
"CT::Mask only defined for unsigned integer types");
99 static_assert(
sizeof(U) >
sizeof(
T),
"sizes ok");
132 static_assert(
sizeof(U) <
sizeof(
T),
"sizes ok");
141 return Mask<T>(ct_is_zero<T>(x));
157 return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
188 const T v_lt_l =
v^((
v^l) | ((
v-l)^
v));
189 const T v_gt_u = u^((u^
v) | ((u-
v)^u));
190 const T either = v_lt_l | v_gt_u;
198 for(
auto a: accepted)
200 const T diff = a ^
v;
201 const T eq_zero = ~diff & (diff - 1);
310 void select_n(
T output[],
const T x[],
const T y[],
size_t len)
const
312 for(
size_t i = 0; i != len; ++i)
313 output[i] = this->
select(x[i], y[i]);
321 for(
size_t i = 0; i != elems; ++i)
354 Mask(
T m) : m_mask(m) {}
367 mask.select_n(to, from0, from1, elems);
376 T t0 = swap.select(y, x);
377 T t1 = swap.select(x, y);
385 uintptr_t xp =
reinterpret_cast<uintptr_t
>(x);
386 uintptr_t yp =
reinterpret_cast<uintptr_t
>(y);
388 conditional_swap<uintptr_t>(cnd, xp, yp);
390 x =
reinterpret_cast<T>(xp);
391 y =
reinterpret_cast<T>(yp);
408 const uint8_t input[],
#define BOTAN_UNUSED(...)
Mask< T > select_mask(Mask< T > x, Mask< T > y) const
void if_set_zero_out(T buf[], size_t elems)
static Mask< T > is_gt(T x, T y)
static Mask< T > is_equal(T x, T y)
static Mask< T > is_zero(T x)
friend Mask< T > operator|(Mask< T > x, Mask< T > y)
Mask< T > & operator^=(Mask< T > o)
T if_not_set_return(T x) const
friend Mask< T > operator^(Mask< T > x, Mask< T > y)
void select_n(T output[], const T x[], const T y[], size_t len) const
static Mask< T > expand(Mask< U > m)
static Mask< T > expand(T v)
T unpoisoned_value() const
T if_set_return(T x) const
Mask< T > & operator=(const Mask< T > &other)=default
Mask(const Mask< T > &other)=default
Mask< T > & operator&=(Mask< T > o)
T select_and_unpoison(T x, T y) const
Mask< T > operator~() const
static Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
Mask< T > & operator|=(Mask< T > o)
static Mask< T > is_gte(T x, T y)
static Mask< T > is_lt(T x, T y)
static Mask< T > cleared()
static Mask< T > is_within_range(T v, T l, T u)
static Mask< T > is_lte(T x, T y)
friend Mask< T > operator&(Mask< T > x, Mask< T > y)
void poison(const T *p, size_t n)
void conditional_swap_ptr(bool cnd, T &x, T &y)
void conditional_swap(bool cnd, T &x, T &y)
secure_vector< uint8_t > copy_output(CT::Mask< uint8_t > bad_input_u8, const uint8_t input[], size_t input_length, size_t offset)
void unpoison(const T *p, size_t n)
secure_vector< uint8_t > strip_leading_zeros(const uint8_t in[], size_t length)
Mask< T > conditional_copy_mem(T cnd, T *to, const T *from0, const T *from1, size_t elems)
constexpr T choose(T mask, T a, T b)
std::vector< T, secure_allocator< T > > secure_vector
constexpr T expand_top_bit(T a)