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

[wasm][crypto] RandomNumberGenerator mapped to Web Crypto getRandomValues #42728

Merged
merged 19 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f0a662c
[wasm][crypto] RandomNumberGenerator mapped to Web Crypto getRandomVa…
kjpou1 Sep 25, 2020
eb46e2f
Remove the emscripten interface code from the driver.c.
kjpou1 Sep 28, 2020
ce149c9
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Sep 28, 2020
878cfcf
remove extraneous code comment
kjpou1 Sep 28, 2020
289efef
Move emscripten definition around.
kjpou1 Sep 28, 2020
b5d372b
Address review comment
kjpou1 Sep 28, 2020
f657f4f
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Sep 29, 2020
cf74a12
Add javascript bridge implementation library to Native source tree.
kjpou1 Sep 29, 2020
88fa846
Change tests to set random values of the buffer instead of return a s…
kjpou1 Sep 29, 2020
2b8987d
Remove old test code
kjpou1 Sep 29, 2020
61f1c19
Remove testing code
kjpou1 Sep 29, 2020
ce13ad1
Incorporate the PAL bridge layer into the `--js-library` build process
kjpou1 Sep 29, 2020
4eb6639
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Sep 30, 2020
5e593bb
Address review comments about directory structure and naming
kjpou1 Sep 30, 2020
76de399
Update src/mono/wasm/runtime-test.js
kjpou1 Oct 1, 2020
75a81ce
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Oct 1, 2020
8077f7c
Add note about insecure code for testing purposes
kjpou1 Oct 1, 2020
ec16f1a
Formatting
kjpou1 Oct 1, 2020
7b7e9d2
Return -1 if crypto does not exist instead of aborting from js. This…
kjpou1 Oct 1, 2020
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
16 changes: 16 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* buffer, int3
#endif // HAVE_ARC4RANDOM_BUF
}

#ifdef __EMSCRIPTEN__
int32_t mono_wasm_browser_crypto_getRandomValues(uint8_t* buffer, int32_t bufferLength);
kjpou1 marked this conversation as resolved.
Show resolved Hide resolved
#endif

/*

Generate cryptographically strong random bytes.
Expand All @@ -68,6 +72,18 @@ int32_t SystemNative_GetCryptographicallySecureRandomBytes(uint8_t* buffer, int3

assert(buffer != NULL);

#ifdef __EMSCRIPTEN__
static bool sMissingBrowserCrypto;
marek-safar marked this conversation as resolved.
Show resolved Hide resolved
if (!sMissingBrowserCrypto)
{
int32_t bff = mono_wasm_browser_crypto_getRandomValues(buffer, bufferLength);
if (bff == -1)
sMissingBrowserCrypto = true;
else
return 0;
}
#endif
kjpou1 marked this conversation as resolved.
Show resolved Hide resolved

if (!sMissingDevURandom)
{
if (rand_des == -1)
Expand Down
12 changes: 12 additions & 0 deletions src/mono/wasm/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -940,3 +940,15 @@ mono_timezone_get_local_name (MonoString **result)
*result = mono_string_from_utf16 (tzd_local_name);
free (tzd_local_name);
}

EM_JS(int32_t, mono_wasm_browser_crypto_getRandomValues, (uint8_t* buffer, int32_t bufferLength),
{
// check that we have crypto available
if (!(typeof crypto !== 'undefined' && crypto.subtle && typeof crypto.getRandomValues === 'function')) {
return -1;
}
// map the work array to the memory buffer passed with the length
var wrkArray = new Uint8Array(Module.HEAPU8.buffer, buffer, bufferLength);
crypto.getRandomValues(wrkArray);
return 0;
});