Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
Classes | Functions
jau::CT Namespace Reference

Functions for constant time operations on data and testing of constant time annotations using valgrind. More...

Classes

class  Mask
 A Mask type used for constant-time operations. More...
 

Functions

template<typename T >
Mask< T > conditional_copy_mem (T cnd, T *to, const T *from0, const T *from1, size_t elems) noexcept
 
template<typename T >
void conditional_swap (bool cnd, T &x, T &y) noexcept
 
template<typename T >
void conditional_swap_ptr (bool cnd, T &x, T &y) noexcept
 
template<typename T >
void poison (const T *p, size_t n)
 Use valgrind to mark the contents of memory as being undefined. More...
 
template<typename T >
void unpoison (const T *p, size_t n)
 
template<typename T >
void unpoison (T &p)
 

Detailed Description

Functions for constant time operations on data and testing of constant time annotations using valgrind.

For more information about constant time programming see Wagner, Molnar, et al "The Program Counter Security Model"

(C) 2010 Falko Strenzke (C) 2015,2016,2018 Jack Lloyd (C) 2024 Sven Gothel

jaulib including this code is released under the MIT License (see COPYING) Botan itself is released under the Simplified BSD License (see COPYING)

Function Documentation

◆ poison()

template<typename T >
void jau::CT::poison ( const T *  p,
size_t  n 
)
inline

Use valgrind to mark the contents of memory as being undefined.

Valgrind will accept operations which manipulate undefined values, but will warn if an undefined value is used to decided a conditional jump or a load/store address. So if we poison all of our inputs we can confirm that the operations in question are truly const time when compiled by whatever compiler is in use.

Even better, the VALGRIND_MAKE_MEM_* macros work even when the program is not run under valgrind (though with a few cycles of overhead, which is unfortunate in final binaries as these annotations tend to be used in fairly important loops).

This approach was first used in ctgrind (https://github.com/agl/ctgrind) but calling the valgrind mecheck API directly works just as well and doesn't require a custom patched valgrind.

Definition at line 46 of file ct_utils.hpp.

◆ unpoison() [1/2]

template<typename T >
void jau::CT::unpoison ( const T *  p,
size_t  n 
)
inline

Definition at line 54 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ unpoison() [2/2]

template<typename T >
void jau::CT::unpoison ( T &  p)
inline

Definition at line 62 of file ct_utils.hpp.

◆ conditional_copy_mem()

template<typename T >
Mask< T > jau::CT::conditional_copy_mem ( cnd,
T *  to,
const T *  from0,
const T *  from1,
size_t  elems 
)
inlinenoexcept

Definition at line 358 of file ct_utils.hpp.

◆ conditional_swap()

template<typename T >
void jau::CT::conditional_swap ( bool  cnd,
T &  x,
T &  y 
)
inlinenoexcept

Definition at line 370 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ conditional_swap_ptr()

template<typename T >
void jau::CT::conditional_swap_ptr ( bool  cnd,
T &  x,
T &  y 
)
inlinenoexcept

Definition at line 381 of file ct_utils.hpp.

Here is the caller graph for this function: