Botan 3.7.1
Crypto and TLS for C&
|
#include <ct_utils.h>
Public Member Functions | |
constexpr void | _const_time_poison () const |
constexpr void | _const_time_unpoison () const |
constexpr bool | as_bool () const |
constexpr CT::Choice | as_choice () const |
template<typename U > requires (sizeof(U) <= sizeof(T)) | |
void | conditional_swap (U &x, U &y) const |
constexpr T | if_not_set_return (T x) const |
constexpr T | if_set_return (T x) const |
constexpr void | if_set_zero_out (T buf[], size_t elems) |
Mask (const Mask< T > &other)=default | |
template<typename U > | |
constexpr | Mask (Mask< U > o) |
Mask< T > & | operator&= (Mask< T > o) |
Mask< T > & | operator= (const Mask< T > &other)=default |
Mask< T > & | operator^= (Mask< T > o) |
Mask< T > & | operator|= (Mask< T > o) |
constexpr Mask< T > | operator~ () const |
constexpr T | select (T x, T y) const |
constexpr T | select_and_unpoison (T x, T y) const |
Mask< T > | select_mask (Mask< T > x, Mask< T > y) const |
constexpr void | select_n (T output[], const T x[], const T y[], size_t len) const |
constexpr T | unpoisoned_value () const |
constexpr T | value () const |
Static Public Member Functions | |
static constexpr Mask< T > | cleared () |
template<typename U > | |
static constexpr Mask< T > | expand (Mask< U > m) |
static constexpr Mask< T > | expand (T v) |
static constexpr Mask< T > | expand_bit (T v, size_t bit) |
static constexpr Mask< T > | expand_top_bit (T v) |
static constexpr Mask< T > | from_choice (Choice c) |
static constexpr Mask< T > | is_any_of (T v, std::initializer_list< T > accepted) |
static constexpr Mask< T > | is_equal (T x, T y) |
static constexpr Mask< T > | is_gt (T x, T y) |
static constexpr Mask< T > | is_gte (T x, T y) |
static constexpr Mask< T > | is_lt (T x, T y) |
static constexpr Mask< T > | is_lte (T x, T y) |
static constexpr Mask< T > | is_within_range (T v, T l, T u) |
static constexpr Mask< T > | is_zero (T x) |
static constexpr Mask< T > | set () |
Friends | |
Mask< T > | operator& (Mask< T > x, Mask< T > y) |
Mask< T > | operator^ (Mask< T > x, Mask< T > y) |
Mask< T > | operator| (Mask< T > x, Mask< T > y) |
A Mask type used for constant-time operations. A Mask<T> always has value either |0| (all bits cleared) or |1| (all bits set). All operations in a Mask<T> are intended to compile to code which does not contain conditional jumps. This must be verified with tooling (eg binary disassembly or using valgrind) since you never know what a compiler might do.
Definition at line 379 of file ct_utils.h.
|
default |
Referenced by Botan::CT::Mask< T >::cleared(), Botan::CT::Mask< T >::conditional_swap(), Botan::CT::Mask< T >::expand_top_bit(), Botan::CT::Mask< T >::from_choice(), Botan::CT::Mask< T >::is_zero(), Botan::CT::Mask< T >::operator~(), Botan::CT::Mask< T >::select_mask(), and Botan::CT::Mask< T >::set().
|
inlineconstexpr |
|
inlineconstexpr |
Definition at line 637 of file ct_utils.h.
References Botan::CT::poison().
|
inlineconstexpr |
Definition at line 639 of file ct_utils.h.
References Botan::CT::unpoison().
|
inlineconstexpr |
Unsafe conversion to bool
This conversion itself is (probably) constant time, but once the mask is reduced to a simple bool, it's entirely possible for the compiler to perform range analysis on the values, since there are just the two. As a consequence even if the caller is not using this in an obviously branchy way (if(mask.as_bool()) ...
) a smart compiler may introduce branches depending on the value.
Definition at line 625 of file ct_utils.h.
References Botan::CT::Mask< T >::unpoisoned_value().
|
inlineconstexpr |
Return a Choice based on this mask
Definition at line 630 of file ct_utils.h.
References Botan::CT::Choice::from_int(), and Botan::CT::Mask< T >::unpoisoned_value().
Referenced by Botan::oaep_find_delim().
|
inlinestaticconstexpr |
Return a Mask<T> of |0| (all bits cleared)
Definition at line 403 of file ct_utils.h.
References Botan::CT::Mask< T >::Mask().
Referenced by Botan::Classic_McEliece_PrivateKeyInternal::check_key(), Botan::low_zero_bits(), Botan::oaep_find_delim(), Botan::OneAndZeros_Padding::unpad(), and Botan::x448().
|
inline |
If this mask is set, swap x and y
Definition at line 596 of file ct_utils.h.
References Botan::CT::Mask< T >::Mask().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if m is set
Definition at line 440 of file ct_utils.h.
References T, and Botan::CT::Mask< T >::value().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if v is != 0
Definition at line 408 of file ct_utils.h.
References Botan::CT::value_barrier().
Referenced by Botan::bigint_cnd_abs(), Botan::bigint_cnd_add(), Botan::bigint_cnd_sub(), Botan::bigint_cnd_swap(), Botan::bigint_ct_is_lt(), Botan::bigint_shl1(), Botan::bigint_shl2(), Botan::bigint_shr1(), Botan::bigint_shr2(), Botan::Scalar448::bytes_are_reduced(), Botan::Classic_McEliece_PrivateKeyInternal::check_key(), Botan::BigInt::cond_flip_sign(), Botan::CT::conditional_assign_mem(), Botan::CT::conditional_copy_mem(), Botan::CT::conditional_swap(), Botan::constant_time_compare(), Botan::Classic_McEliece_Field_Ordering::create_from_control_bits(), Botan::BigInt::ct_cond_assign(), Botan::Ed448Point::decode(), Botan::low_zero_bits(), Botan::bitvector_base< AllocatorT >::bitref< BlockT >::operator=(), Botan::Ed448Point::operator==(), Botan::bitvector_base< AllocatorT >::bitref< BlockT >::operator^=(), Botan::bitvector_base< AllocatorT >::bitref< BlockT >::operator|=(), Botan::Classic_McEliece_Field_Ordering::permute_with_pivots(), Botan::Sodium::sodium_is_zero(), Botan::ANSI_X923_Padding::unpad(), Botan::OneAndZeros_Padding::unpad(), and Botan::x448().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if the given bit
of v
is set. bit
must be from 0 (LSB) to (sizeof(T) * 8 - 1) (MSB).
Definition at line 432 of file ct_utils.h.
References Botan::CT::Mask< T >::expand_top_bit().
Referenced by Botan::FrodoMatrix::sample().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if the top bit of v is set
Definition at line 426 of file ct_utils.h.
References Botan::expand_top_bit(), Botan::CT::Mask< T >::Mask(), and Botan::CT::value_barrier().
Referenced by Botan::ct_divide_word(), Botan::ct_mod_word(), Botan::CT::Mask< T >::expand_bit(), Botan::CT::Mask< T >::is_any_of(), Botan::CT::Mask< T >::is_lt(), and Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::poly_cadd_q().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if choice is set
Definition at line 413 of file ct_utils.h.
References Botan::CT::Mask< T >::Mask(), T, and Botan::CT::Choice::value().
Referenced by Botan::CT::conditional_assign_mem(), Botan::CT::copy_output(), Botan::bitvector_base< AllocatorT >::ct_conditional_xor(), Botan::PK_Ops::Decryption_with_EME::decrypt(), and Botan::CT::Option< T >::value_or().
|
inlineconstexpr |
Return x if the mask is cleared, or otherwise zero
Definition at line 554 of file ct_utils.h.
Referenced by Botan::CT::Mask< T >::if_set_zero_out().
|
inlineconstexpr |
Return x if the mask is set, or otherwise zero
Definition at line 549 of file ct_utils.h.
References Botan::CT::Mask< T >::value().
|
inlineconstexpr |
If this mask is set, zero out buf, otherwise do nothing
Definition at line 586 of file ct_utils.h.
References Botan::CT::Mask< T >::if_not_set_return().
|
inlinestaticconstexpr |
Definition at line 490 of file ct_utils.h.
References Botan::CT::Mask< T >::expand_top_bit(), T, and Botan::CT::value_barrier().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if x == y
Definition at line 453 of file ct_utils.h.
References Botan::CT::Mask< T >::is_zero(), T, and Botan::CT::value_barrier().
Referenced by Botan::bigint_cmp(), Botan::bigint_ct_is_lt(), Botan::Classic_McEliece_PrivateKeyInternal::check_key(), Botan::TLS::check_tls_cbc_padding(), Botan::constant_time_compare(), Botan::CT::copy_output(), Botan::BigInt::ct_cond_assign(), Botan::PK_Decryptor::decrypt_or_random(), Botan::EC_Point_Base_Point_Precompute::mul(), Botan::oaep_find_delim(), Botan::Classic_McEliece_Field_Ordering::permute_with_pivots(), Botan::Sodium::sodium_compare(), Botan::ESP_Padding::unpad(), Botan::OneAndZeros_Padding::unpad(), Botan::PKCS7_Padding::unpad(), and Botan::vartime_divide().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if x > y
Definition at line 469 of file ct_utils.h.
References Botan::CT::Mask< T >::is_lt().
Referenced by Botan::ANSI_X923_Padding::unpad(), Botan::ESP_Padding::unpad(), and Botan::PKCS7_Padding::unpad().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if x >= y
Definition at line 479 of file ct_utils.h.
Referenced by Botan::ct_divide_word(), Botan::ct_mod_word(), Botan::ANSI_X923_Padding::unpad(), and Botan::PKCS7_Padding::unpad().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if x < y
Definition at line 461 of file ct_utils.h.
References Botan::CT::Mask< T >::expand_top_bit(), and T.
Referenced by Botan::bigint_cmp(), Botan::bigint_ct_is_lt(), Botan::TLS::check_tls_cbc_padding(), Botan::CT::Mask< T >::is_gt(), Botan::donna128::operator+=(), Botan::donna128::operator+=(), Botan::FrodoMatrix::sample(), and Botan::Sodium::sodium_compare().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if x <= y
Definition at line 474 of file ct_utils.h.
Referenced by Botan::TLS::check_tls_cbc_padding(), Botan::constant_time_compare(), and Botan::CT::copy_output().
|
inlinestaticconstexpr |
Definition at line 481 of file ct_utils.h.
References T, and Botan::CT::value_barrier().
|
inlinestaticconstexpr |
Return a Mask<T> which is set if v is == 0 or cleared otherwise
Definition at line 448 of file ct_utils.h.
References Botan::ct_is_zero(), Botan::CT::Mask< T >::Mask(), and Botan::CT::value_barrier().
Referenced by Botan::CT::all_zeros(), Botan::bigint_cmp(), Botan::bigint_ct_is_eq(), Botan::bigint_ct_is_lt(), Botan::BigInt::ct_reduce_below(), Botan::CT::is_equal(), Botan::CT::Mask< T >::is_equal(), Botan::oaep_find_delim(), Botan::ESP_Padding::unpad(), and Botan::OneAndZeros_Padding::unpad().
AND-combine two masks
Definition at line 505 of file ct_utils.h.
References Botan::CT::Mask< T >::value().
|
default |
XOR-combine two masks
Definition at line 513 of file ct_utils.h.
References Botan::CT::Mask< T >::value().
OR-combine two masks
Definition at line 521 of file ct_utils.h.
References Botan::CT::Mask< T >::value().
|
inlineconstexpr |
Negate this mask
Definition at line 544 of file ct_utils.h.
References Botan::CT::Mask< T >::Mask(), and Botan::CT::Mask< T >::value().
If this mask is set, return x, otherwise return y
Definition at line 559 of file ct_utils.h.
References Botan::choose(), and Botan::CT::Mask< T >::value().
Referenced by Botan::bigint_cnd_add_or_sub(), Botan::Classic_McEliece_Decryptor::raw_kem_decrypt(), Botan::CT::Mask< T >::select_and_unpoison(), and Botan::CT::Mask< T >::select_mask().
|
inlineconstexpr |
Definition at line 561 of file ct_utils.h.
References Botan::CT::Mask< T >::select(), T, and Botan::CT::unpoison().
|
inline |
If this mask is set, return x, otherwise return y
Definition at line 570 of file ct_utils.h.
References Botan::CT::Mask< T >::Mask(), Botan::CT::Mask< T >::select(), and Botan::CT::Mask< T >::value().
|
inlineconstexpr |
Conditionally set output to x or y, depending on if mask is set or cleared (resp)
Definition at line 576 of file ct_utils.h.
References Botan::choose(), T, and Botan::CT::Mask< T >::value().
Referenced by Botan::bigint_cnd_add_or_sub(), Botan::CT::conditional_copy_mem(), and Botan::Classic_McEliece_Decryptor::raw_kem_decrypt().
|
inlinestaticconstexpr |
Return a Mask<T> of |1| (all bits set)
Definition at line 398 of file ct_utils.h.
References Botan::CT::Mask< T >::Mask(), and T.
Referenced by Botan::oaep_find_delim().
|
inlineconstexpr |
Return the value of the mask, unpoisoned
Definition at line 609 of file ct_utils.h.
References T, Botan::CT::unpoison(), and Botan::CT::Mask< T >::value().
Referenced by Botan::CT::Mask< T >::as_bool(), and Botan::CT::Mask< T >::as_choice().
|
inlineconstexpr |
Return the underlying value of the mask
Definition at line 635 of file ct_utils.h.
References Botan::CT::value_barrier().
Referenced by Botan::CT::Mask< T >::expand(), Botan::CT::Mask< T >::if_set_return(), Botan::CT::Mask< T >::operator&=(), Botan::CT::Mask< T >::operator^=(), Botan::CT::Mask< T >::operator|=(), Botan::CT::Mask< T >::operator~(), Botan::CT::Mask< T >::select(), Botan::CT::Mask< T >::select_mask(), Botan::CT::Mask< T >::select_n(), and Botan::CT::Mask< T >::unpoisoned_value().