From b9d6139178047cba64d53dbbbdf2e6d74fed8ad7 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 10 Nov 2017 02:22:38 +0100 Subject: [PATCH 1/3] Check for alloca() --- configure.ac | 1 + 1 file changed, 1 insertion(+) diff --git a/configure.ac b/configure.ac index a771822f..bb4c61af 100644 --- a/configure.ac +++ b/configure.ac @@ -744,6 +744,7 @@ __sync_lock_release(&_sodium_lock); dnl Checks for functions and headers +AC_FUNC_ALLOCA AS_IF([test "x$EMSCRIPTEN" = "x"],[ AC_CHECK_FUNCS([arc4random arc4random_buf]) AC_CHECK_FUNCS([mmap mlock madvise mprotect memset_s explicit_bzero nanosleep]) From b93d773f7b57c65fe403e56b129b6f6e2217124f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 10 Nov 2017 20:48:05 +0100 Subject: [PATCH 2/3] Add sodium_stackzero() --- src/libsodium/include/sodium/utils.h | 3 +++ src/libsodium/sodium/utils.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/libsodium/include/sodium/utils.h b/src/libsodium/include/sodium/utils.h index a4b4a1cf..46eb331c 100644 --- a/src/libsodium/include/sodium/utils.h +++ b/src/libsodium/include/sodium/utils.h @@ -21,6 +21,9 @@ extern "C" { SODIUM_EXPORT void sodium_memzero(void * const pnt, const size_t len); +SODIUM_EXPORT +void sodium_stackzero(const size_t len); + /* * WARNING: sodium_memcmp() must be used to verify if two secret keys * are equal, in constant time. diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index bb94fe88..b6ed31c4 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -21,6 +21,23 @@ # include #endif +#if HAVE_ALLOCA_H +# include +#elif defined __GNUC__ +# define alloca __builtin_alloca +#elif defined _AIX +# define alloca __alloca +#elif defined _MSC_VER +# include +# define alloca _alloca +#else +# include +# ifdef __cplusplus +extern "C" +# endif +void *alloca (size_t); +#endif + #include "core.h" #include "randombytes.h" #include "utils.h" @@ -103,6 +120,17 @@ sodium_memzero(void *const pnt, const size_t len) #endif } +void +sodium_stackzero(const size_t len) +{ +#ifdef HAVE_ALLOCA + sodium_memzero(alloca(len), len); +#elif __STDC_VERSION__ >= 199901L + unsigned char fodder[len]; + sodium_memzero(fodder, len); +#endif +} + #ifdef HAVE_WEAK_SYMBOLS __attribute__((weak)) void _sodium_dummy_symbol_to_prevent_memcmp_lto(const unsigned char *b1, From 5e1a9c580aa8ff16007c020dcc0b72170fdf6638 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 10 Nov 2017 20:48:39 +0100 Subject: [PATCH 3/3] Register sodium_stackzero() --- dist-build/emscripten-symbols.def | 1 + 1 file changed, 1 insertion(+) diff --git a/dist-build/emscripten-symbols.def b/dist-build/emscripten-symbols.def index 9612c4d4..f14cfdf5 100644 --- a/dist-build/emscripten-symbols.def +++ b/dist-build/emscripten-symbols.def @@ -558,5 +558,6 @@ _sodium_runtime_has_sse3 0 0 _sodium_runtime_has_sse41 0 0 _sodium_runtime_has_ssse3 0 0 _sodium_set_misuse_handler 0 0 +_sodium_stackzero 0 0 _sodium_unpad 1 1 _sodium_version_string 1 1