Coverage Report

Created: 2020-02-14 15:38

/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
204k
   {
50
#if defined(BOTAN_HAS_VALGRIND)
51
   VALGRIND_MAKE_MEM_UNDEFINED(p, n * sizeof(T));
52
#else
53
204k
   BOTAN_UNUSED(p);
54
204k
   BOTAN_UNUSED(n);
55
204k
#endif
56
204k
   }
void Botan::CT::poison<unsigned long>(unsigned long const*, unsigned long)
Line
Count
Source
49
202k
   {
50
#if defined(BOTAN_HAS_VALGRIND)
51
   VALGRIND_MAKE_MEM_UNDEFINED(p, n * sizeof(T));
52
#else
53
202k
   BOTAN_UNUSED(p);
54
202k
   BOTAN_UNUSED(n);
55
202k
#endif
56
202k
   }
void Botan::CT::poison<unsigned char>(unsigned char const*, unsigned long)
Line
Count
Source
49
2.12k
   {
50
#if defined(BOTAN_HAS_VALGRIND)
51
   VALGRIND_MAKE_MEM_UNDEFINED(p, n * sizeof(T));
52
#else
53
2.12k
   BOTAN_UNUSED(p);
54
2.12k
   BOTAN_UNUSED(n);
55
2.12k
#endif
56
2.12k
   }
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
207k
   {
61
#if defined(BOTAN_HAS_VALGRIND)
62
   VALGRIND_MAKE_MEM_DEFINED(p, n * sizeof(T));
63
#else
64
207k
   BOTAN_UNUSED(p);
65
207k
   BOTAN_UNUSED(n);
66
207k
#endif
67
207k
   }
void Botan::CT::unpoison<unsigned long>(unsigned long const*, unsigned long)
Line
Count
Source
60
202k
   {
61
#if defined(BOTAN_HAS_VALGRIND)
62
   VALGRIND_MAKE_MEM_DEFINED(p, n * sizeof(T));
63
#else
64
202k
   BOTAN_UNUSED(p);
65
202k
   BOTAN_UNUSED(n);
66
202k
#endif
67
202k
   }
void Botan::CT::unpoison<unsigned char>(unsigned char const*, unsigned long)
Line
Count
Source
60
5.21k
   {
61
#if defined(BOTAN_HAS_VALGRIND)
62
   VALGRIND_MAKE_MEM_DEFINED(p, n * sizeof(T));
63
#else
64
5.21k
   BOTAN_UNUSED(p);
65
5.21k
   BOTAN_UNUSED(n);
66
5.21k
#endif
67
5.21k
   }
Unexecuted instantiation: void Botan::CT::unpoison<unsigned short>(unsigned short const*, unsigned long)
68
69
template<typename T>
70
inline void unpoison(T& p)
71
267M
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
267M
   BOTAN_UNUSED(p);
76
267M
#endif
77
267M
   }
void Botan::CT::unpoison<unsigned long>(unsigned long&)
Line
Count
Source
71
160M
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
160M
   BOTAN_UNUSED(p);
76
160M
#endif
77
160M
   }
void Botan::CT::unpoison<unsigned long const>(unsigned long const&)
Line
Count
Source
71
10.9M
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
10.9M
   BOTAN_UNUSED(p);
76
10.9M
#endif
77
10.9M
   }
void Botan::CT::unpoison<unsigned int>(unsigned int&)
Line
Count
Source
71
695k
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
695k
   BOTAN_UNUSED(p);
76
695k
#endif
77
695k
   }
void Botan::CT::unpoison<long>(long&)
Line
Count
Source
71
95.3M
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
95.3M
   BOTAN_UNUSED(p);
76
95.3M
#endif
77
95.3M
   }
void Botan::CT::unpoison<unsigned char>(unsigned char&)
Line
Count
Source
71
509
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
509
   BOTAN_UNUSED(p);
76
509
#endif
77
509
   }
Unexecuted instantiation: void Botan::CT::unpoison<Botan::CT::Mask<unsigned char> >(Botan::CT::Mask<unsigned char>&)
void Botan::CT::unpoison<unsigned short>(unsigned short&)
Line
Count
Source
71
584
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
584
   BOTAN_UNUSED(p);
76
584
#endif
77
584
   }
void Botan::CT::unpoison<Botan::CT::Mask<unsigned short> const>(Botan::CT::Mask<unsigned short> const&)
Line
Count
Source
71
292
   {
72
#if defined(BOTAN_HAS_VALGRIND)
73
   VALGRIND_MAKE_MEM_DEFINED(&p, sizeof(T));
74
#else
75
292
   BOTAN_UNUSED(p);
76
292
#endif
77
292
   }
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
6.43k
         {
101
6.43k
         static_assert(sizeof(U) > sizeof(T), "sizes ok");
102
6.43k
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned short>::Mask<unsigned int>(Botan::CT::Mask<unsigned int>)
Botan::CT::Mask<unsigned char>::Mask<unsigned long>(Botan::CT::Mask<unsigned long>)
Line
Count
Source
100
6.43k
         {
101
6.43k
         static_assert(sizeof(U) > sizeof(T), "sizes ok");
102
6.43k
         }
103
104
      /**
105
      * Return a Mask<T> with all bits set
106
      */
107
      static Mask<T> set()
108
2.49k
         {
109
2.49k
         return Mask<T>(static_cast<T>(~0));
110
2.49k
         }
111
112
      /**
113
      * Return a Mask<T> with all bits cleared
114
      */
115
      static Mask<T> cleared()
116
3.22k
         {
117
3.22k
         return Mask<T>(0);
118
3.22k
         }
119
120
      /**
121
      * Return a Mask<T> which is set if v is != 0
122
      */
123
      static Mask<T> expand(T v)
124
1.26G
         {
125
1.26G
         return ~Mask<T>::is_zero(v);
126
1.26G
         }
Botan::CT::Mask<unsigned long>::expand(unsigned long)
Line
Count
Source
124
1.24G
         {
125
1.24G
         return ~Mask<T>::is_zero(v);
126
1.24G
         }
Botan::CT::Mask<unsigned char>::expand(unsigned char)
Line
Count
Source
124
19.6M
         {
125
19.6M
         return ~Mask<T>::is_zero(v);
126
19.6M
         }
Botan::CT::Mask<unsigned short>::expand(unsigned short)
Line
Count
Source
124
584
         {
125
584
         return ~Mask<T>::is_zero(v);
126
584
         }
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
196
         {
134
196
         static_assert(sizeof(U) < sizeof(T), "sizes ok");
135
196
         return ~Mask<T>::is_zero(m.value());
136
196
         }
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.12G
         {
143
4.12G
         return Mask<T>(ct_is_zero<T>(x));
144
4.12G
         }
Botan::CT::Mask<unsigned long>::is_zero(unsigned long)
Line
Count
Source
142
4.10G
         {
143
4.10G
         return Mask<T>(ct_is_zero<T>(x));
144
4.10G
         }
Botan::CT::Mask<unsigned char>::is_zero(unsigned char)
Line
Count
Source
142
21.2M
         {
143
21.2M
         return Mask<T>(ct_is_zero<T>(x));
144
21.2M
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned int>::is_zero(unsigned int)
Botan::CT::Mask<unsigned short>::is_zero(unsigned short)
Line
Count
Source
142
80.3k
         {
143
80.3k
         return Mask<T>(ct_is_zero<T>(x));
144
80.3k
         }
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.81G
         {
151
2.81G
         return Mask<T>::is_zero(static_cast<T>(x ^ y));
152
2.81G
         }
Botan::CT::Mask<unsigned long>::is_equal(unsigned long, unsigned long)
Line
Count
Source
150
2.81G
         {
151
2.81G
         return Mask<T>::is_zero(static_cast<T>(x ^ y));
152
2.81G
         }
Botan::CT::Mask<unsigned char>::is_equal(unsigned char, unsigned char)
Line
Count
Source
150
400k
         {
151
400k
         return Mask<T>::is_zero(static_cast<T>(x ^ y));
152
400k
         }
Botan::CT::Mask<unsigned short>::is_equal(unsigned short, unsigned short)
Line
Count
Source
150
79.7k
         {
151
79.7k
         return Mask<T>::is_zero(static_cast<T>(x ^ y));
152
79.7k
         }
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.20G
         {
159
1.20G
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
1.20G
         }
Botan::CT::Mask<unsigned long>::is_lt(unsigned long, unsigned long)
Line
Count
Source
158
1.20G
         {
159
1.20G
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
1.20G
         }
Botan::CT::Mask<unsigned int>::is_lt(unsigned int, unsigned int)
Line
Count
Source
158
695k
         {
159
695k
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
695k
         }
Botan::CT::Mask<unsigned short>::is_lt(unsigned short, unsigned short)
Line
Count
Source
158
80.3k
         {
159
80.3k
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
80.3k
         }
Botan::CT::Mask<unsigned char>::is_lt(unsigned char, unsigned char)
Line
Count
Source
158
139
         {
159
139
         return Mask<T>(expand_top_bit<T>(x^((x^y) | ((x-y)^x))));
160
139
         }
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
89.1k
         {
167
89.1k
         return Mask<T>::is_lt(y, x);
168
89.1k
         }
Botan::CT::Mask<unsigned long>::is_gt(unsigned long, unsigned long)
Line
Count
Source
166
9.20k
         {
167
9.20k
         return Mask<T>::is_lt(y, x);
168
9.20k
         }
Botan::CT::Mask<unsigned short>::is_gt(unsigned short, unsigned short)
Line
Count
Source
166
79.7k
         {
167
79.7k
         return Mask<T>::is_lt(y, x);
168
79.7k
         }
Botan::CT::Mask<unsigned char>::is_gt(unsigned char, unsigned char)
Line
Count
Source
166
139
         {
167
139
         return Mask<T>::is_lt(y, x);
168
139
         }
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
82.4k
         {
175
82.4k
         return ~Mask<T>::is_gt(x, y);
176
82.4k
         }
Botan::CT::Mask<unsigned long>::is_lte(unsigned long, unsigned long)
Line
Count
Source
174
2.66k
         {
175
2.66k
         return ~Mask<T>::is_gt(x, y);
176
2.66k
         }
Botan::CT::Mask<unsigned short>::is_lte(unsigned short, unsigned short)
Line
Count
Source
174
79.7k
         {
175
79.7k
         return ~Mask<T>::is_gt(x, y);
176
79.7k
         }
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
708k
         {
183
708k
         return ~Mask<T>::is_lt(x, y);
184
708k
         }
Botan::CT::Mask<unsigned int>::is_gte(unsigned int, unsigned int)
Line
Count
Source
182
695k
         {
183
695k
         return ~Mask<T>::is_lt(x, y);
184
695k
         }
Botan::CT::Mask<unsigned long>::is_gte(unsigned long, unsigned long)
Line
Count
Source
182
12.5k
         {
183
12.5k
         return ~Mask<T>::is_lt(x, y);
184
12.5k
         }
185
186
      /**
187
      * AND-combine two masks
188
      */
189
      Mask<T>& operator&=(Mask<T> o)
190
1.07M
         {
191
1.07M
         m_mask &= o.value();
192
1.07M
         return (*this);
193
1.07M
         }
Botan::CT::Mask<unsigned long>::operator&=(Botan::CT::Mask<unsigned long>)
Line
Count
Source
190
279k
         {
191
279k
         m_mask &= o.value();
192
279k
         return (*this);
193
279k
         }
Botan::CT::Mask<unsigned char>::operator&=(Botan::CT::Mask<unsigned char>)
Line
Count
Source
190
797k
         {
191
797k
         m_mask &= o.value();
192
797k
         return (*this);
193
797k
         }
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
1.04M
         {
209
1.04M
         m_mask |= o.value();
210
1.04M
         return (*this);
211
1.04M
         }
Botan::CT::Mask<unsigned long>::operator|=(Botan::CT::Mask<unsigned long>)
Line
Count
Source
208
166k
         {
209
166k
         m_mask |= o.value();
210
166k
         return (*this);
211
166k
         }
Botan::CT::Mask<unsigned char>::operator|=(Botan::CT::Mask<unsigned char>)
Line
Count
Source
208
801k
         {
209
801k
         m_mask |= o.value();
210
801k
         return (*this);
211
801k
         }
Botan::CT::Mask<unsigned short>::operator|=(Botan::CT::Mask<unsigned short>)
Line
Count
Source
208
79.5k
         {
209
79.5k
         m_mask |= o.value();
210
79.5k
         return (*this);
211
79.5k
         }
212
213
      /**
214
      * AND-combine two masks
215
      */
216
      friend Mask<T> operator&(Mask<T> x, Mask<T> y)
217
111M
         {
218
111M
         return Mask<T>(x.value() & y.value());
219
111M
         }
Botan::CT::operator&(Botan::CT::Mask<unsigned long>, Botan::CT::Mask<unsigned long>)
Line
Count
Source
217
110M
         {
218
110M
         return Mask<T>(x.value() & y.value());
219
110M
         }
Botan::CT::operator&(Botan::CT::Mask<unsigned char>, Botan::CT::Mask<unsigned char>)
Line
Count
Source
217
600k
         {
218
600k
         return Mask<T>(x.value() & y.value());
219
600k
         }
Botan::CT::operator&(Botan::CT::Mask<unsigned short>, Botan::CT::Mask<unsigned short>)
Line
Count
Source
217
80.0k
         {
218
80.0k
         return Mask<T>(x.value() & y.value());
219
80.0k
         }
220
221
      /**
222
      * XOR-combine two masks
223
      */
224
      friend Mask<T> operator^(Mask<T> x, Mask<T> y)
225
2.11M
         {
226
2.11M
         return Mask<T>(x.value() ^ y.value());
227
2.11M
         }
228
229
      /**
230
      * OR-combine two masks
231
      */
232
      friend Mask<T> operator|(Mask<T> x, Mask<T> y)
233
110M
         {
234
110M
         return Mask<T>(x.value() | y.value());
235
110M
         }
Botan::CT::operator|(Botan::CT::Mask<unsigned long>, Botan::CT::Mask<unsigned long>)
Line
Count
Source
233
110M
         {
234
110M
         return Mask<T>(x.value() | y.value());
235
110M
         }
Botan::CT::operator|(Botan::CT::Mask<unsigned char>, Botan::CT::Mask<unsigned char>)
Line
Count
Source
233
200k
         {
234
200k
         return Mask<T>(x.value() | y.value());
235
200k
         }
236
237
      /**
238
      * Negate this mask
239
      */
240
      Mask<T> operator~() const
241
1.26G
         {
242
1.26G
         return Mask<T>(~value());
243
1.26G
         }
Botan::CT::Mask<unsigned long>::operator~() const
Line
Count
Source
241
1.24G
         {
242
1.24G
         return Mask<T>(~value());
243
1.24G
         }
Botan::CT::Mask<unsigned char>::operator~() const
Line
Count
Source
241
20.2M
         {
242
20.2M
         return Mask<T>(~value());
243
20.2M
         }
Botan::CT::Mask<unsigned int>::operator~() const
Line
Count
Source
241
695k
         {
242
695k
         return Mask<T>(~value());
243
695k
         }
Botan::CT::Mask<unsigned short>::operator~() const
Line
Count
Source
241
159k
         {
242
159k
         return Mask<T>(~value());
243
159k
         }
244
245
      /**
246
      * Return x if the mask is set, or otherwise zero
247
      */
248
      T if_set_return(T x) const
249
7.10G
         {
250
7.10G
         return m_mask & x;
251
7.10G
         }
Botan::CT::Mask<unsigned long>::if_set_return(unsigned long) const
Line
Count
Source
249
7.10G
         {
250
7.10G
         return m_mask & x;
251
7.10G
         }
Botan::CT::Mask<unsigned char>::if_set_return(unsigned char) const
Line
Count
Source
249
797k
         {
250
797k
         return m_mask & x;
251
797k
         }
Botan::CT::Mask<unsigned short>::if_set_return(unsigned short) const
Line
Count
Source
249
584
         {
250
584
         return m_mask & x;
251
584
         }
252
253
      /**
254
      * Return x if the mask is cleared, or otherwise zero
255
      */
256
      T if_not_set_return(T x) const
257
1.40M
         {
258
1.40M
         return ~m_mask & x;
259
1.40M
         }
Botan::CT::Mask<unsigned char>::if_not_set_return(unsigned char) const
Line
Count
Source
257
1.40M
         {
258
1.40M
         return ~m_mask & x;
259
1.40M
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned long>::if_not_set_return(unsigned long) const
Botan::CT::Mask<unsigned short>::if_not_set_return(unsigned short) const
Line
Count
Source
257
516
         {
258
516
         return ~m_mask & x;
259
516
         }
260
261
      /**
262
      * If this mask is set, return x, otherwise return y
263
      */
264
      T select(T x, T y) const
265
22.7G
         {
266
22.7G
         // (x & value()) | (y & ~value())
267
22.7G
         return static_cast<T>(y ^ (value() & (x ^ y)));
268
22.7G
         }
Botan::CT::Mask<unsigned long>::select(unsigned long, unsigned long) const
Line
Count
Source
265
22.7G
         {
266
22.7G
         // (x & value()) | (y & ~value())
267
22.7G
         return static_cast<T>(y ^ (value() & (x ^ y)));
268
22.7G
         }
Botan::CT::Mask<unsigned char>::select(unsigned char, unsigned char) const
Line
Count
Source
265
19.6M
         {
266
19.6M
         // (x & value()) | (y & ~value())
267
19.6M
         return static_cast<T>(y ^ (value() & (x ^ y)));
268
19.6M
         }
Botan::CT::Mask<unsigned int>::select(unsigned int, unsigned int) const
Line
Count
Source
265
695k
         {
266
695k
         // (x & value()) | (y & ~value())
267
695k
         return static_cast<T>(y ^ (value() & (x ^ y)));
268
695k
         }
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
613
         {
272
613
         T r = this->select(x, y);
273
613
         CT::unpoison(r);
274
613
         return r;
275
613
         }
Botan::CT::Mask<unsigned long>::select_and_unpoison(unsigned long, unsigned long) const
Line
Count
Source
271
474
         {
272
474
         T r = this->select(x, y);
273
474
         CT::unpoison(r);
274
474
         return r;
275
474
         }
Botan::CT::Mask<unsigned char>::select_and_unpoison(unsigned char, unsigned char) const
Line
Count
Source
271
139
         {
272
139
         T r = this->select(x, y);
273
139
         CT::unpoison(r);
274
139
         return r;
275
139
         }
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
762M
         {
282
762M
         return Mask<T>(select(x.value(), y.value()));
283
762M
         }
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
492M
         {
291
4.71G
         for(size_t i = 0; i != len; ++i)
292
4.21G
            output[i] = this->select(x[i], y[i]);
293
492M
         }
Botan::CT::Mask<unsigned long>::select_n(unsigned long*, unsigned long const*, unsigned long const*, unsigned long) const
Line
Count
Source
290
492M
         {
291
4.71G
         for(size_t i = 0; i != len; ++i)
292
4.21G
            output[i] = this->select(x[i], y[i]);
293
492M
         }
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
2.66k
         {
300
1.00M
         for(size_t i = 0; i != elems; ++i)
301
1.00M
            {
302
1.00M
            buf[i] = this->if_not_set_return(buf[i]);
303
1.00M
            }
304
2.66k
         }
305
306
      /**
307
      * Return the value of the mask, unpoisoned
308
      */
309
      T unpoisoned_value() const
310
12.5M
         {
311
12.5M
         T r = value();
312
12.5M
         CT::unpoison(r);
313
12.5M
         return r;
314
12.5M
         }
Botan::CT::Mask<unsigned long>::unpoisoned_value() const
Line
Count
Source
310
11.8M
         {
311
11.8M
         T r = value();
312
11.8M
         CT::unpoison(r);
313
11.8M
         return r;
314
11.8M
         }
Botan::CT::Mask<unsigned int>::unpoisoned_value() const
Line
Count
Source
310
695k
         {
311
695k
         T r = value();
312
695k
         CT::unpoison(r);
313
695k
         return r;
314
695k
         }
Botan::CT::Mask<unsigned char>::unpoisoned_value() const
Line
Count
Source
310
370
         {
311
370
         T r = value();
312
370
         CT::unpoison(r);
313
370
         return r;
314
370
         }
Botan::CT::Mask<unsigned short>::unpoisoned_value() const
Line
Count
Source
310
292
         {
311
292
         T r = value();
312
292
         CT::unpoison(r);
313
292
         return r;
314
292
         }
315
316
      /**
317
      * Return true iff this mask is set
318
      */
319
      bool is_set() const
320
12.5M
         {
321
12.5M
         return unpoisoned_value() != 0;
322
12.5M
         }
Botan::CT::Mask<unsigned long>::is_set() const
Line
Count
Source
320
11.8M
         {
321
11.8M
         return unpoisoned_value() != 0;
322
11.8M
         }
Botan::CT::Mask<unsigned int>::is_set() const
Line
Count
Source
320
695k
         {
321
695k
         return unpoisoned_value() != 0;
322
695k
         }
Unexecuted instantiation: Botan::CT::Mask<unsigned char>::is_set() const
Botan::CT::Mask<unsigned short>::is_set() const
Line
Count
Source
320
292
         {
321
292
         return unpoisoned_value() != 0;
322
292
         }
323
324
      /**
325
      * Return the underlying value of the mask
326
      */
327
      T value() const
328
26.1G
         {
329
26.1G
         return m_mask;
330
26.1G
         }
Botan::CT::Mask<unsigned long>::value() const
Line
Count
Source
328
26.1G
         {
329
26.1G
         return m_mask;
330
26.1G
         }
Botan::CT::Mask<unsigned char>::value() const
Line
Count
Source
328
43.0M
         {
329
43.0M
         return m_mask;
330
43.0M
         }
Botan::CT::Mask<unsigned int>::value() const
Line
Count
Source
328
2.08M
         {
329
2.08M
         return m_mask;
330
2.08M
         }
Botan::CT::Mask<unsigned short>::value() const
Line
Count
Source
328
399k
         {
329
399k
         return m_mask;
330
399k
         }
331
332
   private:
333
7.58G
      Mask(T m) : m_mask(m) {}
Botan::CT::Mask<unsigned long>::Mask(unsigned long)
Line
Count
Source
333
7.53G
      Mask(T m) : m_mask(m) {}
Botan::CT::Mask<unsigned char>::Mask(unsigned char)
Line
Count
Source
333
42.2M
      Mask(T m) : m_mask(m) {}
Botan::CT::Mask<unsigned int>::Mask(unsigned int)
Line
Count
Source
333
1.39M
      Mask(T m) : m_mask(m) {}
Botan::CT::Mask<unsigned short>::Mask(unsigned short)
Line
Count
Source
333
400k
      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
81.8M
   {
345
81.8M
   const auto mask = CT::Mask<T>::expand(cnd);
346
81.8M
   mask.select_n(to, from0, from1, elems);
347
81.8M
   return mask;
348
81.8M
   }
349
350
template<typename T>
351
inline void conditional_swap(bool cnd, T& x, T& y)
352
31.8M
   {
353
31.8M
   const auto swap = CT::Mask<T>::expand(cnd);
354
31.8M
355
31.8M
   T t0 = swap.select(y, x);
356
31.8M
   T t1 = swap.select(x, y);
357
31.8M
   x = t0;
358
31.8M
   y = t1;
359
31.8M
   }
360
361
template<typename T>
362
inline void conditional_swap_ptr(bool cnd, T& x, T& y)
363
15.9M
   {
364
15.9M
   uintptr_t xp = reinterpret_cast<uintptr_t>(x);
365
15.9M
   uintptr_t yp = reinterpret_cast<uintptr_t>(y);
366
15.9M
367
15.9M
   conditional_swap<uintptr_t>(cnd, xp, yp);
368
15.9M
369
15.9M
   x = reinterpret_cast<T>(xp);
370
15.9M
   y = reinterpret_cast<T>(yp);
371
15.9M
   }
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.29k
   {
387
2.29k
   return strip_leading_zeros(in.data(), in.size());
388
2.29k
   }
389
390
}
391
392
}
393
394
#endif