Skip to content

Commit

Permalink
Merge pull request #2770 from wilkart/fix-build-freebsd
Browse files Browse the repository at this point in the history
fix build on FreeBSD
  • Loading branch information
jamescowens authored Jul 3, 2024
2 parents 5644af6 + 1a6f001 commit 9ba9e8a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ To build, run:

* With CMake:

`mkdir build && cmake build && cmake .. && cmake --build .`
`mkdir -p build && cd build && cmake .. && cmake --build .`

* With Autotools:

Expand Down
3 changes: 2 additions & 1 deletion src/pbkdf2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ be32dec(const void *pp)
}
*/

#ifndef __FreeBSD__
static inline void
be32enc(void *pp, uint32_t x)
{
Expand All @@ -26,7 +27,7 @@ be32enc(void *pp, uint32_t x)
p[1] = (x >> 16) & 0xff;
p[0] = (x >> 24) & 0xff;
}

#endif

/**
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
Expand Down
23 changes: 5 additions & 18 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@
#include <sys/time.h>
#endif

#ifdef HAVE_SYS_GETRANDOM
#include <sys/syscall.h>
#include <linux/random.h>
#endif
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
#include <unistd.h>
#if defined(HAVE_GETRANDOM) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
#include <sys/random.h>
#endif

#ifdef HAVE_SYSCTL_ARND
#include <sys/sysctl.h>
#endif
Expand Down Expand Up @@ -286,23 +282,14 @@ void GetOSRand(unsigned char *ent32)
RandFailure();
}
CryptReleaseContext(hProvider, 0);
#elif defined(HAVE_SYS_GETRANDOM)
#elif defined(HAVE_GETRANDOM)
/* Linux. From the getrandom(2) man page:
* "If the urandom source has been initialized, reads of up to 256 bytes
* will always return as many bytes as requested and will not be
* interrupted by signals."
*/
int rv = syscall(SYS_getrandom, ent32, NUM_OS_RANDOM_BYTES, 0);
if (rv != NUM_OS_RANDOM_BYTES) {
if (rv < 0 && errno == ENOSYS) {
/* Fallback for kernel <3.17: the return value will be -1 and errno
* ENOSYS if the syscall is not available, in that case fall back
* to /dev/urandom.
*/
GetDevURandom(ent32);
} else {
RandFailure();
}
if (getrandom(ent32, NUM_OS_RANDOM_BYTES, 0) != NUM_OS_RANDOM_BYTES) {
RandFailure();
}
#elif defined(__OpenBSD__)
/* OpenBSD. From the arc4random(3) man page:
Expand Down

0 comments on commit 9ba9e8a

Please sign in to comment.