Coverage Report

Created: 2020-06-30 13:58

/src/botan/build/include/botan/mem_ops.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Memory Operations
3
* (C) 1999-2009,2012,2015 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_MEMORY_OPS_H_
9
#define BOTAN_MEMORY_OPS_H_
10
11
#include <botan/types.h>
12
#include <cstring>
13
#include <type_traits>
14
#include <vector>
15
16
namespace Botan {
17
18
/**
19
* Allocate a memory buffer by some method. This should only be used for
20
* primitive types (uint8_t, uint32_t, etc).
21
*
22
* @param elems the number of elements
23
* @param elem_size the size of each element
24
* @return pointer to allocated and zeroed memory, or throw std::bad_alloc on failure
25
*/
26
BOTAN_PUBLIC_API(2,3) BOTAN_MALLOC_FN void* allocate_memory(size_t elems, size_t elem_size);
27
28
/**
29
* Free a pointer returned by allocate_memory
30
* @param p the pointer returned by allocate_memory
31
* @param elems the number of elements, as passed to allocate_memory
32
* @param elem_size the size of each element, as passed to allocate_memory
33
*/
34
BOTAN_PUBLIC_API(2,3) void deallocate_memory(void* p, size_t elems, size_t elem_size);
35
36
/**
37
* Ensure the allocator is initialized
38
*/
39
void BOTAN_UNSTABLE_API initialize_allocator();
40
41
class Allocator_Initializer
42
   {
43
   public:
44
9
      Allocator_Initializer() { initialize_allocator(); }
45
   };
46
47
/**
48
* Scrub memory contents in a way that a compiler should not elide,
49
* using some system specific technique. Note that this function might
50
* not zero the memory (for example, in some hypothetical
51
* implementation it might combine the memory contents with the output
52
* of a system PRNG), but if you can detect any difference in behavior
53
* at runtime then the clearing is side-effecting and you can just
54
* use `clear_mem`.
55
*
56
* Use this function to scrub memory just before deallocating it, or on
57
* a stack buffer before returning from the function.
58
*
59
* @param ptr a pointer to memory to scrub
60
* @param n the number of bytes pointed to by ptr
61
*/
62
BOTAN_PUBLIC_API(2,0) void secure_scrub_memory(void* ptr, size_t n);
63
64
/**
65
* Memory comparison, input insensitive
66
* @param x a pointer to an array
67
* @param y a pointer to another array
68
* @param len the number of Ts in x and y
69
* @return 0xFF iff x[i] == y[i] forall i in [0...n) or 0x00 otherwise
70
*/
71
BOTAN_PUBLIC_API(2,9) uint8_t ct_compare_u8(const uint8_t x[],
72
                                            const uint8_t y[],
73
                                            size_t len);
74
75
/**
76
* Memory comparison, input insensitive
77
* @param x a pointer to an array
78
* @param y a pointer to another array
79
* @param len the number of Ts in x and y
80
* @return true iff x[i] == y[i] forall i in [0...n)
81
*/
82
inline bool constant_time_compare(const uint8_t x[],
83
                                  const uint8_t y[],
84
                                  size_t len)
85
952
   {
86
952
   return ct_compare_u8(x, y, len) == 0xFF;
87
952
   }
88
89
/**
90
* Zero out some bytes. Warning: use secure_scrub_memory instead if the
91
* memory is about to be freed or otherwise the compiler thinks it can
92
* elide the writes.
93
*
94
* @param ptr a pointer to memory to zero
95
* @param bytes the number of bytes to zero in ptr
96
*/
97
inline void clear_bytes(void* ptr, size_t bytes)
98
1.05G
   {
99
1.05G
   if(bytes > 0)
100
932M
      {
101
932M
      std::memset(ptr, 0, bytes);
102
932M
      }
103
1.05G
   }
104
105
/**
106
* Zero memory before use. This simply calls memset and should not be
107
* used in cases where the compiler cannot see the call as a
108
* side-effecting operation (for example, if calling clear_mem before
109
* deallocating memory, the compiler would be allowed to omit the call
110
* to memset entirely under the as-if rule.)
111
*
112
* @param ptr a pointer to an array of Ts to zero
113
* @param n the number of Ts pointed to by ptr
114
*/
115
template<typename T> inline void clear_mem(T* ptr, size_t n)
116
1.05G
   {
117
1.05G
   clear_bytes(ptr, sizeof(T)*n);
118
1.05G
   }
void Botan::clear_mem<unsigned char>(unsigned char*, unsigned long)
Line
Count
Source
116
1.24M
   {
117
1.24M
   clear_bytes(ptr, sizeof(T)*n);
118
1.24M
   }
void Botan::clear_mem<unsigned long>(unsigned long*, unsigned long)
Line
Count
Source
116
1.05G
   {
117
1.05G
   clear_bytes(ptr, sizeof(T)*n);
118
1.05G
   }
void Botan::clear_mem<unsigned int>(unsigned int*, unsigned long)
Line
Count
Source
116
51.1k
   {
117
51.1k
   clear_bytes(ptr, sizeof(T)*n);
118
51.1k
   }
Unexecuted instantiation: void Botan::clear_mem<unsigned short>(unsigned short*, unsigned long)
void Botan::clear_mem<int>(int*, unsigned long)
Line
Count
Source
116
165k
   {
117
165k
   clear_bytes(ptr, sizeof(T)*n);
118
165k
   }
Unexecuted instantiation: void Botan::clear_mem<addrinfo>(addrinfo*, unsigned long)
119
120
// is_trivially_copyable is missing in g++ < 5.0
121
#if (BOTAN_GCC_VERSION > 0 && BOTAN_GCC_VERSION < 500)
122
#define BOTAN_IS_TRIVIALLY_COPYABLE(T) true
123
#else
124
#define BOTAN_IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
125
#endif
126
127
/**
128
* Copy memory
129
* @param out the destination array
130
* @param in the source array
131
* @param n the number of elements of in/out
132
*/
133
template<typename T> inline void copy_mem(T* out, const T* in, size_t n)
134
320M
   {
135
320M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
136
320M
   BOTAN_ASSERT_IMPLICATION(n > 0, in != nullptr && out != nullptr,
137
320M
                            "If n > 0 then args are not null");
138
320M
139
320M
   if(in != nullptr && out != nullptr && n > 0)
140
320M
      {
141
320M
      std::memmove(out, in, sizeof(T)*n);
142
320M
      }
143
320M
   }
void Botan::copy_mem<unsigned long>(unsigned long*, unsigned long const*, unsigned long)
Line
Count
Source
134
288M
   {
135
288M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
136
288M
   BOTAN_ASSERT_IMPLICATION(n > 0, in != nullptr && out != nullptr,
137
288M
                            "If n > 0 then args are not null");
138
288M
139
288M
   if(in != nullptr && out != nullptr && n > 0)
140
287M
      {
141
287M
      std::memmove(out, in, sizeof(T)*n);
142
287M
      }
143
288M
   }
void Botan::copy_mem<unsigned char>(unsigned char*, unsigned char const*, unsigned long)
Line
Count
Source
134
32.7M
   {
135
32.7M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
136
32.7M
   BOTAN_ASSERT_IMPLICATION(n > 0, in != nullptr && out != nullptr,
137
32.7M
                            "If n > 0 then args are not null");
138
32.7M
139
32.7M
   if(in != nullptr && out != nullptr && n > 0)
140
32.2M
      {
141
32.2M
      std::memmove(out, in, sizeof(T)*n);
142
32.2M
      }
143
32.7M
   }
Unexecuted instantiation: void Botan::copy_mem<unsigned short>(unsigned short*, unsigned short const*, unsigned long)
Unexecuted instantiation: void Botan::copy_mem<unsigned int>(unsigned int*, unsigned int const*, unsigned long)
void Botan::copy_mem<int>(int*, int const*, unsigned long)
Line
Count
Source
134
9.24k
   {
135
9.24k
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
136
9.24k
   BOTAN_ASSERT_IMPLICATION(n > 0, in != nullptr && out != nullptr,
137
9.24k
                            "If n > 0 then args are not null");
138
9.24k
139
9.24k
   if(in != nullptr && out != nullptr && n > 0)
140
9.24k
      {
141
9.24k
      std::memmove(out, in, sizeof(T)*n);
142
9.24k
      }
143
9.24k
   }
144
145
template<typename T> inline void typecast_copy(uint8_t out[], T in[], size_t N)
146
5.05M
   {
147
5.05M
   static_assert(BOTAN_IS_TRIVIALLY_COPYABLE(T), "");
148
5.05M
   std::memcpy(out, in, sizeof(T)*N);
149
5.05M
   }
void Botan::typecast_copy<unsigned long>(unsigned char*, unsigned long*, unsigned long)
Line
Count
Source
146
2.26M
   {
147
2.26M
   static_assert(BOTAN_IS_TRIVIALLY_COPYABLE(T), "");
148
2.26M
   std::memcpy(out, in, sizeof(T)*N);
149
2.26M
   }
void Botan::typecast_copy<unsigned short>(unsigned char*, unsigned short*, unsigned long)
Line
Count
Source
146
13
   {
147
13
   static_assert(BOTAN_IS_TRIVIALLY_COPYABLE(T), "");
148
13
   std::memcpy(out, in, sizeof(T)*N);
149
13
   }
void Botan::typecast_copy<unsigned int>(unsigned char*, unsigned int*, unsigned long)
Line
Count
Source
146
2.79M
   {
147
2.79M
   static_assert(BOTAN_IS_TRIVIALLY_COPYABLE(T), "");
148
2.79M
   std::memcpy(out, in, sizeof(T)*N);
149
2.79M
   }
150
151
template<typename T> inline void typecast_copy(T out[], const uint8_t in[], size_t N)
152
21.5M
   {
153
21.5M
   static_assert(std::is_trivial<T>::value, "");
154
21.5M
   std::memcpy(out, in, sizeof(T)*N);
155
21.5M
   }
void Botan::typecast_copy<unsigned long>(unsigned long*, unsigned char const*, unsigned long)
Line
Count
Source
152
7.68M
   {
153
7.68M
   static_assert(std::is_trivial<T>::value, "");
154
7.68M
   std::memcpy(out, in, sizeof(T)*N);
155
7.68M
   }
void Botan::typecast_copy<unsigned short>(unsigned short*, unsigned char const*, unsigned long)
Line
Count
Source
152
135k
   {
153
135k
   static_assert(std::is_trivial<T>::value, "");
154
135k
   std::memcpy(out, in, sizeof(T)*N);
155
135k
   }
void Botan::typecast_copy<unsigned int>(unsigned int*, unsigned char const*, unsigned long)
Line
Count
Source
152
13.7M
   {
153
13.7M
   static_assert(std::is_trivial<T>::value, "");
154
13.7M
   std::memcpy(out, in, sizeof(T)*N);
155
13.7M
   }
156
157
template<typename T> inline void typecast_copy(uint8_t out[], T in)
158
4.96M
   {
159
4.96M
   typecast_copy(out, &in, 1);
160
4.96M
   }
void Botan::typecast_copy<unsigned long>(unsigned char*, unsigned long)
Line
Count
Source
158
2.17M
   {
159
2.17M
   typecast_copy(out, &in, 1);
160
2.17M
   }
void Botan::typecast_copy<unsigned short>(unsigned char*, unsigned short)
Line
Count
Source
158
13
   {
159
13
   typecast_copy(out, &in, 1);
160
13
   }
void Botan::typecast_copy<unsigned int>(unsigned char*, unsigned int)
Line
Count
Source
158
2.79M
   {
159
2.79M
   typecast_copy(out, &in, 1);
160
2.79M
   }
161
162
template<typename T> inline void typecast_copy(T& out, const uint8_t in[])
163
21.3M
   {
164
21.3M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
165
21.3M
   typecast_copy(&out, in, 1);
166
21.3M
   }
void Botan::typecast_copy<unsigned long>(unsigned long&, unsigned char const*)
Line
Count
Source
163
7.49M
   {
164
7.49M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
165
7.49M
   typecast_copy(&out, in, 1);
166
7.49M
   }
void Botan::typecast_copy<unsigned short>(unsigned short&, unsigned char const*)
Line
Count
Source
163
135k
   {
164
135k
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
165
135k
   typecast_copy(&out, in, 1);
166
135k
   }
void Botan::typecast_copy<unsigned int>(unsigned int&, unsigned char const*)
Line
Count
Source
163
13.7M
   {
164
13.7M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
165
13.7M
   typecast_copy(&out, in, 1);
166
13.7M
   }
167
168
template <class To, class From> inline To typecast_copy(const From *src) noexcept
169
   {
170
   static_assert(BOTAN_IS_TRIVIALLY_COPYABLE(From) && std::is_trivial<To>::value, "");
171
   To dst;
172
   std::memcpy(&dst, src, sizeof(To));
173
   return dst;
174
   }
175
176
/**
177
* Set memory to a fixed value
178
* @param ptr a pointer to an array of bytes
179
* @param n the number of Ts pointed to by ptr
180
* @param val the value to set each byte to
181
*/
182
inline void set_mem(uint8_t* ptr, size_t n, uint8_t val)
183
6.00k
   {
184
6.00k
   if(n > 0)
185
6.00k
      {
186
6.00k
      std::memset(ptr, val, n);
187
6.00k
      }
188
6.00k
   }
189
190
inline const uint8_t* cast_char_ptr_to_uint8(const char* s)
191
95.5k
   {
192
95.5k
   return reinterpret_cast<const uint8_t*>(s);
193
95.5k
   }
194
195
inline const char* cast_uint8_ptr_to_char(const uint8_t* b)
196
196k
   {
197
196k
   return reinterpret_cast<const char*>(b);
198
196k
   }
199
200
inline uint8_t* cast_char_ptr_to_uint8(char* s)
201
0
   {
202
0
   return reinterpret_cast<uint8_t*>(s);
203
0
   }
204
205
inline char* cast_uint8_ptr_to_char(uint8_t* b)
206
44.6k
   {
207
44.6k
   return reinterpret_cast<char*>(b);
208
44.6k
   }
209
210
/**
211
* Memory comparison, input insensitive
212
* @param p1 a pointer to an array
213
* @param p2 a pointer to another array
214
* @param n the number of Ts in p1 and p2
215
* @return true iff p1[i] == p2[i] forall i in [0...n)
216
*/
217
template<typename T> inline bool same_mem(const T* p1, const T* p2, size_t n)
218
686
   {
219
686
   volatile T difference = 0;
220
686
221
29.9k
   for(size_t i = 0; i != n; ++i)
222
29.2k
      difference |= (p1[i] ^ p2[i]);
223
686
224
686
   return difference == 0;
225
686
   }
bool Botan::same_mem<unsigned int>(unsigned int const*, unsigned int const*, unsigned long)
Line
Count
Source
218
20
   {
219
20
   volatile T difference = 0;
220
20
221
80
   for(size_t i = 0; i != n; ++i)
222
60
      difference |= (p1[i] ^ p2[i]);
223
20
224
20
   return difference == 0;
225
20
   }
bool Botan::same_mem<unsigned char>(unsigned char const*, unsigned char const*, unsigned long)
Line
Count
Source
218
666
   {
219
666
   volatile T difference = 0;
220
666
221
29.8k
   for(size_t i = 0; i != n; ++i)
222
29.1k
      difference |= (p1[i] ^ p2[i]);
223
666
224
666
   return difference == 0;
225
666
   }
226
227
/**
228
* XOR arrays. Postcondition out[i] = in[i] ^ out[i] forall i = 0...length
229
* @param out the input/output buffer
230
* @param in the read-only input buffer
231
* @param length the length of the buffers
232
*/
233
inline void xor_buf(uint8_t out[],
234
                    const uint8_t in[],
235
                    size_t length)
236
127k
   {
237
127k
   const size_t blocks = length - (length % 32);
238
127k
239
203k
   for(size_t i = 0; i != blocks; i += 32)
240
75.7k
      {
241
75.7k
      uint64_t x[4];
242
75.7k
      uint64_t y[4];
243
75.7k
244
75.7k
      typecast_copy(x, out + i, 4);
245
75.7k
      typecast_copy(y, in + i, 4);
246
75.7k
247
75.7k
      x[0] ^= y[0];
248
75.7k
      x[1] ^= y[1];
249
75.7k
      x[2] ^= y[2];
250
75.7k
      x[3] ^= y[3];
251
75.7k
252
75.7k
      typecast_copy(out + i, x, 4);
253
75.7k
      }
254
127k
255
1.57M
   for(size_t i = blocks; i != length; ++i)
256
1.45M
      {
257
1.45M
      out[i] ^= in[i];
258
1.45M
      }
259
127k
   }
260
261
/**
262
* XOR arrays. Postcondition out[i] = in[i] ^ in2[i] forall i = 0...length
263
* @param out the output buffer
264
* @param in the first input buffer
265
* @param in2 the second output buffer
266
* @param length the length of the three buffers
267
*/
268
inline void xor_buf(uint8_t out[],
269
                    const uint8_t in[],
270
                    const uint8_t in2[],
271
                    size_t length)
272
18.6k
   {
273
18.6k
   const size_t blocks = length - (length % 32);
274
18.6k
275
33.8k
   for(size_t i = 0; i != blocks; i += 32)
276
15.1k
      {
277
15.1k
      uint64_t x[4];
278
15.1k
      uint64_t y[4];
279
15.1k
280
15.1k
      typecast_copy(x, in + i, 4);
281
15.1k
      typecast_copy(y, in2 + i, 4);
282
15.1k
283
15.1k
      x[0] ^= y[0];
284
15.1k
      x[1] ^= y[1];
285
15.1k
      x[2] ^= y[2];
286
15.1k
      x[3] ^= y[3];
287
15.1k
288
15.1k
      typecast_copy(out + i, x, 4);
289
15.1k
      }
290
18.6k
291
215k
   for(size_t i = blocks; i != length; ++i)
292
196k
      {
293
196k
      out[i] = in[i] ^ in2[i];
294
196k
      }
295
18.6k
   }
296
297
template<typename Alloc, typename Alloc2>
298
void xor_buf(std::vector<uint8_t, Alloc>& out,
299
             const std::vector<uint8_t, Alloc2>& in,
300
             size_t n)
301
0
   {
302
0
   xor_buf(out.data(), in.data(), n);
303
0
   }
304
305
template<typename Alloc>
306
void xor_buf(std::vector<uint8_t, Alloc>& out,
307
             const uint8_t* in,
308
             size_t n)
309
831
   {
310
831
   xor_buf(out.data(), in, n);
311
831
   }
Unexecuted instantiation: void Botan::xor_buf<Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned char const*, unsigned long)
void Botan::xor_buf<std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, unsigned char const*, unsigned long)
Line
Count
Source
309
831
   {
310
831
   xor_buf(out.data(), in, n);
311
831
   }
312
313
template<typename Alloc, typename Alloc2>
314
void xor_buf(std::vector<uint8_t, Alloc>& out,
315
             const uint8_t* in,
316
             const std::vector<uint8_t, Alloc2>& in2,
317
             size_t n)
318
   {
319
   xor_buf(out.data(), in, in2.data(), n);
320
   }
321
322
template<typename Alloc, typename Alloc2>
323
std::vector<uint8_t, Alloc>&
324
operator^=(std::vector<uint8_t, Alloc>& out,
325
           const std::vector<uint8_t, Alloc2>& in)
326
2.50k
   {
327
2.50k
   if(out.size() < in.size())
328
0
      out.resize(in.size());
329
2.50k
330
2.50k
   xor_buf(out.data(), in.data(), in.size());
331
2.50k
   return out;
332
2.50k
   }
333
334
}
335
336
#endif