14#ifndef BOTAN_CT_UTILS_H_
15#define BOTAN_CT_UTILS_H_
17#include <botan/concepts.h>
18#include <botan/secmem.h>
19#include <botan/internal/bit_ops.h>
20#include <botan/internal/stl_util.h>
21#include <botan/internal/target_info.h>
27#if defined(BOTAN_HAS_VALGRIND)
28 #include <valgrind/memcheck.h>
54constexpr inline void poison(
const T* p,
size_t n) {
55#if defined(BOTAN_HAS_VALGRIND)
56 if(!std::is_constant_evaluated()) {
57 VALGRIND_MAKE_MEM_UNDEFINED(p, n *
sizeof(T));
65constexpr inline void unpoison(
const T* p,
size_t n) {
66#if defined(BOTAN_HAS_VALGRIND)
67 if(!std::is_constant_evaluated()) {
68 VALGRIND_MAKE_MEM_DEFINED(p, n *
sizeof(T));
84#if defined(BOTAN_HAS_VALGRIND)
85 return RUNNING_ON_VALGRIND;
99template <std::
integral T>
104template <std::
integral T>
112template <ranges::spanable_range R>
113 requires std::is_trivially_copyable_v<std::ranges::range_value_t<R>>
116 poison(s.data(), s.size());
119template <ranges::spanable_range R>
120 requires std::is_trivially_copyable_v<std::ranges::range_value_t<R>>
131 requires requires(
const T& x) { x._const_time_poison(); }
133 x._const_time_poison();
137 requires requires(
const T& x) { x._const_time_unpoison(); }
139 x._const_time_unpoison();
147constexpr void poison(
const std::optional<T>& x) {
155constexpr void unpoison(
const std::optional<T>& x) {
174template <std::ranges::range R>
177 for(
const auto& v : r) {
182template <std::ranges::range R>
183 requires unpoisonable<std::ranges::range_value_t<R>>
185 for(
const auto& v : r) {
194template <poisonable... Ts>
195 requires(
sizeof...(Ts) > 0)
200template <unpoisonable... Ts>
201 requires(
sizeof...(Ts) > 0)
215template <
typename... Ts>
216 requires(
sizeof...(Ts) > 0) && (poisonable<Ts> && ...) && (unpoisonable<Ts> && ...)
226template <poisonable T>
228 requires(std::is_rvalue_reference_v<
decltype(v)>)
231 return std::forward<T>(v);
237template <unpoisonable T>
239 requires(std::is_rvalue_reference_v<
decltype(v)>)
242 return std::forward<T>(v);
274 requires std::unsigned_integral<T> && (!std::same_as<bool, T>)
276 if(std::is_constant_evaluated()) {
279#if defined(BOTAN_CT_VALUE_BARRIER_USE_ASM)
287 asm(
"" :
"+r"(x) : );
289#elif defined(BOTAN_CT_VALUE_BARRIER_USE_VOLATILE)
310 template <
typename T>
311 requires std::unsigned_integral<T> && (!std::same_as<bool, T>)
350 constexpr bool as_bool()
const {
return m_value != 0; }
361 constexpr explicit Choice(uint32_t v) : m_value(v) {}
382 static_assert(std::is_unsigned<T>::value && !std::is_same<bool, T>::value,
383 "Only unsigned integer types are supported by CT::Mask");
391 template <
typename U>
393 static_assert(
sizeof(U) >
sizeof(T),
"sizes ok");
415 if constexpr(
sizeof(T) <=
sizeof(uint32_t)) {
440 template <
typename U>
442 static_assert(
sizeof(U) <
sizeof(T),
"sizes ok");
463 T u = x ^ ((x ^ y) | ((x - y) ^ x));
485 const T v_lt_l = v ^ ((v ^ l) | ((v - l) ^ v));
486 const T v_gt_u = u ^ ((u ^ v) | ((u - v) ^ u));
494 for(
auto a : accepted) {
495 const T diff = a ^ v;
577 constexpr void select_n(T output[],
const T x[],
const T y[],
size_t len)
const {
578 const T mask =
value();
579 for(
size_t i = 0; i != len; ++i) {
580 output[i] =
choose(mask, x[i], y[i]);
588 for(
size_t i = 0; i != elems; ++i) {
596 template <
typename U>
598 requires(
sizeof(U) <=
sizeof(T))
601 U t0 = cnd.select(y, x);
602 U t1 = cnd.select(x, y);
643 constexpr Mask(T m) : m_mask(m) {}
659 constexpr Option(T v,
Choice valid) : m_has_value(valid), m_value(std::move(v)) {}
666 requires std::default_initializable<T>
679 template <std::invocable<const T&> F>
681 return {f(m_value), m_has_value};
697 other.conditional_assign(m_has_value, m_value);
706 requires std::unsigned_integral<T>
709 return mask.select(m_value, other);
717 if(m_has_value.as_bool()) {
734 mask.
select_n(to, from0, from1, elems);
747 mask.select_n(sink, src, sink, elems);
754 mask.select_n(sink, src, sink, elems);
761 swap.conditional_swap(x, y);
766 uintptr_t xp =
reinterpret_cast<uintptr_t
>(x);
767 uintptr_t yp =
reinterpret_cast<uintptr_t
>(y);
771 x =
reinterpret_cast<T
>(xp);
772 y =
reinterpret_cast<T
>(yp);
778 for(
size_t i = 0; i != len; ++i) {
790 if(std::is_constant_evaluated()) {
793 for(
size_t i = 0; i != len; ++i) {
794 difference = difference | (x[i] ^ y[i]);
799 volatile T difference = 0;
801 for(
size_t i = 0; i != len; ++i) {
802 difference = difference | (x[i] ^ y[i]);
839 std::span<uint8_t> output,
840 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
constexpr Choice operator!=(const Choice &other) const
constexpr Choice operator||(const Choice &other) const
constexpr Choice operator!() const
static constexpr Choice no()
constexpr bool as_bool() const
constexpr Choice(Choice &&other)=default
constexpr Choice operator&&(const Choice &other) const
constexpr uint32_t value() const
Return the masked value.
constexpr Choice & operator=(Choice &&other) noexcept=default
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)
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
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 > 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)
decltype(auto) driveby_poison(T &&v)
constexpr void unpoison_all(Ts &&... ts)
constexpr Mask< T > conditional_assign_mem(T cnd, T *sink, const T *src, size_t elems)
constexpr auto scoped_poison(const Ts &... xs)
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *to, const T *from0, const T *from1, size_t elems)
constexpr void unpoison_range(R &&r)
constexpr void poison_range(R &&r)
constexpr CT::Mask< T > is_not_equal(const T x[], const T y[], size_t len)
constexpr void poison_all(Ts &&... ts)
constexpr T value_barrier(T x)
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(const T *p, size_t n)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
constexpr void poison(const T *p, size_t n)
constexpr T choose(T mask, T a, T b)
std::vector< T, secure_allocator< T > > secure_vector
constexpr T ct_is_zero(T x)
constexpr T expand_top_bit(T a)