aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-09-29 19:15:48 -0400
committerJack Lloyd <[email protected]>2017-09-29 21:05:29 -0400
commit2edb12e249387a72b24f5f34efae4735d589af9f (patch)
treed2873f1f3d608c0d23869893c22a69b05489ce48 /src/lib/utils
parent184ce9f93906241d1807f73c7ea20283fc6ff222 (diff)
In secure_allocator, hide mlock/new usage in a function in mem_ops
Switch to calloc/free instead of new/delete - shouldn't matter since we are only allocate integral types. This change reduces the size of libbotan-2.so by ~300 Kb on my system.
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/locking_allocator/locking_allocator.cpp2
-rw-r--r--src/lib/utils/locking_allocator/locking_allocator.h2
-rw-r--r--src/lib/utils/mem_ops.cpp32
-rw-r--r--src/lib/utils/mem_ops.h18
4 files changed, 52 insertions, 2 deletions
diff --git a/src/lib/utils/locking_allocator/locking_allocator.cpp b/src/lib/utils/locking_allocator/locking_allocator.cpp
index 880f3add8..f36fa9130 100644
--- a/src/lib/utils/locking_allocator/locking_allocator.cpp
+++ b/src/lib/utils/locking_allocator/locking_allocator.cpp
@@ -116,7 +116,7 @@ void* mlock_allocator::allocate(size_t num_elems, size_t elem_size)
return nullptr;
}
-bool mlock_allocator::deallocate(void* p, size_t num_elems, size_t elem_size)
+bool mlock_allocator::deallocate(void* p, size_t num_elems, size_t elem_size) BOTAN_NOEXCEPT
{
if(!m_pool)
return false;
diff --git a/src/lib/utils/locking_allocator/locking_allocator.h b/src/lib/utils/locking_allocator/locking_allocator.h
index 8e38129e2..e9299c120 100644
--- a/src/lib/utils/locking_allocator/locking_allocator.h
+++ b/src/lib/utils/locking_allocator/locking_allocator.h
@@ -21,7 +21,7 @@ class BOTAN_PUBLIC_API(2,0) mlock_allocator final
void* allocate(size_t num_elems, size_t elem_size);
- bool deallocate(void* p, size_t num_elems, size_t elem_size);
+ bool deallocate(void* p, size_t num_elems, size_t elem_size) BOTAN_NOEXCEPT;
mlock_allocator(const mlock_allocator&) = delete;
diff --git a/src/lib/utils/mem_ops.cpp b/src/lib/utils/mem_ops.cpp
index 45de24cb8..b2097e4c7 100644
--- a/src/lib/utils/mem_ops.cpp
+++ b/src/lib/utils/mem_ops.cpp
@@ -6,8 +6,40 @@
#include <botan/mem_ops.h>
+#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
+ #include <botan/locking_allocator.h>
+#endif
+
namespace Botan {
+void* allocate_memory(size_t elems, size_t elem_size)
+ {
+#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
+ if(void* p = mlock_allocator::instance().allocate(elems, elem_size))
+ return p;
+#endif
+
+ void* ptr = std::calloc(elems, elem_size);
+ if(!ptr)
+ throw std::bad_alloc();
+ return ptr;
+ }
+
+void deallocate_memory(void* p, size_t elems, size_t elem_size)
+ {
+ if(p == nullptr)
+ return;
+
+ secure_scrub_memory(p, elems * elem_size);
+
+#if defined(BOTAN_HAS_LOCKING_ALLOCATOR)
+ if(mlock_allocator::instance().deallocate(p, elems, elem_size))
+ return;
+#endif
+
+ std::free(p);
+ }
+
bool constant_time_compare(const uint8_t x[],
const uint8_t y[],
size_t len)
diff --git a/src/lib/utils/mem_ops.h b/src/lib/utils/mem_ops.h
index 328196000..4b03b23d2 100644
--- a/src/lib/utils/mem_ops.h
+++ b/src/lib/utils/mem_ops.h
@@ -15,6 +15,24 @@
namespace Botan {
/**
+* Allocate a memory buffer by some method. This should only be used for
+* primitive types (uint8_t, uint32_t, etc).
+*
+* @param elems the number of elements
+* @param elem_size the size of each element
+* @return pointer to allocated and zeroed memory, or throw std::bad_alloc on failure
+*/
+BOTAN_PUBLIC_API(2,3) void* allocate_memory(size_t elems, size_t elem_size);
+
+/**
+* Free a pointer returned by allocate_memory
+* @param p the pointer returned by allocate_memory
+* @param elems the number of elements, as passed to allocate_memory
+* @param elem_size the size of each element, as passed to allocate_memory
+*/
+BOTAN_PUBLIC_API(2,3) void deallocate_memory(void* p, size_t elems, size_t elem_size);
+
+/**
* Scrub memory contents in a way that a compiler should not elide,
* using some system specific technique. Note that this function might
* not zero the memory (for example, in some hypothetical