diff options
| author | Nils Maier <maierman@web.de> | 2014-12-30 18:17:44 +0100 |
|---|---|---|
| committer | Nils Maier <maierman@web.de> | 2015-01-20 22:37:24 +0100 |
| commit | 81bdd5f61a8f8da53e22d68190be700902b7dbae (patch) | |
| tree | f706934a5e5e208d8d09490d216fe03f10b38654 /src/getrandom_linux.c | |
| parent | 747131a06c1f1d92a5312a103f4446fabedb6cf7 (diff) | |
Revise getRandom facilities
Use one of the following to provide random bytes:
- Windows CryptGenRandom
- Linux getrandom (syscall interface to urandom, without nasty corner
cases such as file descriptor exhaustion or re-linked /dev/urandom)
- std::device_random (C++ random device, which usually will be urandom)
This also equalizes util::getRandom and SimpleRandomizer (the former
will now use the latter) instead of having essentially two different
PRNG interfaces with potentially different quality.
Closes GH-320
Diffstat (limited to 'src/getrandom_linux.c')
| -rw-r--r-- | src/getrandom_linux.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/getrandom_linux.c b/src/getrandom_linux.c new file mode 100644 index 00000000..85314fd6 --- /dev/null +++ b/src/getrandom_linux.c @@ -0,0 +1,50 @@ +/* <!-- copyright */ +/* + * aria2 - The high speed download utility + * + * Copyright (C) 2014 Nils Maier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations + * including the two. + * You must obey the GNU General Public License in all respects + * for all of the code used other than OpenSSL. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you + * do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source + * files in the program, then also delete it here. + */ +/* copyright --> */ + +#define _GNUSOURCE +#include <unistd.h> +#include <sys/syscall.h> +#include <linux/random.h> + +#include "config.h" +#include "getrandom_linux.h" + +int getrandom_linux(void *buf, size_t buflen) { +#ifdef HAVE_GETRANDOM + return getrandom(buf, buflen, 0); +#else // HAVE_GETRANDOM + return syscall(SYS_getrandom, buf, buflen, 0); +#endif // HAVE_GETRANDOM +} |
