From 8b728c903457305cb4f2e9f10dbbe1f1cc8010a4 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 18 Mar 2013 21:40:16 -0700 Subject: [PATCH] Export sodium_memzero() as this will be required for Blake2, too. --- README.markdown | 9 ++++++++ src/libsodium/Makefile.am | 1 + src/libsodium/include/Makefile.am | 1 + src/libsodium/include/sodium.h | 1 + src/libsodium/include/sodium/utils.h | 17 +++++++++++++++ .../randombytes/randombytes_salsa20_random.c | 16 +------------- src/libsodium/utils.c | 21 +++++++++++++++++++ 7 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 src/libsodium/include/sodium/utils.h create mode 100644 src/libsodium/utils.c diff --git a/README.markdown b/README.markdown index b0582710..de8a39e5 100644 --- a/README.markdown +++ b/README.markdown @@ -104,6 +104,15 @@ provider. A custom implementation of these functions can be registered with `randombytes_set_implementation()`. +In addition, Sodium provides a function to securely wipe a memory +region: + + void sodium_memzero(void * const pnt, const size_t size); + +Warning: if a region has been allocated on the heap, you still have +to make sure that it can't get swapped to disk, possibly using +`mlock(2)`. + ## Bindings for other languages * Ruby: [RbNaCl](https://github.com/cryptosphere/rbnacl) diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index 2a87f149..4e62b94b 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -77,6 +77,7 @@ libsodium_la_SOURCES = \ randombytes/randombytes.c \ randombytes/randombytes_salsa20_random.c \ randombytes/randombytes_sysrandom.c \ + utils.c \ version.c EXTRA_DIST = \ diff --git a/src/libsodium/include/Makefile.am b/src/libsodium/include/Makefile.am index d1b0b2e3..138350c9 100644 --- a/src/libsodium/include/Makefile.am +++ b/src/libsodium/include/Makefile.am @@ -40,6 +40,7 @@ SODIUM_EXPORT = \ sodium/randombytes.h \ sodium/randombytes_salsa20_random.h \ sodium/randombytes_sysrandom.h \ + sodium/utils.h \ sodium/version.h EXTRA_SRC = $(SODIUM_EXPORT) \ diff --git a/src/libsodium/include/sodium.h b/src/libsodium/include/sodium.h index b94d9b7c..274e9918 100644 --- a/src/libsodium/include/sodium.h +++ b/src/libsodium/include/sodium.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #endif diff --git a/src/libsodium/include/sodium/utils.h b/src/libsodium/include/sodium/utils.h new file mode 100644 index 00000000..be45be10 --- /dev/null +++ b/src/libsodium/include/sodium/utils.h @@ -0,0 +1,17 @@ + +#ifndef __SODIUM_UTILS_H__ +#define __SODIUM_UTILS_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void sodium_memzero(void * const pnt, const size_t size); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/libsodium/randombytes/randombytes_salsa20_random.c b/src/libsodium/randombytes/randombytes_salsa20_random.c index 54e88654..063ee574 100644 --- a/src/libsodium/randombytes/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/randombytes_salsa20_random.c @@ -15,6 +15,7 @@ #include "crypto_hash_sha256.h" #include "crypto_stream_salsa20.h" #include "randombytes_salsa20_random.h" +#include "utils.h" #ifdef _WIN32 # include @@ -46,21 +47,6 @@ static Salsa20Random stream = { .initialized = 0 }; -static void -sodium_memzero(void * const pnt, const size_t size) -{ -#ifdef HAVE_SECUREZEROMEMORY - SecureZeroMemory(pnt, size); -#else - volatile unsigned char *pnt_ = (volatile unsigned char *) pnt; - size_t i = (size_t) 0U; - - while (i < size) { - pnt_[i++] = 0U; - } -#endif -} - static uint64_t sodium_hrtime(void) { diff --git a/src/libsodium/utils.c b/src/libsodium/utils.c new file mode 100644 index 00000000..acc80702 --- /dev/null +++ b/src/libsodium/utils.c @@ -0,0 +1,21 @@ + +#include "utils.h" +#ifdef _WIN32 +# include +# include +#endif + +void +sodium_memzero(void * const pnt, const size_t size) +{ +#ifdef HAVE_SECUREZEROMEMORY + SecureZeroMemory(pnt, size); +#else + volatile unsigned char *pnt_ = (volatile unsigned char *) pnt; + size_t i = (size_t) 0U; + + while (i < size) { + pnt_[i++] = 0U; + } +#endif +}