rust-random / getrandom

A small cross-platform library for retrieving random data from (operating) system source

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

wasm32-unknown-unknown without JS bindgen

kkonevets opened this issue · comments

I am posting this issue also here, because it is related to getrandom. Please comment on.
RustCrypto/nacl-compat#90

TLDR:
Can I somehow get rid of js feature but still be able to compile to wasm32-unknown-unknown?

Yes, as noted in the docs, you can enable the "custom" feature and provide your own RNG implementation.

TLDR: Can I somehow get rid of js feature

Also the "js" feature is off by default, so it's mostly about not enabling that feature.

Yes, as noted in the docs, you can enable the "custom" feature and provide your own RNG implementation.

I have seen that, but I don't know how to do that. Is there some example of using custom RNG implementation?

Yes, as noted in the docs, you can enable the "custom" feature and provide your own RNG implementation.

I have seen that, but I don't know how to do that. Is there some example of using custom RNG implementation?

Is there part of the documentation for this crate that is unclear?

TLDR: Can I somehow get rid of js feature

Also the "js" feature is off by default, so it's mostly about not enabling that feature.

But js feature is the only way to target wasm32-unknown-unknown, the defaults don't work

But js feature is the only way to target wasm32-unknown-unknown

This is incorrect. You have to enable some implementation in order for wasm32-unknown-unknown to work.

  • If you enable the js feature, you will use the JavaScript bindgen implementation
  • If you enable the custom feature, you will use your own implementation provided via register_custom_getrandom
  • If you don't enable either feature, this crate will not compile when targeting wasm32-unknown-unknown

I have just enabled custom feature and all my tests are passing. Any idea why? I use getrandom with crypto-box. Does it mean that crypto-box does not use getrandom implementation? You know, I generate lots of random public, secret keys and nonces. I am not a cryptographer, sorry.

UPDATE:
I don't register custom RNG with register_custom_getrandom but it still works ...

I have just enabled custom feature and all my tests are passing. Any idea why?

I would make sure that you are:

  • Actually targeting wasm32-unknown-unknown
  • Actually generating random keys via getrandom

You should get a linker error if you try to call the custom implementation and one is not registered.

I am sorry, it works for std, but fails in wasm32-unknown-unknown with

Uncaught (in promise) LinkError: WebAssembly.instantiate(): 
Import #0 module="env" function="__getrandom_custom" error: function import requires a callable
await (async)
(anonymous) @ (index):55

Is there any usable implementation of custom RNG other then use failure_getrandom::always_fail? The point is that I want getrandom to give me randomness, but now it does not.

That linker error is what I would have expected. You need to use register_custom_getrandom to fix that error.

Is there any usable implementation of custom RNG other then use failure_getrandom::always_fail?

You need a platform specific implementation. Generating cryptographic randomness can be a complicated problem. You may need to write your own implementation that invokes some platform intrinsic (for example). There isn't a general solution to this problem.

Thanks! I got it.
For those who will read this issue, here is what you should do.

use getrandom::{self, register_custom_getrandom};

extern "C" {
	// call host OS random number generator
	fn random_bytes(dest: *mut u8, len: usize);
}

pub fn getrandom_custom(dest: &mut [u8]) -> Result<(), getrandom::Error> {
	unsafe { random_bytes(dest.as_mut_ptr(), dest.len()) };
	Ok(())
}

register_custom_getrandom!(getrandom_custom);

If you use wasmer, then you should create an import object with FunctionEnv that calls host OS random number generator