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

Added a randomnumber generator function for WASI to support WebAssembly PQ crypto #405

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions common/randombytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ THE SOFTWARE.
#include <wincrypt.h> /* CryptAcquireContext, CryptGenRandom */
#endif /* defined(_WIN32) */

#if defined(__wasi__)
#include <stdlib.h>
#endif

#if defined(__linux__)
/* Linux */
// We would need to include <linux/random.h>, but not every target has access
Expand Down Expand Up @@ -130,6 +134,13 @@ static int randombytes_linux_randombytes_getrandom(void *buf, size_t n) {
}
#endif /* defined(__linux__) && defined(SYS_getrandom) */

#if defined(__wasi__)
static int randombytes_wasi_randombytes(void *buf, size_t n) {
arc4random_buf(buf, n);
return 0;
}
#endif /* defined(__linux__) && defined(SYS_getrandom) */

#if defined(__linux__) && !defined(SYS_getrandom)
static int randombytes_linux_read_entropy_ioctl(int device, int *entropy) {
return ioctl(device, RNDGETENTCNT, entropy);
Expand Down Expand Up @@ -319,6 +330,9 @@ int randombytes(uint8_t *buf, size_t n) {
#elif defined(_WIN32)
/* Use windows API */
return randombytes_win32_randombytes(buf, n);
#elif defined(__wasi__)
/* Use WASI */
return randombytes_wasi_randombytes(buf, n);
#else
#error "randombytes(...) is not supported on this platform"
#endif
Expand Down