diff options
-rw-r--r-- | src/utils/info.txt | 1 | ||||
-rw-r--r-- | src/utils/mem_ops.h | 28 | ||||
-rw-r--r-- | src/utils/zero_mem.cpp | 20 |
3 files changed, 38 insertions, 11 deletions
diff --git a/src/utils/info.txt b/src/utils/info.txt index a1079d47b..5bf1b9a54 100644 --- a/src/utils/info.txt +++ b/src/utils/info.txt @@ -9,6 +9,7 @@ charset.cpp cpuid.cpp parsing.cpp version.cpp +zero_mem.cpp </source> <header:internal> diff --git a/src/utils/mem_ops.h b/src/utils/mem_ops.h index fc59c90d6..92a130310 100644 --- a/src/utils/mem_ops.h +++ b/src/utils/mem_ops.h @@ -1,6 +1,6 @@ /* * Memory Operations -* (C) 1999-2009 Jack Lloyd +* (C) 1999-2009,2012 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -14,15 +14,11 @@ namespace Botan { /** -* Copy memory -* @param out the destination array -* @param in the source array -* @param n the number of elements of in/out +* Zeroize memory +* @param ptr a pointer to memory to zero out +* @param n the number of bytes pointed to by ptr */ -template<typename T> inline void copy_mem(T* out, const T* in, size_t n) - { - std::memmove(out, in, sizeof(T)*n); - } +BOTAN_DLL void zero_mem(void* ptr, size_t n); /** * Zeroize memory @@ -31,8 +27,18 @@ template<typename T> inline void copy_mem(T* out, const T* in, size_t n) */ template<typename T> inline void clear_mem(T* ptr, size_t n) { - if(n) // avoid glibc warning if n == 0 - std::memset(ptr, 0, sizeof(T)*n); + zero_mem(ptr, sizeof(T)*n); + } + +/** +* Copy memory +* @param out the destination array +* @param in the source array +* @param n the number of elements of in/out +*/ +template<typename T> inline void copy_mem(T* out, const T* in, size_t n) + { + std::memmove(out, in, sizeof(T)*n); } /** diff --git a/src/utils/zero_mem.cpp b/src/utils/zero_mem.cpp new file mode 100644 index 000000000..e812ced0c --- /dev/null +++ b/src/utils/zero_mem.cpp @@ -0,0 +1,20 @@ +/* +* Zero Memory +* (C) 2012 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mem_ops.h> + +namespace Botan { + +void zero_mem(void* ptr, size_t n) + { + volatile byte* p = reinterpret_cast<volatile byte*>(ptr); + + for(size_t i = 0; i != n; ++i) + p[i] = 0; + } + +} |