Botan 3.7.1
Crypto and TLS for C&
Botan::CT::Mask< T > Class Template Referencefinal

#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< Toperator~ () const
 
constexpr T select (T x, T y) const
 
constexpr T select_and_unpoison (T x, T y) const
 
Mask< Tselect_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< Tcleared ()
 
template<typename U >
static constexpr Mask< Texpand (Mask< U > m)
 
static constexpr Mask< Texpand (T v)
 
static constexpr Mask< Texpand_bit (T v, size_t bit)
 
static constexpr Mask< Texpand_top_bit (T v)
 
static constexpr Mask< Tfrom_choice (Choice c)
 
static constexpr Mask< Tis_any_of (T v, std::initializer_list< T > accepted)
 
static constexpr Mask< Tis_equal (T x, T y)
 
static constexpr Mask< Tis_gt (T x, T y)
 
static constexpr Mask< Tis_gte (T x, T y)
 
static constexpr Mask< Tis_lt (T x, T y)
 
static constexpr Mask< Tis_lte (T x, T y)
 
static constexpr Mask< Tis_within_range (T v, T l, T u)
 
static constexpr Mask< Tis_zero (T x)
 
static constexpr Mask< Tset ()
 

Friends

Mask< Toperator& (Mask< T > x, Mask< T > y)
 
Mask< Toperator^ (Mask< T > x, Mask< T > y)
 
Mask< Toperator| (Mask< T > x, Mask< T > y)
 

Detailed Description

template<typename T>
class Botan::CT::Mask< T >

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.

Constructor & Destructor Documentation

◆ Mask() [1/2]

◆ Mask() [2/2]

template<typename T >
template<typename U >
Botan::CT::Mask< T >::Mask ( Mask< U > o)
inlineconstexpr

Derive a Mask from a Mask of a larger type

Definition at line 391 of file ct_utils.h.

391 : m_mask(static_cast<T>(o.value())) {
392 static_assert(sizeof(U) > sizeof(T), "sizes ok");
393 }
FE_25519 T
Definition ge.cpp:34

References T.

Member Function Documentation

◆ _const_time_poison()

template<typename T >
void Botan::CT::Mask< T >::_const_time_poison ( ) const
inlineconstexpr

Definition at line 637 of file ct_utils.h.

637{ CT::poison(m_mask); }
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:53

References Botan::CT::poison().

◆ _const_time_unpoison()

template<typename T >
void Botan::CT::Mask< T >::_const_time_unpoison ( ) const
inlineconstexpr

Definition at line 639 of file ct_utils.h.

639{ CT::unpoison(m_mask); }
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64

References Botan::CT::unpoison().

◆ as_bool()

template<typename T >
bool Botan::CT::Mask< T >::as_bool ( ) const
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.

625{ return unpoisoned_value() != 0; }
constexpr T unpoisoned_value() const
Definition ct_utils.h:609

References Botan::CT::Mask< T >::unpoisoned_value().

◆ as_choice()

template<typename T >
CT::Choice Botan::CT::Mask< T >::as_choice ( ) const
inlineconstexpr

Return a Choice based on this mask

Definition at line 630 of file ct_utils.h.

static constexpr Choice from_int(T v)
Definition ct_utils.h:311

References Botan::CT::Choice::from_int(), and Botan::CT::Mask< T >::unpoisoned_value().

Referenced by Botan::oaep_find_delim().

◆ cleared()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::cleared ( )
inlinestaticconstexpr

Return a Mask<T> of |0| (all bits cleared)

Definition at line 403 of file ct_utils.h.

403{ return Mask<T>(0); }
Mask(const Mask< T > &other)=default

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().

◆ conditional_swap()

template<typename T >
template<typename U >
requires (sizeof(U) <= sizeof(T))
void Botan::CT::Mask< T >::conditional_swap ( U & x,
U & y ) const
inline

If this mask is set, swap x and y

Definition at line 596 of file ct_utils.h.

598 {
599 auto cnd = Mask<U>(*this);
600 U t0 = cnd.select(y, x);
601 U t1 = cnd.select(x, y);
602 x = t0;
603 y = t1;
604 }

References Botan::CT::Mask< T >::Mask().

◆ expand() [1/2]

template<typename T >
template<typename U >
static constexpr Mask< T > Botan::CT::Mask< T >::expand ( Mask< U > m)
inlinestaticconstexpr

Return a Mask<T> which is set if m is set

Definition at line 440 of file ct_utils.h.

440 {
441 static_assert(sizeof(U) < sizeof(T), "sizes ok");
442 return ~Mask<T>::is_zero(m.value());
443 }

References T, and Botan::CT::Mask< T >::value().

◆ expand() [2/2]

◆ expand_bit()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::expand_bit ( T v,
size_t bit )
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.

432 {
433 return CT::Mask<T>::expand_top_bit(v << (sizeof(v) * 8 - 1 - bit));
434 }
static constexpr Mask< T > expand_top_bit(T v)
Definition ct_utils.h:426

References Botan::CT::Mask< T >::expand_top_bit().

Referenced by Botan::FrodoMatrix::sample().

◆ expand_top_bit()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::expand_top_bit ( T v)
inlinestaticconstexpr

◆ from_choice()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::from_choice ( Choice c)
inlinestaticconstexpr

Return a Mask<T> which is set if choice is set

Definition at line 413 of file ct_utils.h.

413 {
414 if constexpr(sizeof(T) <= sizeof(uint32_t)) {
415 // Take advantage of the fact that Choice's mask is always
416 // either |0| or |1|
417 return Mask<T>(static_cast<T>(c.value()));
418 } else {
419 return ~Mask<T>::is_zero(c.value());
420 }
421 }

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().

◆ if_not_set_return()

template<typename T >
T Botan::CT::Mask< T >::if_not_set_return ( T x) const
inlineconstexpr

Return x if the mask is cleared, or otherwise zero

Definition at line 554 of file ct_utils.h.

554{ return ~value() & x; }

Referenced by Botan::CT::Mask< T >::if_set_zero_out().

◆ if_set_return()

template<typename T >
T Botan::CT::Mask< T >::if_set_return ( T x) const
inlineconstexpr

Return x if the mask is set, or otherwise zero

Definition at line 549 of file ct_utils.h.

549{ return value() & x; }
constexpr T value() const
Definition ct_utils.h:635

References Botan::CT::Mask< T >::value().

◆ if_set_zero_out()

template<typename T >
void Botan::CT::Mask< T >::if_set_zero_out ( T buf[],
size_t elems )
inlineconstexpr

If this mask is set, zero out buf, otherwise do nothing

Definition at line 586 of file ct_utils.h.

586 {
587 for(size_t i = 0; i != elems; ++i) {
588 buf[i] = this->if_not_set_return(buf[i]);
589 }
590 }
constexpr T if_not_set_return(T x) const
Definition ct_utils.h:554

References Botan::CT::Mask< T >::if_not_set_return().

◆ is_any_of()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::is_any_of ( T v,
std::initializer_list< T > accepted )
inlinestaticconstexpr

Definition at line 490 of file ct_utils.h.

490 {
491 T accept = 0;
492
493 for(auto a : accepted) {
494 const T diff = a ^ v;
495 const T eq_zero = value_barrier<T>(~diff & (diff - 1));
496 accept |= eq_zero;
497 }
498
499 return Mask<T>::expand_top_bit(accept);
500 }

References Botan::CT::Mask< T >::expand_top_bit(), T, and Botan::CT::value_barrier().

◆ is_equal()

◆ is_gt()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::is_gt ( T x,
T y )
inlinestaticconstexpr

Return a Mask<T> which is set if x > y

Definition at line 469 of file ct_utils.h.

469{ return Mask<T>::is_lt(y, x); }
static constexpr Mask< T > is_lt(T x, T y)
Definition ct_utils.h:461

References Botan::CT::Mask< T >::is_lt().

Referenced by Botan::ANSI_X923_Padding::unpad(), Botan::ESP_Padding::unpad(), and Botan::PKCS7_Padding::unpad().

◆ is_gte()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::is_gte ( T x,
T y )
inlinestaticconstexpr

Return a Mask<T> which is set if x >= y

Definition at line 479 of file ct_utils.h.

479{ return ~Mask<T>::is_lt(x, y); }

Referenced by Botan::ct_divide_word(), Botan::ct_mod_word(), Botan::ANSI_X923_Padding::unpad(), and Botan::PKCS7_Padding::unpad().

◆ is_lt()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::is_lt ( T x,
T y )
inlinestaticconstexpr

◆ is_lte()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::is_lte ( T x,
T y )
inlinestaticconstexpr

Return a Mask<T> which is set if x <= y

Definition at line 474 of file ct_utils.h.

474{ return ~Mask<T>::is_gt(x, y); }

Referenced by Botan::TLS::check_tls_cbc_padding(), Botan::constant_time_compare(), and Botan::CT::copy_output().

◆ is_within_range()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::is_within_range ( T v,
T l,
T u )
inlinestaticconstexpr

Definition at line 481 of file ct_utils.h.

481 {
482 //return Mask<T>::is_gte(v, l) & Mask<T>::is_lte(v, u);
483
484 const T v_lt_l = v ^ ((v ^ l) | ((v - l) ^ v));
485 const T v_gt_u = u ^ ((u ^ v) | ((u - v) ^ u));
486 const T either = value_barrier(v_lt_l) | value_barrier(v_gt_u);
487 return ~Mask<T>::expand_top_bit(either);
488 }

References T, and Botan::CT::value_barrier().

◆ is_zero()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::is_zero ( T x)
inlinestaticconstexpr

◆ operator&=()

template<typename T >
Mask< T > & Botan::CT::Mask< T >::operator&= ( Mask< T > o)
inline

AND-combine two masks

Definition at line 505 of file ct_utils.h.

505 {
506 m_mask &= o.value();
507 return (*this);
508 }

References Botan::CT::Mask< T >::value().

◆ operator=()

template<typename T >
Mask< T > & Botan::CT::Mask< T >::operator= ( const Mask< T > & other)
default

◆ operator^=()

template<typename T >
Mask< T > & Botan::CT::Mask< T >::operator^= ( Mask< T > o)
inline

XOR-combine two masks

Definition at line 513 of file ct_utils.h.

513 {
514 m_mask ^= o.value();
515 return (*this);
516 }

References Botan::CT::Mask< T >::value().

◆ operator|=()

template<typename T >
Mask< T > & Botan::CT::Mask< T >::operator|= ( Mask< T > o)
inline

OR-combine two masks

Definition at line 521 of file ct_utils.h.

521 {
522 m_mask |= o.value();
523 return (*this);
524 }

References Botan::CT::Mask< T >::value().

◆ operator~()

template<typename T >
Mask< T > Botan::CT::Mask< T >::operator~ ( ) const
inlineconstexpr

Negate this mask

Definition at line 544 of file ct_utils.h.

544{ return Mask<T>(~value()); }

References Botan::CT::Mask< T >::Mask(), and Botan::CT::Mask< T >::value().

◆ select()

template<typename T >
T Botan::CT::Mask< T >::select ( T x,
T y ) const
inlineconstexpr

If this mask is set, return x, otherwise return y

Definition at line 559 of file ct_utils.h.

559{ return choose(value(), x, y); }
constexpr T choose(T mask, T a, T b)
Definition bit_ops.h:204

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().

◆ select_and_unpoison()

template<typename T >
T Botan::CT::Mask< T >::select_and_unpoison ( T x,
T y ) const
inlineconstexpr

Definition at line 561 of file ct_utils.h.

561 {
562 T r = this->select(x, y);
563 CT::unpoison(r);
564 return r;
565 }
constexpr T select(T x, T y) const
Definition ct_utils.h:559

References Botan::CT::Mask< T >::select(), T, and Botan::CT::unpoison().

◆ select_mask()

template<typename T >
Mask< T > Botan::CT::Mask< T >::select_mask ( Mask< T > x,
Mask< T > y ) const
inline

If this mask is set, return x, otherwise return y

Definition at line 570 of file ct_utils.h.

570{ return Mask<T>(select(x.value(), y.value())); }

References Botan::CT::Mask< T >::Mask(), Botan::CT::Mask< T >::select(), and Botan::CT::Mask< T >::value().

◆ select_n()

template<typename T >
void Botan::CT::Mask< T >::select_n ( T output[],
const T x[],
const T y[],
size_t len ) const
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.

576 {
577 const T mask = value();
578 for(size_t i = 0; i != len; ++i) {
579 output[i] = choose(mask, x[i], y[i]);
580 }
581 }

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().

◆ set()

template<typename T >
static constexpr Mask< T > Botan::CT::Mask< T >::set ( )
inlinestaticconstexpr

Return a Mask<T> of |1| (all bits set)

Definition at line 398 of file ct_utils.h.

398{ return Mask<T>(static_cast<T>(~0)); }

References Botan::CT::Mask< T >::Mask(), and T.

Referenced by Botan::oaep_find_delim().

◆ unpoisoned_value()

template<typename T >
T Botan::CT::Mask< T >::unpoisoned_value ( ) const
inlineconstexpr

Return the value of the mask, unpoisoned

Definition at line 609 of file ct_utils.h.

609 {
610 T r = value();
611 CT::unpoison(r);
612 return r;
613 }

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().

◆ value()

Friends And Related Symbol Documentation

◆ operator&

template<typename T >
Mask< T > operator& ( Mask< T > x,
Mask< T > y )
friend

AND-combine two masks

Definition at line 529 of file ct_utils.h.

529{ return Mask<T>(x.value() & y.value()); }

◆ operator^

template<typename T >
Mask< T > operator^ ( Mask< T > x,
Mask< T > y )
friend

XOR-combine two masks

Definition at line 534 of file ct_utils.h.

534{ return Mask<T>(x.value() ^ y.value()); }

◆ operator|

template<typename T >
Mask< T > operator| ( Mask< T > x,
Mask< T > y )
friend

OR-combine two masks

Definition at line 539 of file ct_utils.h.

539{ return Mask<T>(x.value() | y.value()); }

The documentation for this class was generated from the following file: