Coverage Report

Created: 2022-01-14 08:07

/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
8
      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
277
   {
86
277
   return ct_compare_u8(x, y, len) == 0xFF;
87
277
   }
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 constexpr void clear_bytes(void* ptr, size_t bytes)
98
1.34G
   {
99
1.34G
   if(bytes > 0)
100
1.20G
      {
101
1.20G
      std::memset(ptr, 0, bytes);
102
1.20G
      }
103
1.34G
   }
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 constexpr void clear_mem(T* ptr, size_t n)
116
1.34G
   {
117
1.34G
   clear_bytes(ptr, sizeof(T)*n);
118
1.34G
   }
void Botan::clear_mem<unsigned char>(unsigned char*, unsigned long)
Line
Count
Source
116
1.20M
   {
117
1.20M
   clear_bytes(ptr, sizeof(T)*n);
118
1.20M
   }
void Botan::clear_mem<unsigned long>(unsigned long*, unsigned long)
Line
Count
Source
116
1.34G
   {
117
1.34G
   clear_bytes(ptr, sizeof(T)*n);
118
1.34G
   }
void Botan::clear_mem<unsigned int>(unsigned int*, unsigned long)
Line
Count
Source
116
46.5k
   {
117
46.5k
   clear_bytes(ptr, sizeof(T)*n);
118
46.5k
   }
Unexecuted instantiation: void Botan::clear_mem<unsigned short>(unsigned short*, unsigned long)
Unexecuted instantiation: void Botan::clear_mem<addrinfo>(addrinfo*, unsigned long)
void Botan::clear_mem<int>(int*, unsigned long)
Line
Count
Source
116
2.65k
   {
117
2.65k
   clear_bytes(ptr, sizeof(T)*n);
118
2.65k
   }
119
120
/**
121
* Copy memory
122
* @param out the destination array
123
* @param in the source array
124
* @param n the number of elements of in/out
125
*/
126
template<typename T> inline constexpr void copy_mem(T* out, const T* in, size_t n)
127
371M
   {
128
371M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
129
371M
   BOTAN_ASSERT_IMPLICATION(n > 0, in != nullptr && out != nullptr,
130
371M
                            "If n > 0 then args are not null");
131
132
371M
   if(in != nullptr && out != nullptr && n > 0)
133
371M
      {
134
371M
      std::memmove(out, in, sizeof(T)*n);
135
371M
      }
136
371M
   }
void Botan::copy_mem<unsigned long>(unsigned long*, unsigned long const*, unsigned long)
Line
Count
Source
127
335M
   {
128
335M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
129
335M
   BOTAN_ASSERT_IMPLICATION(n > 0, in != nullptr && out != nullptr,
130
335M
                            "If n > 0 then args are not null");
131
132
335M
   if(in != nullptr && out != nullptr && n > 0)
133
335M
      {
134
335M
      std::memmove(out, in, sizeof(T)*n);
135
335M
      }
136
335M
   }
void Botan::copy_mem<unsigned char>(unsigned char*, unsigned char const*, unsigned long)
Line
Count
Source
127
36.7M
   {
128
36.7M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
129
36.7M
   BOTAN_ASSERT_IMPLICATION(n > 0, in != nullptr && out != nullptr,
130
36.7M
                            "If n > 0 then args are not null");
131
132
36.7M
   if(in != nullptr && out != nullptr && n > 0)
133
36.1M
      {
134
36.1M
      std::memmove(out, in, sizeof(T)*n);
135
36.1M
      }
136
36.7M
   }
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
127
9.21k
   {
128
9.21k
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "");
129
9.21k
   BOTAN_ASSERT_IMPLICATION(n > 0, in != nullptr && out != nullptr,
130
9.21k
                            "If n > 0 then args are not null");
131
132
9.21k
   if(in != nullptr && out != nullptr && n > 0)
133
9.21k
      {
134
9.21k
      std::memmove(out, in, sizeof(T)*n);
135
9.21k
      }
136
9.21k
   }
Unexecuted instantiation: void Botan::copy_mem<unsigned short>(unsigned short*, unsigned short const*, unsigned long)
137
138
template<typename T> inline constexpr void typecast_copy(uint8_t out[], T in[], size_t N)
139
4.54M
   {
140
4.54M
   static_assert(std::is_trivially_copyable<T>::value, "Safe to memcpy");
141
4.54M
   std::memcpy(out, in, sizeof(T)*N);
142
4.54M
   }
void Botan::typecast_copy<unsigned long>(unsigned char*, unsigned long*, unsigned long)
Line
Count
Source
139
1.60M
   {
140
1.60M
   static_assert(std::is_trivially_copyable<T>::value, "Safe to memcpy");
141
1.60M
   std::memcpy(out, in, sizeof(T)*N);
142
1.60M
   }
void Botan::typecast_copy<unsigned int>(unsigned char*, unsigned int*, unsigned long)
Line
Count
Source
139
2.94M
   {
140
2.94M
   static_assert(std::is_trivially_copyable<T>::value, "Safe to memcpy");
141
2.94M
   std::memcpy(out, in, sizeof(T)*N);
142
2.94M
   }
void Botan::typecast_copy<unsigned short>(unsigned char*, unsigned short*, unsigned long)
Line
Count
Source
139
59
   {
140
59
   static_assert(std::is_trivially_copyable<T>::value, "Safe to memcpy");
141
59
   std::memcpy(out, in, sizeof(T)*N);
142
59
   }
143
144
template<typename T> inline constexpr void typecast_copy(T out[], const uint8_t in[], size_t N)
145
23.0M
   {
146
23.0M
   static_assert(std::is_trivial<T>::value, "Safe to memcpy");
147
23.0M
   std::memcpy(out, in, sizeof(T)*N);
148
23.0M
   }
void Botan::typecast_copy<unsigned long>(unsigned long*, unsigned char const*, unsigned long)
Line
Count
Source
145
7.78M
   {
146
7.78M
   static_assert(std::is_trivial<T>::value, "Safe to memcpy");
147
7.78M
   std::memcpy(out, in, sizeof(T)*N);
148
7.78M
   }
void Botan::typecast_copy<unsigned short>(unsigned short*, unsigned char const*, unsigned long)
Line
Count
Source
145
119k
   {
146
119k
   static_assert(std::is_trivial<T>::value, "Safe to memcpy");
147
119k
   std::memcpy(out, in, sizeof(T)*N);
148
119k
   }
void Botan::typecast_copy<unsigned int>(unsigned int*, unsigned char const*, unsigned long)
Line
Count
Source
145
15.1M
   {
146
15.1M
   static_assert(std::is_trivial<T>::value, "Safe to memcpy");
147
15.1M
   std::memcpy(out, in, sizeof(T)*N);
148
15.1M
   }
149
150
template<typename T> inline constexpr void typecast_copy(uint8_t out[], T in)
151
4.47M
   {
152
4.47M
   typecast_copy(out, &in, 1);
153
4.47M
   }
void Botan::typecast_copy<unsigned long>(unsigned char*, unsigned long)
Line
Count
Source
151
1.53M
   {
152
1.53M
   typecast_copy(out, &in, 1);
153
1.53M
   }
void Botan::typecast_copy<unsigned int>(unsigned char*, unsigned int)
Line
Count
Source
151
2.94M
   {
152
2.94M
   typecast_copy(out, &in, 1);
153
2.94M
   }
void Botan::typecast_copy<unsigned short>(unsigned char*, unsigned short)
Line
Count
Source
151
59
   {
152
59
   typecast_copy(out, &in, 1);
153
59
   }
154
155
template<typename T> inline constexpr void typecast_copy(T& out, const uint8_t in[])
156
22.9M
   {
157
22.9M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "Safe case");
158
22.9M
   typecast_copy(&out, in, 1);
159
22.9M
   }
void Botan::typecast_copy<unsigned long>(unsigned long&, unsigned char const*)
Line
Count
Source
156
7.63M
   {
157
7.63M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "Safe case");
158
7.63M
   typecast_copy(&out, in, 1);
159
7.63M
   }
void Botan::typecast_copy<unsigned short>(unsigned short&, unsigned char const*)
Line
Count
Source
156
119k
   {
157
119k
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "Safe case");
158
119k
   typecast_copy(&out, in, 1);
159
119k
   }
void Botan::typecast_copy<unsigned int>(unsigned int&, unsigned char const*)
Line
Count
Source
156
15.1M
   {
157
15.1M
   static_assert(std::is_trivial<typename std::decay<T>::type>::value, "Safe case");
158
15.1M
   typecast_copy(&out, in, 1);
159
15.1M
   }
160
161
template <class To, class From> inline constexpr To typecast_copy(const From *src) noexcept
162
   {
163
   static_assert(std::is_trivially_copyable<From>::value && std::is_trivial<To>::value, "Safe for memcpy");
164
   To dst;
165
   std::memcpy(&dst, src, sizeof(To));
166
   return dst;
167
   }
168
169
/**
170
* Set memory to a fixed value
171
* @param ptr a pointer to an array of bytes
172
* @param n the number of Ts pointed to by ptr
173
* @param val the value to set each byte to
174
*/
175
inline constexpr void set_mem(uint8_t* ptr, size_t n, uint8_t val)
176
6.18k
   {
177
6.18k
   if(n > 0)
178
6.18k
      {
179
6.18k
      std::memset(ptr, val, n);
180
6.18k
      }
181
6.18k
   }
182
183
inline const uint8_t* cast_char_ptr_to_uint8(const char* s)
184
73.7k
   {
185
73.7k
   return reinterpret_cast<const uint8_t*>(s);
186
73.7k
   }
187
188
inline const char* cast_uint8_ptr_to_char(const uint8_t* b)
189
174k
   {
190
174k
   return reinterpret_cast<const char*>(b);
191
174k
   }
192
193
inline uint8_t* cast_char_ptr_to_uint8(char* s)
194
0
   {
195
0
   return reinterpret_cast<uint8_t*>(s);
196
0
   }
197
198
inline char* cast_uint8_ptr_to_char(uint8_t* b)
199
29.0k
   {
200
29.0k
   return reinterpret_cast<char*>(b);
201
29.0k
   }
202
203
/**
204
* Memory comparison, input insensitive
205
* @param p1 a pointer to an array
206
* @param p2 a pointer to another array
207
* @param n the number of Ts in p1 and p2
208
* @return true iff p1[i] == p2[i] forall i in [0...n)
209
*/
210
template<typename T> inline bool same_mem(const T* p1, const T* p2, size_t n)
211
455
   {
212
455
   volatile T difference = 0;
213
214
8.64k
   for(size_t i = 0; i != n; ++i)
215
8.19k
      difference |= (p1[i] ^ p2[i]);
216
217
455
   return difference == 0;
218
455
   }
bool Botan::same_mem<unsigned int>(unsigned int const*, unsigned int const*, unsigned long)
Line
Count
Source
211
22
   {
212
22
   volatile T difference = 0;
213
214
88
   for(size_t i = 0; i != n; ++i)
215
66
      difference |= (p1[i] ^ p2[i]);
216
217
22
   return difference == 0;
218
22
   }
bool Botan::same_mem<unsigned char>(unsigned char const*, unsigned char const*, unsigned long)
Line
Count
Source
211
433
   {
212
433
   volatile T difference = 0;
213
214
8.55k
   for(size_t i = 0; i != n; ++i)
215
8.12k
      difference |= (p1[i] ^ p2[i]);
216
217
433
   return difference == 0;
218
433
   }
219
220
template<typename T, typename Alloc>
221
size_t buffer_insert(std::vector<T, Alloc>& buf,
222
                     size_t buf_offset,
223
                     const T input[],
224
                     size_t input_length)
225
1.01M
   {
226
1.01M
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
227
1.01M
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
228
1.01M
   if(to_copy > 0)
229
642k
      {
230
642k
      copy_mem(&buf[buf_offset], input, to_copy);
231
642k
      }
232
1.01M
   return to_copy;
233
1.01M
   }
unsigned long Botan::buffer_insert<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned long, unsigned char const*, unsigned long)
Line
Count
Source
225
1.01M
   {
226
1.01M
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
227
1.01M
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
228
1.01M
   if(to_copy > 0)
229
642k
      {
230
642k
      copy_mem(&buf[buf_offset], input, to_copy);
231
642k
      }
232
1.01M
   return to_copy;
233
1.01M
   }
unsigned long Botan::buffer_insert<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, unsigned long, unsigned char const*, unsigned long)
Line
Count
Source
225
1.11k
   {
226
1.11k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
227
1.11k
   const size_t to_copy = std::min(input_length, buf.size() - buf_offset);
228
1.11k
   if(to_copy > 0)
229
745
      {
230
745
      copy_mem(&buf[buf_offset], input, to_copy);
231
745
      }
232
1.11k
   return to_copy;
233
1.11k
   }
234
235
template<typename T, typename Alloc, typename Alloc2>
236
size_t buffer_insert(std::vector<T, Alloc>& buf,
237
                     size_t buf_offset,
238
                     const std::vector<T, Alloc2>& input)
239
1.15k
   {
240
1.15k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
241
1.15k
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
242
1.15k
   if(to_copy > 0)
243
1.15k
      {
244
1.15k
      copy_mem(&buf[buf_offset], input.data(), to_copy);
245
1.15k
      }
246
1.15k
   return to_copy;
247
1.15k
   }
unsigned long Botan::buffer_insert<unsigned char, Botan::secure_allocator<unsigned char>, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned long, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)
Line
Count
Source
239
1.11k
   {
240
1.11k
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
241
1.11k
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
242
1.11k
   if(to_copy > 0)
243
1.11k
      {
244
1.11k
      copy_mem(&buf[buf_offset], input.data(), to_copy);
245
1.11k
      }
246
1.11k
   return to_copy;
247
1.11k
   }
unsigned long Botan::buffer_insert<unsigned char, Botan::secure_allocator<unsigned char>, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned long, std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
Line
Count
Source
239
38
   {
240
38
   BOTAN_ASSERT_NOMSG(buf_offset <= buf.size());
241
38
   const size_t to_copy = std::min(input.size(), buf.size() - buf_offset);
242
38
   if(to_copy > 0)
243
38
      {
244
38
      copy_mem(&buf[buf_offset], input.data(), to_copy);
245
38
      }
246
38
   return to_copy;
247
38
   }
248
249
/**
250
* XOR arrays. Postcondition out[i] = in[i] ^ out[i] forall i = 0...length
251
* @param out the input/output buffer
252
* @param in the read-only input buffer
253
* @param length the length of the buffers
254
*/
255
inline void xor_buf(uint8_t out[],
256
                    const uint8_t in[],
257
                    size_t length)
258
96.8k
   {
259
96.8k
   const size_t blocks = length - (length % 32);
260
261
166k
   for(size_t i = 0; i != blocks; i += 32)
262
69.8k
      {
263
69.8k
      uint64_t x[4];
264
69.8k
      uint64_t y[4];
265
266
69.8k
      typecast_copy(x, out + i, 4);
267
69.8k
      typecast_copy(y, in + i, 4);
268
269
69.8k
      x[0] ^= y[0];
270
69.8k
      x[1] ^= y[1];
271
69.8k
      x[2] ^= y[2];
272
69.8k
      x[3] ^= y[3];
273
274
69.8k
      typecast_copy(out + i, x, 4);
275
69.8k
      }
276
277
896k
   for(size_t i = blocks; i != length; ++i)
278
799k
      {
279
799k
      out[i] ^= in[i];
280
799k
      }
281
96.8k
   }
282
283
/**
284
* XOR arrays. Postcondition out[i] = in[i] ^ in2[i] forall i = 0...length
285
* @param out the output buffer
286
* @param in the first input buffer
287
* @param in2 the second output buffer
288
* @param length the length of the three buffers
289
*/
290
inline void xor_buf(uint8_t out[],
291
                    const uint8_t in[],
292
                    const uint8_t in2[],
293
                    size_t length)
294
3.59k
   {
295
3.59k
   const size_t blocks = length - (length % 32);
296
297
7.54k
   for(size_t i = 0; i != blocks; i += 32)
298
3.95k
      {
299
3.95k
      uint64_t x[4];
300
3.95k
      uint64_t y[4];
301
302
3.95k
      typecast_copy(x, in + i, 4);
303
3.95k
      typecast_copy(y, in2 + i, 4);
304
305
3.95k
      x[0] ^= y[0];
306
3.95k
      x[1] ^= y[1];
307
3.95k
      x[2] ^= y[2];
308
3.95k
      x[3] ^= y[3];
309
310
3.95k
      typecast_copy(out + i, x, 4);
311
3.95k
      }
312
313
42.0k
   for(size_t i = blocks; i != length; ++i)
314
38.4k
      {
315
38.4k
      out[i] = in[i] ^ in2[i];
316
38.4k
      }
317
3.59k
   }
318
319
template<typename Alloc, typename Alloc2>
320
void xor_buf(std::vector<uint8_t, Alloc>& out,
321
             const std::vector<uint8_t, Alloc2>& in,
322
             size_t n)
323
0
   {
324
0
   xor_buf(out.data(), in.data(), n);
325
0
   }
326
327
template<typename Alloc>
328
void xor_buf(std::vector<uint8_t, Alloc>& out,
329
             const uint8_t* in,
330
             size_t n)
331
264
   {
332
264
   xor_buf(out.data(), in, n);
333
264
   }
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
331
264
   {
332
264
   xor_buf(out.data(), in, n);
333
264
   }
334
335
template<typename Alloc, typename Alloc2>
336
void xor_buf(std::vector<uint8_t, Alloc>& out,
337
             const uint8_t* in,
338
             const std::vector<uint8_t, Alloc2>& in2,
339
             size_t n)
340
   {
341
   xor_buf(out.data(), in, in2.data(), n);
342
   }
343
344
template<typename Alloc, typename Alloc2>
345
std::vector<uint8_t, Alloc>&
346
operator^=(std::vector<uint8_t, Alloc>& out,
347
           const std::vector<uint8_t, Alloc2>& in)
348
667
   {
349
667
   if(out.size() < in.size())
350
0
      out.resize(in.size());
351
352
667
   xor_buf(out.data(), in.data(), in.size());
353
667
   return out;
354
667
   }
355
356
}
357
358
#endif