[wasm][crypto] RandomNumberGenerator mapped to Web Crypto getRandomValues (#42728)
authorKenneth Pouncey <kjpou@pt.lu>
Thu, 1 Oct 2020 21:44:56 +0000 (23:44 +0200)
committerGitHub <noreply@github.com>
Thu, 1 Oct 2020 21:44:56 +0000 (23:44 +0200)
commite573cacc5b66c045574ceb945aacc36879cf2cf8
tree1444000281be3cd793e960df15eb7656cc566d31
parent07984855684d793091fbd93db4b4ad5982eacb86
[wasm][crypto] RandomNumberGenerator mapped to Web Crypto getRandomValues (#42728)

* [wasm][crypto] RandomNumberGenerator mapped to Web Crypto getRandomValues

- Uses Web Crypto API [`getRandomValues`](https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues) if available.
- Falls back to `/dev/urandom` as default if the crypto library is missing.

* Remove the emscripten interface code from the driver.c.

* remove extraneous code comment

* Move emscripten definition around.

* Address review comment

* Add javascript bridge implementation library to Native source tree.

- Javascript checks for crypto interface and uses `crypto.getRandomValues`
- Add api bridge call when building for emscripten browser.
- separate out into browser subdirectory
- If we couldn't find a proper implementation, as Math.random() is not suitable we will abort.

```

ABORT: no cryptographic support found getRandomValues. Consider polyfilling it if you want to use something insecure like Math.random(), e.g. put this in a --pre-js: var crypto = { getRandomValues: function(array) { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };

```

* Change tests to set random values of the buffer instead of return a single value.

* Remove old test code

* Remove testing code

* Incorporate the PAL bridge layer into the `--js-library` build process

* Address review comments about directory structure and naming

* Update src/mono/wasm/runtime-test.js

Co-authored-by: Ryan Lucia <ryan@luciaonline.net>
* Add note about insecure code for testing purposes

* Formatting

* Return -1 if crypto does not exist instead of aborting from js.  This allows the managed code exception flow to continue as normal.

Co-authored-by: Ryan Lucia <ryan@luciaonline.net>
src/libraries/Native/Unix/System.Native/pal_random.c
src/libraries/Native/Unix/System.Native/pal_random.js [new file with mode: 0644]
src/mono/wasm/Makefile
src/mono/wasm/runtime-test.js
src/mono/wasm/runtime/library_mono.js