-
Notifications
You must be signed in to change notification settings - Fork 37
Open
Description
Line 302 in 1c0c666
| crypto = require('crypto'); |
The code under randombytes_js_randombytes_nodejs doesn't seem to work for Electron apps webassembly. Since many implementations don't check for return value of randombytes, downstream code was silently passing and causing security issues.
We have an updated version for randombytes_js_randombytes_nodejs that worked for electronjs app as well (as per Mozilla docs, window.crypto is a CSPRNG)
https://developer.mozilla.org/en-US/docs/Web/API/Crypto
`#if defined(EMSCRIPTEN)
static int randombytes_js_randombytes_nodejs(void *buf, size_t n) {
const int ret = EM_ASM_INT({
if (window.crypto && window.crypto.getRandomValues) {
var randBuffer = new Uint8Array($1);
window.crypto.getRandomValues(randBuffer);
writeArrayToMemory(randBuffer, $0);
return 0;
}
var cryptoMod;
try {
cryptoMod = require('crypto');
} catch (error) {
return -2;
}
try {
writeArrayToMemory(cryptoMod.randomBytes($1), $0);
return 0;
} catch (error) {
return -1;
}
}, buf, n);
switch (ret) {
case 0:
return 0;
case -1:
errno = EINVAL;
return -1;
case -2:
errno = ENOSYS;
return -1;
}
return -3;
assert(false); // Unreachable
}
#endif /* defined(EMSCRIPTEN) */`
Metadata
Metadata
Assignees
Labels
No labels