Skip to content

Commit 5a53f54

Browse files
authored
Merge pull request #34 from john-sharratt/master
Added support for random numbers generated on WASI
2 parents a29d979 + 40cddfd commit 5a53f54

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

randombytes.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
# include <wincrypt.h> /* CryptAcquireContext, CryptGenRandom */
1414
#endif /* defined(_WIN32) */
1515

16+
/* wasi */
17+
#if defined(__wasi__)
18+
#include <stdlib.h>
19+
#endif
1620

1721
#if defined(__linux__)
1822
/* Linux */
@@ -78,6 +82,12 @@ static int randombytes_win32_randombytes(void* buf, const size_t n)
7882
}
7983
#endif /* defined(_WIN32) */
8084

85+
#if defined(__wasi__)
86+
static int randombytes_wasi_randombytes(void *buf, size_t n) {
87+
arc4random_buf(buf, n);
88+
return 0;
89+
}
90+
#endif /* defined(__wasi__) */
8191

8292
#if defined(__linux__) && defined(SYS_getrandom)
8393
static int randombytes_linux_randombytes_getrandom(void *buf, size_t n)
@@ -302,6 +312,10 @@ int randombytes(void *buf, size_t n)
302312
# pragma message("Using Windows cryptographic API")
303313
/* Use windows API */
304314
return randombytes_win32_randombytes(buf, n);
315+
#elif defined(__wasi__)
316+
# pragma message("Using WASI arc4random_buf system call")
317+
/* Use WASI */
318+
return randombytes_wasi_randombytes(buf, n);
305319
#else
306320
# error "randombytes(...) is not supported on this platform"
307321
#endif

0 commit comments

Comments
 (0)