Using rust RSA library in ICP

So basically i am trying to create a wallet recovery protocol on ICP . That uses Vet keys and DKIM signatures … Issue is that i found a library called viadkim which does the DKIM verification. Issue with this is that , it uses rsa crate , which isn’t support on ICP because i am getting this error when compiling
to wasm32-target-target

error: the wasm*-unknown-unknown targets are not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
   --> /root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/getrandom-0.2.10/src/lib.rs:285:9
    |
285 | /         compile_error!("the wasm*-unknown-unknown targets are not supported by \
286 | |                         default, you may need to enable the \"js\" feature. \
287 | |                         For more information see: \
288 | |                         https://docs.rs/getrandom/#webassembly-support");
    | |________________________________________________________________________^

So now , i would like to know if

  1. Is there an rsa solution in ICP that does at least public key signature verification? ( thats all i need to do with rsa basically )
  2. Is there any way to polyfill my way out of it ( i don’t know much of rust well enough , but in javascript land ,you polyfill your way to support a new run time )
    Some of the dependencies are also nested , how do remove or at least silent those ? . For example the library rand_core is used by rsa and also pcks8.

Hi @agentDPS,

You should be able to get rsa 0.9.6 - Docs.rs working in canister. As your output suggests the issue is the getrandom dependency which attempts to access the randomness system API which is not available with wasm*-unkown-unkown.

For verification of a signature you don’t need any randomness, so for some crates it is enough to disable some of the default features, but it seems this doesn’t work for the rsa crate.

The next approach is to register a custom randomness function with getrandom. You can either define one that uses the IC’s randomness API or create a mock function because as mentioned before your use case doesn’t require randomness. For the latter approach you can have a look at the following example: ic/rs/crypto/getrandom_for_wasm at master · dfinity/ic · GitHub

You need to add the getrandom crate explicitly with the custom feature enabled in your project
getrandom = { version = "0.2", features = ["custom"] }

and then register a custom function like:

pub fn always_fail(_buf: &mut [u8]) -> Result<(), getrandom::Error> {
    Err(getrandom::Error::UNSUPPORTED)
}

getrandom::register_custom_getrandom!(always_fail);

For a random function that will always throw an error if it is called.

The last approach could be to utilize GitHub - wasm-forge/ic-wasi-polyfill: The polyfill implementation for WASI functions in the IC environment and to enable the js feature as mentioned in the error, but I haven’t tried this myself.

1 Like

Awesome thanks . i will try this and let you know soon