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

Use arc4random_buf() as the best practice method for obtaining randomness on OpenBSD (examples) #1215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions examples/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Linux -> `getrandom(2)`(`sys/random.h`), if not available `/dev/urandom` should be used. http://man7.org/linux/man-pages/man2/getrandom.2.html, https://linux.die.net/man/4/urandom
* macOS -> `getentropy(2)`(`sys/random.h`), if not available `/dev/urandom` should be used. https://www.unix.com/man-page/mojave/2/getentropy, https://opensource.apple.com/source/xnu/xnu-517.12.7/bsd/man/man4/random.4.auto.html
* FreeBSD -> `getrandom(2)`(`sys/random.h`), if not available `kern.arandom` should be used. https://www.freebsd.org/cgi/man.cgi?query=getrandom, https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4
* OpenBSD -> `getentropy(2)`(`unistd.h`), if not available `/dev/urandom` should be used. https://man.openbsd.org/getentropy, https://man.openbsd.org/urandom
* OpenBSD -> `arc4random_buf(3)`(`stdlib.h`), if not available `/dev/urandom` should be used. https://man.openbsd.org/arc4random.3, https://man.openbsd.org/urandom
* Windows -> `BCryptGenRandom`(`bcrypt.h`). https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
*/

Expand All @@ -23,7 +23,7 @@
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/random.h>
#elif defined(__OpenBSD__)
#include <unistd.h>
#include <stdlib.h>
#else
#error "Couldn't identify the OS"
#endif
Expand All @@ -50,7 +50,7 @@ static int fill_random(unsigned char* data, size_t size) {
} else {
return 1;
}
#elif defined(__APPLE__) || defined(__OpenBSD__)
#elif defined(__APPLE__)
/* If `getentropy(2)` is not available you should fallback to either
* `SecRandomCopyBytes` or /dev/urandom */
int res = getentropy(data, size);
Expand All @@ -59,6 +59,11 @@ static int fill_random(unsigned char* data, size_t size) {
} else {
return 0;
}
#elif defined(__OpenBSD__)
/* This function is always successful, and no return value is reserved to
indicate an error. */
arc4random_buf(data, size);
return 1;
#endif
return 0;
}
Expand Down