Coverage Report

Created: 2020-02-14 15:38

/src/botan/src/lib/utils/mem_ops.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/mem_ops.h>
8
#include <botan/internal/ct_utils.h>
9
#include <cstdlib>
10
#include <new>
11
12
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
13
  #include <botan/locking_allocator.h>
14
#endif
15
16
namespace Botan {
17
18
BOTAN_MALLOC_FN void* allocate_memory(size_t elems, size_t elem_size)
19
100M
   {
20
100M
   if(elems == 0 || elem_size == 0)
21
0
      return nullptr;
22
100M
23
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
24
   if(void* p = mlock_allocator::instance().allocate(elems, elem_size))
25
      return p;
26
#endif
27
28
100M
   void* ptr = std::calloc(elems, elem_size);
29
100M
   if(!ptr)
30
0
      throw std::bad_alloc();
31
100M
   return ptr;
32
100M
   }
33
34
void deallocate_memory(void* p, size_t elems, size_t elem_size)
35
100M
   {
36
100M
   if(p == nullptr)
37
0
      return;
38
100M
39
100M
   secure_scrub_memory(p, elems * elem_size);
40
100M
41
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
42
   if(mlock_allocator::instance().deallocate(p, elems, elem_size))
43
      return;
44
#endif
45
46
100M
   std::free(p);
47
100M
   }
48
49
void initialize_allocator()
50
9
   {
51
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
52
   mlock_allocator::instance();
53
#endif
54
   }
55
56
uint8_t ct_compare_u8(const uint8_t x[],
57
                      const uint8_t y[],
58
                      size_t len)
59
1.21k
   {
60
1.21k
   volatile uint8_t difference = 0;
61
1.21k
62
22.0k
   for(size_t i = 0; i != len; ++i)
63
20.7k
      difference |= (x[i] ^ y[i]);
64
1.21k
65
1.21k
   return CT::Mask<uint8_t>::is_zero(difference).value();
66
1.21k
   }
67
68
}