14#ifndef BOTAN_CT_UTILS_H_
15#define BOTAN_CT_UTILS_H_
17#include <botan/assert.h>
18#include <botan/range_concepts.h>
19#include <botan/secmem.h>
20#include <botan/internal/bit_ops.h>
21#include <botan/internal/scoped_cleanup.h>
22#include <botan/internal/target_info.h>
23#include <botan/internal/value_barrier.h>
29#if defined(BOTAN_HAS_VALGRIND)
30 #include <valgrind/memcheck.h>
56constexpr inline void poison(
const T* p,
size_t n) {
57#if defined(BOTAN_HAS_VALGRIND)
58 if(!std::is_constant_evaluated()) {
59 VALGRIND_MAKE_MEM_UNDEFINED(p, n *
sizeof(T));
67constexpr inline void unpoison(
const T* p,
size_t n) {
68#if defined(BOTAN_HAS_VALGRIND)
69 if(!std::is_constant_evaluated()) {
70 VALGRIND_MAKE_MEM_DEFINED(p, n *
sizeof(T));
86#if defined(BOTAN_HAS_VALGRIND)
87 return RUNNING_ON_VALGRIND;
106template <std::
integral T>
111template <std::
integral T>
119template <ranges::spanable_range R>
120 requires std::is_trivially_copyable_v<std::ranges::range_value_t<R>> && (!custom_poisonable<R>)
122 const std::span s{r};
123 poison(s.data(), s.size());
126template <ranges::spanable_range R>
127 requires std::is_trivially_copyable_v<std::ranges::range_value_t<R>> && (!custom_unpoisonable<R>)
129 const std::span s{r};
137template <custom_poisonable T>
139 x._const_time_poison();
142template <custom_unpoisonable T>
144 x._const_time_unpoison();
152constexpr void poison(
const std::optional<T>& x) {
160constexpr void unpoison(
const std::optional<T>& x) {
179template <std::ranges::range R>
182 for(
const auto& v : r) {
187template <std::ranges::range R>
188 requires unpoisonable<std::ranges::range_value_t<R>>
190 for(
const auto& v : r) {
199template <poisonable... Ts>
200 requires(
sizeof...(Ts) > 0)
205template <unpoisonable... Ts>
206 requires(
sizeof...(Ts) > 0)
220template <
typename... Ts>
221 requires(
sizeof...(Ts) > 0) && (poisonable<Ts> && ...) && (unpoisonable<Ts> && ...)
231template <poisonable T>
233 requires(std::is_rvalue_reference_v<
decltype(v)>)
236 return std::forward<T>(v);
242template <unpoisonable T>
244 requires(std::is_rvalue_reference_v<
decltype(v)>)
247 return std::forward<T>(v);
266 template <
typename T>
267 requires std::unsigned_integral<T> && (!std::same_as<bool, T>)
289 template <
typename T>
290 requires std::unsigned_integral<T> && (!std::same_as<bool, T>)
294 return static_cast<T
>(
value());
329 constexpr bool as_bool()
const {
return m_value != 0; }
362 static_assert(std::is_unsigned_v<T> && !std::is_same_v<bool, T>,
363 "Only unsigned integer types are supported by CT::Mask");
374 template <
typename U>
376 static_assert(
sizeof(U) >
sizeof(T),
"sizes ok");
428 template <
typename U>
430 static_assert(
sizeof(U) <
sizeof(T),
"sizes ok");
451 T u = x ^ ((x ^ y) | ((x - y) ^ x));
473 const T v_lt_l = v ^ ((v ^ l) | ((v - l) ^ v));
474 const T v_gt_u = u ^ ((u ^ v) | ((u - v) ^ u));
482 for(
auto a : accepted) {
483 const T diff = a ^ v;
565 constexpr void select_n(T output[],
const T x[],
const T y[],
size_t len)
const {
566 const T mask =
value();
567 for(
size_t i = 0; i != len; ++i) {
568 output[i] =
choose(mask, x[i], y[i]);
576 for(
size_t i = 0; i != elems; ++i) {
584 template <
typename U>
586 requires(
sizeof(U) <=
sizeof(T))
589 U t0 = cnd.select(y, x);
590 U t1 = cnd.select(x, y);
637 constexpr explicit Mask(T m) : m_mask(m) {}
653 constexpr Option(T v,
Choice valid) : m_has_value(valid), m_value(std::move(v)) {}
660 requires std::default_initializable<T>
673 template <std::invocable<const T&> F>
675 return {f(m_value), m_has_value};
691 other.conditional_assign(m_has_value, m_value);
700 requires std::unsigned_integral<T>
703 return mask.select(m_value, other);
711 if(m_has_value.as_bool()) {
733 mask.
select_n(dest, if_set, if_unset, elems);
751 mask.select_n(dest, src, dest, elems);
763 mask.select_n(dest, src, dest, elems);
770 swap.conditional_swap(x, y);
775 uintptr_t xp =
reinterpret_cast<uintptr_t
>(x);
776 uintptr_t yp =
reinterpret_cast<uintptr_t
>(y);
780 x =
reinterpret_cast<T
>(xp);
781 y =
reinterpret_cast<T
>(yp);
787 for(
size_t i = 0; i != len; ++i) {
799 if(std::is_constant_evaluated()) {
802 for(
size_t i = 0; i != len; ++i) {
803 difference = difference | (x[i] ^ y[i]);
808 volatile T difference = 0;
810 for(
size_t i = 0; i != len; ++i) {
811 difference = difference | (x[i] ^ y[i]);
826 if(x.size() != y.size()) {
830 return is_equal(x.data(), y.data(), x.size());
863 std::span<uint8_t> output,
864 std::span<const uint8_t> input,
#define BOTAN_STATE_CHECK(expr)
constexpr Choice operator==(const Choice &other) const
static constexpr Choice yes()
static constexpr Choice from_int(T v)
constexpr Choice & operator=(const Choice &other) noexcept=default
constexpr Choice(const Choice &other)=default
static constexpr Choice from_mask(underlying_type v)
constexpr Choice operator!=(const Choice &other) const
constexpr Choice operator||(const Choice &other) const
constexpr Choice operator!() const
static constexpr Choice no()
constexpr T into_bitmask() const
constexpr bool as_bool() const
constexpr Choice(Choice &&other)=default
constexpr ~Choice()=default
constexpr Choice operator&&(const Choice &other) const
constexpr Choice & operator=(Choice &&other) noexcept=default
constexpr underlying_type value() const
Return the masked value.
static constexpr Mask< T > expand_bit(T v, size_t bit)
Mask< T > select_mask(Mask< T > x, Mask< T > y) const
void conditional_swap(U &x, U &y) const
constexpr T value() const
constexpr void if_set_zero_out(T buf[], size_t elems)
static constexpr Mask< T > is_lte(T x, T y)
static constexpr Mask< T > is_gte(T x, T y)
constexpr T if_not_set_return(T x) const
constexpr Mask(Mask< U > o)
static constexpr Mask< T > set()
friend Mask< T > operator|(Mask< T > x, Mask< T > y)
Mask< T > & operator^=(Mask< T > o)
Mask(Mask< T > &&other)=default
constexpr T unpoisoned_value() const
constexpr void select_n(T output[], const T x[], const T y[], size_t len) const
friend Mask< T > operator^(Mask< T > x, Mask< T > y)
constexpr T if_set_return(T x) const
Mask< T > & operator=(const Mask< T > &other)=default
Mask< T > & operator=(Mask< T > &&other)=default
constexpr void _const_time_poison() const
Mask(const Mask< T > &other)=default
static constexpr Mask< T > expand(Mask< U > m)
Mask< T > & operator&=(Mask< T > o)
constexpr T select_and_unpoison(T x, T y) const
static constexpr Mask< T > expand(T v)
constexpr T select(T x, T y) const
static constexpr Mask< T > expand_top_bit(T v)
static constexpr Mask< T > is_within_range(T v, T l, T u)
constexpr CT::Choice as_choice() const
static constexpr Mask< T > from_choice(Choice c)
static constexpr Mask< T > is_equal(T x, T y)
Mask< T > & operator|=(Mask< T > o)
constexpr Mask< T > operator~() const
static constexpr Mask< T > expand_bool(bool v)
static constexpr Mask< T > is_gt(T x, T y)
constexpr bool as_bool() const
static constexpr Mask< T > is_lt(T x, T y)
friend Mask< T > operator&(Mask< T > x, Mask< T > y)
constexpr void _const_time_unpoison() const
static constexpr Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
static constexpr Mask< T > is_zero(T x)
static constexpr Mask< T > cleared()
constexpr Option(T v)
Construct a set option with the provided value.
constexpr Option(T v, Choice valid)
Construct an Option which contains the specified value, and is set or not.
constexpr auto transform(F f) const -> Option< std::remove_cvref_t< std::invoke_result_t< F, const T & > > >
constexpr Choice has_value() const
Return true if this Option contains a value.
constexpr CT::Option< T > operator&&(CT::Choice also)
Return a new CT::Option that is set if also is set as well.
constexpr const T & value() const
Either returns the value or throws an exception.
constexpr Option()
Construct an unset option with a default inner value.
constexpr std::optional< T > as_optional_vartime() const
constexpr T value_or(T other) const
constexpr T value_or(T other) const
Helper class to create a RAII-style cleanup callback.
secure_vector< uint8_t > strip_leading_zeros(std::span< const uint8_t > input)
constexpr void conditional_swap_ptr(bool cnd, T &x, T &y)
decltype(auto) driveby_unpoison(T &&v)
constexpr void conditional_swap(bool cnd, T &x, T &y)
constexpr void poison_range(const R &r)
constexpr void poison_all(const Ts &... ts)
constexpr T value_barrier(T x)
decltype(auto) driveby_poison(T &&v)
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *dest, const T *if_set, const T *if_unset, size_t elems)
constexpr auto scoped_poison(const Ts &... xs)
constexpr CT::Mask< T > is_not_equal(const T x[], const T y[], size_t len)
constexpr void unpoison_all(const Ts &... ts)
BOTAN_TEST_API CT::Option< size_t > copy_output(CT::Choice accept, std::span< uint8_t > output, std::span< const uint8_t > input, size_t offset)
size_t count_leading_zero_bytes(std::span< const uint8_t > input)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
constexpr void unpoison_range(const R &r)
constexpr void unpoison(const T *p, size_t n)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
constexpr Mask< T > conditional_assign_mem(T cnd, T *dest, const T *src, size_t elems)
constexpr void poison(const T *p, size_t n)
BOTAN_FORCE_INLINE constexpr T choose(T mask, T a, T b)
std::vector< T, secure_allocator< T > > secure_vector
BOTAN_FORCE_INLINE constexpr T ct_expand_top_bit(T a)
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
BOTAN_FORCE_INLINE constexpr T ct_is_zero(T x)