diff options
| author | beatrice <beatrice@dangofactory.net> | 2026-11-22 20:03:00 +0900 |
|---|---|---|
| committer | beatrice <beatrice@dangofactory.net> | 2026-11-22 20:03:00 +0900 |
| commit | 2d8ae60f18b379215be576a0d533e28fa28e1bf1 (patch) | |
| tree | 47cd73ec5fd53931d2dcdd634a9e7687c4449185 /src/libsodium/randombytes/randombytes.c | |
| parent | 77e1ce5d6dee871c49ef211222ba18ef0c486bda (diff) | |
| download | libsodium-master.tar.gz libsodium-master.zip | |
Release tarball libsodium.tar.gz (dist-generated files / local differences from tag)HEADlocal/libsodiummaster
Diffstat (limited to 'src/libsodium/randombytes/randombytes.c')
| -rw-r--r-- | src/libsodium/randombytes/randombytes.c | 54 |
1 files changed, 18 insertions, 36 deletions
diff --git a/src/libsodium/randombytes/randombytes.c b/src/libsodium/randombytes/randombytes.c index dfb6b355..68583c65 100644 --- a/src/libsodium/randombytes/randombytes.c +++ b/src/libsodium/randombytes/randombytes.c @@ -7,7 +7,8 @@ #include <sys/types.h> #ifdef __EMSCRIPTEN__ -# include <emscripten.h> +# include <unistd.h> +# include <wasi/api.h> #endif #include "core.h" @@ -44,52 +45,33 @@ javascript_implementation_name(void) static uint32_t javascript_random(void) { - return EM_ASM_INT_V({ - return Module.getRandomValue(); - }); + uint32_t r; + + if (getentropy(&r, sizeof r) != 0) { + sodium_misuse(); + } + return r; } static void javascript_stir(void) { - EM_ASM({ - if (Module.getRandomValue === undefined) { - try { - var window_ = 'object' === typeof window ? window : self; - var crypto_ = typeof window_.crypto !== 'undefined' ? window_.crypto : window_.msCrypto; - crypto_ = (crypto_ === undefined) ? crypto : crypto_; - var randomValuesStandard = function() { - var buf = new Uint32Array(1); - crypto_.getRandomValues(buf); - return buf[0] >>> 0; - }; - randomValuesStandard(); - Module.getRandomValue = randomValuesStandard; - } catch (e) { - try { - var crypto = require('crypto'); - var randomValueNodeJS = function() { - var buf = crypto['randomBytes'](4); - return (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]) >>> 0; - }; - randomValueNodeJS(); - Module.getRandomValue = randomValueNodeJS; - } catch (e) { - throw 'No secure random number generator found'; - } - } - } - }); + (void) javascript_random(); } static void -javascript_buf(void * const buf, const size_t size) +javascript_buf(void * const buf, size_t size) { unsigned char *p = (unsigned char *) buf; - size_t i; + size_t chunk_size; - for (i = (size_t) 0U; i < size; i++) { - p[i] = (unsigned char) randombytes_random(); + while (size > (size_t) 0U) { + chunk_size = size > 65536U ? 65536U : size; + if (__wasi_random_get(p, chunk_size) != 0) { + sodium_misuse(); + } + p += chunk_size; + size -= chunk_size; } } #endif |
