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>
47#if defined(BOTAN_HAS_VALGRIND)
48 VALGRIND_MAKE_MEM_UNDEFINED(p, n *
sizeof(
T));
56#if defined(BOTAN_HAS_VALGRIND)
57 VALGRIND_MAKE_MEM_DEFINED(p, n *
sizeof(
T));
65#if defined(BOTAN_HAS_VALGRIND)
66 VALGRIND_MAKE_MEM_DEFINED(&p,
sizeof(
T));
80 requires(std::is_unsigned<T>::value && !std::is_same<bool, T>::value)
91 static_assert(
sizeof(U) >
sizeof(
T),
"sizes ok");
112 template <
typename U>
114 static_assert(
sizeof(U) <
sizeof(
T),
"sizes ok");
151 const T v_lt_l = v ^ ((v ^ l) | ((v - l) ^ v));
152 const T v_gt_u = u ^ ((u ^ v) | ((u - v) ^ u));
153 const T either = v_lt_l | v_gt_u;
160 for(
auto a : accepted) {
161 const T diff = a ^ v;
162 const T eq_zero = ~diff & (diff - 1);
229 T r = this->select(x, y);
243 void select_n(
T output[],
const T x[],
const T y[],
size_t len)
const {
244 for(
size_t i = 0; i != len; ++i) {
245 output[i] = this->select(x[i], y[i]);
253 for(
size_t i = 0; i != elems; ++i) {
254 buf[i] = this->if_not_set_return(buf[i]);
270 bool is_set()
const {
return unpoisoned_value() != 0; }
278 Mask(
T m) : m_mask(m) {}
286 mask.select_n(to, from0, from1, elems);
293 mask.select_n(sink, src, sink, elems);
301 T t0 = swap.select(y, x);
302 T t1 = swap.select(x, y);
309 uintptr_t xp =
reinterpret_cast<uintptr_t
>(x);
310 uintptr_t yp =
reinterpret_cast<uintptr_t
>(y);
312 conditional_swap<uintptr_t>(cnd, xp, yp);
314 x =
reinterpret_cast<T>(xp);
315 y =
reinterpret_cast<T>(yp);
321 for(
size_t i = 0; i != len; ++i) {
341 const uint8_t input[],
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)
Mask< T > conditional_assign_mem(T cnd, T *sink, const T *src, size_t elems)
void unpoison(const T *p, size_t n)
CT::Mask< T > all_zeros(const T elem[], size_t len)
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)