Botan  2.11.0
Crypto and TLS for C++11
mem_ops.cpp
Go to the documentation of this file.
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 
19  {
20 #if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
21  if(void* p = mlock_allocator::instance().allocate(elems, elem_size))
22  return p;
23 #endif
24 
25  void* ptr = std::calloc(elems, elem_size);
26  if(!ptr)
27  throw std::bad_alloc();
28  return ptr;
29  }
30 
31 void deallocate_memory(void* p, size_t elems, size_t elem_size)
32  {
33  if(p == nullptr)
34  return;
35 
37 
38 #if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
39  if(mlock_allocator::instance().deallocate(p, elems, elem_size))
40  return;
41 #endif
42 
43  std::free(p);
44  }
45 
47  {
48 #if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
49  mlock_allocator::instance();
50 #endif
51  }
52 
53 uint8_t ct_compare_u8(const uint8_t x[],
54  const uint8_t y[],
55  size_t len)
56  {
57  volatile uint8_t difference = 0;
58 
59  for(size_t i = 0; i != len; ++i)
60  difference |= (x[i] ^ y[i]);
61 
62  return CT::Mask<uint8_t>::is_zero(difference).value();
63  }
64 
65 }
BigInt const BigInt & x
Definition: numthry.h:139
std::string size_t len
Definition: pk_keys.h:305
size_t elem_size
Definition: mem_ops.h:25
BigInt const BigInt & p
Definition: numthry.h:150
BOTAN_MALLOC_FN void * allocate_memory(size_t elems, size_t elem_size)
Definition: mem_ops.cpp:18
#define BOTAN_MALLOC_FN
Definition: compiler.h:96
uint8_t ct_compare_u8(const uint8_t x[], const uint8_t y[], size_t len)
Definition: mem_ops.cpp:53
void deallocate_memory(void *p, size_t elems, size_t elem_size)
Definition: mem_ops.cpp:31
Definition: alg_id.cpp:13
void secure_scrub_memory(void *ptr, size_t n)
Definition: os_utils.cpp:61
const OctetString & y
Definition: symkey.h:126
static Mask< T > is_zero(T x)
Definition: ct_utils.h:141
size_t elems
Definition: mem_ops.h:33
void initialize_allocator()
Definition: mem_ops.cpp:46