-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
based loosely on patch by Hauke Mehrtens; converted to wrap the public API of the underlying getrandom function rather than direct syscalls, so that if/when a fallback implementation of getrandom is added it will automatically get picked up by getentropy too.
- Loading branch information
1 parent
e206582
commit 82f1768
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <sys/random.h> | ||
#include <pthread.h> | ||
#include <errno.h> | ||
|
||
int getentropy(void *buffer, size_t len) | ||
{ | ||
int cs, ret; | ||
char *pos = buffer; | ||
|
||
if (len > 256) { | ||
errno = EIO; | ||
return -1; | ||
} | ||
|
||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); | ||
|
||
while (len) { | ||
ret = getrandom(pos, len, 0); | ||
if (ret < 0) { | ||
if (errno == EINTR) continue; | ||
else break; | ||
} | ||
pos += ret; | ||
len -= ret; | ||
ret = 0; | ||
} | ||
|
||
pthread_setcancelstate(cs, 0); | ||
|
||
return ret; | ||
} |