jsonwebtoken icon indicating copy to clipboard operation
jsonwebtoken copied to clipboard

RNG dependency injection into signature operations

Open jnicholls opened this issue 3 years ago • 6 comments

I think it would be useful for certain operations (encode, sign, etc.) to have an alternative method that accepts a ring::rand::SecureRandom RNG provider as an argument, or some other means to inject that dependency. Internally where an RNG is used, the crate currently creates a SystemRandom on demand. For cases where a different CSRNG (e.g. a different source of entropy) is deisred, or in mock testing scenarios where deterministic signature output is desired, this feature would be very useful.

I don't have an opinion on the surface-level API. Perhaps encode_with_rng and sign_with_rng. For the RNG itself, it would make the most sense to create a trait owned by this crate e.g. jsonwebtoken::crypto::Rng w/ the same sort of interface as ring::rand::SecureRandom, and do a blanket impl<R> Rng for R where R: ring::rand::SecureRandom { ... }.

jnicholls avatar Apr 12 '21 19:04 jnicholls

I'm not sure it makes sense. Where would this be needed except from WASM? If you want reproducible JWTs for tests, don't use RSA or EcDSA

Keats avatar Apr 12 '21 21:04 Keats

WASI and Node.js WASM, since you mentioned it.

jnicholls avatar Apr 12 '21 22:04 jnicholls

Buuuut, since you mentioned that I'll check in with Brian and see if we can just add those targets to ring directly.

jnicholls avatar Apr 12 '21 22:04 jnicholls

Would love to be able to use this crate with WASI as well

aweis89 avatar Jun 11 '21 20:06 aweis89

@jnicholls any update from the ring side?

Keats avatar Jun 11 '21 21:06 Keats

Unfortunately no, the only WASM target that is supported are browser environments with crypto.getRandomValues() available. WASI's random_get method is still not yet supported.

jnicholls avatar Jun 14 '21 11:06 jnicholls