Coverage Report

Created: 2020-11-21 08:34

/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/internal/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
101M
   {
20
101M
   if(elems == 0 || elem_size == 0)
21
0
      return nullptr;
22
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
101M
   void* ptr = std::calloc(elems, elem_size);
29
101M
   if(!ptr)
30
0
      throw std::bad_alloc();
31
101M
   return ptr;
32
101M
   }
33
34
void deallocate_memory(void* p, size_t elems, size_t elem_size)
35
101M
   {
36
101M
   if(p == nullptr)
37
0
      return;
38
39
101M
   secure_scrub_memory(p, elems * elem_size);
40
41
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
42
   if(mlock_allocator::instance().deallocate(p, elems, elem_size))
43
      return;
44
#endif
45
46
101M
   std::free(p);
47
101M
   }
48
49
void initialize_allocator()
50
9
   {
51
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
52
   mlock_allocator::instance();
53
#endif
54
9
   }
55
56
uint8_t ct_compare_u8(const uint8_t x[],
57
                      const uint8_t y[],
58
                      size_t len)
59
1.12k
   {
60
1.12k
   volatile uint8_t difference = 0;
61
62
19.9k
   for(size_t i = 0; i != len; ++i)
63
18.8k
      difference |= (x[i] ^ y[i]);
64
65
1.12k
   return CT::Mask<uint8_t>::is_zero(difference).value();
66
1.12k
   }
67
68
}