Coverage Report

Created: 2020-05-23 13:54

/src/botan/build/include/botan/internal/ct_utils.h
Line
Count
Source
1
/*
2
* Functions for constant time operations on data and testing of
3
* constant time annotations using valgrind.
4
*
5
* For more information about constant time programming see
6
* Wagner, Molnar, et al "The Program Counter Security Model"
7
*
8
* (C) 2010 Falko Strenzke
9
* (C) 2015,2016,2018 Jack Lloyd
10
*
11
* Botan is released under the Simplified BSD License (see license.txt)
12
*/
13
14
#ifndef BOTAN_CT_UTILS_H_
15
#define BOTAN_CT_UTILS_H_
16
17
#include <botan/secmem.h>
18
#include <botan/internal/bit_ops.h>
19
#include <type_traits>
20
#include <vector>
21
22
#if defined(BOTAN_HAS_VALGRIND)
23
  #include <valgrind/memcheck.h>
24
#endif
25
26
namespace Botan {
27
28
namespace CT {
29
30
/**
31
* Use valgrind to mark the contents of memory as being undefined.
32
* Valgrind will accept operations which manipulate undefined values,
33
* but will warn if an undefined value is used to decided a conditional
34
* jump or a load/store address. So if we poison all of our inputs we
35
* can confirm that the operations in question are truly const time
36
* when compiled by whatever compiler is in use.
37
*
38
* Even better, the VALGRIND_MAKE_MEM_* macros work even when the
39
* program is not run under valgrind (though with a few cycles of
40
* overhead, which is unfortunate in final binaries as these
41
* annotations tend to be used in fairly important loops).
42
*
43
* This approach was first used in ctgrind (https://github.com/agl/ctgrind)
44
* but calling the valgrind mecheck API directly works just as well and
45
* doesn't require a custom patched valgrind.
46
*/
47
template<typename T>
48
inline void poison(const T* p, size_t n)
49
47.0k
   {
50
#if defined(BOTAN_HAS_VALGRIND)
51
   VALGRIND_MAKE_MEM_UNDEFINED(p, n * sizeof(T));
52
#else
53
47.0k
   BOTAN_UNUSED(p);
54
47.0k
   BOTAN_UNUSED(n);
55
47.0k
#endif
56
47.0k
   }
void Botan::CT::poison<unsigned long>(unsigned long const*, unsigned long)
Line
Count
Source
49
43.5k
   {
50
#if defined(BOTAN_HAS_VALGRIND)
51
   VALGRIND_MAKE_MEM_UNDEFINED(p, n * sizeof(T));
52
#else
53
43.5k
   BOTAN_UNUSED(p);
54
43.5k
   BOTAN_UNUSED(n);
55
43.5k
#endif
56
43.5k
   }
void Botan::CT::poison<unsigned char>(unsigned char const*, unsigned long)
Line
Count
Source
49
3.49k
   {
50
#if defined(BOTAN_HAS_VALGRIND)
51
   VALGRIND_MAKE_MEM_UNDEFINED(p, n * sizeof(T));
52
#else
53
3.49k
   BOTAN_UNUSED(p);
54
3.49k
   BOTAN_UNUSED(n);
55
3.49k
#endif
56
3.49k
   }
Unexecuted instantiation: void Botan::CT::poison<unsigned short>(unsigned short const*, unsigned long)
57
58
template<typename T>
59
inline void unpoison(const T* p, size_t n)
60
48.9k
   {
61
#if defined(BOTAN_HAS_VALGRIND)
62
   VALGRIND_MAKE_MEM_DEFINED(p, n * sizeof(T));
63
#else
64
48.9k
   BOTAN_UNUSED(p);
65
48.9k
   BOTAN_UNUSED(n);
66
48.9k
#endif
67
48.9k
   }
void Botan::CT::unpoison<unsigned long>(unsigned long const*, unsigned long)
Line
Count
Source
60
42.8k
   {
61
#if defined(BOTAN_HAS_VALGRIND)
62
   VALGRIND_MAKE_MEM_DEFINED(p, n * sizeof(T));
63
#else
64
42.8k
   BOTAN_UNUSED(p);
65
42.8k
   BOTAN_UNUSED(n);
66
42.8k
#endif
67
42.8k
   }
void Botan::CT::unpoison<unsigned char>(unsigned char const*, unsigned long)
Line
Count
Source
60
6.08k
   {
61
#if defined(BOTAN_HAS_VALGRIND)
62
   VALGRIND_MAKE_MEM_DEFINED(p, n * sizeof(T));
63
#else
64
6.08k
   BOTAN_UNUSED(p);
65
6.08k
   BOTAN_UNUSED(n);
66
6.08k
#endif
67
6.08k
   }
Unexecuted instantiation: void Botan::CT::unpoison<unsigned short>(unsigned short const*, unsigned long)
Unexecuted instantiation: void Botan::CT::unpoison<unsigned int>(unsigned int const*, unsigned long)
68
69
template<typename T>
70
inline void unpoison(T& p)
71
248M
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
248M
   BOTAN_UNUSED(p);
76
248M
#endif
77
248M
   }
void Botan::CT::unpoison<unsigned long>(unsigned long&)
Line
Count
Source
71
148M
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
148M
   BOTAN_UNUSED(p);
76
148M
#endif
77
148M
   }
void Botan::CT::unpoison<unsigned long const>(unsigned long const&)
Line
Count
Source
71
9.74M
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
9.74M
   BOTAN_UNUSED(p);
76
9.74M
#endif
77
9.74M
   }
void Botan::CT::unpoison<unsigned int>(unsigned int&)
Line
Count
Source
71
634k
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
634k
   BOTAN_UNUSED(p);
76
634k
#endif
77
634k
   }
void Botan::CT::unpoison<unsigned char>(unsigned char&)
Line
Count
Source
71
486
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
486
   BOTAN_UNUSED(p);
76
486
#endif
77
486
   }
void Botan::CT::unpoison<unsigned char const>(unsigned char const&)
Line
Count
Source
71
704
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
704
   BOTAN_UNUSED(p);
76
704
#endif
77
704
   }
void Botan::CT::unpoison<unsigned short>(unsigned short&)
Line
Count
Source
71
538
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
538
   BOTAN_UNUSED(p);
76
538
#endif
77
538
   }
void Botan::CT::unpoison<Botan::CT::Mask<unsigned short> const>(Botan::CT::Mask<unsigned short> const&)
Line
Count
Source
71
269
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
269
   BOTAN_UNUSED(p);
76
269
#endif
77
269
   }
void Botan::CT::unpoison<long>(long&)
Line
Count
Source
71
89.9M
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
89.9M
   BOTAN_UNUSED(p);
76
89.9M
#endif
77
89.9M
   }
Unexecuted instantiation: void Botan::CT::unpoison<Botan::CT::Mask<unsigned char> >(Botan::CT::Mask<unsigned char>&)
78
79
/**
80
* A Mask type used for constant-time operations. A Mask<T> always has value
81
* either 0 (all bits cleared) or ~0 (all bits set). All operations in a Mask<T>
82
* are intended to compile to code which does not contain conditional jumps.
83
* This must be verified with tooling (eg binary disassembly or using valgrind)
84
* since you never know what a compiler might do.
85
*/
86
template<typename T>
87
class Mask
88
   {
89
   public:
90
      static_assert(std::is_unsigned<T>::value, "CT::Mask only defined for unsigned integer types");
91
92
      Mask(const Mask<T>& other) = default;
93
      Mask<T>& operator=(const Mask<T>& other) = default;
94
95
      /**
96
      * Derive a Mask from a Mask of a larger type
97
      */
98
      template<typename U>
99
      Mask(Mask<U> o) : m_mask(static_cast<T>(o.value()))
100
15.8k
         {
101
15.8k
         static_assert(sizeof(U) > sizeof(T), "sizes ok");
102
15.8k
         }
Botan::CT::Mask<unsigned char>::Mask<unsigned long>(Botan::CT::Mask<unsigned long>)
Line
Count
Source
100
15.8k
         {
101
15.8k
         static_assert(sizeof(U) > sizeof(T), "sizes ok");
102
15.8k
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned short>::Mask<unsigned int>(Botan::CT::Mask<unsigned int>)
103
104
      /**
105
      * Return a Mask<T> with all bits set
106
      */
107
      static Mask<T> set()
108
45.5k
         {
109
45.5k
         return Mask<T>(static_cast<T>(~0));
110
45.5k
         }
Botan::CT::Mask<unsigned char>::set()
Line
Count
Source
108
2.70k
         {
109
2.70k
         return Mask<T>(static_cast<T>(~0));
110
2.70k
         }
Botan::CT::Mask<unsigned long>::set()
Line
Count
Source
108
42.8k
         {
109
42.8k
         return Mask<T>(static_cast<T>(~0));
110
42.8k
         }
111
112
      /**
113
      * Return a Mask<T> with all bits cleared
114
      */
115
      static Mask<T> cleared()
116
3.03M
         {
117
3.03M
         return Mask<T>(0);
118
3.03M
         }
Botan::CT::Mask<unsigned char>::cleared()
Line
Count
Source
116
3.40k
         {
117
3.40k
         return Mask<T>(0);
118
3.40k
         }
Botan::CT::Mask<unsigned long>::cleared()
Line
Count
Source
116
3.03M
         {
117
3.03M
         return Mask<T>(0);
118
3.03M
         }
119
120
      /**
121
      * Return a Mask<T> which is set if v is != 0
122
      */
123
      static Mask<T> expand(T v)
124
1.30G
         {
125
1.30G
         return ~Mask<T>::is_zero(v);
126
1.30G
         }
Botan::CT::Mask<unsigned long>::expand(unsigned long)
Line
Count
Source
124
1.28G
         {
125
1.28G
         return ~Mask<T>::is_zero(v);
126
1.28G
         }
Botan::CT::Mask<unsigned char>::expand(unsigned char)
Line
Count
Source
124
16.7M
         {
125
16.7M
         return ~Mask<T>::is_zero(v);
126
16.7M
         }
Botan::CT::Mask<unsigned short>::expand(unsigned short)
Line
Count
Source
124
538
         {
125
538
         return ~Mask<T>::is_zero(v);
126
538
         }
127
128
      /**
129
      * Return a Mask<T> which is set if m is set
130
      */
131
      template<typename U>
132
      static Mask<T> expand(Mask<U> m)
133
192
         {
134
192
         static_assert(sizeof(U) < sizeof(T), "sizes ok");
135
192
         return ~Mask<T>::is_zero(m.value());
136
192
         }
137
138
      /**
139
      * Return a Mask<T> which is set if v is == 0 or cleared otherwise
140
      */
141
      static Mask<T> is_zero(T x)
142
4.21G
         {
143
4.21G
         return Mask<T>(ct_is_zero<T>(x));
144
4.21G
         }
Botan::CT::Mask<unsigned char>::is_zero(unsigned char)
Line
Count
Source
142
18.3M
         {
143
18.3M
         return Mask<T>(ct_is_zero<T>(x));
144
18.3M
         }
Botan::CT::Mask<unsigned long>::is_zero(unsigned long)
Line
Count
Source
142
4.19G
         {
143
4.19G
         return Mask<T>(ct_is_zero<T>(x));
144
4.19G
         }
Botan::CT::Mask<unsigned short>::is_zero(unsigned short)
Line
Count
Source
142
76.1k
         {
143
76.1k
         return Mask<T>(ct_is_zero<T>(x));
144
76.1k
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned int>::is_zero(unsigned int)
145
146
      /**
147
      * Return a Mask<T> which is set if x == y
148
      */
149
      static Mask<T> is_equal(T x, T y)
150
2.87G
         {
151
2.87G
         return Mask<T>::is_zero(static_cast<T>(x ^ y));
152
2.87G
         }
Botan::CT::Mask<unsigned long>::is_equal(unsigned long, unsigned long)
Line
Count
Source
150
2.87G
         {
151
2.87G
         return Mask<T>::is_zero(static_cast<T>(x ^ y));
152
2.87G
         }
Botan::CT::Mask<unsigned char>::is_equal(unsigned char, unsigned char)
Line
Count
Source
150
385k
         {
151
385k
         return Mask<T>::is_zero(static_cast<T>(x ^ y));
152
385k
         }
Botan::CT::Mask<unsigned short>::is_equal(unsigned short, unsigned short)
Line
Count
Source
150
75.6k
         {
151
75.6k
         return Mask<T>::is_zero(static_cast<T>(x ^ y));
152
75.6k
         }
153
154
      /**
155
      * Return a Mask<T> which is set if x < y
156
      */
157
      static Mask<T> is_lt(T x, T y)
158
1.24G
         {
159
1.24G
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
1.24G
         }
Botan::CT::Mask<unsigned long>::is_lt(unsigned long, unsigned long)
Line
Count
Source
158
1.24G
         {
159
1.24G
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
1.24G
         }
Botan::CT::Mask<unsigned int>::is_lt(unsigned int, unsigned int)
Line
Count
Source
158
634k
         {
159
634k
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
634k
         }
Botan::CT::Mask<unsigned char>::is_lt(unsigned char, unsigned char)
Line
Count
Source
158
135
         {
159
135
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
135
         }
Botan::CT::Mask<unsigned short>::is_lt(unsigned short, unsigned short)
Line
Count
Source
158
76.1k
         {
159
76.1k
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
76.1k
         }
161
162
      /**
163
      * Return a Mask<T> which is set if x > y
164
      */
165
      static Mask<T> is_gt(T x, T y)
166
85.1k
         {
167
85.1k
         return Mask<T>::is_lt(y, x);
168
85.1k
         }
Botan::CT::Mask<unsigned long>::is_gt(unsigned long, unsigned long)
Line
Count
Source
166
9.38k
         {
167
9.38k
         return Mask<T>::is_lt(y, x);
168
9.38k
         }
Botan::CT::Mask<unsigned char>::is_gt(unsigned char, unsigned char)
Line
Count
Source
166
135
         {
167
135
         return Mask<T>::is_lt(y, x);
168
135
         }
Botan::CT::Mask<unsigned short>::is_gt(unsigned short, unsigned short)
Line
Count
Source
166
75.6k
         {
167
75.6k
         return Mask<T>::is_lt(y, x);
168
75.6k
         }
169
170
      /**
171
      * Return a Mask<T> which is set if x <= y
172
      */
173
      static Mask<T> is_lte(T x, T y)
174
78.4k
         {
175
78.4k
         return ~Mask<T>::is_gt(x, y);
176
78.4k
         }
Botan::CT::Mask<unsigned short>::is_lte(unsigned short, unsigned short)
Line
Count
Source
174
75.6k
         {
175
75.6k
         return ~Mask<T>::is_gt(x, y);
176
75.6k
         }
Botan::CT::Mask<unsigned long>::is_lte(unsigned long, unsigned long)
Line
Count
Source
174
2.85k
         {
175
2.85k
         return ~Mask<T>::is_gt(x, y);
176
2.85k
         }
177
178
      /**
179
      * Return a Mask<T> which is set if x >= y
180
      */
181
      static Mask<T> is_gte(T x, T y)
182
656k
         {
183
656k
         return ~Mask<T>::is_lt(x, y);
184
656k
         }
Botan::CT::Mask<unsigned int>::is_gte(unsigned int, unsigned int)
Line
Count
Source
182
634k
         {
183
634k
         return ~Mask<T>::is_lt(x, y);
184
634k
         }
Botan::CT::Mask<unsigned long>::is_gte(unsigned long, unsigned long)
Line
Count
Source
182
21.9k
         {
183
21.9k
         return ~Mask<T>::is_lt(x, y);
184
21.9k
         }
185
186
      /**
187
      * AND-combine two masks
188
      */
189
      Mask<T>& operator&=(Mask<T> o)
190
1.64M
         {
191
1.64M
         m_mask &= o.value();
192
1.64M
         return (*this);
193
1.64M
         }
Botan::CT::Mask<unsigned long>::operator&=(Botan::CT::Mask<unsigned long>)
Line
Count
Source
190
813k
         {
191
813k
         m_mask &= o.value();
192
813k
         return (*this);
193
813k
         }
Botan::CT::Mask<unsigned char>::operator&=(Botan::CT::Mask<unsigned char>)
Line
Count
Source
190
835k
         {
191
835k
         m_mask &= o.value();
192
835k
         return (*this);
193
835k
         }
194
195
      /**
196
      * XOR-combine two masks
197
      */
198
      Mask<T>& operator^=(Mask<T> o)
199
         {
200
         m_mask ^= o.value();
201
         return (*this);
202
         }
203
204
      /**
205
      * OR-combine two masks
206
      */
207
      Mask<T>& operator|=(Mask<T> o)
208
53.1M
         {
209
53.1M
         m_mask |= o.value();
210
53.1M
         return (*this);
211
53.1M
         }
Botan::CT::Mask<unsigned long>::operator|=(Botan::CT::Mask<unsigned long>)
Line
Count
Source
208
52.3M
         {
209
52.3M
         m_mask |= o.value();
210
52.3M
         return (*this);
211
52.3M
         }
Botan::CT::Mask<unsigned char>::operator|=(Botan::CT::Mask<unsigned char>)
Line
Count
Source
208
792k
         {
209
792k
         m_mask |= o.value();
210
792k
         return (*this);
211
792k
         }
Botan::CT::Mask<unsigned short>::operator|=(Botan::CT::Mask<unsigned short>)
Line
Count
Source
208
75.3k
         {
209
75.3k
         m_mask |= o.value();
210
75.3k
         return (*this);
211
75.3k
         }
212
213
      /**
214
      * AND-combine two masks
215
      */
216
      friend Mask<T> operator&(Mask<T> x, Mask<T> y)
217
128M
         {
218
128M
         return Mask<T>(x.value() & y.value());
219
128M
         }
Botan::CT::operator&(Botan::CT::Mask<unsigned long>, Botan::CT::Mask<unsigned long>)
Line
Count
Source
217
128M
         {
218
128M
         return Mask<T>(x.value() & y.value());
219
128M
         }
Botan::CT::operator&(Botan::CT::Mask<unsigned char>, Botan::CT::Mask<unsigned char>)
Line
Count
Source
217
570k
         {
218
570k
         return Mask<T>(x.value() & y.value());
219
570k
         }
Botan::CT::operator&(Botan::CT::Mask<unsigned short>, Botan::CT::Mask<unsigned short>)
Line
Count
Source
217
75.8k
         {
218
75.8k
         return Mask<T>(x.value() & y.value());
219
75.8k
         }
220
221
      /**
222
      * XOR-combine two masks
223
      */
224
      friend Mask<T> operator^(Mask<T> x, Mask<T> y)
225
1.90M
         {
226
1.90M
         return Mask<T>(x.value() ^ y.value());
227
1.90M
         }
228
229
      /**
230
      * OR-combine two masks
231
      */
232
      friend Mask<T> operator|(Mask<T> x, Mask<T> y)
233
128M
         {
234
128M
         return Mask<T>(x.value() | y.value());
235
128M
         }
Botan::CT::operator|(Botan::CT::Mask<unsigned char>, Botan::CT::Mask<unsigned char>)
Line
Count
Source
233
185k
         {
234
185k
         return Mask<T>(x.value() | y.value());
235
185k
         }
Botan::CT::operator|(Botan::CT::Mask<unsigned long>, Botan::CT::Mask<unsigned long>)
Line
Count
Source
233
128M
         {
234
128M
         return Mask<T>(x.value() | y.value());
235
128M
         }
236
237
      /**
238
      * Negate this mask
239
      */
240
      Mask<T> operator~() const
241
1.30G
         {
242
1.30G
         return Mask<T>(~value());
243
1.30G
         }
Botan::CT::Mask<unsigned long>::operator~() const
Line
Count
Source
241
1.28G
         {
242
1.28G
         return Mask<T>(~value());
243
1.28G
         }
Botan::CT::Mask<unsigned char>::operator~() const
Line
Count
Source
241
17.3M
         {
242
17.3M
         return Mask<T>(~value());
243
17.3M
         }
Botan::CT::Mask<unsigned int>::operator~() const
Line
Count
Source
241
634k
         {
242
634k
         return Mask<T>(~value());
243
634k
         }
Botan::CT::Mask<unsigned short>::operator~() const
Line
Count
Source
241
151k
         {
242
151k
         return Mask<T>(~value());
243
151k
         }
244
245
      /**
246
      * Return x if the mask is set, or otherwise zero
247
      */
248
      T if_set_return(T x) const
249
7.33G
         {
250
7.33G
         return m_mask & x;
251
7.33G
         }
Botan::CT::Mask<unsigned long>::if_set_return(unsigned long) const
Line
Count
Source
249
7.33G
         {
250
7.33G
         return m_mask & x;
251
7.33G
         }
Botan::CT::Mask<unsigned short>::if_set_return(unsigned short) const
Line
Count
Source
249
538
         {
250
538
         return m_mask & x;
251
538
         }
Botan::CT::Mask<unsigned char>::if_set_return(unsigned char) const
Line
Count
Source
249
835k
         {
250
835k
         return m_mask & x;
251
835k
         }
252
253
      /**
254
      * Return x if the mask is cleared, or otherwise zero
255
      */
256
      T if_not_set_return(T x) const
257
53.9M
         {
258
53.9M
         return ~m_mask & x;
259
53.9M
         }
Botan::CT::Mask<unsigned char>::if_not_set_return(unsigned char) const
Line
Count
Source
257
1.45M
         {
258
1.45M
         return ~m_mask & x;
259
1.45M
         }
Botan::CT::Mask<unsigned short>::if_not_set_return(unsigned short) const
Line
Count
Source
257
486
         {
258
486
         return ~m_mask & x;
259
486
         }
Botan::CT::Mask<unsigned long>::if_not_set_return(unsigned long) const
Line
Count
Source
257
52.4M
         {
258
52.4M
         return ~m_mask & x;
259
52.4M
         }
260
261
      /**
262
      * If this mask is set, return x, otherwise return y
263
      */
264
      T select(T x, T y) const
265
21.7G
         {
266
21.7G
         // (x & value()) | (y & ~value())
267
21.7G
         return static_cast<T>(y ^ (value() & (x ^ y)));
268
21.7G
         }
Botan::CT::Mask<unsigned long>::select(unsigned long, unsigned long) const
Line
Count
Source
265
21.7G
         {
266
21.7G
         // (x & value()) | (y & ~value())
267
21.7G
         return static_cast<T>(y ^ (value() & (x ^ y)));
268
21.7G
         }
Botan::CT::Mask<unsigned char>::select(unsigned char, unsigned char) const
Line
Count
Source
265
16.7M
         {
266
16.7M
         // (x & value()) | (y & ~value())
267
16.7M
         return static_cast<T>(y ^ (value() & (x ^ y)));
268
16.7M
         }
Botan::CT::Mask<unsigned int>::select(unsigned int, unsigned int) const
Line
Count
Source
265
634k
         {
266
634k
         // (x & value()) | (y & ~value())
267
634k
         return static_cast<T>(y ^ (value() & (x ^ y)));
268
634k
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned short>::select(unsigned short, unsigned short) const
269
270
      T select_and_unpoison(T x, T y) const
271
597
         {
272
597
         T r = this->select(x, y);
273
597
         CT::unpoison(r);
274
597
         return r;
275
597
         }
Botan::CT::Mask<unsigned long>::select_and_unpoison(unsigned long, unsigned long) const
Line
Count
Source
271
462
         {
272
462
         T r = this->select(x, y);
273
462
         CT::unpoison(r);
274
462
         return r;
275
462
         }
Botan::CT::Mask<unsigned char>::select_and_unpoison(unsigned char, unsigned char) const
Line
Count
Source
271
135
         {
272
135
         T r = this->select(x, y);
273
135
         CT::unpoison(r);
274
135
         return r;
275
135
         }
276
277
      /**
278
      * If this mask is set, return x, otherwise return y
279
      */
280
      Mask<T> select_mask(Mask<T> x, Mask<T> y) const
281
852M
         {
282
852M
         return Mask<T>(select(x.value(), y.value()));
283
852M
         }
284
285
      /**
286
      * Conditionally set output to x or y, depending on if mask is set or
287
      * cleared (resp)
288
      */
289
      void select_n(T output[], const T x[], const T y[], size_t len) const
290
511M
         {
291
4.84G
         for(size_t i = 0; i != len; ++i)
292
4.33G
            output[i] = this->select(x[i], y[i]);
293
511M
         }
Botan::CT::Mask<unsigned long>::select_n(unsigned long*, unsigned long const*, unsigned long const*, unsigned long) const
Line
Count
Source
290
511M
         {
291
4.84G
         for(size_t i = 0; i != len; ++i)
292
4.33G
            output[i] = this->select(x[i], y[i]);
293
511M
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned char>::select_n(unsigned char*, unsigned char const*, unsigned char const*, unsigned long) const
294
295
      /**
296
      * If this mask is set, zero out buf, otherwise do nothing
297
      */
298
      void if_set_zero_out(T buf[], size_t elems)
299
45.7k
         {
300
1.40M
         for(size_t i = 0; i != elems; ++i)
301
1.35M
            {
302
1.35M
            buf[i] = this->if_not_set_return(buf[i]);
303
1.35M
            }
304
45.7k
         }
Botan::CT::Mask<unsigned long>::if_set_zero_out(unsigned long*, unsigned long)
Line
Count
Source
299
42.8k
         {
300
346k
         for(size_t i = 0; i != elems; ++i)
301
303k
            {
302
303k
            buf[i] = this->if_not_set_return(buf[i]);
303
303k
            }
304
42.8k
         }
Botan::CT::Mask<unsigned char>::if_set_zero_out(unsigned char*, unsigned long)
Line
Count
Source
299
2.85k
         {
300
1.05M
         for(size_t i = 0; i != elems; ++i)
301
1.05M
            {
302
1.05M
            buf[i] = this->if_not_set_return(buf[i]);
303
1.05M
            }
304
2.85k
         }
305
306
      /**
307
      * Return the value of the mask, unpoisoned
308
      */
309
      T unpoisoned_value() const
310
11.1M
         {
311
11.1M
         T r = value();
312
11.1M
         CT::unpoison(r);
313
11.1M
         return r;
314
11.1M
         }
Botan::CT::Mask<unsigned long>::unpoisoned_value() const
Line
Count
Source
310
10.4M
         {
311
10.4M
         T r = value();
312
10.4M
         CT::unpoison(r);
313
10.4M
         return r;
314
10.4M
         }
Botan::CT::Mask<unsigned int>::unpoisoned_value() const
Line
Count
Source
310
634k
         {
311
634k
         T r = value();
312
634k
         CT::unpoison(r);
313
634k
         return r;
314
634k
         }
Botan::CT::Mask<unsigned short>::unpoisoned_value() const
Line
Count
Source
310
269
         {
311
269
         T r = value();
312
269
         CT::unpoison(r);
313
269
         return r;
314
269
         }
Botan::CT::Mask<unsigned char>::unpoisoned_value() const
Line
Count
Source
310
351
         {
311
351
         T r = value();
312
351
         CT::unpoison(r);
313
351
         return r;
314
351
         }
315
316
      /**
317
      * Return true iff this mask is set
318
      */
319
      bool is_set() const
320
11.1M
         {
321
11.1M
         return unpoisoned_value() != 0;
322
11.1M
         }
Botan::CT::Mask<unsigned long>::is_set() const
Line
Count
Source
320
10.4M
         {
321
10.4M
         return unpoisoned_value() != 0;
322
10.4M
         }
Botan::CT::Mask<unsigned int>::is_set() const
Line
Count
Source
320
634k
         {
321
634k
         return unpoisoned_value() != 0;
322
634k
         }
Botan::CT::Mask<unsigned short>::is_set() const
Line
Count
Source
320
269
         {
321
269
         return unpoisoned_value() != 0;
322
269
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned char>::is_set() const
323
324
      /**
325
      * Return the underlying value of the mask
326
      */
327
      T value() const
328
25.5G
         {
329
25.5G
         return m_mask;
330
25.5G
         }
Botan::CT::Mask<unsigned char>::value() const
Line
Count
Source
328
37.2M
         {
329
37.2M
         return m_mask;
330
37.2M
         }
Botan::CT::Mask<unsigned long>::value() const
Line
Count
Source
328
25.5G
         {
329
25.5G
         return m_mask;
330
25.5G
         }
Botan::CT::Mask<unsigned int>::value() const
Line
Count
Source
328
1.90M
         {
329
1.90M
         return m_mask;
330
1.90M
         }
Botan::CT::Mask<unsigned short>::value() const
Line
Count
Source
328
378k
         {
329
378k
         return m_mask;
330
378k
         }
331
332
   private:
333
7.88G
      Mask(T m) : m_mask(m) {}
Botan::CT::Mask<unsigned char>::Mask(unsigned char)
Line
Count
Source
333
36.4M
      Mask(T m) : m_mask(m) {}
Botan::CT::Mask<unsigned long>::Mask(unsigned long)
Line
Count
Source
333
7.84G
      Mask(T m) : m_mask(m) {}
Botan::CT::Mask<unsigned int>::Mask(unsigned int)
Line
Count
Source
333
1.26M
      Mask(T m) : m_mask(m) {}
Botan::CT::Mask<unsigned short>::Mask(unsigned short)
Line
Count
Source
333
379k
      Mask(T m) : m_mask(m) {}
334
335
      T m_mask;
336
   };
337
338
template<typename T>
339
inline Mask<T> conditional_copy_mem(T cnd,
340
                                    T* to,
341
                                    const T* from0,
342
                                    const T* from1,
343
                                    size_t elems)
344
78.2M
   {
345
78.2M
   const auto mask = CT::Mask<T>::expand(cnd);
346
78.2M
   mask.select_n(to, from0, from1, elems);
347
78.2M
   return mask;
348
78.2M
   }
349
350
template<typename T>
351
inline void conditional_swap(bool cnd, T& x, T& y)
352
25.7M
   {
353
25.7M
   const auto swap = CT::Mask<T>::expand(cnd);
354
25.7M
355
25.7M
   T t0 = swap.select(y, x);
356
25.7M
   T t1 = swap.select(x, y);
357
25.7M
   x = t0;
358
25.7M
   y = t1;
359
25.7M
   }
360
361
template<typename T>
362
inline void conditional_swap_ptr(bool cnd, T& x, T& y)
363
12.8M
   {
364
12.8M
   uintptr_t xp = reinterpret_cast<uintptr_t>(x);
365
12.8M
   uintptr_t yp = reinterpret_cast<uintptr_t>(y);
366
12.8M
367
12.8M
   conditional_swap<uintptr_t>(cnd, xp, yp);
368
12.8M
369
12.8M
   x = reinterpret_cast<T>(xp);
370
12.8M
   y = reinterpret_cast<T>(yp);
371
12.8M
   }
372
373
/**
374
* If bad_mask is unset, return in[delim_idx:input_length] copied to
375
* new buffer. If bad_mask is set, return an all zero vector of
376
* unspecified length.
377
*/
378
secure_vector<uint8_t> copy_output(CT::Mask<uint8_t> bad_input,
379
                                   const uint8_t input[],
380
                                   size_t input_length,
381
                                   size_t delim_idx);
382
383
secure_vector<uint8_t> strip_leading_zeros(const uint8_t in[], size_t length);
384
385
inline secure_vector<uint8_t> strip_leading_zeros(const secure_vector<uint8_t>& in)
386
2.50k
   {
387
2.50k
   return strip_leading_zeros(in.data(), in.size());
388
2.50k
   }
389
390
}
391
392
}
393
394
#endif