diff options
author | lloyd <[email protected]> | 2012-11-12 19:19:48 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2012-11-12 19:19:48 +0000 |
commit | 579158e826daed42963db0c8b987d51ba7831fb6 (patch) | |
tree | 5e6c93b88460d0ba6871dbc824ccff433c944678 | |
parent | eea72cd2e5ab564e172fc7a2b761b6293605ff91 (diff) |
Move memory zeroing to a compiled function in a new source file. Cast
the pointer to volatile before writing to it. At least for various
versions of GCC, Clang, and ICC on x86-64, this does cause the
compiler to emit a simple byte-at-a-time loop, and at least in non-LTO
builds the compiler won't optimize the call away. For dealing with
LTO, probably would have to do some kind of complicated side-effect.
-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; + } + +} |