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

Support Electron apps #47

Open
DogeProtocol opened this issue Nov 25, 2023 · 0 comments
Open

Support Electron apps #47

DogeProtocol opened this issue Nov 25, 2023 · 0 comments

Comments

@DogeProtocol
Copy link

DogeProtocol commented Nov 25, 2023

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

Example:
https://github.com/DogeProtocol/hybrid-pqc/blob/d13f9d3944515ccdd7eee4fe98b08562b71564ef/random/randombytes.c#L322C1-L327C4

`#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) */`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant