Coverage Report

Created: 2022-11-24 06:56

/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
153M
   {
20
153M
   if(elems == 0 || elem_size == 0)
21
0
      return nullptr;
22
23
   // Some calloc implementations do not check for overflow (?!?)
24
153M
   const size_t total_size = elems * elem_size;
25
26
153M
   if(total_size < elems || total_size < elem_size)
27
0
      throw std::bad_alloc();
28
29
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
30
   if(void* p = mlock_allocator::instance().allocate(elems, elem_size))
31
      return p;
32
#endif
33
34
#if defined(BOTAN_TARGET_OS_HAS_ALLOC_CONCEAL)
35
   void *ptr = ::calloc_conceal(elems, elem_size);
36
#else
37
153M
   void* ptr = std::calloc(elems, elem_size);
38
153M
#endif
39
153M
   if(!ptr)
40
0
      throw std::bad_alloc();
41
153M
   return ptr;
42
153M
   }
43
44
void deallocate_memory(void* p, size_t elems, size_t elem_size)
45
153M
   {
46
153M
   if(p == nullptr)
47
0
      return;
48
49
153M
   secure_scrub_memory(p, elems * elem_size);
50
51
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
52
   if(mlock_allocator::instance().deallocate(p, elems, elem_size))
53
      return;
54
#endif
55
153M
   std::free(p);
56
153M
   }
57
58
void initialize_allocator()
59
8
   {
60
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
61
   mlock_allocator::instance();
62
#endif
63
8
   }
64
65
uint8_t ct_compare_u8(const uint8_t x[],
66
                      const uint8_t y[],
67
                      size_t len)
68
8.98k
   {
69
8.98k
   volatile uint8_t difference = 0;
70
71
286k
   for(size_t i = 0; i != len; ++i)
72
277k
      difference = difference | (x[i] ^ y[i]);
73
74
8.98k
   return CT::Mask<uint8_t>::is_zero(difference).value();
75
8.98k
   }
76
77
}