compat/inet_aton.c compat/inet_ntop.c compat/inet_pton.c compat/malloc.c \
compat/memcmp.c compat/memmove.c compat/snprintf.c compat/strlcat.c \
compat/strlcpy.c compat/strptime.c compat/getentropy_linux.c \
-compat/getentropy_osx.c compat/getentropy_solaris.c compat/explicit_bzero.c \
-compat/arc4random.c compat/arc4random_uniform.c compat/arc4_lock.c \
-compat/sha512.c
+compat/getentropy_osx.c compat/getentropy_solaris.c compat/getentropy_win.c \
+compat/explicit_bzero.c compat/arc4random.c compat/arc4random_uniform.c \
+compat/arc4_lock.c compat/sha512.c
COMPAT_OBJ=$(LIBOBJS:.o=.lo)
COMPAT_OBJ_WITHOUT_CTIME=$(LIBOBJ_WITHOUT_CTIME:.o=.lo)
COMPAT_OBJ_WITHOUT_CTIMEARC4=$(LIBOBJ_WITHOUT_CTIMEARC4:.o=.lo)
getentropy_linux.lo getentropy_linux.o: $(srcdir)/compat/getentropy_linux.c
getentropy_osx.lo getentropy_osx.o: $(srcdir)/compat/getentropy_osx.c
getentropy_solaris.lo getentropy_solaris.o: $(srcdir)/compat/getentropy_solaris.c
+getentropy_win.lo getentropy_win.o: $(srcdir)/compat/getentropy_win.c
explicit_bzero.lo explicit_bzero.o: $(srcdir)/compat/explicit_bzero.c
arc4random.lo arc4random.o: $(srcdir)/compat/arc4random.c $(srcdir)/compat/chacha_private.h
arc4random_uniform.lo arc4random_uniform.o: $(srcdir)/compat/arc4random_uniform.c $(srcdir)/compat/chacha_private.h
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
+#ifndef UB_ON_WINDOWS
#include <sys/mman.h>
+#endif
#define KEYSTREAM_ONLY
#include "chacha_private.h"
-#define min(a, b) ((a) < (b) ? (a) : (b))
+#define arc4_min(a, b) ((a) < (b) ? (a) : (b))
#ifdef __GNUC__
#define inline __inline
#else /* !__GNUC__ */
return;
if (rs == NULL) {
+#ifndef UB_ON_WINDOWS
if ((rs = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
abort();
#ifdef MAP_INHERIT_ZERO
if (minherit(rs, sizeof(*rs), MAP_INHERIT_ZERO) == -1)
abort();
+#endif
+#else /* WINDOWS */
+ rs = malloc(sizeof(*rs));
+ if(!rs)
+ abort();
#endif
}
if (rsx == NULL) {
+#ifndef UB_ON_WINDOWS
if ((rsx = mmap(NULL, sizeof(*rsx), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
abort();
+#else /* WINDOWS */
+ rsx = malloc(sizeof(*rsx));
+ if(!rsx)
+ abort();
+#endif
}
chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0);
{
u_char rnd[KEYSZ + IVSZ];
- if (getentropy(rnd, sizeof rnd) == -1)
+ if (getentropy(rnd, sizeof rnd) == -1) {
+#ifdef SIGKILL
raise(SIGKILL);
+#else
+ exit(9); /* windows */
+#endif
+ }
if (!rs)
_rs_init(rnd, sizeof(rnd));
if (dat) {
size_t i, m;
- m = min(datlen, KEYSZ + IVSZ);
+ m = arc4_min(datlen, KEYSZ + IVSZ);
for (i = 0; i < m; i++)
rsx->rs_buf[i] ^= dat[i];
}
_rs_stir_if_needed(n);
while (n > 0) {
if (rs->rs_have > 0) {
- m = min(n, rs->rs_have);
+ m = arc4_min(n, rs->rs_have);
keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
- rs->rs_have;
memcpy(buf, keystream, m);
void
explicit_bzero(void *buf, size_t len)
{
+#ifdef UB_ON_WINDOWS
+ SecureZeroMemory(buf, len);
+#endif
memset(buf, 0, len);
__explicit_bzero_hook(buf, len);
}
--- /dev/null
+/* getentropy_win.c - get entropy on Windows.
+*
+ * Copyright (c) 2014, NLnet Labs. All rights reserved.
+ *
+ * This software is open source.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * Neither the name of the NLNET LABS nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "config.h"
+
+int
+getentropy(void *buf, size_t len)
+{
+ HMODULE lib;
+
+ if(len > 256) {
+ errno = EIO;
+ return -1;
+ }
+
+ /* Get entropy with windows secure random number generator,
+ * for windows XP and later, it is in the ADVAPI32 dll */
+ lib = LoadLibrary("ADVAPI32.DLL");
+ if(lib) {
+ /* Load the RtlGenRandom function */
+ BOOLEAN (APIENTRY* genrandom)(void*,ULONG) =
+ (BOOLEAN (APIENTRY*)(void*,ULONG))
+ GetProcAddress(lib, "SystemFunction036");
+ if(genrandom) {
+ if(genrandom(buf, len)) {
+ FreeLibrary(lib);
+ return 0; /* success */
+ }
+ }
+ FreeLibrary(lib);
+ }
+
+ errno = EIO;
+ return -1;
+}
else
- case `uname` in
- Darwin)
+ if test "$USE_WINSOCK" = 1; then
case " $LIBOBJS " in
+ *" getentropy_win.$ac_objext "* ) ;;
+ *) LIBOBJS="$LIBOBJS getentropy_win.$ac_objext"
+ ;;
+esac
+
+ else
+ case `uname` in
+ Darwin)
+ case " $LIBOBJS " in
*" getentropy_osx.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS getentropy_osx.$ac_objext"
;;
esac
- ;;
- SunOS)
- case " $LIBOBJS " in
+ ;;
+ SunOS)
+ case " $LIBOBJS " in
*" getentropy_solaris.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS getentropy_solaris.$ac_objext"
;;
esac
- for ac_header in sys/sha2.h
+ for ac_header in sys/sha2.h
do :
ac_fn_c_check_header_compile "$LINENO" "sys/sha2.h" "ac_cv_header_sys_sha2_h" "$ac_includes_default
"
else
- for ac_func in SHA512_Update
+ for ac_func in SHA512_Update
do :
ac_fn_c_check_func "$LINENO" "SHA512_Update" "ac_cv_func_SHA512_Update"
if test "x$ac_cv_func_SHA512_Update" = xyes; then :
else
- case " $LIBOBJS " in
+ case " $LIBOBJS " in
*" sha512.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS sha512.$ac_objext"
;;
done
- if test "$ac_cv_header_sys_sha2_h" = "yes"; then
- LIBS="$LIBS -lmd"
- fi
- ;;
- Linux|*)
- case " $LIBOBJS " in
+ if test "$ac_cv_header_sys_sha2_h" = "yes"; then
+ LIBS="$LIBS -lmd"
+ fi
+ ;;
+ Linux|*)
+ case " $LIBOBJS " in
*" getentropy_linux.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS getentropy_linux.$ac_objext"
;;
esac
- for ac_func in SHA512_Update
+ for ac_func in SHA512_Update
do :
ac_fn_c_check_func "$LINENO" "SHA512_Update" "ac_cv_func_SHA512_Update"
if test "x$ac_cv_func_SHA512_Update" = xyes; then :
else
- case " $LIBOBJS " in
+ case " $LIBOBJS " in
*" sha512.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS sha512.$ac_objext"
;;
fi
done
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing clock_gettime" >&5
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing clock_gettime" >&5
$as_echo_n "checking for library containing clock_gettime... " >&6; }
if ${ac_cv_search_clock_gettime+:} false; then :
$as_echo_n "(cached) " >&6
fi
- ;;
- esac
+ ;;
+ esac
+ fi
fi
done
AC_LIBOBJ(explicit_bzero)
AC_LIBOBJ(arc4_lock)
AC_CHECK_FUNCS([getentropy],,[
- case `uname` in
- Darwin)
- AC_LIBOBJ(getentropy_osx)
- ;;
- SunOS)
- AC_LIBOBJ(getentropy_solaris)
- AC_CHECK_HEADERS([sys/sha2.h],, [
+ if test "$USE_WINSOCK" = 1; then
+ AC_LIBOBJ(getentropy_win)
+ else
+ case `uname` in
+ Darwin)
+ AC_LIBOBJ(getentropy_osx)
+ ;;
+ SunOS)
+ AC_LIBOBJ(getentropy_solaris)
+ AC_CHECK_HEADERS([sys/sha2.h],, [
+ AC_CHECK_FUNCS([SHA512_Update],,[
+ AC_LIBOBJ(sha512)
+ ])
+ ], [AC_INCLUDES_DEFAULT])
+ if test "$ac_cv_header_sys_sha2_h" = "yes"; then
+ LIBS="$LIBS -lmd"
+ fi
+ ;;
+ Linux|*)
+ AC_LIBOBJ(getentropy_linux)
AC_CHECK_FUNCS([SHA512_Update],,[
AC_LIBOBJ(sha512)
])
- ], [AC_INCLUDES_DEFAULT])
- if test "$ac_cv_header_sys_sha2_h" = "yes"; then
- LIBS="$LIBS -lmd"
- fi
- ;;
- Linux|*)
- AC_LIBOBJ(getentropy_linux)
- AC_CHECK_FUNCS([SHA512_Update],,[
- AC_LIBOBJ(sha512)
- ])
- AC_SEARCH_LIBS([clock_gettime], [rt])
- ;;
- esac
+ AC_SEARCH_LIBS([clock_gettime], [rt])
+ ;;
+ esac
+ fi
])
fi
fi
This makes arc4random available on all platforms, except when
compiled with LIBNSS (it uses libNSS crypto random).
- fix strptime implicit declaration error on OpenBSD.
+ - arc4random, getentropy and explicit_bzero compat for Windows.
4 July 2014: Wouter
- Fix #593: segfault or crash upon rotating logfile.