commit 2029729ce17596ab87f1cc37250d23c063571e31
parent afc77c3ca22407cf4304bec23e3490705e7cb912
Author: Michael Savage <mike@mikejsavage.co.uk>
Date: Thu, 23 Feb 2023 23:54:27 +0200
Push v1.0
Apparently I didn't push this when I wrote it nearly two years ago.
- Use `arc4random_buf` on Apple platforms
- Drop `/dev/urandom` fallback on Linux because all versions that
require it have been EOL for years
- Drop Solaris support
Diffstat:
M | README.md | | | 27 | +++++++++++++++++---------- |
M | ggentropy.cpp | | | 61 | ++++--------------------------------------------------------- |
2 files changed, 21 insertions(+), 67 deletions(-)
diff --git a/README.md b/README.md
@@ -1,13 +1,22 @@
# ggentropy
-[![Appveyor](https://ci.appveyor.com/api/projects/status/xjeb72qltn1l0r3v?svg=true)](https://ci.appveyor.com/project/mikejsavage/ggentropy)
+[![Appveyor](https://ci.appveyor.com/api/projects/status/xjeb72qltn1l0r3v?svg=true)](https://ci.appveyor.com/project/mikejsavage/ggentropy) on Windows/macOS/Linux
-ggentropy is a liberally licensed, cross platform, entropy library for
-C++. You can use it to generate cryptographically secure random numbers,
-safe for use as nonces and keys in cryptographic operations.
+ggentropy is a liberally licensed and cross platform entropy library.
+You can use it to generate cryptographically secure random numbers, safe
+for use as nonces and keys in cryptographic operations.
-ggentropy supports Windows, MacOS, Linux, FreeBSD, OpenBSD, NetBSD and
-Solaris.
+ggentropy supports Windows, Apple platforms, Linux, FreeBSD, OpenBSD and
+NetBSD.
+
+
+## Version history
+
+- __v1.0 19th Jul 2021__:
+ - Use `arc4random_buf` on Apple platforms
+ - Drop `/dev/urandom` fallback on Linux because all versions that
+ require it have been EOL for years
+ - Drop Solaris support
## Usage
@@ -60,7 +69,5 @@ ggentropy uses the following functionality:
| OS | Implementation |
| - | - |
| Windows | [BCryptGenRandom](https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom) |
-| MacOS | [getentropy](https://opensource.apple.com/source/xnu/xnu-3789.21.4/bsd/man/man2/getentropy.2.auto.html) |
-| Linux | [getrandom](https://lwn.net/Articles/606141/) with a fallback to /dev/urandom if the syscall doesn't exist |
-| FreeBSD/OpenBSD/NetBSD | [arc4random_buf](https://man.openbsd.org/arc4random_buf) |
-| Solaris | /dev/urandom |
+| Linux | [getrandom](https://lwn.net/Articles/606141/) |
+| macOS/iOS/BSD | [arc4random_buf](https://man.openbsd.org/arc4random_buf) |
diff --git a/ggentropy.cpp b/ggentropy.cpp
@@ -1,7 +1,7 @@
/*
- * ggentropy
+ * ggentropy v1.0
*
- * Copyright (c) 2019 Michael Savage <mike@mikejsavage.co.uk>
+ * Copyright (c) 2021 Michael Savage <mike@mikejsavage.co.uk>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -21,23 +21,13 @@
#elif defined( __linux__ )
# define PLATFORM_LINUX 1
-# define PLATFORM_HAS_URANDOM 1
#elif defined( __APPLE__ )
-# define PLATFORM_MACOS 1
-
-#elif defined( __FreeBSD__ )
-# define PLATFORM_HAS_ARC4RANDOM 1
-
-#elif defined( __OpenBSD__ )
# define PLATFORM_HAS_ARC4RANDOM 1
-#elif defined( __NetBSD__ )
+#elif defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __NetBSD__ )
# define PLATFORM_HAS_ARC4RANDOM 1
-#elif defined( __sun )
-# define PLATFORM_HAS_URANDOM 1
-
#else
# error new platform
#endif
@@ -45,25 +35,6 @@
#include <stddef.h>
#include <assert.h>
-#if PLATFORM_HAS_URANDOM
-
-#include <stdio.h>
-
-static bool try_urandom( void * buf, size_t n ) {
- assert( n <= 256 );
-
- FILE * f = fopen( "/dev/urandom", "r" );
- if( f == NULL )
- return false;
-
- size_t ok = fread( buf, 1, n, f );
- fclose( f );
-
- return ok == n;
-}
-
-#endif
-
#if PLATFORM_WINDOWS
#pragma comment( lib, "bcrypt.lib" )
@@ -79,37 +50,13 @@ bool ggentropy( void * buf, size_t n ) {
#elif PLATFORM_LINUX
-#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>
bool ggentropy( void * buf, size_t n ) {
assert( n <= 256 );
-
int ok = syscall( SYS_getrandom, buf, n, 0 );
- if( ok >= 0 && size_t( ok ) == n )
- return true;
-
- if( errno != ENOSYS )
- return false;
-
- return try_urandom( buf, n );
-}
-
-#elif PLATFORM_MACOS
-
-#include <sys/random.h>
-
-bool ggentropy( void * buf, size_t n ) {
- assert( n <= 256 );
- return getentropy( buf, n ) == 0;
-}
-
-#elif PLATFORM_HAS_URANDOM
-
-bool ggentropy( void * buf, size_t n ) {
- assert( n <= 256 );
- return try_urandom( buf, n );
+ return ok >= 0 && size_t( ok ) == n;
}
#elif PLATFORM_HAS_ARC4RANDOM