Coverage Report

Created: 2020-06-30 13:58

/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
96.4M
   {
20
96.4M
   if(elems == 0 || elem_size == 0)
21
0
      return nullptr;
22
96.4M
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
96.4M
   void* ptr = std::calloc(elems, elem_size);
29
96.4M
   if(!ptr)
30
0
      throw std::bad_alloc();
31
96.4M
   return ptr;
32
96.4M
   }
33
34
void deallocate_memory(void* p, size_t elems, size_t elem_size)
35
96.4M
   {
36
96.4M
   if(p == nullptr)
37
0
      return;
38
96.4M
39
96.4M
   secure_scrub_memory(p, elems * elem_size);
40
96.4M
41
#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
42
   if(mlock_allocator::instance().deallocate(p, elems, elem_size))
43
      return;
44
#endif
45
46
96.4M
   std::free(p);
47
96.4M
   }
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.15k
   {
60
1.15k
   volatile uint8_t difference = 0;
61
1.15k
62
20.8k
   for(size_t i = 0; i != len; ++i)
63
19.6k
      difference |= (x[i] ^ y[i]);
64
1.15k
65
1.15k
   return CT::Mask<uint8_t>::is_zero(difference).value();
66
1.15k
   }
67
68
}