Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add missing randomness checks #2772

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,10 @@ if(NOT HAVE_GMTIME_R)
endif()
endif()

check_symbol_exists(SYS_getrandom "sys/syscall.h" HAVE_SYS_GETRANDOM)
check_symbol_exists(getentropy "unistd.h" HAVE_GETENTROPY)
check_symbol_exists(KERN_ARND "sys/sysctl.h" HAVE_SYSCTL_ARND)
check_symbol_exists(getrandom "sys/random.h" HAVE_GETRANDOM)
check_symbol_exists(getentropy "sys/random.h" HAVE_GETENTROPY_RAND)
check_symbol_exists(sysctl "sys/sysctl.h" "sys/types.h" HAVE_SYSCTL)
check_symbol_exists(KERN_ARND "sys/sysctl.h" "sys/types.h" HAVE_SYSCTL_ARND)

check_symbol_exists(O_CLOEXEC "fcntl.h" HAVE_O_CLOEXEC)
check_symbol_exists(getauxval "sys/auxv.h" HAVE_STRONG_GETAUXVAL)
Expand Down
43 changes: 32 additions & 11 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -912,22 +912,43 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
)

# Check for different ways of gathering OS randomness
AC_MSG_CHECKING(for Linux getrandom syscall)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
#include <sys/syscall.h>
#include <linux/random.h>]],
[[ syscall(SYS_getrandom, nullptr, 32, 0); ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_SYS_GETRANDOM, 1,[Define this symbol if the Linux getrandom system call is available]) ],
[ AC_MSG_RESULT(no)]
AC_MSG_CHECKING([for Linux getrandom function])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <sys/random.h>]],
[[ getrandom(nullptr, 32, 0); ]])],
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETRANDOM], [1], [Define this symbol if the Linux getrandom function call is available]) ],
[ AC_MSG_RESULT([no])]
)

AC_MSG_CHECKING([for getentropy via sys/random.h])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <sys/random.h>]],
[[ getentropy(nullptr, 32) ]])],
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETENTROPY_RAND], [1], [Define this symbol if the BSD getentropy system call is available with sys/random.h]) ],
[ AC_MSG_RESULT([no])]
)

AC_MSG_CHECKING([for sysctl])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
#include <sys/sysctl.h>]],
[[ #ifdef __linux__
#error "Don't use sysctl on Linux, it's deprecated even when it works"
#endif
sysctl(nullptr, 2, nullptr, nullptr, nullptr, 0); ]])],
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_SYSCTL], [1], [Define this symbol if the BSD sysctl() is available]) ],
[ AC_MSG_RESULT([no])]
)

AC_MSG_CHECKING(for sysctl KERN_ARND)
AC_MSG_CHECKING([for sysctl KERN_ARND])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
#include <sys/sysctl.h>]],
[[ static const int name[2] = {CTL_KERN, KERN_ARND};
[[ #ifdef __linux__
#error "Don't use sysctl on Linux, it's deprecated even when it works"
#endif
static int name[2] = {CTL_KERN, KERN_ARND};
sysctl(name, 2, nullptr, nullptr, nullptr, 0); ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_SYSCTL_ARND, 1,[Define this symbol if the BSD sysctl(KERN_ARND) is available]) ],
[ AC_MSG_RESULT(no)]
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_SYSCTL_ARND], [1], [Define this symbol if the BSD sysctl(KERN_ARND) is available]) ],
[ AC_MSG_RESULT([no])]
)

# LevelDB platform checks
Expand Down
8 changes: 3 additions & 5 deletions src/config/gridcoin-config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@
#cmakedefine01 HAVE_SYSTEM
#cmakedefine HAVE_GMTIME_R

// Define if the Linux getrandom system call is available
#cmakedefine HAVE_SYS_GETRANDOM
// Define if the BSD getentropy system call is available
#cmakedefine HAVE_GETENTROPY
// Define if the BSD sysctl(KERN_ARND) is available
#cmakedefine HAVE_GETRANDOM
#cmakedefine HAVE_GETENTROPY_RAND
#cmakedefine HAVE_SYSCTL
#cmakedefine HAVE_SYSCTL_ARND

#cmakedefine01 HAVE_O_CLOEXEC
Expand Down
31 changes: 4 additions & 27 deletions src/pbkdf2.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
// Copyright (c) 2013 NovaCoin Developers

#include <string.h>
#include "pbkdf2.h"

// Only commented out since it will be used in Big endian support
// in the future.
/*
static inline uint32_t
be32dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;

return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
}
*/
#include <crypto/common.h>
#include <pbkdf2.h>

#ifndef __FreeBSD__
static inline void
be32enc(void *pp, uint32_t x)
{
uint8_t * p = (uint8_t *)pp;
#include <string.h>

p[3] = x & 0xff;
p[2] = (x >> 8) & 0xff;
p[1] = (x >> 16) & 0xff;
p[0] = (x >> 24) & 0xff;
}
#endif

/**
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
Expand All @@ -54,7 +31,7 @@ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
/* Iterate through the blocks. */
for (i = 0; i * 32 < dkLen; i++) {
/* Generate INT(i + 1). */
be32enc(ivec, (uint32_t)(i + 1));
WriteBE32(ivec, (uint32_t)(i + 1));

/* Compute U_1 = PRF(P, S || INT(i)). */
CHMAC_SHA256 U_1 = salted;
Expand Down
Loading