summaryrefslogtreecommitdiff
path: root/lib/getrandom.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/getrandom.c')
-rw-r--r--lib/getrandom.c34
1 files changed, 23 insertions, 11 deletions
diff --git a/lib/getrandom.c b/lib/getrandom.c
index f0b3f53500..44283cf181 100644
--- a/lib/getrandom.c
+++ b/lib/getrandom.c
@@ -1,18 +1,18 @@
/* Obtain a series of random bytes.
- Copyright 2020 Free Software Foundation, Inc.
+ Copyright 2020-2024 Free Software Foundation, Inc.
- 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 3 of the License, or
- (at your option) any later version.
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
+ This file 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.
+ GNU Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Paul Eggert. */
@@ -23,13 +23,21 @@
#include <errno.h>
#include <fcntl.h>
-#include <stdbool.h>
#include <unistd.h>
#if defined _WIN32 && ! defined __CYGWIN__
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
-# include <bcrypt.h>
+# if HAVE_BCRYPT_H
+# include <bcrypt.h>
+# else
+# define NTSTATUS LONG
+typedef void * BCRYPT_ALG_HANDLE;
+# define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
+# if HAVE_LIB_BCRYPT
+extern NTSTATUS WINAPI BCryptGenRandom (BCRYPT_ALG_HANDLE, UCHAR *, ULONG, ULONG);
+# endif
+# endif
# if !HAVE_LIB_BCRYPT
# include <wincrypt.h>
# ifndef CRYPT_VERIFY_CONTEXT
@@ -169,7 +177,11 @@ getrandom (void *buffer, size_t length, unsigned int flags)
+ (flags & GRND_NONBLOCK ? O_NONBLOCK : 0));
fd = open (randdevice[devrandom], oflags);
if (fd < 0)
- return fd;
+ {
+ if (errno == ENOENT || errno == ENOTDIR)
+ errno = ENOSYS;
+ return -1;
+ }
randfd[devrandom] = fd;
}

Return to:

Send suggestions and report system problems to the System administrator.